예제 #1
0
/**
 * Upgrade plugins
 * @param string $type The type of plugins that should be updated (e.g. 'enrol', 'qtype')
 * return void
 */
function upgrade_plugins($type, $startcallback, $endcallback, $verbose)
{
    global $CFG, $DB;
    /// special cases
    if ($type === 'mod') {
        return upgrade_plugins_modules($startcallback, $endcallback, $verbose);
    } else {
        if ($type === 'block') {
            return upgrade_plugins_blocks($startcallback, $endcallback, $verbose);
        }
    }
    $plugs = get_plugin_list($type);
    foreach ($plugs as $plug => $fullplug) {
        $component = clean_param($type . '_' . $plug, PARAM_COMPONENT);
        // standardised plugin name
        // check plugin dir is valid name
        if (empty($component)) {
            throw new plugin_defective_exception($type . '_' . $plug, 'Invalid plugin directory name.');
        }
        if (!is_readable($fullplug . '/version.php')) {
            continue;
        }
        $plugin = new stdClass();
        require $fullplug . '/version.php';
        // defines $plugin with version etc
        // if plugin tells us it's full name we may check the location
        if (isset($plugin->component)) {
            if ($plugin->component !== $component) {
                throw new plugin_defective_exception($component, 'Plugin installed in wrong folder.');
            }
        }
        if (empty($plugin->version)) {
            throw new plugin_defective_exception($component, 'Missing version value in version.php');
        }
        $plugin->name = $plug;
        $plugin->fullname = $component;
        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.');
                }
            }
        }
        // try to recover from interrupted install.php if needed
        if (file_exists($fullplug . '/db/install.php')) {
            if (get_config($plugin->fullname, 'installrunning')) {
                require_once $fullplug . '/db/install.php';
                $recover_install_function = 'xmldb_' . $plugin->fullname . '_install_recovery';
                if (function_exists($recover_install_function)) {
                    $startcallback($component, true, $verbose);
                    $recover_install_function();
                    unset_config('installrunning', $plugin->fullname);
                    update_capabilities($component);
                    log_update_descriptions($component);
                    external_update_descriptions($component);
                    events_update_definition($component);
                    message_update_providers($component);
                    if ($type === 'message') {
                        message_update_processors($plug);
                    }
                    upgrade_plugin_mnet_functions($component);
                    $endcallback($component, true, $verbose);
                }
            }
        }
        $installedversion = get_config($plugin->fullname, 'version');
        if (empty($installedversion)) {
            // new installation
            $startcallback($component, true, $verbose);
            /// Install tables if defined
            if (file_exists($fullplug . '/db/install.xml')) {
                $DB->get_manager()->install_from_xmldb_file($fullplug . '/db/install.xml');
            }
            /// store version
            upgrade_plugin_savepoint(true, $plugin->version, $type, $plug, false);
            /// execute post install file
            if (file_exists($fullplug . '/db/install.php')) {
                require_once $fullplug . '/db/install.php';
                set_config('installrunning', 1, $plugin->fullname);
                $post_install_function = 'xmldb_' . $plugin->fullname . '_install';
                $post_install_function();
                unset_config('installrunning', $plugin->fullname);
            }
            /// Install various components
            update_capabilities($component);
            log_update_descriptions($component);
            external_update_descriptions($component);
            events_update_definition($component);
            message_update_providers($component);
            if ($type === 'message') {
                message_update_processors($plug);
            }
            upgrade_plugin_mnet_functions($component);
            purge_all_caches();
            $endcallback($component, true, $verbose);
        } else {
            if ($installedversion < $plugin->version) {
                // upgrade
                /// Run the upgrade function for the plugin.
                $startcallback($component, false, $verbose);
                if (is_readable($fullplug . '/db/upgrade.php')) {
                    require_once $fullplug . '/db/upgrade.php';
                    // defines upgrading function
                    $newupgrade_function = 'xmldb_' . $plugin->fullname . '_upgrade';
                    $result = $newupgrade_function($installedversion);
                } else {
                    $result = true;
                }
                $installedversion = get_config($plugin->fullname, 'version');
                if ($installedversion < $plugin->version) {
                    // store version if not already there
                    upgrade_plugin_savepoint($result, $plugin->version, $type, $plug, false);
                }
                /// Upgrade various components
                update_capabilities($component);
                log_update_descriptions($component);
                external_update_descriptions($component);
                events_update_definition($component);
                message_update_providers($component);
                if ($type === 'message') {
                    message_update_processors($plug);
                }
                upgrade_plugin_mnet_functions($component);
                purge_all_caches();
                $endcallback($component, false, $verbose);
            } else {
                if ($installedversion > $plugin->version) {
                    throw new downgrade_exception($component, $installedversion, $plugin->version);
                }
            }
        }
    }
}
예제 #2
0
/**
 * Upgrade plugins
 * @param string $type The type of plugins that should be updated (e.g. 'enrol', 'qtype')
 * return void
 */
