You are here

class MigrateDestinationUser in Migrate 6.2

Same name and namespace in other branches
  1. 7.2 plugins/destinations/user.inc \MigrateDestinationUser

Destination class implementing migration into users.

Hierarchy

Expanded class hierarchy of MigrateDestinationUser

File

plugins/destinations/user.inc, line 15
Support for user destinations.

View source
class MigrateDestinationUser extends MigrateDestinationEntity {
  public static function getKeySchema() {
    return array(
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'description' => 'ID of destination user',
      ),
    );
  }

  /**
   * Return an options array for user destinations.
   *
   * @param string $language
   *  Default language for users created via this destination class.
   * @param string $text_format
   *  Default text format for users created via this destination class.
   */
  public static function options($language, $text_format) {
    return compact('language', 'text_format');
  }

  /**
   * Basic initialization
   *
   * @param array $options
   *  Options applied to comments.
   */
  public function __construct(array $options = array()) {
    parent::__construct('user', 'user', $options);
  }

  /**
   * Returns a list of fields available to be mapped for users
   *
   * @return array
   *  Keys: machine names of the fields (to be passed to addFieldMapping)
   *  Values: Human-friendly descriptions of the fields.
   */
  public function fields() {
    $fields = array();

    // First the core (users table) properties
    $fields['uid'] = t('User: <a href="@doc">Existing user ID</a>', array(
      '@doc' => 'http://drupal.org/node/1349632#uid',
    ));
    $fields['mail'] = t('User: <a href="@doc">Email address</a>', array(
      '@doc' => 'http://drupal.org/node/1349632#mail',
    ));
    $fields['name'] = t('User: <a href="@doc">Username</a>', array(
      '@doc' => 'http://drupal.org/node/1349632#name',
    ));
    $fields['pass'] = t('User: <a href="@doc">Password (plain text)</a>', array(
      '@doc' => 'http://drupal.org/node/1349632#pass',
    ));
    $fields['status'] = t('User: <a href="@doc">Status</a>', array(
      '@doc' => 'http://drupal.org/node/1349632#status',
    ));
    $fields['created'] = t('User: <a href="@doc">Registered timestamp</a>', array(
      '@doc' => 'http://drupal.org/node/1349632#created',
    ));
    $fields['access'] = t('User: <a href="@doc">Last access timestamp</a>', array(
      '@doc' => 'http://drupal.org/node/1349632#access',
    ));
    $fields['login'] = t('User: <a href="@doc">Last login timestamp</a>', array(
      '@doc' => 'http://drupal.org/node/1349632#login',
    ));
    $fields['roles'] = t('User: <a href="@doc">Role IDs</a>', array(
      '@doc' => 'http://drupal.org/node/1349632#roles',
    ));
    $fields['role_names'] = t('User: <a href="@doc">Role Names</a>', array(
      '@doc' => 'http://drupal.org/node/1349632#role_names',
    ));
    $fields['picture'] = t('User: <a href="@doc">Picture</a>', array(
      '@doc' => 'http://drupal.org/node/1349632#picture',
    ));
    $fields['signature'] = t('User: <a href="@doc">Signature</a>', array(
      '@doc' => 'http://drupal.org/node/1349632#signature',
    ));
    $fields['signature_format'] = t('User: <a href="@doc">Signature format</a>', array(
      '@doc' => 'http://drupal.org/node/1349632#signature_format',
    ));
    $fields['timezone'] = t('User: <a href="@doc">Timezone</a>', array(
      '@doc' => 'http://drupal.org/node/1349632#timezone',
    ));
    $fields['language'] = t('User: <a href="@doc">Language</a>', array(
      '@doc' => 'http://drupal.org/node/1349632#language',
    ));
    $fields['theme'] = t('User: <a href="@doc">Default theme</a>', array(
      '@doc' => 'http://drupal.org/node/1349632#theme',
    ));
    $fields['init'] = t('User: <a href="@doc">Init</a>', array(
      '@doc' => 'http://drupal.org/node/1349632#init',
    ));

    // Then add in anything provided by handlers
    $fields += migrate_handler_invoke_all('User', 'fields', $this->entityType, $this->bundle);
    return $fields;
  }

  /**
   * Delete a batch of users at once.
   *
   * @param $uids
   *  Array of user IDs to be deleted.
   */
  public function bulkRollback(array $uids) {
    migrate_instrument_start('user_delete_multiple');
    $this
      ->prepareRollback($uids);
    user_delete_multiple($uids);
    $this
      ->completeRollback($uids);
    migrate_instrument_stop('user_delete_multiple');
  }

  /**
   * Import a single user.
   *
   * @param $account
   *  Account object to build. Prefilled with any fields mapped in the Migration.
   * @param $row
   *  Raw source data object - passed through to prepare/complete handlers.
   * @return array
   *  Array of key fields (uid only in this case) of the user that was saved if
   *  successful. FALSE on failure.
   */
  public function import(stdClass $account, stdClass $row) {
    $migration = Migration::currentMigration();

    // Updating previously-migrated content?
    if (isset($row->migrate_map_destid1)) {
      if (isset($account->uid)) {
        if ($account->uid != $row->migrate_map_destid1) {
          throw new MigrateException(t("Incoming uid !uid and map destination uid !destid1 don't match", array(
            '!uid' => $account->uid,
            '!destid1' => $row->migrate_map_destid1,
          )));
        }
      }
      else {
        $account->uid = $row->migrate_map_destid1;
      }
    }
    if ($migration
      ->getSystemOfRecord() == Migration::DESTINATION) {
      if (!isset($account->uid)) {
        throw new MigrateException(t('System-of-record is DESTINATION, but no destination uid provided'));
      }
      $old_account = user_load($account->uid, TRUE);
      if (empty($old_account)) {
        throw new MigrateException(t('System-of-record is DESTINATION, but user !uid does not exist', array(
          '!uid' => $account->uid,
        )));
      }
    }
    else {
      if (!isset($account->status)) {
        $account->status = 1;
      }
      $old_account = $account;
    }

    // Convert core date columns to unix timestamp as needed.
    foreach (array(
      'created',
      'access',
      'login',
    ) as $property) {
      if (isset($account->{$property})) {
        $account->{$property} = MigrationBase::timestamp($account->{$property});
      }
      else {

        // Don't touch in the DESTINATION case, preserve the original value
        if ($migration
          ->getSystemOfRecord() != Migration::DESTINATION) {
          $account->{$property} = $_SERVER['REQUEST_TIME'];
        }
      }
    }

    // Roles must be arrays keyed by the role id, which isn't how the data
    // naturally comes in. Fix them up.
    // First, if names instead of IDs are presented, translate them
    if (!empty($account->role_names)) {
      $role_names = is_array($account->role_names) ? $account->role_names : array(
        $account->role_names,
      );
      foreach ($role_names as $role_name) {
        $rid = db_select('role', 'r')
          ->fields('r', array(
          'rid',
        ))
          ->condition('name', $role_name)
          ->execute()
          ->fetchField();
        if ($rid) {
          $account->roles[] = $rid;
        }
      }
    }
    if (!empty($account->roles)) {
      if (!is_array($account->roles)) {
        $account->roles = array(
          $account->roles,
        );
      }
      $account->roles = drupal_map_assoc($account->roles);
    }
    if (empty($account->roles) && empty($old_account->roles)) {
      $account->roles = array();
    }
    $this
      ->prepare($account, $row);
    if (isset($account->uid) && !(isset($account->is_new) && $account->is_new)) {
      $updating = TRUE;
    }
    else {
      $updating = FALSE;

      // Prepare object for a new user to be passed in user_save.
      $old_account->uid = NULL;
    }

    // If any datetime values were included, ensure that they're in timestamp format.
    if (isset($account->created)) {
      $account->created = MigrationBase::timestamp($account->created);
    }
    if (isset($account->access)) {
      $account->access = MigrationBase::timestamp($account->access);
    }
    if (isset($account->login)) {
      $account->login = MigrationBase::timestamp($account->login);
    }
    migrate_instrument_start('user_save');
    $newaccount = user_save($old_account, (array) $account);
    migrate_instrument_stop('user_save');
    if ($newaccount) {
      if ($updating) {
        $this->numUpdated++;
      }
      else {
        $this->numCreated++;
      }
      $this
        ->complete($newaccount, $row);
      $return = array(
        $newaccount->uid,
      );
    }
    else {
      $return = FALSE;
    }
    return $return;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateDestination::$numCreated protected property Maintain stats on the number of destination objects created or updated.
MigrateDestination::$numUpdated protected property
MigrateDestination::getCreated public function
MigrateDestination::getUpdated public function
MigrateDestination::resetStats public function Reset numCreated and numUpdated back to 0.
MigrateDestinationEntity::$bundle protected property The bundle (node type, vocabulary, etc.) of the destination.
MigrateDestinationEntity::$entityType protected property The entity type (node, user, taxonomy_term, etc.) of the destination.
MigrateDestinationEntity::$language protected property Default language for text fields in this destination.
MigrateDestinationEntity::$textFormat protected property Default input format for text fields in this destination.
MigrateDestinationEntity::complete public function Give handlers a shot at modifying the object (or taking additional action) after saving it.
MigrateDestinationEntity::completeRollback public function Give handlers a shot at cleaning up after an entity has been rolled back.
MigrateDestinationEntity::getBundle public function
MigrateDestinationEntity::getEntityType public function
MigrateDestinationEntity::getLanguage public function
MigrateDestinationEntity::getTextFormat public function
MigrateDestinationEntity::prepare public function Give handlers a shot at modifying the object before saving it.
MigrateDestinationEntity::prepareRollback public function Give handlers a shot at cleaning up before an entity has been rolled back.
MigrateDestinationEntity::__toString public function Derived classes must implement __toString(). Overrides MigrateDestination::__toString
MigrateDestinationUser::bulkRollback public function Delete a batch of users at once.
MigrateDestinationUser::fields public function Returns a list of fields available to be mapped for users Overrides MigrateDestination::fields
MigrateDestinationUser::getKeySchema public static function
MigrateDestinationUser::import public function Import a single user. Overrides MigrateDestination::import
MigrateDestinationUser::options public static function Return an options array for user destinations.
MigrateDestinationUser::__construct public function Basic initialization Overrides MigrateDestinationEntity::__construct