Mercurial > hg > isophonics-drupal-site
comparison modules/contrib/migrate_upgrade/src/MigrateUpgradeDrushRunner.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 /** | |
4 * @file | |
5 * Contains \Drupal\migrate_upgrade\MigrateUpgradeDrushRunner. | |
6 */ | |
7 | |
8 namespace Drupal\migrate_upgrade; | |
9 | |
10 use Drupal\migrate\Plugin\MigrationInterface; | |
11 use Drupal\migrate\Event\MigrateEvents; | |
12 use Drupal\migrate\Event\MigrateIdMapMessageEvent; | |
13 use Drupal\migrate\MigrateExecutable; | |
14 use Drupal\Core\StringTranslation\StringTranslationTrait; | |
15 use Drupal\migrate_drupal\MigrationCreationTrait; | |
16 use Drupal\migrate_plus\Entity\Migration; | |
17 use Drupal\migrate_plus\Entity\MigrationGroup; | |
18 | |
19 class MigrateUpgradeDrushRunner { | |
20 | |
21 use MigrationCreationTrait; | |
22 use StringTranslationTrait; | |
23 | |
24 /** | |
25 * The list of migrations to run and their configuration. | |
26 * | |
27 * @var \Drupal\migrate\Plugin\Migration[] | |
28 */ | |
29 protected $migrationList; | |
30 | |
31 /** | |
32 * MigrateMessage instance to display messages during the migration process. | |
33 * | |
34 * @var \Drupal\migrate_upgrade\DrushLogMigrateMessage | |
35 */ | |
36 protected static $messages; | |
37 | |
38 /** | |
39 * The Drupal version being imported. | |
40 * | |
41 * @var string | |
42 */ | |
43 protected $version; | |
44 | |
45 /** | |
46 * The state key used to store database configuration. | |
47 * | |
48 * @var string | |
49 */ | |
50 protected $databaseStateKey; | |
51 | |
52 /** | |
53 * From the provided source information, instantiate the appropriate migrations | |
54 * in the active configuration. | |
55 * | |
56 * @throws \Exception | |
57 */ | |
58 public function configure() { | |
59 $db_url = drush_get_option('legacy-db-url'); | |
60 $db_spec = drush_convert_db_from_db_url($db_url); | |
61 $db_prefix = drush_get_option('legacy-db-prefix'); | |
62 $db_spec['prefix'] = $db_prefix; | |
63 | |
64 $connection = $this->getConnection($db_spec); | |
65 $this->version = $this->getLegacyDrupalVersion($connection); | |
66 $this->createDatabaseStateSettings($db_spec, $this->version); | |
67 $this->databaseStateKey = 'migrate_drupal_' . $this->version; | |
68 $migrations = $this->getMigrations($this->databaseStateKey, $this->version); | |
69 $this->migrationList = []; | |
70 foreach ($migrations as $migration) { | |
71 $destination = $migration->get('destination'); | |
72 if ($destination['plugin'] === 'entity:file') { | |
73 // Make sure we have a single trailing slash. | |
74 $source_base_path = rtrim(drush_get_option('legacy-root'), '/') . '/'; | |
75 $destination['source_base_path'] = $source_base_path; | |
76 $migration->set('destination', $destination); | |
77 } | |
78 $this->migrationList[$migration->id()] = $migration; | |
79 } | |
80 } | |
81 | |
82 /** | |
83 * Run the configured migrations. | |
84 */ | |
85 public function import() { | |
86 static::$messages = new DrushLogMigrateMessage(); | |
87 if (drush_get_option('debug')) { | |
88 \Drupal::service('event_dispatcher')->addListener(MigrateEvents::IDMAP_MESSAGE, | |
89 [get_class(), 'onIdMapMessage']); | |
90 } | |
91 foreach ($this->migrationList as $migration_id => $migration) { | |
92 drush_print(dt('Upgrading @migration', ['@migration' => $migration_id])); | |
93 $executable = new MigrateExecutable($migration, static::$messages); | |
94 // drush_op() provides --simulate support. | |
95 drush_op([$executable, 'import']); | |
96 } | |
97 } | |
98 | |
99 /** | |
100 * Export the configured migration plugins as configuration entities. | |
101 */ | |
102 public function export() { | |
103 $db_info = \Drupal::state()->get($this->databaseStateKey); | |
104 | |
105 // Create a group to hold the database configuration. | |
106 $group = [ | |
107 'id' => $this->databaseStateKey, | |
108 'label' => 'Import from Drupal ' . $this->version, | |
109 'description' => 'Migrations originally generated from drush migrate-upgrade --configure-only', | |
110 'source_type' => 'Drupal ' . $this->version, | |
111 'shared_configuration' => [ | |
112 'source' => [ | |
113 'key' => 'drupal_' . $this->version, | |
114 'database' => $db_info['database'], | |
115 ] | |
116 ] | |
117 ]; | |
118 $group = MigrationGroup::create($group); | |
119 $group->save(); | |
120 foreach ($this->migrationList as $migration_id => $migration) { | |
121 drush_print(dt('Exporting @migration as @new_migration', | |
122 ['@migration' => $migration_id, '@new_migration' => $this->modifyId($migration_id)])); | |
123 $entity_array['id'] = $migration_id; | |
124 $entity_array['migration_group'] = $this->databaseStateKey; | |
125 $entity_array['migration_tags'] = $migration->get('migration_tags'); | |
126 $entity_array['label'] = $migration->get('label'); | |
127 $entity_array['source'] = $migration->getSourceConfiguration(); | |
128 $entity_array['destination'] = $migration->getDestinationConfiguration(); | |
129 $entity_array['process'] = $migration->get('process'); | |
130 $entity_array['migration_dependencies'] = $migration->getMigrationDependencies(); | |
131 $migration_entity = Migration::create($this->substituteIds($entity_array)); | |
132 $migration_entity->save(); | |
133 } | |
134 } | |
135 | |
136 /** | |
137 * Rewrite any migration plugin IDs so they won't conflict with the core | |
138 * IDs. | |
139 * | |
140 * @param $entity_array | |
141 * A configuration array for a migration. | |
142 * | |
143 * @return array | |
144 * The migration configuration array modified with new IDs. | |
145 */ | |
146 protected function substituteIds($entity_array) { | |
147 $entity_array['id'] = $this->modifyId($entity_array['id']); | |
148 foreach ($entity_array['migration_dependencies'] as $type => $dependencies) { | |
149 foreach ($dependencies as $key => $dependency) { | |
150 $entity_array['migration_dependencies'][$type][$key] = $this->modifyId($dependency); | |
151 } | |
152 } | |
153 foreach ($entity_array['process'] as $destination => $process) { | |
154 if (is_array($process)) { | |
155 if ($process['plugin'] == 'migration') { | |
156 $entity_array['process'][$destination]['migration'] = | |
157 $this->modifyId($process['migration']); | |
158 } | |
159 } | |
160 } | |
161 return $entity_array; | |
162 } | |
163 | |
164 /** | |
165 * @param $id | |
166 * The original core plugin ID. | |
167 * | |
168 * @return string | |
169 * The ID modified to serve as a configuration entity ID. | |
170 */ | |
171 protected function modifyId($id) { | |
172 return drush_get_option('migration-prefix', 'upgrade_') . str_replace(':', '_', $id); | |
173 } | |
174 | |
175 /** | |
176 * Rolls back the configured migrations. | |
177 */ | |
178 public function rollback() { | |
179 static::$messages = new DrushLogMigrateMessage(); | |
180 $database_state_key = \Drupal::state()->get('migrate.fallback_state_key'); | |
181 $database_state = \Drupal::state()->get($database_state_key); | |
182 $db_spec = $database_state['database']; | |
183 $connection = $this->getConnection($db_spec); | |
184 $version = $this->getLegacyDrupalVersion($connection); | |
185 $migrations = $this->getMigrations('migrate_drupal_' . $version, $version); | |
186 | |
187 // Roll back in reverse order. | |
188 $this->migrationList = array_reverse($migrations); | |
189 | |
190 foreach ($migrations as $migration) { | |
191 drush_print(dt('Rolling back @migration', ['@migration' => $migration->id()])); | |
192 $executable = new MigrateExecutable($migration, static::$messages); | |
193 // drush_op() provides --simulate support. | |
194 drush_op([$executable, 'rollback']); | |
195 } | |
196 } | |
197 | |
198 /** | |
199 * Display any messages being logged to the ID map. | |
200 * | |
201 * @param \Drupal\migrate\Event\MigrateIdMapMessageEvent $event | |
202 * The message event. | |
203 */ | |
204 public static function onIdMapMessage(MigrateIdMapMessageEvent $event) { | |
205 if ($event->getLevel() == MigrationInterface::MESSAGE_NOTICE || | |
206 $event->getLevel() == MigrationInterface::MESSAGE_INFORMATIONAL) { | |
207 $type = 'status'; | |
208 } | |
209 else { | |
210 $type = 'error'; | |
211 } | |
212 $source_id_string = implode(',', $event->getSourceIdValues()); | |
213 $message = t('Source ID @source_id: @message', | |
214 ['@source_id' => $source_id_string, '@message' => $event->getMessage()]); | |
215 static::$messages->display($message, $type); | |
216 } | |
217 | |
218 } |