MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / forum / db / mysql.php
blobaae07c20d92be8353b0e3b4ceec76a7c0895c677
1 <?php // $Id$
3 // THIS FILE IS DEPRECATED! PLEASE DO NOT MAKE CHANGES TO IT!
4 //
5 // IT IS USED ONLY FOR UPGRADES FROM BEFORE MOODLE 1.7, ALL
6 // LATER CHANGES SHOULD USE upgrade.php IN THIS DIRECTORY.
8 function forum_upgrade($oldversion) {
9 // This function does anything necessary to upgrade
10 // older versions to match current functionality
12 global $CFG, $db;
14 if ($oldversion < 2002073008) {
15 execute_sql("DELETE FROM modules WHERE name = 'discuss' ");
16 execute_sql("ALTER TABLE `discuss` RENAME `forum_discussions` ");
17 execute_sql("ALTER TABLE `discuss_posts` RENAME `forum_posts` ");
18 execute_sql("ALTER TABLE `discuss_ratings` RENAME `forum_ratings` ");
19 execute_sql("ALTER TABLE `forum` CHANGE `intro` `intro` TEXT NOT NULL ");
20 execute_sql("ALTER TABLE `forum` ADD `forcesubscribe` TINYINT(1) UNSIGNED DEFAULT '0' NOT NULL AFTER `assessed`");
21 execute_sql("ALTER TABLE `forum` CHANGE `type` `type` ENUM( 'single', 'news', 'social', 'general',
22 'eachuser', 'teacher' ) DEFAULT 'general' NOT NULL ");
23 execute_sql("ALTER TABLE `forum_posts` CHANGE `discuss` `discussion` INT( 10 ) UNSIGNED DEFAULT '0' NOT NULL ");
24 execute_sql("INSERT INTO log_display (module, action, mtable, field) VALUES ('forum', 'add', 'forum', 'name') ");
25 execute_sql("INSERT INTO log_display (module, action, mtable, field) VALUES ('forum', 'add discussion', 'forum_discussions', 'name') ");
26 execute_sql("INSERT INTO log_display (module, action, mtable, field) VALUES ('forum', 'add post', 'forum_posts', 'subject') ");
27 execute_sql("INSERT INTO log_display (module, action, mtable, field) VALUES ('forum', 'update post', 'forum_posts', 'subject') ");
28 execute_sql("INSERT INTO log_display (module, action, mtable, field) VALUES ('forum', 'view discussion', 'forum_discussions', 'name') ");
29 execute_sql("DELETE FROM log_display WHERE module = 'discuss' ");
30 execute_sql("UPDATE log SET action = 'view discussion' WHERE module = 'discuss' AND action = 'view' ");
31 execute_sql("UPDATE log SET action = 'add discussion' WHERE module = 'discuss' AND action = 'add' ");
32 execute_sql("UPDATE log SET module = 'forum' WHERE module = 'discuss' ");
33 notify("Renamed all the old discuss tables (now part of forum) and created new forum_types");
36 if ($oldversion < 2002080100) {
37 execute_sql("INSERT INTO log_display (module, action, mtable, field) VALUES ('forum', 'view subscribers', 'forum', 'name') ");
38 execute_sql("INSERT INTO log_display (module, action, mtable, field) VALUES ('forum', 'update', 'forum', 'name') ");
41 if ($oldversion < 2002082900) {
42 execute_sql(" ALTER TABLE `forum_posts` ADD `attachment` VARCHAR(100) NOT NULL AFTER `message` ");
45 if ($oldversion < 2002091000) {
46 if (! execute_sql(" ALTER TABLE `forum_posts` ADD `attachment` VARCHAR(100) NOT NULL AFTER `message` ")) {
47 echo "<p>Don't worry about this error - your server already had this upgrade applied";
51 if ($oldversion < 2002100300) {
52 execute_sql(" ALTER TABLE `forum` CHANGE `open` `open` TINYINT(2) UNSIGNED DEFAULT '2' NOT NULL ");
53 execute_sql(" UPDATE `forum` SET `open` = 2 WHERE `open` = 1 ");
54 execute_sql(" UPDATE `forum` SET `open` = 1 WHERE `open` = 0 ");
56 if ($oldversion < 2002101001) {
57 execute_sql(" ALTER TABLE `forum_posts` ADD `format` TINYINT(2) UNSIGNED DEFAULT '0' NOT NULL AFTER `message` ");
60 if ($oldversion < 2002122300) {
61 execute_sql("ALTER TABLE `forum_posts` CHANGE `user` `userid` INT(10) UNSIGNED DEFAULT '0' NOT NULL ");
62 execute_sql("ALTER TABLE `forum_ratings` CHANGE `user` `userid` INT(10) UNSIGNED DEFAULT '0' NOT NULL ");
63 execute_sql("ALTER TABLE `forum_subscriptions` CHANGE `user` `userid` INT(10) UNSIGNED DEFAULT '0' NOT NULL ");
66 if ($oldversion < 2003042402) {
67 execute_sql("INSERT INTO {$CFG->prefix}log_display (module, action, mtable, field) VALUES ('forum', 'move discussion', 'forum_discussions', 'name')");
70 if ($oldversion < 2003081403) {
71 table_column("forum", "assessed", "assessed", "integer", "10", "unsigned", "0");
74 if ($oldversion < 2003082500) {
75 table_column("forum", "", "assesstimestart", "integer", "10", "unsigned", "0", "", "assessed");
76 table_column("forum", "", "assesstimefinish", "integer", "10", "unsigned", "0", "", "assesstimestart");
79 if ($oldversion < 2003082502) {
80 table_column("forum", "scale", "scale", "integer", "10", "", "0");
81 execute_sql("UPDATE {$CFG->prefix}forum SET scale = (- scale)");
84 if ($oldversion < 2003100600) {
85 table_column("forum", "", "maxbytes", "integer", "10", "unsigned", "0", "", "scale");
88 if ($oldversion < 2004010100) {
89 table_column("forum", "", "assesspublic", "integer", "4", "unsigned", "0", "", "assessed");
92 if ($oldversion < 2004011404) {
93 table_column("forum_discussions", "", "userid", "integer", "10", "unsigned", "0", "", "firstpost");
95 if ($discussions = get_records_sql("SELECT d.id, p.userid
96 FROM {$CFG->prefix}forum_discussions as d,
97 {$CFG->prefix}forum_posts as p
98 WHERE d.firstpost = p.id")) {
99 foreach ($discussions as $discussion) {
100 update_record("forum_discussions", $discussion);
105 if ($oldversion < 2004012200) {
106 table_column("forum_discussions", "", "groupid", "integer", "10", "unsigned", "0", "", "userid");
109 if ($oldversion < 2004013000) {
110 table_column("forum_posts", "mailed", "mailed", "tinyint", "2");
113 if ($oldversion < 2004020600) {
114 table_column("forum_discussions", "", "usermodified", "integer", "10", "unsigned", "0", "", "timemodified");
117 if ($oldversion < 2004050300) {
118 table_column("forum","","rsstype","tinyint","2", "unsigned", "0", "", "forcesubscribe");
119 table_column("forum","","rssarticles","tinyint","2", "unsigned", "0", "", "rsstype");
120 set_config("forum_enablerssfeeds",0);
123 if ($oldversion < 2004060100) {
124 modify_database('', "CREATE TABLE `prefix_forum_queue` (
125 `id` int(11) unsigned NOT NULL auto_increment,
126 `userid` int(11) unsigned default 0 NOT NULL,
127 `discussionid` int(11) unsigned default 0 NOT NULL,
128 `postid` int(11) unsigned default 0 NOT NULL,
129 PRIMARY KEY (`id`),
130 KEY `user` (userid),
131 KEY `post` (postid)
132 ) TYPE=MyISAM COMMENT='For keeping track of posts that will be mailed in digest form';");
135 if ($oldversion < 2004070700) { // This may be redoing it from STABLE but that's OK
136 table_column("forum_discussions", "groupid", "groupid", "integer", "10", "", "0", "");
139 if ($oldversion < 2004111700) {
140 execute_sql(" ALTER TABLE `{$CFG->prefix}forum_posts` DROP INDEX {$CFG->prefix}forum_posts_parent_idx;",false);
141 execute_sql(" ALTER TABLE `{$CFG->prefix}forum_posts` DROP INDEX {$CFG->prefix}forum_posts_discussion_idx;",false);
142 execute_sql(" ALTER TABLE `{$CFG->prefix}forum_posts` DROP INDEX {$CFG->prefix}forum_posts_userid_idx;",false);
143 execute_sql(" ALTER TABLE `{$CFG->prefix}forum_discussions` DROP INDEX {$CFG->prefix}forum_discussions_forum_idx;",false);
144 execute_sql(" ALTER TABLE `{$CFG->prefix}forum_discussions` DROP INDEX {$CFG->prefix}forum_discussions_userid_idx;",false);
146 execute_sql(" ALTER TABLE `{$CFG->prefix}forum_posts` ADD INDEX {$CFG->prefix}forum_posts_parent_idx (parent) ");
147 execute_sql(" ALTER TABLE `{$CFG->prefix}forum_posts` ADD INDEX {$CFG->prefix}forum_posts_discussion_idx (discussion) ");
148 execute_sql(" ALTER TABLE `{$CFG->prefix}forum_posts` ADD INDEX {$CFG->prefix}forum_posts_userid_idx (userid) ");
149 execute_sql(" ALTER TABLE `{$CFG->prefix}forum_discussions` ADD INDEX {$CFG->prefix}forum_discussions_forum_idx (forum) ");
150 execute_sql(" ALTER TABLE `{$CFG->prefix}forum_discussions` ADD INDEX {$CFG->prefix}forum_discussions_userid_idx (userid) ");
153 if ($oldversion < 2004111700) {
154 execute_sql("ALTER TABLE {$CFG->prefix}forum DROP INDEX course;",false);
155 execute_sql("ALTER TABLE {$CFG->prefix}forum_ratings DROP INDEX userid;",false);
156 execute_sql("ALTER TABLE {$CFG->prefix}forum_ratings DROP INDEX post;",false);
157 execute_sql("ALTER TABLE {$CFG->prefix}forum_subscriptions DROP INDEX userid;",false);
158 execute_sql("ALTER TABLE {$CFG->prefix}forum_subscriptions DROP INDEX forum;",false);
160 modify_database('','ALTER TABLE prefix_forum ADD INDEX course (course);');
161 modify_database('','ALTER TABLE prefix_forum_ratings ADD INDEX userid (userid);');
162 modify_database('','ALTER TABLE prefix_forum_ratings ADD INDEX post (post);');
163 modify_database('','ALTER TABLE prefix_forum_subscriptions ADD INDEX userid (userid);');
164 modify_database('','ALTER TABLE prefix_forum_subscriptions ADD INDEX forum (forum);');
167 if ($oldversion < 2005011500) {
168 modify_database('','CREATE TABLE prefix_forum_read (
169 `id` int(10) unsigned NOT NULL auto_increment,
170 `userid` int(10) NOT NULL default \'0\',
171 `forumid` int(10) NOT NULL default \'0\',
172 `discussionid` int(10) NOT NULL default \'0\',
173 `postid` int(10) NOT NULL default \'0\',
174 `firstread` int(10) NOT NULL default \'0\',
175 `lastread` int(10) NOT NULL default \'0\',
176 PRIMARY KEY (`id`),
177 KEY `prefix_forum_user_forum_idx` (`userid`,`forumid`),
178 KEY `prefix_forum_user_discussion_idx` (`userid`,`discussionid`),
179 KEY `prefix_forum_user_post_idx` (`userid`,`postid`)
180 ) COMMENT=\'Tracks each users read posts\';');
182 set_config('upgrade', 'forumread'); // The upgrade of this table will be done later by admin/upgradeforumread.php
185 if ($oldversion < 2005032900) {
186 modify_database('','ALTER TABLE prefix_forum_posts ADD INDEX prefix_form_posts_created_idx (created);');
187 modify_database('','ALTER TABLE prefix_forum_posts ADD INDEX prefix_form_posts_mailed_idx (mailed);');
190 if ($oldversion < 2005041100) { // replace wiki-like with markdown
191 include_once( "$CFG->dirroot/lib/wiki_to_markdown.php" );
192 $wtm = new WikiToMarkdown();
193 $sql = "select course from {$CFG->prefix}forum_discussions, {$CFG->prefix}forum_posts ";
194 $sql .= "where {$CFG->prefix}forum_posts.discussion = {$CFG->prefix}forum_discussions.id ";
195 $sql .= "and {$CFG->prefix}forum_posts.id = ";
196 $wtm->update( 'forum_posts','message','format',$sql );
199 if ($oldversion < 2005042300) { // Add tracking prefs table
200 modify_database('','CREATE TABLE prefix_forum_track_prefs (
201 `id` int(10) unsigned NOT NULL auto_increment,
202 `userid` int(10) NOT NULL default \'0\',
203 `forumid` int(10) NOT NULL default \'0\',
204 PRIMARY KEY (`id`),
205 KEY `user_forum_idx` (`userid`,`forumid`)
206 ) COMMENT=\'Tracks each users untracked forums.\';');
209 if ($oldversion < 2005042500) {
210 table_column('forum','','trackingtype','tinyint','2', 'unsigned', '1', '', 'forcesubscribe');
213 if ($oldversion < 2005111100) {
214 table_column('forum_discussions','','timestart','integer');
215 table_column('forum_discussions','','timeend','integer');
218 if ($oldversion < 2006011600) {
219 execute_sql("alter table ".$CFG->prefix."forum change column type type enum('single','news','general','social','eachuser','teacher','qanda') not null default 'general'");
222 if ($oldversion < 2006011601) {
223 table_column('forum','','warnafter');
224 table_column('forum','','blockafter');
225 table_column('forum','','blockperiod');
228 if ($oldversion < 2006011700) {
229 table_column('forum_posts','','mailnow','integer');
232 if ($oldversion < 2006011702) {
233 execute_sql("INSERT INTO {$CFG->prefix}log_display (module, action, mtable, field) VALUES ('forum', 'user report', 'user', 'CONCAT(firstname,\' \',lastname)')");
237 if ($oldversion < 2006081800) {
238 // Upgrades for new roles and capabilities support.
239 require_once($CFG->dirroot.'/mod/forum/lib.php');
241 $forummod = get_record('modules', 'name', 'forum');
243 if ($forums = get_records('forum')) {
245 if (!$teacherroles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW)) {
246 notify('Default teacher role was not found. Roles and permissions '.
247 'for all your forums will have to be manually set after '.
248 'this upgrade.');
250 if (!$studentroles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW)) {
251 notify('Default student role was not found. Roles and permissions '.
252 'for all your forums will have to be manually set after '.
253 'this upgrade.');
255 if (!$guestroles = get_roles_with_capability('moodle/legacy:guest', CAP_ALLOW)) {
256 notify('Default guest role was not found. Roles and permissions '.
257 'for teacher forums will have to be manually set after '.
258 'this upgrade.');
260 foreach ($forums as $forum) {
261 if (!forum_convert_to_roles($forum, $forummod->id, $teacherroles,
262 $studentroles, $guestroles)) {
263 notify('Forum with id '.$forum->id.' was not upgraded');
266 // We need to rebuild all the course caches to refresh the state of
267 // the forum modules.
268 include_once( "$CFG->dirroot/course/lib.php" );
269 rebuild_course_cache();
271 } // End if.
273 // Drop column forum.open.
274 modify_database('', 'ALTER TABLE prefix_forum DROP COLUMN open;');
276 // Drop column forum.assesspublic.
277 modify_database('', 'ALTER TABLE prefix_forum DROP COLUMN assesspublic;');
280 if ($oldversion < 2006082700) {
281 $sql = "UPDATE {$CFG->prefix}forum_posts SET message = REPLACE(message, '".TRUSTTEXT."', '');";
282 $likecond = sql_ilike()." '%".TRUSTTEXT."%'";
283 while (true) {
284 if (!count_records_select('forum_posts', "message $likecond")) {
285 break;
287 execute_sql($sql);
291 ////// DO NOT ADD NEW THINGS HERE!! USE upgrade.php and the lib/ddllib.php functions.
293 return true;