You are here

migrate_extras.module in Migrate Extras 6

Same filename and directory in other branches
  1. 6.2 migrate_extras.module
  2. 7.2 migrate_extras.module

File

migrate_extras.module
View source
<?php

/*
 * Implementation of hook_migrate_api().
 */
function migrate_extras_migrate_api() {

  // Note we enable content (CCK) by default, since almost everyone will need
  // it, but disable the others to avoid function redeclaration errors if/when
  // those modules support migrate directly.
  $api = array(
    'api' => 1,
    'integration modules' => array(
      'content',
      'content_profile' => array(
        'status' => FALSE,
      ),
      'email_registration' => array(
        'status' => FALSE,
      ),
      'filefield' => array(
        'status' => FALSE,
      ),
      'location' => array(
        'status' => FALSE,
      ),
      'location_user' => array(
        'status' => FALSE,
      ),
      'privatemsg' => array(
        'status' => FALSE,
      ),
    ),
  );
  return $api;
}

/*
 * Migrate Extras Settings page.
 */
function migrate_extras_settings() {
  $form = array();
  $form['users'] = array(
    '#type' => 'fieldset',
    '#title' => t('Users'),
  );

  //this should be moved to migrate_module
  $form['users']['migrate_extras_use_md5'] = array(
    '#type' => 'checkbox',
    '#title' => t('MD5 password field'),
    '#default_value' => variable_get('migrate_extras_use_md5', 0),
    '#description' => t("Enable an md5 password field for users, use\n      this if you are migrating passwords that are already encrypted."),
  );
  $form['users']['migrate_extras_user_fields_all'] = array(
    '#type' => 'checkbox',
    '#title' => t("Use all user fields"),
    '#default_value' => variable_get('migrate_extras_user_fields_all', 0),
    '#description' => t('Enable all the core drupal user fields
      available, for advanced imports. None of these fields are required
      to import users. Some fields may only take data in specialized
      format. See <a href="http://api.drupal.org/api/function/user_schema">
      http://api.drupal.org/api/function/user_schema</a> for more info.'),
  );

  /*  This is already implemented at migrate module.
    $form['users']['migrate_extras_set_uid'] = array(
      '#type' => 'checkbox',
      '#title' => t("Set User ID explicitly (dangerous)"),
      '#default_value' => variable_get('migrate_extras_set_uid', 0),
      '#description' => t("This will set the user id of the imported user,
         which means that you may overwrite your existing users, including
         user 1. Use with extreme caution."),
    );
  */
  $form['node'] = array(
    '#type' => 'fieldset',
    '#title' => t('Nodes'),
  );
  $form['node']['migrate_extras_use_master_node_map'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use the migrate extras master node map.'),
    '#default_value' => variable_get('migrate_extras_use_master_node_map', 0),
    '#description' => t("This will make sure that when any node content\n      set adds a new node, that a map will be added to the 'migrate_extras_node_map'\n      table. This is good if you are using multiple content sets to import nodes and\n      want the convenience of using just one map table as the basis for\n      following imports like comments. Only use this if source ids across\n      content sets are unique and are all of integer type."),
  );
  return system_settings_form($form);
}

/**
 * Implementation of hook_menu().
 */
function migrate_extras_menu() {
  $items = array();
  $items['admin/content/migrate/extras'] = array(
    'title' => 'Extras',
    'description' => 'Migrate Extras module settings',
    'weight' => 10,
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'migrate_extras_settings',
    ),
    'access arguments' => array(
      MIGRATE_ACCESS_ADVANCED,
    ),
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * migrate_user hooks -----------------------------------------------\
 */

/**
 * Implementation of hook_migrate_destination_fields_user().
 */
function migrate_extras_migrate_fields_user($type) {
  $fields = array();

  //set md5 passwords without reencrypting them.
  if (variable_get('migrate_extras_use_md5', 0)) {
    $fields['md5pass'] = t('User: Password(md5)');
  }

  /* This is already implemented in migrate.module
    //set md5 passwords without reencrypting them.
    if (variable_get('migrate_extras_set_uid', 0)) {
      $fields['uid'] = t('User: Set UID (dangerous)');
    }
  */

  //set md5 passwords without reencrypting them.
  if (variable_get('migrate_extras_user_fields_all', 0)) {
    $fields['mode'] = t('User: Mode');

    //Per-user comment display mode (threaded vs. flat), used by the {comment} module.
    $fields['sort'] = t('User: Sort');

    // Per-user comment sort order (newest vs. oldest first), used by the {comment} module.
    $fields['threshold'] = t('User: Threshold (unused)');

    // Previously used by the {comment} module for per-user preferences; no longer used.
    $fields['theme'] = t('User: Theme');

    // User's default theme.
    $fields['signature'] = t('User: Signature');

    // User's signature.
    $fields['signature_format'] = t('User: Signature Format');

    // The {filter_formats}.format of the signature.
    // Already implemented in migrate.module

    //$fields['status'] = t('User: Status');

    // Whether the user is active(1) or blocked(0).
    $fields['timezone'] = t('User: Timezone');

    // User's timezone.
    $fields['language'] = t('User: Language');

    // User's default language.
    $fields['picture'] = t('User: Picture');

    // Path to the user's uploaded picture.
    $fields['init'] = t('User: (Init)ial Email');

    //Email address used for initial account creation.
    $fields['data'] = t('User: Data (depricated)');

    /* A serialized array of name value pairs that are related to the user.
     * Any form values posted during user edit are stored and are loaded
     * into the $user object during user_load(). Use of this field is
     * discouraged and it will likely disappear in a future version of
     * Drupal.'
     */
  }
  return $fields;
}

/**
 * Implementation of hook_migrate_destination_prepare_user().
 */
function migrate_extras_migrate_prepare_user(&$newuser, $tblinfo, $row) {

  //note new user is an array, not object

  //get the primary key (uid)
  $srckey = $tblinfo->sourcekey;
  $uid = $row->{$srckey};

  // Remove md5pass from the user object
  if (isset($newuser['md5pass'])) {

    //don't want to let this remain in the $user object or it will be stored in $user->data
    unset($newuser['md5pass']);
  }
}

/**
 * Implementation of hook_migrate_destination_fields_user().
 */
function migrate_extras_migrate_complete_user(&$account, $tblinfo, $row) {
  $errors = array();
  $md5pass = FALSE;
  if (isset($tblinfo->fields['md5pass']['srcfield'])) {
    $md5_src = $tblinfo->fields['md5pass']['srcfield'];
    if (isset($row->{$md5_src})) {
      $md5pass = $row->{$md5_src};
    }
    else {
      $errors[] = migrate_message('No MD5 password provided for this user.');
      return $errors;
    }
  }

  //restore the original md5password
  if ($account->uid && $md5pass !== FALSE && variable_get('migrate_extras_use_md5', 0)) {

    //MD5 password fix
    $ret = db_query("UPDATE {users} SET pass = '%s' WHERE uid = %d", $md5pass, $account->uid);
    $check_user = user_load($account->uid);
    if ($check_user->pass != $md5pass) {
      $errors[] = migrate_message(t("md5 encoded passwords don't match"));
    }
  }
  return $errors;
}

/*
 * MASTER NODE TABLE HOOKS -----------------------------------------/
 */

/**
 * Implementation of hook_migrate_complete_node().
 */
function migrate_extras_migrate_complete_node($node, $tblinfo, $row) {

  //store node mappings in a master node map table
  if (variable_get('migrate_extras_use_master_node_map', 0) && $node->nid) {
    $sourcekey = $tblinfo->sourcekey;
    static $maptables = array();
    if (!isset($maptables[$mcsid])) {
      $maptables[$mcsid] = migrate_map_table_name($tblinfo->mcsid);
    }

    //cehck and see if this nid is going to be added or updated.
    $needs_update = db_result(db_query('SELECT needs_update
                                        FROM {' . $maptables[$mcsid] . "}\n                                        WHERE sourceid='%s'", $row->{$sourcekey}));
    if ($needs_update == 1) {

      //it's going to updated, no need to do anything
    }
    elseif ($needs_update !== 0) {

      //it's going to inserted, we better insert into our node map too.
      db_query("INSERT INTO {migrate_extras_node_map} (sourceid, destid)\n                VALUES('%s', %d)", $row->{$sourcekey}, $node->nid);
    }
  }
}

/**
 * Implementation of hook_migrate_delete_node().
 */
function migrate_extras_migrate_delete_node($nid) {

  //delete any node mappings in the master node map table. We'll leave this on always.
  if ($nid) {
    db_query("DELETE FROM {migrate_extras_node_map} WHERE destid=%d", $nid);
  }
}

Functions

Namesort descending Description
migrate_extras_menu Implementation of hook_menu().
migrate_extras_migrate_api
migrate_extras_migrate_complete_node Implementation of hook_migrate_complete_node().
migrate_extras_migrate_complete_user Implementation of hook_migrate_destination_fields_user().
migrate_extras_migrate_delete_node Implementation of hook_migrate_delete_node().
migrate_extras_migrate_fields_user Implementation of hook_migrate_destination_fields_user().
migrate_extras_migrate_prepare_user Implementation of hook_migrate_destination_prepare_user().
migrate_extras_settings