weekly release 3.8dev
[moodle.git] / cache / admin.php
CommitLineData
8139ad13
SH
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.
13//
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/>.
16
17/**
18 * The administration and management interface for the cache setup and configuration.
19 *
20 * This file is part of Moodle's cache API, affectionately called MUC.
21 *
22 * @package core
23 * @category cache
24 * @copyright 2012 Sam Hemelryk
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28require_once('../config.php');
29require_once($CFG->dirroot.'/lib/adminlib.php');
30require_once($CFG->dirroot.'/cache/locallib.php');
31require_once($CFG->dirroot.'/cache/forms.php');
32
5f5776c1
SH
33// The first time the user visits this page we are going to reparse the definitions.
34// Just ensures that everything is up to date.
35// We flag is session so that this only happens once as people are likely to hit
36// this page several times if making changes.
37if (empty($SESSION->cacheadminreparsedefinitions)) {
38 cache_helper::update_definitions();
39 $SESSION->cacheadminreparsedefinitions = true;
40}
41
8139ad13
SH
42$action = optional_param('action', null, PARAM_ALPHA);
43
44admin_externalpage_setup('cacheconfig');
45$context = context_system::instance();
46
26ce56fd
SH
47$stores = cache_administration_helper::get_store_instance_summaries();
48$plugins = cache_administration_helper::get_store_plugin_summaries();
8139ad13
SH
49$definitions = cache_administration_helper::get_definition_summaries();
50$defaultmodestores = cache_administration_helper::get_default_mode_stores();
167ad91e 51$locks = cache_administration_helper::get_lock_summaries();
8139ad13
SH
52
53$title = new lang_string('cacheadmin', 'cache');
54$mform = null;
727c2173 55$notifications = array();
8139ad13
SH
56$notifysuccess = true;
57
58if (!empty($action) && confirm_sesskey()) {
59 switch ($action) {
170f821b 60 case 'rescandefinitions' : // Rescan definitions.
8139ad13
SH
61 cache_config_writer::update_definitions();
62 redirect($PAGE->url);
63 break;
170f821b 64 case 'addstore' : // Add the requested store.
8139ad13 65 $plugin = required_param('plugin', PARAM_PLUGIN);
7e7e108f
SH
66 if (!$plugins[$plugin]['canaddinstance']) {
67 print_error('ex_unmetstorerequirements', 'cache');
68 }
8139ad13
SH
69 $mform = cache_administration_helper::get_add_store_form($plugin);
70 $title = get_string('addstore', 'cache', $plugins[$plugin]['name']);
71 if ($mform->is_cancelled()) {
7a65e286 72 redirect($PAGE->url);
8139ad13
SH
73 } else if ($data = $mform->get_data()) {
74 $config = cache_administration_helper::get_store_configuration_from_data($data);
75 $writer = cache_config_writer::instance();
167ad91e
SH
76 unset($config['lock']);
77 foreach ($writer->get_locks() as $lock => $lockconfig) {
78 if ($lock == $data->lock) {
79 $config['lock'] = $data->lock;
80 }
81 }
26ce56fd 82 $writer->add_store_instance($data->name, $data->plugin, $config);
8139ad13
SH
83 redirect($PAGE->url, get_string('addstoresuccess', 'cache', $plugins[$plugin]['name']), 5);
84 }
85 break;
170f821b 86 case 'editstore' : // Edit the requested store.
8139ad13
SH
87 $plugin = required_param('plugin', PARAM_PLUGIN);
88 $store = required_param('store', PARAM_TEXT);
89 $mform = cache_administration_helper::get_edit_store_form($plugin, $store);
90 $title = get_string('addstore', 'cache', $plugins[$plugin]['name']);
91 if ($mform->is_cancelled()) {
7a65e286 92 redirect($PAGE->url);
8139ad13
SH
93 } else if ($data = $mform->get_data()) {
94 $config = cache_administration_helper::get_store_configuration_from_data($data);
95 $writer = cache_config_writer::instance();
acf49f4b 96
167ad91e
SH
97 unset($config['lock']);
98 foreach ($writer->get_locks() as $lock => $lockconfig) {
99 if ($lock == $data->lock) {
100 $config['lock'] = $data->lock;
101 }
102 }
26ce56fd 103 $writer->edit_store_instance($data->name, $data->plugin, $config);
8139ad13
SH
104 redirect($PAGE->url, get_string('editstoresuccess', 'cache', $plugins[$plugin]['name']), 5);
105 }
106 break;
170f821b 107 case 'deletestore' : // Delete a given store.
8139ad13
SH
108 $store = required_param('store', PARAM_TEXT);
109 $confirm = optional_param('confirm', false, PARAM_BOOL);
110
111 if (!array_key_exists($store, $stores)) {
112 $notifysuccess = false;
727c2173 113 $notifications[] = array(get_string('invalidstore', 'cache'), false);
8139ad13
SH
114 } else if ($stores[$store]['mappings'] > 0) {
115 $notifysuccess = false;
727c2173 116 $notifications[] = array(get_string('deletestorehasmappings', 'cache'), false);
8139ad13
SH
117 }
118
119 if ($notifysuccess) {
120 if (!$confirm) {
121 $title = get_string('confirmstoredeletion', 'cache');
170f821b
SH
122 $params = array('store' => $store, 'confirm' => 1, 'action' => $action, 'sesskey' => sesskey());
123 $url = new moodle_url($PAGE->url, $params);
8139ad13
SH
124 $button = new single_button($url, get_string('deletestore', 'cache'));
125
126 $PAGE->set_title($title);
127 $PAGE->set_heading($SITE->fullname);
128 echo $OUTPUT->header();
129 echo $OUTPUT->heading($title);
170f821b
SH
130 $confirmation = get_string('deletestoreconfirmation', 'cache', $stores[$store]['name']);
131 echo $OUTPUT->confirm($confirmation, $button, $PAGE->url);
8139ad13
SH
132 echo $OUTPUT->footer();
133 exit;
134 } else {
135 $writer = cache_config_writer::instance();
26ce56fd 136 $writer->delete_store_instance($store);
8139ad13
SH
137 redirect($PAGE->url, get_string('deletestoresuccess', 'cache'), 5);
138 }
139 }
140 break;
170f821b 141 case 'editdefinitionmapping' : // Edit definition mappings.
c6cbc3c7 142 $definition = required_param('definition', PARAM_SAFEPATH);
46e17f04
SH
143 if (!array_key_exists($definition, $definitions)) {
144 throw new cache_exception('Invalid cache definition requested');
145 }
8139ad13
SH
146 $title = get_string('editdefinitionmappings', 'cache', $definition);
147 $mform = new cache_definition_mappings_form($PAGE->url, array('definition' => $definition));
148 if ($mform->is_cancelled()) {
149 redirect($PAGE->url);
150 } else if ($data = $mform->get_data()) {
151 $writer = cache_config_writer::instance();
152 $mappings = array();
153 foreach ($data->mappings as $mapping) {
154 if (!empty($mapping)) {
155 $mappings[] = $mapping;
156 }
157 }
158 $writer->set_definition_mappings($definition, $mappings);
159 redirect($PAGE->url);
160 }
161 break;
46e17f04
SH
162 case 'editdefinitionsharing' :
163 $definition = required_param('definition', PARAM_SAFEPATH);
164 if (!array_key_exists($definition, $definitions)) {
165 throw new cache_exception('Invalid cache definition requested');
166 }
5f5776c1 167 $title = get_string('editdefinitionsharing', 'cache', $definition);
46e17f04 168 $sharingoptions = $definitions[$definition]['sharingoptions'];
5f5776c1
SH
169 $customdata = array('definition' => $definition, 'sharingoptions' => $sharingoptions);
170 $mform = new cache_definition_sharing_form($PAGE->url, $customdata);
46e17f04
SH
171 $mform->set_data(array(
172 'sharing' => $definitions[$definition]['selectedsharingoption'],
173 'userinputsharingkey' => $definitions[$definition]['userinputsharingkey']
174 ));
175 if ($mform->is_cancelled()) {
176 redirect($PAGE->url);
177 } else if ($data = $mform->get_data()) {
178 $component = $definitions[$definition]['component'];
179 $area = $definitions[$definition]['area'];
5f5776c1
SH
180 // Purge the stores removing stale data before we alter the sharing option.
181 cache_helper::purge_stores_used_by_definition($component, $area);
46e17f04
SH
182 $writer = cache_config_writer::instance();
183 $sharing = array_sum(array_keys($data->sharing));
184 $userinputsharingkey = $data->userinputsharingkey;
185 $writer->set_definition_sharing($definition, $sharing, $userinputsharingkey);
186 redirect($PAGE->url);
187 }
188 break;
170f821b 189 case 'editmodemappings': // Edit default mode mappings.
8139ad13
SH
190 $mform = new cache_mode_mappings_form(null, $stores);
191 $mform->set_data(array(
192 'mode_'.cache_store::MODE_APPLICATION => key($defaultmodestores[cache_store::MODE_APPLICATION]),
193 'mode_'.cache_store::MODE_SESSION => key($defaultmodestores[cache_store::MODE_SESSION]),
194 'mode_'.cache_store::MODE_REQUEST => key($defaultmodestores[cache_store::MODE_REQUEST]),
195 ));
196 if ($mform->is_cancelled()) {
197 redirect($PAGE->url);
198 } else if ($data = $mform->get_data()) {
199 $mappings = array(
200 cache_store::MODE_APPLICATION => array($data->{'mode_'.cache_store::MODE_APPLICATION}),
201 cache_store::MODE_SESSION => array($data->{'mode_'.cache_store::MODE_SESSION}),
202 cache_store::MODE_REQUEST => array($data->{'mode_'.cache_store::MODE_REQUEST}),
203 );
204 $writer = cache_config_writer::instance();
205 $writer->set_mode_mappings($mappings);
206 redirect($PAGE->url);
207 }
208 break;
209
50523565 210 case 'purgedefinition': // Purge a specific definition.
c6cbc3c7 211 $definition = required_param('definition', PARAM_SAFEPATH);
50523565 212 list($component, $area) = explode('/', $definition, 2);
045fe95c
SH
213 $factory = cache_factory::instance();
214 $definition = $factory->create_definition($component, $area);
215 if ($definition->has_required_identifiers()) {
216 // We will have to purge the stores used by this definition.
217 cache_helper::purge_stores_used_by_definition($component, $area);
218 } else {
219 // Alrighty we can purge just the data belonging to this definition.
220 cache_helper::purge_by_definition($component, $area);
221 }
50523565
SH
222 redirect($PAGE->url, get_string('purgedefinitionsuccess', 'cache'), 5);
223 break;
224
225 case 'purgestore':
170f821b
SH
226 case 'purge': // Purge a store cache.
227 $store = required_param('store', PARAM_TEXT);
228 cache_helper::purge_store($store);
229 redirect($PAGE->url, get_string('purgestoresuccess', 'cache'), 5);
8139ad13 230 break;
acf49f4b
SH
231
232 case 'newlockinstance':
233 // Adds a new lock instance.
234 $lock = required_param('lock', PARAM_ALPHANUMEXT);
235 $mform = cache_administration_helper::get_add_lock_form($lock);
236 if ($mform->is_cancelled()) {
237 redirect($PAGE->url);
238 } else if ($data = $mform->get_data()) {
239 $factory = cache_factory::instance();
240 $config = $factory->create_config_instance(true);
241 $name = $data->name;
242 $data = cache_administration_helper::get_lock_configuration_from_data($lock, $data);
243 $config->add_lock_instance($name, $lock, $data);
244 redirect($PAGE->url, get_string('addlocksuccess', 'cache', $name), 5);
245 }
246 break;
247 case 'deletelock':
248 // Deletes a lock instance.
249 $lock = required_param('lock', PARAM_ALPHANUMEXT);
250 $confirm = optional_param('confirm', false, PARAM_BOOL);
251 if (!array_key_exists($lock, $locks)) {
252 $notifysuccess = false;
727c2173 253 $notifications[] = array(get_string('invalidlock', 'cache'), false);
acf49f4b
SH
254 } else if ($locks[$lock]['uses'] > 0) {
255 $notifysuccess = false;
727c2173 256 $notifications[] = array(get_string('deletelockhasuses', 'cache'), false);
acf49f4b
SH
257 }
258 if ($notifysuccess) {
259 if (!$confirm) {
260 $title = get_string('confirmlockdeletion', 'cache');
261 $params = array('lock' => $lock, 'confirm' => 1, 'action' => $action, 'sesskey' => sesskey());
262 $url = new moodle_url($PAGE->url, $params);
263 $button = new single_button($url, get_string('deletelock', 'cache'));
264
265 $PAGE->set_title($title);
266 $PAGE->set_heading($SITE->fullname);
267 echo $OUTPUT->header();
268 echo $OUTPUT->heading($title);
269 $confirmation = get_string('deletelockconfirmation', 'cache', $lock);
270 echo $OUTPUT->confirm($confirmation, $button, $PAGE->url);
271 echo $OUTPUT->footer();
272 exit;
273 } else {
274 $writer = cache_config_writer::instance();
275 $writer->delete_lock_instance($lock);
276 redirect($PAGE->url, get_string('deletelocksuccess', 'cache'), 5);
277 }
278 }
279 break;
8139ad13
SH
280 }
281}
282
ed2de2ec
SH
283// Add cache store warnings to the list of notifications.
284// Obviously as these are warnings they are show as failures.
285foreach (cache_helper::warnings($stores) as $warning) {
286 $notifications[] = array($warning, false);
287}
727c2173 288
8139ad13
SH
289$PAGE->set_title($title);
290$PAGE->set_heading($SITE->fullname);
d31fd615 291/* @var core_cache_renderer $renderer */
8139ad13
SH
292$renderer = $PAGE->get_renderer('core_cache');
293
294echo $renderer->header();
295echo $renderer->heading($title);
915140c9 296echo $renderer->notifications($notifications);
8139ad13
SH
297
298if ($mform instanceof moodleform) {
299 $mform->display();
300} else {
26ce56fd
SH
301 echo $renderer->store_plugin_summaries($plugins);
302 echo $renderer->store_instance_summariers($stores, $plugins);
54519330 303 echo $renderer->definition_summaries($definitions, $context);
167ad91e 304 echo $renderer->lock_summaries($locks);
8139ad13
SH
305
306 $applicationstore = join(', ', $defaultmodestores[cache_store::MODE_APPLICATION]);
307 $sessionstore = join(', ', $defaultmodestores[cache_store::MODE_SESSION]);
308 $requeststore = join(', ', $defaultmodestores[cache_store::MODE_REQUEST]);
309 $editurl = new moodle_url('/cache/admin.php', array('action' => 'editmodemappings', 'sesskey' => sesskey()));
310 echo $renderer->mode_mappings($applicationstore, $sessionstore, $requeststore, $editurl);
311}
312
313echo $renderer->footer();