Security update for Core, with self-updated composer
[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       ->orderBy('f.timestamp');
47
48     // Filter by scheme(s), if configured.
49     if (isset($this->configuration['scheme'])) {
50       $schemes = [];
51       // Accept either a single scheme, or a list.
52       foreach ((array) $this->configuration['scheme'] as $scheme) {
53         $schemes[] = rtrim($scheme) . '://';
54       }
55       $schemes = array_map([$this->getDatabase(), 'escapeLike'], $schemes);
56
57       // uri LIKE 'public://%' OR uri LIKE 'private://%'
58       $conditions = new Condition('OR');
59       foreach ($schemes as $scheme) {
60         $conditions->condition('uri', $scheme . '%', 'LIKE');
61       }
62       $query->condition($conditions);
63     }
64
65     return $query;
66   }
67
68   /**
69    * {@inheritdoc}
70    */
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();
76   }
77
78   /**
79    * {@inheritdoc}
80    */
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);
91   }
92
93   /**
94    * {@inheritdoc}
95    */
96   public function fields() {
97     return [
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.'),
105     ];
106   }
107
108   /**
109    * {@inheritdoc}
110    */
111   public function getIds() {
112     $ids['fid']['type'] = 'integer';
113     return $ids;
114   }
115
116 }