2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * The supplementary cache API.
20 * This file is part of Moodle's cache API, affectionately called MUC.
21 * It contains elements of the API that are not required in order to use caching.
22 * Things in here are more in line with administration and management of the cache setup and configuration.
26 * @copyright 2012 Sam Hemelryk
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 defined('MOODLE_INTERNAL') || die();
33 * Cache configuration writer.
35 * This class should only be used when you need to write to the config, all read operations exist within the cache_config.
39 * @copyright 2012 Sam Hemelryk
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class cache_config_writer extends cache_config {
45 * Switch that gets set to true when ever a cache_config_writer instance is saving the cache configuration file.
46 * If this is set to true when save is next called we must avoid the trying to save and instead return the
47 * generated config so that is may be used instead of the file.
50 protected static $creatingconfig = false;
53 * Returns an instance of the configuration writer.
55 * @return cache_config_writer
57 public static function instance() {
58 $factory = cache_factory::instance();
59 return $factory->create_config_instance(true);
63 * Saves the current configuration.
65 * Exceptions within this function are tolerated but must be of type cache_exception.
66 * They are caught during initialisation and written to the error log. This is required in order to avoid
67 * infinite loop situations caused by the cache throwing exceptions during its initialisation.
69 protected function config_save() {
71 $cachefile = self::get_config_file_path();
72 $directory = dirname($cachefile);
73 if ($directory !== $CFG->dataroot && !file_exists($directory)) {
74 $result = make_writable_directory($directory, false);
76 throw new cache_exception('ex_configcannotsave', 'cache', '', null, 'Cannot create config directory. Check the permissions on your moodledata directory.');
79 if (!file_exists($directory) || !is_writable($directory)) {
80 throw new cache_exception('ex_configcannotsave', 'cache', '', null, 'Config directory is not writable. Check the permissions on the moodledata/muc directory.');
83 // Prepare a configuration array to store.
84 $configuration = $this->generate_configuration_array();
86 // Prepare the file content.
87 $content = "<?php defined('MOODLE_INTERNAL') || die();\n \$configuration = ".var_export($configuration, true).";";
89 // We need to create a temporary cache lock instance for use here. Remember we are generating the config file
90 // it doesn't exist and thus we can't use the normal API for this (it'll just try to use config).
91 $lockconf = reset($this->configlocks);
92 if ($lockconf === false) {
93 debugging('Your cache configuration file is out of date and needs to be refreshed.', DEBUG_DEVELOPER);
96 'name' => 'cachelock_file_default',
97 'type' => 'cachelock_file',
102 $factory = cache_factory::instance();
103 $locking = $factory->create_lock_instance($lockconf);
104 if ($locking->lock('configwrite', 'config', true)) {
105 // Its safe to use w mode here because we have already acquired the lock.
106 $handle = fopen($cachefile, 'w');
107 fwrite($handle, $content);
110 $locking->unlock('configwrite', 'config');
112 throw new cache_exception('ex_configcannotsave', 'cache', '', null, 'Unable to open the cache config file.');
117 * Generates a configuration array suitable to be written to the config file.
120 protected function generate_configuration_array() {
121 $configuration = array();
122 $configuration['stores'] = $this->configstores;
123 $configuration['modemappings'] = $this->configmodemappings;
124 $configuration['definitions'] = $this->configdefinitions;
125 $configuration['definitionmappings'] = $this->configdefinitionmappings;
126 $configuration['locks'] = $this->configlocks;
127 return $configuration;
131 * Adds a plugin instance.
133 * This function also calls save so you should redirect immediately, or at least very shortly after
134 * calling this method.
136 * @param string $name The name for the instance (must be unique)
137 * @param string $plugin The name of the plugin.
138 * @param array $configuration The configuration data for the plugin instance.
140 * @throws cache_exception
142 public function add_store_instance($name, $plugin, array $configuration = array()) {
143 if (array_key_exists($name, $this->configstores)) {
144 throw new cache_exception('Duplicate name specificed for cache plugin instance. You must provide a unique name.');
146 $class = 'cachestore_'.$plugin;
147 if (!class_exists($class)) {
148 $plugins = get_plugin_list_with_file('cachestore', 'lib.php');
149 if (!array_key_exists($plugin, $plugins)) {
150 throw new cache_exception('Invalid plugin name specified. The plugin does not exist or is not valid.');
152 $file = $plugins[$plugin];
153 if (file_exists($file)) {
156 if (!class_exists($class)) {
157 throw new cache_exception('Invalid cache plugin specified. The plugin does not contain the required class.');
160 $reflection = new ReflectionClass($class);
161 if (!$reflection->isSubclassOf('cache_store')) {
162 throw new cache_exception('Invalid cache plugin specified. The plugin does not extend the required class.');
164 if (!$class::are_requirements_met()) {
165 throw new cache_exception('Unable to add new cache plugin instance. The requested plugin type is not supported.');
167 $this->configstores[$name] = array(
170 'configuration' => $configuration,
171 'features' => $class::get_supported_features($configuration),
172 'modes' => $class::get_supported_modes($configuration),
173 'mappingsonly' => !empty($configuration['mappingsonly']),
177 if (array_key_exists('lock', $configuration)) {
178 $this->configstores[$name]['lock'] = $configuration['lock'];
179 unset($this->configstores[$name]['configuration']['lock']);
181 // Call instance_created()
182 $store = new $class($name, $this->configstores[$name]['configuration']);
183 $store->instance_created();
185 $this->config_save();
190 * Sets the mode mappings.
192 * These determine the default caches for the different modes.
193 * This function also calls save so you should redirect immediately, or at least very shortly after
194 * calling this method.
196 * @param array $modemappings
198 * @throws cache_exception
200 public function set_mode_mappings(array $modemappings) {
202 cache_store::MODE_APPLICATION => array(),
203 cache_store::MODE_SESSION => array(),
204 cache_store::MODE_REQUEST => array(),
206 foreach ($modemappings as $mode => $stores) {
207 if (!array_key_exists($mode, $mappings)) {
208 throw new cache_exception('The cache mode for the new mapping does not exist');
211 foreach ($stores as $store) {
212 if (!array_key_exists($store, $this->configstores)) {
213 throw new cache_exception('The instance name for the new mapping does not exist');
215 if (array_key_exists($store, $mappings[$mode])) {
216 throw new cache_exception('This cache mapping already exists');
218 $mappings[$mode][] = array(
225 $this->configmodemappings = array_merge(
226 $mappings[cache_store::MODE_APPLICATION],
227 $mappings[cache_store::MODE_SESSION],
228 $mappings[cache_store::MODE_REQUEST]
231 $this->config_save();
236 * Edits a give plugin instance.
238 * The plugin instance is determined by its name, hence you cannot rename plugins.
239 * This function also calls save so you should redirect immediately, or at least very shortly after
240 * calling this method.
242 * @param string $name
243 * @param string $plugin
244 * @param array $configuration
246 * @throws cache_exception
248 public function edit_store_instance($name, $plugin, $configuration) {
249 if (!array_key_exists($name, $this->configstores)) {
250 throw new cache_exception('The requested instance does not exist.');
252 $plugins = get_plugin_list_with_file('cachestore', 'lib.php');
253 if (!array_key_exists($plugin, $plugins)) {
254 throw new cache_exception('Invalid plugin name specified. The plugin either does not exist or is not valid.');
256 $class = 'cachestore_'.$plugin;
257 $file = $plugins[$plugin];
258 if (!class_exists($class)) {
259 if (file_exists($file)) {
262 if (!class_exists($class)) {
263 throw new cache_exception('Invalid cache plugin specified. The plugin does not contain the required class.'.$class);
266 $this->configstores[$name] = array(
269 'configuration' => $configuration,
270 'features' => $class::get_supported_features($configuration),
271 'modes' => $class::get_supported_modes($configuration),
272 'mappingsonly' => !empty($configuration['mappingsonly']),
274 'default' => $this->configstores[$name]['default'] // Can't change the default.
276 if (array_key_exists('lock', $configuration)) {
277 $this->configstores[$name]['lock'] = $configuration['lock'];
278 unset($this->configstores[$name]['configuration']['lock']);
280 $this->config_save();
285 * Deletes a store instance.
287 * This function also calls save so you should redirect immediately, or at least very shortly after
288 * calling this method.
290 * @param string $name The name of the instance to delete.
292 * @throws cache_exception
294 public function delete_store_instance($name) {
295 if (!array_key_exists($name, $this->configstores)) {
296 throw new cache_exception('The requested store does not exist.');
298 if ($this->configstores[$name]['default']) {
299 throw new cache_exception('The can not delete the default stores.');
301 foreach ($this->configmodemappings as $mapping) {
302 if ($mapping['store'] === $name) {
303 throw new cache_exception('You cannot delete a cache store that has mode mappings.');
306 foreach ($this->configdefinitionmappings as $mapping) {
307 if ($mapping['store'] === $name) {
308 throw new cache_exception('You cannot delete a cache store that has definition mappings.');
312 // Call instance_deleted()
313 $class = 'cachestore_'.$this->configstores[$name]['plugin'];
314 $store = new $class($name, $this->configstores[$name]['configuration']);
315 $store->instance_deleted();
317 unset($this->configstores[$name]);
318 $this->config_save();
323 * Creates the default configuration and saves it.
325 * This function calls config_save, however it is safe to continue using it afterwards as this function should only ever
326 * be called when there is no configuration file already.
328 * @return true|array Returns true if the default configuration was successfully created.
329 * Returns a configuration array if it could not be saved. This is a bad situation. Check your error logs.
331 public static function create_default_configuration() {
335 // We probably need to come up with a better way to create the default stores, or at least ensure 100% that the
336 // default store plugins are protected from deletion.
337 require_once($CFG->dirroot.'/cache/stores/file/lib.php');
338 require_once($CFG->dirroot.'/cache/stores/session/lib.php');
339 require_once($CFG->dirroot.'/cache/stores/static/lib.php');
342 $writer->configstores = array(
343 'default_application' => array(
344 'name' => 'default_application',
346 'configuration' => array(),
347 'features' => cachestore_file::get_supported_features(),
348 'modes' => cache_store::MODE_APPLICATION,
351 'default_session' => array(
352 'name' => 'default_session',
353 'plugin' => 'session',
354 'configuration' => array(),
355 'features' => cachestore_session::get_supported_features(),
356 'modes' => cache_store::MODE_SESSION,
359 'default_request' => array(
360 'name' => 'default_request',
361 'plugin' => 'static',
362 'configuration' => array(),
363 'features' => cachestore_static::get_supported_features(),
364 'modes' => cache_store::MODE_REQUEST,
368 $writer->configdefinitions = self::locate_definitions();
369 $writer->configmodemappings = array(
371 'mode' => cache_store::MODE_APPLICATION,
372 'store' => 'default_application',
376 'mode' => cache_store::MODE_SESSION,
377 'store' => 'default_session',
381 'mode' => cache_store::MODE_REQUEST,
382 'store' => 'default_request',
386 $writer->configlocks = array(
387 'default_file_lock' => array(
388 'name' => 'cachelock_file_default',
389 'type' => 'cachelock_file',
390 'dir' => 'filelocks',
395 $factory = cache_factory::instance();
396 // We expect the cache to be initialising presently. If its not then something has gone wrong and likely
397 // we are now in a loop.
398 if ($factory->get_state() !== cache_factory::STATE_INITIALISING) {
399 return $writer->generate_configuration_array();
401 $factory->set_state(cache_factory::STATE_SAVING);
402 $writer->config_save();
407 * Updates the definition in the configuration from those found in the cache files.
409 * Calls config_save further down, you should redirect immediately or asap after calling this method.
411 * @param bool $coreonly If set to true only core definitions will be updated.
413 public static function update_definitions($coreonly = false) {
414 $factory = cache_factory::instance();
415 $factory->updating_started();
416 $config = $factory->create_config_instance(true);
417 $config->write_definitions_to_cache(self::locate_definitions($coreonly));
418 $factory->updating_finished();
422 * Locates all of the definition files.
424 * @param bool $coreonly If set to true only core definitions will be updated.
427 protected static function locate_definitions($coreonly = false) {
431 if (file_exists($CFG->dirroot.'/lib/db/caches.php')) {
432 $files['core'] = $CFG->dirroot.'/lib/db/caches.php';
436 $plugintypes = get_plugin_types();
437 foreach ($plugintypes as $type => $location) {
438 $plugins = get_plugin_list_with_file($type, 'db/caches.php');
439 foreach ($plugins as $plugin => $filepath) {
440 $component = clean_param($type.'_'.$plugin, PARAM_COMPONENT); // Standardised plugin name.
441 $files[$component] = $filepath;
446 $definitions = array();
447 foreach ($files as $component => $file) {
448 $filedefs = self::load_caches_file($file);
449 foreach ($filedefs as $area => $definition) {
450 $area = clean_param($area, PARAM_AREA);
451 $id = $component.'/'.$area;
452 $definition['component'] = $component;
453 $definition['area'] = $area;
454 if (array_key_exists($id, $definitions)) {
455 debugging('Error: duplicate cache definition found with id: '.$id, DEBUG_DEVELOPER);
458 $definitions[$id] = $definition;
466 * Writes the updated definitions for the config file.
467 * @param array $definitions
469 private function write_definitions_to_cache(array $definitions) {
470 $this->configdefinitions = $definitions;
471 foreach ($this->configdefinitionmappings as $key => $mapping) {
472 if (!array_key_exists($mapping['definition'], $definitions)) {
473 unset($this->configdefinitionmappings[$key]);
476 $this->config_save();
480 * Loads the caches file if it exists.
481 * @param string $file Absolute path to the file.
484 private static function load_caches_file($file) {
485 if (!file_exists($file)) {
488 $definitions = array();
494 * Sets the mappings for a given definition.
496 * @param string $definition
497 * @param array $mappings
498 * @throws coding_exception
500 public function set_definition_mappings($definition, $mappings) {
501 if (!array_key_exists($definition, $this->configdefinitions)) {
502 throw new coding_exception('Invalid definition name passed when updating mappings.');
504 foreach ($mappings as $store) {
505 if (!array_key_exists($store, $this->configstores)) {
506 throw new coding_exception('Invalid store name passed when updating definition mappings.');
509 foreach ($this->configdefinitionmappings as $key => $mapping) {
510 if ($mapping['definition'] == $definition) {
511 unset($this->configdefinitionmappings[$key]);
514 $sort = count($mappings);
515 foreach ($mappings as $store) {
516 $this->configdefinitionmappings[] = array(
518 'definition' => $definition,
524 $this->config_save();
530 * A cache helper for administration tasks
534 * @copyright 2012 Sam Hemelryk
535 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
537 abstract class cache_administration_helper extends cache_helper {
540 * Returns an array containing all of the information about stores a renderer needs.
543 public static function get_store_instance_summaries() {
546 $instance = cache_config::instance();
547 $stores = $instance->get_all_stores();
548 foreach ($stores as $name => $details) {
549 $class = $details['class'];
550 $store = new $class($details['name'], $details['configuration']);
553 'plugin' => $details['plugin'],
554 'default' => $details['default'],
555 'isready' => $store->is_ready(),
556 'requirementsmet' => $store->are_requirements_met(),
559 cache_store::MODE_APPLICATION =>
560 ($store->get_supported_modes($return) & cache_store::MODE_APPLICATION) == cache_store::MODE_APPLICATION,
561 cache_store::MODE_SESSION =>
562 ($store->get_supported_modes($return) & cache_store::MODE_SESSION) == cache_store::MODE_SESSION,
563 cache_store::MODE_REQUEST =>
564 ($store->get_supported_modes($return) & cache_store::MODE_REQUEST) == cache_store::MODE_REQUEST,
567 'multipleidentifiers' => $store->supports_multiple_identifiers(),
568 'dataguarantee' => $store->supports_data_guarantee(),
569 'nativettl' => $store->supports_native_ttl(),
570 'nativelocking' => ($store instanceof cache_is_lockable),
571 'keyawareness' => ($store instanceof cache_is_key_aware),
574 if (empty($details['default'])) {
575 $return[$name] = $record;
577 $default[$name] = $record;
583 $return = $return + $default;
585 foreach ($instance->get_definition_mappings() as $mapping) {
586 if (!array_key_exists($mapping['store'], $return)) {
589 $return[$mapping['store']]['mappings']++;
596 * Returns an array of information about plugins, everything a renderer needs.
599 public static function get_store_plugin_summaries() {
601 $plugins = get_plugin_list_with_file('cachestore', 'lib.php', true);
602 foreach ($plugins as $plugin => $path) {
603 $class = 'cachestore_'.$plugin;
604 $return[$plugin] = array(
605 'name' => get_string('pluginname', 'cachestore_'.$plugin),
606 'requirementsmet' => $class::are_requirements_met(),
609 cache_store::MODE_APPLICATION => ($class::get_supported_modes() & cache_store::MODE_APPLICATION),
610 cache_store::MODE_SESSION => ($class::get_supported_modes() & cache_store::MODE_SESSION),
611 cache_store::MODE_REQUEST => ($class::get_supported_modes() & cache_store::MODE_REQUEST),
614 'multipleidentifiers' => ($class::get_supported_features() & cache_store::SUPPORTS_MULTIPLE_IDENTIFIERS),
615 'dataguarantee' => ($class::get_supported_features() & cache_store::SUPPORTS_DATA_GUARANTEE),
616 'nativettl' => ($class::get_supported_features() & cache_store::SUPPORTS_NATIVE_TTL),
617 'nativelocking' => (in_array('cache_is_lockable', class_implements($class))),
618 'keyawareness' => (array_key_exists('cache_is_key_aware', class_implements($class))),
620 'canaddinstance' => ($class::can_add_instance() && $class::are_requirements_met())
624 $instance = cache_config::instance();
625 $stores = $instance->get_all_stores();
626 foreach ($stores as $store) {
627 $plugin = $store['plugin'];
628 if (array_key_exists($plugin, $return)) {
629 $return[$plugin]['instances']++;
637 * Returns an array about the definitions. All the information a renderer needs.
640 public static function get_definition_summaries() {
641 $instance = cache_config::instance();
642 $definitions = $instance->get_definitions();
644 $storenames = array();
645 foreach ($instance->get_all_stores() as $key => $store) {
646 if (!empty($store['default'])) {
647 $storenames[$key] = new lang_string('store_'.$key, 'cache');
651 $modemappings = array();
652 foreach ($instance->get_mode_mappings() as $mapping) {
653 $mode = $mapping['mode'];
654 if (!array_key_exists($mode, $modemappings)) {
655 $modemappings[$mode] = array();
657 if (array_key_exists($mapping['store'], $storenames)) {
658 $modemappings[$mode][] = $storenames[$mapping['store']];
660 $modemappings[$mode][] = $mapping['store'];
664 $definitionmappings = array();
665 foreach ($instance->get_definition_mappings() as $mapping) {
666 $definition = $mapping['definition'];
667 if (!array_key_exists($definition, $definitionmappings)) {
668 $definitionmappings[$definition] = array();
670 if (array_key_exists($mapping['store'], $storenames)) {
671 $definitionmappings[$definition][] = $storenames[$mapping['store']];
673 $definitionmappings[$definition][] = $mapping['store'];
679 foreach ($definitions as $id => $definition) {
682 if (array_key_exists($id, $definitionmappings)) {
683 $mappings = $definitionmappings[$id];
684 } else if (empty($definition['mappingsonly'])) {
685 $mappings = $modemappings[$definition['mode']];
688 $return[$id] = array(
690 'name' => cache_helper::get_definition_name($definition),
691 'mode' => $definition['mode'],
692 'component' => $definition['component'],
693 'area' => $definition['area'],
694 'mappings' => $mappings
701 * Returns all of the actions that can be performed on a definition.
702 * @param context $context
705 public static function get_definition_actions(context $context) {
706 if (has_capability('moodle/site:config', $context)) {
709 'text' => get_string('editmappings', 'cache'),
710 'url' => new moodle_url('/cache/admin.php', array('action' => 'editdefinitionmapping', 'sesskey' => sesskey()))
713 'text' => get_string('purge', 'cache'),
714 'url' => new moodle_url('/cache/admin.php', array('action' => 'purgedefinition', 'sesskey' => sesskey()))
722 * Returns all of the actions that can be performed on a store.
724 * @param string $name The name of the store
725 * @param array $storedetails
728 public static function get_store_instance_actions($name, array $storedetails) {
730 if (has_capability('moodle/site:config', get_system_context())) {
731 $baseurl = new moodle_url('/cache/admin.php', array('store' => $name, 'sesskey' => sesskey()));
732 if (empty($storedetails['default'])) {
734 'text' => get_string('editstore', 'cache'),
735 'url' => new moodle_url($baseurl, array('action' => 'editstore', 'plugin' => $storedetails['plugin']))
738 'text' => get_string('deletestore', 'cache'),
739 'url' => new moodle_url($baseurl, array('action' => 'deletestore'))
743 'text' => get_string('purge', 'cache'),
744 'url' => new moodle_url($baseurl, array('action' => 'purgestore'))
752 * Returns all of the actions that can be performed on a plugin.
754 * @param string $name The name of the plugin
755 * @param array $plugindetails
758 public static function get_store_plugin_actions($name, array $plugindetails) {
760 if (has_capability('moodle/site:config', context_system::instance())) {
761 if (!empty($plugindetails['canaddinstance'])) {
762 $url = new moodle_url('/cache/admin.php', array('action' => 'addstore', 'plugin' => $name, 'sesskey' => sesskey()));
764 'text' => get_string('addinstance', 'cache'),
773 * Returns a form that can be used to add a store instance.
775 * @param string $plugin The plugin to add an instance of
776 * @return cachestore_addinstance_form
777 * @throws coding_exception
779 public static function get_add_store_form($plugin) {
780 global $CFG; // Needed for includes.
781 $plugins = get_plugin_list('cachestore');
782 if (!array_key_exists($plugin, $plugins)) {
783 throw new coding_exception('Invalid cache plugin used when trying to create an edit form.');
785 $plugindir = $plugins[$plugin];
786 $class = 'cachestore_addinstance_form';
787 if (file_exists($plugindir.'/addinstanceform.php')) {
788 require_once($plugindir.'/addinstanceform.php');
789 if (class_exists('cachestore_'.$plugin.'_addinstance_form')) {
790 $class = 'cachestore_'.$plugin.'_addinstance_form';
791 if (!array_key_exists('cachestore_addinstance_form', class_parents($class))) {
792 throw new coding_exception('Cache plugin add instance forms must extend cachestore_addinstance_form');
797 $locks = self::get_possible_locks_for_stores($plugindir, $plugin);
799 $url = new moodle_url('/cache/admin.php', array('action' => 'addstore'));
800 return new $class($url, array('plugin' => $plugin, 'store' => null, 'locks' => $locks));
804 * Returns a form that can be used to edit a store instance.
806 * @param string $plugin
807 * @param string $store
808 * @return cachestore_addinstance_form
809 * @throws coding_exception
811 public static function get_edit_store_form($plugin, $store) {
812 global $CFG; // Needed for includes.
813 $plugins = get_plugin_list('cachestore');
814 if (!array_key_exists($plugin, $plugins)) {
815 throw new coding_exception('Invalid cache plugin used when trying to create an edit form.');
817 $factory = cache_factory::instance();
818 $config = $factory->create_config_instance();
819 $stores = $config->get_all_stores();
820 if (!array_key_exists($store, $stores)) {
821 throw new coding_exception('Invalid store name given when trying to create an edit form.');
823 $plugindir = $plugins[$plugin];
824 $class = 'cachestore_addinstance_form';
825 if (file_exists($plugindir.'/addinstanceform.php')) {
826 require_once($plugindir.'/addinstanceform.php');
827 if (class_exists('cachestore_'.$plugin.'_addinstance_form')) {
828 $class = 'cachestore_'.$plugin.'_addinstance_form';
829 if (!array_key_exists('cachestore_addinstance_form', class_parents($class))) {
830 throw new coding_exception('Cache plugin add instance forms must extend cachestore_addinstance_form');
835 $locks = self::get_possible_locks_for_stores($plugindir, $plugin);
837 $url = new moodle_url('/cache/admin.php', array('action' => 'editstore', 'plugin' => $plugin, 'store' => $store));
838 $editform = new $class($url, array('plugin' => $plugin, 'store' => $store, 'locks' => $locks));
839 // See if the cachestore is going to want to load data for the form.
840 // If it has a customised add instance form then it is going to want to.
841 $storeclass = 'cachestore_'.$plugin;
842 $storedata = $stores[$store];
843 if (array_key_exists('configuration', $storedata) && array_key_exists('cache_is_configurable', class_implements($storeclass))) {
844 $storeclass::config_set_edit_form_data($editform, $storedata['configuration']);
850 * Returns an array of suitable lock instances for use with this plugin, or false if the plugin handles locking itself.
852 * @param string $plugindir
853 * @param string $plugin
854 * @return array|false
856 protected static function get_possible_locks_for_stores($plugindir, $plugin) {
857 global $CFG; // Needed for includes.
858 $supportsnativelocking = false;
859 if (file_exists($plugindir.'/lib.php')) {
860 require_once($plugindir.'/lib.php');
861 $pluginclass = 'cachestore_'.$plugin;
862 if (class_exists($pluginclass)) {
863 $supportsnativelocking = array_key_exists('cache_is_lockable', class_implements($pluginclass));
867 if (!$supportsnativelocking) {
868 $config = cache_config::instance();
870 foreach ($config->get_locks() as $lock => $conf) {
871 if (!empty($conf['default'])) {
872 $name = get_string($lock, 'cache');
876 $locks[$lock] = $name;
886 * Processes the results of the add/edit instance form data for a plugin returning an array of config information suitable to
887 * store in configuration.
889 * @param stdClass $data The mform data.
891 * @throws coding_exception
893 public static function get_store_configuration_from_data(stdClass $data) {
895 $file = $CFG->dirroot.'/cache/stores/'.$data->plugin.'/lib.php';
896 if (!file_exists($file)) {
897 throw new coding_exception('Invalid cache plugin provided. '.$file);
900 $class = 'cachestore_'.$data->plugin;
901 if (!class_exists($class)) {
902 throw new coding_exception('Invalid cache plugin provided.');
904 if (array_key_exists('cache_is_configurable', class_implements($class))) {
905 return $class::config_get_configuration_array($data);
911 * Get an array of stores that are suitable to be used for a given definition.
913 * @param string $component
914 * @param string $area
915 * @return array Array containing 3 elements
916 * 1. An array of currently used stores
917 * 2. An array of suitable stores
918 * 3. An array of default stores
920 public static function get_definition_store_options($component, $area) {
921 $factory = cache_factory::instance();
922 $definition = $factory->create_definition($component, $area);
923 $config = cache_config::instance();
924 $currentstores = $config->get_stores_for_definition($definition);
925 $possiblestores = $config->get_stores($definition->get_mode(), $definition->get_requirements_bin());
928 foreach ($currentstores as $key => $store) {
929 if (!empty($store['default'])) {
931 unset($currentstores[$key]);
934 foreach ($possiblestores as $key => $store) {
935 if ($store['default']) {
936 unset($possiblestores[$key]);
937 $possiblestores[$key] = $store;
940 return array($currentstores, $possiblestores, $defaults);
944 * Get the default stores for all modes.
946 * @return array An array containing sub-arrays, one for each mode.
948 public static function get_default_mode_stores() {
949 $instance = cache_config::instance();
950 $storenames = array();
951 foreach ($instance->get_all_stores() as $key => $store) {
952 if (!empty($store['default'])) {
953 $storenames[$key] = new lang_string('store_'.$key, 'cache');
956 $modemappings = array(
957 cache_store::MODE_APPLICATION => array(),
958 cache_store::MODE_SESSION => array(),
959 cache_store::MODE_REQUEST => array(),
961 foreach ($instance->get_mode_mappings() as $mapping) {
962 $mode = $mapping['mode'];
963 if (!array_key_exists($mode, $modemappings)) {
964 debugging('Unknown mode in cache store mode mappings', DEBUG_DEVELOPER);
967 if (array_key_exists($mapping['store'], $storenames)) {
968 $modemappings[$mode][$mapping['store']] = $storenames[$mapping['store']];
970 $modemappings[$mode][$mapping['store']] = $mapping['store'];
973 return $modemappings;
977 * Returns an array summarising the locks available in the system
979 public static function get_lock_summaries() {
981 $instance = cache_config::instance();
982 $stores = $instance->get_all_stores();
983 foreach ($instance->get_locks() as $lock) {
984 $default = !empty($lock['default']);
986 $name = new lang_string($lock['name'], 'cache');
988 $name = $lock['name'];
991 foreach ($stores as $store) {
992 if (!empty($store['lock']) && $store['lock'] === $lock['name']) {
998 'default' => $default,
1001 $locks[] = $lockdata;