MigrationCreationTrait.php in Migrate Upgrade 8
File
src/MigrationCreationTrait.php
View source
<?php
namespace Drupal\migrate_upgrade;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Database;
use Drupal\migrate\Entity\Migration;
use Drupal\migrate\Exception\RequirementsException;
use Drupal\migrate\Plugin\RequirementsInterface;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
trait MigrationCreationTrait {
protected function getConnection(array $database) {
Database::addConnectionInfo('upgrade', 'default', $database);
$connection = Database::getConnection('default', 'upgrade');
return $connection;
}
protected function getSystemData(array $database) {
$connection = $this
->getConnection($database);
$system_data = [];
try {
$results = $connection
->select('system', 's', [
'fetch' => \PDO::FETCH_ASSOC,
])
->fields('s')
->execute();
foreach ($results as $result) {
$system_data[$result['type']][$result['name']] = $result;
}
} catch (\Exception $e) {
}
return $system_data;
}
protected function getMigrationTemplates(array $database, $source_base_path) {
$connection = $this
->getConnection($database);
if (!($drupal_version = $this
->getLegacyDrupalVersion($connection))) {
throw new \Exception('Source database does not contain a recognizable Drupal version.');
}
$database_state['key'] = 'upgrade';
$database_state['database'] = $database;
$database_state_key = 'migrate_upgrade_' . $drupal_version;
\Drupal::state()
->set($database_state_key, $database_state);
$version_tag = 'Drupal ' . $drupal_version;
$template_storage = \Drupal::service('migrate.template_storage');
$migration_templates = $template_storage
->findTemplatesByTag($version_tag);
foreach ($migration_templates as $id => $template) {
$migration_templates[$id]['source']['database_state_key'] = $database_state_key;
if ($template['destination']['plugin'] == 'entity:file') {
if ($source_base_path) {
$source_base_path = rtrim($source_base_path, '/') . '/';
$migration_templates[$id]['destination']['source_base_path'] = $source_base_path;
}
}
}
return $migration_templates;
}
protected function getMigrations(array $migration_templates) {
$builder = \Drupal::service('migrate.migration_builder');
$initial_migrations = $builder
->createMigrations($migration_templates);
$migrations = [];
foreach ($initial_migrations as $migration) {
try {
$source_plugin = $migration
->getSourcePlugin();
if ($source_plugin instanceof RequirementsInterface) {
$source_plugin
->checkRequirements();
}
$destination_plugin = $migration
->getDestinationPlugin();
if ($destination_plugin instanceof RequirementsInterface) {
$destination_plugin
->checkRequirements();
}
$migrations[] = $migration;
} catch (RequirementsException $e) {
} catch (PluginNotFoundException $e) {
}
}
return $migrations;
}
protected function createMigrations(array $migration_templates) {
$migration_ids = [];
$migrations = $this
->getMigrations($migration_templates);
foreach ($migrations as $migration) {
if (!Migration::load($migration
->id())) {
$migration
->save();
}
$migration_ids[] = $migration
->id();
}
return array_keys(Migration::loadMultiple($migration_ids));
}
protected function getLegacyDrupalVersion(Connection $connection) {
if ($connection
->schema()
->tableExists('system')) {
try {
$version_string = $connection
->query('SELECT schema_version FROM {system} WHERE name = :module', [
':module' => 'system',
])
->fetchField();
if ($version_string && $version_string[0] == '1') {
if ((int) $version_string >= 1000) {
$version_string = '5';
}
else {
$version_string = FALSE;
}
}
} catch (\PDOException $e) {
$version_string = FALSE;
}
}
elseif ($connection
->schema()
->tableExists('key_value')) {
$result = $connection
->query("SELECT value FROM {key_value} WHERE collection = :system_schema and name = :module", [
':system_schema' => 'system.schema',
':module' => 'system',
])
->fetchField();
$version_string = unserialize($result);
}
else {
$version_string = FALSE;
}
return $version_string ? substr($version_string, 0, 1) : FALSE;
}
}