diff --git a/workbench_moderation.migrate.inc b/workbench_moderation.migrate.inc old mode 100644 new mode 100755 index 2d2e322..9f8ebee --- a/workbench_moderation.migrate.inc +++ b/workbench_moderation.migrate.inc @@ -2,18 +2,58 @@ class WorkbenchModerationMigrateDestinationHandler extends MigrateDestinationHandler { - public function __construct() {} - - public function handlesType($destination) { - return ($destination == 'Node'); + public function __construct() { + $this->registerTypes(array('node')); } + /** + * Implements MigrationDestinationHandler::fields(). + */ public function fields($entity_type, $bundle_type) { - $fields = array(); - + $fields = array(); if (workbench_moderation_node_type_moderated($bundle_type)) { $fields['workbench_moderation_state_new'] = t('Moderation state'); } return $fields; } + + /** + * Implements MigrationDestinationHandler::prepare(). + */ + public function prepare($entity, $row) { + // Prevents workbench_moderation_moderate from being called, which would + // result in every node being resaved at shutdown time. We will do the + // necessary history record writing in complete(). + if (workbench_moderation_node_type_moderated($entity->type)) { + $entity->revision = FALSE; + + if (isset($entity->workbench_moderation_state_new)) { + $entity->workbench_moderation_state_current = + $entity->workbench_moderation_state_new; + } + } + } + + /** + * Implements MigrationDestinationHandler::complete(). + */ + public function complete($entity, $row) { + if (workbench_moderation_node_type_moderated($entity->type)) { + workbench_moderation_save_history($entity, + $entity->workbench_moderation_state_new, + $entity->workbench_moderation['my_revision']->state); + } + } +} + +/** + * Implements hook_migrate_api(). + */ +function workbench_moderation_migrate_api() { + return array( + 'api' => 2, + 'destination handlers' => array( + 'WorkbenchModerationMigrateDestinationHandler' + ), + ); } diff --git a/workbench_moderation.module b/workbench_moderation.module old mode 100644 new mode 100755 index 26ea794..103c4f3 --- a/workbench_moderation.module +++ b/workbench_moderation.module @@ -1642,55 +1642,9 @@ function workbench_moderation_states_next($current_state, $account = NULL, $node * The new moderation state requested. */ function workbench_moderation_moderate($node, $state) { - global $user; - $old_revision = $node->workbench_moderation['my_revision']; - // Get the number of revisions for this node with vids greater than $node->vid - $vid_count = db_select('node_revision', 'r') - ->condition('r.nid', $node->nid) - ->condition('r.vid', $node->vid, '>') - ->countQuery()->execute()->fetchField(); - // If the number of greater vids is 0, then this is the most current revision - $current = ($vid_count == 0); - - // Build a history record. - $new_revision = (object) array( - 'from_state' => $old_revision->state, - 'state' => $state, - 'nid' => $node->nid, - 'vid' => $node->vid, - 'uid' => $user->uid, - 'is_current' => $current, - 'published' => ($state == workbench_moderation_state_published()), - 'stamp' => $_SERVER['REQUEST_TIME'], - ); - - // If this is the new 'current' moderation record, it should be the only one - // flagged 'current' in {workbench_moderation_node_history}. - if ($new_revision->is_current) { - $query = db_update('workbench_moderation_node_history') - ->condition('nid', $node->nid) - ->fields(array('is_current' => 0)) - ->execute(); - } - - // If this revision is to be published, the new moderation record should be - // the only one flagged 'published' in both - // {workbench_moderation_node_history} AND {node_revision} - if ($new_revision->published) { - $query = db_update('workbench_moderation_node_history') - ->condition('nid', $node->nid) - ->fields(array('published' => 0)) - ->execute(); - $query = db_update('node_revision') - ->condition('nid', $node->nid) - ->fields(array('status' => 0)) - ->execute(); - } - - // Save the node history record. - drupal_write_record('workbench_moderation_node_history', $new_revision); + $new_revision = workbench_moderation_save_history($node, $state, $old_revision->state); // Update the node's content_moderation information so that we can publish it // if necessary. @@ -1736,6 +1690,73 @@ function workbench_moderation_moderate($node, $state) { } /** + * Save a history record for a moderated node. + * + * @param object $node + * The node being acted upon. + * @param string $new_state + * The new moderation state. + * @param string $old_state + * The former moderation state. + * + * @return object + * The new history record as saved. + */ +function workbench_moderation_save_history($node, $new_state, $old_state) { + global $user; + + // Get the number of revisions for this node with vids greater than $node->vid + $vid_count = db_select('node_revision', 'r') + ->condition('r.nid', $node->nid) + ->condition('r.vid', $node->vid, '>') + ->countQuery()->execute()->fetchField(); + // If the number of greater vids is 0, then this is the most current revision. + $current = ($vid_count == 0); + + // Build a history record. + $new_revision = (object) array( + 'from_state' => $old_state, + 'state' => $new_state, + 'nid' => $node->nid, + 'vid' => $node->vid, + 'uid' => $user->uid, + 'is_current' => $current, + 'published' => ($new_state == workbench_moderation_state_published()), + 'stamp' => $_SERVER['REQUEST_TIME'], + ); + + // If this is the new 'current' moderation record, it should be the only one + // flagged 'current' in {workbench_moderation_node_history}. + if ($new_revision->is_current) { + db_update('workbench_moderation_node_history') + ->condition('nid', $node->nid) + ->fields(array('is_current' => 0)) + ->execute(); + } + + // If this revision is to be published, the new moderation record should be + // the only one flagged 'published' in both + // {workbench_moderation_node_history} AND {node_revision}. + if ($new_revision->published) { + db_update('workbench_moderation_node_history') + ->condition('nid', $node->nid) + ->condition('vid', $node->vid, '!=') + ->fields(array('published' => 0)) + ->execute(); + db_update('node_revision') + ->condition('nid', $node->nid) + ->condition('vid', $node->vid, '!=') + ->fields(array('status' => 0)) + ->execute(); + } + + // Save the node history record. + drupal_write_record('workbench_moderation_node_history', $new_revision); + + return $new_revision; +} + +/** * Shutdown callback for saving a node revision. * * This function is called by drupal_register_shutdown_function(). @@ -2354,13 +2375,6 @@ function workbench_moderation_ctools_plugin_api($module, $api) { } /** - * Implement hook_migrate_api(). - */ -function workbench_moderation_migrate_api() { - return array('api' => 2); -} - -/** * Implements hook_entity_info(). */ function workbench_moderation_entity_info() {