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