plugin_manager.php 81 KB
Newer Older
2001
2002
2003
2004
2005
2006
2007
2008
     * Subplugins can be cancelled only via their parent plugin, not separately
     * (they are considered as implicit requirements if distributed together
     * with the main package).
     *
     * @param \core\plugininfo\base $plugin
     * @return bool
     */
    public function can_cancel_plugin_upgrade(\core\plugininfo\base $plugin) {
2009
2010
2011
2012
2013
2014
2015
        global $CFG;

        if (!empty($CFG->disableupdateautodeploy)) {
            // Cancelling the plugin upgrade is actually installation of the
            // previously archived version.
            return false;
        }
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030

        if (empty($plugin) or $plugin->is_standard() or $plugin->is_subplugin()
                or !$this->is_plugin_folder_removable($plugin->component)) {
            return false;
        }

        if ($plugin->get_status() === self::PLUGIN_STATUS_UPGRADE) {
            if ($this->get_code_manager()->get_archived_plugin_version($plugin->component, $plugin->versiondb)) {
                return true;
            }
        }

        return false;
    }

2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
    /**
     * Removes the plugin code directory if it is not installed yet.
     *
     * This is intended for the plugins check screen to give the admin a chance
     * to cancel the installation of just unzipped plugin before the database
     * upgrade happens.
     *
     * @param string $component
     */
    public function cancel_plugin_installation($component) {
2041
2042
2043
2044
2045
        global $CFG;

        if (!empty($CFG->disableupdateautodeploy)) {
            return false;
        }
2046
2047
2048

        $plugin = $this->get_plugin_info($component);

2049
        if ($this->can_cancel_plugin_installation($plugin)) {
2050
            $this->remove_plugin_folder($plugin);
2051
2052
        }

2053
        return false;
2054
2055
2056
    }

    /**
2057
2058
2059
     * Returns plugins, the installation of which can be cancelled.
     *
     * @return array [(string)component] => (\core\plugininfo\base)plugin
2060
     */
2061
    public function list_cancellable_installations() {
2062
2063
2064
2065
2066
        global $CFG;

        if (!empty($CFG->disableupdateautodeploy)) {
            return array();
        }
2067

2068
        $cancellable = array();
2069
2070
        foreach ($this->get_plugins() as $type => $plugins) {
            foreach ($plugins as $plugin) {
2071
                if ($this->can_cancel_plugin_installation($plugin)) {
2072
                    $cancellable[$plugin->component] = $plugin;
2073
2074
2075
                }
            }
        }
2076
2077

        return $cancellable;
2078
2079
    }

2080
2081
2082
2083
2084
2085
2086
    /**
     * Archive the current on-disk plugin code.
     *
     * @param \core\plugiinfo\base $plugin
     * @return bool
     */
    public function archive_plugin_version(\core\plugininfo\base $plugin) {
2087
        return $this->get_code_manager()->archive_plugin_version($plugin->rootdir, $plugin->component, $plugin->versiondisk);
2088
2089
    }

2090
2091
2092
2093
2094
2095
    /**
     * Returns list of all archives that can be installed to cancel the plugin upgrade.
     *
     * @return array [(string)component] => {(string)->component, (string)->zipfilepath}
     */
    public function list_restorable_archives() {
2096
2097
2098
2099
2100
        global $CFG;

        if (!empty($CFG->disableupdateautodeploy)) {
            return false;
        }
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117

        $codeman = $this->get_code_manager();
        $restorable = array();
        foreach ($this->get_plugins() as $type => $plugins) {
            foreach ($plugins as $plugin) {
                if ($this->can_cancel_plugin_upgrade($plugin)) {
                    $restorable[$plugin->component] = (object)array(
                        'component' => $plugin->component,
                        'zipfilepath' => $codeman->get_archived_plugin_version($plugin->component, $plugin->versiondb)
                    );
                }
            }
        }

        return $restorable;
    }