function upgrade_plugins($type, $startcallback, $endcallback, $verbose) {
    global $CFG, $DB;

/// special cases
    if ($type === 'mod') {
        return upgrade_plugins_modules($startcallback, $endcallback, $verbose);
    } else if ($type === 'block') {
        return upgrade_plugins_blocks($startcallback, $endcallback, $verbose);
    }

    $plugs = core_component::get_plugin_list($type);

    foreach ($plugs as $plug=>$fullplug) {
        // Reset time so that it works when installing a large number of plugins
        core_php_time_limit::raise(600);
        $component = clean_param($type.'_'.$plug, PARAM_COMPONENT); // standardised plugin name

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

        if (!is_readable($fullplug.'/version.php')) {
            continue;
        }

        $plugin = new stdClass();
        $plugin->version = null;
        $module = $plugin; // Prevent some notices when plugin placed in wrong directory.
        require($fullplug.'/version.php');  // defines $plugin with version etc
        unset($module);

        // 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, $fullplug);
            }
        }

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

        $plugin->name     = $plug;
        $plugin->fullname = $component;

        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.');
            }
        }

        // try to recover from interrupted install.php if needed
        if (file_exists($fullplug.'/db/install.php')) {
            if (get_config($plugin->fullname, 'installrunning')) {
                require_once($fullplug.'/db/install.php');
                $recover_install_function = 'xmldb_'.$plugin->fullname.'_install_recovery';
                if (function_exists($recover_install_function)) {
                    $startcallback($component, true, $verbose);
                    $recover_install_function();
                    unset_config('installrunning', $plugin->fullname);
                    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);
                    if ($type === 'message') {
                        message_update_processors($plug);
                    }
                    upgrade_plugin_mnet_functions($component);
                    $endcallback($component, true, $verbose);
                }
            }
        }

        $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
        if (empty($installedversion)) { // new installation
            $startcallback($component, true, $verbose);

        /// Install tables if defined
            if (file_exists($fullplug.'/db/install.xml')) {
                $DB->get_manager()->install_from_xmldb_file($fullplug.'/db/install.xml');
            }

        /// store version
            upgrade_plugin_savepoint(true, $plugin->version, $type, $plug, false);

        /// execute post install file
            if (file_exists($fullplug.'/db/install.php')) {
                require_once($fullplug.'/db/install.php');
                set_config('installrunning', 1, $plugin->fullname);
                $post_install_function = 'xmldb_'.$plugin->fullname.'_install';
                $post_install_function();
                unset_config('installrunning', $plugin->fullname);
            }

        /// 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);
            if ($type === 'message') {
                message_update_processors($plug);
            }
            upgrade_plugin_mnet_functions($component);
            $endcallback($component, true, $verbose);

        } else if ($installedversion < $plugin->version) { // upgrade
        /// Run the upgrade function for the plugin.
            $startcallback($component, false, $verbose);

            if (is_readable($fullplug.'/db/upgrade.php')) {
                require_once($fullplug.'/db/upgrade.php');  // defines upgrading function

                $newupgrade_function = 'xmldb_'.$plugin->fullname.'_upgrade';
                $result = $newupgrade_function($installedversion);
            } else {
                $result = true;
            }

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

        /// 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);
            if ($type === 'message') {
                // Ugly hack!
                message_update_processors($plug);
            }
            upgrade_plugin_mnet_functions($component);
            $endcallback($component, false, $verbose);

        } else if ($installedversion > $plugin->version) {
            throw new downgrade_exception($component, $installedversion, $plugin->version);
        }
    }
}
/**
 * Find and check all submodules and load them up or upgrade them if necessary
 *
 * @global object
 * @global object
 */
