### Eclipse Workspace Patch 1.0
#P moodle19
Index: tag/lib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/tag/lib.php,v
retrieving revision 1.43.2.36
diff -u -r1.43.2.36 lib.php
--- tag/lib.php	2 May 2008 01:12:49 -0000	1.43.2.36
+++ tag/lib.php	6 May 2008 10:28:27 -0000
@@ -691,48 +691,61 @@
 function tag_cleanup() {
     global $CFG;
 
-    $instances = get_recordset('tag_instance');
+    $sql = "SELECT ti.id
+              FROM {$CFG->prefix}tag_instance ti
+                   LEFT JOIN {$CFG->prefix}tag t ON t.id = ti.tagid
+             WHERE t.id IS NULL";
+    if ($rs = get_recordset_sql($sql)) {
+        while ($instance = rs_fetch_next_record($rs)) {
+            delete_records('tag_instance', 'id', $instance->id); 
+        }
+        rs_close($rs);
+    }
 
-    // cleanup tag instances
-    while ($instance = rs_fetch_next_record($instances)) {
-        $delete = false;
-        
-        if (!record_exists('tag', 'id', $instance->tagid)) {
-            // if the tag has been removed, instance should be deleted.
-            $delete = true;
-        } else {
-            switch ($instance->itemtype) {
-                case 'user': // users are marked as deleted, but not actually deleted
-                    if (record_exists('user', 'id', $instance->itemid, 'deleted', 1)) {
-                        $delete = true;
-                    }
-                    break;
-                default: // anything else, if the instance is not there, delete.
-                    if (!record_exists($instance->itemtype, 'id', $instance->itemid)) {
-                        $delete = true;
-                    }
-                    break;
-            }
+    $sql = "SELECT ti.id, ti.itemtype,ti.itemid, ti.tagid
+              FROM {$CFG->prefix}tag_instance ti
+                   JOIN {$CFG->prefix}user u ON u.id = ti.itemid
+             WHERE ti.itemtype = 'user' AND u.deleted = 1";
+    if ($rs = get_recordset_sql($sql)) {
+        while ($instance = rs_fetch_next_record($rs)) {
+            tag_delete_instance($instance->itemtype, $instance->itemid, $instance->tagid); 
         }
-        if ($delete) {
-            tag_delete_instance($instance->itemtype, $instance->itemid, $instance->tagid);
-            //debugging('deleting tag_instance #'. $instance->id .', linked to tag id #'. $instance->tagid, DEBUG_DEVELOPER);
+        rs_close($rs);
+    }
+
+    $sql = "SELECT DISTINCT ti.itemtype
+              FROM {$CFG->prefix}tag_instance ti
+             WHERE ti.itemtype <> 'user'";
+    if ($tables = get_fieldset_sql($sql)) {
+        foreach ($tables as $table) {
+            $sql = "SELECT ti.id, ti.itemtype,ti.itemid, ti.tagid
+                      FROM {$CFG->prefix}tag_instance ti
+                           LEFT JOIN {$CFG->prefix}$table t ON t.id = ti.itemid
+                     WHERE ti.itemtype = '$table' AND t.id IS NULL";
+            if ($rs = get_recordset_sql($sql)) {
+                while ($instance = rs_fetch_next_record($rs)) {
+                    tag_delete_instance($instance->itemtype, $instance->itemid, $instance->tagid); 
+                }
+                rs_close($rs);
+            }
         }
     }
-    rs_close($instances);
 
     // TODO: this will only clean tags of type 'default'.  This is good as
     // it won't delete 'official' tags, but the day we get more than two
     // types, we need to fix this.
-    $unused_tags = get_recordset_sql("SELECT tg.id FROM {$CFG->prefix}tag tg WHERE tg.tagtype = 'default' AND NOT EXISTS (".
-        "SELECT 'x' FROM {$CFG->prefix}tag_instance ti WHERE ti.tagid = tg.id)");
-
-    // cleanup tags
-    while ($unused_tag = rs_fetch_next_record($unused_tags)) {
-        tag_delete($unused_tag->id);
-        //debugging('deleting unused tag #'. $unused_tag->id,  DEBUG_DEVELOPER);
+    $sql = "SELECT tg.id
+              FROM {$CFG->prefix}tag tg
+                   LEFT JOIN {$CFG->prefix}tag_instance ti ON ti.tagid = tg.id
+             WHERE tg.tagtype = 'default' AND ti.id IS NULL";
+    if ($rs = get_recordset_sql($sql)) {
+        // cleanup tags
+        while ($unused_tag = rs_fetch_next_record($rs)) {
+            tag_delete($unused_tag->id);
+            //debugging('deleting unused tag #'. $unused_tag->id,  DEBUG_DEVELOPER);
+        }
+        rs_close($rs);
     }
-    rs_close($unused_tags);
 }
 
 /**
