annotate core/modules/migrate_drupal/src/MigrationConfigurationTrait.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children 12f9dff5fda9
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@0 16 * The follow-up migration tags.
Chris@0 17 *
Chris@0 18 * @var string[]
Chris@0 19 */
Chris@0 20 protected $followUpMigrationTags;
Chris@0 21
Chris@0 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@0 106 // Skip migrations tagged with any of the follow-up migration tags. They
Chris@0 107 // will be derived and executed after the migrations on which they depend
Chris@0 108 // have been successfully executed.
Chris@0 109 // @see Drupal\migrate_drupal\Plugin\MigrationWithFollowUpInterface
Chris@0 110 if (!empty(array_intersect($migration->getMigrationTags(), $this->getFollowUpMigrationTags()))) {
Chris@0 111 continue;
Chris@0 112 }
Chris@4 113 // Multilingual migrations require migrate_drupal_multilingual.
Chris@4 114 $tags = $migration->getMigrationTags() ?: [];
Chris@4 115 if (in_array('Multilingual', $tags, TRUE) && (!\Drupal::service('module_handler')->moduleExists('migrate_drupal_multilingual'))) {
Chris@4 116 throw new RequirementsException(sprintf("Install migrate_drupal_multilingual to run migration '%s'.", $migration->getPluginId()));
Chris@4 117 }
Chris@4 118
Chris@0 119 try {
Chris@0 120 // @todo https://drupal.org/node/2681867 We should be able to validate
Chris@0 121 // the entire migration at this point.
Chris@0 122 $source_plugin = $migration->getSourcePlugin();
Chris@0 123 if ($source_plugin instanceof RequirementsInterface) {
Chris@0 124 $source_plugin->checkRequirements();
Chris@0 125 }
Chris@0 126 $destination_plugin = $migration->getDestinationPlugin();
Chris@0 127 if ($destination_plugin instanceof RequirementsInterface) {
Chris@0 128 $destination_plugin->checkRequirements();
Chris@0 129 }
Chris@0 130 $migrations[] = $migration;
Chris@0 131 }
Chris@0 132 catch (RequirementsException $e) {
Chris@0 133 // Migrations which are not applicable given the source and destination
Chris@0 134 // site configurations (e.g., what modules are enabled) will be silently
Chris@0 135 // ignored.
Chris@0 136 }
Chris@0 137 }
Chris@0 138
Chris@0 139 return $migrations;
Chris@0 140 }
Chris@0 141
Chris@0 142 /**
Chris@0 143 * Returns the follow-up migration tags.
Chris@0 144 *
Chris@0 145 * @return string[]
Chris@0 146 */
Chris@0 147 protected function getFollowUpMigrationTags() {
Chris@0 148 if ($this->followUpMigrationTags === NULL) {
Chris@0 149 $this->followUpMigrationTags = \Drupal::configFactory()
Chris@0 150 ->get('migrate_drupal.settings')
Chris@0 151 ->get('follow_up_migration_tags') ?: [];
Chris@0 152 }
Chris@0 153 return $this->followUpMigrationTags;
Chris@0 154 }
Chris@0 155
Chris@0 156 /**
Chris@0 157 * Determines what version of Drupal the source database contains.
Chris@0 158 *
Chris@0 159 * @param \Drupal\Core\Database\Connection $connection
Chris@0 160 * The database connection object.
Chris@0 161 *
Chris@0 162 * @return string|false
Chris@0 163 * A string representing the major branch of Drupal core (e.g. '6' for
Chris@0 164 * Drupal 6.x), or FALSE if no valid version is matched.
Chris@0 165 */
Chris@0 166 protected function getLegacyDrupalVersion(Connection $connection) {
Chris@0 167 // Don't assume because a table of that name exists, that it has the columns
Chris@0 168 // we're querying. Catch exceptions and report that the source database is
Chris@0 169 // not Drupal.
Chris@0 170 // Drupal 5/6/7 can be detected by the schema_version in the system table.
Chris@0 171 if ($connection->schema()->tableExists('system')) {
Chris@0 172 try {
Chris@0 173 $version_string = $connection
Chris@0 174 ->query('SELECT schema_version FROM {system} WHERE name = :module', [':module' => 'system'])
Chris@0 175 ->fetchField();
Chris@0 176 if ($version_string && $version_string[0] == '1') {
Chris@0 177 if ((int) $version_string >= 1000) {
Chris@0 178 $version_string = '5';
Chris@0 179 }
Chris@0 180 else {
Chris@0 181 $version_string = FALSE;
Chris@0 182 }
Chris@0 183 }
Chris@0 184 }
Chris@0 185 catch (\PDOException $e) {
Chris@0 186 $version_string = FALSE;
Chris@0 187 }
Chris@0 188 }
Chris@0 189 // For Drupal 8 (and we're predicting beyond) the schema version is in the
Chris@0 190 // key_value store.
Chris@0 191 elseif ($connection->schema()->tableExists('key_value')) {
Chris@0 192 $result = $connection
Chris@0 193 ->query("SELECT value FROM {key_value} WHERE collection = :system_schema and name = :module", [':system_schema' => 'system.schema', ':module' => 'system'])
Chris@0 194 ->fetchField();
Chris@0 195 $version_string = unserialize($result);
Chris@0 196 }
Chris@0 197 else {
Chris@0 198 $version_string = FALSE;
Chris@0 199 }
Chris@0 200
Chris@0 201 return $version_string ? substr($version_string, 0, 1) : FALSE;
Chris@0 202 }
Chris@0 203
Chris@0 204 }