~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to lib/upgradelib.php

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
98
98
}
99
99
 
100
100
/**
 
101
 * Misplaced plugin exception.
 
102
 *
 
103
 * Note: this should be used only from the upgrade/admin code.
 
104
 *
101
105
 * @package    core
102
106
 * @subpackage upgrade
103
107
 * @copyright  2009 Petr Skoda {@link http://skodak.org}
104
108
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
105
109
 */
106
110
class plugin_misplaced_exception extends moodle_exception {
107
 
    function __construct($component, $expected, $current) {
 
111
    /**
 
112
     * Constructor.
 
113
     * @param string $component the component from version.php
 
114
     * @param string $expected expected directory, null means calculate
 
115
     * @param string $current plugin directory path
 
116
     */
 
117
    public function __construct($component, $expected, $current) {
108
118
        global $CFG;
 
119
        if (empty($expected)) {
 
120
            list($type, $plugin) = core_component::normalize_component($component);
 
121
            $plugintypes = core_component::get_plugin_types();
 
122
            if (isset($plugintypes[$type])) {
 
123
                $expected = $plugintypes[$type] . '/' . $plugin;
 
124
            }
 
125
        }
 
126
        if (strpos($expected, '$CFG->dirroot') !== 0) {
 
127
            $expected = str_replace($CFG->dirroot, '$CFG->dirroot', $expected);
 
128
        }
 
129
        if (strpos($current, '$CFG->dirroot') !== 0) {
 
130
            $current = str_replace($CFG->dirroot, '$CFG->dirroot', $current);
 
131
        }
109
132
        $a = new stdClass();
110
133
        $a->component = $component;
111
134
        $a->expected  = $expected;
222
245
function upgrade_mod_savepoint($result, $version, $modname, $allowabort=true) {
223
246
    global $DB;
224
247
 
 
248
    $component = 'mod_'.$modname;
 
249
 
225
250
    if (!$result) {
226
 
        throw new upgrade_exception("mod_$modname", $version);
 
251
        throw new upgrade_exception($component, $version);
227
252
    }
228
253
 
 
254
    $dbversion = $DB->get_field('config_plugins', 'value', array('plugin'=>$component, 'name'=>'version'));
 
255
 
229
256
    if (!$module = $DB->get_record('modules', array('name'=>$modname))) {
230
257
        print_error('modulenotexist', 'debug', '', $modname);
231
258
    }
232
259
 
233
 
    if ($module->version >= $version) {
 
260
    if ($dbversion >= $version) {
234
261
        // something really wrong is going on in upgrade script
235
 
        throw new downgrade_exception("mod_$modname", $module->version, $version);
 
262
        throw new downgrade_exception($component, $dbversion, $version);
236
263
    }
237
 
    $module->version = $version;
238
 
    $DB->update_record('modules', $module);
239
 
    upgrade_log(UPGRADE_LOG_NORMAL, "mod_$modname", 'Upgrade savepoint reached');
 
264
    set_config('version', $version, $component);
 
265
 
 
266
    upgrade_log(UPGRADE_LOG_NORMAL, $component, 'Upgrade savepoint reached');
240
267
 
241
268
    // reset upgrade timeout to default
242
269
    upgrade_set_timeout();
262
289
function upgrade_block_savepoint($result, $version, $blockname, $allowabort=true) {
263
290
    global $DB;
264
291
 
 
292
    $component = 'block_'.$blockname;
 
293
 
265
294
    if (!$result) {
266
 
        throw new upgrade_exception("block_$blockname", $version);
 
295
        throw new upgrade_exception($component, $version);
267
296
    }
268
297
 
 
298
    $dbversion = $DB->get_field('config_plugins', 'value', array('plugin'=>$component, 'name'=>'version'));
 
299
 
269
300
    if (!$block = $DB->get_record('block', array('name'=>$blockname))) {
270
301
        print_error('blocknotexist', 'debug', '', $blockname);
271
302
    }
272
303
 
273
 
    if ($block->version >= $version) {
 
304
    if ($dbversion >= $version) {
274
305
        // something really wrong is going on in upgrade script
275
 
        throw new downgrade_exception("block_$blockname", $block->version, $version);
 
306
        throw new downgrade_exception($component, $dbversion, $version);
276
307
    }
277
 
    $block->version = $version;
278
 
    $DB->update_record('block', $block);
279
 
    upgrade_log(UPGRADE_LOG_NORMAL, "block_$blockname", 'Upgrade savepoint reached');
 
308
    set_config('version', $version, $component);
 
309
 
 
310
    upgrade_log(UPGRADE_LOG_NORMAL, $component, 'Upgrade savepoint reached');
280
311
 
281
312
    // reset upgrade timeout to default
282
313
    upgrade_set_timeout();
301
332
 * @return void
302
333
 */
303
334
function upgrade_plugin_savepoint($result, $version, $type, $plugin, $allowabort=true) {
 
335
    global $DB;
 
336
 
304
337
    $component = $type.'_'.$plugin;
305
338
 
306
339
    if (!$result) {
307
340
        throw new upgrade_exception($component, $version);
308
341
    }
309
342
 
310
 
    $installedversion = get_config($component, 'version');
311
 
    if ($installedversion >= $version) {
 
343
    $dbversion = $DB->get_field('config_plugins', 'value', array('plugin'=>$component, 'name'=>'version'));
 
344
 
 
345
    if ($dbversion >= $version) {
312
346
        // Something really wrong is going on in the upgrade script
313
 
        throw new downgrade_exception($component, $installedversion, $version);
 
347
        throw new downgrade_exception($component, $dbversion, $version);
314
348
    }
315
349
    set_config('version', $version, $component);
316
350
    upgrade_log(UPGRADE_LOG_NORMAL, $component, 'Upgrade savepoint reached');
337
371
    global $CFG;
338
372
 
339
373
    $someexamplesofremovedfiles = array(
 
374
        // removed in 2.6dev
 
375
        '/admin/block.php',
 
376
        '/admin/oacleanup.php',
340
377
        // removed in 2.5dev
341
378
        '/backup/lib.php',
342
379
        '/backup/bb/README.txt',
383
420
        return upgrade_plugins_blocks($startcallback, $endcallback, $verbose);
384
421
    }
385
422
 
386
 
    $plugs = get_plugin_list($type);
 
423
    $plugs = core_component::get_plugin_list($type);
387
424
 
388
425
    foreach ($plugs as $plug=>$fullplug) {
389
426
        // Reset time so that it works when installing a large number of plugins
400
437
        }
401
438
 
402
439
        $plugin = new stdClass();
403
 
        $module = new stdClass(); // Prevent some notices when plugin placed in wrong directory.
 
440
        $plugin->version = null;
 
441
        $module = $plugin; // Prevent some notices when plugin placed in wrong directory.
404
442
        require($fullplug.'/version.php');  // defines $plugin with version etc
405
 
 
406
 
        if (!isset($plugin->version) and isset($module->version)) {
407
 
            $plugin = $module;
408
 
        }
 
443
        unset($module);
409
444
 
410
445
        // if plugin tells us it's full name we may check the location
411
446
        if (isset($plugin->component)) {
412
447
            if ($plugin->component !== $component) {
413
 
                $current = str_replace($CFG->dirroot, '$CFG->dirroot', $fullplug);
414
 
                $expected = str_replace($CFG->dirroot, '$CFG->dirroot', get_component_directory($plugin->component));
415
 
                throw new plugin_misplaced_exception($component, $expected, $current);
 
448
                throw new plugin_misplaced_exception($plugin->component, null, $fullplug);
416
449
            }
417
450
        }
418
451
 
423
456
        $plugin->name     = $plug;
424
457
        $plugin->fullname = $component;
425
458
 
426
 
 
427
459
        if (!empty($plugin->requires)) {
428
460
            if ($plugin->requires > $CFG->version) {
429
461
                throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires);
455
487
            }
456
488
        }
457
489
 
458
 
        $installedversion = get_config($plugin->fullname, 'version');
 
490
        $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
459
491
        if (empty($installedversion)) { // new installation
460
492
            $startcallback($component, true, $verbose);
461
493
 
486
518
                message_update_processors($plug);
487
519
            }
488
520
            upgrade_plugin_mnet_functions($component);
489
 
            cache_helper::purge_all(true);
490
 
            purge_all_caches();
491
521
            $endcallback($component, true, $verbose);
492
522
 
493
523
        } else if ($installedversion < $plugin->version) { // upgrade
503
533
                $result = true;
504
534
            }
505
535
 
506
 
            $installedversion = get_config($plugin->fullname, 'version');
 
536
            $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
507
537
            if ($installedversion < $plugin->version) {
508
538
                // store version if not already there
509
539
                upgrade_plugin_savepoint($result, $plugin->version, $type, $plug, false);
516
546
            events_update_definition($component);
517
547
            message_update_providers($component);
518
548
            if ($type === 'message') {
 
549
                // Ugly hack!
519
550
                message_update_processors($plug);
520
551
            }
521
552
            upgrade_plugin_mnet_functions($component);
522
 
            cache_helper::purge_all(true);
523
 
            purge_all_caches();
524
553
            $endcallback($component, false, $verbose);
525
554
 
526
555
        } else if ($installedversion > $plugin->version) {
538
567
function upgrade_plugins_modules($startcallback, $endcallback, $verbose) {
539
568
    global $CFG, $DB;
540
569
 
541
 
    $mods = get_plugin_list('mod');
 
570
    $mods = core_component::get_plugin_list('mod');
542
571
 
543
572
    foreach ($mods as $mod=>$fullmod) {
544
573
 
557
586
            throw new plugin_defective_exception($component, 'Missing version.php');
558
587
        }
559
588
 
560
 
        $module = new stdClass();
561
 
        $plugin = new stdClass(); // Prevent some notices when plugin placed in wrong directory.
562
 
        require($fullmod .'/version.php');  // defines $module with version etc
563
 
 
564
 
        if (!isset($module->version) and isset($plugin->version)) {
565
 
            $module = $plugin;
566
 
        }
 
589
        $plugin = new stdClass();
 
590
        $plugin->version = null;
 
591
        $module = $plugin;
 
592
        require($fullmod .'/version.php');  // Defines $module/$plugin with version etc.
 
593
        $plugin = clone($module);
 
594
        unset($module->version);
 
595
        unset($module->component);
 
596
        unset($module->dependencies);
 
597
        unset($module->release);
567
598
 
568
599
        // if plugin tells us it's full name we may check the location
569
 
        if (isset($module->component)) {
570
 
            if ($module->component !== $component) {
571
 
                $current = str_replace($CFG->dirroot, '$CFG->dirroot', $fullmod);
572
 
                $expected = str_replace($CFG->dirroot, '$CFG->dirroot', get_component_directory($module->component));
573
 
                throw new plugin_misplaced_exception($component, $expected, $current);
 
600
        if (isset($plugin->component)) {
 
601
            if ($plugin->component !== $component) {
 
602
                throw new plugin_misplaced_exception($plugin->component, null, $fullmod);
574
603
            }
575
604
        }
576
605
 
577
 
        if (empty($module->version)) {
578
 
            if (isset($module->version)) {
579
 
                // Version is empty but is set - it means its value is 0 or ''. Let us skip such module.
580
 
                // This is intended for developers so they can work on the early stages of the module.
581
 
                continue;
582
 
            }
 
606
        if (empty($plugin->version)) {
 
607
            // Version must be always set now!
583
608
            throw new plugin_defective_exception($component, 'Missing version value in version.php');
584
609
        }
585
610
 
586
 
        if (!empty($module->requires)) {
587
 
            if ($module->requires > $CFG->version) {
588
 
                throw new upgrade_requires_exception($component, $module->version, $CFG->version, $module->requires);
589
 
            } else if ($module->requires < 2010000000) {
 
611
        if (!empty($plugin->requires)) {
 
612
            if ($plugin->requires > $CFG->version) {
 
613
                throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires);
 
614
            } else if ($plugin->requires < 2010000000) {
590
615
                throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.');
591
616
            }
592
617
        }
602
627
 
603
628
        $module->name = $mod;   // The name MUST match the directory
604
629
 
605
 
        $currmodule = $DB->get_record('modules', array('name'=>$module->name));
 
630
        $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
606
631
 
607
632
        if (file_exists($fullmod.'/db/install.php')) {
608
633
            if (get_config($module->name, 'installrunning')) {
624
649
            }
625
650
        }
626
651
 
627
 
        if (empty($currmodule->version)) {
 
652
        if (empty($installedversion)) {
628
653
            $startcallback($component, true, $verbose);
629
654
 
630
655
        /// Execute install.xml (XMLDB) - must be present in all modules
632
657
 
633
658
        /// Add record into modules table - may be needed in install.php already
634
659
            $module->id = $DB->insert_record('modules', $module);
 
660
            upgrade_mod_savepoint(true, $plugin->version, $module->name, false);
635
661
 
636
662
        /// Post installation hook - optional
637
663
            if (file_exists("$fullmod/db/install.php")) {
651
677
            message_update_providers($component);
652
678
            upgrade_plugin_mnet_functions($component);
653
679
 
654
 
            purge_all_caches();
655
680
            $endcallback($component, true, $verbose);
656
681
 
657
 
        } else if ($currmodule->version < $module->version) {
 
682
        } else if ($installedversion < $plugin->version) {
658
683
        /// If versions say that we need to upgrade but no upgrade files are available, notify and continue
659
684
            $startcallback($component, false, $verbose);
660
685
 
661
686
            if (is_readable($fullmod.'/db/upgrade.php')) {
662
687
                require_once($fullmod.'/db/upgrade.php');  // defines new upgrading function
663
688
                $newupgrade_function = 'xmldb_'.$module->name.'_upgrade';
664
 
                $result = $newupgrade_function($currmodule->version, $module);
 
689
                $result = $newupgrade_function($installedversion, $module);
665
690
            } else {
666
691
                $result = true;
667
692
            }
668
693
 
 
694
            $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
669
695
            $currmodule = $DB->get_record('modules', array('name'=>$module->name));
670
 
            if ($currmodule->version < $module->version) {
 
696
            if ($installedversion < $plugin->version) {
671
697
                // store version if not already there
672
 
                upgrade_mod_savepoint($result, $module->version, $mod, false);
 
698
                upgrade_mod_savepoint($result, $plugin->version, $mod, false);
673
699
            }
674
700
 
675
701
            // update cron flag if needed
685
711
            message_update_providers($component);
686
712
            upgrade_plugin_mnet_functions($component);
687
713
 
688
 
            purge_all_caches();
689
 
 
690
714
            $endcallback($component, false, $verbose);
691
715
 
692
 
        } else if ($currmodule->version > $module->version) {
693
 
            throw new downgrade_exception($component, $currmodule->version, $module->version);
 
716
        } else if ($installedversion > $plugin->version) {
 
717
            throw new downgrade_exception($component, $installedversion, $plugin->version);
694
718
        }
695
719
    }
696
720
}
713
737
    //Is this a first install
714
738
    $first_install = null;
715
739
 
716
 
    $blocks = get_plugin_list('block');
 
740
    $blocks = core_component::get_plugin_list('block');
717
741
 
718
742
    foreach ($blocks as $blockname=>$fullblock) {
719
743
 
736
760
            throw new plugin_defective_exception('block/'.$blockname, 'Missing version.php file.');
737
761
        }
738
762
        $plugin = new stdClass();
739
 
        $module = new stdClass(); // Prevent some notices when module placed in wrong directory.
740
 
        $plugin->version = NULL;
 
763
        $plugin->version = null;
741
764
        $plugin->cron    = 0;
 
765
        $module = $plugin; // Prevent some notices when module placed in wrong directory.
742
766
        include($fullblock.'/version.php');
743
 
        if (!isset($plugin->version) and isset($module->version)) {
744
 
            $plugin = $module;
745
 
        }
746
 
        $block = $plugin;
 
767
        unset($module);
 
768
        $block = clone($plugin);
 
769
        unset($block->version);
 
770
        unset($block->component);
 
771
        unset($block->dependencies);
 
772
        unset($block->release);
747
773
 
748
774
        // if plugin tells us it's full name we may check the location
749
 
        if (isset($block->component)) {
750
 
            if ($block->component !== $component) {
751
 
                $current = str_replace($CFG->dirroot, '$CFG->dirroot', $fullblock);
752
 
                $expected = str_replace($CFG->dirroot, '$CFG->dirroot', get_component_directory($block->component));
753
 
                throw new plugin_misplaced_exception($component, $expected, $current);
 
775
        if (isset($plugin->component)) {
 
776
            if ($plugin->component !== $component) {
 
777
                throw new plugin_misplaced_exception($plugin->component, null, $fullblock);
754
778
            }
755
779
        }
756
780
 
 
781
        if (empty($plugin->version)) {
 
782
            throw new plugin_defective_exception($component, 'Missing block version.');
 
783
        }
 
784
 
757
785
        if (!empty($plugin->requires)) {
758
786
            if ($plugin->requires > $CFG->version) {
759
787
                throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires);
783
811
 
784
812
        $block->name     = $blockname;   // The name MUST match the directory
785
813
 
786
 
        if (empty($block->version)) {
787
 
            throw new plugin_defective_exception($component, 'Missing block version.');
788
 
        }
789
 
 
790
 
        $currblock = $DB->get_record('block', array('name'=>$block->name));
 
814
        $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
791
815
 
792
816
        if (file_exists($fullblock.'/db/install.php')) {
793
817
            if (get_config('block_'.$blockname, 'installrunning')) {
809
833
            }
810
834
        }
811
835
 
812
 
        if (empty($currblock->version)) { // block not installed yet, so install it
 
836
        if (empty($installedversion)) { // block not installed yet, so install it
813
837
            $conflictblock = array_search($blocktitle, $blocktitles);
814
838
            if ($conflictblock !== false) {
815
839
                // Duplicate block titles are not allowed, they confuse people
822
846
                $DB->get_manager()->install_from_xmldb_file($fullblock.'/db/install.xml');
823
847
            }
824
848
            $block->id = $DB->insert_record('block', $block);
 
849
            upgrade_block_savepoint(true, $plugin->version, $block->name, false);
825
850
 
826
851
            if (file_exists($fullblock.'/db/install.php')) {
827
852
                require_once($fullblock.'/db/install.php');
842
867
            message_update_providers($component);
843
868
            upgrade_plugin_mnet_functions($component);
844
869
 
845
 
            purge_all_caches();
846
870
            $endcallback($component, true, $verbose);
847
871
 
848
 
        } else if ($currblock->version < $block->version) {
 
872
        } else if ($installedversion < $plugin->version) {
849
873
            $startcallback($component, false, $verbose);
850
874
 
851
875
            if (is_readable($fullblock.'/db/upgrade.php')) {
852
876
                require_once($fullblock.'/db/upgrade.php');  // defines new upgrading function
853
877
                $newupgrade_function = 'xmldb_block_'.$blockname.'_upgrade';
854
 
                $result = $newupgrade_function($currblock->version, $block);
 
878
                $result = $newupgrade_function($installedversion, $block);
855
879
            } else {
856
880
                $result = true;
857
881
            }
858
882
 
 
883
            $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
859
884
            $currblock = $DB->get_record('block', array('name'=>$block->name));
860
 
            if ($currblock->version < $block->version) {
 
885
            if ($installedversion < $plugin->version) {
861
886
                // store version if not already there
862
 
                upgrade_block_savepoint($result, $block->version, $block->name, false);
 
887
                upgrade_block_savepoint($result, $plugin->version, $block->name, false);
863
888
            }
864
889
 
865
890
            if ($currblock->cron != $block->cron) {
875
900
            message_update_providers($component);
876
901
            upgrade_plugin_mnet_functions($component);
877
902
 
878
 
            purge_all_caches();
879
903
            $endcallback($component, false, $verbose);
880
904
 
881
 
        } else if ($currblock->version > $block->version) {
882
 
            throw new downgrade_exception($component, $currblock->version, $block->version);
 
905
        } else if ($installedversion > $plugin->version) {
 
906
            throw new downgrade_exception($component, $installedversion, $plugin->version);
883
907
        }
884
908
    }
885
909
 
907
931
function log_update_descriptions($component) {
908
932
    global $DB;
909
933
 
910
 
    $defpath = get_component_directory($component).'/db/log.php';
 
934
    $defpath = core_component::get_component_directory($component).'/db/log.php';
911
935
 
912
936
    if (!file_exists($defpath)) {
913
937
        $DB->delete_records('log_display', array('component'=>$component));
964
988
function external_update_descriptions($component) {
965
989
    global $DB, $CFG;
966
990
 
967
 
    $defpath = get_component_directory($component).'/db/services.php';
 
991
    $defpath = core_component::get_component_directory($component).'/db/services.php';
968
992
 
969
993
    if (!file_exists($defpath)) {
970
994
        require_once($CFG->dirroot.'/lib/externallib.php');
1043
1067
        $service['requiredcapability'] = empty($service['requiredcapability']) ? null : $service['requiredcapability'];
1044
1068
        $service['restrictedusers'] = !isset($service['restrictedusers']) ? 1 : $service['restrictedusers'];
1045
1069
        $service['downloadfiles'] = !isset($service['downloadfiles']) ? 0 : $service['downloadfiles'];
 
1070
        $service['uploadfiles'] = !isset($service['uploadfiles']) ? 0 : $service['uploadfiles'];
1046
1071
        $service['shortname'] = !isset($service['shortname']) ? null : $service['shortname'];
1047
1072
 
1048
1073
        $update = false;
1058
1083
            $dbservice->downloadfiles = $service['downloadfiles'];
1059
1084
            $update = true;
1060
1085
        }
 
1086
        if ($dbservice->uploadfiles != $service['uploadfiles']) {
 
1087
            $dbservice->uploadfiles = $service['uploadfiles'];
 
1088
            $update = true;
 
1089
        }
1061
1090
        //if shortname is not a PARAM_ALPHANUMEXT, fail (tested here for service update and creation)
1062
1091
        if (isset($service['shortname']) and
1063
1092
                (clean_param($service['shortname'], PARAM_ALPHANUMEXT) != $service['shortname'])) {
1112
1141
        $dbservice->requiredcapability = empty($service['requiredcapability']) ? null : $service['requiredcapability'];
1113
1142
        $dbservice->restrictedusers    = !isset($service['restrictedusers']) ? 1 : $service['restrictedusers'];
1114
1143
        $dbservice->downloadfiles      = !isset($service['downloadfiles']) ? 0 : $service['downloadfiles'];
 
1144
        $dbservice->uploadfiles        = !isset($service['uploadfiles']) ? 0 : $service['uploadfiles'];
1115
1145
        $dbservice->shortname          = !isset($service['shortname']) ? null : $service['shortname'];
1116
1146
        $dbservice->component          = $component;
1117
1147
        $dbservice->timecreated        = time();
1140
1170
    upgrade_log(UPGRADE_LOG_ERROR, $plugin, 'Exception: ' . get_class($ex), $info->message, $info->backtrace);
1141
1171
 
1142
1172
    // Always turn on debugging - admins need to know what is going on
1143
 
    $CFG->debug = DEBUG_DEVELOPER;
 
1173
    set_debugging(DEBUG_DEVELOPER, true);
1144
1174
 
1145
1175
    default_exception_handler($ex, true, $plugin);
1146
1176
}
1162
1192
        $plugin = 'core';
1163
1193
    }
1164
1194
 
1165
 
    list($plugintype, $pluginname) = normalize_component($plugin);
 
1195
    list($plugintype, $pluginname) = core_component::normalize_component($plugin);
1166
1196
    $component = is_null($pluginname) ? $plugintype : $plugintype . '_' . $pluginname;
1167
1197
 
1168
1198
    $backtrace = format_backtrace($backtrace, true);
1179
1209
        include("$CFG->dirroot/version.php");
1180
1210
        $targetversion = $version;
1181
1211
 
1182
 
    } else if ($plugintype === 'mod') {
1183
 
        try {
1184
 
            $currentversion = $DB->get_field('modules', 'version', array('name'=>$pluginname));
1185
 
            $currentversion = ($currentversion === false) ? null : $currentversion;
1186
 
        } catch (Exception $ignored) {
1187
 
        }
1188
 
        $cd = get_component_directory($component);
1189
 
        if (file_exists("$cd/version.php")) {
1190
 
            $module = new stdClass();
1191
 
            $module->version = null;
1192
 
            include("$cd/version.php");
1193
 
            $targetversion = $module->version;
1194
 
        }
1195
 
 
1196
 
    } else if ($plugintype === 'block') {
1197
 
        try {
1198
 
            if ($block = $DB->get_record('block', array('name'=>$pluginname))) {
1199
 
                $currentversion = $block->version;
1200
 
            }
1201
 
        } catch (Exception $ignored) {
1202
 
        }
1203
 
        $cd = get_component_directory($component);
1204
 
        if (file_exists("$cd/version.php")) {
1205
 
            $plugin = new stdClass();
1206
 
            $plugin->version = null;
1207
 
            include("$cd/version.php");
1208
 
            $targetversion = $plugin->version;
1209
 
        }
1210
 
 
1211
1212
    } else {
1212
1213
        $pluginversion = get_config($component, 'version');
1213
1214
        if (!empty($pluginversion)) {
1214
1215
            $currentversion = $pluginversion;
1215
1216
        }
1216
 
        $cd = get_component_directory($component);
 
1217
        $cd = core_component::get_component_directory($component);
1217
1218
        if (file_exists("$cd/version.php")) {
1218
1219
            $plugin = new stdClass();
1219
1220
            $plugin->version = null;
 
1221
            $module = $plugin;
1220
1222
            include("$cd/version.php");
1221
1223
            $targetversion = $plugin->version;
1222
1224
        }
1272
1274
        }
1273
1275
 
1274
1276
        ignore_user_abort(true);
1275
 
        register_shutdown_function('upgrade_finished_handler');
 
1277
        core_shutdown_manager::register_function('upgrade_finished_handler');
1276
1278
        upgrade_setup_debug(true);
1277
1279
        set_config('upgraderunning', time()+300);
1278
1280
        $started = true;
1465
1467
    global $CFG, $DB;
1466
1468
 
1467
1469
    // We can not call purge_all_caches() yet, make sure the temp and cache dirs exist and are empty.
 
1470
    remove_dir($CFG->cachedir.'', true);
1468
1471
    make_cache_directory('', true);
1469
 
    remove_dir($CFG->cachedir.'', true);
 
1472
 
 
1473
    remove_dir($CFG->localcachedir.'', true);
 
1474
    make_localcache_directory('', true);
 
1475
 
 
1476
    remove_dir($CFG->tempdir.'', true);
1470
1477
    make_temp_directory('', true);
1471
 
    remove_dir($CFG->tempdir.'', true);
 
1478
 
 
1479
    remove_dir($CFG->dataroot.'/muc', true);
1472
1480
    make_writable_directory($CFG->dataroot.'/muc', true);
1473
 
    remove_dir($CFG->dataroot.'/muc', true);
1474
1481
 
1475
1482
    try {
1476
1483
        set_time_limit(600);
1512
1519
 * @return void, may throw exception
1513
1520
 */
1514
1521
function upgrade_core($version, $verbose) {
1515
 
    global $CFG;
 
1522
    global $CFG, $SITE, $DB, $COURSE;
1516
1523
 
1517
1524
    raise_memory_limit(MEMORY_EXTRA);
1518
1525
 
1519
1526
    require_once($CFG->libdir.'/db/upgrade.php');    // Defines upgrades
1520
1527
 
1521
1528
    try {
1522
 
        // Reset caches before any output
 
1529
        // Reset caches before any output.
 
1530
        cache_helper::purge_all(true);
1523
1531
        purge_all_caches();
1524
 
        cache_helper::purge_all(true);
1525
1532
 
1526
1533
        // Upgrade current language pack if we can
1527
1534
        upgrade_language_pack();
1543
1550
            upgrade_main_savepoint($result, $version, false);
1544
1551
        }
1545
1552
 
 
1553
        // In case structure of 'course' table has been changed and we forgot to update $SITE, re-read it from db.
 
1554
        $SITE = $DB->get_record('course', array('id' => $SITE->id));
 
1555
        $COURSE = clone($SITE);
 
1556
 
1546
1557
        // perform all other component upgrade routines
1547
1558
        update_capabilities('moodle');
1548
1559
        log_update_descriptions('moodle');
1553
1564
        cache_helper::update_definitions(true);
1554
1565
 
1555
1566
        // Purge caches again, just to be sure we arn't holding onto old stuff now.
 
1567
        cache_helper::purge_all(true);
1556
1568
        purge_all_caches();
1557
 
        cache_helper::purge_all(true);
1558
1569
 
1559
1570
        // Clean up contexts - more and more stuff depends on existence of paths and contexts
1560
1571
        context_helper::cleanup_instances();
1581
1592
 
1582
1593
    // upgrade all plugins types
1583
1594
    try {
1584
 
        $plugintypes = get_plugin_types();
 
1595
        // Reset caches before any output.
 
1596
        cache_helper::purge_all(true);
 
1597
        purge_all_caches();
 
1598
 
 
1599
        $plugintypes = core_component::get_plugin_types();
1585
1600
        foreach ($plugintypes as $type=>$location) {
1586
1601
            upgrade_plugins($type, 'print_upgrade_part_start', 'print_upgrade_part_end', $verbose);
1587
1602
        }
1588
1603
        // Update cache definitions. Involves scanning each plugin for any changes.
1589
1604
        cache_helper::update_definitions();
 
1605
        // Mark the site as upgraded.
 
1606
        set_config('allversionshash', core_component::get_all_versions_hash());
 
1607
 
 
1608
        // Purge caches again, just to be sure we arn't holding onto old stuff now.
 
1609
        cache_helper::purge_all(true);
 
1610
        purge_all_caches();
 
1611
 
1590
1612
    } catch (Exception $ex) {
1591
1613
        upgrade_handle_exception($ex);
1592
1614
    }
1626
1648
function upgrade_plugin_mnet_functions($component) {
1627
1649
    global $DB, $CFG;
1628
1650
 
1629
 
    list($type, $plugin) = normalize_component($component);
1630
 
    $path = get_plugin_directory($type, $plugin);
 
1651
    list($type, $plugin) = core_component::normalize_component($component);
 
1652
    $path = core_component::get_plugin_directory($type, $plugin);
1631
1653
 
1632
1654
    $publishes = array();
1633
1655
    $subscribes = array();
1973
1995
        return;
1974
1996
    }
1975
1997
 
1976
 
    require_once($CFG->libdir.'/textlib.class.php');
1977
1998
    require_once($CFG->dirroot.'/backup/util/includes/backup_includes.php');
1978
 
    $backupword = str_replace(' ', '_', textlib::strtolower(get_string('backupfilename')));
 
1999
    $backupword = str_replace(' ', '_', core_text::strtolower(get_string('backupfilename')));
1979
2000
    $backupword = trim(clean_filename($backupword), '_');
1980
2001
    $filename = $backupword . '-' . backup::FORMAT_MOODLE . '-' . backup::TYPE_1COURSE . '-';
1981
2002
    $regex = '#^'.preg_quote($filename, '#').'.*\.mbz$#';
2026
2047
        $newname = $filename . $bcinfo->original_course_id . '-';
2027
2048
        if ($useshortname) {
2028
2049
            $shortname = str_replace(' ', '_', $bcinfo->original_course_shortname);
2029
 
            $shortname = textlib::strtolower(trim(clean_filename($shortname), '_'));
 
2050
            $shortname = core_text::strtolower(trim(clean_filename($shortname), '_'));
2030
2051
            $newname .= $shortname . '-';
2031
2052
        }
2032
2053
 
2033
2054
        $backupdateformat = str_replace(' ', '_', get_string('backupnameformat', 'langconfig'));
2034
2055
        $date = userdate($bcinfo->backup_date, $backupdateformat, 99, false);
2035
 
        $date = textlib::strtolower(trim(clean_filename($date), '_'));
 
2056
        $date = core_text::strtolower(trim(clean_filename($date), '_'));
2036
2057
        $newname .= $date;
2037
2058
 
2038
2059
        if (isset($bcinfo->root_settings['users']) && !$bcinfo->root_settings['users']) {
2049
2070
        @rename($dir . '/' . $file, $dir . '/' . $newname);
2050
2071
    }
2051
2072
}
 
2073
 
 
2074
/**
 
2075
 * Detect duplicate grade item sortorders and resort the
 
2076
 * items to remove them.
 
2077
 */
 
2078
function upgrade_grade_item_fix_sortorder() {
 
2079
    global $DB;
 
2080
 
 
2081
    // The simple way to fix these sortorder duplicates would be simply to resort each
 
2082
    // affected course. But in order to reduce the impact of this upgrade step we're trying
 
2083
    // to do it more efficiently by doing a series of update statements rather than updating
 
2084
    // every single grade item in affected courses.
 
2085
 
 
2086
    $sql = "SELECT DISTINCT g1.courseid
 
2087
              FROM {grade_items} g1
 
2088
              JOIN {grade_items} g2 ON g1.courseid = g2.courseid
 
2089
             WHERE g1.sortorder = g2.sortorder AND g1.id != g2.id
 
2090
             ORDER BY g1.courseid ASC";
 
2091
    foreach ($DB->get_fieldset_sql($sql) as $courseid) {
 
2092
        $transaction = $DB->start_delegated_transaction();
 
2093
        $items = $DB->get_records('grade_items', array('courseid' => $courseid), '', 'id, sortorder, sortorder AS oldsort');
 
2094
 
 
2095
        // Get all duplicates in course order, highest sort order, and higest id first so that we can make space at the
 
2096
        // bottom higher end of the sort orders and work down by id.
 
2097
        $sql = "SELECT DISTINCT g1.id, g1.sortorder
 
2098
                FROM {grade_items} g1
 
2099
                JOIN {grade_items} g2 ON g1.courseid = g2.courseid
 
2100
                WHERE g1.sortorder = g2.sortorder AND g1.id != g2.id AND g1.courseid = :courseid
 
2101
                ORDER BY g1.sortorder DESC, g1.id DESC";
 
2102
 
 
2103
        // This is the O(N*N) like the database version we're replacing, but at least the constants are a billion times smaller...
 
2104
        foreach ($DB->get_records_sql($sql, array('courseid' => $courseid)) as $duplicate) {
 
2105
            foreach ($items as $item) {
 
2106
                if ($item->sortorder > $duplicate->sortorder || ($item->sortorder == $duplicate->sortorder && $item->id > $duplicate->id)) {
 
2107
                    $item->sortorder += 1;
 
2108
                }
 
2109
            }
 
2110
        }
 
2111
        foreach ($items as $item) {
 
2112
            if ($item->sortorder != $item->oldsort) {
 
2113
                $DB->update_record('grade_items', array('id' => $item->id, 'sortorder' => $item->sortorder));
 
2114
            }
 
2115
        }
 
2116
 
 
2117
        $transaction->allow_commit();
 
2118
    }
 
2119
}
 
2120
 
 
2121
/**
 
2122
 * Detect file areas with missing root directory records and add them.
 
2123
 */
 
2124
function upgrade_fix_missing_root_folders() {
 
2125
    global $DB, $USER;
 
2126
 
 
2127
    $transaction = $DB->start_delegated_transaction();
 
2128
 
 
2129
    $sql = "SELECT contextid, component, filearea, itemid
 
2130
              FROM {files}
 
2131
             WHERE (component <> 'user' OR filearea <> 'draft')
 
2132
          GROUP BY contextid, component, filearea, itemid
 
2133
            HAVING MAX(CASE WHEN filename = '.' AND filepath = '/' THEN 1 ELSE 0 END) = 0";
 
2134
 
 
2135
    $rs = $DB->get_recordset_sql($sql);
 
2136
    $defaults = array('filepath' => '/',
 
2137
        'filename' => '.',
 
2138
        'userid' => 0, // Don't rely on any particular user for these system records.
 
2139
        'filesize' => 0,
 
2140
        'timecreated' => time(),
 
2141
        'timemodified' => time(),
 
2142
        'contenthash' => sha1(''));
 
2143
    foreach ($rs as $r) {
 
2144
        $pathhash = sha1("/$r->contextid/$r->component/$r->filearea/$r->itemid/.");
 
2145
        $DB->insert_record('files', (array)$r + $defaults +
 
2146
            array('pathnamehash' => $pathhash));
 
2147
    }
 
2148
    $rs->close();
 
2149
    $transaction->allow_commit();
 
2150
}