upgrade.php 41.7 KB
Newer Older
1
2
3
4
<?php
/**
 *
 * @package    mahara
Penny Leach's avatar
Penny Leach committed
5
 * @subpackage core
6
 * @author     Catalyst IT Ltd
7
8
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
9
10
11
12
13
14
 *
 */

defined('INTERNAL') || die();

function xmldb_core_upgrade($oldversion=0) {
15
    global $SESSION;
16
    raise_time_limit(120);
17
    raise_memory_limit('256M');
18
19
20

    $status = true;

21
22
23
24
    /**
     * Only have the upgrade steps after the $version->minupgradeversion value
     * If you are needing to see the older upgrade code look at an older version of Mahara
     */
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    if ($oldversion < 2017051100) {
        if ($records = get_records_sql_assoc("SELECT aa.annotation, va.view
                                              FROM {artefact_annotation} aa
                                              JOIN {view_artefact} va ON va.artefact = aa.annotation
                                              WHERE aa.view != va.view", array())) {
            log_debug('Fix artefact_annotation table data for copied collections');
            foreach ($records as $record) {
                $data = (object)array(
                    'annotation'    => $record->annotation,
                    'view'          => $record->view,
                );
                update_record('artefact_annotation', $data, 'annotation');
            }
        }
    }

41
    if ($oldversion < 2017052300) {
42
43
44
45
46
47
48
        // These are the records with passwords in the data.
        if ($records = get_records_sql_array("SELECT event, data, time
                                          FROM {event_log}
                                          WHERE event = ?
                                          AND POSITION(',\"password\":\"\",' IN data) = 0
                                         ", array('createuser'))
        ) {
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
            log_debug('Remove sensitive data from event_log');
            $count = 0;
            $limit = 1000;
            $total = count($records);
            foreach ($records as $record) {
                $where = clone $record;
                $data = json_decode($record->data);
                if (isset($data->password)) {
                    unset($data->password);
                    $cleandata = json_encode($data);
                    $record->data = $cleandata;
                    update_record('event_log', $record, $where);
                    set_field('usr', 'passwordchange', 1, 'username', $data->username);
                }
                $count++;
                if (($count % $limit) == 0 || $count == $total) {
                    log_debug("$count/$total");
                    set_time_limit(30);
                }
            }
        }
70
71
72
73
74
75
76
77
78
79
80
81
        // These are the records with empty passwords in the data.
        // No need for them to reset the password.
        $wheresql = " WHERE event = ?
            AND POSITION(',\"password\":\"\",' IN data) > 0";
        $sql_count = "SELECT COUNT(*)
                  FROM {event_log}" . $wheresql;
        if ($count = get_field_sql($sql_count, array('createuser'))) {
            $sql = "UPDATE {event_log}
                SET data = REPLACE(data, ',\"password\":\"\"', '')" . $wheresql;
            execute_sql($sql, array('createuser'));
            log_debug("$count records also cleaned up");
        }
82
83
    }

84
85
86
87
88
89
90
91
92
93
    if ($oldversion < 2017061200) {
        log_debug('Add new logoxs column in institution table for small logos');
        $table = new XMLDBTable('institution');
        $field = new XMLDBField('logoxs');
        if (!field_exists($table, $field)) {
            $field->setAttributes(XMLDB_TYPE_INTEGER, 10);
            add_field($table, $field);
        }
    }

94
95
96
97
98
99
100
101
    if ($oldversion < 2017062000) {
        if ($records = get_records_sql_array("SELECT bi.id AS blockid
                                              FROM {group} g
                                              JOIN {view} v ON (v.group = g.id AND v.type = 'grouphomepage')
                                              JOIN {block_instance} bi ON (bi.view = v.id AND bi.blocktype = 'groupviews')
                                              WHERE g.submittableto = 1
                                              AND bi.configdata NOT LIKE '%showsubmitted%'", array())) {
            safe_require('blocktype', 'groupviews');
102
            drop_elasticsearch_triggers();
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
            log_debug('Update submittable groups to display submissions in block by default');
            // We can only update those blocks where a decision hasn't yet been made rather than include blocks
            // that have showsubmitted set to false as that may be a valid choice by the group administrator.
            $count = 0;
            $limit = 1000;
            $total = count($records);
            foreach ($records as $record) {
                $bi = new BlockInstance($record->blockid);
                $configdata = $bi->get('configdata');
                $configdata['showsubmitted'] = 1;
                $bi->set('configdata', $configdata);
                $bi->commit();
                $count++;
                if (($count % $limit) == 0 || $count == $total) {
                    log_debug("$count/$total");
                    set_time_limit(30);
                }
            }
121
            create_elasticsearch_triggers();
122
123
124
        }
    }

125
126
127
128
129
130
131
132
133
134
135
136
    if ($oldversion < 2017071100) {
        log_debug('Fix up missing creation time for users');
        // Guess their creation time from their earliest view
        if ($results = get_records_sql_array("SELECT u.id, (
                SELECT MIN(v.ctime) FROM {view} v WHERE v.owner = u.id)
           AS starttime FROM {usr} u WHERE u.deleted = 0 AND u.ctime IS NULL AND u.id != 0")) {
            foreach ($results as $result) {
                execute_sql("UPDATE {usr} SET ctime = ? WHERE id = ?", array($result->starttime, $result->id));
            }
        }
    }

137
138
139
140
141
142
143
144
145
146
147
148
    if ($oldversion < 2017082900) {
        log_debug('Fix missing property in group Forums');
        $sql = "INSERT INTO {interaction_forum_instance_config} (forum,field,value)
            SELECT DISTINCT forum, 'createtopicusers', 'members'
            FROM {interaction_forum_instance_config}
            WHERE forum NOT IN (
                SELECT DISTINCT forum FROM {interaction_forum_instance_config}
                WHERE field = 'createtopicusers'
            )";
        execute_sql($sql);
    }

149
150
151
152
153
154
155
156
157
158
159
    if ($oldversion < 2017090400) {
        if ($data = get_config_plugin('blocktype', 'internalmedia', 'enabledtypes')) {
            $types = unserialize($data);
            if (($key = array_search('swf', $types)) !== false) {
                unset($types[$key]);
                $typestr = serialize($types);
                set_config_plugin('blocktype', 'internalmedia', 'enabledtypes', $typestr);
            }
        }
    }

160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
    if ($oldversion < 2017090800) {
        log_debug('Clear menu cache for removal of menu items');
        clear_menu_cache();
    }

    if ($oldversion < 2017090800) {
        log_debug('Add new fields to "event_log" table');
        // Instead of recording event resource id information in the 'data' json blob
        // we will add it to it's own columns for easier searching / faster retrieval
        // We will record if necessary the resourcetype/resourceid (and parenttype/parentid if necessary)
        // And for elasticsearch we will need to add an id column to the table and change 'time' to 'ctime'.

        $table = new XMLDBTable('event_log');
        $field = new XMLDBField('id');
        if (!field_exists($table, $field)) {
175
            log_debug('Adding id column');
176
177
178
179
180
            if (is_mysql()) {
                // MySQL requires the auto-increment column to be a primary key right away.
                execute_sql('ALTER TABLE {event_log} ADD id BIGINT(10) NOT NULL auto_increment PRIMARY KEY FIRST');
            }
            else {
181
182
                // This will auto-populate the id column without having to create a temp table.
                execute_sql('ALTER TABLE {event_log} ADD COLUMN id BIGSERIAL PRIMARY KEY');
183
            }
184

185
            // Add 'ctime' field and drop 'time' field
186
            log_debug('Adding ctime column');
187
            $field = new XMLDBField('ctime');
188
            $field->setAttributes(XMLDB_TYPE_DATETIME, null, null);
189
            add_field($table, $field);
190

191
            $sql = "UPDATE {event_log} SET ctime = time";
192
193
194
195
196
197
198
            execute_sql($sql);

            // now set ctime attribute to NOT NULL.
            $field = new XMLDBField('ctime');
            $field->setAttributes(XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL);
            change_field_notnull($table, $field);

199
200
201
            $field = new XMLDBField('time');
            drop_field($table, $field);

202
203
204
205
            log_debug('Adding resourceid column');
            $field = new XMLDBField('resourceid');
            $field->setAttributes(XMLDB_TYPE_INTEGER, 10);
            add_field($table, $field);
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
            log_debug('Adding resourcetype column');
            $field = new XMLDBField('resourcetype');
            $field->setAttributes(XMLDB_TYPE_CHAR, 255);
            add_field($table, $field);

            log_debug('Adding parentresourceid column');
            $field = new XMLDBField('parentresourceid');
            $field->setAttributes(XMLDB_TYPE_INTEGER, 10);
            add_field($table, $field);

            log_debug('Adding parentresourcetype column');
            $field = new XMLDBField('parentresourcetype');
            $field->setAttributes(XMLDB_TYPE_CHAR, 255);
            add_field($table, $field);

            log_debug('Adding ownerid column');
            $field = new XMLDBField('ownerid');
            $field->setAttributes(XMLDB_TYPE_INTEGER, 10);
            add_field($table, $field);

            log_debug('Adding ownertype column');
            $field = new XMLDBField('ownertype');
            $field->setAttributes(XMLDB_TYPE_CHAR, 255);
            add_field($table, $field);
231
232
        }

233
234
        log_debug('Adjust existing "event_log" data to fit new table structure');
        $db_version = get_db_version();
235
        if (is_mysql() && mysql_get_type() == 'mysql' && version_compare($db_version, '5.7.8', '>=')) {
236
237
238
239
240
            // Update the event_log table using the json datatype
            // by converting the data field to json.
            // This datatype was introduced in Mysql 5.7.8.
            log_debug('Adjust existing "event_log" data for "saveview" and "deleteview" events');
            $sql = "UPDATE {event_log} e
241
                    LEFT JOIN {view} v ON v.id = JSON_EXTRACT( CAST( e.data AS JSON ), '$.id')
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
                    SET e.resourceid   = JSON_EXTRACT( CAST( e.data AS JSON ), '$.id'),
                        e.resourcetype = 'view',
                        e.ownerid      = v.owner,
                        e.ownertype    = CASE WHEN v.owner IS NULL THEN NULL ELSE 'view' END
                    WHERE e.event IN ('saveview', 'deleteview')";
            execute_sql($sql);

            log_debug('Adjust existing "event_log" data for "userjoinsgroup" event');
            $sql = "UPDATE {event_log}
                    SET resourceid   = JSON_EXTRACT( CAST( data AS JSON ), '$.group' ),
                        resourcetype = 'group',
                        ownerid      = JSON_EXTRACT( CAST( data AS JSON ), '$.group' ),
                        ownertype    = 'group'
                    WHERE event IN ('userjoinsgroup')";
            execute_sql($sql);

            log_debug('Adjust existing "event_log" data for "creategroup" event');
            $sql = "UPDATE {event_log}
                    SET resourceid   = JSON_EXTRACT( CAST( data AS JSON ), '$.id' ),
                        resourcetype = 'group',
                        ownerid      = JSON_EXTRACT( CAST( data AS JSON ), '$.id' ),
                        ownertype    = 'group'
                    WHERE event IN ('creategroup')";
            execute_sql($sql);

            log_debug('Adjust existing "event_log" data for "saveartefact" and "deleteartefact" events');
            $sql = "UPDATE {event_log}
                    SET resourceid   = JSON_EXTRACT( CAST( data AS JSON ), '$.id' ),
                        resourcetype = JSON_EXTRACT( CAST( data AS JSON), '$.artefacttype' )
                    WHERE event IN ('saveartefact', 'deleteartefact')";
            execute_sql($sql);
273

274
275
276
277
278
279
            log_debug('Adjust existing "event_log" data for "blockinstancecommit" and "deleteblockinstance" events');
            $sql = "UPDATE {event_log}
                    SET resourceid   = JSON_EXTRACT( CAST( data AS JSON ), '$.id' ),
                        resourcetype = JSON_EXTRACT( CAST( data AS JSON), '$.blocktype' )
                    WHERE event IN ('blockinstancecommit', 'deleteblockinstance')";
            execute_sql($sql);
280

281
282
283
284
285
286
            log_debug('Adjust existing "event_log" data for "addfriend" and "removefriend" events');
            $sql = "UPDATE {event_log}
                    SET resourceid   = JSON_EXTRACT( CAST( data AS JSON ), '$.friend' ),
                        resourcetype = 'friend'
                    WHERE event IN ('addfriend', 'removefriend')";
            execute_sql($sql);
287

288
289
290
291
292
293
            log_debug('Adjust existing "event_log" data for "addfriendrequest" event');
            $sql = "UPDATE {event_log}
                    SET resourceid   = JSON_EXTRACT( CAST( data AS JSON ), '$.owner' ),
                        resourcetype = 'friend'
                    WHERE event IN ('addfriendrequest')";
            execute_sql($sql);
294

295
296
297
298
299
300
301
302
303
304
305
306
            log_debug('Adjust existing "event_log" data for "removefriendrequest" event');
            $sql = "UPDATE {event_log}
                    SET resourceid   = JSON_EXTRACT( CAST( data AS JSON ), '$.requester' ),
                        resourcetype = 'friend'
                    WHERE event IN ('removefriendrequest')";
            execute_sql($sql);
        }
        else if (is_postgres() && version_compare($db_version, '9.2.0', '>=')) {
            // Update the event_log table using the json datatype
            // by converting the data field to json.
            // This datatype was introduced in Postgres 9.2.
            log_debug('Adjust existing "event_log" data for "saveview" and "deleteview" events');
307
            $sql = "UPDATE {event_log} e2
308
309
                    SET resourceid   = CAST( CAST( e.data AS JSON )->>'id' AS INTEGER ),
                        resourcetype = 'view',
310
311
312
313
                        ownerid      = v.owner,
                        ownertype    = CASE WHEN v.owner IS NULL THEN NULL ELSE 'view' END
                    FROM {event_log} e
                    LEFT JOIN {view} v ON v.id = CAST( CAST( e.data AS JSON )->>'id' AS BIGINT)
314
                    WHERE e.event IN ('saveview', 'deleteview')
315
                    AND e2.id = e.id";
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
            execute_sql($sql);

            log_debug('Adjust existing "event_log" data for "userjoinsgroup" event');
            $sql = "UPDATE {event_log}
                    SET resourceid   = CAST( CAST( data AS JSON )->>'group' AS INTEGER ),
                        resourcetype = 'group',
                        ownerid      = CAST( CAST( data AS JSON )->>'group' AS BIGINT ),
                        ownertype    = 'group'
                    WHERE event IN ('userjoinsgroup')";
            execute_sql($sql);

            log_debug('Adjust existing "event_log" data for "creategroup" event');
            $sql = "UPDATE {event_log}
                    SET resourceid   = CAST( CAST( data AS JSON )->>'id' AS INTEGER ),
                        resourcetype = 'group',
                        ownerid      = CAST( CAST( data AS JSON )->>'id' AS BIGINT ),
                        ownertype    = 'group'
                    WHERE event IN ('creategroup')";
            execute_sql($sql);

            log_debug('Adjust existing "event_log" data for "saveartefact" and "deleteartefact" events');
            $sql = "UPDATE {event_log}
                    SET resourceid   = CAST( CAST( data AS JSON )->>'id' AS INTEGER ),
                        resourcetype = CAST( data AS JSON)->>'artefacttype'
                    WHERE event IN ('saveartefact', 'deleteartefact')";
            execute_sql($sql);
342

343
344
345
346
347
348
            log_debug('Adjust existing "event_log" data for "blockinstancecommit" and "deleteblockinstance" events');
            $sql = "UPDATE {event_log}
                    SET resourceid   = CAST( CAST( data AS JSON )->>'id' AS INTEGER ),
                        resourcetype = CAST( data AS JSON)->>'blocktype'
                    WHERE event IN ('blockinstancecommit', 'deleteblockinstance')";
            execute_sql($sql);
349

350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
            log_debug('Adjust existing "event_log" data for "addfriend" and "removefriend" events');
            $sql = "UPDATE {event_log}
                    SET resourceid   = CAST( CAST( data AS JSON )->>'friend' AS INTEGER ),
                        resourcetype = 'friend'
                    WHERE event IN ('addfriend', 'removefriend')";
            execute_sql($sql);

            log_debug('Adjust existing "event_log" data for "addfriendrequest" event');
            $sql = "UPDATE {event_log}
                    SET resourceid   = CAST( CAST( data AS JSON )->>'owner' AS INTEGER ),
                        resourcetype = 'friend'
                    WHERE event IN ('addfriendrequest')";
            execute_sql($sql);

            log_debug('Adjust existing "event_log" data for "removefriendrequest" event');
            $sql = "UPDATE {event_log}
                    SET resourceid   = CAST( CAST( data AS JSON )->>'requester' AS INTEGER ),
                        resourcetype = 'friend'
                    WHERE event IN ('removefriendrequest')";
            execute_sql($sql);
        }
        else {
            // This is an older database. Can't use json datatype.
            // Will need to loop through all the records and adjust.
            // As there can be very many rows we need to do the adjusting in chuncks.
            log_debug('Loop through all records and adjust');
            $count = 0;
            $limit = 10000;
            $chunk = 5000;
            $total = count_records_select('event_log', 'data != ?', array('{}'));
            if ($total > 0) {
                for ($i = 0; $i <= $total; $i += $chunk) {
                    $results = get_records_sql_array("SELECT id, event, data FROM {event_log}", array(), $count, $chunk);
                    foreach ($results as $result) {
                        $data = json_decode($result->data);
                        $where = clone $result;
                        switch ($result->event) {
                            case 'saveview':
                            case 'deleteview':
                                $result->resourceid = $data->id;
                                $result->resourcetype = 'view';
                                break;
                            case 'userjoinsgroup':
                                $result->resourceid = $data->group;
                                $result->resourcetype = 'group';
                                break;
                            case 'creategroup':
                                $result->resourceid = $data->id;
                                $result->resourcetype = 'group';
                                break;
                            case 'saveartefact':
                            case 'deleteartefact':
                                // Make sure there is actually an id and artefacttype.
                                if (isset($data->id)) {
                                    $result->resourceid = $data->id;
                                }
                                if (isset($data->artefacttype)) {
                                    $result->resourcetype = $data->artefacttype;
                                }
                                break;
                            case 'deleteartefacts':
                                // These hold multiple ids. Can't do much with them.
                                // Will leave them as is.
                                break;
                            case 'blockinstancecommit':
                            case 'deleteblockinstance':
                                $result->resourceid = $data->id;
                                $result->resourcetype = $data->blocktype;
                                break;
                            case 'addfriend':
                            case 'removefriend':
                                $result->resourceid = $data->friend;
                                $result->resourcetype = 'friend';
                                break;
                            case 'addfriendrequest':
                                $result->resourceid = $data->owner;
                                $result->resourcetype = 'friend';
                                break;
                            case 'removefriendrequest':
                                $result->resourceid = $data->requester;
                                $result->resourcetype = 'friend';
                                break;
                        }
                        list ($ownerid, $ownertype) = event_find_owner_type($result);
                        $result->ownerid = $ownerid;
                        $result->ownertype = $ownertype;
                        unset($result->id); // No reason to update the ID.
                        update_record('event_log', $result, array('id'=>$where->id));
438
                    }
439
440
441
442
443
444
445
                    $count += $chunk;
                    if (($count % $limit) == 0 || $count >= $total) {
                        if ($count > $total) {
                            $count = $total;
                        }
                        log_debug("$count/$total");
                        set_time_limit(30);
446
447
448
449
450
451
452
453
454
455
456
457
                    }
                }
            }
        }

        log_debug('Add new logging events');
        $newevents = array('createview',
                           'createcollection',
                           'updatecollection',
                           'deletecollection',
                           'addsubmission',
                           'releasesubmission',
458
459
                           'updateviewaccess',
                           'sharedcommenttogroup');
460
461
462
463
464
465
466
467
        foreach ($newevents as $newevent) {
            $event = (object)array(
                'name' => $newevent,
            );
            ensure_record_exists('event_type', $event, $event);
        }
    }

468
469
470
471
472
    if ($oldversion < 2017092100) {
        log_debug('Remove obsolete default notification method');
        delete_records('config', 'field', 'defaultnotificationmethod');
    }

473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
    if ($oldversion < 2017092200) {
        // This code taken directly from browserid plugin, which we've just deleted so we need
        // to run the move users to 'internal' auth here
        if ($instances = get_records_array('auth_instance', 'authname', 'browserid', 'id')) {
            log_debug('Re-assigning users from "Persona" to "Internal" authentication');
            foreach ($instances as $authinst) {
                // Are there any users with this auth instance?
                if (record_exists('usr', 'authinstance', $authinst->id)) {
                    // Find the internal auth instance for this institution
                    $internal = get_field('auth_instance', 'id', 'authname', 'internal', 'institution', $authinst->institution);
                    if (!$internal) {
                        // Institution has no internal auth instance. Create one.
                        $todb = new stdClass();
                        $todb->instancename = 'internal';
                        $todb->authname = 'internal';
                        $todb->active = 1;
                        $todb->institution = $authinst->institution;
                        $todb->priority = $authinst->priority;
                        $internal = insert_record('auth_instance', $todb, 'id', true);
                    }
                    // Set the password & salt for Persona users to "*", which means "no password set"
                    update_record('usr', (object)array('password' => '*', 'salt' => '*'), array('authinstance' => $authinst->id));
                    set_field('usr', 'authinstance', $internal, 'authinstance', $authinst->id);
                    set_field('usr_registration', 'authtype', 'internal', 'authtype', 'browserid');
                }
                // Delete the Persona auth instance
                delete_records('auth_remote_user', 'authinstance', $authinst->id);
                delete_records('auth_instance_config', 'instance', $authinst->id);
                delete_records('auth_instance', 'id', $authinst->id);
                // Make it no longer be the parent authority to any auth instances
                delete_records('auth_instance_config', 'field', 'parent', 'value', $authinst->id);
            }
        }
        log_debug('Removing "Persona" authentication plugin');
        delete_records('auth_config', 'plugin', 'browserid');
        delete_records('auth_cron', 'plugin', 'browserid');
        delete_records('auth_event_subscription', 'plugin', 'browserid');
        delete_records('auth_installed', 'name', 'browserid');
    }

513
514
515
516
517
    if ($oldversion < 2017092500) {
        log_debug('Clear all caches to allow regeneration of session directories');
        clear_all_caches(true);
    }

518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
    if ($oldversion < 2017092600) {
        log_debug('Add primary key to site_content table');

        // See if we need to add the id column
        $table = new XMLDBTable('site_content');
        $field = new XMLDBField('id');
        if (!field_exists($table, $field)) {
            log_debug('Making a temp copy and adding id column');
            execute_sql('CREATE TEMPORARY TABLE {temp_site_content} AS SELECT DISTINCT * FROM {site_content}', array());
            execute_sql('TRUNCATE {site_content}', array());

            // Drop the current primary key as we will move it to the id column
            $key = new XMLDBKey('primary');
            $key->setAttributes(XMLDB_KEY_PRIMARY, array('name', 'institution'));
            drop_key($table, $key);

            if (is_mysql()) {
                // MySQL requires the auto-increment column to be a primary key right away.
                execute_sql('ALTER TABLE {site_content} ADD id BIGINT(10) NOT NULL auto_increment PRIMARY KEY FIRST');
            }
            else {
                $field->setAttributes(XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, XMLDB_SEQUENCE);
                add_field($table, $field);
            }

            log_debug('Adding back in the site_content information');
            // We will do in chuncks for large sites.
            $count = 0;
            $x = 0;
            $limit = 1000;
            $total = count_records('temp_site_content');
            for ($i = 0; $i <= $total; $i += $limit) {
                if (is_postgres()) {
                    $limitsql = ' OFFSET ' . $i . ' LIMIT ' . $limit;
                }
                else {
                    $limitsql = ' LIMIT ' . $i . ',' . $limit;
                }
                execute_sql('INSERT INTO {site_content} (name, content, ctime, mtime, mauthor, institution) SELECT name, content, ctime, mtime, mauthor, institution FROM {temp_site_content}' . $limitsql, array());
                $count += $limit;
                if (($count % ($limit *10)) == 0 || $count >= $total) {
                    if ($count > $total) {
                        $count = $total;
                    }
                    log_debug("$count/$total");
                    set_time_limit(30);
                }
                set_time_limit(30);
            }
            execute_sql('DROP TABLE {temp_site_content}', array());

            if (!is_mysql()) {
                log_debug('Adding primary key index to site_content.id column');
                $key = new XMLDBKey('primary');
                $key->setAttributes(XMLDB_KEY_PRIMARY, array('id'));
                add_key($table, $key);
            }
            // Add the old key as new unique index
            $index = new XMLDBIndex('nameinstuk');
            $index->setAttributes(XMLDB_INDEX_UNIQUE, array('name', 'institution'));
            add_index($table, $index);
        }

        if ($results = get_records_sql_array("SELECT id, content FROM {site_content} WHERE content LIKE '%<img%'")) {
            log_debug('Make sure images in static pages are embedded images');
            require_once('embeddedimage.php');
            foreach ($results as $result) {
                // Update the page text with any embedded image info
                $result->content = EmbeddedImage::prepare_embedded_images($result->content, 'staticpages', $result->id);
                update_record('site_content', $result, array('id'));
            }
        }
    }

592
593
594
595
596
597
598
599
600
601
    if ($oldversion < 2017102700) {
        log_debug('Add an "useragent" field to usr_session table');
        $table = new XMLDBTable('usr_session');
        $field = new XMLDBField('useragent');
        $field->setType(XMLDB_TYPE_TEXT);
        $field->setLength('small');
        if (!field_exists($table, $field)) {
            add_field($table, $field);
        }
    }
602
603

    if ($oldversion < 2017103100) {
604
        log_debug('Remove MaharaDroid configurations');
605
606
607
608
609
610
611
        // remove allowmobileuploads from config, used only by old mobile api
        delete_records('config','field','allowmobileuploads');
        // remove mobileuploadtoken from account settings, used only by old mobile api
        // for new mobile api, we save tokens in table external_tokens
        delete_records('usr_account_preference','field','mobileuploadtoken');
    }

612
613
614
615
616
    if ($oldversion < 2017110600) {
        log_debug('Add Voki filters to DB');
        reload_html_filters();
    }

617
    if ($oldversion < 2018010300) {
618
        log_debug('Anonymising remaining deleted user data');
619
620
621
622
623
624
625
        $sql = "UPDATE {usr}
            SET username = CONCAT(MD5(username), 1000000 + id),
            email = CONCAT(MD5(email), 1000000 + id)
            WHERE deleted = 1 ";
        execute_sql($sql);
    }

626
627
628
629
630
631
632
633
    if ($oldversion < 2018010400) {
        log_debug('Adding new event type "userleavesgroup"');
        $event = (object)array(
            'name'  => 'userleavesgroup'
        );
        ensure_record_exists('event_type', $event, $event);
    }

634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
    if ($oldversion < 2018010500) {
        log_debug('Make site_content mauthor field have a user id by defaut');
        // Defaults to usr = 0 on install
        execute_sql("UPDATE {site_content} SET mauthor = 0 WHERE mauthor IS NULL");
        $table = new XMLDBTable('site_content');
        $key = new XMLDBKEY('mauthorfk');
        $key->setAttributes(XMLDB_KEY_FOREIGN, array('mauthor'), 'usr', array('id'));
        drop_key($table, $key);

        $field = new XMLDBField('mauthor');
        $field->setAttributes(XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL);
        change_field_notnull($table, $field);

        $key = new XMLDBKEY('mauthorfk');
        $key->setAttributes(XMLDB_KEY_FOREIGN, array('mauthor'), 'usr', array('id'));
        add_key($table, $key);
    }

652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
    if ($oldversion < 2018010600) {
        log_debug('Create "site_content_version" table');

        $table = new XMLDBTable('site_content_version');
        create_table($table);

        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, XMLDB_SEQUENCE);
        $table->addFieldInfo('type', XMLDB_TYPE_CHAR, 100, null, XMLDB_NOTNULL);
        $table->addFieldInfo('content', XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL);
        $table->addFieldInfo('author', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL);
        $table->addFieldInfo('institution', XMLDB_TYPE_CHAR, 255, null, XMLDB_NOTNULL);
        $table->addFieldInfo('version', XMLDB_TYPE_CHAR, 15, null, XMLDB_NOTNULL);
        $table->addFieldInfo('ctime', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL);

        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->addKeyInfo('institutionfk', XMLDB_KEY_FOREIGN, array('institution'), 'institution', array('name'));
        $table->addKeyInfo('authorfk', XMLDB_KEY_FOREIGN, array('author'), 'usr', array('id'));

        create_table($table);
    }

673
    if ($oldversion < 2018010700) {
674
675
676
677
        log_debug('Move the site and institution Privacy statement from the site_content table to the site_content_version table');
        //For all istitutions, get the values of the "Use site default" option regarding the privacy page
        $instconfigs = get_records_sql_assoc("SELECT institution, value FROM {institution_config}
                                                WHERE field = 'sitepages_privacy'");
678
679
        if ($records = get_records_array('site_content', 'name', 'privacy')) {
            foreach ($records as $data) {
680
681
682
683
684
685
686
687
688
689
                if ($data->institution == 'mahara' || $instconfigs[$data->institution]->value == $data->institution) {
                    $record = new stdClass;
                    $record->type = 'privacy';
                    $record->content = $data->content;
                    $record->author = $data->mauthor;
                    $record->institution = $data->institution;
                    $record->version = '1.0';
                    $record->ctime = db_format_timestamp(time());
                    insert_record('site_content_version', $record);
                }
690
691
692
693
694
                delete_records('site_content', 'id', $data->id);
            }
        }
    }

695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
    if ($oldversion < 2018011000) {
        log_debug('Create "usr_agreement" table');

        $table = new XMLDBTable('usr_agreement');

        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, XMLDB_SEQUENCE);
        $table->addFieldInfo('usr', XMLDB_TYPE_INTEGER, 10, null, true);
        $table->addFieldInfo('sitecontentid', XMLDB_TYPE_INTEGER, 10, null, true);
        $table->addFieldInfo('ctime', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL);
        $table->addFieldInfo('agreed', XMLDB_TYPE_INTEGER, 1, null, XMLDB_NOTNULL);

        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->addKeyInfo('usrfk', XMLDB_KEY_FOREIGN, array('usr'), 'usr', array('id'));
        $table->addKeyInfo('sitecontentfk', XMLDB_KEY_FOREIGN, array('sitecontentid'), 'site_content_version', array('id'));

        create_table($table);
    }

713
714
715
716
717
718
719
720
721
    if ($oldversion < 2018013000) {
        log_debug('Auto accept the privacy agreement for all site admins');
        $sitecontentid = get_field('site_content_version', 'id', 'type', 'privacy', 'institution', 'mahara');
        $admins = get_site_admins();
        foreach ($admins as $admin) {
            save_user_reply_to_agreement($admin->id, $sitecontentid, 1);
        }
    }

722
    if ($oldversion < 2018013001) {
723
724
725
726
        log_debug('Move the site and institution terms and conditions  from the site_content table to the site_content_version table');
        //For all istitutions, get the values of the "Use site default" option regarding the termsandconditions page
        $instconfigs = get_records_sql_assoc("SELECT institution, value FROM {institution_config}
                                        WHERE field = 'sitepages_termsandconditions'");
727
728
        if ($records = get_records_array('site_content', 'name', 'termsandconditions')) {
            foreach ($records as $data) {
729
730
731
732
733
734
735
736
737
738
739
                if ($data->institution == 'mahara' || $instconfigs[$data->institution]->value == $data->institution) {
                    $record = new stdClass;
                    $record->type = 'termsandconditions';
                    $record->content = $data->content;
                    $record->author = $data->mauthor;
                    $record->institution = $data->institution;
                    $record->version = '1.0';
                    $record->ctime = db_format_timestamp(time());

                    insert_record('site_content_version', $record);
                }
740
741
742
743
744
745
746
747
748
749
750
751
                delete_records('site_content', 'id', $data->id);
            }
        }

        log_debug('Auto accept the terms and conditions for all site admins');
        $sitecontentid = get_field('site_content_version', 'id', 'type', 'termsandconditions', 'institution', 'mahara');
        $admins = get_site_admins();
        foreach ($admins as $admin) {
            save_user_reply_to_agreement($admin->id, $sitecontentid, 1);
        }
    }

752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
    if ($oldversion < 2018021500) {
        log_debug('Remove privacy statement and T&C conditions custom links from footer');
        // remove custom links
        if ($footercustomlinks = get_config('footercustomlinks')) {
            $footercustomlinks = unserialize($footercustomlinks);
            $removedlinks = array();
            foreach ($footercustomlinks as $key => $customlink) {
                if ($key == 'termsandconditions' || $key == 'privacystatement') {
                    $removedlinks[] = $customlink;
                    unset($footercustomlinks[$key]);
                }
            }
            if ($removedlinks) {
                $SESSION->add_error_msg(get_string('removefooterlinksupgradewarning', 'mahara', implode(', ', $removedlinks)));
            }
            set_config('footercustomlinks', serialize($footercustomlinks));
        }
    }

771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
    if ($oldversion < 2018021600) {
        log_debug('Add an "usr_pendingdeletion" table');
        $table = new XMLDBTable('usr_pendingdeletion');

        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('usr', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL);
        $table->addFieldInfo('ctime', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL);
        $table->addFieldInfo('reason', XMLDB_TYPE_TEXT);

        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->addKeyInfo('usrfk', XMLDB_KEY_FOREIGN, array('usr'), 'usr', array('id'));

        create_table($table);

        log_debug('Clean old alwaysallowselfdelete configuration setting');
        execute_sql("DELETE FROM {config} WHERE field = ?", array('alwaysallowselfdelete'));
    }

789
790
791
792
793
794
795
796
797
798
799
800
801
    if ($oldversion < 2018021601) {
        log_debug('Remove auto-added users from deleted groups if added post deletion');
        if ($records = get_records_sql_array("SELECT id, SUBSTR(name, POSITION('.deleted.' IN name) + LENGTH('.deleted.')) AS deltime
                                              FROM {group} WHERE deleted = 1")) {
            foreach ($records as $key => $record) {
                if (is_numeric($record->deltime)) {
                    $timestamp = date('Y-m-d H:i:s', $record->deltime);
                    delete_records_sql('DELETE FROM {group_member} WHERE "group" = ? AND ctime >= ?', array($record->id, $timestamp));
                }
            }
        }
    }

802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
    if ($oldversion < 2018022100) {
        log_debug('Combine footer links T&C and Privacy into Legal link');

        if ($enabledfooterlinks = get_config('footerlinks')) {
            $enabledfooterlinks = unserialize($enabledfooterlinks);
            $enablelegal = false;
            if (in_array('termsandconditions', $enabledfooterlinks)) {
                $key = array_search('termsandconditions', $enabledfooterlinks);
                unset($enabledfooterlinks[$key]);
                $enablelegal = true;
            }
            if (in_array('privacystatement', $enabledfooterlinks)) {
                $key = array_search('privacystatement', $enabledfooterlinks);
                unset($enabledfooterlinks[$key]);
                $enablelegal = true;
            }
            // if T&C or privacy links were shown on the footer, then show legal link
            if ($enablelegal) {
                $enabledfooterlinks[] = 'legal';
                set_config('footerlinks', serialize($enabledfooterlinks));
            }
        }
    }

826
827
828
829
830
    if ($oldversion < 2018022200) {
        log_debug('Removing confusing blockinstance title information');
        execute_sql("UPDATE {block_instance} SET title = '' WHERE blocktype IN ('myviews', 'mygroups', 'myfriends', 'wall')");
    }

831
832
833
834
835
836
837
838
839
840
841
    if ($oldversion < 2018022300) {
        log_debug('Add "manualhelp" as a default footer link');
        $footerlinkstr = get_config('footerlinks');
        $footerlinks = unserialize($footerlinkstr);
        if (!in_array('manualhelp', $footerlinks)) {
            $footerlinks[] = 'manualhelp';
        }
        $footerlinkstr = serialize($footerlinks);
        set_config('footerlinks', $footerlinkstr);
    }

842
843
844
845
846
847
848
849
850
851
852
853
    if ($oldversion < 2018022400) {
        log_debug('Create a new "unsubscribetoken" field in "usr_watchlist_view" table');
        $table = new XMLDBTable('usr_watchlist_view');
        $field = new XMLDBField('unsubscribetoken');
        $field->setAttributes(XMLDB_TYPE_CHAR, 24);
        add_field($table, $field);

        $index = new XMLDBIndex('unsubscribetokenix');
        $index->setAttributes(XMLDB_INDEX_UNIQUE, array('unsubscribetoken'));
        add_index($table, $index);
    }

854
855
856
857
858
859
860
    if ($oldversion < 2018022500) {
        log_debug('Upgrade artefact/file plugin');
        if ($data = check_upgrades('artefact.file')) {
            upgrade_plugin($data);
        }
    }

861
862
863
864
865
866
867
868
869
870
871
872
873
874
    if ($oldversion < 2018022503) {
        if (!get_config('passwordpolicy')) {
            log_debug('Force users to change their password to fix new password policy');
            execute_sql("UPDATE {usr} SET passwordchange = 1
                         WHERE authinstance IN (
                             SELECT ai.id
                             FROM {auth_instance} ai
                             WHERE ai.authname = 'internal'
                         )
                         AND id != 0"); // Ignore the root user
            set_config('passwordpolicy', '8_ulns');
        }
    }

875
876
877
878
879
880
881
    if ($oldversion < 2018022516) {
        log_debug('offset some troublesome cron jobs');
        execute_sql("UPDATE {cron} SET minute = ? WHERE callfunction = ?", array('2-59/5', 'user_login_tries_to_zero'));
        execute_sql("UPDATE {interaction_cron} SET minute = ? WHERE plugin = ? AND callfunction = ?", array('3-59/30', 'forum', 'interaction_forum_new_post'));
        execute_sql("UPDATE {search_cron} SET minute = ? WHERE plugin = ? AND callfunction = ?", array('4-59/5', 'elasticsearch', 'cron'));
    }

882
    return $status;
883
}