Index: revision_moderation.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/revision_moderation/revision_moderation.module,v
retrieving revision 1.49
diff -u -r1.49 revision_moderation.module
--- revision_moderation.module	27 Dec 2008 22:27:36 -0000	1.49
+++ revision_moderation.module	12 Dec 2010 17:28:45 -0000
@@ -87,10 +97,10 @@
  */
 function revision_moderation_form_alter(&$form, $form_state, $form_id) {
   // On node edit forms, add in the "New revisions in moderation" option.
-  if (isset($form['#id']) && $form['#id'] == 'node-form') {
+  if (isset($form['type']) && $form['type']['#value'] . '_node_form' == $form_id) {
     $default_value = in_array('revision_moderation', variable_get("node_options_{$form['type']['#value']}", array('status', 'promote')));
     if ($form['nid']['#value']) {
-      $result = db_result(db_query('SELECT revision_moderation FROM {revision_moderation} WHERE nid = %d', $form['nid']['#value']));
+      $result = db_query('SELECT revision_moderation FROM {revision_moderation} WHERE nid = :nid', array(':nid' => $form['nid']['#value']))->fetchField();
       if ($result !== FALSE) {
         $default_value = $result;
       }
@@ -117,126 +127,217 @@
 }
 
 /**
- * Implementation of hook_nodeapi().
+ * Implementation of hook_node_insert().
+ */
+function revision_moderation_node_insert($node) {
+  // Store revision moderation setting of this node.
+  drupal_write_record('revision_moderation', $node);
+}
+
+/**
+ * Implementation of hook_node_update().
+ */
+function revision_moderation_node_update($node) {
+  $args = arg();
+
+  // Update revision moderation setting of this node.
+  drupal_write_record('revision_moderation', $node, 'nid');
+
+  // Only do this logic for non-admin users on nodes with revision moderation
+  // turned on.
+  // And not editing a chose revision
+  if ($node->nid && $node->revision_moderation == 1 && arg(2) != 'revisions' && (!user_access('administer nodes') || !variable_get('revision_moderation_exempt', 1))) {
+    if (isset($node->original_node)) {
+      // Update node table's vid to the original value.
+
+      db_update('node')
+      ->fields(array(
+        'vid' => $node->original_node->vid,
+        'title' => $node->original_node->title,
+        'status' => $node->original_node->status,
+        ))
+      ->condition('nid',$node->nid,'=')
+      ->execute();
+      // If node doesn't exist in revision_moderation table, add it.
+      $in_db = db_query('SELECT revision_moderation FROM {revision_moderation} WHERE nid = :nid', array(':nid' => $node->nid))->fetchField();
+      if ($in_db === FALSE) {
+        db_insert('revision_moderation')
+        ->fields(array(
+          'nid' => $node->nid,
+          'revision_moderation' => 1,
+          ))
+        ->execute();
+      }
+
+      drupal_set_message(t('Your changes have been submitted for moderation.'));
+    }
+  }
+  else if ($node->nid && $node->revision_moderation == 1 && end($args) == 'edit') {
+    if (isset($node->original_node)) {
+      // Exempt admin users publish their version immediately
+      if(!user_access('administer nodes') || !variable_get('revision_moderation_exempt', 1)) {
+        // Update node table's vid to the original value.
+
+        db_update('node')
+        ->fields(array(
+          'vid' => $node->original_node->vid,
+          'title' => $node->original_node->title,
+          'status' => $node->original_node->status,
+          ))
+        ->condition('nid',$node->nid,'=')
+        ->execute();
+        drupal_set_message(t('Your changes have been submitted for moderation.'));
+      }
+    }
+  }
+
+}
+
+/**
+ * Implementation of hook_node_delete().
+ */
+function revision_moderation_node_delete($node) {
+  // Delete record from revision_moderation table when node is deleted.
+  db_query('DELETE FROM {revision_moderation} WHERE nid = :nid', array(':nid' => $node->nid));
+}
+
+/**
+ * Implementation of hook_node_load().
+ */
+function revision_moderation_node_load($nodes, $types) {
+  // Set a revision_moderation property which can be checked later.
+  foreach ($nodes as $node) {
+    $node->revision_moderation = db_query('SELECT revision_moderation FROM {revision_moderation} WHERE nid = :nid', array(':nid' => $node->nid))->fetchField();
+  }
+}
+
+/**
+ * Implementation of hook_node_view().
  */
-function revision_moderation_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
-  switch ($op) {
-    case 'insert':
-      // Store revision moderation setting of this node.
-      drupal_write_record('revision_moderation', $node);
-      break;
-
-    case 'update':
-      // Update revision moderation setting of this node.
-      drupal_write_record('revision_moderation', $node, 'nid');
-      break;
-
-    case 'delete':
-      // Delete record from revision_moderation table when node is deleted.
-      db_query('DELETE FROM {revision_moderation} WHERE nid = %d', $node->nid);
-      break;
-
-    case 'load':
-      // Set a revision_moderation property which can be checked later.
-      $node->revision_moderation = db_result(db_query('SELECT revision_moderation FROM {revision_moderation} WHERE nid = %d', $node->nid));
-      break;
-
-    case 'view':
-      // Cannot use _node_revision_access() here, it's static cached with 1 op
-      $access_update = user_access('revert revisions');
-      $access_delete = user_access('delete revisions');
-      // Display more descriptive message at the top of node revision views, including operations
-      // that the current user has available to them.
-      $current_vid = db_result(db_query('SELECT vid FROM {node} WHERE nid = %d', $node->nid));
-      if ($node->vid != $current_vid) {
-        $links = array(); // Array of links to show along with the message.
-        if ($access_update) {
-          // Add a link directly to the diff if we have Diff module installed.
-          if (module_exists('diff')) {
-            if ($node->vid > $current_vid) {
-             $difflink = "node/$node->nid/revisions/view/$current_vid/$node->vid";
-            }
-            else {
-             $difflink = "node/$node->nid/revisions/view/$node->vid/$current_vid";
-            }
-            $links[] = l(t('Compare revisions'), $difflink);
-          }
-          $links[] = l(t('Edit revision'), "node/$node->nid/revisions/$node->vid/edit");
-          // If this revision is old, show an option to revert to it.
-          // Otherwise, show an option to publish it.
-          if ($node->vid < $current_vid) {
-            $links[] = l(t('Revert to revision'), "node/$node->nid/revisions/$node->vid/revert");
-          }
-          else {
-            $links[] = l(t('Publish revision'), "node/$node->nid/revisions/$node->vid/publish");
-          }
+function revision_moderation_node_view($node, $view_mode, $langcode) {
+  // Cannot use _node_revision_access() here, it's static cached with 1 op
+  $access_update = user_access('revert revisions');
+  $access_delete = user_access('delete revisions');
+  // Display more descriptive message at the top of node revision views, including operations
+  // that the current user has available to them.
+  $current_vid = db_query('SELECT vid FROM {node} WHERE nid = :nid', array(':nid' => $node->nid))->fetchField();
+  if ($node->vid != $current_vid) {
+    $links = array(); // Array of links to show along with the message.
+    if ($access_update) {
+      // Add a link directly to the diff if we have Diff module installed.
+      if (module_exists('diff')) {
+        if ($node->vid > $current_vid) {
+          $difflink = "node/$node->nid/revisions/view/$current_vid/$node->vid";
         }
-        if ($access_delete) {
-          $links[] = l(t('Delete revision'), "node/$node->nid/revisions/$node->vid/delete");
+        else {
+          $difflink = "node/$node->nid/revisions/view/$node->vid/$current_vid";
         }
-        // Get username for the revision rather than the original node.
-        $revision_author = user_load($node->revision_uid);
-        drupal_set_message(t('You are currently viewing a revision of this post created on @date by !author.', array('@date' => format_date($node->revision_timestamp, 'small'), '!author' => theme('username', $revision_author))) . theme('item_list', $links));
+        $links[] = l(t('Compare revisions'), $difflink);
       }
-      elseif ($node->revision_moderation == 1 && !$teaser) {
-        // Notify admin if a node has pending revisions.
-        if ($access_update && arg(2) != 'revisions' && revision_moderation_get_node_pending_revisions($node->nid)) {
-          drupal_set_message(t('This post has one or more pending revisions: <a href="@list">view list of revisions</a>.', array('@list' => url("node/$node->nid/revisions"))));
-        }
+      $links[] = l(t('Edit revision'), "node/$node->nid/revisions/$node->vid/edit");
+      // If this revision is old, show an option to revert to it.
+      // Otherwise, show an option to publish it.
+      if ($node->vid < $current_vid) {
+        $links[] = l(t('Revert to revision'), "node/$node->nid/revisions/$node->vid/revert");
+      }
+      else {
+        $links[] = l(t('Publish revision'), "node/$node->nid/revisions/$node->vid/publish");
       }
-      break;
+    }
+    if ($access_delete) {
+      $links[] = l(t('Delete revision'), "node/$node->nid/revisions/$node->vid/delete");
+    }
+    // Get username for the revision rather than the original node.
+    $revision_author = user_load($node->revision_uid);
+    drupal_set_message(t('You are currently viewing a revision of this post created on @date by !author.', array('@date' => format_date($node->revision_timestamp, 'small'), '!author' => theme('username', array('username' => $revision_author)))) . theme('item_list', array('items' => $links)));
   }
+  elseif ($node->revision_moderation == 1) {
+    // Notify admin if a node has pending revisions.
+    if ($access_update && arg(2) != 'revisions' && revision_moderation_get_node_pending_revisions($node->nid)) {
+      drupal_set_message(t('This post has one or more pending revisions: <a href="@list">view list of revisions</a>.', array('@list' => url("node/$node->nid/revisions"))));
+    }
+  }
+}
+
+/**
+ * Implementation of hook_node_prepare().
+ */
+function revision_moderation_node_prepare($node) {
+  $args = arg();
 
   // Only do this logic for non-admin users on nodes with revision moderation
   // turned on.
   // And not editing a chose revision
-  if ($node->nid && $node->revision_moderation == 1 && arg(2) != 'revisions'
-    && (!user_access('administer nodes') || !variable_get('revision_moderation_exempt', 1))) {
-    switch ($op) {
-      case 'prepare':
-        // If user has a pending revision for this node, load the latest version of
-        // it instead.
-        if ($revisions = revision_moderation_get_node_pending_revisions($node->nid)) {
-          global $user;
-          foreach ($revisions as $revision) {
-            if ($revision->uid == $user->uid) {
-              drupal_set_message(t('Editing your latest revision, which is still pending moderation.'));
-              $node = node_load($node->nid, $revision->vid);
-              break;
-            }
-          }
+  if ($node->nid && $node->revision_moderation == 1 && arg(2) != 'revisions' && (!user_access('administer nodes') || !variable_get('revision_moderation_exempt', 1))) {
+    // If user has a pending revision for this node, load the latest version of
+    // it instead.
+    if ($revisions = revision_moderation_get_node_pending_revisions($node->nid)) {
+      global $user;
+      foreach ($revisions as $revision) {
+        if ($revision->uid == $user->uid) {
+          drupal_set_message(t('Editing your latest revision, which is still pending moderation.'));
+          $node = node_load($node->nid, $revision->vid);
         }
-        break;
-
-      case 'presave':
-        $current_vid = db_result(db_query('SELECT vid FROM {node} WHERE nid = %d', $node->nid));
-        $node->original_node = node_load($node->nid, $current_vid);
-        break;
-
-      case 'update':
-        if (isset($node->original_node)) {
-          // Update node table's vid to the original value.
+      }
+    }
 
-          db_query("UPDATE {node} SET vid = %d, title = '%s', status = %d, moderate = %d WHERE nid = %d", $node->original_node->vid, $node->original_node->title, $node->original_node->status, $node->original_node->moderate, $node->nid);
-          drupal_set_message(t('Your changes have been submitted for moderation.'));
+  }
+  else if ($node->nid && $node->revision_moderation == 1 && end($args) == 'edit') {
+    $revision_author = user_load($node->revision_uid);
+    $current_vid = db_query('SELECT vid FROM {node} WHERE nid = ;nid', array(':nid' => $node->nid))->fetchField();
+    if($node->vid > $current_vid) {
+      drupal_set_message(t('You are currently editing a pending update of this post created on @date by !author.', array('@date' => format_date($node->revision_timestamp, 'small'), '!author' => theme('username', $revision_author))));
+    }
+    if($node->vid == $current_vid) {
+      if(revision_moderation_get_node_pending_revisions($node->nid)) {
+        if(variable_get('revision_moderation_exempt', 1)) {
+          drupal_set_message(t('You are currently editing the current published version of this post, created on @date by !author. Note that there are other pending revisions, and that pressing save will publish this version of the post instead.', array('@date' => format_date($node->revision_timestamp, 'small'), '!author' => theme('username', $revision_author))));
         }
-        break;
+        else {
+          drupal_set_message(t('You are currently editing the current published version of this post, created on @date by !author.', array('@date' => format_date($node->revision_timestamp, 'small'), '!author' => theme('username', $revision_author))));
+        }
+      }
     }
+
   }
+
 }
 
 /**
- * Implementation of hook_block().
+ * Implementation of hook_node_presave().
  */
-function revision_moderation_block($op = 'list', $delta = 0, $edit = array()) {
-  if ($op == 'list') {
-    $blocks[0]['info'] = t('Pending revisions');
+function revision_moderation_node_presave($node) {
+  $args = arg();
 
-    return $blocks;
+  // Only do this logic for non-admin users on nodes with revision moderation
+  // turned on.
+  // And not editing a chose revision
+  if ($node->nid && $node->revision_moderation == 1 && arg(2) != 'revisions' && (!user_access('administer nodes') || !variable_get('revision_moderation_exempt', 1))) {
+    $current_vid = db_query('SELECT vid FROM {node} WHERE nid = :nid', array(':nid' => $node->nid))->fetchField();
+    $node->original_node = node_load($node->nid, $current_vid);
   }
-  elseif ($op == 'view') {
-    $block = array();
+  else if ($node->nid && $node->revision_moderation == 1 && end($args) == 'edit') {
+    $current_vid = db_query('SELECT vid FROM {node} WHERE nid = :nid', array(':nid' => $node->nid))->fetchField();
+    $node->original_node = node_load($node->nid, $current_vid);
+  }
+
+}
 
+/**
+ * Implementation of hook_block_info().
+ */
+function revision_moderation_block_info() {
+  $block['pending_revisions'] = array('info' => t('Pending revisions'));
+  return $block;
+}
+
+/**
+ * Implementation of hook_block_view().
+ */
+function revision_moderation_block_view($delta = '') {
+  switch ($delta) {
+    case 'pending_revisions':
+    $block = array();
     if (user_access('administer nodes')) {
       $output = '';
       $list = array();
@@ -246,19 +347,17 @@
         foreach ($nodes as $node) {
           $list[] = l($node->title, "node/$node->nid/revisions/$node->vid/view");
         }
-        $output .= theme('item_list', $list);
+        $output .= theme('item_list', array('list' => $list));
         $output .= '<p>'. l(t('View all pending revisions'), 'admin/content/node/revisions') .'</p>';
       }
       else {
         $output .= t('No pending revisions found.');
       }
-
       $block['subject'] = t('Pending revisions');
       $block['content'] = $output;
     }
-
-    return $block;
   }
+  return $block;
 }
 
 /**
@@ -274,7 +373,7 @@
 function revision_moderation_theme() {
   return array(
     'revision_moderation_pending_revisions_admin' => array(
-      'arguments' => array(),
+      'variables' => array(),
     ),
   );
 }
@@ -296,11 +395,11 @@
       $rows[] = array(
         l($node->title, "node/$node->nid/revisions"),
         check_plain(node_get_types('name', $node)),
-        theme('username', user_load(array('uid' => $node->uid))),
+        theme('username', array('username' => user_load(array('uid' => $node->uid)))),
         format_date($node->timestamp),
       );
     }
-    return theme('table', $header, $rows);
+    return theme('table', array('header' => $header, 'rows' => $rows));
   }
   else {
     return '<p>'. t('No pending revisions found.') .'</p>';
@@ -315,10 +414,9 @@
  */
 function revision_moderation_get_all_pending_revisions($limit) {
   // Obtain a list of nodes with revisions higher than current published revision.
-  $sql = "SELECT n.nid, r.vid, n.type, r.title, r.body, r.uid, r.timestamp FROM {node} n INNER JOIN {node_revisions} r ON n.nid = r.nid WHERE r.vid > n.vid ORDER BY r.vid DESC LIMIT %d";
-  $result = db_query($sql, $limit);
+  $result = db_query('SELECT n.nid, r.vid, n.type, r.title, r.uid, r.timestamp FROM {node} n INNER JOIN {node_revision} r ON n.nid = r.nid WHERE r.vid > n.vid ORDER BY r.vid DESC LIMIT :limit', array(':limit' => $limit));
   $revisions = array();
-  while ($revision = db_fetch_object($result)) {
+  foreach ($result as $revision) {
     $revisions[$revision->nid] = $revision;
   }
 
@@ -333,10 +431,9 @@
  */
 function revision_moderation_get_node_pending_revisions($nid) {
   // Obtain a list of revisions higher than current published revision for a given node.
-  $sql = "SELECT n.nid, r.vid, r.uid FROM {node} n INNER JOIN {node_revisions} r ON n.nid = r.nid WHERE r.vid > n.vid AND n.nid = %d ORDER BY r.vid DESC";
-  $result = db_query($sql, $nid);
+  $result = db_query('SELECT n.nid, r.vid, r.uid FROM {node} n INNER JOIN {node_revision} r ON n.nid = r.nid WHERE r.vid > n.vid AND n.nid = :nid ORDER BY r.vid DESC', array(':nid' => $nid));
   $revisions = array();
-  while ($revision = db_fetch_object($result)) {
+  foreach ($result as $revision) {
     $revisions[$revision->vid] = $revision;
   }
 
@@ -378,7 +475,13 @@
   $vid = $form_state['values']['revision'];
   $type = $form_state['values']['type'];
 
-  db_query("UPDATE {node} SET vid = %d, title = '%s' WHERE nid = %d", $vid, $title, $nid);
+  db_update('node')
+  ->fields(array(
+    'vid' => $vid,
+    'title' => $title,
+  ))
+  ->condition('nid',$nid,'=')
+  ->execute();
   // Clear the cache so an anonymous poster can see the changes
   cache_clear_all();
   drupal_set_message('The selected revision has been published.');
Index: revision_moderation.install
===================================================================
RCS file: /cvs/drupal/contributions/modules/revision_moderation/revision_moderation.install,v
retrieving revision 1.6
diff -u -r1.6 revision_moderation.install
--- revision_moderation.install	19 Dec 2008 22:11:44 -0000	1.6
+++ revision_moderation.install	12 Dec 2010 17:28:44 -0000
@@ -1,23 +1,31 @@
 <?php
 // $Id: revision_moderation.install,v 1.6 2008/12/19 22:11:44 add1sun Exp $
+
+/**
+ * @file
+ * Install, update and uninstall functions for the revision_moderation module.
+ */
 
 /**
  * Implementation of hook_schema().
  */ 
 function revision_moderation_schema() {
   $schema['revision_moderation'] = array(
+    'description' => 'holds the last approved revision of the node',
     'fields' => array(
       'nid' => array(
         'type' => 'int',
         'unsigned' => TRUE,
         'not null' => TRUE,
+        'description' => 'Node ID',
         'default' => 0
       ),
       'revision_moderation' => array(
         'type' => 'int',
         'not null' => TRUE,
         'default' => 0,
-        'size' => 'tiny'
+        'size' => 'tiny',
+        'description' => 'last approved revision for the {nid}'
       ),
     ),
     'primary key' => array('nid'),
@@ -27,18 +35,8 @@
 }
 
 /**
- * Implementation of hook_install().
- */
-function revision_moderation_install() {
-  // Create table
-  drupal_install_schema('revision_moderation');
-}
-  
-/**
  * Implementation of hook_uninstall().
  */
 function revision_moderation_uninstall() {
-  // Drop the table
-  drupal_uninstall_schema('revision_moderation');
   variable_del('revision_moderation_exempt');
 }
Index: revision_moderation.info
===================================================================
RCS file: /cvs/drupal/contributions/modules/revision_moderation/revision_moderation.info,v
retrieving revision 1.3
diff -u -r1.3 revision_moderation.info
--- revision_moderation.info	18 Dec 2007 03:14:40 -0000	1.3
+++ revision_moderation.info	12 Dec 2010 17:28:44 -0000
@@ -1,4 +1,6 @@
 ; $Id: revision_moderation.info,v 1.3 2007/12/18 03:14:40 webchick Exp $
 name = Revision Moderation
 description = Allows moderation of new node revisions while existing approved revisions stay visible.
-core = "6.x"
+core = 7.x
+files[] = revision_moderation.module
+files[] = revision_moderation_actions.inc
