-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate_plus.drush.inc
193 lines (176 loc) · 6.08 KB
/
migrate_plus.drush.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
/**
* @file
* Command-line tools to aid performing and developing migrations.
*/
use Drupal\Component\Utility\Unicode;
use Drupal\migrate\Entity\MigrationInterface;
use Drupal\migrate\MigrateExecutable;
use Drush\Migrate\DrushLogMigrateMessage;
/**
* Implements hook_drush_command().
*/
function migrate_plus_drush_command() {
$items['migrate-status'] = array(
'description' => 'List all migrations with current status.',
'options' => array(
'group' => 'Name of the migration group to list',
'names-only' => 'Only return names, not all the details (faster)',
),
'arguments' => array(
'migration' => 'Restrict to a comma-separated list of migrations. Optional',
),
'examples' => array(
'migrate-status' => 'Retrieve status for all migrations',
'migrate-status --group=beer' => 'Retrieve status for all migrations in a given group',
'migrate-status BeerTerm,BeerNode' => 'Retrieve status for specific migrations',
),
'drupal dependencies' => array('migrate_plus'),
'aliases' => array('ms'),
);
$items['migrate-import'] = array(
'description' => 'Perform one or more migration processes.',
'options' => array(
'all' => 'Process all migrations.',
'group' => 'Name of the migration group to import',
),
'arguments' => array(
'migration' => 'Name of migration(s) to import. Delimit multiple using commas.',
),
'examples' => array(
'migrate-import --all' => 'Perform all migrations',
'migrate-import --group=beer' => 'Import all migrations in the beer group',
'migrate-import BeerTerm,BeerNode' => 'Import new terms and nodes',
),
'drupal dependencies' => array('migrate_plus'),
'aliases' => array('mi'),
);
return $items;
}
/**
* @param string $migration_names
*/
function drush_migrate_plus_migrate_status($migration_names = '') {
$group_name = drush_get_option('group');
$names_only = drush_get_option('names-only');
$migrations = drush_migrate_plus_migration_list($group_name, $migration_names);
$table = array();
// Take it one group at a time, listing the migrations within each group.
foreach ($migrations as $group_id => $migration_list) {
if ($names_only) {
$table[] = array(
dt('Group: !name', array('!name' => $group_id))
);
}
else {
$table[] = array(
dt('Group: !name', array('!name' => $group_id)),
dt('Total'),
dt('Imported'),
dt('Unprocessed'),
);
}
foreach ($migration_list as $migration_id => $migration) {
try {
$map = $migration->getIdMap();
$imported = $map->importedCount();
$source_plugin = $migration->getSourcePlugin();
}
catch (Exception $e) {
continue;
}
// We can't get counts from an EmptySource plugin, or one without a query.
// @todo: Is there a more general approach? Are there other edge cases?
try {
if (is_a($source_plugin, 'Drupal\migrate\Plugin\migrate\source\EmptySource')) {
$source_rows = dt('N/A');
$unprocessed = dt('N/A');
}
else {
$source_rows = $source_plugin->count();
$unprocessed = $source_rows - $map->processedCount();
}
}
catch (Exception $e) {
drush_log(dt('Could not retrieve source count from !migration',
array('!migration' => $migration_id)));
$source_rows = dt('N/A');
$unprocessed = dt('N/A');
}
if ($names_only) {
$table[] = array($migration_id);
}
else {
$table[] = array($migration_id, $source_rows, $imported, $unprocessed);
}
}
}
drush_print_table($table);
}
/**
* @param string $migration_names
*/
function drush_migrate_plus_migrate_import($migration_names = '') {
$group_name = drush_get_option('group');
$all = drush_get_option('all');
if (!$all && !$group_name && !$migration_names) {
drush_set_error('MIGRATE_ERROR', dt('You must specify --all, --group, or one or more migration names separated by commas'));
return;
}
$log = new DrushLogMigrateMessage();
$migrations = drush_migrate_plus_migration_list($group_name, $migration_names);
// Take it one group at a time, importing the migrations within each group.
foreach ($migrations as $group_id => $migration_list) {
foreach ($migration_list as $migration_id => $migration) {
$executable = new MigrateExecutable($migration, $log);
// drush_op() provides --simulate support.
drush_op(array($executable, 'import'));
}
}
}
/**
* Retrieve a list of active migrations.
*
* @param string $group_id
* Group machine name - if present, return only migrations in this group.
* @param string $migration_ids
* Comma-separated list of migrations - if present, return only these migrations.
*
* @return MigrationInterface[][]
* An array keyed by migration group, each value containing an array of migrations.
*/
function drush_migrate_plus_migration_list($group_id = '', $migration_ids = '') {
$query = \Drupal::entityQuery('migration');
if ($group_id) {
$query->condition('migration_groups.*', $group_id);
}
$names = $query->execute();
// Order the migrations according to their dependencies.
/** @var MigrationInterface[] $migrations */
$migrations = \Drupal::entityManager()
->getStorage('migration')
->loadMultiple($names);
if (!empty($migration_ids)) {
$migration_ids = explode(',', Unicode::strtolower($migration_ids));
}
else {
$migration_ids = array();
}
$return = array();
foreach ($migrations as $migration_id => $migration) {
if (empty($migration_ids) || in_array(Unicode::strtolower($migration_id), $migration_ids)) {
if (is_array($migration->migration_groups)) {
$group_id = reset($migration->migration_groups);
// @todo: Temporary - ignore incomplete migrate_drupal migrations.
if (substr($group_id, 0, 7) == 'Drupal ' && !$migration->get('source.key')) {
continue;
}
}
else {
$group_id = 'default';
}
$return[$group_id][$migration_id] = $migration;
}
}
return $return;
}