View source
<?php
namespace Drupal\file\Plugin\migrate\source\d7;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
class File extends DrupalSqlBase {
protected $publicPath;
protected $privatePath;
public function query() {
$query = $this
->select('file_managed', 'f')
->fields('f')
->condition('f.uri', 'temporary://%', 'NOT LIKE')
->orderBy('f.timestamp');
if (isset($this->configuration['scheme'])) {
$schemes = [];
$valid_schemes = array_diff((array) $this->configuration['scheme'], [
'temporary',
]);
foreach ((array) $valid_schemes as $scheme) {
$schemes[] = rtrim($scheme) . '://';
}
$schemes = array_map([
$this
->getDatabase(),
'escapeLike',
], $schemes);
$conditions = $this
->getDatabase()
->condition('OR');
foreach ($schemes as $scheme) {
$conditions
->condition('f.uri', $scheme . '%', 'LIKE');
}
$query
->condition($conditions);
}
return $query;
}
protected function initializeIterator() {
$this->publicPath = $this
->variableGet('file_public_path', 'sites/default/files');
$this->privatePath = $this
->variableGet('file_private_path', NULL);
return parent::initializeIterator();
}
public function prepareRow(Row $row) {
$path = str_replace([
'public:/',
'private:/',
], [
$this->publicPath,
$this->privatePath,
], $row
->getSourceProperty('uri'));
$path = preg_replace('#' . preg_quote($this->configuration['constants']['source_base_path']) . '#', '', $path, 1);
$row
->setSourceProperty('filepath', $path);
return parent::prepareRow($row);
}
public function fields() {
return [
'fid' => $this
->t('File ID'),
'uid' => $this
->t('The {users}.uid who added the file. If set to 0, this file was added by an anonymous user.'),
'filename' => $this
->t('File name'),
'filepath' => $this
->t('File path'),
'filemime' => $this
->t('File MIME Type'),
'status' => $this
->t('The published status of a file.'),
'timestamp' => $this
->t('The time that the file was added.'),
];
}
public function getIds() {
$ids['fid']['type'] = 'integer';
$ids['fid']['alias'] = 'f';
return $ids;
}
}