MDL-37474 cache: fixed bug with definition cleaning
[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();
167ad91e
SH
87 unset($config['lock']);
88 foreach ($writer->get_locks() as $lock => $lockconfig) {
89 if ($lock == $data->lock) {
90 $config['lock'] = $data->lock;
91 }
92 }
26ce56fd 93 $writer->edit_store_instance($data->name, $data->plugin, $config);
8139ad13
SH
94 redirect($PAGE->url, get_string('editstoresuccess', 'cache', $plugins[$plugin]['name']), 5);
95 }
96 break;
170f821b 97 case 'deletestore' : // Delete a given store.
8139ad13
SH
98 $store = required_param('store', PARAM_TEXT);
99 $confirm = optional_param('confirm', false, PARAM_BOOL);
100
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;
f1d29cf5 106 $notification = get_string('deletestorehasmappings', 'cache');
8139ad13
SH
107 }
108
109 if ($notifysuccess) {
110 if (!$confirm) {
111 $title = get_string('confirmstoredeletion', 'cache');
170f821b
SH
112 $params = array('store' => $store, 'confirm' => 1, 'action' => $action, 'sesskey' => sesskey());
113 $url = new moodle_url($PAGE->url, $params);
8139ad13
SH
114 $button = new single_button($url, get_string('deletestore', 'cache'));
115
116 $PAGE->set_title($title);
117 $PAGE->set_heading($SITE->fullname);
118 echo $OUTPUT->header();
119 echo $OUTPUT->heading($title);
170f821b
SH
120 $confirmation = get_string('deletestoreconfirmation', 'cache', $stores[$store]['name']);
121 echo $OUTPUT->confirm($confirmation, $button, $PAGE->url);
8139ad13
SH
122 echo $OUTPUT->footer();
123 exit;
124 } else {
125 $writer = cache_config_writer::instance();
26ce56fd 126 $writer->delete_store_instance($store);
8139ad13
SH
127 redirect($PAGE->url, get_string('deletestoresuccess', 'cache'), 5);
128 }
129 }
130 break;
170f821b 131 case 'editdefinitionmapping' : // Edit definition mappings.
c6cbc3c7 132 $definition = required_param('definition', PARAM_SAFEPATH);
8139ad13
SH
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;
170f821b 149 case 'editmodemappings': // Edit default mode mappings.
8139ad13
SH
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;
169
50523565 170 case 'purgedefinition': // Purge a specific definition.
c6cbc3c7 171 $definition = required_param('definition', PARAM_SAFEPATH);
50523565
SH
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;
176
177 case 'purgestore':
170f821b
SH
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);
8139ad13
SH
182 break;
183 }
184}
185
186$PAGE->set_title($title);
187$PAGE->set_heading($SITE->fullname);
188$renderer = $PAGE->get_renderer('core_cache');
189
190echo $renderer->header();
191echo $renderer->heading($title);
192
193if (!is_null($notification)) {
194 echo $renderer->notification($notification, ($notifysuccess)?'notifysuccess' : 'notifyproblem');
195}
196
197if ($mform instanceof moodleform) {
198 $mform->display();
199} else {
26ce56fd
SH
200 echo $renderer->store_plugin_summaries($plugins);
201 echo $renderer->store_instance_summariers($stores, $plugins);
8139ad13 202 echo $renderer->definition_summaries($definitions, cache_administration_helper::get_definition_actions($context));
167ad91e 203 echo $renderer->lock_summaries($locks);
8139ad13
SH
204
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);
210}
211
212echo $renderer->footer();