3 namespace Drupal\file\Plugin\migrate\source\d7;
5 use Drupal\Core\Database\Query\Condition;
6 use Drupal\migrate\Row;
7 use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
10 * Drupal 7 file source from database.
14 * source_module = "file"
17 class File extends DrupalSqlBase {
20 * The public file directory path.
24 protected $publicPath;
27 * The private file directory path, if any.
31 protected $privatePath;
34 * The temporary file directory path.
38 protected $temporaryPath;
43 public function query() {
44 $query = $this->select('file_managed', 'f')
46 ->condition('uri', 'temporary://%', 'NOT LIKE')
47 ->orderBy('f.timestamp');
49 // Filter by scheme(s), if configured.
50 if (isset($this->configuration['scheme'])) {
52 // Remove 'temporary' scheme.
53 $valid_schemes = array_diff((array) $this->configuration['scheme'], ['temporary']);
54 // Accept either a single scheme, or a list.
55 foreach ((array) $valid_schemes as $scheme) {
56 $schemes[] = rtrim($scheme) . '://';
58 $schemes = array_map([$this->getDatabase(), 'escapeLike'], $schemes);
60 // Add conditions, uri LIKE 'public://%' OR uri LIKE 'private://%'.
61 $conditions = new Condition('OR');
62 foreach ($schemes as $scheme) {
63 $conditions->condition('uri', $scheme . '%', 'LIKE');
65 $query->condition($conditions);
74 protected function initializeIterator() {
75 $this->publicPath = $this->variableGet('file_public_path', 'sites/default/files');
76 $this->privatePath = $this->variableGet('file_private_path', NULL);
77 $this->temporaryPath = $this->variableGet('file_temporary_path', '/tmp');
78 return parent::initializeIterator();
84 public function prepareRow(Row $row) {
85 // Compute the filepath property, which is a physical representation of
86 // the URI relative to the Drupal root.
87 $path = str_replace(['public:/', 'private:/', 'temporary:/'], [$this->publicPath, $this->privatePath, $this->temporaryPath], $row->getSourceProperty('uri'));
88 // At this point, $path could be an absolute path or a relative path,
89 // depending on how the scheme's variable was set. So we need to shear out
90 // the source_base_path in order to make them all relative.
91 $path = str_replace($this->configuration['constants']['source_base_path'], NULL, $path);
92 $row->setSourceProperty('filepath', $path);
93 return parent::prepareRow($row);
99 public function fields() {
101 'fid' => $this->t('File ID'),
102 'uid' => $this->t('The {users}.uid who added the file. If set to 0, this file was added by an anonymous user.'),
103 'filename' => $this->t('File name'),
104 'filepath' => $this->t('File path'),
105 'filemime' => $this->t('File MIME Type'),
106 'status' => $this->t('The published status of a file.'),
107 'timestamp' => $this->t('The time that the file was added.'),
114 public function getIds() {
115 $ids['fid']['type'] = 'integer';