forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrade.php
363 lines (298 loc) · 14.2 KB
/
upgrade.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file keeps track of upgrades to Moodle.
*
* Sometimes, changes between versions involve
* alterations to database structures and other
* major things that may break installations.
*
* The upgrade function in this file will attempt
* to perform all the necessary actions to upgrade
* your older installation to the current version.
*
* If there's something it cannot do itself, it
* will tell you what you need to do.
*
* The commands in here will all be database-neutral,
* using the methods of database_manager class
*
* Please do not forget to use upgrade_set_timeout()
* before any action that may take longer time to finish.
*
* @package core_install
* @category upgrade
* @copyright 2006 onwards Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Main upgrade tasks to be executed on Moodle version bump
*
* This function is automatically executed after one bump in the Moodle core
* version is detected. It's in charge of performing the required tasks
* to raise core from the previous version to the next one.
*
* It's a collection of ordered blocks of code, named "upgrade steps",
* each one performing one isolated (from the rest of steps) task. Usually
* tasks involve creating new DB objects or performing manipulation of the
* information for cleanup/fixup purposes.
*
* Each upgrade step has a fixed structure, that can be summarised as follows:
*
* if ($oldversion < XXXXXXXXXX.XX) {
* // Explanation of the update step, linking to issue in the Tracker if necessary
* upgrade_set_timeout(XX); // Optional for big tasks
* // Code to execute goes here, usually the XMLDB Editor will
* // help you here. See {@link http://docs.moodle.org/dev/XMLDB_editor}.
* upgrade_main_savepoint(true, XXXXXXXXXX.XX);
* }
*
* All plugins within Moodle (modules, blocks, reports...) support the existence of
* their own upgrade.php file, using the "Frankenstyle" component name as
* defined at {@link http://docs.moodle.org/dev/Frankenstyle}, for example:
* - {@link xmldb_page_upgrade($oldversion)}. (modules don't require the plugintype ("mod_") to be used.
* - {@link xmldb_auth_manual_upgrade($oldversion)}.
* - {@link xmldb_workshopform_accumulative_upgrade($oldversion)}.
* - ....
*
* In order to keep the contents of this file reduced, it's allowed to create some helper
* functions to be used here in the {@link upgradelib.php} file at the same directory. Note
* that such a file must be manually included from upgrade.php, and there are some restrictions
* about what can be used within it.
*
* For more information, take a look to the documentation available:
* - Data definition API: {@link http://docs.moodle.org/dev/Data_definition_API}
* - Upgrade API: {@link http://docs.moodle.org/dev/Upgrade_API}
*
* @param int $oldversion
* @return bool always true
*/
function xmldb_main_upgrade($oldversion) {
global $CFG, $USER, $DB, $OUTPUT;
require_once($CFG->libdir.'/db/upgradelib.php'); // Core Upgrade-related functions
$dbman = $DB->get_manager(); // loads ddl manager and xmldb classes
if ($oldversion < 2011120500) {
// just in case somebody hacks upgrade scripts or env, we really can not continue
echo("You need to upgrade to 2.2.x first!\n");
exit(1);
// Note this savepoint is 100% unreachable, but needed to pass the upgrade checks
upgrade_main_savepoint(true, 2011120500);
}
// Moodle v2.2.0 release upgrade line
// Put any upgrade step following this
if ($oldversion < 2011120500.02) {
upgrade_set_timeout(60*20); // This may take a while
// MDL-28180. Some missing restrictions in certain backup & restore operations
// were causing incorrect duplicates in the course_completion_aggr_methd table.
// This upgrade step takes rid of them.
$sql = 'SELECT course, criteriatype, MIN(id) AS minid
FROM {course_completion_aggr_methd}
GROUP BY course, criteriatype
HAVING COUNT(*) > 1';
$duprs = $DB->get_recordset_sql($sql);
foreach ($duprs as $duprec) {
// We need to handle NULLs in criteriatype diferently
if (is_null($duprec->criteriatype)) {
$where = 'course = ? AND criteriatype IS NULL AND id > ?';
$params = array($duprec->course, $duprec->minid);
} else {
$where = 'course = ? AND criteriatype = ? AND id > ?';
$params = array($duprec->course, $duprec->criteriatype, $duprec->minid);
}
$DB->delete_records_select('course_completion_aggr_methd', $where, $params);
}
$duprs->close();
// Main savepoint reached
upgrade_main_savepoint(true, 2011120500.02);
}
if ($oldversion < 2011120500.03) {
// Changing precision of field value on table user_preferences to (1333)
$table = new xmldb_table('user_preferences');
$field = new xmldb_field('value', XMLDB_TYPE_CHAR, '1333', null, XMLDB_NOTNULL, null, null, 'name');
// Launch change of precision for field value
$dbman->change_field_precision($table, $field);
// Main savepoint reached
upgrade_main_savepoint(true, 2011120500.03);
}
if ($oldversion < 2012020200.03) {
// Define index rolecontext (not unique) to be added to role_assignments
$table = new xmldb_table('role_assignments');
$index = new xmldb_index('rolecontext', XMLDB_INDEX_NOTUNIQUE, array('roleid', 'contextid'));
// Conditionally launch add index rolecontext
if (!$dbman->index_exists($table, $index)) {
$dbman->add_index($table, $index);
}
// Define index usercontextrole (not unique) to be added to role_assignments
$index = new xmldb_index('usercontextrole', XMLDB_INDEX_NOTUNIQUE, array('userid', 'contextid', 'roleid'));
// Conditionally launch add index usercontextrole
if (!$dbman->index_exists($table, $index)) {
$dbman->add_index($table, $index);
}
// Main savepoint reached
upgrade_main_savepoint(true, 2012020200.03);
}
if ($oldversion < 2012020200.06) {
// Previously we always allowed users to override their email address via the messaging system
// We have now added a setting to allow admins to turn this this ability on and off
// While this setting defaults to 0 (off) we're setting it to 1 (on) to maintain the behaviour for upgrading sites
set_config('messagingallowemailoverride', 1);
// Main savepoint reached
upgrade_main_savepoint(true, 2012020200.06);
}
if ($oldversion < 2012021700.01) {
// Changing precision of field uniquehash on table post to 255
$table = new xmldb_table('post');
$field = new xmldb_field('uniquehash', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'content');
// Launch change of precision for field uniquehash
$dbman->change_field_precision($table, $field);
// Main savepoint reached
upgrade_main_savepoint(true, 2012021700.01);
}
if ($oldversion < 2012021700.02) {
// Somewhere before 1.9 summary and content column in post table were not null. In 1.9+
// not null became false.
$columns = $DB->get_columns('post');
// Fix discrepancies in summary field after upgrade from 1.9
if (array_key_exists('summary', $columns) && $columns['summary']->not_null != false) {
$table = new xmldb_table('post');
$summaryfield = new xmldb_field('summary', XMLDB_TYPE_TEXT, 'big', null, null, null, null, 'subject');
if ($dbman->field_exists($table, $summaryfield)) {
$dbman->change_field_notnull($table, $summaryfield);
}
}
// Fix discrepancies in content field after upgrade from 1.9
if (array_key_exists('content', $columns) && $columns['content']->not_null != false) {
$table = new xmldb_table('post');
$contentfield = new xmldb_field('content', XMLDB_TYPE_TEXT, 'big', null, null, null, null, 'summary');
if ($dbman->field_exists($table, $contentfield)) {
$dbman->change_field_notnull($table, $contentfield);
}
}
upgrade_main_savepoint(true, 2012021700.02);
}
// The ability to backup user (private) files is out completely - MDL-29248
if ($oldversion < 2012030100.01) {
unset_config('backup_general_user_files', 'backup');
unset_config('backup_general_user_files_locked', 'backup');
unset_config('backup_auto_user_files', 'backup');
upgrade_main_savepoint(true, 2012030100.01);
}
if ($oldversion < 2012030100.02) {
// migrate all numbers to signed - it should be safe to interrupt this and continue later
upgrade_mysql_fix_unsigned_columns();
// Main savepoint reached
upgrade_main_savepoint(true, 2012030100.02);
}
if ($oldversion < 2012030900.01) {
// migrate all texts and binaries to big size - it should be safe to interrupt this and continue later
upgrade_mysql_fix_lob_columns();
// Main savepoint reached
upgrade_main_savepoint(true, 2012030900.01);
}
if ($oldversion < 2012031500.01) {
// Upgrade old course_allowed_modules data to be permission overrides.
if ($CFG->restrictmodulesfor === 'all') {
$courses = $DB->get_records_menu('course', array(), 'id', 'id, 1');
} else if ($CFG->restrictmodulesfor === 'requested') {
$courses = $DB->get_records_menu('course', array('retrictmodules' => 1), 'id', 'id, 1');
} else {
$courses = array();
}
if (!$dbman->table_exists('course_allowed_modules')) {
// Upgrade must already have been run on this server. This might happen,
// for example, during development of these changes.
$courses = array();
}
$modidtoname = $DB->get_records_menu('modules', array(), 'id', 'id, name');
$coursecount = count($courses);
if ($coursecount) {
$pbar = new progress_bar('allowedmods', 500, true);
$transaction = $DB->start_delegated_transaction();
}
$i = 0;
foreach ($courses as $courseid => $notused) {
$i += 1;
upgrade_set_timeout(60); // 1 minute per course should be fine.
$allowedmoduleids = $DB->get_records_menu('course_allowed_modules',
array('course' => $courseid), 'module', 'module, 1');
if (empty($allowedmoduleids)) {
// This seems to be the best match for backwards compatibility,
// not necessarily with the old code in course_allowed_module function,
// but with the code that used to be in the coures settings form.
$allowedmoduleids = explode(',', $CFG->defaultallowedmodules);
$allowedmoduleids = array_combine($allowedmoduleids, $allowedmoduleids);
}
$context = context_course::instance($courseid);
list($roleids) = get_roles_with_cap_in_context($context, 'moodle/course:manageactivities');
list($managerroleids) = get_roles_with_cap_in_context($context, 'moodle/site:config');
foreach ($managerroleids as $roleid) {
unset($roleids[$roleid]);
}
foreach ($modidtoname as $modid => $modname) {
if (isset($allowedmoduleids[$modid])) {
// Module is allowed, no worries.
continue;
}
$capability = 'mod/' . $modname . ':addinstance';
foreach ($roleids as $roleid) {
assign_capability($capability, CAP_PREVENT, $roleid, $context);
}
}
$pbar->update($i, $coursecount, "Upgrading legacy course_allowed_modules data - $i/$coursecount.");
}
if ($coursecount) {
$transaction->allow_commit();
}
upgrade_main_savepoint(true, 2012031500.01);
}
if ($oldversion < 2012031500.02) {
// Define field retrictmodules to be dropped from course
$table = new xmldb_table('course');
$field = new xmldb_field('restrictmodules');
// Conditionally launch drop field requested
if ($dbman->field_exists($table, $field)) {
$dbman->drop_field($table, $field);
}
upgrade_main_savepoint(true, 2012031500.02);
}
if ($oldversion < 2012031500.03) {
// Define table course_allowed_modules to be dropped
$table = new xmldb_table('course_allowed_modules');
// Conditionally launch drop table for course_allowed_modules
if ($dbman->table_exists($table)) {
$dbman->drop_table($table);
}
upgrade_main_savepoint(true, 2012031500.03);
}
if ($oldversion < 2012031500.04) {
// Clean up the old admin settings.
unset_config('restrictmodulesfor');
unset_config('restrictbydefault');
unset_config('defaultallowedmodules');
upgrade_main_savepoint(true, 2012031500.04);
}
if ($oldversion < 2012032300.02) {
// Migrate the old admin debug setting.
if ($CFG->debug == 38911) {
set_config('debug', DEBUG_DEVELOPER);
} else if ($CFG->debug == 6143) {
set_config('debug', DEBUG_ALL);
}
upgrade_main_savepoint(true, 2012032300.02);
}
return true;
}