MDL-25501 reverting PULL-95
[moodle.git] / mod / wiki / db / upgrade.php
CommitLineData
14a960ac 1<?php
b8a342d7 2
00710f4c 3// This file is part of Moodle - http://moodle.org/
b8a342d7 4//
00710f4c
DC
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
b8a342d7 9//
00710f4c
DC
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
b8a342d7 14//
00710f4c
DC
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * This file keeps track of upgrades to the wiki module
20 *
21 * Sometimes, changes between versions involve
22 * alterations to database structures and other
23 * major things that may break installations.
24 *
25 * The upgrade function in this file will attempt
26 * to perform all the necessary actions to upgrade
2e0406a5 27 * your older installation to the current version.
00710f4c
DC
28 *
29 * @package mod-wiki-2.0
30 * @copyrigth 2009 Marc Alier, Jordi Piguillem marc.alier@upc.edu
31 * @copyrigth 2009 Universitat Politecnica de Catalunya http://www.upc.edu
32 *
33 * @author Jordi Piguillem
34 *
35 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
36 *
37 */
38
39/**
40 *
41 * TODO LIST:
42 *
43 * 1. Add needed fields to wiki table. DONE
44 * 2. Rename other wiki tables. DONE
45 * 3. Create new wiki tables. DONE BUT NOT FINISHED, WATING FOR NEW TABLES
46 * 4. Move/Adapt/Transform configurations info to new structure
47 * 5. Migrate wiki entries to subwikis. DONE
48 * 6. Fill pages table with latest versions of every page. DONE
49 * 7. Migrate page history to new table (transforming formats). DONE, BUT STILL WORKING
50 * 8. Fill links table
51 * 9. Drop useless information
52 *
53 * ADITIONAL THINGS AFTER CHAT WITH ELOY:
54 *
55 * 1. addField is deprecated. DONE
56 * 2. Fix SQL error at block 3. DONE
57 * 3. Merge set_field_select with previous update sentence. DONE
58 * 4. Don't insert id fields on database (it won't work on mssql, oracle, pg). DONE.
59 * 5. Use upgrade_set_timeout function.
60 * 6. Use grafic of progess
61 *
62 * OTHER THINGS:
63 *
64 * 1. Use recordset instead of record when migrating historic
65 * 2. Select only usefull data on block 06
66 *
67 */
775f811a 68function xmldb_wiki_upgrade($oldversion) {
00710f4c 69 global $CFG, $DB, $OUTPUT;
b8a342d7 70
775f811a 71 $dbman = $DB->get_manager();
b8a342d7 72
00710f4c 73 // Step 0: Add new fields to main wiki table
a4cdd6d2 74 if ($oldversion < 2010040100) {
00710f4c
DC
75 require_once(dirname(__FILE__) . '/upgradelib.php');
76 echo $OUTPUT->notification('Adding new fields to wiki table', 'notifysuccess');
77 wiki_add_wiki_fields();
219f652b 78
a4cdd6d2 79 upgrade_mod_savepoint(true, 2010040100, 'wiki');
00710f4c 80 }
8b6f8099 81
00710f4c 82 // Step 1: Rename old tables
a4cdd6d2 83 if ($oldversion < 2010040101) {
00710f4c 84 $tables = array('wiki_pages', 'wiki_locks', 'wiki_entries');
8b6f8099 85
00710f4c
DC
86 echo $OUTPUT->notification('Renaming old wiki module tables', 'notifysuccess');
87 foreach ($tables as $tablename) {
88 $table = new xmldb_table($tablename);
89 if ($dbman->table_exists($table)) {
90 if ($dbman->table_exists($table)) {
91 $dbman->rename_table($table, $tablename . '_old');
92 }
93 }
94 }
a4cdd6d2 95 upgrade_mod_savepoint(true, 2010040101, 'wiki');
00710f4c 96 }
8b6f8099 97
00710f4c 98 // Step 2: Creating new tables
a4cdd6d2 99 if ($oldversion < 2010040102) {
00710f4c
DC
100 require_once(dirname(__FILE__) . '/upgradelib.php');
101 echo $OUTPUT->notification('Installing new wiki module tables', 'notifysuccess');
102 wiki_upgrade_install_20_tables();
a4cdd6d2 103 upgrade_mod_savepoint(true, 2010040102, 'wiki');
8b6f8099 104 }
105
00710f4c 106 // Step 3: migrating wiki instances
a4cdd6d2 107 if ($oldversion < 2010040103) {
00710f4c 108 upgrade_set_timeout();
8b6f8099 109
00710f4c 110 // Setting up wiki configuration
621f3ef5
JP
111 $sql = "UPDATE {wiki}
112 SET intro = summary,
113 firstpagetitle = pagename,
114 defaultformat = ?";
d3dbc520
DM
115 $DB->execute($sql, array('html'));
116
621f3ef5
JP
117 $sql = "UPDATE {wiki}
118 SET wikimode = ?
119 WHERE wtype = ?";
d3dbc520
DM
120 $DB->execute($sql, array('collaborative', 'group'));
121
621f3ef5
JP
122 $sql = "UPDATE {wiki}
123 SET wikimode = ?
124 WHERE wtype != ?";
d3dbc520 125 $DB->execute($sql, array('individual', 'group'));
00710f4c
DC
126
127 // Removing edit & create capability to students in old teacher wikis
128 $studentroles = $DB->get_records('role', array('archetype' => 'student'));
129 $wikis = $DB->get_records('wiki');
130 foreach ($wikis as $wiki) {
131 echo $OUTPUT->notification('Migrating '.$wiki->wtype.' type wiki instance: '.$wiki->name, 'notifysuccess');
132 if ($wiki->wtype == 'teacher') {
133 $cm = get_coursemodule_from_instance('wiki', $wiki->id);
134 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
135 foreach ($studentroles as $studentrole) {
136 role_change_permission($studentrole->id, $context, 'mod/wiki:editpage', CAP_PROHIBIT);
137 role_change_permission($studentrole->id, $context, 'mod/wiki:createpage', CAP_PROHIBIT);
138 }
139 }
140 }
141
142 echo $OUTPUT->notification('Migrating old wikis to new wikis', 'notifysuccess');
a4cdd6d2 143 upgrade_mod_savepoint(true, 2010040103, 'wiki');
00710f4c
DC
144 }
145
146 // Step 4: migrating wiki entries to new subwikis
a4cdd6d2 147 if ($oldversion < 2010040104) {
00710f4c
DC
148 /**
149 * Migrating wiki entries to new subwikis
150 */
90c42e0f
PS
151 $sql = "INSERT into {wiki_subwikis} (wikiid, groupid, userid)
152 SELECT DISTINCT e.wikiid, e.groupid, e.userid
153 FROM {wiki_entries_old} e";
00710f4c
DC
154 echo $OUTPUT->notification('Migrating old entries to new subwikis', 'notifysuccess');
155
156 $DB->execute($sql, array());
157
a4cdd6d2 158 upgrade_mod_savepoint(true, 2010040104, 'wiki');
00710f4c
DC
159 }
160
161 // Step 5: Migrating pages
a4cdd6d2 162 if ($oldversion < 2010040105) {
90c42e0f
PS
163 /**
164 * Filling pages table with latest versions of every page.
165 *
166 * @TODO: Ensure that ALL versions of every page are always in database and
167 * they can be removed or cleaned.
168 * That fact could let us rewrite the subselect to execute a count(*) to avoid
169 * the order by and it would be much faster.
170 */
00710f4c 171
90c42e0f
PS
172 $sql = "INSERT into {wiki_pages} (subwikiid, title, cachedcontent, timecreated, timemodified, userid, pageviews)
173 SELECT s.id, p.pagename, ?, p.created, p.lastmodified, p.userid, p.hits
174 FROM {wiki_pages_old} p
175 LEFT OUTER JOIN {wiki_entries_old} e ON e.id = p.wiki
176 LEFT OUTER JOIN {wiki_subwikis} s
177 ON s.wikiid = e.wikiid AND s.groupid = e.groupid AND s.userid = e.userid
178 WHERE p.version = (
179 SELECT max(po.version)
180 FROM {wiki_pages_old} po
181 WHERE p.pagename = po.pagename and
182 p.wiki = po.wiki
183 )";
00710f4c 184 echo $OUTPUT->notification('Migrating old pages to new pages', 'notifysuccess');
8b6f8099 185
90c42e0f 186 $DB->execute($sql, array('**reparse needed**'));
00710f4c 187
a4cdd6d2 188 upgrade_mod_savepoint(true, 2010040105, 'wiki');
00710f4c 189 }
8b6f8099 190
00710f4c 191 // Step 6: Migrating versions
a4cdd6d2 192 if ($oldversion < 2010040106) {
00710f4c
DC
193 require_once(dirname(__FILE__) . '/upgradelib.php');
194 echo $OUTPUT->notification('Migrating old history to new history', 'notifysuccess');
195 wiki_upgrade_migrate_versions();
a4cdd6d2 196 upgrade_mod_savepoint(true, 2010040106, 'wiki');
8b6f8099 197 }
198
00710f4c 199 // Step 7: refresh cachedcontent and fill wiki links table
a4cdd6d2 200 if ($oldversion < 2010040107) {
00710f4c
DC
201 require_once($CFG->dirroot. '/mod/wiki/locallib.php');
202 upgrade_set_timeout();
e37cd84a 203
00710f4c
DC
204 $pages = $DB->get_recordset('wiki_pages');
205
77b8c5ca 206 foreach ($pages as $page) {
00710f4c 207 wiki_refresh_cachedcontent($page);
00710f4c
DC
208 }
209
210 $pages->close();
211
212 echo $OUTPUT->notification('Caching content', 'notifysuccess');
a4cdd6d2 213 upgrade_mod_savepoint(true, 2010040107, 'wiki');
00710f4c
DC
214 }
215 // Step 8, migrating files
a4cdd6d2 216 if ($oldversion < 2010040108) {
00710f4c 217 $fs = get_file_storage();
49550e49
DC
218 $sql = "SELECT files.*, po.meta AS filemeta FROM {wiki_pages_old} po JOIN (
219 SELECT DISTINCT po.id, po.pagename, w.id AS wikiid, po.userid,
220 eo.id AS entryid, eo.groupid, s.id AS subwiki,
221 w.course AS courseid, cm.id AS cmid
222 FROM {wiki_pages_old} po
223 LEFT OUTER JOIN {wiki_entries_old} eo
224 ON eo.id=po.wiki
225 LEFT OUTER JOIN {wiki} w
226 ON w.id = eo.wikiid
227 LEFT OUTER JOIN {wiki_subwikis} s
228 ON s.groupid = eo.groupid AND s.wikiid = eo.wikiid AND eo.userid = s.userid
229 JOIN {modules} m ON m.name = 'wiki'
134d38ff
DC
230 JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = w.id)
231 ) files ON files.id = po.id";
00710f4c
DC
232
233 $rs = $DB->get_recordset_sql($sql);
234 foreach ($rs as $r) {
235 if (strpos($r->pagename, 'internal://') !== false) {
236 // Found a file resource!
237 $pattern = 'internal://';
238 $matches = array();
239 $filename = str_replace($pattern, '', $r->pagename);
240 $orgifilename = $filename = clean_param($filename, PARAM_FILE);
241 $context = get_context_instance(CONTEXT_MODULE, $r->cmid);
242 $filemeta = unserialize($r->filemeta);
243 $filesection = $filemeta['section'];
244 // When attach a file to wiki page, user can customize the file name instead of original file name
245 // if user did, old wiki will create two pages, internal://original_pagename and internal://renamed_pagename
246 // internal://original_pagename record has renamed pagename in meta field
247 // but all file have this field
248 // old wiki will rename file names to filter space and special character
249 if (!empty($filemeta['Content-Location'])) {
250 $orgifilename = urldecode($filemeta['Content-Location']);
251 $orgifilename = str_replace(' ', '_', $orgifilename);
252 }
b420734a 253 $thefile = $CFG->dataroot . '/' . $r->courseid . '/moddata/wiki/' . $r->wikiid .'/' . $r->entryid . '/'. $filesection .'/'. $filename;
00710f4c 254
b420734a 255 if (is_file($thefile) && is_readable($thefile)) {
00710f4c 256 $filerecord = array('contextid' => $context->id,
64f93798
PS
257 'component' => 'mod_wiki',
258 'filearea' => 'attachments',
00710f4c
DC
259 'itemid' => $r->subwiki,
260 'filepath' => '/',
261 'filename' => $orgifilename,
262 'userid' => $r->userid);
64f93798 263 if (!$fs->file_exists($context->id, 'mod_wiki', 'attachments', $r->subwiki, '/', $orgifilename)) {
b420734a 264 //echo $OUTPUT->notification('Migrating file '.$orgifilename, 'notifysuccess');
00710f4c
DC
265 $storedfile = $fs->create_file_from_pathname($filerecord, $thefile);
266 }
267 // we have to create another file here to make sure interlinks work
89e2569b 268 if (!$fs->file_exists($context->id, 'mod_wiki', 'attachments', $r->subwiki, '/', $filename)) {
00710f4c 269 $filerecord['filename'] = $filename;
b420734a 270 //echo $OUTPUT->notification('Migrating file '.$filename, 'notifysuccess');
00710f4c
DC
271 $storedfile = $fs->create_file_from_pathname($filerecord, $thefile);
272 }
b420734a
DC
273 } else {
274 echo $OUTPUT->notification("Bad data found: $r->pagename <br/> Expected file path: $thefile Please fix the bad file path manually.");
00710f4c
DC
275 }
276 }
277 }
278 $rs->close();
a4cdd6d2 279 upgrade_mod_savepoint(true, 2010040108, 'wiki');
00710f4c
DC
280 }
281
282 // Step 9: clean wiki table
a4cdd6d2 283 if ($oldversion < 2010040109) {
00710f4c 284 $fields = array('summary', 'pagename', 'wtype', 'ewikiprinttitle', 'htmlmode', 'ewikiacceptbinary', 'disablecamelcase', 'setpageflags', 'strippages', 'removepages', 'revertchanges', 'initialcontent');
e37cd84a 285 $table = new xmldb_table('wiki');
00710f4c
DC
286 foreach ($fields as $fieldname) {
287 $field = new xmldb_field($fieldname);
288 if ($dbman->field_exists($table, $field)) {
289 $dbman->drop_field($table, $field);
290 }
e37cd84a 291
00710f4c
DC
292 }
293 echo $OUTPUT->notification('Cleaning wiki table', 'notifysuccess');
a4cdd6d2 294 upgrade_mod_savepoint(true, 2010040109, 'wiki');
00710f4c
DC
295 }
296
90c42e0f
PS
297 // TODO: Will hold the old tables so we will have chance to fix problems
298 // Will remove old tables once migrating 100% stable
299 // Step 10: delete old tables
300 if ($oldversion < 2010040120) {
301 //$tables = array('wiki_pages', 'wiki_locks', 'wiki_entries');
302
303 //foreach ($tables as $tablename) {
304 //$table = new xmldb_table($tablename . '_old');
305 //if ($dbman->table_exists($table)) {
306 //$dbman->drop_table($table);
307 //}
308 //}
309 //echo $OUTPUT->notification('Droping old tables', 'notifysuccess');
310 //upgrade_mod_savepoint(true, 2010040120, 'wiki');
311 }
312
ab422920
JP
313 if ($oldversion < 2010080201) {
314
315 $sql = "UPDATE {comments}
316 SET commentarea = 'wiki_page'
317 WHERE commentarea = 'wiki_comment_section'";
318 $DB->execute($sql);
319
320 $sql = "UPDATE {tag_instance}
321 SET itemtype = 'wiki_page'
322 WHERE itemtype = 'wiki'";
323 $DB->execute($sql);
324
325 echo $OUTPUT->notification('Updating comments and tags', 'notifysuccess');
dec303ad
EL
326
327 upgrade_mod_savepoint(true, 2010080201, 'wiki');
ab422920
JP
328 }
329
b52d9601
PS
330 if ($oldversion < 2010102500) {
331
332 // Define key subwikifk (foreign) to be added to wiki_pages
333 $table = new xmldb_table('wiki_pages');
334 $key = new xmldb_key('subwikifk', XMLDB_KEY_FOREIGN, array('subwikiid'), 'wiki_subwikis', array('id'));
335
336 // Launch add key subwikifk
337 $dbman->add_key($table, $key);
338
339 // Define key subwikifk (foreign) to be added to wiki_links
340 $table = new xmldb_table('wiki_links');
341 $key = new xmldb_key('subwikifk', XMLDB_KEY_FOREIGN, array('subwikiid'), 'wiki_subwikis', array('id'));
342
343 // Launch add key subwikifk
344 $dbman->add_key($table, $key);
345
346 // wiki savepoint reached
347 upgrade_mod_savepoint(true, 2010102500, 'wiki');
348 }
349
f0f77dfa 350 if ($oldversion < 2010102800) {
ab422920 351
f0f77dfa
DC
352 $sql = "UPDATE {tag_instance}
353 SET itemtype = 'wiki_pages'
354 WHERE itemtype = 'wiki_page'";
355 $DB->execute($sql);
356
357 echo $OUTPUT->notification('Updating tags itemtype', 'notifysuccess');
358
359 upgrade_mod_savepoint(true, 2010102800, 'wiki');
360 }
ab422920 361
d481e2a8 362 if ($oldversion < 2011011000) {
1be91921
SH
363 // Fix wiki in the post table after upgrade from 1.9
364 $table = new xmldb_table('wiki');
742d877e 365
1be91921
SH
366 // name should default to Wiki
367 $field = new xmldb_field('name', XMLDB_TYPE_CHAR, 255, null, XMLDB_NOTNULL, null, 'Wiki', 'course');
368 if ($dbman->field_exists($table, $field)) {
369 $dbman->change_field_default($table, $field);
370 }
742d877e 371
1be91921
SH
372 // timecreated field is missing after 1.9 upgrade
373 $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'introformat');
374 if (!$dbman->field_exists($table, $field)) {
375 $dbman->add_field($table, $field);
376 }
742d877e 377
1be91921
SH
378 // timemodified field is missing after 1.9 upgrade
379 $field = new xmldb_field('timemodified', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'timecreated');
380 if (!$dbman->field_exists($table, $field)) {
381 $dbman->add_field($table, $field);
382 }
742d877e 383
1be91921
SH
384 // scaleid is not there any more
385 $field = new xmldb_field('scaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null);
386 if ($dbman->field_exists($table, $field)) {
387 $dbman->drop_field($table, $field);
388 }
742d877e 389
d481e2a8 390 upgrade_mod_savepoint(true, 2011011000, 'wiki');
1be91921 391 }
ab422920 392
a4cdd6d2 393 return true;
b8a342d7 394}