2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
    /**
     * Reorders plugin types into a sequence to be displayed
     *
     * For technical reasons, plugin types returned by {@link core_component::get_plugin_types()} are
     * in a certain order that does not need to fit the expected order for the display.
     * Particularly, activity modules should be displayed first as they represent the
     * real heart of Moodle. They should be followed by other plugin types that are
     * used to build the courses (as that is what one expects from LMS). After that,
     * other supportive plugin types follow.
     *
     * @param array $types associative array
     * @return array same array with altered order of items
     */
    protected function reorder_plugin_types(array $types) {
        $fix = array('mod' => $types['mod']);
        foreach (core_component::get_plugin_list('mod') as $plugin => $fulldir) {
            if (!$subtypes = core_component::get_subplugins('mod_'.$plugin)) {
                continue;
            }
            foreach ($subtypes as $subtype => $ignored) {
                $fix[$subtype] = $types[$subtype];
            }
        }

        $fix['mod']        = $types['mod'];
        $fix['block']      = $types['block'];
        $fix['qtype']      = $types['qtype'];
        $fix['qbehaviour'] = $types['qbehaviour'];
        $fix['qformat']    = $types['qformat'];
        $fix['filter']     = $types['filter'];

        $fix['editor']     = $types['editor'];
        foreach (core_component::get_plugin_list('editor') as $plugin => $fulldir) {
            if (!$subtypes = core_component::get_subplugins('editor_'.$plugin)) {
                continue;
            }
            foreach ($subtypes as $subtype => $ignored) {
                $fix[$subtype] = $types[$subtype];
            }
        }

        $fix['enrol'] = $types['enrol'];
        $fix['auth']  = $types['auth'];
        $fix['tool']  = $types['tool'];
        foreach (core_component::get_plugin_list('tool') as $plugin => $fulldir) {
            if (!$subtypes = core_component::get_subplugins('tool_'.$plugin)) {
                continue;
            }
            foreach ($subtypes as $subtype => $ignored) {
                $fix[$subtype] = $types[$subtype];
            }
        }

        foreach ($types as $type => $path) {
            if (!isset($fix[$type])) {
                $fix[$type] = $path;
            }
        }
        return $fix;
    }

    /**
     * Check if the given directory can be removed by the web server process.
     *
     * This recursively checks that the given directory and all its contents
     * it writable.
     *
     * @param string $fullpath
     * @return boolean
     */
2188
    public function is_directory_removable($fullpath) {
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235

        if (!is_writable($fullpath)) {
            return false;
        }

        if (is_dir($fullpath)) {
            $handle = opendir($fullpath);
        } else {
            return false;
        }

        $result = true;

        while ($filename = readdir($handle)) {

            if ($filename === '.' or $filename === '..') {
                continue;
            }

            $subfilepath = $fullpath.'/'.$filename;

            if (is_dir($subfilepath)) {
                $result = $result && $this->is_directory_removable($subfilepath);

            } else {
                $result = $result && is_writable($subfilepath);
            }
        }

        closedir($handle);

        return $result;
    }

    /**
     * Helper method that implements common uninstall prerequisites
     *
     * @param \core\plugininfo\base $pluginfo
     * @return bool
     */
    protected function common_uninstall_check(\core\plugininfo\base $pluginfo) {

        if (!$pluginfo->is_uninstall_allowed()) {
            // The plugin's plugininfo class declares it should not be uninstalled.
            return false;
        }

2236
        if ($pluginfo->get_status() === static::PLUGIN_STATUS_NEW) {
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
            // The plugin is not installed. It should be either installed or removed from the disk.
            // Relying on this temporary state may be tricky.
            return false;
        }

        if (method_exists($pluginfo, 'get_uninstall_url') and is_null($pluginfo->get_uninstall_url())) {
            // Backwards compatibility.
            debugging('\core\plugininfo\base subclasses should use is_uninstall_allowed() instead of returning null in get_uninstall_url()',
                DEBUG_DEVELOPER);
            return false;
        }

        return true;
    }
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264

    /**
     * Returns a code_manager instance to be used for the plugins code operations.
     *
     * @return \core\update\code_manager
     */
    protected function get_code_manager() {

        if ($this->codemanager === null) {
            $this->codemanager = new \core\update\code_manager();
        }

        return $this->codemanager;
    }
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278

    /**
     * Returns a client for https://download.moodle.org/api/
     *
     * @return \core\update\api
     */
    protected function get_update_api_client() {

        if ($this->updateapiclient === null) {
            $this->updateapiclient = \core\update\api::client();
        }

        return $this->updateapiclient;
    }
2279
}
For faster browsing, not all history is shown. View entire blame