From 7b3ee7ff2bbb55f25f405f8a431075a8fa050240 Mon Sep 17 00:00:00 2001
From: danepowell <danepowell@339326.no-reply.drupal.org>
Date: Fri, 6 Apr 2012 21:14:37 +0100
Subject: [PATCH 1/2] Issue #1185690 by Dane Powell: Apply patch for
 hosting_task_log() entries are not deleted when task
 is.

---
 modules/hosting/task/hosting_task.install |   40 +++++++++++++++++++++++++++++
 modules/hosting/task/hosting_task.module  |    2 +
 2 files changed, 42 insertions(+), 0 deletions(-)

diff --git a/modules/hosting/task/hosting_task.install b/modules/hosting/task/hosting_task.install
index d339ce7..3901546 100644
--- a/modules/hosting/task/hosting_task.install
+++ b/modules/hosting/task/hosting_task.install
@@ -81,6 +81,12 @@ function hosting_task_schema() {
         'not null' => TRUE,
         'default' => 0,
       ),
+      'nid' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,
+      ),
       'type' => array(
         'type' => 'varchar',
         'length' => 16,
@@ -240,3 +246,37 @@ function hosting_task_update_6005() {
                   array('type' => 'text', 'size' => 'big', 'not null' => TRUE));
   return $ret;
 }
