contexts MDL-20211 changed fetch_context_capabilities() to use underscores instead...
[moodle.git] / mod / glossary / db / upgrade.php
CommitLineData
86342d63 1<?php
ecfeb03a 2
cbc2b5df 3// This file keeps track of upgrades to
ecfeb03a 4// the glossary module
5//
6// Sometimes, changes between versions involve
7// alterations to database structures and other
8// major things that may break installations.
9//
10// The upgrade function in this file will attempt
11// to perform all the necessary actions to upgrade
12// your older installtion to the current version.
13//
14// If there's something it cannot do itself, it
15// will tell you what you need to do.
16//
17// The commands in here will all be database-neutral,
b1f93b15 18// using the methods of database_manager class
775f811a 19//
20// Please do not forget to use upgrade_set_timeout()
21// before any action that may take longer time to finish.
ecfeb03a 22
775f811a 23function xmldb_glossary_upgrade($oldversion) {
ee9e63b5 24 global $CFG, $DB, $OUTPUT;
ecfeb03a 25
775f811a 26 $dbman = $DB->get_manager();
ecfeb03a 27 $result = true;
28
219f652b 29//===== 1.9.0 upgrade line ======//
ecfeb03a 30
6b04ddc5 31 if ($result && $oldversion < 2008081900) {
49bcd737 32
33 /////////////////////////////////////
34 /// new file storage upgrade code ///
35 /////////////////////////////////////
36
37 $fs = get_file_storage();
38
39 $empty = $DB->sql_empty(); // silly oracle empty string handling workaround
40
41 $sqlfrom = "FROM {glossary_entries} ge
42 JOIN {glossary} g ON g.id = ge.glossaryid
43 JOIN {modules} m ON m.name = 'glossary'
44 JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = g.id)
c48068f8 45 WHERE ge.attachment <> '$empty' AND ge.attachment <> '1'";
49bcd737 46
47 $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");
48
c48068f8 49 if ($rs = $DB->get_recordset_sql("SELECT ge.id, ge.userid, ge.attachment, ge.glossaryid, ge.sourceglossaryid, g.course, cm.id AS cmid $sqlfrom ORDER BY g.course, g.id")) {
49bcd737 50
51 $pbar = new progress_bar('migrateglossaryfiles', 500, true);
52
49bcd737 53 $i = 0;
54 foreach ($rs as $entry) {
55 $i++;
56 upgrade_set_timeout(60); // set up timeout, may also abort execution
57 $pbar->update($i, $count, "Migrating glossary entries - $i/$count.");
58
59 $filepath = "$CFG->dataroot/$entry->course/$CFG->moddata/glossary/$entry->glossaryid/$entry->id/$entry->attachment";
0d18a9d6 60 if ($entry->sourceglossaryid and !is_readable($filepath)) {
49bcd737 61 //eh - try the second possible location
49bcd737 62 $filepath = "$CFG->dataroot/$entry->course/$CFG->moddata/glossary/$entry->sourceglossaryid/$entry->id/$entry->attachment";
cbc2b5df 63
49bcd737 64 }
65 if (!is_readable($filepath)) {
66 //file missing??
ee9e63b5 67 echo $OUTPUT->notification("File not readable, skipping: $filepath");
49bcd737 68 $entry->attachment = '';
69 $DB->update_record('glossary_entries', $entry);
70 continue;
71 }
72 $context = get_context_instance(CONTEXT_MODULE, $entry->cmid);
73
74 $filearea = 'glossary_attachment';
75 $filename = clean_param($entry->attachment, PARAM_FILE);
76 if ($filename === '') {
ee9e63b5 77 echo $OUTPUT->notification("Unsupported entry filename, skipping: ".$filepath);
49bcd737 78 $entry->attachment = '';
79 $DB->update_record('glossary_entries', $entry);
80 continue;
81 }
82 if (!$fs->file_exists($context->id, $filearea, $entry->id, '/', $filename)) {
83 $file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>$entry->id, 'filepath'=>'/', 'filename'=>$filename, 'userid'=>$entry->userid);
84 if ($fs->create_file_from_pathname($file_record, $filepath)) {
85 $entry->attachment = '1';
86 if ($DB->update_record('glossary_entries', $entry)) {
87 unlink($filepath);
88 }
89 }
90 }
91
92 // remove dirs if empty
93 @rmdir("$CFG->dataroot/$entry->course/$CFG->moddata/glossary/$entry->glossaryid/$entry->id");
94 @rmdir("$CFG->dataroot/$entry->course/$CFG->moddata/glossary/$entry->glossaryid");
95 @rmdir("$CFG->dataroot/$entry->course/$CFG->moddata/glossary");
96 }
49bcd737 97 $rs->close();
98 }
99
6b04ddc5 100 upgrade_mod_savepoint($result, 2008081900, 'glossary');
49bcd737 101 }
102
cbc2b5df 103 if ($result && $oldversion < 2009042000) {
104
2a88f626 105 /// Rename field definitionformat on table glossary_entries to definitionformat
cbc2b5df 106 $table = new xmldb_table('glossary_entries');
2a88f626 107 $field = new xmldb_field('format', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'definition');
cbc2b5df 108
109 /// Launch rename field definitionformat
110 $dbman->rename_field($table, $field, 'definitionformat');
111
112 /// glossary savepoint reached
113 upgrade_mod_savepoint($result, 2009042000, 'glossary');
114 }
115
116 if ($result && $oldversion < 2009042001) {
117
118 /// Define field definitiontrust to be added to glossary_entries
119 $table = new xmldb_table('glossary_entries');
2a88f626 120 $field = new xmldb_field('definitiontrust', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'definitionformat');
cbc2b5df 121
122 /// Launch add field definitiontrust
123 $dbman->add_field($table, $field);
124
125 /// glossary savepoint reached
126 upgrade_mod_savepoint($result, 2009042001, 'glossary');
127 }
128
129 if ($result && $oldversion < 2009042002) {
130
131 /// Rename field format on table glossary_comments to NEWNAMEGOESHERE
132 $table = new xmldb_table('glossary_comments');
2a88f626 133 $field = new xmldb_field('format', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'entrycomment');
cbc2b5df 134
135 /// Launch rename field format
136 $dbman->rename_field($table, $field, 'entrycommentformat');
137
138 /// glossary savepoint reached
139 upgrade_mod_savepoint($result, 2009042002, 'glossary');
140 }
141
142 if ($result && $oldversion < 2009042003) {
143
144 /// Define field entrycommenttrust to be added to glossary_comments
145 $table = new xmldb_table('glossary_comments');
2a88f626 146 $field = new xmldb_field('entrycommenttrust', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'entrycommentformat');
cbc2b5df 147
148 /// Conditionally launch add field entrycommenttrust
149 if (!$dbman->field_exists($table, $field)) {
150 $dbman->add_field($table, $field);
151 }
152
153 /// glossary savepoint reached
154 upgrade_mod_savepoint($result, 2009042003, 'glossary');
155 }
156
157 if ($result && $oldversion < 2009042004) {
158 $trustmark = '#####TRUSTTEXT#####';
159 $rs = $DB->get_recordset_sql("SELECT * FROM {glossary_entries} WHERE definition LIKE '$trustmark%'");
160 foreach ($rs as $entry) {
161 if (strpos($entry->definition, $trustmark) !== 0) {
162 // probably lowercase in some DBs
163 continue;
164 }
165 $entry->definition = trusttext_strip($entry->definition);
166 $entry->definitiontrust = 1;
167 $DB->update_record('glossary_entries', $entry);
168 }
169 $rs->close();
170
171 /// glossary savepoint reached
172 upgrade_mod_savepoint($result, 2009042004, 'glossary');
173 }
174
175 if ($result && $oldversion < 2009042005) {
176 $trustmark = '#####TRUSTTEXT#####';
177 $rs = $DB->get_recordset_sql("SELECT * FROM {glossary_comments} WHERE entrycomment LIKE '$trustmark%'");
178 foreach ($rs as $comment) {
179 if (strpos($comment->entrycomment, $trustmark) !== 0) {
180 // probably lowercase in some DBs
181 continue;
182 }
183 $comment->entrycomment = trusttext_strip($comment->entrycomment);
184 $comment->entrycommenttrust = 1;
185 $DB->update_record('glossary_comments', $comment);
186 }
187 $rs->close();
188
189 /// glossary savepoint reached
190 upgrade_mod_savepoint($result, 2009042005, 'glossary');
191 }
192
6507d1a9 193 if ($result && $oldversion < 2009042006) {
194
195 /// Define field introformat to be added to glossary
196 $table = new xmldb_table('glossary');
2a88f626 197 $field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
6507d1a9 198
199 /// Conditionally launch add field introformat
200 if (!$dbman->field_exists($table, $field)) {
201 $dbman->add_field($table, $field);
202 }
203
204 /// glossary savepoint reached
205 upgrade_mod_savepoint($result, 2009042006, 'glossary');
206 }
c8092ea5
DC
207 if ($result && $oldversion < 2009110800) {
208 require_once($CFG->libdir . '/commentlib.php');
03221650 209
c8092ea5
DC
210 /// Define table glossary_comments to be dropped
211 $table = new xmldb_table('glossary_comments');
212
213 /// Conditionally launch drop table for glossary_comments
214 if ($dbman->table_exists($table)) {
215 $sql = 'SELECT e.glossaryid AS glossaryid,
216 g.course AS courseid,
217 e.id AS itemid,
218 c.id AS old_id,
219 c.entrycomment AS comment,
220 c.entrycommentformat AS format,
221 c.entrycommenttrust AS trust,
222 c.timemodified AS timemodified
223 FROM {glossary_comments} c, {glossary_entries} e, {glossary} g
224 WHERE c.entryid=e.id AND e.glossaryid=g.id';
225 /// move glossary comments to new comments table
226 $error = false;
227 if ($rs = $DB->get_recordset_sql($sql)) {
228 foreach($rs as $res) {
229 if ($cm = get_coursemodule_from_instance('glossary', $res->glossaryid, $res->courseid)) {
230 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
231 $cmt = new stdclass;
232 $cmt->contextid = $context->id;
233 $cmt->courseid = $res->courseid;
234 $cmt->area = 'glossary_entry';
235 $cmt->itemid = $res->itemid;
236 $comment = new comment($cmt);
237 try {
238 $cmt = $comment->add($res->comment, $res->format);
239 } catch (comment_exception $e) {
240 add_to_log($res->courseid, 'comments', 'add', '', 'Cannot migrate glossary comment with ID# '.$res->old_id);
241 $error = true;
242 }
243 }
244 }
245 }
246 if (empty($error)) {
247 $dbman->drop_table($table);
248 } else {
249 print_error('error');
250 }
251 }
252
253 /// glossary savepoint reached
254 upgrade_mod_savepoint($result, 2009110800, 'glossary');
255 }
6507d1a9 256
ecfeb03a 257 return $result;
258}
259
86342d63 260