View source
<?php
define('OG_MIGRATE_NOT_EXECUTED', 0);
define('OG_MIGRATE_EXECUTED', 1);
define('OG_MIGRATE_FAILED', 2);
function og_migrate_menu() {
$items['admin/config/group/group-migrate'] = array(
'title' => 'Organic groups migrate',
'description' => 'Migrate Organic groups data.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'og_migrate_admin',
),
'access arguments' => array(
'access administration pages',
),
);
return $items;
}
function og_migrate_ctools_plugin_directory($module, $type) {
if (!module_invoke('ctools', 'api_version', OG_MIGRATE_REQUIRED_CTOOLS_API)) {
return;
}
if ($module == 'og_migrate' && $type == 'og_migrate') {
return 'plugins/og_migrate';
}
}
function og_migrate_ctools_plugin_type() {
return array(
'og_migrate' => array(
'process' => 'og_migrate_process',
),
);
}
function og_migrate_init() {
og_migrate_register_plugins();
$item = menu_get_item();
if ($item['path'] == 'batch') {
og_migrate_get_plugins();
}
elseif (!og_migrate_is_pending() && user_access('access administration pages')) {
og_needs_migrate(FALSE);
drupal_set_message(t('There are no pending migration plugins that need to be executed. You can disable Organic groups migrate module.'));
}
}
function og_migrate_get_plugins_dependencies($plugins_name) {
$plugins_data = og_migrate_build_dependencies();
$plugins_name = array_flip(array_values($plugins_name));
$plugins_accessible = og_migrate_get_accessible_plugins();
while (list($plugin_name) = each($plugins_name)) {
if (!isset($plugins_data[$plugin_name])) {
return FALSE;
}
if (empty($plugins_accessible[$plugin_name])) {
unset($plugins_name[$plugin_name]);
}
$plugins_name[$plugin_name] = $plugins_data[$plugin_name]['sort'];
foreach (array_keys($plugins_data[$plugin_name]['requires']) as $dependency) {
if (!isset($plugins_name[$dependency])) {
$plugins_name[$dependency] = 0;
}
}
}
if (!$plugins_name) {
return FALSE;
}
arsort($plugins_name);
$plugins_name = array_keys($plugins_name);
return $plugins_name;
}
function og_migrate_build_dependencies() {
require_once DRUPAL_ROOT . '/includes/graph.inc';
$plugins = og_migrate_get_plugins();
foreach ($plugins as $plugin_name => $plugin) {
$graph[$plugin_name]['edges'] = array();
if (isset($plugin['dependencies']) && is_array($plugin['dependencies'])) {
foreach ($plugin['dependencies'] as $dependency) {
$graph[$plugin_name]['edges'][$dependency] = $dependency;
}
}
}
drupal_depth_first_search($graph);
foreach ($graph as $plugin_name => $data) {
$plugins[$plugin_name]['required_by'] = isset($data['reverse_paths']) ? $data['reverse_paths'] : array();
$plugins[$plugin_name]['requires'] = isset($data['paths']) ? $data['paths'] : array();
$plugins[$plugin_name]['sort'] = $data['weight'];
}
return $plugins;
}
function og_migrate_register_plugins() {
$names = array();
foreach (og_migrate_get_plugins() as $name => $plugin) {
$names[$name] = $name;
}
$query = db_select('og_migrate', 'ogm');
$result = $query
->fields('ogm', array(
'plugin',
))
->execute()
->fetchAll();
foreach ($result as $row) {
unset($names[$row->plugin]);
}
if ($names) {
foreach ($names as $name) {
og_migrate_write_record($name, OG_MIGRATE_NOT_EXECUTED);
}
}
}
function og_migrate_write_record($name, $status = OG_MIGRATE_EXECUTED) {
db_delete('og_migrate')
->condition('plugin', $name)
->execute();
$object = new stdClass();
$object->plugin = $name;
$object->status = $status;
drupal_write_record('og_migrate', $object);
}
function og_migrate_process(&$plugin, $info) {
$plugin += array(
'description' => '',
'access callback' => TRUE,
);
}
function og_migrate_get_plugin($plugin_name) {
ctools_include('plugins');
return ctools_get_plugins('og_migrate', 'og_migrate', $plugin_name);
}
function og_migrate_get_plugins() {
ctools_include('plugins');
return ctools_get_plugins('og_migrate', 'og_migrate');
}
function og_migrate_get_accessible_plugins() {
$plugins = array();
foreach (og_migrate_get_plugins() as $name => $plugin) {
if ($plugin['access callback'] === TRUE || call_user_func($plugin['access callback'])) {
$plugins[$name] = $plugin;
}
}
return $plugins;
}
function og_migrate_is_pending() {
$plugins = og_migrate_get_accessible_plugins();
if (!$plugins) {
return FALSE;
}
$plugin_names = array();
foreach ($plugins as $name => $plugin) {
$plugin_names[] = $name;
}
$query = db_select('og_migrate', 'ogm');
$result = $query
->fields('ogm', array(
'plugin',
))
->condition('plugin', $plugin_names, 'IN')
->condition('status', OG_MIGRATE_EXECUTED, '!=')
->countQuery()
->execute()
->fetchField();
return (bool) $result;
}
function og_migrate_7000_access() {
return db_table_exists('d6_og');
}
function og_migrate_admin() {
$form = array();
$plugins = og_migrate_get_accessible_plugins();
if ($plugins) {
$header = array(
'name' => t('Name'),
'description' => t('Description'),
'status' => t('Status'),
);
$query = db_select('og_migrate', 'ogm');
$result = $query
->fields('ogm', array(
'plugin',
'status',
))
->execute()
->fetchAll();
$status_map = og_migrate_status();
$status = array();
foreach ($result as $row) {
$status[$row->plugin] = $row->status;
}
$options = array();
foreach ($plugins as $name => $plugin) {
$options[$name] = array(
'name' => filter_xss($plugin['name']),
'description' => filter_xss($plugin['description']),
'status' => $status_map[$status[$name]],
);
}
$form['migrate'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
);
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Migrate'),
);
}
else {
$form['no-options'] = array(
'#markup' => t('There are no migration options.'),
);
}
return $form;
}
function og_migrate_admin_submit($form, &$form_state) {
if ($plugins_name = array_filter($form_state['values']['migrate'])) {
og_migrate_batch($plugins_name);
}
}
function og_migrate_batch($plugins_name) {
$plugins_name = og_migrate_get_plugins_dependencies($plugins_name);
if (!$plugins_name) {
return;
}
$plugins = og_migrate_get_plugins();
foreach ($plugins_name as $plugin_name) {
$operations[] = array(
$plugins[$plugin_name]['migrate callback'],
array(),
);
}
$batch = array(
'title' => t('Migrating Organic groups data'),
'operations' => $operations,
'finished' => 'og_migrate_finished',
'init_message' => t("Organic groups migration is starting."),
'progress_message' => t('Processed @current out of @total.'),
'error_message' => t('Organic groups migration has encountered an error.'),
'og_migrate_plugins' => $plugins_name,
);
batch_set($batch);
}
function og_migrate_create_fields() {
foreach (node_type_get_types() as $type) {
if ($type_usage = variable_get('og_content_type_usage_' . $type->type)) {
switch ($type_usage) {
case 'group':
$content_type['group'][] = $type->type;
break;
case 'group_post_standard':
case 'group_post_wiki':
$content_type['group content'][] = $type->type;
break;
}
}
}
if ($content_type) {
$fields = array(
'group' => OG_GROUP_FIELD,
'group content' => OG_AUDIENCE_FIELD,
);
foreach ($fields as $key => $field_name) {
foreach ($content_type[$key] as $type) {
og_create_field($field_name, 'node', $type);
if ($key == 'group') {
if (!field_info_field('og_description')) {
$field = array(
'field_name' => 'og_description',
'type' => 'text',
'entity_types' => array(
'node',
),
'cardinality' => 1,
);
$field = field_create_field($field);
}
if (!field_info_instance('node', 'og_description', $type)) {
$instance = array(
'field_name' => 'og_description',
'bundle' => $type,
'entity_type' => 'node',
'label' => t('Group description'),
'description' => t('This is description of the group.'),
);
field_create_instance($instance);
}
}
}
}
}
}
function og_migrate_finished($success, $results, $operations) {
$batch = batch_get();
$plugins_name = $batch['sets'][0]['og_migrate_plugins'];
if ($success) {
$message = format_plural(count($results), 'One post processed.', '@count posts processed.');
$status = OG_MIGRATE_EXECUTED;
}
else {
$message = t('Finished with an error.');
$status = OG_MIGRATE_FAILED;
}
drupal_set_message($message);
foreach ($plugins_name as $plugin_name) {
og_migrate_write_record($plugin_name, $status);
}
if (!empty($results)) {
foreach ($results as $result) {
}
}
}
function og_migrate_status() {
return array(
OG_MIGRATE_NOT_EXECUTED => t('Not executed'),
OG_MIGRATE_EXECUTED => t('Executed successfully'),
OG_MIGRATE_FAILED => t('Executed but failed'),
);
}