? votingapi_pluggable.patch
Index: votingapi.api.php
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/votingapi/Attic/votingapi.api.php,v
retrieving revision 1.1.2.1.2.1
diff -u -p -r1.1.2.1.2.1 votingapi.api.php
--- votingapi.api.php	1 Jul 2009 07:26:12 -0000	1.1.2.1.2.1
+++ votingapi.api.php	19 May 2010 23:21:40 -0000
@@ -160,4 +160,74 @@ function hook_votingapi_views_formatters
   if ($field->field == 'tag') {
     return array('mymodule_funky_tags' => t('MyModule tag formatter'));
   }
-}
\ No newline at end of file
+}
+
+/**
+ * Save a vote in the database.
+ *
+ * @param $vote
+ *   See votingapi_add_votes() for the structure of this array, with the
+ *   defaults loaded from votingapi_prep_vote().
+ */
+function hook_votingapi_storage_add_vote(&$vote) {
+  // Note that this is not the real MongoDB code, the necessary casting is
+  // missing.
+  mongodb_collection('votingapi_vote')->insert($vote);
+}
+
+/**
+ * Delete votes from the database.
+ *
+ * @param $votes
+ *   An array of votes to delete. Minimally, each vote must have the 'vote_id'
+ *   key set.
+ * @param $vids
+ *   A list of the 'vote_id' values from $voes.
+ */
+function votingapi_votingapi_storage_delete_votes($votes, $vids) {
+  mongodb_collection('votingapi_vote')->delete(array('vote_id' => array('$in' => array_map('intval', $vids))));
+}
+
+/**
+ * Select invidual votes from the database
+ *
+/**
+ * Select individual votes from the database.
+ *
+ * @param $criteria
+ *   A keyed array used to build the select query. Keys can contain
+ *   a single value or an array of values to be matched.
+ *   $criteria['vote_id']       (If this is set, all other keys are skipped)
+ *   $criteria['entity_id']
+ *   $criteria['entity_type']
+ *   $criteria['value_type']
+ *   $criteria['tag']
+ *   $criteria['uid']
+ *   $criteria['vote_source']
+ *   $criteria['timestamp']   If this is set, records with timestamps
+ *      GREATER THAN the set value will be selected. Defaults to
+ *      REQUEST_TIME - variable_get('votingapi_anonymous_window', 3600); if
+ *      the anonymous window is above zero.
+ * @param $limit
+ *   An integer specifying the maximum number of votes to return. 0 means
+ *   unlimited and is the default.
+ * @return
+ *   An array of votes matching the criteria.
+ */
+function votingapi_votingapi_storage_select_votes($criteria, $limit) {
+  $query = mongodb_collection('votingapi_vote');
+  foreach ($criteria as $key => $value) {
+    // Note that this is not the real MongoDB code, the necessary casting is
+    // missing.
+    $find[$key] = is_array($value) ? array('$in' => $value) : $value;
+  }
+  $cursor = $query->find($find);
+  if (!empty($limit)) {
+    $cursor->limit($limit);
+  }
+  $votes = array();
+  foreach ($cursor as $vote) {
+    $votes[] = $vote;
+  }
+  return $votes;
+}
Index: votingapi.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/votingapi/votingapi.module,v
retrieving revision 1.46.2.23.2.6
diff -u -p -r1.46.2.23.2.6 votingapi.module
--- votingapi.module	27 Jan 2010 02:39:31 -0000	1.46.2.23.2.6
+++ votingapi.module	19 May 2010 23:21:41 -0000
@@ -183,6 +183,34 @@ function votingapi_current_user_identifi
 }
 
 /**
+ * Implementation of hook_votingapi_storage_add_vote().
+ */
+function votingapi_votingapi_storage_add_vote(&$vote) {
+  drupal_write_record('votingapi_vote', $vote);
+}
+
+/**
+ * Implementation of hook_votingapi_storage_delete_votes().
+ */
+function votingapi_votingapi_storage_delete_votes($votes, $vids) {
+  db_delete('votingapi_vote')->condition('vote_id', $vids, 'IN')->execute();
+}
+
+/**
+ * Implementation of hook_votingapi_storage_select_votes().
+ */
+function votingapi_votingapi_storage_select_votes($criteria, $limit) {
+  $query = db_select('votingapi_vote')->fields('votingapi_vote');
+  foreach ($criteria as $key => $value) {
+    $query->condition($key, $value, is_array($value) ? 'IN' : '=');
+  }
+  if (!empty($limit)) {
+    $query->range(0, $limit);
+  }
+  return $query->execute()->fetchAll(PDO::FETCH_ASSOC);
+}
+
+/**
  * Save a collection of votes to the database.
  *
  * This function does most of the heavy lifting for VotingAPI the main
@@ -213,9 +241,10 @@ function votingapi_add_votes(&$votes) {
   if (!empty($votes['entity_id'])) {
     $votes = array($votes);
   }
+  $function = variable_get('votingapi_storage_module', 'votingapi') . '_votingapi_storage_add_vote';
   foreach ($votes as $key => $vote) {
     _votingapi_prep_vote($vote);
-    drupal_write_record('votingapi_vote', $vote);
+    $function($vote);
     $votes[$key] = $vote;
   }
   module_invoke_all('votingapi_insert', $votes);
@@ -265,7 +294,8 @@ function votingapi_delete_votes($votes =
     foreach ($votes as $vote) {
       $vids[] = $vote['vote_id'];
     }
-    db_delete('votingapi_vote')->condition('vote_id', $vids, 'IN')->execute();
+    $function = variable_get('votingapi_storage_module', 'votingapi') . '_votingapi_storage_delete_votes';
+    $function($votes, $vids);
   }
 }
 
@@ -311,14 +341,8 @@ function votingapi_select_votes($criteri
   if (!empty($criteria['vote_source']) && $anon_window > 0) {
     $criteria['timestamp'] = REQUEST_TIME - $anon_window;
   }
-  $query = db_select('votingapi_vote')->fields('votingapi_vote');
-  foreach ($criteria as $key => $value) {
-    $query->condition($key, $value, is_array($value) ? 'IN' : '=');
-  }
-  if (!empty($limit)) {
-    $query->range(0, $limit);
-  }
-  return $query->execute()->fetchAll(PDO::FETCH_ASSOC);
+  $function = variable_get('votingapi_storage_module', 'votingapi') . '_votingapi_storage_select_votes';
+  return $function($criteria, $limit);
 }
 
 /**
@@ -379,7 +403,7 @@ function votingapi_recalculate_results($
       ->condition('entity_type', $entity_type)
       ->condition('entity_id', $entity_id)
       ->execute();
-      
+
     // Bulk query to pull the majority of the results we care about.
     $cache = _votingapi_get_standard_results($entity_type, $entity_id);
 
@@ -550,4 +574,4 @@ function _votingapi_prep_vote(&$vote) {
       'prepped' => TRUE
     );
   }
-}
\ No newline at end of file
+}
