Index: db/upgrade.php ========================================================= --- db/upgrade.php (revision 1.4) +++ db/upgrade.php Tue Apr 20 14:34:07 WST 2010 @@ -1,8 +1,23 @@ . + + /** - * This file keeps track of upgrades to - * the wiki module + * This file keeps track of upgrades to the wiki module * * Sometimes, changes between versions involve * alterations to database structures and other @@ -22,7 +37,6 @@ * */ - /** * * TODO LIST: @@ -51,200 +65,73 @@ * 1. Use recordset instead of record when migrating historic * 2. Select only usefull data on block 06 * + * FROM DONGSHENG + * 1. In old wiki (moodle 2.0), wiki.summary doesn't exist, the sql in + * wiki_upgrade_migrate_versions should be midified to support 1.9 and 2.0 both + * 2. the sql in wiki_upgrade_migrate_versions should be 'LEFT OUTER JOIN {wiki_old} w' + * not '{wiki} w' */ -require_once($CFG->dirroot.'/mod/wiki/db/migration/lib.php'); - - -function xmldb_wiki_upgrade($oldversion=0) { - +function xmldb_wiki_upgrade($oldversion) { global $CFG, $DB, $OUTPUT; $dbman = $DB->get_manager(); $result = true; - if ($oldversion < 2010032302 ){ - - // Adding fields to wiki table - $wikitable = new xmldb_table('wiki'); - - $field = new xmldb_field('intro', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null); - $dbman->add_field($wikitable, $field); - - $field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null); - $dbman->add_field($wikitable, $field); - - $field = new xmldb_field('firstpagetitle', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, 'First Page', null); - $dbman->add_field($wikitable, $field); - - $field = new xmldb_field('defaultformat', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'creole', null); - $dbman->add_field($wikitable, $field); - - $field = new xmldb_field('forceformat', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null); - $dbman->add_field($wikitable, $field); - - $field = new xmldb_field('ratingmode', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null); - $dbman->add_field($wikitable, $field); - - $field = new xmldb_field('scaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null); - $dbman->add_field($wikitable, $field); - - $field = new xmldb_field('editbegin', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null); - $dbman->add_field($wikitable, $field); - - $field = new xmldb_field('editend', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0', null); - $dbman->add_field($wikitable, $field); - - // Renaming tables - $pagestable = new xmldb_table('wiki_pages'); - $dbman->rename_table($pagestable,'wiki_pages_old'); - $lockstable = new xmldb_table('wiki_locks'); - $dbman->rename_table($lockstable, 'wiki_locks_old'); - $entriestable = new xmldb_table('wiki_entries'); - $dbman->rename_table($entriestable, 'wiki_entries_old'); + // Step 1: Rename old tables + if ($result && $oldversion < 2010040100) { + $tables = array('wiki', 'wiki_pages', 'wiki_locks', 'wiki_entries'); echo $OUTPUT->notification('Renaming old wiki module tables', 'notifysuccess'); - - // Creating new tables - // Creating wiki_subwikis table - $subwikitable = new xmldb_table('wiki_subwikis'); - - $subwikitable->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null); - $subwikitable->add_field('wikiid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null); - $subwikitable->add_field('groupid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null); - $subwikitable->add_key('pk', XMLDB_KEY_PRIMARY, array('id'), null, null); - $subwikitable->add_key('wikiuk', XMLDB_KEY_UNIQUE, array('wikiid', 'groupid'), null, null); - $subwikitable->add_key('fk', XMLDB_KEY_FOREIGN, array('wikiid'), 'wiki', array('id')); - - $dbman->create_table($subwikitable); - - - // Creating wiki_pages table - $pagestable = new xmldb_table('wiki_pages'); - - $pagestable->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null); - $pagestable->add_field('subwikiid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null); - $pagestable->add_field('title', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, 'title', null); - $pagestable->add_field('cachedcontent', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null, null); - $pagestable->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null); - $pagestable->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null); - $pagestable->add_field('timerendered', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null); - $pagestable->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null); - $pagestable->add_field('pageviews', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null); - $pagestable->add_field('readonly', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null); - $pagestable->add_key('pk', XMLDB_KEY_PRIMARY, array('id'), null, null); - $pagestable->add_key('subwikifk', XMLDB_KEY_FOREIGN, array('subwikiid'), 'wiki_subwiki', array('id')); - - $dbman->create_table($pagestable); - - - // Creating wiki_versions table - $versionstable = new xmldb_table('wiki_versions'); - - $versionstable->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null); - $versionstable->add_field('pageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null); - $versionstable->add_field('content', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null, null); - $versionstable->add_field('contentformat', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'creole', null); - $versionstable->add_field('version', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null); - $versionstable->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null); - $versionstable->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null); - $versionstable->add_key('pk', XMLDB_KEY_PRIMARY, array('id'), null, null); - $versionstable->add_key('pagefk', XMLDB_KEY_FOREIGN, array('pageid'), 'wiki_pages', array('id')); - - $dbman->create_table($versionstable); - - - // Creating wiki_links table - $linkstable = new xmldb_table('wiki_links'); - - $linkstable->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null); - $linkstable->add_field('subwikiid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null); - $linkstable->add_field('frompageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null); - $linkstable->add_field('topageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null); - $linkstable->add_field('tomissingpage', XMLDB_TYPE_CHAR, '255', XMLDB_UNSIGNED, null, null, null, null); - $linkstable->add_key('pk', XMLDB_KEY_PRIMARY, array('id'), null, null); - $linkstable->add_key('pagefk', XMLDB_KEY_FOREIGN, array('frompageid'), 'wiki_pages', array('id')); - $linkstable->add_key('subwikifk', XMLDB_KEY_FOREIGN, array('subwikiid'), 'wiki_subwiki', array('id')); - - $dbman->create_table($linkstable); - - - // Creating wiki_synonyms table - $synonymstable = new xmldb_table('wiki_synonyms'); - - $synonymstable->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null); - $synonymstable->add_field('subwikiid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null); - $synonymstable->add_field('pageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null); - $synonymstable->add_field('pagesynonym', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, 'Pagesynonym', null); - $synonymstable->add_key('pk', XMLDB_KEY_PRIMARY, array('id'), null, null); - $synonymstable->add_key('pagefk', XMLDB_KEY_UNIQUE, array('pageid', 'pagesynonym'), null, null); - - $dbman->create_table($synonymstable); - - - // Creating wiki_locks table - $lockstable = new xmldb_table('wiki_locks'); - - $lockstable->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null); - $lockstable->add_field('pageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null); - $lockstable->add_field('sectionname', XMLDB_TYPE_CHAR, '255', null, null, null, null, null); - $lockstable->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null); - $lockstable->add_field('lockedat', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null); - $lockstable->add_key('pk', XMLDB_KEY_PRIMARY, array('id'), null, null); - - $dbman->create_table($lockstable); - - /** - * - * @TODO: Add here other new tables (grades, rate, etc) creation when they become definitive - * - * - */ - - echo $OUTPUT->notification('Creating new wiki module tables', 'notifysuccess'); - - upgrade_mod_savepoint($result, 2010032302, 'wiki'); + foreach ($tables as $tablename) { + $table = new xmldb_table($tablename); + if ($dbman->table_exists($table)) { + if ($dbman->table_exists($table)) { + $dbman->rename_table($table, $tablename.'_old'); + } + } + } + upgrade_mod_savepoint($result, 2010040100, 'wiki'); + } + // Step 2: Creating new tables + if ($result && $oldversion < 2010040101) { + require_once(dirname(__FILE__) . '/upgradelib.php'); + echo $OUTPUT->notification('Installing new wiki module tables', 'notifysuccess'); + wiki_upgrade_install_20_tables(); + upgrade_mod_savepoint($result, 2010040101, 'wiki'); } - if ($oldversion < 2010032303){ - // Setting up wiki configuration - $sql = 'UPDATE {wiki} w ' . - 'SET w.intro = w.summary, ' . - 'w.firstpagetitle = w.pagename, ' . - 'w.defaultformat = "html"'; - - $DB->execute($sql); - - echo $OUTPUT->notification('Migrating old wikis to new wikis', 'notifysuccess'); - upgrade_mod_savepoint($result, 2010032303, 'wiki'); - + // Step 3: migrating wiki instances + if ($result && $oldversion < 2010040102) { + require_once(dirname(__FILE__) . '/upgradelib.php'); + echo $OUTPUT->notification('Migrating old wiki instances to new wiki instances', 'notifysuccess'); + wiki_upgrade_migrate_instances(); + upgrade_mod_savepoint($result, 2010040102, 'wiki'); } - if ($oldversion < 2010032304){ - - + // Step 4: migrating wiki entries to new subwikis + if ($result && $oldversion < 2010040103) { /** * Migrating wiki entries to new subwikis * * IMPORTANT NOTE: Student wikis will NOT be migrated. * Wiki 2.0 has not this feature. * @TODO: Arrage a solution with Moodle HQ - * */ $sql = 'INSERT into {wiki_subwikis} (wikiid, groupid) ' . 'SELECT DISTINCT e.wikiid, e.groupid ' . 'FROM {wiki_entries_old} e ' . 'GROUP BY e.wikiid, e.groupid'; - $DB->execute($sql, array()); - echo $OUTPUT->notification('Migrating old entries to new subwikis', 'notifysuccess'); - upgrade_mod_savepoint($result, 2010032304, 'wiki'); - } + $DB->execute($sql, array()); - if ($oldversion < 2010032305){ + upgrade_mod_savepoint($result, 2010040103, 'wiki'); + } + // Step 5: Migrating pages + if ($result && $oldversion < 2010040104) { /** * Filling pages table with latest versions of every page. * @@ -267,101 +154,36 @@ 'p.wiki = po.wiki ' . 'ORDER BY p.version DESC ' . 'LIMIT 1)'; - $DB->execute($sql, array()); - echo $OUTPUT->notification('Migrating old pages to new pages', 'notifysuccess'); - upgrade_mod_savepoint($result, 2010032305, 'wiki'); - } - - if ($oldversion < 2010032306){ - $sql = 'SELECT po.id as oldpage_id, po.pagename as oldpage_pagename, po.version, po.flags, po.content, po.author, po.userid as oldpage_userid, po.created, po.lastmodified, po.refs, po.meta, po.hits, po.wiki, ' . - 'p.id as newpage_id, p.subwikiid, p.title, p.cachedcontent, p.timecreated, p.timemodified as newpage_timemodified, p.timerendered, p.userid as newpage_userid, p.pageviews, p.readonly, ' . - 'e.id as entry_id, e.wikiid, e.course as entrycourse, e.groupid, e.userid as entry_userid, e.pagename as entry_pagename, e.timemodified as entry_timemodified, ' . - 'w.id as wiki_id, w.course as wiki_course, w.name, w.summary, w.pagename as wiki_pagename, w.wtype, w.ewikiprinttitle, w.htmlmode, w.ewikiacceptbinary, w.disablecamelcase, w.setpageflags, w.strippages, w.removepages, w.revertchanges, w.initialcontent, w.timemodified as wiki_timemodified ' . - 'FROM {wiki_pages_old} po LEFT OUTER JOIN {wiki_entries_old} e ' . - 'ON e.id = po.wiki ' . - 'LEFT OUTER JOIN {wiki} w ' . - 'ON w.id = e.wikiid '. - 'LEFT OUTER JOIN {wiki_subwikis} s ' . - 'ON e.groupid = s.groupid AND e.wikiid = s.wikiid ' . - 'LEFT OUTER JOIN {wiki_pages} p ' . - 'ON po.pagename = p.title AND p.subwikiid = s.id'; - $pagesinfo = $DB->get_recordset_sql($sql, array()); - - while ($pagesinfo->valid()){ - $pageinfo = $pagesinfo->current(); - - $oldpage = new StdClass(); - $oldpage->id = $pageinfo->oldpage_id; - $oldpage->pagename = $pageinfo->oldpage_pagename; - $oldpage->version = $pageinfo->version; - $oldpage->flags = $pageinfo->flags; - $oldpage->content = $pageinfo->content; - $oldpage->author = $pageinfo->author; - $oldpage->userid = $pageinfo->oldpage_userid; - $oldpage->created = $pageinfo->created; - $oldpage->lastmodified = $pageinfo->lastmodified; - $oldpage->refs = $pageinfo->refs; - $oldpage->meta = $pageinfo->meta; - $oldpage->hits = $pageinfo->hits; - $oldpage->wiki = $pageinfo->wiki; - - $page = new StdClass(); - $page->id = $pageinfo->newpage_id; - $page->subwikiid = $pageinfo->subwikiid; - $page->title = $pageinfo->title; - $page->cachedcontent = $pageinfo->cachedcontent; - $page->timecreated = $pageinfo->timecreated; - $page->timemodified = $pageinfo->newpage_timemodified; - $page->timerendered = $pageinfo->timerendered; - $page->userid = $pageinfo->newpage_userid; - $page->pageviews = $pageinfo->pageviews; - $page->readonly = $pageinfo->readonly; - - $entry = new StdClass(); - $entry->id = $pageinfo->entry_id; - $entry->wikiid = $pageinfo->wikiid; - $entry->course = $pageinfo->entrycourse; - $entry->groupid = $pageinfo->groupid; - $entry->userid = $pageinfo->entry_userid; - $entry->pagename = $pageinfo->entry_pagename; - $entry->timemodified = $pageinfo->entry_timemodified; - - $wiki = new StdClass(); - $wiki->id = $pageinfo->wiki_id; - $wiki->course = $pageinfo->wiki_course; - $wiki->name = $pageinfo->name; - $wiki->summary = $pageinfo->summary; - $wiki->pagename = $pageinfo->wiki_pagename; - $wiki->wtype = $pageinfo->wtype; - $wiki->ewikiprinttitle = $pageinfo->ewikiprinttitle; - $wiki->htmlmode = $pageinfo->htmlmode; - $wiki->ewikiacceptbinary = $pageinfo->ewikiacceptbinary; - $wiki->disablecamelcase = $pageinfo->disablecamelcase; - $wiki->setpageflags = $pageinfo->setpageflags; - $wiki->strippages = $pageinfo->strippages; - $wiki->removepages = $pageinfo->removepages; - $wiki->revertchanges = $pageinfo->revertchanges; - $wiki->initialcontent = $pageinfo->initialcontent; - $wiki->timemodified = $pageinfo->wiki_timemodified; - - $version = new StdClass(); - $version->pageid = $page->id; - $version->content = wiki_ewiki_2_html($entry, $oldpage, $wiki); - $version->contentformat = "html"; - $version->version = $oldpage->version; - $version->timecreated = $oldpage->lastmodified; - $version->userid = $oldpage->userid; - $DB->insert_record('wiki_versions', $version); - - $pagesinfo->next(); - } + $DB->execute($sql, array()); - $pagesinfo->close(); + upgrade_mod_savepoint($result, 2010040104, 'wiki'); + } + // Step 6: Migrating versions + if ($result && $oldversion < 2010040105) { + require_once(dirname(__FILE__) . '/upgradelib.php'); echo $OUTPUT->notification('Migrating old history to new history', 'notifysuccess'); - upgrade_mod_savepoint($result, 2010032306, 'wiki'); + wiki_upgrade_migrate_versions(); + upgrade_mod_savepoint($result, 2010040105, 'wiki'); } + + // TODO: Should we delete the old tables? + // Step 7: delete old tables + //if ($result && $oldversion < 2010040106) { + //$tables = array('wiki', 'wiki_pages', 'wiki_locks', 'wiki_entries'); + + //foreach ($tables as $tablename) { + //$table = new xmldb_table($tablename.'_old'); + //if ($dbman->table_exists($table)) { + //if ($dbman->table_exists($table)) { + //$dbman->drop_table($table); + //} + //} + //} + //echo $OUTPUT->notification('Droping old tables', 'notifysuccess'); + //upgrade_mod_savepoint($result, 2010040106, 'wiki'); + //} return $result; } Index: db/upgradelib.php ========================================================= --- db/upgradelib.php Tue Apr 20 14:10:31 WST 2010 +++ db/upgradelib.php Tue Apr 20 14:10:31 WST 2010 @@ -0,0 +1,339 @@ +. + +/** + * @package mod-wiki + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Install wiki 2.0 tables + */ +function wiki_upgrade_install_20_tables() { + global $DB; + upgrade_set_timeout(); + $dbman = $DB->get_manager(); + /// Define table wiki to be created + $table = new xmldb_table('wiki'); + + /// Adding fields to table wiki + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, 'Wiki'); + $table->add_field('intro', XMLDB_TYPE_TEXT, 'medium', null, null, null, null); + $table->add_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('firstpagetitle', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, 'First Page'); + $table->add_field('defaultformat', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'creole'); + $table->add_field('forceformat', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('ratingmode', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('scaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('editbegin', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('editend', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0'); + + /// Adding keys to table wiki + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + + /// Adding indexes to table wiki + $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course')); + + /// Conditionally launch create table for wiki + if (!$dbman->table_exists($table)) { + $dbman->create_table($table); + } + + /// Define table wiki_subwikis to be created + $table = new xmldb_table('wiki_subwikis'); + + /// Adding fields to table wiki_subwikis + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('wikiid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('groupid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); + + /// Adding keys to table wiki_subwikis + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->add_key('wikiidgroupid', XMLDB_KEY_UNIQUE, array('wikiid', 'groupid')); + $table->add_key('wikifk', XMLDB_KEY_FOREIGN, array('wikiid'), 'wiki', array('id')); + + /// Conditionally launch create table for wiki_subwikis + if (!$dbman->table_exists($table)) { + $dbman->create_table($table); + } + + /// Define table wiki_pages to be created + $table = new xmldb_table('wiki_pages'); + + /// Adding fields to table wiki_pages + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('subwikiid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('title', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, 'title'); + $table->add_field('cachedcontent', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null); + $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('timerendered', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('pageviews', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('readonly', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + + /// Adding keys to table wiki_pages + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->add_key('subwikititle', XMLDB_KEY_UNIQUE, array('subwikiid', 'title')); + $table->add_key('subwikifk', XMLDB_KEY_FOREIGN, array('subwikiid'), 'wiki_subwiki', array('id')); + + /// Conditionally launch create table for wiki_pages + if (!$dbman->table_exists($table)) { + $dbman->create_table($table); + } + + /// Define table wiki_versions to be created + $table = new xmldb_table('wiki_versions'); + + /// Adding fields to table wiki_versions + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('pageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('content', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null); + $table->add_field('contentformat', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'creole'); + $table->add_field('version', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + + /// Adding keys to table wiki_versions + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->add_key('pagefk', XMLDB_KEY_FOREIGN, array('pageid'), 'wiki_pages', array('id')); + + /// Conditionally launch create table for wiki_versions + if (!$dbman->table_exists($table)) { + $dbman->create_table($table); + } + + /// Define table wiki_synonyms to be created + $table = new xmldb_table('wiki_synonyms'); + + /// Adding fields to table wiki_synonyms + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('subwikiid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('pageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('pagesynonym', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, 'Pagesynonym'); + + /// Adding keys to table wiki_synonyms + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->add_key('pageidsyn', XMLDB_KEY_UNIQUE, array('pageid', 'pagesynonym')); + + /// Conditionally launch create table for wiki_synonyms + if (!$dbman->table_exists($table)) { + $dbman->create_table($table); + } + + /// Define table wiki_links to be created + $table = new xmldb_table('wiki_links'); + + /// Adding fields to table wiki_links + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('subwikiid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('frompageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('topageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('tomissingpage', XMLDB_TYPE_CHAR, '255', null, null, null, null); + + /// Adding keys to table wiki_links + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->add_key('frompageidfk', XMLDB_KEY_FOREIGN, array('frompageid'), 'wiki_pages', array('id')); + $table->add_key('subwikifk', XMLDB_KEY_FOREIGN, array('subwikiid'), 'wiki_subwiki', array('id')); + + /// Conditionally launch create table for wiki_links + if (!$dbman->table_exists($table)) { + $dbman->create_table($table); + } + + /// Define table wiki_locks to be created + $table = new xmldb_table('wiki_locks'); + + /// Adding fields to table wiki_locks + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('pageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('sectionname', XMLDB_TYPE_CHAR, '255', null, null, null, null); + $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('lockedat', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + + /// Adding keys to table wiki_locks + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + + /// Conditionally launch create table for wiki_locks + if (!$dbman->table_exists($table)) { + $dbman->create_table($table); + } + + /// Define table wiki_rates to be created + $table = new xmldb_table('wiki_rates'); + + /// Adding fields to table wiki_rates + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('rating', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('pageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('versionid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0'); + $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + + /// Adding keys to table wiki_rates + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + + /// Conditionally launch create table for wiki_rates + if (!$dbman->table_exists($table)) { + $dbman->create_table($table); + } +} + +/** + * Convert old wiki instance to new one + * @param object $record old wiki intance record + * @return object the new wiki instance record + */ +function wiki_upgrade_convert_instance($record) { + $newrecord = new stdclass; + $newrecord->course = $record->course; + $newrecord->name = $record->name; + if (isset($record->summary)) { + // 1.9 + $newrecord->intro = $record->summary; + } else { + // head + $newrecord->intro = $record->intro; + $newrecord->introformat = $record->introformat; + } + $newrecord->timemodified = $record->timemodified; + $newrecord->firstpagetitle = $record->pagename; + $newrecord->defaultformat = 'html'; + $newrecord->forceformat = 0; + $newrecord->ratingmode = 0; + $newrecord->scaleid = 0; + $newrecord->editbegin = 0; + $newrecord->editend = 0; + return $newrecord; +} + +/** + * Migrating wiki instances + */ +function wiki_upgrade_migrate_instances() { + global $DB; + upgrade_set_timeout(); + $moduleid = $DB->get_field('modules', 'id', array('name'=>'wiki'), MUST_EXIST); + $oldwikis = $DB->get_recordset('wiki_old'); + foreach($oldwikis as $oldrecord) { + $newrecord = wiki_upgrade_convert_instance($oldrecord); + $newid = $DB->insert_record('wiki', $newrecord); + // update instanceid in course_modules table + $DB->set_field('course_modules', 'instance', $newid, array('module' => $moduleid, 'instance' => $oldrecord->id)); + } + $oldwikis->close(); +} + +/** + * Migrating wiki pages history + */ +function wiki_upgrade_migrate_versions() { + global $DB, $CFG; + upgrade_set_timeout(); + require_once($CFG->dirroot.'/mod/wiki/db/migration/lib.php'); + $sql = 'SELECT po.id as oldpage_id, po.pagename as oldpage_pagename, po.version, po.flags, po.content, po.author, po.userid as oldpage_userid, po.created, po.lastmodified, po.refs, po.meta, po.hits, po.wiki, ' . + 'p.id as newpage_id, p.subwikiid, p.title, p.cachedcontent, p.timecreated, p.timemodified as newpage_timemodified, p.timerendered, p.userid as newpage_userid, p.pageviews, p.readonly, ' . + 'e.id as entry_id, e.wikiid, e.course as entrycourse, e.groupid, e.userid as entry_userid, e.pagename as entry_pagename, e.timemodified as entry_timemodified, ' . + // TODO: + // in head w.summary doesn't exist, should use w.intro + // w.summary in 1.9 + 'w.id as wiki_id, w.course as wiki_course, w.name, w.summary as summary, w.pagename as wiki_pagename, w.wtype, w.ewikiprinttitle, w.htmlmode, w.ewikiacceptbinary, w.disablecamelcase, w.setpageflags, w.strippages, w.removepages, w.revertchanges, w.initialcontent, w.timemodified as wiki_timemodified ' . + 'FROM {wiki_pages_old} po LEFT OUTER JOIN {wiki_entries_old} e ' . + 'ON e.id = po.wiki ' . + 'LEFT OUTER JOIN {wiki_old} w ' . + 'ON w.id = e.wikiid '. + 'LEFT OUTER JOIN {wiki_subwikis} s ' . + 'ON e.groupid = s.groupid AND e.wikiid = s.wikiid ' . + 'LEFT OUTER JOIN {wiki_pages} p ' . + 'ON po.pagename = p.title AND p.subwikiid = s.id'; + $pagesinfo = $DB->get_recordset_sql($sql, array()); + + while ($pagesinfo->valid()){ + $pageinfo = $pagesinfo->current(); + + $oldpage = new StdClass(); + $oldpage->id = $pageinfo->oldpage_id; + $oldpage->pagename = $pageinfo->oldpage_pagename; + $oldpage->version = $pageinfo->version; + $oldpage->flags = $pageinfo->flags; + $oldpage->content = $pageinfo->content; + $oldpage->author = $pageinfo->author; + $oldpage->userid = $pageinfo->oldpage_userid; + $oldpage->created = $pageinfo->created; + $oldpage->lastmodified = $pageinfo->lastmodified; + $oldpage->refs = $pageinfo->refs; + $oldpage->meta = $pageinfo->meta; + $oldpage->hits = $pageinfo->hits; + $oldpage->wiki = $pageinfo->wiki; + + $page = new StdClass(); + $page->id = $pageinfo->newpage_id; + $page->subwikiid = $pageinfo->subwikiid; + $page->title = $pageinfo->title; + $page->cachedcontent = $pageinfo->cachedcontent; + $page->timecreated = $pageinfo->timecreated; + $page->timemodified = $pageinfo->newpage_timemodified; + $page->timerendered = $pageinfo->timerendered; + $page->userid = $pageinfo->newpage_userid; + $page->pageviews = $pageinfo->pageviews; + $page->readonly = $pageinfo->readonly; + + $entry = new StdClass(); + $entry->id = $pageinfo->entry_id; + $entry->wikiid = $pageinfo->wikiid; + $entry->course = $pageinfo->entrycourse; + $entry->groupid = $pageinfo->groupid; + $entry->userid = $pageinfo->entry_userid; + $entry->pagename = $pageinfo->entry_pagename; + $entry->timemodified = $pageinfo->entry_timemodified; + + $wiki = new StdClass(); + $wiki->id = $pageinfo->wiki_id; + $wiki->course = $pageinfo->wiki_course; + $wiki->name = $pageinfo->name; + $wiki->summary = $pageinfo->summary; + $wiki->pagename = $pageinfo->wiki_pagename; + $wiki->wtype = $pageinfo->wtype; + $wiki->ewikiprinttitle = $pageinfo->ewikiprinttitle; + $wiki->htmlmode = $pageinfo->htmlmode; + $wiki->ewikiacceptbinary = $pageinfo->ewikiacceptbinary; + $wiki->disablecamelcase = $pageinfo->disablecamelcase; + $wiki->setpageflags = $pageinfo->setpageflags; + $wiki->strippages = $pageinfo->strippages; + $wiki->removepages = $pageinfo->removepages; + $wiki->revertchanges = $pageinfo->revertchanges; + $wiki->initialcontent = $pageinfo->initialcontent; + $wiki->timemodified = $pageinfo->wiki_timemodified; + + $version = new StdClass(); + $version->pageid = $page->id; + $version->content = wiki_ewiki_2_html($entry, $oldpage, $wiki); + $version->contentformat = "html"; + $version->version = $oldpage->version; + $version->timecreated = $oldpage->lastmodified; + $version->userid = $oldpage->userid; + $DB->insert_record('wiki_versions', $version); + + $pagesinfo->next(); + } + + $pagesinfo->close(); +}