|
By scyrma:
MDL-13424 and MDL-14544: New function tag_cleanup removes unused un-official tags, or unlinked tags. Not yet called automatically.
|
| 681 |
} |
} |
| 682 |
|
|
| 683 |
/** |
/** |
| 684 |
|
* Clean up the tag tables, making sure all tagged object still exists. |
| 685 |
|
* |
| 686 |
|
* This should normally not be necessary, but in case related tags are not deleted |
| 687 |
|
* when the tagged record is removed, this should be done once in a while, perhaps on |
| 688 |
|
* an occasional cron run. On a site with lots of tags, this could become an expensive |
| 689 |
|
* function to call: don't run at peak time. |
| 690 |
|
*/ |
| 691 |
|
function tag_cleanup() { |
| 692 |
|
global $CFG; |
| 693 |
|
|
| 694 |
|
$tags = get_records('tag'); |
| 695 |
|
$instances = get_records('tag_instance'); |
| 696 |
|
|
| 697 |
|
foreach($instances as $instance) { |
| 698 |
|
$delete = false; |
| 699 |
|
|
| 700 |
|
if(!array_key_exists($instance->tagid, $tags)) { |
| 701 |
|
// if the tag has been removed, instance should be deleted. |
| 702 |
|
$delete = true; |
| 703 |
|
} else { |
| 704 |
|
switch ($instance->itemtype) { |
| 705 |
|
case 'user': // users are marked as deleted, but not actually deleted |
| 706 |
|
if (record_exists('user', 'id', $instance->itemid, 'deleted', 1)) { |
| 707 |
|
$delete = true; |
| 708 |
|
} |
| 709 |
|
break; |
| 710 |
|
default: // anything else, if the instance is not there, delete. |
| 711 |
|
if (!record_exists($instance->itemtype, 'id', $instance->itemid)) { |
| 712 |
|
$delete = true; |
| 713 |
|
} |
| 714 |
|
break; |
| 715 |
|
} |
| 716 |
|
} |
| 717 |
|
if ($delete) { |
| 718 |
|
tag_delete_instance($instance->itemtype, $instance->itemid, $instance->tagid); |
| 719 |
|
//debugging('deleting tag_instance #'. $instance->id .', linked to tag id #'. $instance->tagid, DEBUG_DEVELOPER); |
| 720 |
|
} |
| 721 |
|
} |
| 722 |
|
|
| 723 |
|
// TODO: this will only clean tags of type 'default'. This is good as |
| 724 |
|
// it won't delete 'official' tags, but the day we get more than two |
| 725 |
|
// types, we need to fix this. |
| 726 |
|
$tags = get_records('tag', 'tagtype', 'default'); // do it again - might be different now. |
| 727 |
|
|
| 728 |
|
foreach($tags as $tag) { |
| 729 |
|
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")) { |
| 730 |
|
tag_delete($tag->id); |
| 731 |
|
//debugging('deleting tag #'. $tag->id, DEBUG_DEVELOPER); |
| 732 |
|
} |
| 733 |
|
} |
| 734 |
|
} |
| 735 |
|
|
| 736 |
|
/** |
| 737 |
* Calculates and stores the correlated tags of all tags. |
* Calculates and stores the correlated tags of all tags. |
| 738 |
* The correlations are stored in the 'tag_correlation' table. |
* The correlations are stored in the 'tag_correlation' table. |
| 739 |
* |
* |