Commit e44c4441 authored by KarenS's avatar KarenS
Browse files

#1058186 by Owen Barton, more work on Drush script for Content Migrate.

parent 680215e8
......@@ -140,6 +140,12 @@ function content_migrate_select($form, &$form_state) {
*/
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) {
foreach ($field_names as $field_name) {
$field = field_info_field($field_name);
......@@ -197,10 +203,10 @@ function _content_migrate_batch($field_names) {
/**
* Batch operation callback to create fields.
*/
function _content_migrate_batch_process_create_fields($field_names) {
function _content_migrate_batch_process_create_fields($field_names, &$context) {
$type_names = node_type_get_names();
foreach ($field_names as $field_name) {
$context['message'] = "creating field $field_name";
$context['message'] = t('"Creating field: %field', array('%field' => $field_name));
$field_value = content_migrate_get_field_values($field_name);
// Create the field and store the new field
......
<?php
// $Id$
/**
* @file content_migrate.admin.inc
* Code to process field data migration, moved into a separate file for efficiency.
* @file content_migrate.drush.inc
* Drush commands for content_migrate,
*/
/**
......@@ -10,8 +11,12 @@
*/
function content_migrate_drush_help($section) {
switch ($section) {
case 'content migrate:content-migrate':
return dt("Migrate all fields");
case 'content migrate:content-migrate-status':
return dt("Show field migration status");
case 'content migrate:content-migrate-fields':
return dt("Migrate some or all fields");
case 'content migrate:content-migrate-rollback':
return dt("Roll back some or all fields");
case 'content migrate:content-migrate-structure':
return dt("Migrate a single field structure");
case 'content migrate:content-migrate-data':
......@@ -24,9 +29,22 @@ function content_migrate_drush_help($section) {
* Implementation of hook_drush_command().
*/
function content_migrate_drush_command() {
$items['content-migrate'] = array(
'description' => 'Migrate fields.',
$items['content-migrate-status'] = array(
'description' => 'Show field migration status.',
);
$items['content-migrate-fields'] = array(
'description' => 'Migrate some or all fields.',
'arguments' => array(
'name' => 'An optional list of field system names. If not specified, all available fields will be migrated.',
),
);
$items['content-migrate-rollback'] = array(
'description' => 'Rollback migration of some or all fields.',
'arguments' => array(
'name' => 'An optional list of field system names. If not specified, all available fields will be migrated.',
),
);
// TODO: decide if these have purpose on their own, or if we should make them hidden.
$items['content-migrate-field-structure'] = array(
'description' => 'Migrate a single field structure.',
'arguments' => array(
......@@ -35,6 +53,9 @@ function content_migrate_drush_command() {
);
$items['content-migrate-field-data'] = array(
'description' => 'Migrate a single field data.',
'arguments' => array(
'name' => 'A field system name.',
),
);
return $items;
......@@ -43,10 +64,33 @@ function content_migrate_drush_command() {
/**
* Command callback.
*/
function drush_content_migrate() {
function drush_content_migrate_status() {
module_load_include('inc', 'content_migrate', 'includes/content_migrate.admin');
$fields = content_migrate_get_options();
$field_names = array_keys($fields['available']);
$fields_info = content_migrate_get_options();
$rows = array();
$rows[] = array(t('Status'), t('Field'), t('Field type'), t('Content type(s)'), t('Other information'));
foreach ($fields_info as $status => $fields) {
foreach ($fields as $field_name => $field) {
array_unshift($field, t(ucfirst($status)));
$rows[] = $field;
}
}
drush_print_table($rows, TRUE);
}
/**
* Command callback.
*/
function drush_content_migrate_fields() {
$field_names = drush_content_migrate_get_fields(_convert_csv_to_array(func_get_args()), 'available');
if (empty($field_names)) {
return drush_set_error('CONTENT_MIGRATE_NOTHING_TO_DO', dt('No fields to migrate.'));
}
drush_log(dt('The following fields will be migrated: !fields', array('!fields' => implode(', ', $field_names))), 'status');
if(!drush_confirm(dt('Do you really want to continue?'))) {
return drush_user_abort();
}
foreach ($field_names as $field_name) {
drush_invoke_process_args('content-migrate-field-structure', array($field_name));
......@@ -54,19 +98,62 @@ function drush_content_migrate() {
foreach ($field_names as $field_name) {
drush_invoke_process_args('content-migrate-field-data', array($field_name));
}
// TODO: make an API function for this in Drush.
_drush_log_drupal_messages();
drush_log(dt('Migration complete'), 'status');
}
function drush_content_migrate_field_structure($field_name) {
drush_log('Migrating structure: ' . $field_name);
drush_log(dt('Migrating structure for !field', array('!fields' => $field_name)), 'status');
module_load_include('inc', 'content_migrate', 'includes/content_migrate.admin');
_content_migrate_batch_process_create_fields(array($field_name));
$context = array();
_content_migrate_batch_process_create_fields(array($field_name), $context);
}
function drush_content_migrate_field_data($field_name) {
drush_log('Migrating data: ' . $field_name);
drush_log(dt('Migrating data for !field', array('!fields' => $field_name)), 'status');
module_load_include('inc', 'content_migrate', 'includes/content_migrate.admin');
$context = array(
'sandbox' => array(),
);
_content_migrate_batch_process_migrate_data($field_name, $context);
drush_log(strip_tags($context['message']), 'status');
}
/**
* Command callback.
*/
function drush_content_migrate_rollback() {
$field_names = drush_content_migrate_get_fields(_convert_csv_to_array(func_get_args()), 'converted');
if (empty($field_names)) {
return drush_set_error('CONTENT_MIGRATE_NOTHING_TO_DO', dt('No fields to roll back.'));
}
drush_log(dt('The following fields will be rolled back: !fields', array('!fields' => implode(', ', $field_names))), 'status');
drush_log(dt('Rolling fields back will completely destroy the new field tables. This operation cannot be undone!'), 'warning');
if(!drush_confirm(dt('Do you really want to continue?'))) {
return drush_user_abort();
}
content_migrate_rollback($field_names);
// Print messages here, so we can give our status at the end.
// TODO: make an API function for this in Drush.
_drush_log_drupal_messages();
drush_log(dt('Roll back complete'), 'status');
}
function drush_content_migrate_get_fields($requests = array(), $status) {
module_load_include('inc', 'content_migrate', 'includes/content_migrate.admin');
$fields_info = content_migrate_get_options();
$field_names = array();
if (!empty($fields_info[$status])) {
$field_names = array_keys($fields_info[$status]);
if (!empty($requests) && !empty($field_names)) {
$not_found = array_diff($requests, $field_names);
$field_names = array_intersect($field_names, $requests);
if (!empty($not_found)) {
return drush_set_error('CONTENT_MIGRATE_FIELD_NOT_FOUND', dt('The following fields were not found: !fields', array('!fields' => implode(', ', $not_found))));
}
}
}
return $field_names;
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment