3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * This file keeps track of upgrades to the wiki module
21 * Sometimes, changes between versions involve
22 * alterations to database structures and other
23 * major things that may break installations.
25 * The upgrade function in this file will attempt
26 * to perform all the necessary actions to upgrade
27 * your older installation to the current version.
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
33 * @author Jordi Piguillem
35 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
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
51 * 9. Drop useless information
53 * ADITIONAL THINGS AFTER CHAT WITH ELOY:
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
64 * 1. Use recordset instead of record when migrating historic
65 * 2. Select only usefull data on block 06
68 function xmldb_wiki_upgrade($oldversion) {
69 global $CFG, $DB, $OUTPUT;
71 $dbman = $DB->get_manager();
73 // Step 0: Add new fields to main wiki table
74 if ($oldversion < 2010040100) {
75 require_once(dirname(__FILE__) . '/upgradelib.php');
76 echo $OUTPUT->notification('Adding new fields to wiki table', 'notifysuccess');
77 wiki_add_wiki_fields();
79 upgrade_mod_savepoint(true, 2010040100, 'wiki');
82 // Step 1: Rename old tables
83 if ($oldversion < 2010040101) {
84 $tables = array('wiki_pages', 'wiki_locks', 'wiki_entries');
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');
95 upgrade_mod_savepoint(true, 2010040101, 'wiki');
98 // Step 2: Creating new tables
99 if ($oldversion < 2010040102) {
100 require_once(dirname(__FILE__) . '/upgradelib.php');
101 echo $OUTPUT->notification('Installing new wiki module tables', 'notifysuccess');
102 wiki_upgrade_install_20_tables();
103 upgrade_mod_savepoint(true, 2010040102, 'wiki');
106 // Step 3: migrating wiki instances
107 if ($oldversion < 2010040103) {
108 upgrade_set_timeout();
110 // Setting up wiki configuration
111 $sql = "UPDATE {wiki}
113 firstpagetitle = pagename,
115 $DB->execute($sql, array('html'));
117 $sql = "UPDATE {wiki}
120 $DB->execute($sql, array('collaborative', 'group'));
122 $sql = "UPDATE {wiki}
125 $DB->execute($sql, array('individual', 'group'));
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);
142 echo $OUTPUT->notification('Migrating old wikis to new wikis', 'notifysuccess');
143 upgrade_mod_savepoint(true, 2010040103, 'wiki');
146 // Step 4: migrating wiki entries to new subwikis
147 if ($oldversion < 2010040104) {
149 * Migrating wiki entries to new subwikis
151 $sql = "INSERT into {wiki_subwikis} (wikiid, groupid, userid)
152 SELECT DISTINCT e.wikiid, e.groupid, e.userid
153 FROM {wiki_entries_old} e";
154 echo $OUTPUT->notification('Migrating old entries to new subwikis', 'notifysuccess');
156 $DB->execute($sql, array());
158 upgrade_mod_savepoint(true, 2010040104, 'wiki');
161 // Step 5: Migrating pages
162 if ($oldversion < 2010040105) {
164 * Filling pages table with latest versions of every page.
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.
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
179 SELECT max(po.version)
180 FROM {wiki_pages_old} po
181 WHERE p.pagename = po.pagename and
184 echo $OUTPUT->notification('Migrating old pages to new pages', 'notifysuccess');
186 $DB->execute($sql, array('**reparse needed**'));
188 upgrade_mod_savepoint(true, 2010040105, 'wiki');
191 // Step 6: Migrating versions
192 if ($oldversion < 2010040106) {
193 require_once(dirname(__FILE__) . '/upgradelib.php');
194 echo $OUTPUT->notification('Migrating old history to new history', 'notifysuccess');
195 wiki_upgrade_migrate_versions();
196 upgrade_mod_savepoint(true, 2010040106, 'wiki');
199 // Step 7: refresh cachedcontent and fill wiki links table
200 if ($oldversion < 2010040107) {
201 require_once($CFG->dirroot. '/mod/wiki/locallib.php');
202 upgrade_set_timeout();
204 $pages = $DB->get_recordset('wiki_pages');
206 foreach ($pages as $page) {
207 wiki_refresh_cachedcontent($page);
212 echo $OUTPUT->notification('Caching content', 'notifysuccess');
213 upgrade_mod_savepoint(true, 2010040107, 'wiki');
215 // Step 8, migrating files
216 if ($oldversion < 2010040108) {
217 $fs = get_file_storage();
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
225 LEFT OUTER JOIN {wiki} w
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'
230 JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = w.id)
231 ) files ON files.id = po.id";
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://';
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);
253 $thefile = $CFG->dataroot . '/' . $r->courseid . '/moddata/wiki/' . $r->wikiid .'/' . $r->entryid . '/'. $filesection .'/'. $filename;
255 if (is_file($thefile) && is_readable($thefile)) {
256 $filerecord = array('contextid' => $context->id,
257 'component' => 'mod_wiki',
258 'filearea' => 'attachments',
259 'itemid' => $r->subwiki,
261 'filename' => $orgifilename,
262 'userid' => $r->userid);
263 if (!$fs->file_exists($context->id, 'mod_wiki', 'attachments', $r->subwiki, '/', $orgifilename)) {
264 //echo $OUTPUT->notification('Migrating file '.$orgifilename, 'notifysuccess');
265 $storedfile = $fs->create_file_from_pathname($filerecord, $thefile);
267 // we have to create another file here to make sure interlinks work
268 if (!$fs->file_exists($context->id, 'mod_wiki', 'attachments', $r->subwiki, '/', $filename)) {
269 $filerecord['filename'] = $filename;
270 //echo $OUTPUT->notification('Migrating file '.$filename, 'notifysuccess');
271 $storedfile = $fs->create_file_from_pathname($filerecord, $thefile);
274 echo $OUTPUT->notification("Bad data found: $r->pagename <br/> Expected file path: $thefile Please fix the bad file path manually.");
279 upgrade_mod_savepoint(true, 2010040108, 'wiki');
282 // Step 9: clean wiki table
283 if ($oldversion < 2010040109) {
284 $fields = array('summary', 'pagename', 'wtype', 'ewikiprinttitle', 'htmlmode', 'ewikiacceptbinary', 'disablecamelcase', 'setpageflags', 'strippages', 'removepages', 'revertchanges', 'initialcontent');
285 $table = new xmldb_table('wiki');
286 foreach ($fields as $fieldname) {
287 $field = new xmldb_field($fieldname);
288 if ($dbman->field_exists($table, $field)) {
289 $dbman->drop_field($table, $field);
293 echo $OUTPUT->notification('Cleaning wiki table', 'notifysuccess');
294 upgrade_mod_savepoint(true, 2010040109, 'wiki');
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');
303 //foreach ($tables as $tablename) {
304 //$table = new xmldb_table($tablename . '_old');
305 //if ($dbman->table_exists($table)) {
306 //$dbman->drop_table($table);
309 //echo $OUTPUT->notification('Droping old tables', 'notifysuccess');
310 //upgrade_mod_savepoint(true, 2010040120, 'wiki');
313 if ($oldversion < 2010080201) {
315 $sql = "UPDATE {comments}
316 SET commentarea = 'wiki_page'
317 WHERE commentarea = 'wiki_comment_section'";
320 $sql = "UPDATE {tag_instance}
321 SET itemtype = 'wiki_page'
322 WHERE itemtype = 'wiki'";
325 echo $OUTPUT->notification('Updating comments and tags', 'notifysuccess');
327 upgrade_mod_savepoint(true, 2010080201, 'wiki');
330 if ($oldversion < 2010102500) {
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'));
336 // Launch add key subwikifk
337 $dbman->add_key($table, $key);
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'));
343 // Launch add key subwikifk
344 $dbman->add_key($table, $key);
346 // wiki savepoint reached
347 upgrade_mod_savepoint(true, 2010102500, 'wiki');
350 if ($oldversion < 2010102800) {
352 $sql = "UPDATE {tag_instance}
353 SET itemtype = 'wiki_pages'
354 WHERE itemtype = 'wiki_page'";
357 echo $OUTPUT->notification('Updating tags itemtype', 'notifysuccess');
359 upgrade_mod_savepoint(true, 2010102800, 'wiki');
362 if ($oldversion < 2011011000) {
363 // Fix wiki in the post table after upgrade from 1.9
364 $table = new xmldb_table('wiki');
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);
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);
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);
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);
390 upgrade_mod_savepoint(true, 2011011000, 'wiki');