From: skodak Date: Mon, 24 Sep 2007 19:18:48 +0000 (+0000) Subject: MDL-10781 reimplemented tags upgrade code: uses recordsets; no calls to tag/lib.php... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=535c0080c3029d30316899b82f4e07845dad0132;p=moodle.git MDL-10781 reimplemented tags upgrade code: uses recordsets; no calls to tag/lib.php functions - those would break if we changed db structure; no ordering in blog tags - we did not respect the tag order before, this way it is much faster --- diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 841f4fbdc6..293859b97e 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -1708,45 +1708,64 @@ function xmldb_main_upgrade($oldversion=0) { $result = $result && add_field($table, $field); } - /// migrate all tags table to tag + /// migrate all tags table to tag - this code MUST use SQL only, + /// because if the db structure changes the library functions will fail in future if ($result && $oldversion < 2007082701) { - require_once($CFG->dirroot.'/tag/lib.php'); $tagrefs = array(); // $tagrefs[$oldtagid] = $newtagid - if ($tags = get_records('tags')) { - foreach ($tags as $oldtag) { - // if this tag does not exist in tag table yet - if (!$newtag = get_record('tag', 'name', tag_normalize($oldtag->text))) { - $itag = new object(); - $itag->name = tag_normalize($oldtag->text); - $itag->rawname = tag_normalize($oldtag->text, false); - - if ($oldtag->type == 'official') { - $itag->tagtype = $oldtag->type; - } else { - $itag->tagtype = 'default'; - } - $itag->userid = $oldtag->userid; - $itag->timemodified = time(); + if ($rs = get_recordset('tags')) { + $db->debug = false; + if ($rs->RecordCount() > 0) { + while ($oldtag = rs_fetch_next_record($rs)) { + $raw_normalized = clean_param($oldtag->text, PARAM_TAG); + $normalized = moodle_strtolower($raw_normalized); + // if this tag does not exist in tag table yet + if (!$newtag = get_record('tag', 'name', $normalized, '', '', '', '', 'id')) { + $itag = new object(); + $itag->name = $normalized; + $itag->rawname = $raw_normalized; + $itag->userid = $oldtag->userid; + $itag->timemodified = time(); + if ($oldtag->type == 'official') { + $itag->tagtype = 'official'; + } else { + $itag->tagtype = 'default'; + } - if ($idx = insert_record('tag', $itag)) { - $tagrefs[$oldtag->id] = $idx; + if ($idx = insert_record('tag', $itag)) { + $tagrefs[$oldtag->id] = $idx; + } + // if this tag is already used by tag table + } else { + $tagrefs[$oldtag->id] = $newtag->id; } - // if this tag is already used by tag table - } else { - $tagrefs[$oldtag->id] = $newtag->id; } } + $db->debug = true; + rs_close($rs); } // fetch all the tag instances and migrate them as well - if ($blogtags = get_records('blog_tag_instance')) { - foreach ($blogtags as $blogtag) { - if (!empty($tagrefs[$blogtag->tagid])) { - tag_an_item('blog', $blogtag->entryid, $tagrefs[$blogtag->tagid]); + if ($rs = get_recordset('blog_tag_instance')) { + $db->debug = false; + if ($rs->RecordCount() > 0) { + while ($blogtag = rs_fetch_next_record($rs)) { + if (array_key_exists($blogtag->tagid, $tagrefs)) { + $tag_instance = new object(); + $tag_instance->tagid = $tagrefs[$blogtag->tagid]; + $tag_instance->itemtype = 'blog'; + $tag_instance->itemid = $blogtag->entryid; + $tag_instance->ordering = 1; // does not matter much, because originally there was no ordering in blogs + $tag_instance->timemodified = time(); + insert_record('tag_instance', $tag_instance); + } } } + $db->debug = true; + rs_close($rs); } + unset($tagrefs); // release memory + $table = new XMLDBTable('tags'); drop_table($table); $table = new XMLDBTable('blog_tag_instance');