annotate core/modules/migrate_drupal/src/MigrationConfigurationTrait.php @ 16:c2387f117808

Routine composer update
author Chris Cannam
date Tue, 10 Jul 2018 15:07:59 +0100
parents 1fec387a4317
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\migrate_drupal;
Chris@0 4
Chris@0 5 use Drupal\Core\Database\Connection;
Chris@0 6 use Drupal\Core\Database\Database;
Chris@0 7 use Drupal\migrate\Exception\RequirementsException;
Chris@0 8 use Drupal\migrate\Plugin\RequirementsInterface;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Configures the appropriate migrations for a given source Drupal database.
Chris@0 12 */
Chris@0 13 trait MigrationConfigurationTrait {
Chris@0 14
Chris@0 15 /**
Chris@16 16 * The follow-up migration tags.
Chris@16 17 *
Chris@16 18 * @var string[]
Chris@16 19 */
Chris@16 20 protected $followUpMigrationTags;
Chris@16 21
Chris@16 22 /**
Chris@0 23 * Gets the database connection for the source Drupal database.
Chris@0 24 *
Chris@0 25 * @param array $database
Chris@0 26 * Database array representing the source Drupal database.
Chris@0 27 *
Chris@0 28 * @return \Drupal\Core\Database\Connection
Chris@0 29 * The database connection for the source Drupal database.
Chris@0 30 */
Chris@0 31 protected function getConnection(array $database) {
Chris@0 32 // Set up the connection.
Chris@0 33 Database::addConnectionInfo('upgrade', 'default', $database);
Chris@0 34 $connection = Database::getConnection('default', 'upgrade');
Chris@0 35 return $connection;
Chris@0 36 }
Chris@0 37
Chris@0 38 /**
Chris@0 39 * Gets the system data from the system table of the source Drupal database.
Chris@0 40 *
Chris@0 41 * @param \Drupal\Core\Database\Connection $connection
Chris@0 42 * Database connection to the source Drupal database.
Chris@0 43 *
Chris@0 44 * @return array
Chris@0 45 * The system data from the system table of the source Drupal database.
Chris@0 46 */
Chris@0 47 protected function getSystemData(Connection $connection) {
Chris@0 48 $system_data = [];
Chris@0 49 try {
Chris@0 50 $results = $connection->select('system', 's', [
Chris@0 51 'fetch' => \PDO::FETCH_ASSOC,
Chris@0 52 ])
Chris@0 53 ->fields('s')
Chris@0 54 ->execute();
Chris@0 55 foreach ($results as $result) {
Chris@0 56 $system_data[$result['type']][$result['name']] = $result;
Chris@0 57 }
Chris@0 58 }
Chris@0 59 catch (\Exception $e) {
Chris@0 60 // The table might not exist for example in tests.
Chris@0 61 }
Chris@0 62 return $system_data;
Chris@0 63 }
Chris@0 64
Chris@0 65 /**
Chris@0 66 * Creates the necessary state entries for SqlBase::getDatabase() to work.
Chris@0 67 *
Chris@0 68 * The state entities created here have to exist before migration plugin
Chris@0 69 * instances are created so that derivers such as
Chris@0 70 * \Drupal\taxonomy\Plugin\migrate\D6TermNodeDeriver can access the source
Chris@0 71 * database.
Chris@0 72 *
Chris@0 73 * @param array $database
Chris@0 74 * The source database settings.
Chris@0 75 * @param string $drupal_version
Chris@0 76 * The Drupal version.
Chris@0 77 *
Chris@0 78 * @see \Drupal\migrate\Plugin\migrate\source\SqlBase::getDatabase()
Chris@0 79 */
Chris@0 80 protected function createDatabaseStateSettings(array $database, $drupal_version) {
Chris@0 81 $database_state['key'] = 'upgrade';
Chris@0 82 $database_state['database'] = $database;
Chris@0 83 $database_state_key = 'migrate_drupal_' . $drupal_version;
Chris@0 84 \Drupal::state()->set($database_state_key, $database_state);
Chris@0 85 \Drupal::state()->set('migrate.fallback_state_key', $database_state_key);
Chris@0 86 }
Chris@0 87
Chris@0 88 /**
Chris@0 89 * Gets the migrations for import.
Chris@0 90 *
Chris@0 91 * @param string $database_state_key
Chris@0 92 * The state key.
Chris@0 93 * @param int $drupal_version
Chris@0 94 * The version of Drupal we're getting the migrations for.
Chris@0 95 *
Chris@0 96 * @return \Drupal\migrate\Plugin\MigrationInterface[]
Chris@0 97 * The migrations for import.
Chris@0 98 */
Chris@0 99 protected function getMigrations($database_state_key, $drupal_version) {
Chris@0 100 $version_tag = 'Drupal ' . $drupal_version;
Chris@0 101 $plugin_manager = \Drupal::service('plugin.manager.migration');
Chris@0 102 /** @var \Drupal\migrate\Plugin\Migration[] $all_migrations */
Chris@0 103 $all_migrations = $plugin_manager->createInstancesByTag($version_tag);
Chris@0 104 $migrations = [];
Chris@0 105 foreach ($all_migrations as $migration) {
Chris@16 106 // Skip migrations tagged with any of the follow-up migration tags. They
Chris@16 107 // will be derived and executed after the migrations on which they depend
Chris@16 108 // have been successfully executed.
Chris@16 109 // @see Drupal\migrate_drupal\Plugin\MigrationWithFollowUpInterface
Chris@16 110 if (!empty(array_intersect($migration->getMigrationTags(), $this->getFollowUpMigrationTags()))) {
Chris@16 111 continue;
Chris@16 112 }
Chris@0 113 try {
Chris@0 114 // @todo https://drupal.org/node/2681867 We should be able to validate
Chris@0 115 // the entire migration at this point.
Chris@0 116 $source_plugin = $migration->getSourcePlugin();
Chris@0 117 if ($source_plugin instanceof RequirementsInterface) {
Chris@0 118 $source_plugin->checkRequirements();
Chris@0 119 }
Chris@0 120 $destination_plugin = $migration->getDestinationPlugin();
Chris@0 121 if ($destination_plugin instanceof RequirementsInterface) {
Chris@0 122 $destination_plugin->checkRequirements();
Chris@0 123 }
Chris@0 124 $migrations[] = $migration;
Chris@0 125 }
Chris@0 126 catch (RequirementsException $e) {
Chris@0 127 // Migrations which are not applicable given the source and destination
Chris@0 128 // site configurations (e.g., what modules are enabled) will be silently
Chris@0 129 // ignored.
Chris@0 130 }
Chris@0 131 }
Chris@0 132
Chris@0 133 return $migrations;
Chris@0 134 }
Chris@0 135
Chris@0 136 /**
Chris@16 137 * Returns the follow-up migration tags.
Chris@16 138 *
Chris@16 139 * @return string[]
Chris@16 140 */
Chris@16 141 protected function getFollowUpMigrationTags() {
Chris@16 142 if ($this->followUpMigrationTags === NULL) {
Chris@16 143 $this->followUpMigrationTags = \Drupal::configFactory()
Chris@16 144 ->get('migrate_drupal.settings')
Chris@16 145 ->get('follow_up_migration_tags') ?: [];
Chris@16 146 }
Chris@16 147 return $this->followUpMigrationTags;
Chris@16 148 }
Chris@16 149
Chris@16 150 /**
Chris@0 151 * Determines what version of Drupal the source database contains.
Chris@0 152 *
Chris@0 153 * @param \Drupal\Core\Database\Connection $connection
Chris@0 154 * The database connection object.
Chris@0 155 *
Chris@14 156 * @return string|false
Chris@14 157 * A string representing the major branch of Drupal core (e.g. '6' for
Chris@0 158 * Drupal 6.x), or FALSE if no valid version is matched.
Chris@0 159 */
Chris@0 160 protected function getLegacyDrupalVersion(Connection $connection) {
Chris@0 161 // Don't assume because a table of that name exists, that it has the columns
Chris@0 162 // we're querying. Catch exceptions and report that the source database is
Chris@0 163 // not Drupal.
Chris@0 164 // Drupal 5/6/7 can be detected by the schema_version in the system table.
Chris@0 165 if ($connection->schema()->tableExists('system')) {
Chris@0 166 try {
Chris@0 167 $version_string = $connection
Chris@0 168 ->query('SELECT schema_version FROM {system} WHERE name = :module', [':module' => 'system'])
Chris@0 169 ->fetchField();
Chris@0 170 if ($version_string && $version_string[0] == '1') {
Chris@0 171 if ((int) $version_string >= 1000) {
Chris@0 172 $version_string = '5';
Chris@0 173 }
Chris@0 174 else {
Chris@0 175 $version_string = FALSE;
Chris@0 176 }
Chris@0 177 }
Chris@0 178 }
Chris@0 179 catch (\PDOException $e) {
Chris@0 180 $version_string = FALSE;
Chris@0 181 }
Chris@0 182 }
Chris@0 183 // For Drupal 8 (and we're predicting beyond) the schema version is in the
Chris@0 184 // key_value store.
Chris@0 185 elseif ($connection->schema()->tableExists('key_value')) {
Chris@0 186 $result = $connection
Chris@0 187 ->query("SELECT value FROM {key_value} WHERE collection = :system_schema and name = :module", [':system_schema' => 'system.schema', ':module' => 'system'])
Chris@0 188 ->fetchField();
Chris@0 189 $version_string = unserialize($result);
Chris@0 190 }
Chris@0 191 else {
Chris@0 192 $version_string = FALSE;
Chris@0 193 }
Chris@0 194
Chris@0 195 return $version_string ? substr($version_string, 0, 1) : FALSE;
Chris@0 196 }
Chris@0 197
Chris@0 198 }