MDL-25501 reverting PULL-95
[moodle.git] / mod / wiki / db / upgradelib.php
CommitLineData
00710f4c
DC
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
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.
9//
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.
14//
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 * @package mod-wiki
20 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
21 */
22
23function wiki_add_wiki_fields() {
24 global $DB;
25
26 upgrade_set_timeout();
27 $dbman = $DB->get_manager();
28 /// Define table wiki to be created
29 $table = new xmldb_table('wiki');
30
31 // Adding fields to wiki table
32 $wikitable = new xmldb_table('wiki');
33
34 // in MOODLE_20_SABLE branch, summary field is renamed as intro
35 // so we renamed it back to summary to keep upgrade going as moodle 1.9
36 $field = new xmldb_field('intro', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null);
37 if ($dbman->field_exists($wikitable, $field)) {
38 $dbman->rename_field($wikitable, $field, 'summary');
39 }
40 $dbman->add_field($wikitable, $field);
41
42 $field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null);
43 if (!$dbman->field_exists($wikitable, $field)) {
44 $dbman->add_field($wikitable, $field);
45 }
46
47 $field = new xmldb_field('firstpagetitle', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, 'First Page', null);
48 $dbman->add_field($wikitable, $field);
49
50 $field = new xmldb_field('wikimode', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'collaborative', null);
51 $dbman->add_field($wikitable, $field);
52
53 $field = new xmldb_field('defaultformat', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'creole', null);
54 $dbman->add_field($wikitable, $field);
55
56 $field = new xmldb_field('forceformat', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1', null);
57 $dbman->add_field($wikitable, $field);
58
59 $field = new xmldb_field('scaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null);
60 $dbman->add_field($wikitable, $field);
61
62 $field = new xmldb_field('editbegin', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null);
63 $dbman->add_field($wikitable, $field);
64
65 $field = new xmldb_field('editend', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0', null);
66 $dbman->add_field($wikitable, $field);
67
68}
69
70/**
71 * Install wiki 2.0 tables
72 */
73function wiki_upgrade_install_20_tables() {
74 global $DB;
75 upgrade_set_timeout();
76 $dbman = $DB->get_manager();
77
78 /// Define table wiki_subwikis to be created
79 $table = new xmldb_table('wiki_subwikis');
80
81 /// Adding fields to table wiki_subwikis
82 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
83 $table->add_field('wikiid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
84 $table->add_field('groupid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
85 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
86
87 /// Adding keys to table wiki_subwikis
88 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
89 $table->add_key('wikiidgroupiduserid', XMLDB_KEY_UNIQUE, array('wikiid', 'groupid', 'userid'));
90 $table->add_key('wikifk', XMLDB_KEY_FOREIGN, array('wikiid'), 'wiki', array('id'));
91
92 /// Conditionally launch create table for wiki_subwikis
93 if (!$dbman->table_exists($table)) {
94 $dbman->create_table($table);
95 }
96
97 /// Define table wiki_pages to be created
98 $table = new xmldb_table('wiki_pages');
99
100 /// Adding fields to table wiki_pages
101 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
102 $table->add_field('subwikiid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
103 $table->add_field('title', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, 'title');
104 $table->add_field('cachedcontent', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null);
105 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
106 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
107 $table->add_field('timerendered', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
108 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
109 $table->add_field('pageviews', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
110 $table->add_field('readonly', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
111
112 /// Adding keys to table wiki_pages
113 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
114 $table->add_key('subwikititleuser', XMLDB_KEY_UNIQUE, array('subwikiid', 'title', 'userid'));
115 $table->add_key('subwikifk', XMLDB_KEY_FOREIGN, array('subwikiid'), 'wiki_subwiki', array('id'));
116
117 /// Conditionally launch create table for wiki_pages
118 if (!$dbman->table_exists($table)) {
119 $dbman->create_table($table);
120 }
121
122 /// Define table wiki_versions to be created
123 $table = new xmldb_table('wiki_versions');
124
125 /// Adding fields to table wiki_versions
126 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
127 $table->add_field('pageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
128 $table->add_field('content', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null);
129 $table->add_field('contentformat', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'creole');
130 $table->add_field('version', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
131 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
132 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
133
134 /// Adding keys to table wiki_versions
135 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
136 $table->add_key('pagefk', XMLDB_KEY_FOREIGN, array('pageid'), 'wiki_pages', array('id'));
137
138 /// Conditionally launch create table for wiki_versions
139 if (!$dbman->table_exists($table)) {
140 $dbman->create_table($table);
141 }
142
143 /// Define table wiki_synonyms to be created
144 $table = new xmldb_table('wiki_synonyms');
145
146 /// Adding fields to table wiki_synonyms
147 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
148 $table->add_field('subwikiid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
149 $table->add_field('pageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
150 $table->add_field('pagesynonym', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, 'Pagesynonym');
151
152 /// Adding keys to table wiki_synonyms
153 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
154 $table->add_key('pageidsyn', XMLDB_KEY_UNIQUE, array('pageid', 'pagesynonym'));
155
156 /// Conditionally launch create table for wiki_synonyms
157 if (!$dbman->table_exists($table)) {
158 $dbman->create_table($table);
159 }
160
161 /// Define table wiki_links to be created
162 $table = new xmldb_table('wiki_links');
163
164 /// Adding fields to table wiki_links
165 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
166 $table->add_field('subwikiid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
167 $table->add_field('frompageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
168 $table->add_field('topageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
169 $table->add_field('tomissingpage', XMLDB_TYPE_CHAR, '255', null, null, null, null);
170
171 /// Adding keys to table wiki_links
172 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
173 $table->add_key('frompageidfk', XMLDB_KEY_FOREIGN, array('frompageid'), 'wiki_pages', array('id'));
174 $table->add_key('subwikifk', XMLDB_KEY_FOREIGN, array('subwikiid'), 'wiki_subwiki', array('id'));
175
176 /// Conditionally launch create table for wiki_links
177 if (!$dbman->table_exists($table)) {
178 $dbman->create_table($table);
179 }
180
181 /// Define table wiki_locks to be created
182 $table = new xmldb_table('wiki_locks');
183
184 /// Adding fields to table wiki_locks
185 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
186 $table->add_field('pageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
187 $table->add_field('sectionname', XMLDB_TYPE_CHAR, '255', null, null, null, null);
188 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
189 $table->add_field('lockedat', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
190
191 /// Adding keys to table wiki_locks
192 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
193
194 /// Conditionally launch create table for wiki_locks
195 if (!$dbman->table_exists($table)) {
196 $dbman->create_table($table);
197 }
198}
199
200/**
201 * Migrating wiki pages history
202 */
203function wiki_upgrade_migrate_versions() {
da897079 204 global $DB, $CFG, $OUTPUT;
90c42e0f 205 upgrade_set_timeout();
00710f4c 206 require_once($CFG->dirroot . '/mod/wiki/db/migration/lib.php');
90c42e0f
PS
207 $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,
208 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,
209 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,
210 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
211 FROM {wiki_pages_old} po LEFT OUTER JOIN {wiki_entries_old} e
212 ON e.id = po.wiki
213 LEFT OUTER JOIN {wiki} w
214 ON w.id = e.wikiid
215 LEFT OUTER JOIN {wiki_subwikis} s
216 ON e.groupid = s.groupid AND e.wikiid = s.wikiid AND e.userid = s.userid
217 LEFT OUTER JOIN {wiki_pages} p
218 ON po.pagename = p.title AND p.subwikiid = s.id";
00710f4c
DC
219 $pagesinfo = $DB->get_recordset_sql($sql, array());
220
77b8c5ca 221 foreach ($pagesinfo as $pageinfo) {
00710f4c
DC
222
223 $oldpage = new StdClass();
224 $oldpage->id = $pageinfo->oldpage_id;
225 $oldpage->pagename = $pageinfo->oldpage_pagename;
226 $oldpage->version = $pageinfo->version;
227 $oldpage->flags = $pageinfo->flags;
228 $oldpage->content = $pageinfo->content;
229 $oldpage->author = $pageinfo->author;
230 $oldpage->userid = $pageinfo->oldpage_userid;
231 $oldpage->created = $pageinfo->created;
232 $oldpage->lastmodified = $pageinfo->lastmodified;
233 $oldpage->refs = $pageinfo->refs;
234 $oldpage->meta = $pageinfo->meta;
235 $oldpage->hits = $pageinfo->hits;
236 $oldpage->wiki = $pageinfo->wiki;
237
238 $page = new StdClass();
239 $page->id = $pageinfo->newpage_id;
240 $page->subwikiid = $pageinfo->subwikiid;
241 $page->title = $pageinfo->title;
242 $page->cachedcontent = $pageinfo->cachedcontent;
243 $page->timecreated = $pageinfo->timecreated;
244 $page->timemodified = $pageinfo->newpage_timemodified;
245 $page->timerendered = $pageinfo->timerendered;
246 $page->userid = $pageinfo->newpage_userid;
247 $page->pageviews = $pageinfo->pageviews;
248 $page->readonly = $pageinfo->readonly;
249
250 $entry = new StdClass();
251 $entry->id = $pageinfo->entry_id;
252 $entry->wikiid = $pageinfo->wikiid;
253 $entry->course = $pageinfo->entrycourse;
254 $entry->groupid = $pageinfo->groupid;
255 $entry->userid = $pageinfo->entry_userid;
256 $entry->pagename = $pageinfo->entry_pagename;
257 $entry->timemodified = $pageinfo->entry_timemodified;
258
259 $wiki = new StdClass();
260 $wiki->id = $pageinfo->wiki_id;
261 $wiki->course = $pageinfo->wiki_course;
262 $wiki->name = $pageinfo->name;
263 $wiki->summary = $pageinfo->summary;
264 $wiki->pagename = $pageinfo->wiki_pagename;
265 $wiki->wtype = $pageinfo->wtype;
266 $wiki->ewikiprinttitle = $pageinfo->ewikiprinttitle;
267 $wiki->htmlmode = $pageinfo->htmlmode;
268 $wiki->ewikiacceptbinary = $pageinfo->ewikiacceptbinary;
269 $wiki->disablecamelcase = $pageinfo->disablecamelcase;
270 $wiki->setpageflags = $pageinfo->setpageflags;
271 $wiki->strippages = $pageinfo->strippages;
272 $wiki->removepages = $pageinfo->removepages;
273 $wiki->revertchanges = $pageinfo->revertchanges;
274 $wiki->initialcontent = $pageinfo->initialcontent;
275 $wiki->timemodified = $pageinfo->wiki_timemodified;
276
277 $version = new StdClass();
278 $version->pageid = $page->id;
279 $version->content = wiki_ewiki_2_html($entry, $oldpage, $wiki);
90c42e0f 280 $version->contentformat = "html";
00710f4c
DC
281 $version->version = $oldpage->version;
282 $version->timecreated = $oldpage->lastmodified;
283 $version->userid = $oldpage->userid;
284 if ($version->version == 1) {
285 // The oldest version of page in moodle 2.0 is 0 which has empty content
286 // so we need to insert an extra record
7feb91ad
DC
287 try {
288 $content = $version->content;
289 $version->version = 0;
290 $version->content = '';
291 $DB->insert_record('wiki_versions', $version);
292 $version->version = 1;
293 $version->content = $content;
294 $DB->insert_record('wiki_versions', $version);
90c42e0f
PS
295 } catch (Exception $e) {
296 echo $OUTPUT->notification('Cannot insert this record, page id: ' . $page->id);
7feb91ad 297 }
00710f4c 298 } else {
7feb91ad
DC
299 try {
300 $DB->insert_record('wiki_versions', $version);
90c42e0f
PS
301 } catch (Exception $e) {
302 echo $OUTPUT->notification('Cannot insert this record, page id: ' . $page->id);
7feb91ad 303 }
00710f4c 304 }
00710f4c
DC
305 }
306
307 $pagesinfo->close();
308}