|
By scyrma:
MDL-13424 Fixes suggested by Eloy, in tag_cleanup.
|
| 691 |
function tag_cleanup() { |
function tag_cleanup() { |
| 692 |
global $CFG; |
global $CFG; |
| 693 |
|
|
| 694 |
$tags = get_records('tag'); |
$instances = get_recordset('tag_instance'); |
|
$instances = get_records('tag_instance'); |
|
| 695 |
|
|
| 696 |
foreach($instances as $instance) { |
// cleanup tag instances |
| 697 |
|
while ($instance = rs_fetch_next_record($instances)) { |
| 698 |
$delete = false; |
$delete = false; |
| 699 |
|
|
| 700 |
if(!array_key_exists($instance->tagid, $tags)) { |
if (!record_exists('tag', 'id', $instance->tagid)) { |
| 701 |
// if the tag has been removed, instance should be deleted. |
// if the tag has been removed, instance should be deleted. |
| 702 |
$delete = true; |
$delete = true; |
| 703 |
} else { |
} else { |
| 719 |
//debugging('deleting tag_instance #'. $instance->id .', linked to tag id #'. $instance->tagid, DEBUG_DEVELOPER); |
//debugging('deleting tag_instance #'. $instance->id .', linked to tag id #'. $instance->tagid, DEBUG_DEVELOPER); |
| 720 |
} |
} |
| 721 |
} |
} |
| 722 |
|
rs_close($instances); |
| 723 |
|
|
| 724 |
// TODO: this will only clean tags of type 'default'. This is good as |
// TODO: this will only clean tags of type 'default'. This is good as |
| 725 |
// it won't delete 'official' tags, but the day we get more than two |
// it won't delete 'official' tags, but the day we get more than two |
| 726 |
// types, we need to fix this. |
// types, we need to fix this. |
| 727 |
$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 (". |
| 728 |
|
"SELECT 'x' FROM {$CFG->prefix}tag_instance ti WHERE ti.tagid = tg.id)"); |
| 729 |
|
|
| 730 |
foreach($tags as $tag) { |
// cleanup tags |
| 731 |
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")) { |
while ($unused_tag = rs_fetch_next_record($unused_tags)) { |
| 732 |
tag_delete($tag->id); |
tag_delete($unused_tag->id); |
| 733 |
//debugging('deleting tag #'. $tag->id, DEBUG_DEVELOPER); |
//debugging('deleting unused tag #'. $unused_tag->id, DEBUG_DEVELOPER); |
|
} |
|
| 734 |
} |
} |
| 735 |
|
rs_close($unused_tags); |
| 736 |
} |
} |
| 737 |
|
|
| 738 |
/** |
/** |
| 805 |
*/ |
*/ |
| 806 |
function tag_cron() { |
function tag_cron() { |
| 807 |
tag_compute_correlations(); |
tag_compute_correlations(); |
| 808 |
|
//tag_cleanup(); |
| 809 |
} |
} |
| 810 |
|
|
| 811 |
/** |
/** |