MDL-37500 cache: implemented sharing options and gui
[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
33$action = optional_param('action', null, PARAM_ALPHA);
34
35admin_externalpage_setup('cacheconfig');
36$context = context_system::instance();
37
26ce56fd
SH
38$stores = cache_administration_helper::get_store_instance_summaries();
39$plugins = cache_administration_helper::get_store_plugin_summaries();
8139ad13
SH
40$definitions = cache_administration_helper::get_definition_summaries();
41$defaultmodestores = cache_administration_helper::get_default_mode_stores();
167ad91e 42$locks = cache_administration_helper::get_lock_summaries();
8139ad13
SH
43
44$title = new lang_string('cacheadmin', 'cache');
45$mform = null;
46$notification = null;
47$notifysuccess = true;
48
49if (!empty($action) && confirm_sesskey()) {
50 switch ($action) {
170f821b 51 case 'rescandefinitions' : // Rescan definitions.
8139ad13
SH
52 cache_config_writer::update_definitions();
53 redirect($PAGE->url);
54 break;
170f821b 55 case 'addstore' : // Add the requested store.
8139ad13 56 $plugin = required_param('plugin', PARAM_PLUGIN);
7e7e108f
SH
57 if (!$plugins[$plugin]['canaddinstance']) {
58 print_error('ex_unmetstorerequirements', 'cache');
59 }
8139ad13
SH
60 $mform = cache_administration_helper::get_add_store_form($plugin);
61 $title = get_string('addstore', 'cache', $plugins[$plugin]['name']);
62 if ($mform->is_cancelled()) {
7a65e286 63 redirect($PAGE->url);
8139ad13
SH
64 } else if ($data = $mform->get_data()) {
65 $config = cache_administration_helper::get_store_configuration_from_data($data);
66 $writer = cache_config_writer::instance();
167ad91e
SH
67 unset($config['lock']);
68 foreach ($writer->get_locks() as $lock => $lockconfig) {
69 if ($lock == $data->lock) {
70 $config['lock'] = $data->lock;
71 }
72 }
26ce56fd 73 $writer->add_store_instance($data->name, $data->plugin, $config);
8139ad13
SH
74 redirect($PAGE->url, get_string('addstoresuccess', 'cache', $plugins[$plugin]['name']), 5);
75 }
76 break;
170f821b 77 case 'editstore' : // Edit the requested store.
8139ad13
SH
78 $plugin = required_param('plugin', PARAM_PLUGIN);
79 $store = required_param('store', PARAM_TEXT);
80 $mform = cache_administration_helper::get_edit_store_form($plugin, $store);
81 $title = get_string('addstore', 'cache', $plugins[$plugin]['name']);
82 if ($mform->is_cancelled()) {
7a65e286 83 redirect($PAGE->url);
8139ad13
SH
84 } else if ($data = $mform->get_data()) {
85 $config = cache_administration_helper::get_store_configuration_from_data($data);
86 $writer = cache_config_writer::instance();
acf49f4b 87
167ad91e
SH
88 unset($config['lock']);
89 foreach ($writer->get_locks() as $lock => $lockconfig) {
90 if ($lock == $data->lock) {
91 $config['lock'] = $data->lock;
92 }
93 }
26ce56fd 94 $writer->edit_store_instance($data->name, $data->plugin, $config);
8139ad13
SH
95 redirect($PAGE->url, get_string('editstoresuccess', 'cache', $plugins[$plugin]['name']), 5);
96 }
97 break;
170f821b 98 case 'deletestore' : // Delete a given store.
8139ad13
SH
99 $store = required_param('store', PARAM_TEXT);
100 $confirm = optional_param('confirm', false, PARAM_BOOL);
101
102 if (!array_key_exists($store, $stores)) {
103 $notifysuccess = false;
104 $notification = get_string('invalidstore');
105 } else if ($stores[$store]['mappings'] > 0) {
106 $notifysuccess = false;
f1d29cf5 107 $notification = get_string('deletestorehasmappings', 'cache');
8139ad13
SH
108 }
109
110 if ($notifysuccess) {
111 if (!$confirm) {
112 $title = get_string('confirmstoredeletion', 'cache');
170f821b
SH
113 $params = array('store' => $store, 'confirm' => 1, 'action' => $action, 'sesskey' => sesskey());
114 $url = new moodle_url($PAGE->url, $params);
8139ad13
SH
115 $button = new single_button($url, get_string('deletestore', 'cache'));
116
117 $PAGE->set_title($title);
118 $PAGE->set_heading($SITE->fullname);
119 echo $OUTPUT->header();
120 echo $OUTPUT->heading($title);
170f821b
SH
121 $confirmation = get_string('deletestoreconfirmation', 'cache', $stores[$store]['name']);
122 echo $OUTPUT->confirm($confirmation, $button, $PAGE->url);
8139ad13
SH
123 echo $OUTPUT->footer();
124 exit;
125 } else {
126 $writer = cache_config_writer::instance();
26ce56fd 127 $writer->delete_store_instance($store);
8139ad13
SH
128 redirect($PAGE->url, get_string('deletestoresuccess', 'cache'), 5);
129 }
130 }
131 break;
170f821b 132 case 'editdefinitionmapping' : // Edit definition mappings.
c6cbc3c7 133 $definition = required_param('definition', PARAM_SAFEPATH);
46e17f04
SH
134 if (!array_key_exists($definition, $definitions)) {
135 throw new cache_exception('Invalid cache definition requested');
136 }
8139ad13
SH
137 $title = get_string('editdefinitionmappings', 'cache', $definition);
138 $mform = new cache_definition_mappings_form($PAGE->url, array('definition' => $definition));
139 if ($mform->is_cancelled()) {
140 redirect($PAGE->url);
141 } else if ($data = $mform->get_data()) {
142 $writer = cache_config_writer::instance();
143 $mappings = array();
144 foreach ($data->mappings as $mapping) {
145 if (!empty($mapping)) {
146 $mappings[] = $mapping;
147 }
148 }
149 $writer->set_definition_mappings($definition, $mappings);
150 redirect($PAGE->url);
151 }
152 break;
46e17f04
SH
153 case 'editdefinitionsharing' :
154 $definition = required_param('definition', PARAM_SAFEPATH);
155 if (!array_key_exists($definition, $definitions)) {
156 throw new cache_exception('Invalid cache definition requested');
157 }
158 $title = get_string('editdefinitionmappings', 'cache', $definition);
159 $sharingoptions = $definitions[$definition]['sharingoptions'];
160 $mform = new cache_definition_sharing_form($PAGE->url, array('definition' => $definition, 'sharingoptions' => $sharingoptions));
161 $mform->set_data(array(
162 'sharing' => $definitions[$definition]['selectedsharingoption'],
163 'userinputsharingkey' => $definitions[$definition]['userinputsharingkey']
164 ));
165 if ($mform->is_cancelled()) {
166 redirect($PAGE->url);
167 } else if ($data = $mform->get_data()) {
168 $component = $definitions[$definition]['component'];
169 $area = $definitions[$definition]['area'];
170 cache_helper::purge_by_definition($component, $area);
171 $writer = cache_config_writer::instance();
172 $sharing = array_sum(array_keys($data->sharing));
173 $userinputsharingkey = $data->userinputsharingkey;
174 $writer->set_definition_sharing($definition, $sharing, $userinputsharingkey);
175 redirect($PAGE->url);
176 }
177 break;
170f821b 178 case 'editmodemappings': // Edit default mode mappings.
8139ad13
SH
179 $mform = new cache_mode_mappings_form(null, $stores);
180 $mform->set_data(array(
181 'mode_'.cache_store::MODE_APPLICATION => key($defaultmodestores[cache_store::MODE_APPLICATION]),
182 'mode_'.cache_store::MODE_SESSION => key($defaultmodestores[cache_store::MODE_SESSION]),
183 'mode_'.cache_store::MODE_REQUEST => key($defaultmodestores[cache_store::MODE_REQUEST]),
184 ));
185 if ($mform->is_cancelled()) {
186 redirect($PAGE->url);
187 } else if ($data = $mform->get_data()) {
188 $mappings = array(
189 cache_store::MODE_APPLICATION => array($data->{'mode_'.cache_store::MODE_APPLICATION}),
190 cache_store::MODE_SESSION => array($data->{'mode_'.cache_store::MODE_SESSION}),
191 cache_store::MODE_REQUEST => array($data->{'mode_'.cache_store::MODE_REQUEST}),
192 );
193 $writer = cache_config_writer::instance();
194 $writer->set_mode_mappings($mappings);
195 redirect($PAGE->url);
196 }
197 break;
198
50523565 199 case 'purgedefinition': // Purge a specific definition.
c6cbc3c7 200 $definition = required_param('definition', PARAM_SAFEPATH);
50523565
SH
201 list($component, $area) = explode('/', $definition, 2);
202 cache_helper::purge_by_definition($component, $area);
203 redirect($PAGE->url, get_string('purgedefinitionsuccess', 'cache'), 5);
204 break;
205
206 case 'purgestore':
170f821b
SH
207 case 'purge': // Purge a store cache.
208 $store = required_param('store', PARAM_TEXT);
209 cache_helper::purge_store($store);
210 redirect($PAGE->url, get_string('purgestoresuccess', 'cache'), 5);
8139ad13 211 break;
acf49f4b
SH
212
213 case 'newlockinstance':
214 // Adds a new lock instance.
215 $lock = required_param('lock', PARAM_ALPHANUMEXT);
216 $mform = cache_administration_helper::get_add_lock_form($lock);
217 if ($mform->is_cancelled()) {
218 redirect($PAGE->url);
219 } else if ($data = $mform->get_data()) {
220 $factory = cache_factory::instance();
221 $config = $factory->create_config_instance(true);
222 $name = $data->name;
223 $data = cache_administration_helper::get_lock_configuration_from_data($lock, $data);
224 $config->add_lock_instance($name, $lock, $data);
225 redirect($PAGE->url, get_string('addlocksuccess', 'cache', $name), 5);
226 }
227 break;
228 case 'deletelock':
229 // Deletes a lock instance.
230 $lock = required_param('lock', PARAM_ALPHANUMEXT);
231 $confirm = optional_param('confirm', false, PARAM_BOOL);
232 if (!array_key_exists($lock, $locks)) {
233 $notifysuccess = false;
234 $notification = get_string('invalidlock');
235 } else if ($locks[$lock]['uses'] > 0) {
236 $notifysuccess = false;
237 $notification = get_string('deletelockhasuses', 'cache');
238 }
239 if ($notifysuccess) {
240 if (!$confirm) {
241 $title = get_string('confirmlockdeletion', 'cache');
242 $params = array('lock' => $lock, 'confirm' => 1, 'action' => $action, 'sesskey' => sesskey());
243 $url = new moodle_url($PAGE->url, $params);
244 $button = new single_button($url, get_string('deletelock', 'cache'));
245
246 $PAGE->set_title($title);
247 $PAGE->set_heading($SITE->fullname);
248 echo $OUTPUT->header();
249 echo $OUTPUT->heading($title);
250 $confirmation = get_string('deletelockconfirmation', 'cache', $lock);
251 echo $OUTPUT->confirm($confirmation, $button, $PAGE->url);
252 echo $OUTPUT->footer();
253 exit;
254 } else {
255 $writer = cache_config_writer::instance();
256 $writer->delete_lock_instance($lock);
257 redirect($PAGE->url, get_string('deletelocksuccess', 'cache'), 5);
258 }
259 }
260 break;
8139ad13
SH
261 }
262}
263
264$PAGE->set_title($title);
265$PAGE->set_heading($SITE->fullname);
266$renderer = $PAGE->get_renderer('core_cache');
267
268echo $renderer->header();
269echo $renderer->heading($title);
270
271if (!is_null($notification)) {
272 echo $renderer->notification($notification, ($notifysuccess)?'notifysuccess' : 'notifyproblem');
273}
274
275if ($mform instanceof moodleform) {
276 $mform->display();
277} else {
26ce56fd
SH
278 echo $renderer->store_plugin_summaries($plugins);
279 echo $renderer->store_instance_summariers($stores, $plugins);
8139ad13 280 echo $renderer->definition_summaries($definitions, cache_administration_helper::get_definition_actions($context));
167ad91e 281 echo $renderer->lock_summaries($locks);
8139ad13
SH
282
283 $applicationstore = join(', ', $defaultmodestores[cache_store::MODE_APPLICATION]);
284 $sessionstore = join(', ', $defaultmodestores[cache_store::MODE_SESSION]);
285 $requeststore = join(', ', $defaultmodestores[cache_store::MODE_REQUEST]);
286 $editurl = new moodle_url('/cache/admin.php', array('action' => 'editmodemappings', 'sesskey' => sesskey()));
287 echo $renderer->mode_mappings($applicationstore, $sessionstore, $requeststore, $editurl);
288}
289
290echo $renderer->footer();