+
+/**
+ * Add nid field to hosting_task_log and remove orphaned log entries.
+ */
+function hosting_task_update_6006() {
+  $ret = array();
+
+  // Add nid field
+  $field = array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,
+      );
+  db_add_field($ret, 'hosting_task_log', 'nid', $field);
+
+  // Populate nid field
+  $query = "SELECT nid, vid FROM {hosting_task}";
+  $result = db_query($query);
+  while ($task = db_fetch_object($result)) {
+    $query = "UPDATE {hosting_task_log} SET nid = %d WHERE vid = %d";
+    db_query($query, $task->nid, $task->vid);
+  }
+
+  // Remove orphaned logs
+  $query = "SELECT DISTINCT h.vid FROM {hosting_task_log} h LEFT OUTER JOIN {node_revisions} n ON h.vid = n.vid WHERE n.vid IS NULL";
+  $result = db_query($query);
+  while ($revision = db_fetch_object($result)) {
+    $query = "DELETE FROM {hosting_task_log} WHERE vid = %d";
+    db_query($query, $revision->vid);
+  }
+
+  return $ret;
+}
diff --git a/modules/hosting/task/hosting_task.module b/modules/hosting/task/hosting_task.module
index 144aac0..3de9794 100644
--- a/modules/hosting/task/hosting_task.module
+++ b/modules/hosting/task/hosting_task.module
@@ -589,6 +589,7 @@ function hosting_task_update($node) {
 function hosting_nodeapi_task_delete_revision(&$node) {
   db_query('DELETE FROM {hosting_task} WHERE vid = %d', $node->vid);
   db_query('DELETE FROM {hosting_task_arguments} WHERE vid = %d', $node->vid);
+  db_query('DELETE FROM {hosting_task_log} WHERE vid = %d', $node->vid);
 }
 
 /**
@@ -597,6 +598,7 @@ function hosting_nodeapi_task_delete_revision(&$node) {
 function hosting_task_delete($node) {
   db_query('DELETE FROM {hosting_task} WHERE nid = %d', $node->nid);
   db_query('DELETE FROM {hosting_task_arguments} WHERE nid = %d', $node->nid);
+  db_query('DELETE FROM {hosting_task_log} WHERE nid = %d', $node->nid);
 }
 
 /**
-- 
1.7.5.4


From de29227011939dbe7e62d47190dd739b94cf7672 Mon Sep 17 00:00:00 2001
From: Steven Jones <steven.jones@computerminds.co.uk>
Date: Fri, 6 Apr 2012 21:59:58 +0100
Subject: [PATCH 2/2] Issue #1185690 by Steven Jones: Fixed hosting_task_log()
 entries are not deleted when task is.

---
 modules/hosting/task/hosting_task.install |   35 ++++++++++++++++------------
 modules/hosting/task/hosting_task.module  |   12 ++++++---
 2 files changed, 28 insertions(+), 19 deletions(-)

diff --git a/modules/hosting/task/hosting_task.install b/modules/hosting/task/hosting_task.install
index 3901546..5532f92 100644
--- a/modules/hosting/task/hosting_task.install
+++ b/modules/hosting/task/hosting_task.install
@@ -111,6 +111,8 @@ function hosting_task_schema() {
     ),
     'indexes' => array(
       'type' => array('type'),
+      'vid_lid' => array('vid', 'lid'),
+      'nid' => array('nid'),
     ),
     'primary key' => array('lid'),
   );
@@ -253,16 +255,23 @@ function hosting_task_update_6005() {
 function hosting_task_update_6006() {
   $ret = array();
 
-  // Add nid field
+  // Add nid field.
   $field = array(
-        'type' => 'int',
-        'unsigned' => TRUE,
-        'not null' => TRUE,
-        'default' => 0,
-      );
-  db_add_field($ret, 'hosting_task_log', 'nid', $field);
+    'type' => 'int',
+    'unsigned' => TRUE,
+    'not null' => TRUE,
+    'default' => 0,
+  );
+  // Add some useful indexes.
+  $indexes = array(
+    'indexes' => array(
+      'vid_lid' => array('vid', 'lid'),
+      'nid' => array('nid'),
+    ),
+  );
+  db_add_field($ret, 'hosting_task_log', 'nid', $field, $indexes);
 
-  // Populate nid field
+  // Populate nid field.
   $query = "SELECT nid, vid FROM {hosting_task}";
   $result = db_query($query);
   while ($task = db_fetch_object($result)) {
@@ -270,13 +279,9 @@ function hosting_task_update_6006() {
     db_query($query, $task->nid, $task->vid);
   }
 
-  // Remove orphaned logs
-  $query = "SELECT DISTINCT h.vid FROM {hosting_task_log} h LEFT OUTER JOIN {node_revisions} n ON h.vid = n.vid WHERE n.vid IS NULL";
-  $result = db_query($query);
-  while ($revision = db_fetch_object($result)) {
-    $query = "DELETE FROM {hosting_task_log} WHERE vid = %d";
-    db_query($query, $revision->vid);
-  }
+  // Remove orphaned logs, which will have nid == 0 now.
+  $query = "DELETE FROM {hosting_task_log} WHERE nid = %d";
+  db_query($query, 0);
 
   return $ret;
 }
diff --git a/modules/hosting/task/hosting_task.module b/modules/hosting/task/hosting_task.module
index 3de9794..cdefa07 100644
--- a/modules/hosting/task/hosting_task.module
+++ b/modules/hosting/task/hosting_task.module
@@ -294,9 +294,13 @@ function hosting_task_hosting_queues() {
 /**
  * Insert an entry in the task log
  */
-function hosting_task_log($vid, $type, $message, $error = '', $timestamp = null ) {
- $timestamp = ($timestamp) ? $timestamp : mktime();
- db_query("INSERT INTO {hosting_task_log} (vid, type, message, error, timestamp) VALUES (%d, '%s', '%s', '%s', %d)", $vid, $type, $message, $error, $timestamp);
+function hosting_task_log($vid, $type, $message, $error = '', $timestamp = NULL ) {
+  $timestamp = ($timestamp) ? $timestamp : time();
+
+  // We need to insert the nid in addition to the vid, so look it up.
+  $nid = (int)db_result(db_query('SELECT nid FROM {hosting_task} WHERE vid = %d', $vid));
+
+  db_query("INSERT INTO {hosting_task_log} (vid, nid, type, message, error, timestamp) VALUES (%d, %d, '%s', '%s', '%s', %d)", $vid, $nid, $type, $message, $error, $timestamp);
 }
 
 /**
@@ -735,7 +739,7 @@ function hosting_task_view($node, $teaser = FALSE, $page = FALSE) {
  * Display table containing the logged information for this task
  */
 function _hosting_task_log_table($vid) {
-  $result = db_query("SELECT * FROM {hosting_task_log} WHERE vid=%d", $vid);
+  $result = db_query("SELECT * FROM {hosting_task_log} WHERE vid = %d ORDER BY lid", $vid);
   if ($result) {
     $header = array('data' => 'Log message');
     while ($entry = db_fetch_object($result)) {
-- 
1.7.5.4

