Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / wiki / db / postgres7.php
blobffd05088ff7609ff7f9c0b6969338ee134443a7e
1 <?PHP
3 // THIS FILE IS DEPRECATED! PLEASE DO NOT MAKE CHANGES TO IT!
4 //
5 // IT IS USED ONLY FOR UPGRADES FROM BEFORE MOODLE 1.7, ALL
6 // LATER CHANGES SHOULD USE upgrade.php IN THIS DIRECTORY.
8 function wiki_upgrade($oldversion) {
9 /// This function does anything necessary to upgrade
10 /// older versions to match current functionality
12 global $CFG;
14 if ($oldversion < 2004073000) {
16 modify_database("", "ALTER TABLE prefix_wiki_pages DROP COLUMN id;");
17 modify_database("", "ALTER TABLE ONLY prefix_wiki_pages
18 ADD CONSTRAINT id PRIMARY KEY (pagename, \"version\");");
20 if ($oldversion < 2004073001) {
22 modify_database("", "ALTER TABLE prefix_wiki_pages DROP CONSTRAINT id;");
23 modify_database("", "ALTER TABLE ONLY prefix_wiki_pages
24 ADD CONSTRAINT id PRIMARY KEY (pagename, \"version\", wiki);");
26 if ($oldversion < 2004082200) {
27 table_column('wiki_pages', '', 'userid', "integer", "10", "unsigned", "0", "not null", "author");
29 if ($oldversion < 2004082303) { // Try to update userid for old records
30 if ($pages = get_records('wiki_pages', 'userid', 0, 'pagename', 'lastmodified,author,pagename,version')) {
31 foreach ($pages as $page) {
32 $name = explode('(', $page->author);
33 $name = trim($name[0]);
34 $name = explode(' ', $name);
35 $firstname = $name[0];
36 unset($name[0]);
37 $lastname = trim(implode(' ', $name));
38 if ($user = get_record('user', 'firstname', $firstname, 'lastname', $lastname)) {
39 set_field('wiki_pages', 'userid', $user->id, 'pagename', addslashes($page->pagename), 'version', $page->version);
45 if ($oldversion < 2004111200) {
46 execute_sql("DROP INDEX {$CFG->prefix}wiki_course_idx;",false);
47 execute_sql("DROP INDEX {$CFG->prefix}wiki_entries_wikiid_idx;",false);
48 execute_sql("DROP INDEX {$CFG->prefix}wiki_entries_userid_idx;",false);
49 execute_sql("DROP INDEX {$CFG->prefix}wiki_entries_groupid_idx;",false);
50 execute_sql("DROP INDEX {$CFG->prefix}wiki_entries_course_idx;",false);
51 execute_sql("DROP INDEX {$CFG->prefix}wiki_entries_pagename_idx;",false);
53 modify_database('','CREATE INDEX prefix_wiki_course_idx ON prefix_wiki (course);');
54 modify_database('','CREATE INDEX prefix_wiki_entries_wikiid_idx ON prefix_wiki_entries (wikiid);');
55 modify_database('','CREATE INDEX prefix_wiki_entries_userid_idx ON prefix_wiki_entries (userid);');
56 modify_database('','CREATE INDEX prefix_wiki_entries_groupid_idx ON prefix_wiki_entries (groupid);');
57 modify_database('','CREATE INDEX prefix_wiki_entries_course_idx ON prefix_wiki_entries (course);');
58 modify_database('','CREATE INDEX prefix_wiki_entries_pagename_idx ON prefix_wiki_entries (pagename);');
62 if ($oldversion < 2004112400) {
63 execute_sql("ALTER TABLE {$CFG->prefix}wiki_pages DROP CONSTRAINT id;",false);
64 execute_sql("ALTER TABLE {$CFG->prefix}wiki_pages DROP CONSTRAINT {$CFG->prefix}wiki_pages_id;",false);
65 execute_sql("ALTER TABLE {$CFG->prefix}wiki_pages DROP CONSTRAINT {$CFG->prefix}wiki_pages_pagename_version_wiki_unique;",false);
66 modify_database("", "ALTER TABLE ONLY prefix_wiki_pages
67 ADD CONSTRAINT prefix_wiki_pages_pagename_version_wiki_unique PRIMARY KEY (pagename, \"version\", wiki);");
70 if ($oldversion < 2005022000) {
71 // recreating the wiki_pages table completelly (missing id, bug 2608)
72 if ($rows = count_records("wiki_pages")) {
73 // we need to use the temp stuff
74 modify_database("","CREATE TABLE prefix_wiki_pages_tmp (
75 id SERIAL8 PRIMARY KEY,
76 pagename VARCHAR(160) NOT NULL,
77 version INTEGER NOT NULL DEFAULT 0,
78 flags INTEGER DEFAULT 0,
79 content TEXT,
80 author VARCHAR(100) DEFAULT 'ewiki',
81 userid INTEGER NOT NULL DEFAULT 0,
82 created INTEGER DEFAULT 0,
83 lastmodified INTEGER DEFAULT 0,
84 refs TEXT,
85 meta TEXT,
86 hits INTEGER DEFAULT 0,
87 wiki INT8 NOT NULL);");
89 execute_sql("INSERT INTO {$CFG->prefix}wiki_pages_tmp (pagename, version, flags, content,
90 author, userid, created, lastmodified,
91 refs, meta, hits, wiki)
92 SELECT pagename, version, flags, content,
93 author, userid, created, lastmodified,
94 refs, meta, hits, wiki
95 FROM {$CFG->prefix}wiki_pages");
97 $insertafter = true;
100 execute_sql("DROP TABLE {$CFG->prefix}wiki_pages");
102 modify_database("","CREATE TABLE prefix_wiki_pages (
103 id SERIAL8 PRIMARY KEY,
104 pagename VARCHAR(160) NOT NULL,
105 version INTEGER NOT NULL DEFAULT 0,
106 flags INTEGER DEFAULT 0,
107 content TEXT,
108 author VARCHAR(100) DEFAULT 'ewiki',
109 userid INTEGER NOT NULL DEFAULT 0,
110 created INTEGER DEFAULT 0,
111 lastmodified INTEGER DEFAULT 0,
112 refs TEXT,
113 meta TEXT,
114 hits INTEGER DEFAULT 0,
115 wiki INT8 NOT NULL);");
117 modify_database("","CREATE UNIQUE INDEX prefix_wiki_pages_pagename_version_wiki_uk
118 ON prefix_wiki_pages (pagename, version, wiki);");
120 if (!empty($insertafter)) {
121 execute_sql("INSERT INTO {$CFG->prefix}wiki_pages (pagename, version, flags, content,
122 author, userid, created, lastmodified,
123 refs, meta, hits, wiki)
124 SELECT pagename, version, flags, content,
125 author, userid, created, lastmodified,
126 refs, meta, hits, wiki
127 FROM {$CFG->prefix}wiki_pages_tmp");
129 execute_sql("DROP TABLE {$CFG->prefix}wiki_pages_tmp");
133 if ($oldversion < 2006032900) {
134 global $db;
135 table_column("wiki_pages",'','content_base64','text');
136 table_column("wiki_pages",'','refs_base64','text');
137 $olddebug = $db->debug;
138 $db->debug = false;
139 $data = $db->GetAll("SELECT id,content,refs FROM {$CFG->prefix}wiki_pages");
140 foreach ($data as $d) {
141 $db->AutoExecute("{$CFG->prefix}wiki_pages", array('refs_base64' => base64_encode($d['refs']), 'content_base64' => base64_encode($d['content'])), 'UPDATE', 'id = '.$d['id']);
143 $db->debug = $olddebug;
144 execute_sql("ALTER TABLE {$CFG->prefix}wiki_pages DROP COLUMN content");
145 execute_sql("ALTER TABLE {$CFG->prefix}wiki_pages DROP COLUMN refs");
146 table_column("wiki_pages",'','content','bytea');
147 table_column("wiki_pages",'','refs','bytea');
148 execute_sql("UPDATE {$CFG->prefix}wiki_pages SET refs = decode(refs_base64, 'base64'), content = decode(content_base64, 'base64')");
149 execute_sql("ALTER TABLE {$CFG->prefix}wiki_pages DROP COLUMN content_base64");
150 execute_sql("ALTER TABLE {$CFG->prefix}wiki_pages DROP COLUMN refs_base64");
153 if ($oldversion < 2006042801) {
154 modify_database('', 'ALTER TABLE prefix_wiki_pages
155 ALTER COLUMN content SET DEFAULT \'\'');
156 modify_database('', 'ALTER TABLE prefix_wiki_pages
157 ALTER COLUMN refs SET DEFAULT \'\'');
158 modify_database('', 'ALTER TABLE prefix_wiki_pages
159 ALTER COLUMN content DROP NOT NULL');
160 modify_database('', 'ALTER TABLE prefix_wiki_pages
161 ALTER COLUMN refs DROP NOT NULL');
164 if ($oldversion < 2006092502) {
165 modify_database("","
166 CREATE TABLE prefix_wiki_locks
168 id SERIAL PRIMARY KEY,
169 wikiid INTEGER NOT NULL,
170 pagename VARCHAR(160) NOT NULL DEFAULT '',
171 lockedby INTEGER NOT NULL DEFAULT 0,
172 lockedsince INTEGER NOT NULL DEFAULT 0,
173 lockedseen INTEGER NOT NULL DEFAULT 0
174 );");
175 modify_database("","CREATE INDEX prefix_wikilock_loc_ix ON prefix_wiki_locks (lockedseen);");
176 modify_database("","CREATE UNIQUE INDEX prefix_wikilock_wikpag_uix ON prefix_wiki_locks (wikiid, pagename);");
179 if($oldversion < 2006092602) {
181 // This used to be a BYTEA type for no apparent reason, which caused various queries to fail. The new
182 // install.xml uses TEXT so I figure it's safe to change it in upgrade too. This one broke the links page...
183 modify_database('',"ALTER TABLE prefix_wiki_pages ALTER COLUMN refs DROP DEFAULT;");
184 modify_database('',"ALTER TABLE prefix_wiki_pages ALTER COLUMN refs TYPE TEXT USING ENCODE(refs,'escape');");
185 modify_database('',"ALTER TABLE prefix_wiki_pages ALTER COLUMN refs SET DEFAULT '';");
186 // ...and this one broke the search page.
187 modify_database('',"ALTER TABLE prefix_wiki_pages ALTER COLUMN content DROP DEFAULT;");
188 modify_database('',"ALTER TABLE prefix_wiki_pages ALTER COLUMN content TYPE TEXT USING ENCODE(content,'escape');");
189 modify_database('',"ALTER TABLE prefix_wiki_pages ALTER COLUMN content SET DEFAULT '';");
191 // following code should be compatible with both pg 8.x and 7.4
192 table_column('wiki_pages', '', 'tempcontent', 'TEXT', '', '', '', '');
193 execute_sql("UPDATE {$CFG->prefix}wiki_pages SET tempcontent = ENCODE(content,'escape')");
194 execute_sql("ALTER TABLE {$CFG->prefix}wiki_pages DROP COLUMN content");
195 execute_sql("ALTER TABLE {$CFG->prefix}wiki_pages RENAME COLUMN tempcontent TO content");
197 table_column('wiki_pages', '', 'temprefs', 'TEXT', '', '', '', '');
198 execute_sql("UPDATE {$CFG->prefix}wiki_pages SET temprefs = ENCODE(refs,'escape')");
199 execute_sql("ALTER TABLE {$CFG->prefix}wiki_pages DROP COLUMN refs");
200 execute_sql("ALTER TABLE {$CFG->prefix}wiki_pages RENAME COLUMN temprefs TO refs");
204 ////// DO NOT ADD NEW THINGS HERE!! USE upgrade.php and the lib/ddllib.php functions.
206 return true;