upgrade.php 18.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?php
/**
 * This program is part of Mahara
 *
 *  This program 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 * @package    mahara
Penny Leach's avatar
Penny Leach committed
20 21
 * @subpackage core
 * @author     Penny Leach <penny@catalyst.net.nz>
22 23 24 25 26 27 28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

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

29 30
require_once('template.php');

31 32 33
function xmldb_core_upgrade($oldversion=0) {

    $status = true;
34
    $prefix = get_config('dbprefix');
35

Penny Leach's avatar
Penny Leach committed
36 37 38 39 40 41 42 43 44 45
    if ($oldversion < 2006121400) {

        set_field('community', 'jointype', 'open', 'jointype', 'anyone');
        $table = new XMLDBTable('community');
        $field = new XMLDBField('jointype');
        $field->setAttributes(XMLDB_TYPE_CHAR, 20, false, true, false, true, 
                              array('controlled', 'invite', 'request', 'open'), 'open');
        change_field_default($table, $field);
        change_field_enum($table, $field);
    }
46

47 48 49 50 51 52 53 54 55 56
    if ($oldversion < 2006121401) {
        $table = new XMLDBTable('usr');
        $field = new XMLDBField('expirymailsent');
        $field->setAttributes(XMLDB_TYPE_INTEGER, 1, false, true, false, null, null, 0);
        add_field($table, $field);
        $field = new XMLDBField('inactivemailsent');
        $field->setAttributes(XMLDB_TYPE_INTEGER, 1, false, true, false, null, null, 0);
        add_field($table, $field);
    }

Nigel McNie's avatar
Nigel McNie committed
57 58 59 60 61 62 63 64 65
    // Add cron jobs for authentication cleanup: partial registrations and account expiries
    if ($oldversion < 2006121500) {
        $registrations = new StdClass;
        $registrations->callfunction = 'auth_clean_partial_registrations';
        $registrations->minute = '0';
        $registrations->hour = '5';
        insert_record('cron', $registrations);

        $expiries = new StdClass;
66 67 68
        $expiries->callfunction = 'auth_handle_account_expiries';
        $expiries->minute = '10';
        $expiries->hour = '5';
Nigel McNie's avatar
Nigel McNie committed
69 70 71
        insert_record('cron', $expiries);
    }

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
    // Merge the usr_suspension table with the usr table, and add a 'active' column and
    // various events and fields to do with this column
    if ($oldversion < 2006121800) {
        $table = new XMLDBTable('usr_suspension');
        drop_table($table);

        $table = new XMLDBTable('usr');

        $field = new XMLDBField('suspendedctime');
        $field->setAttributes(XMLDB_TYPE_DATETIME);
        add_field($table, $field);

        $field = new XMLDBField('suspendedreason');
        $field->setAttributes(XMLDB_TYPE_TEXT);
        add_field($table, $field);

        $field = new XMLDBField('suspendedcusr');
        $field->setAttributes(XMLDB_TYPE_INTEGER, 10, false, false, false);
        add_field($table, $field);

        $field = new XMLDBField('active');
        $field->setAttributes(XMLDB_TYPE_INTEGER, 1, false, true, false, null, null, 1);
        add_field($table, $field);
        set_field('usr', 'active', 1);

        $key = new XMLDBKey('sus_fk');
        $key->setAttributes(XMLDB_KEY_FOREIGN, array('suspendedcusr'), 'usr', array('id'));
        add_key($table, $key);

        $event = new StdClass;
        $event->name = 'unsuspenduser';
        insert_record('event_type', $event);
        $event->name = 'undeleteuser';
        insert_record('event_type', $event);
        $event->name = 'expireuser';
        insert_record('event_type', $event);
        $event->name = 'unexpireuser';
        insert_record('event_type', $event);
        $event->name = 'deactivateuser';
        insert_record('event_type', $event);
        $event->name = 'activateuser';
        insert_record('event_type', $event);

        $activecheck = new StdClass;
        $activecheck->plugin = 'internal';
        $activecheck->event = 'suspenduser';
        $activecheck->callfunction = 'update_active_flag';
        insert_record('auth_event_subscription', $activecheck);
        $activecheck->event = 'unsuspenduser';
        insert_record('auth_event_subscription', $activecheck);
        $activecheck->event = 'deleteuser';
        insert_record('auth_event_subscription', $activecheck);
        $activecheck->event = 'undeleteuser';
        insert_record('auth_event_subscription', $activecheck);
        $activecheck->event = 'expireuser';
        insert_record('auth_event_subscription', $activecheck);
        $activecheck->event = 'unexpireuser';
        insert_record('auth_event_subscription', $activecheck);
        $activecheck->event = 'deactivateuser';
        insert_record('auth_event_subscription', $activecheck);
        $activecheck->event = 'activateuser';
        insert_record('auth_event_subscription', $activecheck);

    }

Richard Mansfield's avatar
Richard Mansfield committed
137 138 139 140 141 142 143 144 145 146 147 148
    if ($oldversion < 2006122200) {
        // Creating usr_infectedupload table
        $table = new XMLDBTable('usr_infectedupload');

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

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

149
        create_table($table);
Richard Mansfield's avatar
Richard Mansfield committed
150 151
    }

152 153 154 155 156
    if ($oldversion < 2007011200) {
        // Add path to file to the config table
        set_config('pathtofile', '/usr/bin/file');
    }

157 158 159 160 161 162 163 164
    if ($oldversion < 2007011500) {
        // Add the 'profileicon' field to the usr table
        $table = new XMLDBTable('usr');
        $field = new XMLDBField('profileicon');
        $field->setAttributes(XMLDB_TYPE_INTEGER, 10, false, false, false);
        add_field($table, $field);
    }

165 166 167 168 169 170 171 172 173 174
    if ($oldversion < 2007011600) {
        // Add the 'quota' and 'quotaused' fields to the usr table
        $table = new XMLDBTable('usr');
        $field = new XMLDBField('quota');
        $field->setAttributes(XMLDB_TYPE_INTEGER, 10, false, false, false);
        add_field($table, $field);
        $field = new XMLDBField('quotaused');
        $field->setAttributes(XMLDB_TYPE_INTEGER, 10, false, true, null, null, null, 0);
        add_field($table, $field);

175
        execute_sql('UPDATE ' . $prefix . 'usr SET quota=10485760');
176 177
    }

178 179 180 181 182 183 184 185 186
    if ($oldversion < 2007012300) {
        // fix up a broken cron entry...
        set_field('cron', 'minute', '0', 'callfunction', 'rebuild_artefact_parent_cache_complete');
        $c = new StdClass;
        $c->callfunction = 'activity_process_queue';
        $c->minute = '*/5';
        insert_record('cron', $c);
    }

