"MDL-23823 allow users pick same file multi times in html editor"
[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() {
204 global $DB, $CFG;
205 upgrade_set_timeout();
206 require_once($CFG->dirroot . '/mod/wiki/db/migration/lib.php');
621f3ef5
JP
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
221 while ($pagesinfo->valid()) {
222 $pageinfo = $pagesinfo->current();
223
224 $oldpage = new StdClass();
225 $oldpage->id = $pageinfo->oldpage_id;
226 $oldpage->pagename = $pageinfo->oldpage_pagename;
227 $oldpage->version = $pageinfo->version;
228 $oldpage->flags = $pageinfo->flags;
229 $oldpage->content = $pageinfo->content;
230 $oldpage->author = $pageinfo->author;
231 $oldpage->userid = $pageinfo->oldpage_userid;
232 $oldpage->created = $pageinfo->created;
233 $oldpage->lastmodified = $pageinfo->lastmodified;
234 $oldpage->refs = $pageinfo->refs;
235 $oldpage->meta = $pageinfo->meta;
236 $oldpage->hits = $pageinfo->hits;
237 $oldpage->wiki = $pageinfo->wiki;
238
239 $page = new StdClass();
240 $page->id = $pageinfo->newpage_id;
241 $page->subwikiid = $pageinfo->subwikiid;
242 $page->title = $pageinfo->title;
243 $page->cachedcontent = $pageinfo->cachedcontent;
244 $page->timecreated = $pageinfo->timecreated;
245 $page->timemodified = $pageinfo->newpage_timemodified;
246 $page->timerendered = $pageinfo->timerendered;
247 $page->userid = $pageinfo->newpage_userid;
248 $page->pageviews = $pageinfo->pageviews;
249 $page->readonly = $pageinfo->readonly;
250
251 $entry = new StdClass();
252 $entry->id = $pageinfo->entry_id;
253 $entry->wikiid = $pageinfo->wikiid;
254 $entry->course = $pageinfo->entrycourse;
255 $entry->groupid = $pageinfo->groupid;
256 $entry->userid = $pageinfo->entry_userid;
257 $entry->pagename = $pageinfo->entry_pagename;
258 $entry->timemodified = $pageinfo->entry_timemodified;
259
260 $wiki = new StdClass();
261 $wiki->id = $pageinfo->wiki_id;
262 $wiki->course = $pageinfo->wiki_course;
263 $wiki->name = $pageinfo->name;
264 $wiki->summary = $pageinfo->summary;
265 $wiki->pagename = $pageinfo->wiki_pagename;
266 $wiki->wtype = $pageinfo->wtype;
267 $wiki->ewikiprinttitle = $pageinfo->ewikiprinttitle;
268 $wiki->htmlmode = $pageinfo->htmlmode;
269 $wiki->ewikiacceptbinary = $pageinfo->ewikiacceptbinary;
270 $wiki->disablecamelcase = $pageinfo->disablecamelcase;
271 $wiki->setpageflags = $pageinfo->setpageflags;
272 $wiki->strippages = $pageinfo->strippages;
273 $wiki->removepages = $pageinfo->removepages;
274 $wiki->revertchanges = $pageinfo->revertchanges;
275 $wiki->initialcontent = $pageinfo->initialcontent;
276 $wiki->timemodified = $pageinfo->wiki_timemodified;
277
278 $version = new StdClass();
279 $version->pageid = $page->id;
280 $version->content = wiki_ewiki_2_html($entry, $oldpage, $wiki);
281 $version->contentformat = "html";
282 $version->version = $oldpage->version;
283 $version->timecreated = $oldpage->lastmodified;
284 $version->userid = $oldpage->userid;
285 if ($version->version == 1) {
286 // The oldest version of page in moodle 2.0 is 0 which has empty content
287 // so we need to insert an extra record
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);
295 } else {
296 $DB->insert_record('wiki_versions', $version);
297 }
298
299 $pagesinfo->next();
300 }
301
302 $pagesinfo->close();
303}