Index: mollom.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/mollom/mollom.install,v
retrieving revision 1.2.2.33
diff -u -p -r1.2.2.33 mollom.install
--- mollom.install	12 Sep 2010 23:44:14 -0000	1.2.2.33
+++ mollom.install	14 Sep 2010 18:52:33 -0000
@@ -4,6 +4,9 @@
 /**
  * @file
  * Install and uninstall functions as well as schema definition for the Mollom module.
+ *
+ * @todo Rename 'did' column after mollom_set_data() and mollom_get_data() have
+ *   been removed.
  */
 
 /**
@@ -94,7 +97,14 @@ function mollom_schema() {
         'default' => '',
       ),
       'session' => array(
-        'description' => "Content author's session ID.",
+        'description' => "Session hash returned by Mollom.",
+        'type' => 'varchar',
+        'length' => 255,
+        'not null' => TRUE,
+        'default' => '',
+      ),
+      'form_id' => array(
+        'description' => 'The $form_id of the form being protected.',
         'type' => 'varchar',
         'length' => 255,
         'not null' => TRUE,
@@ -132,6 +142,14 @@ function mollom_schema() {
     ),
     'indexes' => array('session' => array('session')),
     'primary key' => array('entity', 'did'),
+    'foreign keys' => array(
+      'mollom_form_id' => array(
+        'table' => 'mollom_form',
+        'columns' => array(
+          'form_id' => 'form_id',
+        ),
+      ),
+    ),
   );
 
   $schema['mollom_form'] = array(
@@ -209,7 +227,13 @@ function mollom_uninstall() {
  */
 function mollom_update_1() {
   $ret = array();
-  db_add_field($ret, 'mollom', 'languages', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
+  db_add_field($ret, 'mollom', 'languages', array(
+    'description' => 'Text analysis language check result.',
+    'type' => 'varchar',
+    'length' => 255,
+    'not null' => TRUE,
+    'default' => '',
+  ));
   return $ret;
 }
 
@@ -238,7 +262,7 @@ function mollom_update_3() {
     $mode = variable_get($name, NULL);
     // $mode was stored as 1; convert to MOLLOM_MODE_ANALYSIS.
     if (isset($mode)) {
-      variable_set($name, MOLLOM_MODE_ANALYSIS);
+      variable_set($name, 2);
     }
   }
 }
