コード例 #1
0
ファイル: upgradelib.php プロジェクト: jtibbetts/moodle
/**
 * This function finds all available blocks and install them
 * into blocks table or do all the upgrade process if newer.
 *
 * @global object
 * @global object
 */
function upgrade_plugins_blocks($startcallback, $endcallback, $verbose) {
    global $CFG, $DB;

    require_once($CFG->dirroot.'/blocks/moodleblock.class.php');

    $blocktitles   = array(); // we do not want duplicate titles

    //Is this a first install
    $first_install = null;

    $blocks = core_component::get_plugin_list('block');

    foreach ($blocks as $blockname=>$fullblock) {

        if (is_null($first_install)) {
            $first_install = ($DB->count_records('block_instances') == 0);
        }

        if ($blockname === 'NEWBLOCK') {   // Someone has unzipped the template, ignore it
            continue;
        }

        $component = clean_param('block_'.$blockname, PARAM_COMPONENT);

        // check block dir is valid name
        if (empty($component)) {
            throw new plugin_defective_exception('block_'.$blockname, 'Invalid plugin directory name.');
        }

        if (!is_readable($fullblock.'/version.php')) {
            throw new plugin_defective_exception('block/'.$blockname, 'Missing version.php file.');
        }
        $plugin = new stdClass();
        $plugin->version = null;
        $plugin->cron    = 0;
        $module = $plugin; // Prevent some notices when module placed in wrong directory.
        include($fullblock.'/version.php');
        unset($module);
        $block = clone($plugin);
        unset($block->version);
        unset($block->component);
        unset($block->dependencies);
        unset($block->release);

        // if plugin tells us it's full name we may check the location
        if (isset($plugin->component)) {
            if ($plugin->component !== $component) {
                throw new plugin_misplaced_exception($plugin->component, null, $fullblock);
            }
        }

        if (empty($plugin->version)) {
            throw new plugin_defective_exception($component, 'Missing block version.');
        }

        if (!empty($plugin->requires)) {
            if ($plugin->requires > $CFG->version) {
                throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires);
            } else if ($plugin->requires < 2010000000) {
                throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.');
            }
        }

        if (!is_readable($fullblock.'/block_'.$blockname.'.php')) {
            throw new plugin_defective_exception('block/'.$blockname, 'Missing main block class file.');
        }
        include_once($fullblock.'/block_'.$blockname.'.php');

        $classname = 'block_'.$blockname;

        if (!class_exists($classname)) {
            throw new plugin_defective_exception($component, 'Can not load main class.');
        }

        $blockobj    = new $classname;   // This is what we'll be testing
        $blocktitle  = $blockobj->get_title();

        // OK, it's as we all hoped. For further tests, the object will do them itself.
        if (!$blockobj->_self_test()) {
            throw new plugin_defective_exception($component, 'Self test failed.');
        }

        $block->name     = $blockname;   // The name MUST match the directory

        $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!

        if (file_exists($fullblock.'/db/install.php')) {
            if (get_config('block_'.$blockname, 'installrunning')) {
                require_once($fullblock.'/db/install.php');
                $recover_install_function = 'xmldb_block_'.$blockname.'_install_recovery';
                if (function_exists($recover_install_function)) {
                    $startcallback($component, true, $verbose);
                    $recover_install_function();
                    unset_config('installrunning', 'block_'.$blockname);
                    // Install various components
                    update_capabilities($component);
                    log_update_descriptions($component);
                    external_update_descriptions($component);
                    events_update_definition($component);
                    \core\task\manager::reset_scheduled_tasks_for_component($component);
                    message_update_providers($component);
                    \core\message\inbound\manager::update_handlers_for_component($component);
                    upgrade_plugin_mnet_functions($component);
                    $endcallback($component, true, $verbose);
                }
            }
        }

        if (empty($installedversion)) { // block not installed yet, so install it
            $conflictblock = array_search($blocktitle, $blocktitles);
            if ($conflictblock !== false) {
                // Duplicate block titles are not allowed, they confuse people
                // AND PHP's associative arrays ;)
                throw new plugin_defective_exception($component, get_string('blocknameconflict', 'error', (object)array('name'=>$block->name, 'conflict'=>$conflictblock)));
            }
            $startcallback($component, true, $verbose);

            if (file_exists($fullblock.'/db/install.xml')) {
                $DB->get_manager()->install_from_xmldb_file($fullblock.'/db/install.xml');
            }
            $block->id = $DB->insert_record('block', $block);
            upgrade_block_savepoint(true, $plugin->version, $block->name, false);

            if (file_exists($fullblock.'/db/install.php')) {
                require_once($fullblock.'/db/install.php');
                // Set installation running flag, we need to recover after exception or error
                set_config('installrunning', 1, 'block_'.$blockname);
                $post_install_function = 'xmldb_block_'.$blockname.'_install';
                $post_install_function();
                unset_config('installrunning', 'block_'.$blockname);
            }

            $blocktitles[$block->name] = $blocktitle;

            // Install various components
            update_capabilities($component);
            log_update_descriptions($component);
            external_update_descriptions($component);
            events_update_definition($component);
            \core\task\manager::reset_scheduled_tasks_for_component($component);
            message_update_providers($component);
            \core\message\inbound\manager::update_handlers_for_component($component);
            upgrade_plugin_mnet_functions($component);

            $endcallback($component, true, $verbose);

        } else if ($installedversion < $plugin->version) {
            $startcallback($component, false, $verbose);

            if (is_readable($fullblock.'/db/upgrade.php')) {
                require_once($fullblock.'/db/upgrade.php');  // defines new upgrading function
                $newupgrade_function = 'xmldb_block_'.$blockname.'_upgrade';
                $result = $newupgrade_function($installedversion, $block);
            } else {
                $result = true;
            }

            $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
            $currblock = $DB->get_record('block', array('name'=>$block->name));
            if ($installedversion < $plugin->version) {
                // store version if not already there
                upgrade_block_savepoint($result, $plugin->version, $block->name, false);
            }

            if ($currblock->cron != $block->cron) {
                // update cron flag if needed
                $DB->set_field('block', 'cron', $block->cron, array('id' => $currblock->id));
            }

            // Upgrade various components
            update_capabilities($component);
            log_update_descriptions($component);
            external_update_descriptions($component);
            events_update_definition($component);
            \core\task\manager::reset_scheduled_tasks_for_component($component);
            message_update_providers($component);
            \core\message\inbound\manager::update_handlers_for_component($component);
            upgrade_plugin_mnet_functions($component);

            $endcallback($component, false, $verbose);

        } else if ($installedversion > $plugin->version) {
            throw new downgrade_exception($component, $installedversion, $plugin->version);
        }
    }


    // Finally, if we are in the first_install of BLOCKS setup frontpage and admin page blocks
    if ($first_install) {
        //Iterate over each course - there should be only site course here now
        if ($courses = $DB->get_records('course')) {
            foreach ($courses as $course) {
                blocks_add_default_course_blocks($course);
            }
        }

        blocks_add_default_system_blocks();
    }
}
コード例 #2
0
ファイル: upgradelib.php プロジェクト: ajv/Offline-Caching
/**
 * This function finds all available blocks and install them
 * into blocks table or do all the upgrade process if newer.
 *
 * @global object
 * @global object
 */
