View source
<?php
namespace Drupal\media_migration\Plugin\migrate\source\d7;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Driver\sqlite\Connection as SqLiteConnection;
use Drupal\Core\Database\Driver\pgsql\Connection as PostgreSqlConnection;
trait MediaMigrationDatabaseTrait {
protected function getFilePlainBaseQuery($connection = NULL, bool $distinct = TRUE) {
$db = $connection ?? $this
->getDatabase();
assert($db instanceof Connection);
$options = [
'fetch' => \PDO::FETCH_ASSOC,
];
$query = $db
->select('file_managed', 'fm', $options);
$query
->distinct($distinct);
$query
->condition('fm.status', TRUE);
$query
->addExpression($this
->getSchemeExpression($db), 'scheme');
$query
->addExpression($this
->getMainMimeTypeExpression($db), 'mime');
$query
->where("{$this->getSchemeExpression($db)} <> 'temporary'");
$query
->condition('fm.fid', $this
->getUserPictureOnlyFidsQuery($db), 'NOT IN');
$query
->condition('fm.fid', $this
->getWebformOrUserPictureOnlyFidsQuery($db), 'NOT IN');
return $query;
}
protected function getFileEntityBaseQuery($connection = NULL, bool $distinct = TRUE) {
$db = $connection ?? $this
->getDatabase();
assert($db instanceof Connection);
$options = [
'fetch' => \PDO::FETCH_ASSOC,
];
$query = $db
->select('file_managed', 'fm', $options);
if ($distinct) {
$query
->distinct();
}
$query
->fields('fm', [
'type',
])
->condition('fm.status', TRUE)
->condition('fm.uri', 'temporary://%', 'NOT LIKE')
->condition('fm.type', 'undefined', '<>');
$query
->addExpression($this
->getSchemeExpression($db), 'scheme');
$query
->condition('fm.fid', $this
->getUserPictureOnlyFidsQuery($db), 'NOT IN');
$query
->condition('fm.fid', $this
->getWebformOrUserPictureOnlyFidsQuery($db), 'NOT IN');
return $query;
}
protected function getSchemeExpression($connection = NULL) {
$db = $connection ?? $this
->getDatabase();
assert($db instanceof Connection);
return $db instanceof SqLiteConnection ? "SUBSTRING(fm.uri, 1, INSTR(fm.uri, '://') - 1)" : "SUBSTRING(fm.uri, 1, POSITION('://' IN fm.uri) - 1)";
}
protected function getMainMimeTypeExpression($connection = NULL) {
$db = $connection ?? $this
->getDatabase();
assert($db instanceof Connection);
return $db instanceof SqLiteConnection ? "SUBSTRING(fm.filemime, 1, INSTR(fm.filemime, '/') - 1)" : "SUBSTRING(fm.filemime, 1, POSITION('/' IN fm.filemime) - 1)";
}
protected function getExtensionExpression($connection = NULL) {
$db = $connection ?? $this
->getDatabase();
assert($db instanceof Connection);
return $db instanceof SqLiteConnection ? "REPLACE(fm.uri, RTRIM(fm.uri, REPLACE(fm.uri, '.', '')), '')" : "SUBSTRING(fm.uri FROM CHAR_LENGTH(fm.uri) - POSITION('.' IN REVERSE(fm.uri)) + 2)";
}
protected function getUserPictureOnlyFidsQuery(Connection $connection) {
$query = $connection
->select('users', 'u');
$query
->leftJoin('file_usage', 'fu', 'fu.fid = u.picture');
$query
->where('u.picture > 0');
$query
->fields('fu', [
'fid',
]);
$query
->groupBy('fu.fid');
$concat_expression = $connection instanceof PostgreSqlConnection ? "STRING_AGG(DISTINCT fu.type, ',')" : "GROUP_CONCAT(DISTINCT fu.type)";
$query
->having("{$concat_expression} = :allowed_value_user_only OR {$concat_expression} = :allowed_value_user_webform_only OR {$concat_expression} = :allowed_value_webform_user_only", [
':allowed_value_user_only' => 'user',
':allowed_value_user_webform_only' => 'user,webform',
':allowed_value_webform_user_only' => 'webform,user',
]);
return $query;
}
protected function getWebformOrUserPictureOnlyFidsQuery(Connection $connection) {
$query = $connection
->select('file_usage', 'fu');
$query
->fields('fu', [
'fid',
]);
$query
->groupBy('fu.fid');
$wf_type_concat_expression = $connection instanceof PostgreSqlConnection ? "STRING_AGG(DISTINCT fu.type, ',')" : "GROUP_CONCAT(DISTINCT fu.type)";
$query
->having("{$wf_type_concat_expression} = :allowed_type_submission_only OR {$wf_type_concat_expression} = :allowed_type_submission_user_only OR {$wf_type_concat_expression} = :allowed_type_user_submission_only", [
':allowed_type_submission_only' => 'submission',
':allowed_type_submission_user_only' => 'submission,user',
':allowed_type_user_submission_only' => 'user,submission',
]);
return $query;
}
}