Version 1
[yaffs-website] / web / core / modules / file / src / Plugin / migrate / source / d7 / File.php
1 <?php
2
3 namespace Drupal\file\Plugin\migrate\source\d7;
4
5 use Drupal\Core\Database\Query\Condition;
6 use Drupal\migrate\Row;
7 use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
8
9 /**
10  * Drupal 7 file source from database.
11  *
12  * @MigrateSource(
13  *   id = "d7_file"
14  * )
15  */
16 class File extends DrupalSqlBase {
17
18   /**
19    * The public file directory path.
20    *
21    * @var string
22    */
23   protected $publicPath;
24
25   /**
26    * The private file directory path, if any.
27    *
28    * @var string
29    */
30   protected $privatePath;
31
32   /**
33    * The temporary file directory path.
34    *
35    * @var string
36    */
37   protected $temporaryPath;
38
39   /**
40    * {@inheritdoc}
41    */
42   public function query() {
43     $query = $this->select('file_managed', 'f')
44       ->fields('f')
45       ->orderBy('f.timestamp');
46
47     // Filter by scheme(s), if configured.
48     if (isset($this->configuration['scheme'])) {
49       $schemes = [];
50       // Accept either a single scheme, or a list.
51       foreach ((array) $this->configuration['scheme'] as $scheme) {
52         $schemes[] = rtrim($scheme) . '://';
53       }
54       $schemes = array_map([$this->getDatabase(), 'escapeLike'], $schemes);
55
56       // uri LIKE 'public://%' OR uri LIKE 'private://%'
57       $conditions = new Condition('OR');
58       foreach ($schemes as $scheme) {
59         $conditions->condition('uri', $scheme . '%', 'LIKE');
60       }
61       $query->condition($conditions);
62     }
63
64     return $query;
65   }
66
67   /**
68    * {@inheritdoc}
69    */
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();
75   }
76
77   /**
78    * {@inheritdoc}
79    */
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);
90   }
91
92   /**
93    * {@inheritdoc}
94    */
95   public function fields() {
96     return [
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.'),
104     ];
105   }
106
107   /**
108    * {@inheritdoc}
109    */
110   public function getIds() {
111     $ids['fid']['type'] = 'integer';
112     return $ids;
113   }
114
115 }