#1058186 by Owen Barton, more work on Drush script for Content Migrate.
[project/cck.git] / modules / content_migrate / includes / content_migrate.admin.inc
1 <?php
2 // $Id$
3 /**
4 * @file content_migrate.admin.inc
5 * Code to process field data migration, moved into a separate file for efficiency.
6 */
7
8 /**
9 * Determine which fields can be migrated, have already been migrated, and are
10 * unable to be migrated due to missing modules.
11 */
12 function content_migrate_get_options() {
13 $options = array('available' => array(), 'converted' => array(), 'missing' => array());
14
15 $field_values = content_migrate_get_field_values();
16 if (empty($field_values)) {
17 drupal_set_message(t('There is no D6 field information in this database.'));
18 return FALSE;
19 }
20
21 $type_names = node_type_get_names();
22 $new_fields = array_keys(field_info_fields());
23
24 // Figure out which field and widget modules are available.
25 $available_modules = array_unique(array_merge(module_implements('field_info'), module_implements('field_widget_info')));
26
27 foreach ($field_values as $field_name => $field_value) {
28 $bundles = array();
29 $missing_module = !in_array($field_value['module'], $available_modules);
30 $missing_modules = $missing_module ? array($field_value['module']) : array();
31 $instance_values = content_migrate_get_instance_values(NULL, $field_name);
32
33 // Debug
34 //dsm($field_value);
35 //dsm($instance_values);
36
37 foreach ($instance_values as $bundle => $instance_value) {
38 $bundles[] = $type_names[$bundle];
39 $label = $instance_value['label'];
40 if (!in_array($instance_value['widget']['module'], $available_modules)) {
41 $missing_module = TRUE;
42 $missing_modules[] = $instance_value['widget']['module'];
43 }
44 }
45 $data = array(
46 0 => $field_name,
47 1 => $field_value['type'],
48 2 => implode(', ', $bundles),
49 3 => empty($missing_modules) ? '' : t('Missing modules: @list', array('@list' => implode(', ', $missing_modules))),
50 );
51 if (in_array($field_name, $new_fields)) {
52 $options['converted'][$field_name] = $data;
53 }
54 // TODO, do we need to check for more than the mere presence of a module?
55 elseif ($missing_module) {
56 $options['missing'][$field_name] = $data;
57 }
58 else {
59 $options['available'][$field_name] = $data;
60 }
61 }
62 return $options;
63 }
64
65 /**
66 * Form generator for the migration selection form.
67 *
68 * @todo Make this into a nice table where you have
69 * an option to check all available fields to migrate
70 * them all at once.
71 */
72 function content_migrate_select($form, &$form_state) {
73 $form = array();
74 $options = content_migrate_get_options();
75 if (!$options) {
76 return $form;
77 }
78
79 $header = array(t('Field'), t('Field type'), t('Content type(s)'), t('Other information'));
80 $form['#tree'] = TRUE;
81 $form['available'] = array(
82 '#type' => 'fieldset',
83 '#collapsible' => TRUE,
84 '#collapsed' => count($options['available']) < 1,
85 '#title' => t('Available fields'),
86 '#description' => t('Fields that have not yet been migrated but are available for migration.'),
87 );
88 $form['available']['data'] = array(
89 '#type' => 'tableselect',
90 '#header' => $header,
91 '#options' => $options['available'],
92 '#empty' => t('No fields are available to be migrated.'),
93 );
94 $form['available']['submit'] = array(
95 '#type' => 'submit',
96 '#value' => t('Migrate selected fields'),
97 '#submit' => array('content_migrate_select_submit'),
98 );
99
100 $form['converted'] = array(
101 '#type' => 'fieldset',
102 '#collapsible' => TRUE,
103 '#collapsed' => count($options['converted']) < 1,
104 '#title' => t('Converted fields'),
105 '#description' => '<p>'. t('Fields that have already been converted. You can choose to roll them back if the conversion did not work correctly. Note that rolling fields back will completely destroy the new field tables.') . ' <span class="error"><strong>' . t('This operation cannot be undone!') . '</strong></span>',
106 );
107 $form['converted']['data'] = array(
108 '#type' => 'tableselect',
109 '#header' => $header,
110 '#options' => $options['converted'],
111 '#empty' => t('No fields are already converted.'),
112 );
113 $form['converted']['submit'] = array(
114 '#type' => 'submit',
115 '#value' => t('Roll back selected fields'),
116 '#submit' => array('content_migrate_rollback_submit'),
117 );
118
119 $form['missing'] = array(
120 '#type' => 'fieldset',
121 '#collapsible' => TRUE,
122 '#collapsed' => count($options['missing']) < 1,
123 '#title' => t('Unavailable fields'),
124 '#description' => t('Fields that cannot be migrated because some modules are missing.'),
125 );
126 $form['missing']['data'] = array(
127 '#type' => 'tableselect',
128 '#header' => $header,
129 '#options' => $options['missing'],
130 '#empty' => t('No fields have missing modules.'),
131 );
132
133 return $form;
134 }
135
136 /**
137 * Submit handler.
138 *
139 * @TODO add a confirmation on the rollback submission.
140 */
141 function content_migrate_rollback_submit($form, &$form_state) {
142 $field_names = array_filter($form_state['values']['converted']['data']);
143 }
144
145 /**
146 * Helper function to perform rollback.
147 */
148 function content_migrate_rollback($field_names) {
149 foreach ($field_names as $field_name) {
150 $field = field_info_field($field_name);
151
152 // Deleting the field only marks it for deletion.
153 field_delete_field($field_name);
154
155 // We are bypassing the field batch processing
156 // and simply deleting all the data.
157 // The assumption is that the migration was
158 // unsuccessful and will be re-attempted
159 // and we need to remove all traces of the
160 // new field for later migrations to work.
161 $new_table = content_migrate_new_table($field);
162 db_drop_table($new_table);
163
164 $instances = field_read_instances(array('field_id' => $field['id']), array('include_deleted' => 1));
165 foreach ($instances as $instance) {
166 field_purge_instance($instance);
167 }
168 field_purge_field($field);
169 drupal_set_message(t('Rolling back @field_name.', array('@field_name' => $field_name)));
170 }
171 }
172
173 /**
174 * Submit handler.
175 */
176 function content_migrate_select_submit($form, &$form_state) {
177 $field_names = array_filter($form_state['values']['available']['data']);
178 _content_migrate_batch($field_names);
179 }
180
181 /**
182 * Helper function to create a batch.
183 */
184 function _content_migrate_batch($field_names) {
185 $batch = array(
186 'title' => t('Migrating data'),
187 'file' => drupal_get_path('module', 'content_migrate') . '/includes/content_migrate.admin.inc',
188 'operations' => array(
189 array('_content_migrate_batch_process_create_fields', array($field_names)),
190 ),
191 'finished' => "Field migration is finished",
192 'init_message' => t("Fields migration is starting."),
193 'progress_message' => t('Processed @current out of @total.'),
194 'error_message' => t('Field migration has encountered an error.'),
195 );
196 // Migrate field data one field at a time.
197 foreach ($field_names as $field_name) {
198 $batch['operations'][] = array('_content_migrate_batch_process_migrate_data', array($field_name));
199 }
200 batch_set($batch);
201 }
202
203 /**
204 * Batch operation callback to create fields.
205 */
206 function _content_migrate_batch_process_create_fields($field_names, &$context) {
207 $type_names = node_type_get_names();
208 foreach ($field_names as $field_name) {
209 $context['message'] = t('"Creating field: %field', array('%field' => $field_name));
210 $field_value = content_migrate_get_field_values($field_name);
211
212 // Create the field and store the new field
213 // definition in $context so we can retrieve it later.
214 try {
215 // A shared field may already have been created, check first.
216 $field = field_info_field('node', $field_value['field_name']);
217 if (empty($field)) {
218 unset($field_value['columns']);
219 unset($field_value['db_storage']);
220 $field = field_create_field($field_value);
221 $context['fields'][$field_name] = $field;
222 drupal_set_message(t("Created field @field_name", array('@field_name' => $field_name)));
223 }
224
225 // Create each of the new instances and store
226 // the new instance definitions in $context.
227 $instance_values = content_migrate_get_instance_values(NULL, $field_name);
228
229 foreach ($instance_values as $bundle => $instance_value) {
230 try {
231 $instance = field_create_instance($instance_value);
232 $context['instances'][$field_name][$instance['bundle']] = $instance;
233 drupal_set_message(t("Created instance of @field_name in bundle @bundle.", array(
234 '@field_name' => $field_name, '@bundle' => $type_names[$instance['bundle']])));
235
236 }
237 catch (Exception $e) {
238 drupal_set_message(t('Error creating instance of @field_name in bundle @bundle.', array(
239 '@field_name' => $field_name, '@bundle' => $type_names[$instance_value['bundle']])), 'error');
240 drupal_set_message($e, 'error');
241 }
242 }
243 }
244 catch (Exception $e) {
245 drupal_set_message(t("Error creating field @field_name", array('@field_name' => $field_name)), 'error');
246 drupal_set_message($e, 'error');
247 }
248 field_info_cache_clear();
249
250 }
251 $context['finished'] = TRUE;
252 }
253
254 /**
255 * Batch operation callback to migrate data.
256 * Copy old table data to new field table.
257 */
258 function _content_migrate_batch_process_migrate_data($field_name, &$context) {
259
260 // The first time through, find all the nodes that have this field.
261 if (!isset($context['sandbox']['progress'])) {
262
263 $field_value = content_migrate_get_field_values($field_name);
264 $instance_values = content_migrate_get_instance_values(NULL, $field_name);
265 $types = array();
266 foreach ($instance_values as $bundle => $instance_value) {
267 $types[] = $bundle;
268 }
269 $field = field_info_field($field_name);
270 $old_table = content_migrate_old_table($field_value, $instance_value);
271 $old_cols = content_migrate_old_columns($field_value, $instance_value);
272 $new_table = content_migrate_new_table($field);
273 $new_revision_table = content_migrate_new_revision($field);
274 $new_columns = content_migrate_new_columns($field);
275 // Shared, non-multiple fields do not have a delta but are still in per-field tables.
276 $add_delta = $field_value['cardinality'] != 1 && content_migrate_storage_type($field_value, $instance_value) == CONTENT_DB_STORAGE_PER_FIELD;
277
278 $query = db_select($old_table, 'old_table', array('fetch' => PDO::FETCH_ASSOC));
279 $node_alias = $query->join('node', 'n', 'old_table.nid=n.nid');
280 $result = $query
281 ->fields($node_alias, array('title', 'type', 'vid'))
282 ->fields('old_table', array('nid'))
283 ->orderBy('nid', 'ASC')
284 ->distinct()
285 ->execute();
286
287 $nodes = array();
288 foreach ($result as $row) {
289 $nodes[] = array('nid' => $row['nid'], 'title' => $row['title'], 'type' => $row['type'], 'vid' => $row['vid']);
290 }
291 $context['sandbox']['progress'] = 0;
292 $context['sandbox']['max'] = count($nodes);
293 $context['sandbox']['nodes'] = $nodes;
294 $context['sandbox']['old_table'] = $old_table;
295 $context['sandbox']['new_table'] = $new_table;
296 $context['sandbox']['new_revision_table'] = $new_revision_table;
297 $context['sandbox']['old_cols'] = $old_cols;
298 $context['sandbox']['new_cols'] = $new_columns;
299 $context['sandbox']['types'] = $types;
300 $context['sandbox']['field'] = $field;
301 $context['sandbox']['add_delta'] = $add_delta;
302
303 }
304
305 // Process one node in each batch.
306
307 $node = array_shift($context['sandbox']['nodes']);
308 if (!$node) {
309 return;
310 }
311
312 // Construct an record to insert into the new field table
313 // from the data in the old table.
314
315 $query = db_select($context['sandbox']['old_table'], 'old_table', array('fetch' => PDO::FETCH_ASSOC));
316
317 // We need new columns for bundle name, entity type, and language.
318 $query->addExpression("'". $node['type'] ."'", 'bundle');
319 $query->addExpression("'node'", 'entity_type');
320 $query->addExpression("'". LANGUAGE_NONE ."'", 'language');
321
322 // There are new names for what were the nid and vid columns.
323 $query->addField('old_table', 'nid', 'entity_id');
324 $query->addField('old_table', 'vid', 'revision_id');
325
326 // Add the field columns to the select query.
327 // Use the new column names as aliases in case the
328 // name changed, hopefully none did.
329 foreach ($context['sandbox']['old_cols'] as $key => $col) {
330 $query->addField('old_table', $col, $context['sandbox']['new_cols'][$key]);
331 }
332
333 // Add delta, or construct it if missing.
334 if ($context['sandbox']['add_delta']) {
335 $query->addField('old_table', 'delta', 'delta');
336 }
337 else {
338 $query->addExpression(0, 'delta');
339 }
340 $query->condition('nid', $node['nid']);
341 $result = $query->execute();
342
343 foreach ($result as $record) {
344
345 // Let modules alter this before the insert.
346 drupal_alter('content_migrate_data_record', $record, $context['sandbox']['field']);
347
348 if (!empty($record)) {
349 if ($record['revision_id'] == $node['vid']) {
350 drupal_write_record($context['sandbox']['new_table'], $record);
351 }
352 drupal_write_record($context['sandbox']['new_revision_table'], $record);
353 }
354 }
355
356 // Update our progress information.
357 $context['sandbox']['progress']++;
358 $context['message'] = t('Processing %nid : %title', array('%title' => $node['title'], '%nid' => $node['nid']));
359
360 // Inform the batch engine that we are not finished,
361 // and provide an estimation of the completion level we reached.
362 if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
363 $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
364 }
365
366 }
367
368