Newer
Older
Karen Stevenson
committed
<?php
// $Id$
/**
* @file content_migrate.admin.inc
Karen Stevenson
committed
* Code to process field data migration, moved into a separate file for efficiency.
Karen Stevenson
committed
*/
/**
* Determine which fields can be migrated, have already been migrated, and are
* unable to be migrated due to missing modules.
Karen Stevenson
committed
*/
function content_migrate_get_options() {
Karen Stevenson
committed
$options = array('available' => array(), 'converted' => array(), 'missing' => array());
$field_values = content_migrate_get_field_values();
if (empty($field_values)) {
drupal_set_message(t('There is no D6 field information in this database.'));
return FALSE;
Karen Stevenson
committed
}
$type_names = node_type_get_names();
$new_fields = array_keys(field_info_fields());
// Figure out which field and widget modules are available.
Karen Stevenson
committed
$available_modules = array_unique(array_merge(module_implements('field_info'), module_implements('field_widget_info')));
Karen Stevenson
committed
foreach ($field_values as $field_name => $field_value) {
$bundles = array();
$missing_module = !in_array($field_value['module'], $available_modules);
Karen Stevenson
committed
$missing_modules = $missing_module ? array($field_value['module']) : array();
Karen Stevenson
committed
$instance_values = content_migrate_get_instance_values(NULL, $field_name);
Karen Stevenson
committed
// Debug
//dsm($field_value);
//dsm($instance_values);
Karen Stevenson
committed
foreach ($instance_values as $bundle => $instance_value) {
$bundles[] = $type_names[$bundle];
$label = $instance_value['label'];
if (!in_array($instance_value['widget']['module'], $available_modules)) {
$missing_module = TRUE;
Karen Stevenson
committed
$missing_modules[] = $instance_value['widget']['module'];
Karen Stevenson
committed
}
}
$data = array(
0 => $field_name,
1 => $field_value['type'],
2 => implode(', ', $bundles),
Karen Stevenson
committed
3 => empty($missing_modules) ? '' : t('Missing modules: @list', array('@list' => implode(', ', $missing_modules))),
Karen Stevenson
committed
);
if (in_array($field_name, $new_fields)) {
$options['converted'][$field_name] = $data;
}
// TODO, do we need to check for more than the mere presence of a module?
elseif ($missing_module) {
$options['missing'][$field_name] = $data;
}
else {
$options['available'][$field_name] = $data;
}
}
return $options;
}
/**
* Form generator for the migration selection form.
*
* @todo Make this into a nice table where you have
* an option to check all available fields to migrate
* them all at once.
*/
function content_migrate_select($form, &$form_state) {
$form = array();
$options = content_migrate_get_options();
if (!$options) {
return $form;
}
Karen Stevenson
committed
Karen Stevenson
committed
$header = array(t('Field'), t('Field type'), t('Content type(s)'), t('Other information'));
Karen Stevenson
committed
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
$form['#tree'] = TRUE;
$form['available'] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => count($options['available']) < 1,
'#title' => t('Available fields'),
'#description' => t('Fields that have not yet been migrated but are available for migration.'),
);
$form['available']['data'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options['available'],
'#empty' => t('No fields are available to be migrated.'),
);
$form['available']['submit'] = array(
'#type' => 'submit',
'#value' => t('Migrate selected fields'),
'#submit' => array('content_migrate_select_submit'),
);
$form['converted'] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => count($options['converted']) < 1,
'#title' => t('Converted fields'),
'#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>',
);
$form['converted']['data'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options['converted'],
'#empty' => t('No fields are already converted.'),
);
$form['converted']['submit'] = array(
'#type' => 'submit',
'#value' => t('Roll back selected fields'),
'#submit' => array('content_migrate_rollback_submit'),
);
$form['missing'] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => count($options['missing']) < 1,
'#title' => t('Unavailable fields'),
'#description' => t('Fields that cannot be migrated because some modules are missing.'),
);
$form['missing']['data'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options['missing'],
'#empty' => t('No fields have missing modules.'),
);
return $form;
}
/**
* Submit handler.
*
* @TODO add a confirmation on the rollback submission.
*/
function content_migrate_rollback_submit($form, &$form_state) {
$field_names = array_filter($form_state['values']['converted']['data']);
}
/**
* Helper function to perform rollback.
*/
function content_migrate_rollback($field_names) {
Karen Stevenson
committed
foreach ($field_names as $field_name) {
$field = field_info_field($field_name);
// Deleting the field only marks it for deletion.
field_delete_field($field_name);
// We are bypassing the field batch processing
// and simply deleting all the data.
// The assumption is that the migration was
// unsuccessful and will be re-attempted
// and we need to remove all traces of the
// new field for later migrations to work.
$new_table = content_migrate_new_table($field);
Karen Stevenson
committed
db_drop_table($new_table);
Karen Stevenson
committed
$instances = field_read_instances(array('field_id' => $field['id']), array('include_deleted' => 1));
foreach ($instances as $instance) {
field_purge_instance($instance);
}
field_purge_field($field);
drupal_set_message(t('Rolling back @field_name.', array('@field_name' => $field_name)));
}
}
/**
* Submit handler.
*/
function content_migrate_select_submit($form, &$form_state) {
$field_names = array_filter($form_state['values']['available']['data']);
_content_migrate_batch($field_names);
}
/**
* Helper function to create a batch.
*/
function _content_migrate_batch($field_names) {
$batch = array(
'title' => t('Migrating data'),
Karen Stevenson
committed
'file' => drupal_get_path('module', 'content_migrate') . '/includes/content_migrate.admin.inc',
Karen Stevenson
committed
'operations' => array(
array('_content_migrate_batch_process_create_fields', array($field_names)),
),
'finished' => "Field migration is finished",
'init_message' => t("Fields migration is starting."),
'progress_message' => t('Processed @current out of @total.'),
'error_message' => t('Field migration has encountered an error.'),
);
// Migrate field data one field at a time.
foreach ($field_names as $field_name) {
$batch['operations'][] = array('_content_migrate_batch_process_migrate_data', array($field_name));
}
batch_set($batch);
}
/**
* Batch operation callback to create fields.
*/
function _content_migrate_batch_process_create_fields($field_names, &$context) {
Karen Stevenson
committed
$type_names = node_type_get_names();
foreach ($field_names as $field_name) {
$context['message'] = t('"Creating field: %field', array('%field' => $field_name));
Karen Stevenson
committed
$field_value = content_migrate_get_field_values($field_name);
Karen Stevenson
committed
Karen Stevenson
committed
// Create the field and store the new field
// definition in $context so we can retrieve it later.
try {
// A shared field may already have been created, check first.
$field = field_info_field('node', $field_value['field_name']);
if (empty($field)) {
unset($field_value['columns']);
unset($field_value['db_storage']);
$field = field_create_field($field_value);
$context['fields'][$field_name] = $field;
drupal_set_message(t("Created field @field_name", array('@field_name' => $field_name)));
}
// Create each of the new instances and store
Karen Stevenson
committed
// the new instance definitions in $context.
$instance_values = content_migrate_get_instance_values(NULL, $field_name);
Karen Stevenson
committed
Karen Stevenson
committed
foreach ($instance_values as $bundle => $instance_value) {
try {
$instance = field_create_instance($instance_value);
Karen Stevenson
committed
$context['instances'][$field_name][$instance['bundle']] = $instance;
Karen Stevenson
committed
drupal_set_message(t("Created instance of @field_name in bundle @bundle.", array(
'@field_name' => $field_name, '@bundle' => $type_names[$instance['bundle']])));
Karen Stevenson
committed
}
catch (Exception $e) {
drupal_set_message(t('Error creating instance of @field_name in bundle @bundle.', array(
'@field_name' => $field_name, '@bundle' => $type_names[$instance_value['bundle']])), 'error');
drupal_set_message($e, 'error');
}
}
}
catch (Exception $e) {
drupal_set_message(t("Error creating field @field_name", array('@field_name' => $field_name)), 'error');
drupal_set_message($e, 'error');
}
Karen Stevenson
committed
field_info_cache_clear();
Karen Stevenson
committed
}
$context['finished'] = TRUE;
}
/**
* Batch operation callback to migrate data.
* Copy old table data to new field table.
*/
function _content_migrate_batch_process_migrate_data($field_name, &$context) {
// The first time through, find all the nodes that have this field.
if (!isset($context['sandbox']['progress'])) {
$field_value = content_migrate_get_field_values($field_name);
$instance_values = content_migrate_get_instance_values(NULL, $field_name);
$types = array();
foreach ($instance_values as $bundle => $instance_value) {
$types[] = $bundle;
}
$field = field_info_field($field_name);
Karen Stevenson
committed
$old_table = content_migrate_old_table($field_value, $instance_value);
$old_cols = content_migrate_old_columns($field_value, $instance_value);
$new_table = content_migrate_new_table($field);
$new_revision_table = content_migrate_new_revision($field);
$new_columns = content_migrate_new_columns($field);
// Shared, non-multiple fields do not have a delta but are still in per-field tables.
$add_delta = $field_value['cardinality'] != 1 && content_migrate_storage_type($field_value, $instance_value) == CONTENT_DB_STORAGE_PER_FIELD;
$query = db_select($old_table, 'old_table', array('fetch' => PDO::FETCH_ASSOC));
$node_alias = $query->join('node', 'n', 'old_table.nid=n.nid');
$result = $query
->fields($node_alias, array('title', 'type', 'vid'))
->fields('old_table', array('nid'))
->orderBy('nid', 'ASC')
Karen Stevenson
committed
->distinct()
->execute();
Karen Stevenson
committed
$nodes = array();
foreach ($result as $row) {
$nodes[] = array('nid' => $row['nid'], 'title' => $row['title'], 'type' => $row['type'], 'vid' => $row['vid']);
}
$context['sandbox']['progress'] = 0;
$context['sandbox']['max'] = count($nodes);
$context['sandbox']['nodes'] = $nodes;
$context['sandbox']['old_table'] = $old_table;
$context['sandbox']['new_table'] = $new_table;
$context['sandbox']['new_revision_table'] = $new_revision_table;
$context['sandbox']['old_cols'] = $old_cols;
$context['sandbox']['new_cols'] = $new_columns;
$context['sandbox']['types'] = $types;
$context['sandbox']['field'] = $field;
$context['sandbox']['add_delta'] = $add_delta;
}
// Process one node in each batch.
$node = array_shift($context['sandbox']['nodes']);
if (!$node) {
return;
}
// Construct an record to insert into the new field table
// from the data in the old table.
$query = db_select($context['sandbox']['old_table'], 'old_table', array('fetch' => PDO::FETCH_ASSOC));
Karen Stevenson
committed
// We need new columns for bundle name, entity type, and language.
$query->addExpression("'". $node['type'] ."'", 'bundle');
$query->addExpression("'node'", 'entity_type');
$query->addExpression("'". LANGUAGE_NONE ."'", 'language');
Karen Stevenson
committed
// There are new names for what were the nid and vid columns.
$query->addField('old_table', 'nid', 'entity_id');
$query->addField('old_table', 'vid', 'revision_id');
Karen Stevenson
committed
// Add the field columns to the select query.
// Use the new column names as aliases in case the
// name changed, hopefully none did.
foreach ($context['sandbox']['old_cols'] as $key => $col) {
$query->addField('old_table', $col, $context['sandbox']['new_cols'][$key]);
}
Karen Stevenson
committed
// Add delta, or construct it if missing.
if ($context['sandbox']['add_delta']) {
$query->addField('old_table', 'delta', 'delta');
}
else {
$query->addExpression(0, 'delta');
}
$query->condition('nid', $node['nid']);
$result = $query->execute();
foreach ($result as $record) {
// Let modules alter this before the insert.
drupal_alter('content_migrate_data_record', $record, $context['sandbox']['field']);
Karen Stevenson
committed
if (!empty($record)) {
if ($record['revision_id'] == $node['vid']) {
drupal_write_record($context['sandbox']['new_table'], $record);
}
Karen Stevenson
committed
drupal_write_record($context['sandbox']['new_revision_table'], $record);
}
}
// Update our progress information.
$context['sandbox']['progress']++;
$context['message'] = t('Processing %nid : %title', array('%title' => $node['title'], '%nid' => $node['nid']));
// Inform the batch engine that we are not finished,
// and provide an estimation of the completion level we reached.
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
Karen Stevenson
committed
}
Karen Stevenson
committed
}