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