You are here

class WordPressAuthorDestination in WordPress Migrate 7.2

Hierarchy

Expanded class hierarchy of WordPressAuthorDestination

File

./wordpress_author.inc, line 74
Support for migrating authors from a WordPress blog into Drupal.

View source
class WordPressAuthorDestination extends MigrateDestinationUser {

  /**
   * Whethere we will create new user accounts from authors with email
   * addresses not already registered.
   *
   * @var bool
   */
  protected $importUsers = TRUE;

  /**
   * The user ID of the account to use for any unimported authors.
   *
   * @var int
   */
  protected $defaultAuthorUid = 1;
  public function __construct($options = array()) {
    parent::__construct($options);
    $this->importUsers = $options['import_users'];
    $this->defaultAuthorUid = $options['default_author_uid'];
  }

  /**
   * Override of MigrateDestinationUser::import().
   *
   * On initial import, if the email address already exists we want to link to
   * that account, and remember that we did so. On updates, if we see that it
   * is a linked account, we don't want to update it.
   *
   * @param stdClass $account
   * @param stdClass $row
   */
  public function import(stdClass $account, stdClass $row) {
    if (isset($row->migrate_map_destid1)) {

      // Updating the account - if it's linked, just return the ID of the linked
      // account
      $uid = db_select('wordpress_migrate_linked_authors', 'a')
        ->fields('a', array(
        'uid',
      ))
        ->condition('mail', $account->mail)
        ->execute()
        ->fetchField();
      if ($uid) {
        $this->numUpdated++;
        return array(
          $uid,
        );
      }
    }
    else {

      // Initial import - if already in the users table, add to our linked_authors
      // list and return the uid of the existing account.
      $uid = db_select('users', 'u')
        ->fields('u', array(
        'uid',
      ))
        ->condition('mail', $account->mail)
        ->execute()
        ->fetchField();
      if (!$uid) {

        // This user does not yet exist on the site. See if we're supposed to
        // create it.
        $create_new_users = $this->importUsers;
        if (!$create_new_users) {

          // Link this content to the default user chosen in the import settings.
          $uid = $this->defaultAuthorUid;
        }
      }
      if ($uid) {
        db_merge('wordpress_migrate_linked_authors')
          ->key(array(
          'mail' => $account->mail,
        ))
          ->fields(array(
          'uid' => $uid,
        ))
          ->execute();
        $this->numCreated++;
        return array(
          $uid,
        );
      }
    }

    // If there's no linkage, do the normal thing and create a new user.
    return parent::import($account, $row);
  }

  /**
   * Override of MigrateDestinationUser::bulkRollback().
   *
   * We want to make sure we don't delete any users who existed before we first
   * imported.
   *
   * @param $uids
   *  Array of user IDs to be deleted.
   */
  public function bulkRollback(array $uids) {

    // Make sure we only attempt each uid once - if multiple authors had the
    // same email address in WordPress, the corresponding Drupal uid will be in
    // the array twice, we'll delete the linked_authors row the first time we
    // see it, and the second pass on that uid will delete the account.
    $uids = array_unique($uids, SORT_NUMERIC);
    $delete_uids = array();
    foreach ($uids as $uid) {
      $mail = db_select('wordpress_migrate_linked_authors', 'a')
        ->fields('a', array(
        'mail',
      ))
        ->condition('uid', $uid)
        ->execute()
        ->fetchField();
      if ($mail) {
        db_delete('wordpress_migrate_linked_authors')
          ->condition('mail', $mail)
          ->execute();
      }
      else {
        $delete_uids[] = $uid;
      }
    }
    parent::bulkRollback($delete_uids);
  }

}

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::array_flatten public static function Flattens an array of allowed values.
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::fieldAttachValidate public static function Perform field validation against the field data in an entity. Wraps field_attach_validate to handle exceptions cleanly and provide maximum information for identifying the cause of validation errors.
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::$md5Passwords protected property Indicates whether incoming passwords are md5-encrypted - if so, we will rehash them similarly to the D6->D7 upgrade path.
MigrateDestinationUser::fields public function Returns a list of fields available to be mapped for users Overrides MigrateDestination::fields
MigrateDestinationUser::getKeySchema public static function
MigrateDestinationUser::options public static function Return an options array for user destinations.
WordPressAuthorDestination::$defaultAuthorUid protected property The user ID of the account to use for any unimported authors.
WordPressAuthorDestination::$importUsers protected property Whethere we will create new user accounts from authors with email addresses not already registered.
WordPressAuthorDestination::bulkRollback public function Override of MigrateDestinationUser::bulkRollback(). Overrides MigrateDestinationUser::bulkRollback
WordPressAuthorDestination::import public function Override of MigrateDestinationUser::import(). Overrides MigrateDestinationUser::import
WordPressAuthorDestination::__construct public function Basic initialization Overrides MigrateDestinationUser::__construct