187 188 189 190 191 192 193 194
    if ($oldversion < 2007012301) {
        $at = new StdClass;
        $at->name = 'viewaccess';
        $at->admin = 0;
        $at->delay = 1;
        insert_record('activity_type', $at);
    }

195 196
    if ($oldversion < 2007012500) {
        $table = new XMLDBTable('usr_watchlist_artefact');
197
        $field = new XMLDBField('deleted');
198 199 200 201
        $field->setAttributes(XMLDB_TYPE_INTEGER, 1, false, true, false, false, false, 0);
        add_field($table, $field);
    }

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    if ($oldversion < 2007020300) {
        // artefact_tag table
        $table = new XMLDBTable('artefact_tag');
        $table->addFieldInfo('artefact', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL);
        $table->addKeyInfo('artefactfk', XMLDB_KEY_FOREIGN, array('artefact'), 'artefact', array('id'));
        $table->addFieldInfo('tag', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL);
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('artefact', 'tag'));
        create_table($table);

        // view_tag table
        $table = new XMLDBTable('view_tag');
        $table->addFieldInfo('view', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL);
        $table->addKeyInfo('viewfk', XMLDB_KEY_FOREIGN, array('view'), 'view', array('id'));
        $table->addFieldInfo('tag', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL);
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('view', 'tag'));
        create_table($table);
    }
219 220 221 222
    if ($oldversion < 2007020500) {
        insert_record('event_type', (object)array('name' => 'saveartefact'));
        insert_record('event_type', (object)array('name' => 'deleteartefact'));
    }
223

224 225 226 227 228
    if ($oldversion < 2007021600) {
        $template = template_parse('gallery');
        upgrade_template('gallery', $template);
    }

Martyn Smith's avatar
Martyn Smith committed
229 230 231 232 233
    if ($oldversion < 2007021900) {
        insert_record('event_type', (object)array('name' => 'saveview'));
        insert_record('event_type', (object)array('name' => 'deleteview'));
    }

