Merge branch 'MDL-81084-main' of https://github.com/andrewnicols/moodle
[moodle.git] / cache / locallib.php
blob572e2f8e55cbd7e786c25fb554fc8c8fef5d7678
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
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.
24 * @package core
25 * @category cache
26 * @copyright 2012 Sam Hemelryk
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 defined('MOODLE_INTERNAL') || die();
32 /**
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.
37 * @package core
38 * @category cache
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 {
44 /**
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.
48 * @var bool
50 protected static $creatingconfig = false;
52 /**
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);
62 /**
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() {
70 global $CFG;
71 static $confighash = '';
72 $cachefile = static::get_config_file_path();
73 $directory = dirname($cachefile);
74 if ($directory !== $CFG->dataroot && !file_exists($directory)) {
75 $result = make_writable_directory($directory, false);
76 if (!$result) {
77 throw new cache_exception('ex_configcannotsave', 'cache', '', null, 'Cannot create config directory. Check the permissions on your moodledata directory.');
80 if (!file_exists($directory) || !is_writable($directory)) {
81 throw new cache_exception('ex_configcannotsave', 'cache', '', null, 'Config directory is not writable. Check the permissions on the moodledata/muc directory.');
84 // Prepare a configuration array to store.
85 $configuration = $this->generate_configuration_array();
87 // Prepare the file content.
88 $content = "<?php defined('MOODLE_INTERNAL') || die();\n \$configuration = ".var_export($configuration, true).";";
90 // Do both file content and hash based detection because this might be called
91 // many times within a single request.
92 $hash = sha1($content);
93 if (($hash === $confighash) || (file_exists($cachefile) && $content === file_get_contents($cachefile))) {
94 // Config is unchanged so don't bother locking and writing.
95 $confighash = $hash;
96 return;
99 // We need to create a temporary cache lock instance for use here. Remember we are generating the config file
100 // it doesn't exist and thus we can't use the normal API for this (it'll just try to use config).
101 $lockconf = reset($this->configlocks);
102 if ($lockconf === false) {
103 debugging('Your cache configuration file is out of date and needs to be refreshed.', DEBUG_DEVELOPER);
104 // Use the default
105 $lockconf = array(
106 'name' => 'cachelock_file_default',
107 'type' => 'cachelock_file',
108 'dir' => 'filelocks',
109 'default' => true
112 $factory = cache_factory::instance();
113 $locking = $factory->create_lock_instance($lockconf);
114 if ($locking->lock('configwrite', 'config', true)) {
115 $tempcachefile = "{$cachefile}.tmp";
116 // Its safe to use w mode here because we have already acquired the lock.
117 $handle = fopen($tempcachefile, 'w');
118 fwrite($handle, $content);
119 fflush($handle);
120 fclose($handle);
121 $locking->unlock('configwrite', 'config');
122 @chmod($tempcachefile, $CFG->filepermissions);
123 rename($tempcachefile, $cachefile);
124 // Tell PHP to recompile the script.
125 core_component::invalidate_opcode_php_cache($cachefile);
126 } else {
127 throw new cache_exception('ex_configcannotsave', 'cache', '', null, 'Unable to open the cache config file.');
132 * Generates a configuration array suitable to be written to the config file.
133 * @return array
135 protected function generate_configuration_array() {
136 $configuration = array();
137 $configuration['siteidentifier'] = $this->siteidentifier;
138 $configuration['stores'] = $this->configstores;
139 $configuration['modemappings'] = $this->configmodemappings;
140 $configuration['definitions'] = $this->configdefinitions;
141 $configuration['definitionmappings'] = $this->configdefinitionmappings;
142 $configuration['locks'] = $this->configlocks;
143 return $configuration;
147 * Adds a plugin instance.
149 * This function also calls save so you should redirect immediately, or at least very shortly after
150 * calling this method.
152 * @param string $name The name for the instance (must be unique)
153 * @param string $plugin The name of the plugin.
154 * @param array $configuration The configuration data for the plugin instance.
155 * @return bool
156 * @throws cache_exception
158 public function add_store_instance($name, $plugin, array $configuration = array()) {
159 if (array_key_exists($name, $this->configstores)) {
160 throw new cache_exception('Duplicate name specificed for cache plugin instance. You must provide a unique name.');
162 $class = 'cachestore_'.$plugin;
163 if (!class_exists($class)) {
164 $plugins = core_component::get_plugin_list_with_file('cachestore', 'lib.php');
165 if (!array_key_exists($plugin, $plugins)) {
166 throw new cache_exception('Invalid plugin name specified. The plugin does not exist or is not valid.');
168 $file = $plugins[$plugin];
169 if (file_exists($file)) {
170 require_once($file);
172 if (!class_exists($class)) {
173 throw new cache_exception('Invalid cache plugin specified. The plugin does not contain the required class.');
176 $reflection = new ReflectionClass($class);
177 if (!$reflection->isSubclassOf('cache_store')) {
178 throw new cache_exception('Invalid cache plugin specified. The plugin does not extend the required class.');
180 if (!$class::are_requirements_met()) {
181 throw new cache_exception('Unable to add new cache plugin instance. The requested plugin type is not supported.');
183 $this->configstores[$name] = array(
184 'name' => $name,
185 'plugin' => $plugin,
186 'configuration' => $configuration,
187 'features' => $class::get_supported_features($configuration),
188 'modes' => $class::get_supported_modes($configuration),
189 'mappingsonly' => !empty($configuration['mappingsonly']),
190 'class' => $class,
191 'default' => false
193 if (array_key_exists('lock', $configuration)) {
194 $this->configstores[$name]['lock'] = $configuration['lock'];
195 unset($this->configstores[$name]['configuration']['lock']);
197 // Call instance_created()
198 $store = new $class($name, $this->configstores[$name]['configuration']);
199 $store->instance_created();
201 $this->config_save();
202 return true;
206 * Adds a new lock instance to the config file.
208 * @param string $name The name the user gave the instance. PARAM_ALHPANUMEXT
209 * @param string $plugin The plugin we are creating an instance of.
210 * @param string $configuration Configuration data from the config instance.
211 * @throws cache_exception
213 public function add_lock_instance($name, $plugin, $configuration = array()) {
214 if (array_key_exists($name, $this->configlocks)) {
215 throw new cache_exception('Duplicate name specificed for cache lock instance. You must provide a unique name.');
217 $class = 'cachelock_'.$plugin;
218 if (!class_exists($class)) {
219 $plugins = core_component::get_plugin_list_with_file('cachelock', 'lib.php');
220 if (!array_key_exists($plugin, $plugins)) {
221 throw new cache_exception('Invalid lock name specified. The plugin does not exist or is not valid.');
223 $file = $plugins[$plugin];
224 if (file_exists($file)) {
225 require_once($file);
227 if (!class_exists($class)) {
228 throw new cache_exception('Invalid lock plugin specified. The plugin does not contain the required class.');
231 $reflection = new ReflectionClass($class);
232 if (!$reflection->implementsInterface('cache_lock_interface')) {
233 throw new cache_exception('Invalid lock plugin specified. The plugin does not implement the required interface.');
235 $this->configlocks[$name] = array_merge($configuration, array(
236 'name' => $name,
237 'type' => 'cachelock_'.$plugin,
238 'default' => false
240 $this->config_save();
244 * Deletes a lock instance given its name.
246 * @param string $name The name of the plugin, PARAM_ALPHANUMEXT.
247 * @return bool
248 * @throws cache_exception
250 public function delete_lock_instance($name) {
251 if (!array_key_exists($name, $this->configlocks)) {
252 throw new cache_exception('The requested store does not exist.');
254 if ($this->configlocks[$name]['default']) {
255 throw new cache_exception('You can not delete the default lock.');
257 foreach ($this->configstores as $store) {
258 if (isset($store['lock']) && $store['lock'] === $name) {
259 throw new cache_exception('You cannot delete a cache lock that is being used by a store.');
262 unset($this->configlocks[$name]);
263 $this->config_save();
264 return true;
268 * Sets the mode mappings.
270 * These determine the default caches for the different modes.
271 * This function also calls save so you should redirect immediately, or at least very shortly after
272 * calling this method.
274 * @param array $modemappings
275 * @return bool
276 * @throws cache_exception
278 public function set_mode_mappings(array $modemappings) {
279 $mappings = array(
280 cache_store::MODE_APPLICATION => array(),
281 cache_store::MODE_SESSION => array(),
282 cache_store::MODE_REQUEST => array(),
284 foreach ($modemappings as $mode => $stores) {
285 if (!array_key_exists($mode, $mappings)) {
286 throw new cache_exception('The cache mode for the new mapping does not exist');
288 $sort = 0;
289 foreach ($stores as $store) {
290 if (!array_key_exists($store, $this->configstores)) {
291 throw new cache_exception('The instance name for the new mapping does not exist');
293 if (array_key_exists($store, $mappings[$mode])) {
294 throw new cache_exception('This cache mapping already exists');
296 $mappings[$mode][] = array(
297 'store' => $store,
298 'mode' => $mode,
299 'sort' => $sort++
303 $this->configmodemappings = array_merge(
304 $mappings[cache_store::MODE_APPLICATION],
305 $mappings[cache_store::MODE_SESSION],
306 $mappings[cache_store::MODE_REQUEST]
309 $this->config_save();
310 return true;
314 * Edits a give plugin instance.
316 * The plugin instance is determined by its name, hence you cannot rename plugins.
317 * This function also calls save so you should redirect immediately, or at least very shortly after
318 * calling this method.
320 * @param string $name
321 * @param string $plugin
322 * @param array $configuration
323 * @return bool
324 * @throws cache_exception
326 public function edit_store_instance($name, $plugin, $configuration) {
327 if (!array_key_exists($name, $this->configstores)) {
328 throw new cache_exception('The requested instance does not exist.');
330 $plugins = core_component::get_plugin_list_with_file('cachestore', 'lib.php');
331 if (!array_key_exists($plugin, $plugins)) {
332 throw new cache_exception('Invalid plugin name specified. The plugin either does not exist or is not valid.');
334 $class = 'cachestore_'.$plugin;
335 $file = $plugins[$plugin];
336 if (!class_exists($class)) {
337 if (file_exists($file)) {
338 require_once($file);
340 if (!class_exists($class)) {
341 throw new cache_exception('Invalid cache plugin specified. The plugin does not contain the required class.'.$class);
344 $this->configstores[$name] = array(
345 'name' => $name,
346 'plugin' => $plugin,
347 'configuration' => $configuration,
348 'features' => $class::get_supported_features($configuration),
349 'modes' => $class::get_supported_modes($configuration),
350 'mappingsonly' => !empty($configuration['mappingsonly']),
351 'class' => $class,
352 'default' => $this->configstores[$name]['default'] // Can't change the default.
354 if (array_key_exists('lock', $configuration)) {
355 $this->configstores[$name]['lock'] = $configuration['lock'];
356 unset($this->configstores[$name]['configuration']['lock']);
358 $this->config_save();
359 return true;
363 * Deletes a store instance.
365 * This function also calls save so you should redirect immediately, or at least very shortly after
366 * calling this method.
368 * @param string $name The name of the instance to delete.
369 * @return bool
370 * @throws cache_exception
372 public function delete_store_instance($name) {
373 if (!array_key_exists($name, $this->configstores)) {
374 throw new cache_exception('The requested store does not exist.');
376 if ($this->configstores[$name]['default']) {
377 throw new cache_exception('The can not delete the default stores.');
379 foreach ($this->configmodemappings as $mapping) {
380 if ($mapping['store'] === $name) {
381 throw new cache_exception('You cannot delete a cache store that has mode mappings.');
384 foreach ($this->configdefinitionmappings as $mapping) {
385 if ($mapping['store'] === $name) {
386 throw new cache_exception('You cannot delete a cache store that has definition mappings.');
390 // Call instance_deleted()
391 $class = 'cachestore_'.$this->configstores[$name]['plugin'];
392 $store = new $class($name, $this->configstores[$name]['configuration']);
393 $store->instance_deleted();
395 unset($this->configstores[$name]);
396 $this->config_save();
397 return true;
401 * Creates the default configuration and saves it.
403 * This function calls config_save, however it is safe to continue using it afterwards as this function should only ever
404 * be called when there is no configuration file already.
406 * @param bool $forcesave If set to true then we will forcefully save the default configuration file.
407 * @return true|array Returns true if the default configuration was successfully created.
408 * Returns a configuration array if it could not be saved. This is a bad situation. Check your error logs.
410 public static function create_default_configuration($forcesave = false) {
411 // HACK ALERT.
412 // We probably need to come up with a better way to create the default stores, or at least ensure 100% that the
413 // default store plugins are protected from deletion.
414 $writer = new self;
415 $writer->configstores = self::get_default_stores();
416 $writer->configdefinitions = self::locate_definitions();
417 $writer->configmodemappings = array(
418 array(
419 'mode' => cache_store::MODE_APPLICATION,
420 'store' => 'default_application',
421 'sort' => -1
423 array(
424 'mode' => cache_store::MODE_SESSION,
425 'store' => 'default_session',
426 'sort' => -1
428 array(
429 'mode' => cache_store::MODE_REQUEST,
430 'store' => 'default_request',
431 'sort' => -1
434 $writer->configlocks = array(
435 'default_file_lock' => array(
436 'name' => 'cachelock_file_default',
437 'type' => 'cachelock_file',
438 'dir' => 'filelocks',
439 'default' => true
443 $factory = cache_factory::instance();
444 // We expect the cache to be initialising presently. If its not then something has gone wrong and likely
445 // we are now in a loop.
446 if (!$forcesave && $factory->get_state() !== cache_factory::STATE_INITIALISING) {
447 return $writer->generate_configuration_array();
449 $factory->set_state(cache_factory::STATE_SAVING);
450 $writer->config_save();
451 return true;
455 * Returns an array of default stores for use.
457 * @return array
459 protected static function get_default_stores() {
460 global $CFG;
462 require_once($CFG->dirroot.'/cache/stores/file/lib.php');
463 require_once($CFG->dirroot.'/cache/stores/session/lib.php');
464 require_once($CFG->dirroot.'/cache/stores/static/lib.php');
466 return array(
467 'default_application' => array(
468 'name' => 'default_application',
469 'plugin' => 'file',
470 'configuration' => array(),
471 'features' => cachestore_file::get_supported_features(),
472 'modes' => cachestore_file::get_supported_modes(),
473 'default' => true,
475 'default_session' => array(
476 'name' => 'default_session',
477 'plugin' => 'session',
478 'configuration' => array(),
479 'features' => cachestore_session::get_supported_features(),
480 'modes' => cachestore_session::get_supported_modes(),
481 'default' => true,
483 'default_request' => array(
484 'name' => 'default_request',
485 'plugin' => 'static',
486 'configuration' => array(),
487 'features' => cachestore_static::get_supported_features(),
488 'modes' => cachestore_static::get_supported_modes(),
489 'default' => true,
495 * Updates the default stores within the MUC config file.
497 public static function update_default_config_stores() {
498 $factory = cache_factory::instance();
499 $factory->updating_started();
500 $config = $factory->create_config_instance(true);
501 $config->configstores = array_merge($config->configstores, self::get_default_stores());
502 $config->config_save();
503 $factory->updating_finished();
507 * Updates the definition in the configuration from those found in the cache files.
509 * Calls config_save further down, you should redirect immediately or asap after calling this method.
511 * @param bool $coreonly If set to true only core definitions will be updated.
513 public static function update_definitions($coreonly = false) {
514 $factory = cache_factory::instance();
515 $factory->updating_started();
516 $config = $factory->create_config_instance(true);
517 $config->write_definitions_to_cache(self::locate_definitions($coreonly));
518 $factory->updating_finished();
522 * Locates all of the definition files.
524 * @param bool $coreonly If set to true only core definitions will be updated.
525 * @return array
527 protected static function locate_definitions($coreonly = false) {
528 global $CFG;
530 $files = array();
531 if (file_exists($CFG->dirroot.'/lib/db/caches.php')) {
532 $files['core'] = $CFG->dirroot.'/lib/db/caches.php';
535 if (!$coreonly) {
536 $plugintypes = core_component::get_plugin_types();
537 foreach ($plugintypes as $type => $location) {
538 $plugins = core_component::get_plugin_list_with_file($type, 'db/caches.php');
539 foreach ($plugins as $plugin => $filepath) {
540 $component = clean_param($type.'_'.$plugin, PARAM_COMPONENT); // Standardised plugin name.
541 $files[$component] = $filepath;
546 $definitions = array();
547 foreach ($files as $component => $file) {
548 $filedefs = self::load_caches_file($file);
549 foreach ($filedefs as $area => $definition) {
550 $area = clean_param($area, PARAM_AREA);
551 $id = $component.'/'.$area;
552 $definition['component'] = $component;
553 $definition['area'] = $area;
554 if (array_key_exists($id, $definitions)) {
555 debugging('Error: duplicate cache definition found with id: '.$id, DEBUG_DEVELOPER);
556 continue;
558 $definitions[$id] = $definition;
562 return $definitions;
566 * Writes the updated definitions for the config file.
567 * @param array $definitions
569 private function write_definitions_to_cache(array $definitions) {
571 // Preserve the selected sharing option when updating the definitions.
572 // This is set by the user and should never come from caches.php.
573 foreach ($definitions as $key => $definition) {
574 unset($definitions[$key]['selectedsharingoption']);
575 unset($definitions[$key]['userinputsharingkey']);
576 if (isset($this->configdefinitions[$key]) && isset($this->configdefinitions[$key]['selectedsharingoption'])) {
577 $definitions[$key]['selectedsharingoption'] = $this->configdefinitions[$key]['selectedsharingoption'];
579 if (isset($this->configdefinitions[$key]) && isset($this->configdefinitions[$key]['userinputsharingkey'])) {
580 $definitions[$key]['userinputsharingkey'] = $this->configdefinitions[$key]['userinputsharingkey'];
584 $this->configdefinitions = $definitions;
585 foreach ($this->configdefinitionmappings as $key => $mapping) {
586 if (!array_key_exists($mapping['definition'], $definitions)) {
587 unset($this->configdefinitionmappings[$key]);
590 $this->config_save();
594 * Loads the caches file if it exists.
595 * @param string $file Absolute path to the file.
596 * @return array
598 private static function load_caches_file($file) {
599 if (!file_exists($file)) {
600 return array();
602 $definitions = array();
603 include($file);
604 return $definitions;
608 * Sets the mappings for a given definition.
610 * @param string $definition
611 * @param array $mappings
612 * @throws coding_exception
614 public function set_definition_mappings($definition, $mappings) {
615 if (!array_key_exists($definition, $this->configdefinitions)) {
616 throw new coding_exception('Invalid definition name passed when updating mappings.');
618 foreach ($mappings as $store) {
619 if (!array_key_exists($store, $this->configstores)) {
620 throw new coding_exception('Invalid store name passed when updating definition mappings.');
623 foreach ($this->configdefinitionmappings as $key => $mapping) {
624 if ($mapping['definition'] == $definition) {
625 unset($this->configdefinitionmappings[$key]);
628 $sort = count($mappings);
629 foreach ($mappings as $store) {
630 $this->configdefinitionmappings[] = array(
631 'store' => $store,
632 'definition' => $definition,
633 'sort' => $sort
635 $sort--;
638 $this->config_save();
642 * Update the site identifier stored by the cache API.
644 * @param string $siteidentifier
645 * @return string The new site identifier.
647 public function update_site_identifier($siteidentifier) {
648 $this->siteidentifier = md5((string)$siteidentifier);
649 $this->config_save();
650 return $this->siteidentifier;
654 * Sets the selected sharing options and key for a definition.
656 * @param string $definition The name of the definition to set for.
657 * @param int $sharingoption The sharing option to set.
658 * @param string|null $userinputsharingkey The user input key or null.
659 * @throws coding_exception
661 public function set_definition_sharing($definition, $sharingoption, $userinputsharingkey = null) {
662 if (!array_key_exists($definition, $this->configdefinitions)) {
663 throw new coding_exception('Invalid definition name passed when updating sharing options.');
665 if (!($this->configdefinitions[$definition]['sharingoptions'] & $sharingoption)) {
666 throw new coding_exception('Invalid sharing option passed when updating definition.');
668 $this->configdefinitions[$definition]['selectedsharingoption'] = (int)$sharingoption;
669 if (!empty($userinputsharingkey)) {
670 $this->configdefinitions[$definition]['userinputsharingkey'] = (string)$userinputsharingkey;
672 $this->config_save();