From 084426c5d9469f118381698cc961dd2d4ef20b3a Mon Sep 17 00:00:00 2001 From: scyrma Date: Thu, 1 May 2008 06:15:51 +0000 Subject: [PATCH] MDL-13424 Fixes suggested by Eloy, in tag_cleanup. (merge from 1.9) --- tag/lib.php | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/tag/lib.php b/tag/lib.php index 933b01cb84..498345bc88 100644 --- a/tag/lib.php +++ b/tag/lib.php @@ -691,13 +691,13 @@ function tag_autocomplete($text) { function tag_cleanup() { global $CFG; - $tags = get_records('tag'); - $instances = get_records('tag_instance'); + $instances = get_recordset('tag_instance'); - foreach($instances as $instance) { + // cleanup tag instances + while ($instance = rs_fetch_next_record($instances)) { $delete = false; - if(!array_key_exists($instance->tagid, $tags)) { + if (!record_exists('tag', 'id', $instance->tagid)) { // if the tag has been removed, instance should be deleted. $delete = true; } else { @@ -719,18 +719,20 @@ function tag_cleanup() { //debugging('deleting tag_instance #'. $instance->id .', linked to tag id #'. $instance->tagid, DEBUG_DEVELOPER); } } + 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. - $tags = get_records('tag', 'tagtype', 'default'); // do it again - might be different now. + $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)"); - foreach($tags as $tag) { - if (!record_exists_sql("SELECT * FROM {$CFG->prefix}tag tg, {$CFG->prefix}tag_instance ti WHERE tg.id = {$tag->id} AND ti.tagid = tg.id")) { - tag_delete($tag->id); - //debugging('deleting tag #'. $tag->id, DEBUG_DEVELOPER); - } + // 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); } + rs_close($unused_tags); } /** @@ -803,6 +805,7 @@ function tag_compute_correlations($min_correlation=2) { */ function tag_cron() { tag_compute_correlations(); + //tag_cleanup(); } /** -- 2.39.5