234 235 236 237 238
    if ($oldversion < 2007021902) {
        $template = template_parse('filelist');
        upgrade_template('filelist', $template);
    }

239 240 241 242 243 244
    if ($oldversion < 2007021903) {
        execute_sql("UPDATE {$prefix}artefact
            SET container = 1
            WHERE artefacttype = 'blog'");
    }

245 246 247 248 249
    if ($oldversion < 2007021905) {
        $template = template_parse('blogandprofile');
        upgrade_template('blogandprofile', $template);
    }

250 251 252 253
    if ($oldversion < 2007042500) {
        // migrate everything we had to change to  make mysql happy
        execute_sql("ALTER TABLE {$prefix}cron DROP CONSTRAINT {$prefix}cron_cal_pk"); // can't drop primary keys using xmldb...
        execute_sql("ALTER TABLE {$prefix}cron ADD CONSTRAINT {$prefix}cron_id_pk PRIMARY KEY (id)"); // or add them!
254
        execute_sql("ALTER TABLE {$prefix}cron ADD CONSTRAINT {$prefix}cron_cal_uix UNIQUE (callfunction)");
255 256 257 258 259 260 261 262
        execute_sql("ALTER TABLE {$prefix}community ALTER COLUMN name TYPE varchar (128)");
        execute_sql("ALTER TABLE {$prefix}usr_activity_preference ALTER COLUMN method TYPE varchar(255)");
        execute_sql("ALTER TABLE {$prefix}template_category ALTER COLUMN name TYPE varchar(128)");
        execute_sql("ALTER TABLE {$prefix}template_category ALTER COLUMN parent TYPE varchar(128)");
        execute_sql("ALTER TABLE {$prefix}template ALTER COLUMN name TYPE varchar(128)");
        execute_sql("ALTER TABLE {$prefix}template ALTER COLUMN category TYPE varchar(128)");
        execute_sql("ALTER TABLE {$prefix}view ALTER COLUMN template TYPE varchar(128)");
        execute_sql("ALTER TABLE {$prefix}view_access ALTER COLUMN accesstype SET DEFAULT 'public'");
263
        execute_sql("ALTER TABLE {$prefix}usr ALTER COLUMN email TYPE varchar(255)");
264 265
    }

266
    // everything up to here was pre mysql support.
267 268

    if ($oldversion < 2007062000) {
269
        if (!get_record('config', 'field', 'lang')) {
270 271
            execute_sql("INSERT INTO {$prefix}config (field, value) VALUES ('lang', (SELECT value FROM {$prefix}config WHERE field = 'language'))");
        }
272 273 274 275 276 277 278 279 280 281 282 283 284
        delete_records('config', 'field', 'language');
    }

    if ($oldversion < 2007062900) {
        // application table
        $table = new XMLDBTable('application');
        $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL);
        $table->addFieldInfo('displayname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL);
        $table->addFieldInfo('xmlrpcserverurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL);
        $table->addFieldInfo('ssolandurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL);
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('name'));
        create_table($table);

285
        $table = new XMLDBTable('auth_installed');
Donal McMullan's avatar
Donal McMullan committed
286 287 288
        //$field = new XMLDBField('name');
        //$field->setAttributes(XMLDB_TYPE_CHAR, 100, null, true, null, null, null);
        //change_field_precision($table, $field);
289
        
290
        $field = new XMLDBField('requires_config');
291
        $field->setAttributes(XMLDB_TYPE_INTEGER, 1, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, 0);
292 293
        add_field($table, $field);

294
        $field = new XMLDBField('requires_parent');
295
        $field->setAttributes(XMLDB_TYPE_INTEGER, 1, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, 0);
296 297
        add_field($table, $field);

298 299 300
        $table = new XMLDBTable('auth_instance');
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE);
        $table->addFieldInfo('instancename', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL);
301
        $table->addFieldInfo('priority', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, 0);
302 303 304 305
        $table->addFieldInfo('institution', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL);
        $table->addFieldInfo('authname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL);
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        create_table($table);
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
        $key   = new XMLDBKey("authnamefk");
        $key->setAttributes(XMLDB_KEY_FOREIGN, array('authname'), 'auth_installed', array('name'));
        add_key($table, $key);
        $key   = new XMLDBKey("institutionfk");
        $key->setAttributes(XMLDB_KEY_FOREIGN, array('institution'), 'institution', array('name'));
        add_key($table, $key);

        $table = new XMLDBTable('auth_instance_config');
        $table->addFieldInfo('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL);
        $table->addFieldInfo('field', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL);
        $table->addFieldInfo('value', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL);
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('instance','field'));
        create_table($table);

        $key   = new XMLDBKey("instancefk");
        $key->setAttributes(XMLDB_KEY_FOREIGN, array('instance'), 'auth_instance', array('id'));
        add_key($table, $key);

        $table = new XMLDBTable('host');
        $table->addFieldInfo('wwwroot', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL);
        $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '80', null, XMLDB_NOTNULL);
        $table->addFieldInfo('institution', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL);
        $table->addFieldInfo('ipaddress', XMLDB_TYPE_CHAR, '39', null, XMLDB_NOTNULL);
        $table->addFieldInfo('portno', XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, 80);
        $table->addFieldInfo('appname', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL);
332
        $table->addFieldInfo('publickey', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null, null, '');
333
        $table->addFieldInfo('publickeyexpires', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL);
334 335
        $table->addFieldInfo('deleted', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, 0);
        $table->addFieldInfo('lastconnecttime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, 0);
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 364 365 366 367
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('wwwroot'));
        create_table($table);

        $key   = new XMLDBKey("appnamefk");
        $key->setAttributes(XMLDB_KEY_FOREIGN, array('appname'), 'application', array('name'));
        add_key($table, $key);

        $key   = new XMLDBKey("institutionfk");
        $key->setAttributes(XMLDB_KEY_FOREIGN, array('institution'), 'institution', array('name'));
        add_key($table, $key);

        $table = new XMLDBTable('institution');
        $key   = new XMLDBKey("pluginfk");
        $key->setAttributes(XMLDB_KEY_FOREIGN, array('authplugin'), 'auth_installed', array('name'));
        drop_key($table, $key);

        $field = new XMLDBField('authplugin');
        $field->setAttributes(XMLDB_TYPE_CHAR, 255, null, XMLDB_NOTNULL, null, null, null, '');
        drop_field($table, $field);

        $table = new XMLDBTable('sso_session');
        $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL);
        $table->addFieldInfo('instanceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL);
        $table->addFieldInfo('username', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL);
        $table->addFieldInfo('useragent', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL);
        $table->addFieldInfo('token', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL);
        $table->addFieldInfo('confirmtimeout', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL);
        $table->addFieldInfo('expires', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL);
        $table->addFieldInfo('sessionid', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL);
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('userid','instanceid'));
        create_table($table);

368
        $table = new XMLDBTable('usr');
369 370 371 372
        $field = new XMLDBField('authinstance');
        $field->setAttributes(XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, 1);
        add_field($table, $field);
        $index = new XMLDBIndex('usernameuk');
373 374 375 376 377 378 379
        $index->setAttributes(XMLDB_INDEX_UNIQUE, array('username', 'institution'));
        drop_index($table, $index);

        $index = new XMLDBIndex("usernameuk");
        $index->setAttributes(XMLDB_INDEX_UNIQUE, array('username', 'authinstance'));
        add_index($table, $index);

380
        $field = new XMLDBField('sessionid');
381
        $field->setAttributes(XMLDB_TYPE_CHAR, 32, null, null, null, null, null, '');
382
        add_field($table, $field);
383

384 385 386
        $field = new XMLDBField('lastauthinstance');
        $field->setAttributes(XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED);
        add_field($table, $field);
387

388 389 390
        $key   = new XMLDBKey("lastauthinstancefk");
        $key->setAttributes(XMLDB_KEY_FOREIGN, array('lastauthinstance'), 'auth_instance', array('id'));
        add_key($table, $key);
Donal McMullan's avatar
Donal McMullan committed
391 392

        $record = new stdClass();
393 394 395 396 397 398 399 400 401 402 403 404 405

        $record->name            = 'mahara';
        $record->displayname     = 'Mahara';
        $record->xmlrpcserverurl = '/api/xmlrpc/server.php';
        $record->ssolandurl      = '/auth/xmlrpc/land.php';
        insert_record('application',$record);

        $record->name            = 'moodle';
        $record->displayname     = 'Moodle';
        $record->xmlrpcserverurl = '/mnet/xmlrpc/server.php';
        $record->ssolandurl      = '/auth/mnet/land.php';
        insert_record('application',$record);

406 407
    }
    
408 409 410 411
    return $status;

}
?>