diff --git a/core/modules/migrate_drupal/src/MigrationConfigurationTrait.php b/core/modules/migrate_drupal/src/MigrationConfigurationTrait.php new file mode 100644 index 0000000..a7aab64 --- /dev/null +++ b/core/modules/migrate_drupal/src/MigrationConfigurationTrait.php @@ -0,0 +1,170 @@ +select('system', 's', [ + 'fetch' => \PDO::FETCH_ASSOC, + ]) + ->fields('s') + ->execute(); + foreach ($results as $result) { + $system_data[$result['type']][$result['name']] = $result; + } + } + catch (\Exception $e) { + // The table might not exist for example in tests. + } + return $system_data; + } + + /** + * Creates the necessary state entries for SqlBase::getDatabase() to work. + * + * The state entities created here have to exist before migration plugin + * instances are created so that derivers such as + * \Drupal\taxonomy\Plugin\migrate\D6TermNodeDeriver can access the source + * database. + * + * @param array $database + * The source database settings. + * @param string $drupal_version + * The Drupal version. + * + * @see \Drupal\migrate\Plugin\migrate\source\SqlBase::getDatabase() + */ + protected function createDatabaseStateSettings(array $database, $drupal_version) { + $database_state['key'] = 'upgrade'; + $database_state['database'] = $database; + $database_state_key = 'migrate_drupal_' . $drupal_version; + \Drupal::state()->set($database_state_key, $database_state); + \Drupal::state()->set('migrate.fallback_state_key', $database_state_key); + } + + /** + * Gets the migrations for import. + * + * @param string $database_state_key + * The state key. + * @param int $drupal_version + * The version of Drupal we're getting the migrations for. + * + * @return \Drupal\migrate\Plugin\MigrationInterface[] + * The migrations for import. + */ + protected function getMigrations($database_state_key, $drupal_version) { + $version_tag = 'Drupal ' . $drupal_version; + $plugin_manager = \Drupal::service('plugin.manager.migration'); + /** @var \Drupal\migrate\Plugin\Migration[] $all_migrations */ + $all_migrations = $plugin_manager->createInstancesByTag($version_tag); + $migrations = []; + foreach ($all_migrations as $migration) { + try { + // @todo https://drupal.org/node/2681867 We should be able to validate + // the entire migration at this point. + $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) { + // Migrations which are not applicable given the source and destination + // site configurations (e.g., what modules are enabled) will be silently + // ignored. + } + } + + return $migrations; + } + + /** + * Determines what version of Drupal the source database contains. + * + * @param \Drupal\Core\Database\Connection $connection + * The database connection object. + * + * @return int|FALSE + * An integer representing the major branch of Drupal core (e.g. '6' for + * Drupal 6.x), or FALSE if no valid version is matched. + */ + protected function getLegacyDrupalVersion(Connection $connection) { + // Don't assume because a table of that name exists, that it has the columns + // we're querying. Catch exceptions and report that the source database is + // not Drupal. + // Drupal 5/6/7 can be detected by the schema_version in the system table. + 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; + } + } + // For Drupal 8 (and we're predicting beyond) the schema version is in the + // key_value store. + 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; + } + +} diff --git a/core/modules/migrate_drupal/src/MigrationCreationTrait.php b/core/modules/migrate_drupal/src/MigrationCreationTrait.php index 6446c72..9bbc386 100644 --- a/core/modules/migrate_drupal/src/MigrationCreationTrait.php +++ b/core/modules/migrate_drupal/src/MigrationCreationTrait.php @@ -1,173 +1,13 @@ select('system', 's', [ - 'fetch' => \PDO::FETCH_ASSOC, - ]) - ->fields('s') - ->execute(); - foreach ($results as $result) { - $system_data[$result['type']][$result['name']] = $result; - } - } - catch (\Exception $e) { - // The table might not exist for example in tests. - } - return $system_data; - } - - /** - * Creates the necessary state entries for SqlBase::getDatabase() to work. - * - * The state entities created here have to exist before migration plugin - * instances are created so that derivers such as - * \Drupal\taxonomy\Plugin\migrate\D6TermNodeDeriver can access the source - * database. - * - * @param array $database - * The source database settings. - * @param string $drupal_version - * The Drupal version. - * - * @see \Drupal\migrate\Plugin\migrate\source\SqlBase::getDatabase() - */ - protected function createDatabaseStateSettings(array $database, $drupal_version) { - $database_state['key'] = 'upgrade'; - $database_state['database'] = $database; - $database_state_key = 'migrate_drupal_' . $drupal_version; - \Drupal::state()->set($database_state_key, $database_state); - \Drupal::state()->set('migrate.fallback_state_key', $database_state_key); - } - - /** - * Gets the migrations for import. - * - * @param string $database_state_key - * The state key. - * @param int $drupal_version - * The version of Drupal we're getting the migrations for. - * - * @return \Drupal\migrate\Plugin\MigrationInterface[] - * The migrations for import. - */ - protected function getMigrations($database_state_key, $drupal_version) { - $version_tag = 'Drupal ' . $drupal_version; - $plugin_manager = \Drupal::service('plugin.manager.migration'); - /** @var \Drupal\migrate\Plugin\Migration[] $all_migrations */ - $all_migrations = $plugin_manager->createInstancesByTag($version_tag); - $migrations = []; - foreach ($all_migrations as $migration) { - try { - // @todo https://drupal.org/node/2681867 We should be able to validate - // the entire migration at this point. - $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) { - // Migrations which are not applicable given the source and destination - // site configurations (e.g., what modules are enabled) will be silently - // ignored. - } - } - - return $migrations; - } - - /** - * Determines what version of Drupal the source database contains. - * - * @param \Drupal\Core\Database\Connection $connection - * The database connection object. - * - * @return int|FALSE - * An integer representing the major branch of Drupal core (e.g. '6' for - * Drupal 6.x), or FALSE if no valid version is matched. - */ - protected function getLegacyDrupalVersion(Connection $connection) { - // Don't assume because a table of that name exists, that it has the columns - // we're querying. Catch exceptions and report that the source database is - // not Drupal. - // Drupal 5/6/7 can be detected by the schema_version in the system table. - 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; - } - } - // For Drupal 8 (and we're predicting beyond) the schema version is in the - // key_value store. - 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; - } + use MigrationConfigurationTrait; } diff --git a/core/modules/migrate_drupal_ui/src/Form/MigrateUpgradeForm.php b/core/modules/migrate_drupal_ui/src/Form/MigrateUpgradeForm.php index 643c116..8ef5f48 100644 --- a/core/modules/migrate_drupal_ui/src/Form/MigrateUpgradeForm.php +++ b/core/modules/migrate_drupal_ui/src/Form/MigrateUpgradeForm.php @@ -8,19 +8,19 @@ use Drupal\Core\Render\RendererInterface; use Drupal\Core\State\StateInterface; use Drupal\Core\Url; use Drupal\migrate\Plugin\MigrationPluginManagerInterface; use Drupal\migrate_drupal_ui\MigrateUpgradeRunBatch; -use Drupal\migrate_drupal\MigrationCreationTrait; +use Drupal\migrate_drupal\MigrationConfigurationTrait; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Defines a multi-step form for performing direct site upgrades. */ class MigrateUpgradeForm extends ConfirmFormBase { - use MigrationCreationTrait; + use MigrationConfigurationTrait; /** * Mapping of known migrations and their source and destination modules. * * @todo https://www.drupal.org/node/2569805 Hardcoding this information is diff --git a/core/modules/migrate_drupal_ui/src/Tests/MigrateUpgradeTestBase.php b/core/modules/migrate_drupal_ui/src/Tests/MigrateUpgradeTestBase.php index 5398c48..7a2d2c2 100644 --- a/core/modules/migrate_drupal_ui/src/Tests/MigrateUpgradeTestBase.php +++ b/core/modules/migrate_drupal_ui/src/Tests/MigrateUpgradeTestBase.php @@ -2,18 +2,18 @@ namespace Drupal\migrate_drupal_ui\Tests; use Drupal\Core\Database\Database; use Drupal\migrate\Plugin\MigrateIdMapInterface; -use Drupal\migrate_drupal\MigrationCreationTrait; +use Drupal\migrate_drupal\MigrationConfigurationTrait; use Drupal\simpletest\WebTestBase; /** * Provides a base class for testing migration upgrades in the UI. */ abstract class MigrateUpgradeTestBase extends WebTestBase { - use MigrationCreationTrait; + use MigrationConfigurationTrait; /** * Use the Standard profile to test help implementations of many core modules. */ protected $profile = 'standard';