MDL-37474 cache: fixed bug with definition cleaning
[moodle.git] / cache / admin.php
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/>.
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  */
28 require_once('../config.php');
29 require_once($CFG->dirroot.'/lib/adminlib.php');
30 require_once($CFG->dirroot.'/cache/locallib.php');
31 require_once($CFG->dirroot.'/cache/forms.php');
33 $action = optional_param('action', null, PARAM_ALPHA);
35 admin_externalpage_setup('cacheconfig');
36 $context = context_system::instance();
38 $stores = cache_administration_helper::get_store_instance_summaries();
39 $plugins = cache_administration_helper::get_store_plugin_summaries();
40 $definitions = cache_administration_helper::get_definition_summaries();
41 $defaultmodestores = cache_administration_helper::get_default_mode_stores();
42 $locks = cache_administration_helper::get_lock_summaries();
44 $title = new lang_string('cacheadmin', 'cache');
45 $mform = null;
46 $notification = null;
47 $notifysuccess = true;
49 if (!empty($action) && confirm_sesskey()) {
50     switch ($action) {
51         case 'rescandefinitions' : // Rescan definitions.
52             cache_config_writer::update_definitions();
53             redirect($PAGE->url);
54             break;
55         case 'addstore' : // Add the requested store.
56             $plugin = required_param('plugin', PARAM_PLUGIN);
57             if (!$plugins[$plugin]['canaddinstance']) {
58                 print_error('ex_unmetstorerequirements', 'cache');
59             }
60             $mform = cache_administration_helper::get_add_store_form($plugin);
61             $title = get_string('addstore', 'cache', $plugins[$plugin]['name']);
62             if ($mform->is_cancelled()) {
63                 redirect($PAGE->url);
64             } else if ($data = $mform->get_data()) {
65                 $config = cache_administration_helper::get_store_configuration_from_data($data);
66                 $writer = cache_config_writer::instance();
67                 unset($config['lock']);
68                 foreach ($writer->get_locks() as $lock => $lockconfig) {
69                     if ($lock == $data->lock) {
70                         $config['lock'] = $data->lock;
71                     }
72                 }
73                 $writer->add_store_instance($data->name, $data->plugin, $config);
74                 redirect($PAGE->url, get_string('addstoresuccess', 'cache', $plugins[$plugin]['name']), 5);
75             }
76             break;
77         case 'editstore' : // Edit the requested store.
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()) {
83                 redirect($PAGE->url);
84             } else if ($data = $mform->get_data()) {
85                 $config = cache_administration_helper::get_store_configuration_from_data($data);
86                 $writer = cache_config_writer::instance();
87                 unset($config['lock']);
88                 foreach ($writer->get_locks() as $lock => $lockconfig) {
89                     if ($lock == $data->lock) {
90                         $config['lock'] = $data->lock;
91                     }
92                 }
93                 $writer->edit_store_instance($data->name, $data->plugin, $config);
94                 redirect($PAGE->url, get_string('editstoresuccess', 'cache', $plugins[$plugin]['name']), 5);
95             }
96             break;
97         case 'deletestore' : // Delete a given store.
98             $store = required_param('store', PARAM_TEXT);
99             $confirm = optional_param('confirm', false, PARAM_BOOL);
101             if (!array_key_exists($store, $stores)) {
102                 $notifysuccess = false;
103                 $notification = get_string('invalidstore');
104             } else if ($stores[$store]['mappings'] > 0) {
105                 $notifysuccess = false;
106                 $notification = get_string('deletestorehasmappings', 'cache');
107             }
109             if ($notifysuccess) {
110                 if (!$confirm) {
111                     $title = get_string('confirmstoredeletion', 'cache');
112                     $params = array('store' => $store, 'confirm' => 1, 'action' => $action, 'sesskey' => sesskey());
113                     $url = new moodle_url($PAGE->url, $params);
114                     $button = new single_button($url, get_string('deletestore', 'cache'));
116                     $PAGE->set_title($title);
117                     $PAGE->set_heading($SITE->fullname);
118                     echo $OUTPUT->header();
119                     echo $OUTPUT->heading($title);
120                     $confirmation = get_string('deletestoreconfirmation', 'cache', $stores[$store]['name']);
121                     echo $OUTPUT->confirm($confirmation, $button, $PAGE->url);
122                     echo $OUTPUT->footer();
123                     exit;
124                 } else {
125                     $writer = cache_config_writer::instance();
126                     $writer->delete_store_instance($store);
127                     redirect($PAGE->url, get_string('deletestoresuccess', 'cache'), 5);
128                 }
129             }
130             break;
131         case 'editdefinitionmapping' : // Edit definition mappings.
132             $definition = required_param('definition', PARAM_SAFEPATH);
133             $title = get_string('editdefinitionmappings', 'cache', $definition);
134             $mform = new cache_definition_mappings_form($PAGE->url, array('definition' => $definition));
135             if ($mform->is_cancelled()) {
136                 redirect($PAGE->url);
137             } else if ($data = $mform->get_data()) {
138                 $writer = cache_config_writer::instance();
139                 $mappings = array();
140                 foreach ($data->mappings as $mapping) {
141                     if (!empty($mapping)) {
142                         $mappings[] = $mapping;
143                     }
144                 }
145                 $writer->set_definition_mappings($definition, $mappings);
146                 redirect($PAGE->url);
147             }
148             break;
149         case 'editmodemappings': // Edit default mode mappings.
150             $mform = new cache_mode_mappings_form(null, $stores);
151             $mform->set_data(array(
152                 'mode_'.cache_store::MODE_APPLICATION => key($defaultmodestores[cache_store::MODE_APPLICATION]),
153                 'mode_'.cache_store::MODE_SESSION => key($defaultmodestores[cache_store::MODE_SESSION]),
154                 'mode_'.cache_store::MODE_REQUEST => key($defaultmodestores[cache_store::MODE_REQUEST]),
155             ));
156             if ($mform->is_cancelled()) {
157                 redirect($PAGE->url);
158             } else if ($data = $mform->get_data()) {
159                 $mappings = array(
160                     cache_store::MODE_APPLICATION => array($data->{'mode_'.cache_store::MODE_APPLICATION}),
161                     cache_store::MODE_SESSION => array($data->{'mode_'.cache_store::MODE_SESSION}),
162                     cache_store::MODE_REQUEST => array($data->{'mode_'.cache_store::MODE_REQUEST}),
163                 );
164                 $writer = cache_config_writer::instance();
165                 $writer->set_mode_mappings($mappings);
166                 redirect($PAGE->url);
167             }
168             break;
170         case 'purgedefinition': // Purge a specific definition.
171             $definition = required_param('definition', PARAM_SAFEPATH);
172             list($component, $area) = explode('/', $definition, 2);
173             cache_helper::purge_by_definition($component, $area);
174             redirect($PAGE->url, get_string('purgedefinitionsuccess', 'cache'), 5);
175             break;
177         case 'purgestore':
178         case 'purge': // Purge a store cache.
179             $store = required_param('store', PARAM_TEXT);
180             cache_helper::purge_store($store);
181             redirect($PAGE->url, get_string('purgestoresuccess', 'cache'), 5);
182             break;
183     }
186 $PAGE->set_title($title);
187 $PAGE->set_heading($SITE->fullname);
188 $renderer = $PAGE->get_renderer('core_cache');
190 echo $renderer->header();
191 echo $renderer->heading($title);
193 if (!is_null($notification)) {
194     echo $renderer->notification($notification, ($notifysuccess)?'notifysuccess' : 'notifyproblem');
197 if ($mform instanceof moodleform) {
198     $mform->display();
199 } else {
200     echo $renderer->store_plugin_summaries($plugins);
201     echo $renderer->store_instance_summariers($stores, $plugins);
202     echo $renderer->definition_summaries($definitions, cache_administration_helper::get_definition_actions($context));
203     echo $renderer->lock_summaries($locks);
205     $applicationstore = join(', ', $defaultmodestores[cache_store::MODE_APPLICATION]);
206     $sessionstore = join(', ', $defaultmodestores[cache_store::MODE_SESSION]);
207     $requeststore = join(', ', $defaultmodestores[cache_store::MODE_REQUEST]);
208     $editurl = new moodle_url('/cache/admin.php', array('action' => 'editmodemappings', 'sesskey' => sesskey()));
209     echo $renderer->mode_mappings($applicationstore, $sessionstore, $requeststore, $editurl);
212 echo $renderer->footer();