diff --git a/plugins/destinations/node.inc b/plugins/destinations/node.inc index 3a6be8f..bbc5d32 100644 --- a/plugins/destinations/node.inc +++ b/plugins/destinations/node.inc @@ -128,7 +128,7 @@ class MigrateDestinationNode extends MigrateDestinationEntity { public function import(stdClass $node, stdClass $row) { // Updating previously-migrated content? $migration = Migration::currentMigration(); - if (isset($row->migrate_map_destid1)) { + if (isset($row->migrate_map_destid1) && empty($row->bypass_destid_check)) { // Make sure is_new is off $node->is_new = FALSE; if (isset($node->nid)) { @@ -305,3 +305,113 @@ class MigrateDestinationNode extends MigrateDestinationEntity { return $return; } } + +/** + * Allows you to import revisions. + * + * Adapted from http://www.darrenmothersele.com/blog/2012/07/16/migrating-node-revisions-drupal-7/ + * + * Class MigrateDestinationNodeRevision + * + * @author darrenmothersele + * @author cthos + */ +class MigrateDestinationNodeRevision extends MigrateDestinationNode { + /** + * Get key schema for the node revision destination. + * + * @see MigrateDestination::getKeySchema + * + * @return array + * Returns the key schema. + */ + static public 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: Revision (vid)', 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); + 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); + } + } + $this->completeRollback($vids); + 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; + $row->bypass_destid_check = TRUE; + + 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); + } +}