From a370e4e4fa00f5818cadadda70a830a2b77afb8b Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Wed, 20 Apr 2011 12:04:02 +0800 Subject: [PATCH 1/3] MDL-24355 Added code to delete any tag correlations that wern't just identified --- tag/lib.php | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/tag/lib.php b/tag/lib.php index 47c8c003c52..4d9916680aa 100644 --- a/tag/lib.php +++ b/tag/lib.php @@ -831,8 +831,7 @@ function tag_compute_correlations($mincorrelation = 2) { // correlationid : This is the id of the row in the tag_correlation table that // relates to the tagid field and will be NULL if there are no // existing correlations - $sql = 'SELECT pairs.tagid, pairs.correlation, pairs.ocurrences, - co.id AS correlationid, co.correlatedtags + $sql = 'SELECT pairs.tagid, pairs.correlation, pairs.ocurrences, co.id AS correlationid FROM ( SELECT ta.tagid, tb.tagid AS correlation, COUNT(*) AS ocurrences FROM {tag_instance} ta @@ -850,12 +849,19 @@ function tag_compute_correlations($mincorrelation = 2) { $tagcorrelation->tagid = null; $tagcorrelation->correlatedtags = array(); + // We store each correction id in this array so we can remove any correlations + // that no longer exist. + $correlations = array(); + // Iterate each row of the result set and build them into tag correlations. foreach ($rs as $row) { if ($row->tagid != $tagcorrelation->tagid) { // The tag id has changed so its now time to process the tag // correlation information we have. - tag_process_computed_correlation($tagcorrelation); + $tagcorrelationid = tag_process_computed_correlation($tagcorrelation); + if ($tagcorrelationid) { + $correlations[] = $tagcorrelationid; + } // Now we reset the tag correlation object so we can reuse it and set it // up for the current record. $tagcorrelation = new stdClass; @@ -866,10 +872,18 @@ function tag_compute_correlations($mincorrelation = 2) { $tagcorrelation->correlatedtags[] = $row->correlation; } // Update the current correlation after the last record. - tag_process_computed_correlation($tagcorrelation); + $tagcorrelationid = tag_process_computed_correlation($tagcorrelation); + if ($tagcorrelationid) { + $correlations[] = $tagcorrelationid; + } + // Close the recordset $rs->close(); + + // Remove any correlations that weren't just identified + list($sql, $params) = $DB->get_in_or_equal($correlations, SQL_PARAMS_NAMED, 'param0000', false); + $DB->delete_records_select('tag_correlation', 'id '.$sql, $params); } /** @@ -880,7 +894,7 @@ function tag_compute_correlations($mincorrelation = 2) { * property that is an array. * * @param stdClass $tagcorrelation - * @return bool True if the function completed, false if something was wrong. + * @return int The id of the tag correlation that was just processed. */ function tag_process_computed_correlation(stdClass $tagcorrelation) { global $DB; @@ -899,9 +913,9 @@ function tag_process_computed_correlation(stdClass $tagcorrelation) { $DB->update_record('tag_correlation', $tagcorrelation); } else { // This is a new correlation to insert - $DB->insert_record('tag_correlation', $tagcorrelation); + $tagcorrelation->id = $DB->insert_record('tag_correlation', $tagcorrelation); } - return true; + return $tagcorrelation->id; } /** From 15f0d2ed378a649d8ea558569aaf6fc6692e8599 Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Wed, 20 Apr 2011 12:20:55 +0800 Subject: [PATCH 2/3] MDL-24355 Fixed up deletion of tag correlations when deleting tags --- tag/lib.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tag/lib.php b/tag/lib.php index 4d9916680aa..73f2f72f9c7 100644 --- a/tag/lib.php +++ b/tag/lib.php @@ -536,14 +536,14 @@ function tag_delete($tagids) { $success = true; $context = get_context_instance(CONTEXT_SYSTEM); - foreach( $tagids as $tagid ) { + foreach ($tagids as $tagid) { if (is_null($tagid)) { // can happen if tag doesn't exists continue; } // only delete the main entry if there were no problems deleting all the // instances - that (and the fact we won't often delete lots of tags) // is the reason for not using $DB->delete_records_select() - if ($DB->delete_records('tag_instance', array('tagid'=>$tagid)) ) { + if ($DB->delete_records('tag_instance', array('tagid'=>$tagid)) && $DB->delete_records('tag_correlation', array('tagid' => $tagid))) { $success &= (bool) $DB->delete_records('tag', array('id'=>$tagid)); // Delete all files associated with this tag $fs = get_file_storage(); @@ -997,9 +997,11 @@ function tag_get_correlated($tag_id, $limitnum=null) { } // this is (and has to) return the same fields as the query in tag_get_tags - if ( !$result = $DB->get_records_sql("SELECT DISTINCT tg.id, tg.tagtype, tg.name, tg.rawname, tg.flag, ti.ordering - FROM {tag} tg INNER JOIN {tag_instance} ti ON tg.id = ti.tagid - WHERE tg.id IN ({$tag_correlation->correlatedtags})") ) { + $sql = "SELECT DISTINCT tg.id, tg.tagtype, tg.name, tg.rawname, tg.flag + FROM {tag} tg + WHERE tg.id IN ({$tag_correlation->correlatedtags})"; + $result = $DB->get_records_sql($sql); + if (!$result) { return array(); } From cfc9f55ca625cc971f3b750f3a154c0a953ac617 Mon Sep 17 00:00:00 2001 From: "Andrew Davis (andyjdavis)" Date: Tue, 7 Jun 2011 16:18:09 +0800 Subject: [PATCH 3/3] MDL-24355 tag: minor clean up of tags changes --- tag/lib.php | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/tag/lib.php b/tag/lib.php index 73f2f72f9c7..3a49176a3f7 100644 --- a/tag/lib.php +++ b/tag/lib.php @@ -250,6 +250,7 @@ function tag_get($field, $value, $returnfields='id, name, rawname') { /** * Get the array of db record of tags associated to a record (instances). Use * tag_get_tags_csv to get the same information in a comma-separated string. + * This should really be called tag_get_tag_instances() * * @param string $record_type the record type for which we want to get the tags * @param int $record_id the record id for which we want to get the tags @@ -277,13 +278,14 @@ function tag_get_tags($record_type, $record_id, $type=null, $userid=0) { } $sql = "SELECT tg.id, tg.tagtype, tg.name, tg.rawname, tg.flag, ti.ordering - FROM {tag_instance} ti JOIN {tag} tg ON tg.id = ti.tagid + FROM {tag_instance} ti + JOIN {tag} tg ON tg.id = ti.tagid WHERE ti.itemtype = :recordtype AND ti.itemid = :recordid $u $sql_type ORDER BY ti.ordering ASC"; $params['recordtype'] = $record_type; $params['recordid'] = $record_id; - // if the fields in this query are changed, you need to do the same changes in tag_get_correlated_tags + // if the fields in this query are changed, you need to do the same changes in tag_get_correlated return $DB->get_records_sql($sql, $params); // This version of the query, reversing the ON clause, "correctly" returns // a row with NULL values for instances that are still in the DB even though @@ -849,15 +851,16 @@ function tag_compute_correlations($mincorrelation = 2) { $tagcorrelation->tagid = null; $tagcorrelation->correlatedtags = array(); - // We store each correction id in this array so we can remove any correlations + // We store each correlation id in this array so we can remove any correlations // that no longer exist. $correlations = array(); // Iterate each row of the result set and build them into tag correlations. + // We add all of a tag's correlations to $tagcorrelation->correlatedtags[] + // then save the $tagcorrelation object foreach ($rs as $row) { if ($row->tagid != $tagcorrelation->tagid) { - // The tag id has changed so its now time to process the tag - // correlation information we have. + // The tag id has changed so we have all of the correlations for this tag $tagcorrelationid = tag_process_computed_correlation($tagcorrelation); if ($tagcorrelationid) { $correlations[] = $tagcorrelationid; @@ -869,6 +872,7 @@ function tag_compute_correlations($mincorrelation = 2) { $tagcorrelation->tagid = $row->tagid; $tagcorrelation->correlatedtags = array(); } + //Save the correlation on the tag correlation object $tagcorrelation->correlatedtags[] = $row->correlation; } // Update the current correlation after the last record. @@ -882,8 +886,13 @@ function tag_compute_correlations($mincorrelation = 2) { $rs->close(); // Remove any correlations that weren't just identified - list($sql, $params) = $DB->get_in_or_equal($correlations, SQL_PARAMS_NAMED, 'param0000', false); - $DB->delete_records_select('tag_correlation', 'id '.$sql, $params); + if (empty($correlations)) { + //there are no tag correlations + $DB->delete_records('tag_correlation'); + } else { + list($sql, $params) = $DB->get_in_or_equal($correlations, SQL_PARAMS_NAMED, 'param0000', false); + $DB->delete_records_select('tag_correlation', 'id '.$sql, $params); + } } /** @@ -904,9 +913,6 @@ function tag_process_computed_correlation(stdClass $tagcorrelation) { return false; } - // The row tagid doesn't match the current tag id which means we are onto - // the next tag. Before we switch over we need to either insert or update - // the correlation. $tagcorrelation->correlatedtags = join(',', $tagcorrelation->correlatedtags); if (!empty($tagcorrelation->id)) { // The tag correlation already exists so update it @@ -997,8 +1003,9 @@ function tag_get_correlated($tag_id, $limitnum=null) { } // this is (and has to) return the same fields as the query in tag_get_tags - $sql = "SELECT DISTINCT tg.id, tg.tagtype, tg.name, tg.rawname, tg.flag + $sql = "SELECT DISTINCT tg.id, tg.tagtype, tg.name, tg.rawname, tg.flag, ti.ordering FROM {tag} tg + INNER JOIN {tag_instance} ti ON tg.id = ti.tagid WHERE tg.id IN ({$tag_correlation->correlatedtags})"; $result = $DB->get_records_sql($sql); if (!$result) {