From 443bc3d864d8a04095aceba5b2e1d7f229fa690b Mon Sep 17 00:00:00 2001
From: Thijs van den Anker <thijs@bitsnbolts.nl>
Date: Wed, 17 Jun 2015 10:05:47 +0200
Subject: [PATCH 1/2] 1445824-34

---
 workbench_moderation.migrate.inc |  46 +++++++++++++--
 workbench_moderation.module      | 124 ++++++++++++++++++++++-----------------
 2 files changed, 110 insertions(+), 60 deletions(-)

diff --git a/workbench_moderation.migrate.inc b/workbench_moderation.migrate.inc
index 2d2e322..0971dcd 100644
--- a/workbench_moderation.migrate.inc
+++ b/workbench_moderation.migrate.inc
@@ -2,18 +2,52 @@
 
 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().
+    $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) {
+    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
index 5a2ff16..f67c7ff 100644
--- a/workbench_moderation.module
+++ b/workbench_moderation.module
@@ -1624,55 +1624,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.
@@ -1718,6 +1672,75 @@ function workbench_moderation_moderate($node, $state) {
 }
 
 /**
+ * Save a history record for a moderated node.
+ *
+ * @param $node
+ *  The node being acted upon.
+ *
+ * @param $new_state
+ *  The new moderation state.
+ *
+ * @param $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().
@@ -2336,13 +2359,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() {
-- 
2.3.2 (Apple Git-55)


From 8c5a7e800fff76f1a88bd10aaeffe56acc2cd715 Mon Sep 17 00:00:00 2001
From: Thijs van den Anker <thijs@bitsnbolts.nl>
Date: Wed, 17 Jun 2015 10:24:16 +0200
Subject: [PATCH 2/2] Check if entity type is using moderation

---
 workbench_moderation.migrate.inc | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/workbench_moderation.migrate.inc b/workbench_moderation.migrate.inc
index 0971dcd..ca25f77 100644
--- a/workbench_moderation.migrate.inc
+++ b/workbench_moderation.migrate.inc
@@ -24,11 +24,13 @@ class WorkbenchModerationMigrateDestinationHandler extends MigrateDestinationHan
     // 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().
-    $entity->revision = FALSE;
+    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;
+      if (isset($entity->workbench_moderation_state_new)) {
+        $entity->workbench_moderation_state_current =
+          $entity->workbench_moderation_state_new;
+      }
     }
   }
 
@@ -36,9 +38,11 @@ class WorkbenchModerationMigrateDestinationHandler extends MigrateDestinationHan
    * Implements MigrationDestinationHandler::complete().
    */
   public function complete($entity, $row) {
-    workbench_moderation_save_history($entity,
-      $entity->workbench_moderation_state_new,
-      $entity->workbench_moderation['my_revision']->state);
+    if (workbench_moderation_node_type_moderated($entity->type)) {
+      workbench_moderation_save_history($entity,
+        $entity->workbench_moderation_state_new,
+        $entity->workbench_moderation['my_revision']->state);
+    }
   }
 }
 
-- 
2.3.2 (Apple Git-55)

