on demand release 2.5beta+
[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);
8139ad13
SH
134 $title = get_string('editdefinitionmappings', 'cache', $definition);
135 $mform = new cache_definition_mappings_form($PAGE->url, array('definition' => $definition));
136 if ($mform->is_cancelled()) {
137 redirect($PAGE->url);
138 } else if ($data = $mform->get_data()) {
139 $writer = cache_config_writer::instance();
140 $mappings = array();
141 foreach ($data->mappings as $mapping) {
142 if (!empty($mapping)) {
143 $mappings[] = $mapping;
144 }
145 }
146 $writer->set_definition_mappings($definition, $mappings);
147 redirect($PAGE->url);
148 }
149 break;
170f821b 150 case 'editmodemappings': // Edit default mode mappings.
8139ad13
SH
151 $mform = new cache_mode_mappings_form(null, $stores);
152 $mform->set_data(array(
153 'mode_'.cache_store::MODE_APPLICATION => key($defaultmodestores[cache_store::MODE_APPLICATION]),
154 'mode_'.cache_store::MODE_SESSION => key($defaultmodestores[cache_store::MODE_SESSION]),
155 'mode_'.cache_store::MODE_REQUEST => key($defaultmodestores[cache_store::MODE_REQUEST]),
156 ));
157 if ($mform->is_cancelled()) {
158 redirect($PAGE->url);
159 } else if ($data = $mform->get_data()) {
160 $mappings = array(
161 cache_store::MODE_APPLICATION => array($data->{'mode_'.cache_store::MODE_APPLICATION}),
162 cache_store::MODE_SESSION => array($data->{'mode_'.cache_store::MODE_SESSION}),
163 cache_store::MODE_REQUEST => array($data->{'mode_'.cache_store::MODE_REQUEST}),
164 );
165 $writer = cache_config_writer::instance();
166 $writer->set_mode_mappings($mappings);
167 redirect($PAGE->url);
168 }
169 break;
170
50523565 171 case 'purgedefinition': // Purge a specific definition.
c6cbc3c7 172 $definition = required_param('definition', PARAM_SAFEPATH);
50523565
SH
173 list($component, $area) = explode('/', $definition, 2);
174 cache_helper::purge_by_definition($component, $area);
175 redirect($PAGE->url, get_string('purgedefinitionsuccess', 'cache'), 5);
176 break;
177
178 case 'purgestore':
170f821b
SH
179 case 'purge': // Purge a store cache.
180 $store = required_param('store', PARAM_TEXT);
181 cache_helper::purge_store($store);
182 redirect($PAGE->url, get_string('purgestoresuccess', 'cache'), 5);
8139ad13 183 break;
acf49f4b
SH
184
185 case 'newlockinstance':
186 // Adds a new lock instance.
187 $lock = required_param('lock', PARAM_ALPHANUMEXT);
188 $mform = cache_administration_helper::get_add_lock_form($lock);
189 if ($mform->is_cancelled()) {
190 redirect($PAGE->url);
191 } else if ($data = $mform->get_data()) {
192 $factory = cache_factory::instance();
193 $config = $factory->create_config_instance(true);
194 $name = $data->name;
195 $data = cache_administration_helper::get_lock_configuration_from_data($lock, $data);
196 $config->add_lock_instance($name, $lock, $data);
197 redirect($PAGE->url, get_string('addlocksuccess', 'cache', $name), 5);
198 }
199 break;
200 case 'deletelock':
201 // Deletes a lock instance.
202 $lock = required_param('lock', PARAM_ALPHANUMEXT);
203 $confirm = optional_param('confirm', false, PARAM_BOOL);
204 if (!array_key_exists($lock, $locks)) {
205 $notifysuccess = false;
206 $notification = get_string('invalidlock');
207 } else if ($locks[$lock]['uses'] > 0) {
208 $notifysuccess = false;
209 $notification = get_string('deletelockhasuses', 'cache');
210 }
211 if ($notifysuccess) {
212 if (!$confirm) {
213 $title = get_string('confirmlockdeletion', 'cache');
214 $params = array('lock' => $lock, 'confirm' => 1, 'action' => $action, 'sesskey' => sesskey());
215 $url = new moodle_url($PAGE->url, $params);
216 $button = new single_button($url, get_string('deletelock', 'cache'));
217
218 $PAGE->set_title($title);
219 $PAGE->set_heading($SITE->fullname);
220 echo $OUTPUT->header();
221 echo $OUTPUT->heading($title);
222 $confirmation = get_string('deletelockconfirmation', 'cache', $lock);
223 echo $OUTPUT->confirm($confirmation, $button, $PAGE->url);
224 echo $OUTPUT->footer();
225 exit;
226 } else {
227 $writer = cache_config_writer::instance();
228 $writer->delete_lock_instance($lock);
229 redirect($PAGE->url, get_string('deletelocksuccess', 'cache'), 5);
230 }
231 }
232 break;
8139ad13
SH
233 }
234}
235
236$PAGE->set_title($title);
237$PAGE->set_heading($SITE->fullname);
238$renderer = $PAGE->get_renderer('core_cache');
239
240echo $renderer->header();
241echo $renderer->heading($title);
242
243if (!is_null($notification)) {
244 echo $renderer->notification($notification, ($notifysuccess)?'notifysuccess' : 'notifyproblem');
245}
246
247if ($mform instanceof moodleform) {
248 $mform->display();
249} else {
26ce56fd
SH
250 echo $renderer->store_plugin_summaries($plugins);
251 echo $renderer->store_instance_summariers($stores, $plugins);
8139ad13 252 echo $renderer->definition_summaries($definitions, cache_administration_helper::get_definition_actions($context));
167ad91e 253 echo $renderer->lock_summaries($locks);
8139ad13
SH
254
255 $applicationstore = join(', ', $defaultmodestores[cache_store::MODE_APPLICATION]);
256 $sessionstore = join(', ', $defaultmodestores[cache_store::MODE_SESSION]);
257 $requeststore = join(', ', $defaultmodestores[cache_store::MODE_REQUEST]);
258 $editurl = new moodle_url('/cache/admin.php', array('action' => 'editmodemappings', 'sesskey' => sesskey()));
259 echo $renderer->mode_mappings($applicationstore, $sessionstore, $requeststore, $editurl);
260}
261
262echo $renderer->footer();