Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/wiki/db/upgrade.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
00017 
00039 function xmldb_wiki_upgrade($oldversion) {
00040     global $CFG, $DB, $OUTPUT;
00041 
00042     $dbman = $DB->get_manager();
00043 
00044     // Step 0: Add new fields to main wiki table
00045     if ($oldversion < 2010040100) {
00046         require_once(dirname(__FILE__) . '/upgradelib.php');
00047         echo $OUTPUT->notification('Adding new fields to wiki table', 'notifysuccess');
00048         wiki_add_wiki_fields();
00049 
00050         upgrade_mod_savepoint(true, 2010040100, 'wiki');
00051     }
00052 
00053     // Step 1: Rename old tables
00054     if ($oldversion < 2010040101) {
00055         $tables = array('wiki_pages', 'wiki_locks', 'wiki_entries');
00056 
00057         echo $OUTPUT->notification('Renaming old wiki module tables', 'notifysuccess');
00058         foreach ($tables as $tablename) {
00059             $table = new xmldb_table($tablename);
00060             if ($dbman->table_exists($table)) {
00061                 if ($dbman->table_exists($table)) {
00062                     $dbman->rename_table($table, $tablename . '_old');
00063                 }
00064             }
00065         }
00066         upgrade_mod_savepoint(true, 2010040101, 'wiki');
00067     }
00068 
00069     // Step 2: Creating new tables
00070     if ($oldversion < 2010040102) {
00071         require_once(dirname(__FILE__) . '/upgradelib.php');
00072         echo $OUTPUT->notification('Installing new wiki module tables', 'notifysuccess');
00073         wiki_upgrade_install_20_tables();
00074         upgrade_mod_savepoint(true, 2010040102, 'wiki');
00075     }
00076 
00077     // Step 3: migrating wiki instances
00078     if ($oldversion < 2010040103) {
00079         upgrade_set_timeout();
00080 
00081         // Setting up wiki configuration
00082         $sql = "UPDATE {wiki}
00083                     SET intro = summary,
00084                     firstpagetitle = pagename,
00085                     defaultformat = ?";
00086         $DB->execute($sql, array('html'));
00087 
00088         $sql = "UPDATE {wiki}
00089                     SET wikimode = ?
00090                     WHERE wtype = ?";
00091         $DB->execute($sql, array('collaborative', 'group'));
00092 
00093         $sql = "UPDATE {wiki}
00094                     SET wikimode = ?
00095                     WHERE wtype != ?";
00096         $DB->execute($sql, array('individual', 'group'));
00097 
00098         // Removing edit & create capability to students in old teacher wikis
00099         $studentroles = $DB->get_records('role', array('archetype' => 'student'));
00100         $wikis = $DB->get_records('wiki');
00101         foreach ($wikis as $wiki) {
00102             echo $OUTPUT->notification('Migrating '.$wiki->wtype.' type wiki instance: '.$wiki->name, 'notifysuccess');
00103             if ($wiki->wtype == 'teacher') {
00104                 $cm = get_coursemodule_from_instance('wiki', $wiki->id);
00105                 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
00106                 foreach ($studentroles as $studentrole) {
00107                     role_change_permission($studentrole->id, $context, 'mod/wiki:editpage', CAP_PROHIBIT);
00108                     role_change_permission($studentrole->id, $context, 'mod/wiki:createpage', CAP_PROHIBIT);
00109                 }
00110             }
00111         }
00112 
00113         echo $OUTPUT->notification('Migrating old wikis to new wikis', 'notifysuccess');
00114         upgrade_mod_savepoint(true, 2010040103, 'wiki');
00115     }
00116 
00117     // Step 4: migrating wiki entries to new subwikis
00118     if ($oldversion < 2010040104) {
00122         $sql = "INSERT INTO {wiki_subwikis} (wikiid, groupid, userid)
00123                 SELECT DISTINCT e.wikiid, e.groupid, e.userid
00124                   FROM {wiki_entries_old} e";
00125         echo $OUTPUT->notification('Migrating old entries to new subwikis', 'notifysuccess');
00126 
00127         $DB->execute($sql, array());
00128 
00129         upgrade_mod_savepoint(true, 2010040104, 'wiki');
00130     }
00131 
00132     // Step 5: Migrating pages
00133     if ($oldversion < 2010040105) {
00134 
00135         // select all wiki pages
00136         $sql = "SELECT s.id, p.pagename, p.created, p.lastmodified, p.userid, p.hits
00137                   FROM {wiki_pages_old} p
00138                   LEFT OUTER JOIN {wiki_entries_old} e ON e.id = p.wiki
00139                   LEFT OUTER JOIN {wiki_subwikis} s ON s.wikiid = e.wikiid AND s.groupid = e.groupid AND s.userid = e.userid
00140                  WHERE p.version = (SELECT max(po.version)
00141                                       FROM {wiki_pages_old} po
00142                                      WHERE p.pagename = po.pagename AND p.wiki = po.wiki)";
00143         echo $OUTPUT->notification('Migrating old pages to new pages', 'notifysuccess');
00144 
00145         $records = $DB->get_recordset_sql($sql);
00146         foreach ($records as $record) {
00147             $page = new stdclass();
00148             $page->subwikiid     = $record->id;
00149             $page->title         = $record->pagename;
00150             $page->cachedcontent = '**reparse needed**';
00151             $page->timecreated   = $record->created;
00152             $page->timemodified  = $record->lastmodified;
00153             $page->userid        = $record->userid;
00154             $page->pageviews     = $record->hits;
00155             try {
00156                 // make sure there is no duplicated records exist
00157                 if (!$DB->record_exists('wiki_pages', array('subwikiid'=>$record->id, 'userid'=>$record->userid, 'title'=>$record->pagename))) {
00158                     $DB->insert_record('wiki_pages', $page);
00159                 }
00160             } catch (dml_exception $e) {
00161                 // catch possible insert exception
00162                 debugging($e->getMessage());
00163                 continue;
00164             }
00165         }
00166         $records->close();
00167 
00168         upgrade_mod_savepoint(true, 2010040105, 'wiki');
00169     }
00170 
00171     // Step 6: Migrating versions
00172     if ($oldversion < 2010040106) {
00173         require_once(dirname(__FILE__) . '/upgradelib.php');
00174         echo $OUTPUT->notification('Migrating old history to new history', 'notifysuccess');
00175         wiki_upgrade_migrate_versions();
00176         upgrade_mod_savepoint(true, 2010040106, 'wiki');
00177     }
00178 
00179     // Step 7: refresh cachedcontent and fill wiki links table
00180     if ($oldversion < 2010040107) {
00181         require_once($CFG->dirroot. '/mod/wiki/locallib.php');
00182         upgrade_set_timeout();
00183 
00184         $pages = $DB->get_recordset('wiki_pages');
00185 
00186         foreach ($pages as $page) {
00187             wiki_refresh_cachedcontent($page);
00188         }
00189 
00190         $pages->close();
00191 
00192         echo $OUTPUT->notification('Caching content', 'notifysuccess');
00193         upgrade_mod_savepoint(true, 2010040107, 'wiki');
00194     }
00195     // Step 8, migrating files
00196     if ($oldversion < 2010040108) {
00197         $fs = get_file_storage();
00198         $sql = "SELECT files.*, po.meta AS filemeta FROM {wiki_pages_old} po JOIN (
00199                     SELECT DISTINCT po.id, po.pagename, w.id AS wikiid, po.userid,
00200                         eo.id AS entryid, eo.groupid, s.id AS subwiki,
00201                         w.course AS courseid, cm.id AS cmid
00202                         FROM {wiki_pages_old} po
00203                         LEFT OUTER JOIN {wiki_entries_old} eo
00204                         ON eo.id=po.wiki
00205                         LEFT OUTER JOIN {wiki} w
00206                         ON w.id = eo.wikiid
00207                         LEFT OUTER JOIN {wiki_subwikis} s
00208                         ON s.groupid = eo.groupid AND s.wikiid = eo.wikiid AND eo.userid = s.userid
00209                         JOIN {modules} m ON m.name = 'wiki'
00210                         JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = w.id)
00211                 ) files ON files.id = po.id";
00212 
00213         $rs = $DB->get_recordset_sql($sql);
00214         foreach ($rs as $r) {
00215             if (strpos($r->pagename, 'internal://') !== false) {
00216                 // Found a file resource!
00217                 $pattern = 'internal://';
00218                 $matches = array();
00219                 $filename = str_replace($pattern, '', $r->pagename);
00220                 $orgifilename = $filename = clean_param($filename, PARAM_FILE);
00221                 $context = get_context_instance(CONTEXT_MODULE, $r->cmid);
00222                 $filemeta = unserialize($r->filemeta);
00223                 $filesection = $filemeta['section'];
00224                 // When attach a file to wiki page, user can customize the file name instead of original file name
00225                 // if user did, old wiki will create two pages, internal://original_pagename and internal://renamed_pagename
00226                 // internal://original_pagename record has renamed pagename in meta field
00227                 // but all file have this field
00228                 // old wiki will rename file names to filter space and special character
00229                 if (!empty($filemeta['Content-Location'])) {
00230                     $orgifilename = urldecode($filemeta['Content-Location']);
00231                     $orgifilename = str_replace(' ', '_', $orgifilename);
00232                 }
00233                 $thefile = $CFG->dataroot . '/' . $r->courseid . '/moddata/wiki/' . $r->wikiid .'/' . $r->entryid . '/'. $filesection .'/'. $filename;
00234 
00235                 if (is_file($thefile) && is_readable($thefile)) {
00236                     $filerecord = array('contextid' => $context->id,
00237                                         'component' => 'mod_wiki',
00238                                         'filearea'  => 'attachments',
00239                                         'itemid'    => $r->subwiki,
00240                                         'filepath'  => '/',
00241                                         'filename'  => $orgifilename,
00242                                         'userid'    => $r->userid);
00243                     if (!$fs->file_exists($context->id, 'mod_wiki', 'attachments', $r->subwiki, '/', $orgifilename)) {
00244                         //echo $OUTPUT->notification('Migrating file '.$orgifilename, 'notifysuccess');
00245                         $storedfile = $fs->create_file_from_pathname($filerecord, $thefile);
00246                     }
00247                     // we have to create another file here to make sure interlinks work
00248                     if (!$fs->file_exists($context->id, 'mod_wiki', 'attachments', $r->subwiki, '/', $filename)) {
00249                         $filerecord['filename'] = $filename;
00250                         //echo $OUTPUT->notification('Migrating file '.$filename, 'notifysuccess');
00251                         $storedfile = $fs->create_file_from_pathname($filerecord, $thefile);
00252                     }
00253                 } else {
00254                     echo $OUTPUT->notification("Bad data found: $r->pagename <br/> Expected file path: $thefile Please fix the bad file path manually.");
00255                 }
00256             }
00257         }
00258         $rs->close();
00259         upgrade_mod_savepoint(true, 2010040108, 'wiki');
00260     }
00261 
00262     // Step 9: clean wiki table
00263     if ($oldversion < 2010040109) {
00264         $fields = array('summary', 'pagename', 'wtype', 'ewikiprinttitle', 'htmlmode', 'ewikiacceptbinary', 'disablecamelcase', 'setpageflags', 'strippages', 'removepages', 'revertchanges', 'initialcontent');
00265         $table = new xmldb_table('wiki');
00266         foreach ($fields as $fieldname) {
00267             $field = new xmldb_field($fieldname);
00268             if ($dbman->field_exists($table, $field)) {
00269                 $dbman->drop_field($table, $field);
00270             }
00271 
00272         }
00273         echo $OUTPUT->notification('Cleaning wiki table', 'notifysuccess');
00274         upgrade_mod_savepoint(true, 2010040109, 'wiki');
00275     }
00276 
00277     if ($oldversion < 2010080201) {
00278 
00279         $sql = "UPDATE {comments}
00280                 SET commentarea = 'wiki_page'
00281                 WHERE commentarea = 'wiki_comment_section'";
00282         $DB->execute($sql);
00283 
00284         $sql = "UPDATE {tag_instance}
00285                 SET itemtype = 'wiki_page'
00286                 WHERE itemtype = 'wiki'";
00287         $DB->execute($sql);
00288 
00289         echo $OUTPUT->notification('Updating comments and tags', 'notifysuccess');
00290 
00291         upgrade_mod_savepoint(true, 2010080201, 'wiki');
00292     }
00293 
00294     if ($oldversion < 2010102500) {
00295 
00296         // Define key subwikifk (foreign) to be added to wiki_pages
00297         $table = new xmldb_table('wiki_pages');
00298         $key = new xmldb_key('subwikifk', XMLDB_KEY_FOREIGN, array('subwikiid'), 'wiki_subwikis', array('id'));
00299 
00300         // Launch add key subwikifk
00301         $dbman->add_key($table, $key);
00302 
00303          // Define key subwikifk (foreign) to be added to wiki_links
00304         $table = new xmldb_table('wiki_links');
00305         $key = new xmldb_key('subwikifk', XMLDB_KEY_FOREIGN, array('subwikiid'), 'wiki_subwikis', array('id'));
00306 
00307         // Launch add key subwikifk
00308         $dbman->add_key($table, $key);
00309 
00310         // wiki savepoint reached
00311         upgrade_mod_savepoint(true, 2010102500, 'wiki');
00312     }
00313 
00314     if ($oldversion < 2010102800) {
00315 
00316         $sql = "UPDATE {tag_instance}
00317                 SET itemtype = 'wiki_pages'
00318                 WHERE itemtype = 'wiki_page'";
00319         $DB->execute($sql);
00320 
00321         echo $OUTPUT->notification('Updating tags itemtype', 'notifysuccess');
00322 
00323         upgrade_mod_savepoint(true, 2010102800, 'wiki');
00324     }
00325 
00326     if ($oldversion < 2011011000) {
00327         // Fix wiki in the post table after upgrade from 1.9
00328         $table = new xmldb_table('wiki');
00329 
00330         // name should default to Wiki
00331         $field = new xmldb_field('name', XMLDB_TYPE_CHAR, 255, null, XMLDB_NOTNULL, null, 'Wiki', 'course');
00332         if ($dbman->field_exists($table, $field)) {
00333             $dbman->change_field_default($table, $field);
00334         }
00335 
00336         // timecreated field is missing after 1.9 upgrade
00337         $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'introformat');
00338         if (!$dbman->field_exists($table, $field)) {
00339             $dbman->add_field($table, $field);
00340         }
00341 
00342         // timemodified field is missing after 1.9 upgrade
00343         $field = new xmldb_field('timemodified', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'timecreated');
00344         if (!$dbman->field_exists($table, $field)) {
00345             $dbman->add_field($table, $field);
00346         }
00347 
00348         // scaleid is not there any more
00349         $field = new xmldb_field('scaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null);
00350         if ($dbman->field_exists($table, $field)) {
00351             $dbman->drop_field($table, $field);
00352         }
00353 
00354         upgrade_mod_savepoint(true, 2011011000, 'wiki');
00355     }
00356 
00357     // TODO: Will hold the old tables so we will have chance to fix problems
00358     // Will remove old tables once migrating 100% stable
00359     // Step 10: delete old tables
00360     //if ($oldversion < 2011060300) {
00361         //$tables = array('wiki_pages', 'wiki_locks', 'wiki_entries');
00362 
00363         //foreach ($tables as $tablename) {
00364             //$table = new xmldb_table($tablename . '_old');
00365             //if ($dbman->table_exists($table)) {
00366                 //$dbman->drop_table($table);
00367             //}
00368         //}
00369         //echo $OUTPUT->notification('Droping old tables', 'notifysuccess');
00370         //upgrade_mod_savepoint(true, 2011060300, 'wiki');
00371     //}
00372 
00373     // Moodle v2.1.0 release upgrade line
00374     // Put any upgrade step following this
00375 
00376     // Moodle v2.2.0 release upgrade line
00377     // Put any upgrade step following this
00378 
00379     return true;
00380 }
 All Data Structures Namespaces Files Functions Variables Enumerations