4 * @file content_migrate.admin.inc
5 * Code to process field data migration, moved into a separate file for efficiency.
9 * Determine which fields can be migrated, have already been migrated, and are
10 * unable to be migrated due to missing modules.
12 function content_migrate_get_options() {
13 $options = array('available' => array(), 'converted' => array(), 'missing' => array());
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.'));
21 $type_names = node_type_get_names();
22 $new_fields = array_keys(field_info_fields());
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')));
27 foreach ($field_values as
$field_name => $field_value) {
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);
35 //dsm($instance_values);
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'];
47 1 => $field_value['type'],
48 2 => implode(', ', $bundles),
49 3 => empty($missing_modules) ?
'' : t('Missing modules: @list', array('@list' => implode(', ', $missing_modules))),
51 if (in_array($field_name, $new_fields)) {
52 $options['converted'][$field_name] = $data;
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;
59 $options['available'][$field_name] = $data;
66 * Form generator for the migration selection form.
68 * @todo Make this into a nice table where you have
69 * an option to check all available fields to migrate
72 function content_migrate_select($form, &$form_state) {
74 $options = content_migrate_get_options();
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.'),
88 $form['available']['data'] = array(
89 '#type' => 'tableselect',
91 '#options' => $options['available'],
92 '#empty' => t('No fields are available to be migrated.'),
94 $form['available']['submit'] = array(
96 '#value' => t('Migrate selected fields'),
97 '#submit' => array('content_migrate_select_submit'),
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>',
107 $form['converted']['data'] = array(
108 '#type' => 'tableselect',
109 '#header' => $header,
110 '#options' => $options['converted'],
111 '#empty' => t('No fields are already converted.'),
113 $form['converted']['submit'] = array(
115 '#value' => t('Roll back selected fields'),
116 '#submit' => array('content_migrate_rollback_submit'),
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.'),
126 $form['missing']['data'] = array(
127 '#type' => 'tableselect',
128 '#header' => $header,
129 '#options' => $options['missing'],
130 '#empty' => t('No fields have missing modules.'),
139 * @TODO add a confirmation on the rollback submission.
141 function content_migrate_rollback_submit($form, &$form_state) {
142 $field_names = array_filter($form_state['values']['converted']['data']);
146 * Helper function to perform rollback.
148 function content_migrate_rollback($field_names) {
149 foreach ($field_names as
$field_name) {
150 $field = field_info_field($field_name);
152 // Deleting the field only marks it for deletion.
153 field_delete_field($field_name);
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);
164 $instances = field_read_instances(array('field_id' => $field['id']), array('include_deleted' => 1));
165 foreach ($instances as
$instance) {
166 field_purge_instance($instance);
168 field_purge_field($field);
169 drupal_set_message(t('Rolling back @field_name.', array('@field_name' => $field_name)));
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);
182 * Helper function to create a batch.
184 function _content_migrate_batch($field_names) {
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)),
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.'),
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));
204 * Batch operation callback to create fields.
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);
212 // Create the field and store the new field
213 // definition in $context so we can retrieve it later.
215 // A shared field may already have been created, check first.
216 $field = field_info_field('node', $field_value['field_name']);
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)));
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);
229 foreach ($instance_values as
$bundle => $instance_value) {
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']])));
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');
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');
248 field_info_cache_clear();
251 $context['finished'] = TRUE
;
255 * Batch operation callback to migrate data.
256 * Copy old table data to new field table.
258 function _content_migrate_batch_process_migrate_data($field_name, &$context) {
260 // The first time through, find all the nodes that have this field.
261 if (!isset($context['sandbox']['progress'])) {
263 $field_value = content_migrate_get_field_values($field_name);
264 $instance_values = content_migrate_get_instance_values(NULL
, $field_name);
266 foreach ($instance_values as
$bundle => $instance_value) {
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
;
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');
281 ->fields($node_alias, array('title', 'type', 'vid'))
282 ->fields('old_table', array('nid'))
283 ->orderBy('nid', 'ASC')
288 foreach ($result as
$row) {
289 $nodes[] = array('nid' => $row['nid'], 'title' => $row['title'], 'type' => $row['type'], 'vid' => $row['vid']);
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;
305 // Process one node in each batch.
307 $node = array_shift($context['sandbox']['nodes']);
312 // Construct an record to insert into the new field table
313 // from the data in the old table.
315 $query = db_select($context['sandbox']['old_table'], 'old_table', array('fetch' => PDO
::FETCH_ASSOC
));
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');
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');
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]);
333 // Add delta, or construct it if missing.
334 if ($context['sandbox']['add_delta']) {
335 $query->addField('old_table', 'delta', 'delta');
338 $query->addExpression(0, 'delta');
340 $query->condition('nid', $node['nid']);
341 $result = $query->execute();
343 foreach ($result as
$record) {
345 // Let modules alter this before the insert.
346 drupal_alter('content_migrate_data_record', $record, $context['sandbox']['field']);
348 if (!empty($record)) {
349 if ($record['revision_id'] == $node['vid']) {
350 drupal_write_record($context['sandbox']['new_table'], $record);
352 drupal_write_record($context['sandbox']['new_revision_table'], $record);
356 // Update our progress information.
357 $context['sandbox']['progress']++;
358 $context['message'] = t('Processing %nid : %title', array('%title' => $node['title'], '%nid' => $node['nid']));
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'];