Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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  *   source_module = "file"
15  * )
16  */
17 class File extends DrupalSqlBase {
18
19   /**
20    * The public file directory path.
21    *
22    * @var string
23    */
24   protected $publicPath;
25
26   /**
27    * The private file directory path, if any.
28    *
29    * @var string
30    */
31   protected $privatePath;
32
33   /**
34    * The temporary file directory path.
35    *
36    * @var string
37    */
38   protected $temporaryPath;
39
40   /**
41    * {@inheritdoc}
42    */
43   public function query() {
44     $query = $this->select('file_managed', 'f')
45       ->fields('f')
46       ->condition('uri', 'temporary://%', 'NOT LIKE')
47       ->orderBy('f.timestamp');
48
49     // Filter by scheme(s), if configured.
50     if (isset($this->configuration['scheme'])) {
51       $schemes = [];
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) . '://';
57       }
58       $schemes = array_map([$this->getDatabase(), 'escapeLike'], $schemes);
59
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');
64       }
65       $query->condition($conditions);
66     }
67
68     return $query;
69   }
70
71   /**
72    * {@inheritdoc}
73    */
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();
79   }
80
81   /**
82    * {@inheritdoc}
83    */
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);
94   }
95
96   /**
97    * {@inheritdoc}
98    */
99   public function fields() {
100     return [
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.'),
108     ];
109   }
110
111   /**
112    * {@inheritdoc}
113    */
114   public function getIds() {
115     $ids['fid']['type'] = 'integer';
116     return $ids;
117   }
118
119 }