annotate modules/contrib/migrate_tools/migrate_tools.drush.inc @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents c75dbcec494b
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /**
Chris@0 4 * @file
Chris@0 5 * Command-line tools to aid performing and developing migrations.
Chris@0 6 */
Chris@0 7
Chris@0 8 use Drupal\Component\Plugin\Exception\PluginException;
Chris@0 9 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
Chris@0 10 use Drupal\Component\Utility\Unicode;
Chris@0 11 use Drupal\Core\Database\ConnectionNotDefinedException;
Chris@0 12 use Drupal\migrate\Exception\RequirementsException;
Chris@0 13 use Drupal\migrate\Plugin\MigrationInterface;
Chris@0 14 use Drupal\migrate_tools\MigrateExecutable;
Chris@0 15 use Drupal\migrate_tools\DrushLogMigrateMessage;
Chris@0 16 use Drupal\Core\Datetime\DateFormatter;
Chris@0 17
Chris@0 18 /**
Chris@0 19 * Implements hook_drush_command().
Chris@0 20 */
Chris@0 21 function migrate_tools_drush_command() {
Chris@0 22 $items['migrate-status'] = [
Chris@0 23 'description' => 'List all migrations with current status.',
Chris@0 24 'options' => [
Chris@0 25 'group' => 'Name of the migration group to list',
Chris@0 26 'names-only' => 'Only return names, not all the details (faster)',
Chris@0 27 ],
Chris@0 28 'arguments' => [
Chris@0 29 'migration' => 'Restrict to a comma-separated list of migrations. Optional',
Chris@0 30 ],
Chris@0 31 'examples' => [
Chris@0 32 'migrate-status' => 'Retrieve status for all migrations',
Chris@0 33 'migrate-status --group=beer' => 'Retrieve status for all migrations in a given group',
Chris@0 34 'migrate-status BeerTerm,BeerNode' => 'Retrieve status for specific migrations',
Chris@0 35 ],
Chris@0 36 'drupal dependencies' => ['migrate_tools'],
Chris@0 37 'aliases' => ['ms'],
Chris@0 38 ];
Chris@0 39
Chris@0 40 $items['migrate-import'] = [
Chris@0 41 'description' => 'Perform one or more migration processes.',
Chris@0 42 'options' => [
Chris@0 43 'all' => 'Process all migrations.',
Chris@0 44 'group' => 'Name of the migration group to import',
Chris@0 45 'limit' => 'Limit on the number of items to process in each migration',
Chris@0 46 'feedback' => 'Frequency of progress messages, in items processed',
Chris@0 47 'idlist' => 'Comma-separated list of IDs to import',
Chris@0 48 'update' => ' In addition to processing unprocessed items from the source, update previously-imported items with the current data',
Chris@0 49 'force' => 'Force an operation to run, even if all dependencies are not satisfied',
Chris@0 50 'execute-dependencies' => 'Execute all dependent migrations first.',
Chris@0 51 ],
Chris@0 52 'arguments' => [
Chris@0 53 'migration' => 'Name of migration(s) to import. Delimit multiple using commas.',
Chris@0 54 ],
Chris@0 55 'examples' => [
Chris@0 56 'migrate-import --all' => 'Perform all migrations',
Chris@0 57 'migrate-import --group=beer' => 'Import all migrations in the beer group',
Chris@0 58 'migrate-import BeerTerm,BeerNode' => 'Import new terms and nodes',
Chris@0 59 'migrate-import BeerUser --limit=2' => 'Import no more than 2 users',
Chris@0 60 'migrate-import BeerUser --idlist=5' => 'Import the user record with source ID 5',
Chris@0 61 ],
Chris@0 62 'drupal dependencies' => ['migrate_tools'],
Chris@0 63 'aliases' => ['mi'],
Chris@0 64 ];
Chris@0 65
Chris@0 66 $items['migrate-rollback'] = array(
Chris@0 67 'description' => 'Rollback one or more migrations.',
Chris@0 68 'options' => array(
Chris@0 69 'all' => 'Process all migrations.',
Chris@0 70 'group' => 'Name of the migration group to rollback',
Chris@0 71 'feedback' => 'Frequency of progress messages, in items processed',
Chris@0 72 ),
Chris@0 73 'arguments' => array(
Chris@0 74 'migration' => 'Name of migration(s) to rollback. Delimit multiple using commas.',
Chris@0 75 ),
Chris@0 76 'examples' => array(
Chris@0 77 'migrate-rollback --all' => 'Perform all migrations',
Chris@0 78 'migrate-rollback --group=beer' => 'Rollback all migrations in the beer group',
Chris@0 79 'migrate-rollback BeerTerm,BeerNode' => 'Rollback imported terms and nodes',
Chris@0 80 ),
Chris@0 81 'drupal dependencies' => array('migrate_tools'),
Chris@0 82 'aliases' => array('mr'),
Chris@0 83 );
Chris@0 84
Chris@0 85 $items['migrate-stop'] = [
Chris@0 86 'description' => 'Stop an active migration operation.',
Chris@0 87 'arguments' => [
Chris@0 88 'migration' => 'Name of migration to stop',
Chris@0 89 ],
Chris@0 90 'drupal dependencies' => ['migrate_tools'],
Chris@0 91 'aliases' => ['mst'],
Chris@0 92 ];
Chris@0 93
Chris@0 94 $items['migrate-reset-status'] = [
Chris@0 95 'description' => 'Reset a active migration\'s status to idle.',
Chris@0 96 'arguments' => [
Chris@0 97 'migration' => 'Name of migration to reset',
Chris@0 98 ],
Chris@0 99 'drupal dependencies' => ['migrate_tools'],
Chris@0 100 'aliases' => ['mrs'],
Chris@0 101 ];
Chris@0 102
Chris@0 103 $items['migrate-messages'] = [
Chris@0 104 'description' => 'View any messages associated with a migration.',
Chris@0 105 'arguments' => [
Chris@0 106 'migration' => 'Name of the migration',
Chris@0 107 ],
Chris@0 108 'options' => [
Chris@0 109 'csv' => 'Export messages as a CSV'
Chris@0 110 ],
Chris@0 111 'examples' => [
Chris@0 112 'migrate-messages MyNode' => 'Show all messages for the MyNode migration',
Chris@0 113 ],
Chris@0 114 'drupal dependencies' => ['migrate_tools'],
Chris@0 115 'aliases' => ['mmsg'],
Chris@0 116 ];
Chris@0 117
Chris@0 118 $items['migrate-fields-source'] = [
Chris@0 119 'description' => 'List the fields available for mapping in a source.',
Chris@0 120 'arguments' => [
Chris@0 121 'migration' => 'Name of the migration',
Chris@0 122 ],
Chris@0 123 'examples' => [
Chris@0 124 'migrate-fields-source my_node' => 'List fields for the source in the my_node migration',
Chris@0 125 ],
Chris@0 126 'drupal dependencies' => ['migrate_tools'],
Chris@0 127 'aliases' => ['mfs'],
Chris@0 128 ];
Chris@0 129
Chris@0 130 return $items;
Chris@0 131 }
Chris@0 132
Chris@0 133 /**
Chris@0 134 * @param string $migration_names
Chris@0 135 */
Chris@0 136 function drush_migrate_tools_migrate_status($migration_names = '') {
Chris@0 137 $group_name = drush_get_option('group');
Chris@0 138 $names_only = drush_get_option('names-only');
Chris@0 139
Chris@0 140 $migrations = drush_migrate_tools_migration_list($group_name, $migration_names);
Chris@0 141
Chris@0 142 $table = [];
Chris@0 143 // Take it one group at a time, listing the migrations within each group.
Chris@0 144 foreach ($migrations as $group_id => $migration_list) {
Chris@0 145 if ($names_only) {
Chris@0 146 $table[] = [
Chris@0 147 dt('Group: @name', array('@name' => $group_id))
Chris@0 148 ];
Chris@0 149 }
Chris@0 150 else {
Chris@0 151 $table[] = [
Chris@0 152 dt('Group: @name', array('@name' => $group_id)),
Chris@0 153 dt('Status'),
Chris@0 154 dt('Total'),
Chris@0 155 dt('Imported'),
Chris@0 156 dt('Unprocessed'),
Chris@0 157 dt('Last imported'),
Chris@0 158 ];
Chris@0 159 }
Chris@0 160 foreach ($migration_list as $migration_id => $migration) {
Chris@0 161 try {
Chris@0 162 $map = $migration->getIdMap();
Chris@0 163 $imported = $map->importedCount();
Chris@0 164 $source_plugin = $migration->getSourcePlugin();
Chris@0 165 }
Chris@0 166 catch (Exception $e) {
Chris@0 167 drush_log(dt('Failure retrieving information on @migration: @message',
Chris@0 168 ['@migration' => $migration_id, '@message' => $e->getMessage()]));
Chris@0 169 continue;
Chris@0 170 }
Chris@0 171 try {
Chris@0 172 $source_rows = $source_plugin->count();
Chris@0 173 // -1 indicates uncountable sources.
Chris@0 174 if ($source_rows == -1) {
Chris@0 175 $source_rows = dt('N/A');
Chris@0 176 $unprocessed = dt('N/A');
Chris@0 177 }
Chris@0 178 else {
Chris@0 179 $unprocessed = $source_rows - $map->processedCount();
Chris@0 180 }
Chris@0 181 }
Chris@0 182 catch (Exception $e) {
Chris@0 183 drush_print($e->getMessage());
Chris@0 184 drush_log(dt('Could not retrieve source count from @migration: @message',
Chris@0 185 ['@migration' => $migration_id, '@message' => $e->getMessage()]));
Chris@0 186 $source_rows = dt('N/A');
Chris@0 187 $unprocessed = dt('N/A');
Chris@0 188 }
Chris@0 189
Chris@0 190 if ($names_only) {
Chris@0 191 $table[] = [$migration_id];
Chris@0 192 }
Chris@0 193 else {
Chris@0 194 $status = $migration->getStatusLabel();
Chris@0 195 $migrate_last_imported_store = \Drupal::keyValue('migrate_last_imported');
Chris@0 196 $last_imported = $migrate_last_imported_store->get($migration->id(), FALSE);
Chris@0 197 if ($last_imported) {
Chris@0 198 /** @var DateFormatter $date_formatter */
Chris@0 199 $date_formatter = \Drupal::service('date.formatter');
Chris@0 200 $last_imported = $date_formatter->format($last_imported / 1000,
Chris@0 201 'custom', 'Y-m-d H:i:s');
Chris@0 202 }
Chris@0 203 else {
Chris@0 204 $last_imported = '';
Chris@0 205 }
Chris@0 206 $table[] = [$migration_id, $status, $source_rows, $imported, $unprocessed, $last_imported];
Chris@0 207 }
Chris@0 208 }
Chris@0 209 }
Chris@0 210 drush_print_table($table);
Chris@0 211 }
Chris@0 212
Chris@0 213 /**
Chris@0 214 * @param string $migration_names
Chris@0 215 */
Chris@0 216 function drush_migrate_tools_migrate_import($migration_names = '') {
Chris@0 217 $group_name = drush_get_option('group');
Chris@0 218 $all = drush_get_option('all');
Chris@0 219 $options = [];
Chris@0 220 if (!$all && !$group_name && !$migration_names) {
Chris@0 221 drush_set_error('MIGRATE_ERROR', dt('You must specify --all, --group, or one or more migration names separated by commas'));
Chris@0 222 return;
Chris@0 223 }
Chris@0 224
Chris@0 225 foreach (['limit', 'feedback', 'idlist', 'update', 'force'] as $option) {
Chris@0 226 if (drush_get_option($option)) {
Chris@0 227 $options[$option] = drush_get_option($option);
Chris@0 228 }
Chris@0 229 }
Chris@0 230
Chris@0 231 $migrations = drush_migrate_tools_migration_list($group_name, $migration_names);
Chris@0 232
Chris@0 233 // Take it one group at a time, importing the migrations within each group.
Chris@0 234 foreach ($migrations as $group_id => $migration_list) {
Chris@0 235 array_walk($migration_list, '_drush_migrate_tools_execute_migration', $options);
Chris@0 236 }
Chris@0 237 }
Chris@0 238
Chris@0 239 /**
Chris@0 240 * Executes a single migration. If the --execute-dependencies option was given,
Chris@0 241 * the migration's dependencies will also be executed first.
Chris@0 242 *
Chris@0 243 * @param \Drupal\migrate\Plugin\MigrationInterface $migration
Chris@0 244 * The migration to execute.
Chris@0 245 * @param string $migration_id
Chris@0 246 * The migration ID (not used, just an artifact of array_walk()).
Chris@0 247 * @param array $options
Chris@0 248 * Additional options for the migration.
Chris@0 249 */
Chris@0 250 function _drush_migrate_tools_execute_migration(MigrationInterface $migration, $migration_id, array $options = []) {
Chris@0 251 $log = new DrushLogMigrateMessage();
Chris@0 252
Chris@0 253 if (drush_get_option('execute-dependencies')) {
Chris@0 254 if ($required_IDS = $migration->get('requirements')) {
Chris@0 255 $manager = \Drupal::service('plugin.manager.config_entity_migration');
Chris@0 256 $required_migrations = $manager->createInstances($required_IDS);
Chris@0 257 $dependency_options = array_merge($options, ['is_dependency' => TRUE]);
Chris@0 258 array_walk($required_migrations, __FUNCTION__, $dependency_options);
Chris@0 259 }
Chris@0 260 }
Chris@0 261 if ($options['force']) {
Chris@0 262 $migration->set('requirements', []);
Chris@0 263 }
Chris@0 264 if ($options['update']) {
Chris@0 265 $migration->getIdMap()->prepareUpdate();
Chris@0 266 }
Chris@0 267 $executable = new MigrateExecutable($migration, $log, $options);
Chris@0 268 // drush_op() provides --simulate support
Chris@0 269 drush_op(array($executable, 'import'));
Chris@0 270 }
Chris@0 271
Chris@0 272 /**
Chris@0 273 * @param string $migration_names
Chris@0 274 */
Chris@0 275 function drush_migrate_tools_migrate_rollback($migration_names = '') {
Chris@0 276 $group_name = drush_get_option('group');
Chris@0 277 $all = drush_get_option('all');
Chris@0 278 $options = [];
Chris@0 279 if (!$all && !$group_name && !$migration_names) {
Chris@0 280 drush_set_error('MIGRATE_ERROR', dt('You must specify --all, --group, or one or more migration names separated by commas'));
Chris@0 281 return;
Chris@0 282 }
Chris@0 283
Chris@0 284 if (drush_get_option('feedback')) {
Chris@0 285 $options['feedback'] = drush_get_option('feedback');
Chris@0 286 }
Chris@0 287
Chris@0 288 $log = new DrushLogMigrateMessage();
Chris@0 289
Chris@0 290 $migrations = drush_migrate_tools_migration_list($group_name, $migration_names);
Chris@0 291
Chris@0 292 // Take it one group at a time, rolling back the migrations within each group.
Chris@0 293 foreach ($migrations as $group_id => $migration_list) {
Chris@0 294 // Roll back in reverse order.
Chris@0 295 $migration_list = array_reverse($migration_list);
Chris@0 296 foreach ($migration_list as $migration_id => $migration) {
Chris@0 297 $executable = new MigrateExecutable($migration, $log, $options);
Chris@0 298 // drush_op() provides --simulate support.
Chris@0 299 drush_op(array($executable, 'rollback'));
Chris@0 300 }
Chris@0 301 }
Chris@0 302 }
Chris@0 303
Chris@0 304 /**
Chris@0 305 * @param string $migration_id
Chris@0 306 */
Chris@0 307 function drush_migrate_tools_migrate_stop($migration_id = '') {
Chris@0 308 /** @var MigrationInterface $migration */
Chris@0 309 $migration = \Drupal::service('plugin.manager.migration')->createInstance($migration_id);
Chris@0 310 if ($migration) {
Chris@0 311 $status = $migration->getStatus();
Chris@0 312 switch ($status) {
Chris@0 313 case MigrationInterface::STATUS_IDLE:
Chris@0 314 drush_log(dt('Migration @id is idle', ['@id' => $migration_id]), 'warning');
Chris@0 315 break;
Chris@0 316 case MigrationInterface::STATUS_DISABLED:
Chris@0 317 drush_log(dt('Migration @id is disabled', ['@id' => $migration_id]), 'warning');
Chris@0 318 break;
Chris@0 319 case MigrationInterface::STATUS_STOPPING:
Chris@0 320 drush_log(dt('Migration @id is already stopping', ['@id' => $migration_id]), 'warning');
Chris@0 321 break;
Chris@0 322 default:
Chris@0 323 $migration->interruptMigration(MigrationInterface::RESULT_STOPPED);
Chris@0 324 drush_log(dt('Migration @id requested to stop', ['@id' => $migration_id]), 'success');
Chris@0 325 break;
Chris@0 326 }
Chris@0 327 }
Chris@0 328 else {
Chris@0 329 drush_log(dt('Migration @id does not exist', ['@id' => $migration_id]), 'error');
Chris@0 330 }
Chris@0 331 }
Chris@0 332
Chris@0 333 /**
Chris@0 334 * @param string $migration_id
Chris@0 335 */
Chris@0 336 function drush_migrate_tools_migrate_reset_status($migration_id = '') {
Chris@0 337 /** @var MigrationInterface $migration */
Chris@0 338 $migration = \Drupal::service('plugin.manager.migration')->createInstance($migration_id);
Chris@0 339 if ($migration) {
Chris@0 340 $status = $migration->getStatus();
Chris@0 341 if ($status == MigrationInterface::STATUS_IDLE) {
Chris@0 342 drush_log(dt('Migration @id is already Idle', ['@id' => $migration_id]), 'warning');
Chris@0 343 }
Chris@0 344 else {
Chris@0 345 $migration->setStatus(MigrationInterface::STATUS_IDLE);
Chris@0 346 drush_log(dt('Migration @id reset to Idle', ['@id' => $migration_id]), 'status');
Chris@0 347 }
Chris@0 348 }
Chris@0 349 else {
Chris@0 350 drush_log(dt('Migration @id does not exist', ['@id' => $migration_id]), 'error');
Chris@0 351 }
Chris@0 352 }
Chris@0 353
Chris@0 354 /**
Chris@0 355 * @param string $migration_id
Chris@0 356 */
Chris@0 357 function drush_migrate_tools_migrate_messages($migration_id) {
Chris@0 358 /** @var MigrationInterface $migration */
Chris@0 359 $migration = \Drupal::service('plugin.manager.migration')->createInstance($migration_id);
Chris@0 360 if ($migration) {
Chris@0 361 $map = $migration->getIdMap();
Chris@0 362 $first = TRUE;
Chris@0 363 $table = [];
Chris@0 364 foreach ($map->getMessageIterator() as $row) {
Chris@0 365 unset($row->msgid);
Chris@0 366 if ($first) {
Chris@0 367 // @todo: Ideally, replace sourceid* with source key names. Or, should
Chris@0 368 // getMessageIterator() do that?
Chris@0 369 foreach ($row as $column => $value) {
Chris@0 370 $table[0][] = $column;
Chris@0 371 }
Chris@0 372 $first = FALSE;
Chris@0 373 }
Chris@0 374 $table[] = (array)$row;
Chris@0 375 }
Chris@0 376 if (empty($table)) {
Chris@0 377 drush_log(dt('No messages for this migration'), 'status');
Chris@0 378 }
Chris@0 379 else {
Chris@0 380 if (drush_get_option('csv')) {
Chris@0 381 foreach ($table as $row) {
Chris@0 382 fputcsv(STDOUT, $row);
Chris@0 383 }
Chris@0 384 }
Chris@0 385 else {
Chris@0 386 $widths = [];
Chris@0 387 foreach ($table[0] as $header) {
Chris@0 388 $widths[] = strlen($header) + 1;
Chris@0 389 }
Chris@0 390 drush_print_table($table, TRUE, $widths);
Chris@0 391 }
Chris@0 392 }
Chris@0 393 }
Chris@0 394 else {
Chris@0 395 drush_log(dt('Migration @id does not exist', ['@id' => $migration_id]), 'error');
Chris@0 396 }
Chris@0 397 }
Chris@0 398
Chris@0 399 /**
Chris@0 400 * @param string $migration_id
Chris@0 401 */
Chris@0 402 function drush_migrate_tools_migrate_fields_source($migration_id) {
Chris@0 403 /** @var MigrationInterface $migration */
Chris@0 404 $migration = \Drupal::service('plugin.manager.migration')->createInstance($migration_id);
Chris@0 405 if ($migration) {
Chris@0 406 $source = $migration->getSourcePlugin();
Chris@0 407 $table = [];
Chris@0 408 foreach ($source->fields() as $machine_name => $description) {
Chris@0 409 $table[] = [strip_tags($description), $machine_name];
Chris@0 410 }
Chris@0 411 drush_print_table($table);
Chris@0 412 }
Chris@0 413 else {
Chris@0 414 drush_log(dt('Migration @id does not exist', ['@id' => $migration_id]), 'error');
Chris@0 415 }
Chris@0 416 }
Chris@0 417
Chris@0 418 /**
Chris@0 419 * Retrieve a list of active migrations.
Chris@0 420 *
Chris@0 421 * @param string $group_id
Chris@0 422 * Group machine name - if present, return only migrations in this group.
Chris@0 423 * @param string $migration_ids
Chris@0 424 * Comma-separated list of migrations - if present, return only these migrations.
Chris@0 425 *
Chris@0 426 * @return MigrationInterface[][]
Chris@0 427 * An array keyed by migration group, each value containing an array of migrations.
Chris@0 428 */
Chris@0 429 function drush_migrate_tools_migration_list($group_id = '', $migration_ids = '') {
Chris@0 430 if (!empty($migration_ids)) {
Chris@0 431 $migration_ids = explode(',', Unicode::strtolower($migration_ids));
Chris@0 432 }
Chris@0 433 else {
Chris@0 434 $migration_ids = [];
Chris@0 435 }
Chris@0 436
Chris@0 437 $manager = \Drupal::service('plugin.manager.config_entity_migration');
Chris@0 438 $plugins = $manager->createInstances([]);
Chris@0 439 $migrations = [];
Chris@0 440 foreach ($plugins as $id => $migration) {
Chris@0 441 $configured_group_id = $migration->get('migration_group');
Chris@0 442 if (empty($configured_group_id)) {
Chris@0 443 $configured_group_id = 'default';
Chris@0 444 }
Chris@0 445 if (empty($group_id) || $group_id == $configured_group_id) {
Chris@0 446 if (empty($migration_ids) || in_array(Unicode::strtolower($id), $migration_ids)) {
Chris@0 447 $migrations[$configured_group_id][$id] = $migration;
Chris@0 448 }
Chris@0 449 }
Chris@0 450 }
Chris@0 451 return $migrations;
Chris@0 452 }