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 ->orderBy('f.timestamp');
48 // Filter by scheme(s), if configured.
49 if (isset($this->configuration['scheme'])) {
51 // Accept either a single scheme, or a list.
52 foreach ((array) $this->configuration['scheme'] as $scheme) {
53 $schemes[] = rtrim($scheme) . '://';
55 $schemes = array_map([$this->getDatabase(), 'escapeLike'], $schemes);
57 // uri LIKE 'public://%' OR uri LIKE 'private://%'
58 $conditions = new Condition('OR');
59 foreach ($schemes as $scheme) {
60 $conditions->condition('uri', $scheme . '%', 'LIKE');
62 $query->condition($conditions);
71 protected function initializeIterator() {
72 $this->publicPath = $this->variableGet('file_public_path', 'sites/default/files');
73 $this->privatePath = $this->variableGet('file_private_path', NULL);
74 $this->temporaryPath = $this->variableGet('file_temporary_path', '/tmp');
75 return parent::initializeIterator();
81 public function prepareRow(Row $row) {
82 // Compute the filepath property, which is a physical representation of
83 // the URI relative to the Drupal root.
84 $path = str_replace(['public:/', 'private:/', 'temporary:/'], [$this->publicPath, $this->privatePath, $this->temporaryPath], $row->getSourceProperty('uri'));
85 // At this point, $path could be an absolute path or a relative path,
86 // depending on how the scheme's variable was set. So we need to shear out
87 // the source_base_path in order to make them all relative.
88 $path = str_replace($this->configuration['constants']['source_base_path'], NULL, $path);
89 $row->setSourceProperty('filepath', $path);
90 return parent::prepareRow($row);
96 public function fields() {
98 'fid' => $this->t('File ID'),
99 'uid' => $this->t('The {users}.uid who added the file. If set to 0, this file was added by an anonymous user.'),
100 'filename' => $this->t('File name'),
101 'filepath' => $this->t('File path'),
102 'filemime' => $this->t('File MIME Type'),
103 'status' => $this->t('The published status of a file.'),
104 'timestamp' => $this->t('The time that the file was added.'),
111 public function getIds() {
112 $ids['fid']['type'] = 'integer';