You are here

class MigrateDestinationNodeRevision in Migrate 7.2

Allows you to import revisions.

Adapted from http://www.darrenmothersele.com/blog/2012/07/16/migrating-node-revisions...

Class MigrateDestinationNodeRevision

@author darrenmothersele @author cthos

Hierarchy

Expanded class hierarchy of MigrateDestinationNodeRevision

File

plugins/destinations/node.inc, line 336
Support for node destinations.

View source
class MigrateDestinationNodeRevision extends MigrateDestinationNode {

  /**
   * Basic initialization.
   *
   * @see parent::__construct
   *
   * @param string $bundle
   *   A.k.a. the content type (page, article, etc.) of the node.
   * @param array $options
   *   Options applied to nodes.
   */
  public function __construct($bundle, array $options = array()) {
    parent::__construct($bundle, $options);
    $this->bypassDestIdCheck = TRUE;
  }

  /**
   * Get key schema for the node revision destination.
   *
   * @see MigrateDestination::getKeySchema
   *
   * @return array
   *   Returns the key schema.
   */
  public static function getKeySchema() {
    return array(
      'vid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'description' => 'ID of destination node revision',
      ),
    );
  }

  /**
   * Returns additional fields on top of node destinations.
   *
   * @param string $migration
   *   Active migration
   *
   * @return array
   *   Fields.
   */
  public function fields($migration = NULL) {
    $fields = parent::fields($migration);
    $fields['vid'] = t('Node: <a href="@doc">Revision (vid)</a>', array(
      '@doc' => 'http://drupal.org/node/1298724',
    ));
    return $fields;
  }

  /**
   * Rolls back any versions that have been created.
   *
   * @param array $vids
   *   Version ids to roll back.
   */
  public function bulkRollback(array $vids) {
    migrate_instrument_start('revision_delete_multiple');
    $this
      ->prepareRollback($vids);
    $nids = array();
    foreach ($vids as $vid) {
      if ($revision = node_load(NULL, $vid)) {
        db_delete('node_revision')
          ->condition('vid', $revision->vid)
          ->execute();
        module_invoke_all('node_revision_delete', $revision);
        field_attach_delete_revision('node', $revision);
        $nids[$revision->nid] = $revision->nid;
      }
    }
    $this
      ->completeRollback($vids);
    foreach ($nids as $nid) {
      $vid = db_select('node_revision', 'nr')
        ->fields('nr', array(
        'vid',
      ))
        ->condition('nid', $nid, '=')
        ->execute()
        ->fetchField();
      if (!empty($vid)) {
        db_update('node')
          ->fields(array(
          'vid' => $vid,
        ))
          ->condition('nid', $nid, '=')
          ->execute();
      }
    }
    migrate_instrument_stop('revision_delete_multiple');
  }

  /**
   * Overridden import method.
   *
   * This is done because parent::import will return the nid of the newly
   * created nodes. This is bad since the migrate_map_* table will have
   * nids instead of vids, which could cause a nightmare explosion on
   * rollback.
   *
   * @param stdClass $node
   *   Populated entity.
   *
   * @param stdClass $row
   *   Source information in object format.
   *
   * @return array|bool
   *   Array with newly created vid, or FALSE on error.
   *
   * @throws MigrateException
   */
  public function import(stdClass $node, stdClass $row) {

    // We're importing revisions, this should be set.
    $node->revision = 1;
    if (empty($node->nid)) {
      throw new MigrateException(t('Missing incoming nid.'));
    }
    $original_updated = $this->numUpdated;
    parent::import($node, $row);

    // Reset num updated and increment created since new revision is always an update.
    $this->numUpdated = $original_updated;
    $this->numCreated++;
    if (empty($node->vid)) {
      return FALSE;
    }
    return array(
      $node->vid,
    );
  }

}

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
MigrateDestinationNode::$bypassDestIdCheck protected property
MigrateDestinationNode::options public static function Return an options array for node destinations.
MigrateDestinationNodeRevision::bulkRollback public function Rolls back any versions that have been created. Overrides MigrateDestinationNode::bulkRollback
MigrateDestinationNodeRevision::fields public function Returns additional fields on top of node destinations. Overrides MigrateDestinationNode::fields
MigrateDestinationNodeRevision::getKeySchema public static function Get key schema for the node revision destination. Overrides MigrateDestinationNode::getKeySchema
MigrateDestinationNodeRevision::import public function Overridden import method. Overrides MigrateDestinationNode::import
MigrateDestinationNodeRevision::__construct public function Basic initialization. Overrides MigrateDestinationNode::__construct