function vmoodle_upgrade_subplugins_modules($startcallback, $endcallback, $verbose = true)
{
    global $CFG, $DB;
    include $CFG->dirroot . '/local/vmoodle/db/subplugins.php';
    foreach ($subplugins as $type => $subpluginpath) {
        $plugindirs = glob($CFG->dirroot . '/' . $subpluginpath . '/*');
        foreach ($plugindirs as $dir) {
            $plug = basename($dir);
            $fullplug = $dir;
            if ($plug === 'CVS') {
                // Someone has unzipped the template, ignore it.
                continue;
            }
            if ($plug === 'NEWMODULE') {
                // Someone has unzipped the template, ignore it.
                continue;
            }
            // Reset time so that it works when installing a large number of plugins.
            set_time_limit(600);
            $component = clean_param($type . '_' . $plug, PARAM_COMPONENT);
            // standardised plugin name
            // Check plugin dir is valid name.
            if (empty($component)) {
                throw new plugin_defective_exception($type . '_' . $plug, 'Invalid plugin directory name.');
            }
            if (!is_readable($fullplug . '/version.php')) {
                continue;
            }
            $plugin = new stdClass();
            require $fullplug . '/version.php';
            // Defines $plugin with version etc.
            // If plugin tells us it's full name we may check the location.
            if (isset($plugin->component)) {
                if ($plugin->component !== $component) {
                    throw new plugin_defective_exception($component, 'Plugin installed in wrong folder.');
                }
            }
            if (empty($plugin->version)) {
                throw new plugin_defective_exception($component, 'Missing version value in version.php');
            }
            $plugin->name = $plug;
            $plugin->fullname = $component;
            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.');
                    }
                }
            }
            // Try to recover from interrupted install.php if needed.
            if (file_exists($fullplug . '/db/install.php')) {
                if (get_config($plugin->fullname, 'installrunning')) {
                    require_once $fullplug . '/db/install.php';
                    $recover_install_function = 'xmldb_' . $plugin->fullname . '_install_recovery';
                    if (function_exists($recover_install_function)) {
                        $startcallback($component, true, $verbose);
                        $recover_install_function();
                        unset_config('installrunning', $plugin->fullname);
                        update_capabilities($component);
                        log_update_descriptions($component);
                        external_update_descriptions($component);
                        events_update_definition($component);
                        message_update_providers($component);
                        if ($type === 'message') {
                            message_update_processors($plug);
                        }
                        vmoodle_upgrade_plugin_mnet_functions($component, $fullplug);
                        // Fix wrongly twicked paths.
                        if ($rpc_shifted_defines = $DB->get_records_select('mnet_rpc', " xmlrpcpath LIKE 'vmoodleadminset' ", array())) {
                            foreach ($rpc_shifted_defines as $rpc) {
                                $rpc->xmlrpcpath = str_replace('vmoocleadminset', 'local/vmoodle/plugins');
                                $DB->update_record('mnet_rpc', $rpc);
                            }
                        }
                        $endcallback($component, true, $verbose);
                    }
                }
            }
            $installedversion = get_config($plugin->fullname, 'version');
            if (empty($installedversion)) {
                // New installation.
                $startcallback($component, true, $verbose);
                // Install tables if defined.
                if (file_exists($fullplug . '/db/install.xml')) {
                    $DB->get_manager()->install_from_xmldb_file($fullplug . '/db/install.xml');
                }
                // Store version.
                upgrade_plugin_savepoint(true, $plugin->version, $type, $plug, false);
                // Execute post install file.
                if (file_exists($fullplug . '/db/install.php')) {
                    require_once $fullplug . '/db/install.php';
                    set_config('installrunning', 1, $plugin->fullname);
                    $post_install_function = 'xmldb_' . $plugin->fullname . '_install';
                    $post_install_function();
                    unset_config('installrunning', $plugin->fullname);
                }
                // Install various components.
                update_capabilities($component);
                log_update_descriptions($component);
                external_update_descriptions($component);
                events_update_definition($component);
                message_update_providers($component);
                if ($type === 'message') {
                    message_update_processors($plug);
                }
                vmoodle_upgrade_plugin_mnet_functions($component, $fullplug);
                // Fix wrongly twicked paths.
                if ($rpc_shifted_defines = $DB->get_records_select('mnet_rpc', " xmlrpcpath LIKE 'vmoodleadminset' ", array())) {
                    foreach ($rpc_shifted_defines as $rpc) {
                        $rpc->xmlrpcpath = str_replace('vmoocleadminset', 'local/vmoodle/plugins');
                        $DB->update_record('mnet_rpc', $rpc);
                    }
                }
                purge_all_caches();
                $endcallback($component, true, $verbose);
            } else {
                if ($installedversion < $plugin->version) {
                    // Upgrade
                    // Run the upgrade function for the plugin.
                    $startcallback($component, false, $verbose);
                    if (is_readable($fullplug . '/db/upgrade.php')) {
                        require_once $fullplug . '/db/upgrade.php';
                        // Defines upgrading function
                        $newupgrade_function = 'xmldb_' . $plugin->fullname . '_upgrade';
                        $result = $newupgrade_function($installedversion);
                    } else {
                        $result = true;
                    }
                    $installedversion = get_config($plugin->fullname, 'version');
                    if ($installedversion < $plugin->version) {
                        // store version if not already there.
                        upgrade_plugin_savepoint($result, $plugin->version, $type, $plug, false);
                    }
                    // Upgrade various components.
                    update_capabilities($component);
                    log_update_descriptions($component);
                    external_update_descriptions($component);
                    events_update_definition($component);
                    message_update_providers($component);
                    if ($type === 'message') {
                        message_update_processors($plug);
                    }
                    vmoodle_upgrade_plugin_mnet_functions($component, $fullplug);
                    purge_all_caches();
                    $endcallback($component, false, $verbose);
                } else {
                    if ($installedversion > $plugin->version) {
                        throw new downgrade_exception($component, $installedversion, $plugin->version);
                    }
                }
            }
        }
    }
}