3 namespace Drupal\migrate_drupal;
5 use Drupal\Core\Database\Connection;
6 use Drupal\Core\Database\Database;
7 use Drupal\migrate\Exception\RequirementsException;
8 use Drupal\migrate\Plugin\RequirementsInterface;
11 * Configures the appropriate migrations for a given source Drupal database.
13 trait MigrationConfigurationTrait {
16 * Gets the database connection for the source Drupal database.
18 * @param array $database
19 * Database array representing the source Drupal database.
21 * @return \Drupal\Core\Database\Connection
22 * The database connection for the source Drupal database.
24 protected function getConnection(array $database) {
25 // Set up the connection.
26 Database::addConnectionInfo('upgrade', 'default', $database);
27 $connection = Database::getConnection('default', 'upgrade');
32 * Gets the system data from the system table of the source Drupal database.
34 * @param \Drupal\Core\Database\Connection $connection
35 * Database connection to the source Drupal database.
38 * The system data from the system table of the source Drupal database.
40 protected function getSystemData(Connection $connection) {
43 $results = $connection->select('system', 's', [
44 'fetch' => \PDO::FETCH_ASSOC,
48 foreach ($results as $result) {
49 $system_data[$result['type']][$result['name']] = $result;
52 catch (\Exception $e) {
53 // The table might not exist for example in tests.
59 * Creates the necessary state entries for SqlBase::getDatabase() to work.
61 * The state entities created here have to exist before migration plugin
62 * instances are created so that derivers such as
63 * \Drupal\taxonomy\Plugin\migrate\D6TermNodeDeriver can access the source
66 * @param array $database
67 * The source database settings.
68 * @param string $drupal_version
71 * @see \Drupal\migrate\Plugin\migrate\source\SqlBase::getDatabase()
73 protected function createDatabaseStateSettings(array $database, $drupal_version) {
74 $database_state['key'] = 'upgrade';
75 $database_state['database'] = $database;
76 $database_state_key = 'migrate_drupal_' . $drupal_version;
77 \Drupal::state()->set($database_state_key, $database_state);
78 \Drupal::state()->set('migrate.fallback_state_key', $database_state_key);
82 * Gets the migrations for import.
84 * @param string $database_state_key
86 * @param int $drupal_version
87 * The version of Drupal we're getting the migrations for.
89 * @return \Drupal\migrate\Plugin\MigrationInterface[]
90 * The migrations for import.
92 protected function getMigrations($database_state_key, $drupal_version) {
93 $version_tag = 'Drupal ' . $drupal_version;
94 $plugin_manager = \Drupal::service('plugin.manager.migration');
95 /** @var \Drupal\migrate\Plugin\Migration[] $all_migrations */
96 $all_migrations = $plugin_manager->createInstancesByTag($version_tag);
98 foreach ($all_migrations as $migration) {
100 // @todo https://drupal.org/node/2681867 We should be able to validate
101 // the entire migration at this point.
102 $source_plugin = $migration->getSourcePlugin();
103 if ($source_plugin instanceof RequirementsInterface) {
104 $source_plugin->checkRequirements();
106 $destination_plugin = $migration->getDestinationPlugin();
107 if ($destination_plugin instanceof RequirementsInterface) {
108 $destination_plugin->checkRequirements();
110 $migrations[] = $migration;
112 catch (RequirementsException $e) {
113 // Migrations which are not applicable given the source and destination
114 // site configurations (e.g., what modules are enabled) will be silently
123 * Determines what version of Drupal the source database contains.
125 * @param \Drupal\Core\Database\Connection $connection
126 * The database connection object.
129 * An integer representing the major branch of Drupal core (e.g. '6' for
130 * Drupal 6.x), or FALSE if no valid version is matched.
132 protected function getLegacyDrupalVersion(Connection $connection) {
133 // Don't assume because a table of that name exists, that it has the columns
134 // we're querying. Catch exceptions and report that the source database is
136 // Drupal 5/6/7 can be detected by the schema_version in the system table.
137 if ($connection->schema()->tableExists('system')) {
139 $version_string = $connection
140 ->query('SELECT schema_version FROM {system} WHERE name = :module', [':module' => 'system'])
142 if ($version_string && $version_string[0] == '1') {
143 if ((int) $version_string >= 1000) {
144 $version_string = '5';
147 $version_string = FALSE;
151 catch (\PDOException $e) {
152 $version_string = FALSE;
155 // For Drupal 8 (and we're predicting beyond) the schema version is in the
157 elseif ($connection->schema()->tableExists('key_value')) {
158 $result = $connection
159 ->query("SELECT value FROM {key_value} WHERE collection = :system_schema and name = :module", [':system_schema' => 'system.schema', ':module' => 'system'])
161 $version_string = unserialize($result);
164 $version_string = FALSE;
167 return $version_string ? substr($version_string, 0, 1) : FALSE;