function upgrade_plugins_blocks($startcallback, $endcallback, $verbose)
{
    global $CFG, $DB;
    require_once $CFG->dirroot . '/blocks/moodleblock.class.php';
    $blocktitles = array();
    // we do not want duplicate titles
    //Is this a first install
    $first_install = null;
    $blocks = get_plugin_list('block');
    foreach ($blocks as $blockname => $fullblock) {
        if (is_null($first_install)) {
            $first_install = $DB->count_records('block') == 0;
        }
        if ($blockname == 'NEWBLOCK') {
            // Someone has unzipped the template, ignore it
            continue;
        }
        $component = 'block_' . $blockname;
        if (!is_readable($fullblock . '/block_' . $blockname . '.php')) {
            throw new plugin_defective_exception('block/' . $blockname, 'Missing main block class file.');
        }
        require_once $fullblock . '/block_' . $blockname . '.php';
        $classname = 'block_' . $blockname;
        if (!class_exists($classname)) {
            throw new plugin_defective_exception($component, 'Can not load main class.');
        }
        $blockobj = new $classname();
        // This is what we 'll be testing
        $blocktitle = $blockobj->get_title();
        // OK, it's as we all hoped. For further tests, the object will do them itself.
        if (!$blockobj->_self_test()) {
            throw new plugin_defective_exception($component, 'Self test failed.');
        }
        $block = new object();
        // This may be used to update the db below
        $block->name = $blockname;
        // The name MUST match the directory
        $block->version = $blockobj->get_version();
        $block->cron = !empty($blockobj->cron) ? $blockobj->cron : 0;
        $block->multiple = $blockobj->instance_allow_multiple() ? 1 : 0;
        if (empty($block->version)) {
            throw new plugin_defective_exception($component, 'Missing block version.');
        }
        $currblock = $DB->get_record('block', array('name' => $block->name));
        if (file_exists($fullblock . '/db/install.php')) {
            if (get_config('block_' . $blockname, 'installrunning')) {
                require_once $fullblock . '/db/install.php';
                $recover_install_function = 'xmldb_block_' . $blockname . '_install_recovery';
                if (function_exists($recover_install_function)) {
                    $startcallback($component, true, $verbose);
                    $recover_install_function();
                    unset_config('installrunning', 'block_' . $blockname);
                    // Install various components
                    update_capabilities($component);
                    events_update_definition($component);
                    message_update_providers($component);
                    $endcallback($component, true, $verbose);
                }
            }
        }
        if (empty($currblock->version)) {
            // block not installed yet, so install it
            // If it allows multiples, start with it enabled
            $conflictblock = array_search($blocktitle, $blocktitles);
            if ($conflictblock !== false) {
                // Duplicate block titles are not allowed, they confuse people
                // AND PHP's associative arrays ;)
                throw new plugin_defective_exception($component, get_string('blocknameconflict', '', (object) array('name' => $block->name, 'conflict' => $conflictblock)));
            }
            $startcallback($component, true, $verbose);
            if (file_exists($fullblock . '/db/install.xml')) {
                $DB->get_manager()->install_from_xmldb_file($fullblock . '/db/install.xml');
            }
            $block->id = $DB->insert_record('block', $block);
            if (file_exists($fullblock . '/db/install.php')) {
                require_once $fullblock . '/db/install.php';
                // Set installation running flag, we need to recover after exception or error
                set_config('installrunning', 1, 'block_' . $blockname);
                $post_install_function = 'xmldb_block_' . $blockname . '_install';
                $post_install_function();
                unset_config('installrunning', 'block_' . $blockname);
            }
            $blocktitles[$block->name] = $blocktitle;
            // Install various components
            update_capabilities($component);
            events_update_definition($component);
            message_update_providers($component);
            $endcallback($component, true, $verbose);
        } else {
            if ($currblock->version < $block->version) {
                $startcallback($component, false, $verbose);
                if (is_readable($fullblock . '/db/upgrade.php')) {
                    require_once $fullblock . '/db/upgrade.php';
                    // defines new upgrading function
                    $newupgrade_function = 'xmldb_block_' . $blockname . '_upgrade';
                    $result = $newupgrade_function($currblock->version, $block);
                } else {
                    $result = true;
                }
                $currblock = $DB->get_record('block', array('name' => $block->name));
                if ($currblock->version < $block->version) {
                    // store version if not already there
                    upgrade_block_savepoint($result, $block->version, $block->name, false);
                }
                if ($currblock->cron != $block->cron) {
                    // update cron flag if needed
                    $currblock->cron = $block->cron;
                    $DB->update_record('block', $currblock);
                }
                // Upgrade various componebts
                update_capabilities($component);
                events_update_definition($component);
                message_update_providers($component);
                $endcallback($component, false, $verbose);
            } else {
                if ($currblock->version > $block->version) {
                    throw new downgrade_exception($component, $currblock->version, $block->version);
                }
            }
        }
    }
    // Finally, if we are in the first_install of BLOCKS setup frontpage and admin page blocks
    if ($first_install) {
        //Iterate over each course - there should be only site course here now
        if ($courses = $DB->get_records('course')) {
            foreach ($courses as $course) {
                blocks_add_default_course_blocks($course);
            }
        }
        blocks_add_default_system_blocks();
    }
}