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 * Cache administration helper.
20 * This file is part of Moodle's cache API, affectionately called MUC.
21 * It contains the components that are requried in order to use caching.
25 * @author Peter Burnett <peterburnett@catalyst-au.net>
26 * @copyright 2020 Catalyst IT
27 * @copyright 2012 Sam Hemelryk
28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 defined('MOODLE_INTERNAL') ||
die();
34 use cache_helper
, cache_store
, cache_config
, cache_factory
, cache_definition
;
37 * Administration helper base class.
39 * Defines abstract methods for a subclass to define the admin page.
43 * @author Peter Burnett <peterburnett@catalyst-au.net>
44 * @copyright 2020 Catalyst IT
45 * @copyright 2012 Sam Hemelryk
46 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
48 abstract class administration_helper
extends cache_helper
{
51 * Returns an array containing all of the information about stores a renderer needs.
54 public static function get_store_instance_summaries(): array {
57 $instance = \cache_config
::instance();
58 $stores = $instance->get_all_stores();
59 $locks = $instance->get_locks();
60 foreach ($stores as $name => $details) {
61 $class = $details['class'];
63 if ($class::are_requirements_met()) {
64 $store = new $class($details['name'], $details['configuration']);
66 $lock = (isset($details['lock'])) ?
$locks[$details['lock']] : $instance->get_default_lock();
69 'plugin' => $details['plugin'],
70 'default' => $details['default'],
71 'isready' => $store ?
$store->is_ready() : false,
72 'requirementsmet' => $class::are_requirements_met(),
76 cache_store
::MODE_APPLICATION
=>
77 ($class::get_supported_modes($return) & cache_store
::MODE_APPLICATION
) == cache_store
::MODE_APPLICATION
,
78 cache_store
::MODE_SESSION
=>
79 ($class::get_supported_modes($return) & cache_store
::MODE_SESSION
) == cache_store
::MODE_SESSION
,
80 cache_store
::MODE_REQUEST
=>
81 ($class::get_supported_modes($return) & cache_store
::MODE_REQUEST
) == cache_store
::MODE_REQUEST
,
84 'multipleidentifiers' => $store ?
$store->supports_multiple_identifiers() : false,
85 'dataguarantee' => $store ?
$store->supports_data_guarantee() : false,
86 'nativettl' => $store ?
$store->supports_native_ttl() : false,
87 'nativelocking' => ($store instanceof \cache_is_lockable
),
88 'keyawareness' => ($store instanceof \cache_is_key_aware
),
89 'searchable' => ($store instanceof \cache_is_searchable
)
91 'warnings' => $store ?
$store->get_warnings() : array()
93 if (empty($details['default'])) {
94 $return[$name] = $record;
96 $default[$name] = $record;
102 $return = $return +
$default;
104 $mappings = $instance->get_definition_mappings();
105 foreach ($mappings as $mapping) {
106 if (!array_key_exists($mapping['store'], $return)) {
109 $return[$mapping['store']]['mappings']++
;
112 // Now get all definitions, and if not mapped, increment the defaults for the mode.
113 $modemappings = $instance->get_mode_mappings();
114 foreach ($instance->get_definitions() as $definition) {
115 // Construct the definition name to search for.
116 $defname = $definition['component'] . '/' . $definition['area'];
117 // Skip if definition is already mapped.
118 if (array_search($defname, array_column($mappings, 'definition')) !== false) {
122 $mode = $definition['mode'];
123 // Get the store name of the default mapping from the mode.
124 $index = array_search($mode, array_column($modemappings, 'mode'));
125 $store = $modemappings[$index]['store'];
126 $return[$store]['mappings']++
;
133 * Returns an array of information about plugins, everything a renderer needs.
135 * @return array for each store, an array containing various information about each store.
136 * See the code below for details
138 public static function get_store_plugin_summaries(): array {
140 $plugins = \core_component
::get_plugin_list_with_file('cachestore', 'lib.php', true);
141 foreach ($plugins as $plugin => $path) {
142 $class = 'cachestore_'.$plugin;
143 $return[$plugin] = array(
144 'name' => get_string('pluginname', 'cachestore_'.$plugin),
145 'requirementsmet' => $class::are_requirements_met(),
148 cache_store
::MODE_APPLICATION
=> ($class::get_supported_modes() & cache_store
::MODE_APPLICATION
),
149 cache_store
::MODE_SESSION
=> ($class::get_supported_modes() & cache_store
::MODE_SESSION
),
150 cache_store
::MODE_REQUEST
=> ($class::get_supported_modes() & cache_store
::MODE_REQUEST
),
153 'multipleidentifiers' => ($class::get_supported_features() & cache_store
::SUPPORTS_MULTIPLE_IDENTIFIERS
),
154 'dataguarantee' => ($class::get_supported_features() & cache_store
::SUPPORTS_DATA_GUARANTEE
),
155 'nativettl' => ($class::get_supported_features() & cache_store
::SUPPORTS_NATIVE_TTL
),
156 'nativelocking' => (in_array('cache_is_lockable', class_implements($class))),
157 'keyawareness' => (array_key_exists('cache_is_key_aware', class_implements($class))),
159 'canaddinstance' => ($class::can_add_instance() && $class::are_requirements_met())
163 $instance = cache_config
::instance();
164 $stores = $instance->get_all_stores();
165 foreach ($stores as $store) {
166 $plugin = $store['plugin'];
167 if (array_key_exists($plugin, $return)) {
168 $return[$plugin]['instances']++
;
176 * Returns an array about the definitions. All the information a renderer needs.
178 * @return array for each store, an array containing various information about each store.
179 * See the code below for details
181 public static function get_definition_summaries(): array {
182 $factory = cache_factory
::instance();
183 $config = $factory->create_config_instance();
184 $storenames = array();
185 foreach ($config->get_all_stores() as $key => $store) {
186 if (!empty($store['default'])) {
187 $storenames[$key] = new \
lang_string('store_'.$key, 'cache');
189 $storenames[$store['name']] = $store['name'];
192 /* @var cache_definition[] $definitions */
195 foreach ($config->get_definitions() as $key => $definition) {
196 $definitions[$key] = cache_definition
::load($definition['component'].'/'.$definition['area'], $definition);
198 foreach ($definitions as $id => $definition) {
200 foreach (cache_helper
::get_stores_suitable_for_definition($definition) as $store) {
201 $mappings[] = $storenames[$store->my_name()];
203 $return[$id] = array(
205 'name' => $definition->get_name(),
206 'mode' => $definition->get_mode(),
207 'component' => $definition->get_component(),
208 'area' => $definition->get_area(),
209 'mappings' => $mappings,
210 'canuselocalstore' => $definition->can_use_localstore(),
211 'sharingoptions' => self
::get_definition_sharing_options($definition->get_sharing_options(), false),
212 'selectedsharingoption' => self
::get_definition_sharing_options($definition->get_selected_sharing_option(), true),
213 'userinputsharingkey' => $definition->get_user_input_sharing_key()
220 * Get the default stores for all modes.
222 * @return array An array containing sub-arrays, one for each mode.
224 public static function get_default_mode_stores(): array {
226 $instance = cache_config
::instance();
227 $adequatestores = cache_helper
::get_stores_suitable_for_mode_default();
228 $icon = new \
pix_icon('i/warning', new \
lang_string('inadequatestoreformapping', 'cache'));
229 $storenames = array();
230 foreach ($instance->get_all_stores() as $key => $store) {
231 if (!empty($store['default'])) {
232 $storenames[$key] = new \
lang_string('store_'.$key, 'cache');
235 $modemappings = array(
236 cache_store
::MODE_APPLICATION
=> array(),
237 cache_store
::MODE_SESSION
=> array(),
238 cache_store
::MODE_REQUEST
=> array(),
240 foreach ($instance->get_mode_mappings() as $mapping) {
241 $mode = $mapping['mode'];
242 if (!array_key_exists($mode, $modemappings)) {
243 debugging('Unknown mode in cache store mode mappings', DEBUG_DEVELOPER
);
246 if (array_key_exists($mapping['store'], $storenames)) {
247 $modemappings[$mode][$mapping['store']] = $storenames[$mapping['store']];
249 $modemappings[$mode][$mapping['store']] = $mapping['store'];
251 if (!array_key_exists($mapping['store'], $adequatestores)) {
252 $modemappings[$mode][$mapping['store']] = $modemappings[$mode][$mapping['store']].' '.$OUTPUT->render($icon);
255 return $modemappings;
259 * Returns an array summarising the locks available in the system.
261 * @return array array of lock summaries.
263 public static function get_lock_summaries(): array {
265 $instance = cache_config
::instance();
266 $stores = $instance->get_all_stores();
267 foreach ($instance->get_locks() as $lock) {
268 $default = !empty($lock['default']);
270 $name = new \
lang_string($lock['name'], 'cache');
272 $name = $lock['name'];
275 foreach ($stores as $store) {
276 if (!empty($store['lock']) && $store['lock'] === $lock['name']) {
282 'default' => $default,
284 'type' => get_string('pluginname', $lock['type'])
286 $locks[$lock['name']] = $lockdata;
292 * Given a sharing option hash this function returns an array of strings that can be used to describe it.
294 * @param int $sharingoption The sharing option hash to get strings for.
295 * @param bool $isselectedoptions Set to true if the strings will be used to view the selected options.
296 * @return array An array of lang_string's.
298 public static function get_definition_sharing_options(int $sharingoption, bool $isselectedoptions = true): array {
300 $prefix = ($isselectedoptions) ?
'sharingselected' : 'sharing';
301 if ($sharingoption & cache_definition
::SHARING_ALL
) {
302 $options[cache_definition
::SHARING_ALL
] = new \
lang_string($prefix.'_all', 'cache');
304 if ($sharingoption & cache_definition
::SHARING_SITEID
) {
305 $options[cache_definition
::SHARING_SITEID
] = new \
lang_string($prefix.'_siteid', 'cache');
307 if ($sharingoption & cache_definition
::SHARING_VERSION
) {
308 $options[cache_definition
::SHARING_VERSION
] = new \
lang_string($prefix.'_version', 'cache');
310 if ($sharingoption & cache_definition
::SHARING_INPUT
) {
311 $options[cache_definition
::SHARING_INPUT
] = new \
lang_string($prefix.'_input', 'cache');
317 * Get an array of stores that are suitable to be used for a given definition.
319 * @param string $component
320 * @param string $area
321 * @return array Array containing 3 elements
322 * 1. An array of currently used stores
323 * 2. An array of suitable stores
324 * 3. An array of default stores
326 public static function get_definition_store_options(string $component, string $area): array {
327 $factory = cache_factory
::instance();
328 $definition = $factory->create_definition($component, $area);
329 $config = cache_config
::instance();
330 $currentstores = $config->get_stores_for_definition($definition);
331 $possiblestores = $config->get_stores($definition->get_mode(), $definition->get_requirements_bin());
334 foreach ($currentstores as $key => $store) {
335 if (!empty($store['default'])) {
337 unset($currentstores[$key]);
340 foreach ($possiblestores as $key => $store) {
341 if ($store['default']) {
342 unset($possiblestores[$key]);
343 $possiblestores[$key] = $store;
346 return array($currentstores, $possiblestores, $defaults);
350 * This function must be implemented to display options for store plugins.
352 * @param string $name the name of the store plugin.
353 * @param array $plugindetails array of store plugin details.
354 * @return array array of actions.
356 public function get_store_plugin_actions(string $name, array $plugindetails): array {
361 * This function must be implemented to display options for store instances.
363 * @param string $name the store instance name.
364 * @param array $storedetails array of store instance details.
365 * @return array array of actions.
367 public function get_store_instance_actions(string $name, array $storedetails): array {
372 * This function must be implemented to display options for definition mappings.
374 * @param context $context the context for the definition.
375 * @param array $definitionsummary the definition summary.
376 * @return array array of actions.
378 public function get_definition_actions(\context
$context, array $definitionsummary): array {
383 * This function must be implemented to get addable locks.
385 * @return array array of locks that are addable.
387 public function get_addable_lock_options(): array {
392 * This function must be implemented to perform any page actions by a child class.
394 * @param string $action the action to perform.
395 * @param array $forminfo empty array to be set by actions.
396 * @return array array of form info.
398 public abstract function perform_cache_actions(string $action, array $forminfo): array;
401 * This function must be implemented to display the cache admin page.
403 * @param core_cache_renderer $renderer the renderer used to generate the page.
404 * @return string the HTML for the page.
406 public abstract function generate_admin_page(\core_cache_renderer
$renderer): string;