@@ -260,22 +284,26 @@ function mollom_update_6105() {
   $schema = array(
     'fields' => array(
       'form_id' => array(
+        'description' => 'The protected form ID.',
         'type' => 'varchar',
         'length' => 255,
         'not null' => TRUE,
         'default' => '',
       ),
       'mode' => array(
+        'description' => 'Protection mode for the form.',
         'type' => 'int',
         'size' => 'tiny',
         'not null' => TRUE,
         'default' => 0,
       ),
       'enabled_fields' => array(
+        'description' => 'Form elements to analyze.',
         'type' => 'text',
         'serialize' => TRUE,
       ),
       'module' => array(
+        'description' => 'Module name owning the form.',
         'type' => 'varchar',
         'length' => 255,
         'not null' => TRUE,
@@ -310,14 +338,12 @@ function mollom_update_6105() {
 
 /**
  * Add the {mollom}.form_id column.
- *
- * @todo Rename 'did' column after mollom_set_data() and mollom_get_data() have
- *   been removed.
  */
 function mollom_update_6106() {
   $ret = array();
   // Add the 'entity' column.
   db_add_field($ret, 'mollom', 'entity', array(
+    'description' => 'Entity type of the content.',
     'type' => 'varchar',
     'length' => 32,
     'not null' => TRUE,
@@ -340,6 +366,7 @@ function mollom_update_6106() {
   // @todo We do not change the type to 'int' here to still support named
   //   identifiers. Reconsider this.
   db_change_field($ret, 'mollom', 'did', 'did', array(
+    'description' => 'Unique entity ID of the content.',
     'type' => 'varchar',
     'length' => 32,
     'not null' => TRUE,
@@ -377,6 +404,7 @@ function mollom_update_6109() {
   $ret = array();
   if (db_column_exists('mollom_form', 'fields')) {
     db_change_field($ret, 'mollom_form', 'fields', 'enabled_fields', array(
+      'description' => 'Form elements to analyze.',
       'type' => 'text',
       'serialize' => TRUE,
     ));
@@ -420,6 +448,7 @@ function mollom_update_6112() {
   $ret = array();
   if (!db_column_exists('mollom', 'changed')) {
     db_add_field($ret, 'mollom', 'changed', array(
+      'description' => 'Unix timestamp when the data was changed.',
       'type' => 'int',
       'not null' => TRUE,
       'default' => 0,
@@ -437,6 +466,7 @@ function mollom_update_6113() {
   // Add {mollom_form}.checks.
   if (!db_column_exists('mollom_form', 'checks')) {
     db_add_field($ret, 'mollom_form', 'checks', array(
+      'description' => 'Text analyis checks to perform.',
       'type' => 'text',
       'not null' => FALSE,
       'serialize' => TRUE,
@@ -465,6 +495,7 @@ function mollom_update_6114() {
 
   // Change {mollom}.quality from varchar into float.
   db_change_field($ret, 'mollom', 'quality', 'quality', array(
+    'description' => 'Text analysis quality check result.',
     'type' => 'float',
     'size' => 'tiny',
     'not null' => FALSE,
@@ -473,6 +504,7 @@ function mollom_update_6114() {
   // Add {mollom}.spam.
   if (!db_column_exists('mollom', 'spam')) {
     db_add_field($ret, 'mollom', 'spam', array(
+      'description' => 'Text analysis spam check result.',
       'type' => 'int',
       'size' => 'tiny',
       'not null' => FALSE,
@@ -492,3 +524,28 @@ function mollom_update_6114() {
 
   return $ret;
 }
+
+/**
+ * Add {mollom}.form_id column to look up the originating form_id for an entity.
+ */
+function mollom_update_6115() {
+  $ret = array();
+  if (!db_column_exists('mollom', 'form_id')) {
+    db_add_field($ret, 'mollom', 'form_id', array(
+      'description' => 'The $form_id of the form being protected.',
+      'type' => 'varchar',
+      'length' => 255,
+      'not null' => TRUE,
+      'default' => '',
+    ));
+    // Update simple cases.
+    $ret[] = update_sql("UPDATE {mollom} SET form_id = 'comment_form' WHERE entity = 'comment'");
+    $ret[] = update_sql("UPDATE {mollom} SET form_id = 'user_register' WHERE entity = 'user'");
+    $ret[] = update_sql("UPDATE {mollom} SET form_id = 'contact_mail_page' WHERE entity = 'session'");
+    // @todo Account for sub-form IDs.
+    $ret[] = update_sql("UPDATE {mollom} SET form_id = 'node_form' WHERE entity = 'node'");
+    // Also update data on behalf of Webform, as that is a very common case.
+    $ret[] = update_sql("UPDATE {mollom} SET form_id = CONCAT('webform_client_form_', did) WHERE entity = 'webform'");
+  }
+  return $ret;
+}
Index: mollom.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/mollom/mollom.module,v
retrieving revision 1.2.2.162
diff -u -p -r1.2.2.162 mollom.module
--- mollom.module	13 Sep 2010 18:05:25 -0000	1.2.2.162
+++ mollom.module	14 Sep 2010 19:17:48 -0000
@@ -327,6 +327,8 @@ function _mollom_access($permission = FA
  *   The entity type of the data to report.
  * @param $id
  *   The entity id of the data to report.
+ *
+ * @todo Revamp this based on new {mollom}.form_id info.
  */
 function mollom_report_access($entity, $id) {
   // The special entity 'session' means that $id is a Mollom session_id, which
@@ -388,6 +390,20 @@ function mollom_cron() {
 }
 
 /**
+ * Load a Mollom data record from the database by Mollom session ID.
+ *
+ * @param $session_id
+ *   A session hash, as returned from Mollom services.
+ */
+function mollom_session_load($session_id) {
+  $data = db_fetch_object(db_query("SELECT * FROM {mollom} WHERE session = '%s'", array($session_id)));
+  if (!$data) {
+    _mollom_watchdog(array('Session @session_id not found.' => array('@session_id' => $session_id)), WATCHDOG_ERROR);
+  }
+  return $data;
+}
+
+/**
  * Load a Mollom data record from the database.
  *
  * @param $entity
@@ -421,30 +437,31 @@ function mollom_data_load($entity, $id) 
  *   The entity type of the data to save.
  * @param $id
  *   The entity id the data belongs to.
+ * @param $form_id
+ *   The form ID the session data belongs to.
+ * @param $response
+ *   An associative array of session data returned by Mollom.
  *
  * @todo Remove usage of global $mollom variable.
  */
-function mollom_data_save($entity, $id) {
+function mollom_data_save($entity, $id, $form_id, $response) {
+  $data = array(
+    'entity' => $entity,
+    'did' => $id,
+    'form_id' => $form_id,
+    'session' => $response['session_id'],
+    'changed' => time(),
+  );
+  $data += $response;
   // Nothing to do, if we do not have a valid Mollom response.
   if (empty($GLOBALS['mollom']['response']['session_id'])) {
     return FALSE;
   }
-  $data = $GLOBALS['mollom']['response'];
-  $data['session'] = $data['session_id'];
-  $data['entity'] = $entity;
-  $data['did'] = $id;
-  $data['changed'] = time();
 
   // Convert languages into a string.
   if (!empty($data['languages'])) {
     $data['languages'] = implode(' ', $data['languages']);
   }
-  // Merge in default values that may not exist in the response.
-  $data += array(
-    'languages' => '',
-    'quality' => '',
-    'reputation' => '',
-  );
 
   $update = db_result(db_query_range("SELECT 'did' FROM {mollom} WHERE entity = '%s' AND did = '%s'", $entity, $id, 0, 1));
   drupal_write_record('mollom', $data, $update ? $update : array());
@@ -1630,7 +1647,7 @@ function mollom_form_submit($form, &$for
     $data = mollom_form_get_values($form_state['values'], array(), $form_state['mollom']['mapping']);
     // We only consider non-empty and non-zero values as valid entity ids.
     if (!empty($data['post_id'])) {
-      mollom_data_save($form_state['mollom']['entity'], $data['post_id']);
+      mollom_data_save($form_state['mollom']['entity'], $data['post_id'], $form_state['mollom']['form_id'], $form_state['mollom']['response']);
     }
   }
   // Flush Mollom session information from database cache.
@@ -2063,7 +2080,7 @@ function mollom_get_captcha($type, array
  * form submissions.
  */
 function mollom_mail_alter(&$message) {
-  // Attached the Mollom report link to any mails with IDs specified from the
+  // Attaches the Mollom report link to any mails with IDs specified from the
   // submitted form's hook_mollom_form_info(). This should ensure that the
   // report link is added to mails sent by actual users and not any mails sent
   // by Drupal since they should never be reported as spam.
@@ -2077,9 +2094,10 @@ function mollom_mail_alter(&$message) {
  */
 function mollom_mail_add_report_link(&$message) {
   if (!empty($GLOBALS['mollom']['response']['session_id'])) {
-    mollom_data_save('session', $GLOBALS['mollom']['response']['session_id']);
+    $mollom = $GLOBALS['mollom'];
+    mollom_data_save('session', $mollom['response']['session_id'], $mollom['form_id'], $mollom['response']);
     $report_link = t('Report as inappropriate: @link', array(
-      '@link' => url('mollom/report/session/' . $GLOBALS['mollom']['response']['session_id'], array('absolute' => TRUE)),
+      '@link' => url('mollom/report/session/' . $mollom['response']['session_id'], array('absolute' => TRUE)),
     ));
     // Until D7, hook_mail_alter() accepts both arrays and strings.
     if (is_array($message['body'])) {
@@ -2157,10 +2175,7 @@ function node_mollom_form_info($form_id)
  * Implements hook_nodeapi().
  */
 function mollom_nodeapi($node, $op) {
-  if ($op == 'insert') {
-    mollom_data_save('node', $node->nid);
-  }
-  elseif ($op == 'delete') {
+  if ($op == 'delete') {
     mollom_data_delete('node', $node->nid);
   }
 }
@@ -2292,10 +2307,7 @@ function mollom_comment_form_validate($f
  * Implements hook_comment().
  */
 function mollom_comment($comment, $op) {
-  if ($op == 'insert') {
-    mollom_data_save('comment', $comment['cid']);
-  }
-  elseif ($op == 'delete') {
+  if ($op == 'delete') {
     mollom_data_delete('comment', $comment->cid);
   }
 }
