Mercurial > hg > isophonics-drupal-site
comparison core/modules/migrate_drupal/src/MigrationConfigurationTrait.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\migrate_drupal; | |
4 | |
5 use Drupal\Core\Database\Connection; | |
6 use Drupal\Core\Database\Database; | |
7 use Drupal\migrate\Exception\RequirementsException; | |
8 use Drupal\migrate\Plugin\RequirementsInterface; | |
9 | |
10 /** | |
11 * Configures the appropriate migrations for a given source Drupal database. | |
12 */ | |
13 trait MigrationConfigurationTrait { | |
14 | |
15 /** | |
16 * Gets the database connection for the source Drupal database. | |
17 * | |
18 * @param array $database | |
19 * Database array representing the source Drupal database. | |
20 * | |
21 * @return \Drupal\Core\Database\Connection | |
22 * The database connection for the source Drupal database. | |
23 */ | |
24 protected function getConnection(array $database) { | |
25 // Set up the connection. | |
26 Database::addConnectionInfo('upgrade', 'default', $database); | |
27 $connection = Database::getConnection('default', 'upgrade'); | |
28 return $connection; | |
29 } | |
30 | |
31 /** | |
32 * Gets the system data from the system table of the source Drupal database. | |
33 * | |
34 * @param \Drupal\Core\Database\Connection $connection | |
35 * Database connection to the source Drupal database. | |
36 * | |
37 * @return array | |
38 * The system data from the system table of the source Drupal database. | |
39 */ | |
40 protected function getSystemData(Connection $connection) { | |
41 $system_data = []; | |
42 try { | |
43 $results = $connection->select('system', 's', [ | |
44 'fetch' => \PDO::FETCH_ASSOC, | |
45 ]) | |
46 ->fields('s') | |
47 ->execute(); | |
48 foreach ($results as $result) { | |
49 $system_data[$result['type']][$result['name']] = $result; | |
50 } | |
51 } | |
52 catch (\Exception $e) { | |
53 // The table might not exist for example in tests. | |
54 } | |
55 return $system_data; | |
56 } | |
57 | |
58 /** | |
59 * Creates the necessary state entries for SqlBase::getDatabase() to work. | |
60 * | |
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 | |
64 * database. | |
65 * | |
66 * @param array $database | |
67 * The source database settings. | |
68 * @param string $drupal_version | |
69 * The Drupal version. | |
70 * | |
71 * @see \Drupal\migrate\Plugin\migrate\source\SqlBase::getDatabase() | |
72 */ | |
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); | |
79 } | |
80 | |
81 /** | |
82 * Gets the migrations for import. | |
83 * | |
84 * @param string $database_state_key | |
85 * The state key. | |
86 * @param int $drupal_version | |
87 * The version of Drupal we're getting the migrations for. | |
88 * | |
89 * @return \Drupal\migrate\Plugin\MigrationInterface[] | |
90 * The migrations for import. | |
91 */ | |
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); | |
97 $migrations = []; | |
98 foreach ($all_migrations as $migration) { | |
99 try { | |
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(); | |
105 } | |
106 $destination_plugin = $migration->getDestinationPlugin(); | |
107 if ($destination_plugin instanceof RequirementsInterface) { | |
108 $destination_plugin->checkRequirements(); | |
109 } | |
110 $migrations[] = $migration; | |
111 } | |
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 | |
115 // ignored. | |
116 } | |
117 } | |
118 | |
119 return $migrations; | |
120 } | |
121 | |
122 /** | |
123 * Determines what version of Drupal the source database contains. | |
124 * | |
125 * @param \Drupal\Core\Database\Connection $connection | |
126 * The database connection object. | |
127 * | |
128 * @return int|false | |
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. | |
131 */ | |
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 | |
135 // not Drupal. | |
136 // Drupal 5/6/7 can be detected by the schema_version in the system table. | |
137 if ($connection->schema()->tableExists('system')) { | |
138 try { | |
139 $version_string = $connection | |
140 ->query('SELECT schema_version FROM {system} WHERE name = :module', [':module' => 'system']) | |
141 ->fetchField(); | |
142 if ($version_string && $version_string[0] == '1') { | |
143 if ((int) $version_string >= 1000) { | |
144 $version_string = '5'; | |
145 } | |
146 else { | |
147 $version_string = FALSE; | |
148 } | |
149 } | |
150 } | |
151 catch (\PDOException $e) { | |
152 $version_string = FALSE; | |
153 } | |
154 } | |
155 // For Drupal 8 (and we're predicting beyond) the schema version is in the | |
156 // key_value store. | |
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']) | |
160 ->fetchField(); | |
161 $version_string = unserialize($result); | |
162 } | |
163 else { | |
164 $version_string = FALSE; | |
165 } | |
166 | |
167 return $version_string ? substr($version_string, 0, 1) : FALSE; | |
168 } | |
169 | |
170 } |