Created
August 24, 2015 14:11
-
-
Save adamzimmermann/9d5d265c6b224e9f3387 to your computer and use it in GitHub Desktop.
Programatically Creating and Storing WordPress Migrate Migrations in Drupal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Define the WordPress blogs to be imported. | |
*/ | |
function example_wordpress_migrate_wordpress_blogs() { | |
// Any key not set here will default to the values set in the | |
// $blog_default_settings variable in the drush command. | |
$blogs = array( | |
array( | |
'domain' => 'www.example.com/site-one/', | |
), | |
array( | |
'domain' => 'www.example.com/site-two/', | |
), | |
array( | |
'domain' => 'www.test.com/', | |
), | |
); | |
return $blogs; | |
} | |
/** | |
* Implements hook_drush_command(). | |
*/ | |
function example_wordpress_migrate_drush_command() { | |
$items = array(); | |
// Creates WordPress migrations. | |
$items['example-migrate-create-wordpress-migrations'] = array( | |
'description' => 'Creates the WordPress migrations.', | |
'aliases' => array('mcwm'), | |
); | |
return $items; | |
} | |
/** | |
* Callback for WordPress migration creation drush command. | |
*/ | |
function drush_example_wordpress_migrate_create_wordpress_migrations() { | |
// Reset the file_get_stream_wrappers static cache so the 'wordpress' stream | |
// wrapper created by the wordpress_migrate module is available. | |
$wrappers_storage = &drupal_static('file_get_stream_wrappers', NULL, TRUE); | |
// The wordpress_migrate module's UI is a multi-step form that collects all | |
// configuration needed to migrate a given blog. As this form's steps are | |
// submitted and validated, an export file is downloaded for each blog and its | |
// contents are migrated. There is no easy way to export these settings or use | |
// code to provide that configuration and then trigger a migration, so the best | |
// bet is simulate the submission of those form steps with the needed data. | |
module_load_include('inc', 'migrate_ui', 'migrate_ui.wizard'); | |
// Get a list of blogs to migrate. | |
$blogs = example_migrate_wordpress_blogs(); | |
$blog_default_settings = array( | |
'source_select' => '1', | |
'domain' => '', | |
'username' => 'admin', | |
'password' => variable_get('example_migrate_wordpress_password', ''), | |
'wxr_file' => NULL, | |
'do_migration' => 0, | |
'default_author' => 'admin', | |
'page_type' => '', | |
'blog_post_type' => 'story', | |
'path_action' => 1, | |
'tag_field' => '', | |
'category_field' => '', | |
'attachment_field' => '', | |
'text_format' => 'filtered_html', | |
'text_format_comment' => 'filtered_html', | |
); | |
// Import each of the blogs. | |
foreach ($blogs as $blog_settings) { | |
// Combine the default settings and the custom per blog settings. | |
$blog_settings = array_merge($blog_default_settings, $blog_settings); | |
// Skip the import if no username or password was found. | |
if (empty($blog_settings['username']) || empty($blog_settings['password'])) { | |
$message = t('The @site-name migration was not created since no username and/or password could be found. Verify that the example_migrate_wordpress_password variable has been set.'); | |
$replacements = array( | |
'@site-name' => $blog_settings['domain'], | |
); | |
drupal_set_message(t($message, $replacements), 'warning'); | |
continue; | |
} | |
// Set the form state values. | |
$form_state['values'] = $blog_settings; | |
// Store the values so we can use them again since $form_state is | |
// a reference variable. | |
$form_state_values = $form_state['values']; | |
// Build the import form. | |
$form = drupal_get_form('migrate_ui_wizard', 'WordPressMigrateWizard'); | |
$form = migrate_ui_wizard($form, $form_state, 'WordPressMigrateWizard'); | |
// Create a Migrate Wizard object. | |
$form_state['wizard'] = new WordPressMigrateWizard(); | |
// Set the number of steps in the form. | |
$form_steps = 6; | |
// Go through all of the steps. | |
foreach (range(1, $form_steps) as $step) { | |
// Validate the form data. | |
$form_state['wizard']->formValidate($form_state); | |
// Submit the form page. | |
migrate_ui_wizard_next_submit($form, $form_state); | |
// Put any values removed from the array back in for the next step. | |
$form_state['values'] = array_merge($form_state_values, $form_state['values']); | |
} | |
// Submit the form. | |
drupal_form_submit('migrate_ui_wizard', $form_state); | |
// Save the settings into the wizard object. | |
$form_state['wizard']->formSaveSettings(); | |
// Notify the user that the migration was created successfully. | |
$replacements = array( | |
'@site-name' => $blog_settings['domain'], | |
); | |
$message = t('The @site-name migration was successfully created.', $replacements); | |
drupal_set_message($message, 'success'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment