2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * The administration and management interface for the cache setup and configuration.
20 * This file is part of Moodle's cache API, affectionately called MUC.
24 * @copyright 2012 Sam Hemelryk
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
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');
47 $notifysuccess = true;
49 if (!empty($action) && confirm_sesskey()) {
51 case 'rescandefinitions' : // Rescan definitions.
52 cache_config_writer
::update_definitions();
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');
60 $mform = cache_administration_helper
::get_add_store_form($plugin);
61 $title = get_string('addstore', 'cache', $plugins[$plugin]['name']);
62 if ($mform->is_cancelled()) {
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
;
73 $writer->add_store_instance($data->name
, $data->plugin
, $config);
74 redirect($PAGE->url
, get_string('addstoresuccess', 'cache', $plugins[$plugin]['name']), 5);
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()) {
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
;
93 $writer->edit_store_instance($data->name
, $data->plugin
, $config);
94 redirect($PAGE->url
, get_string('editstoresuccess', 'cache', $plugins[$plugin]['name']), 5);
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');
109 if ($notifysuccess) {
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();
125 $writer = cache_config_writer
::instance();
126 $writer->delete_store_instance($store);
127 redirect($PAGE->url
, get_string('deletestoresuccess', 'cache'), 5);
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();
140 foreach ($data->mappings
as $mapping) {
141 if (!empty($mapping)) {
142 $mappings[] = $mapping;
145 $writer->set_definition_mappings($definition, $mappings);
146 redirect($PAGE->url
);
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
]),
156 if ($mform->is_cancelled()) {
157 redirect($PAGE->url
);
158 } else if ($data = $mform->get_data()) {
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
}),
164 $writer = cache_config_writer
::instance();
165 $writer->set_mode_mappings($mappings);
166 redirect($PAGE->url
);
170 case 'purge': // Purge a store cache.
171 $store = required_param('store', PARAM_TEXT
);
172 cache_helper
::purge_store($store);
173 redirect($PAGE->url
, get_string('purgestoresuccess', 'cache'), 5);
178 $PAGE->set_title($title);
179 $PAGE->set_heading($SITE->fullname
);
180 $renderer = $PAGE->get_renderer('core_cache');
182 echo $renderer->header();
183 echo $renderer->heading($title);
185 if (!is_null($notification)) {
186 echo $renderer->notification($notification, ($notifysuccess)?
'notifysuccess' : 'notifyproblem');
189 if ($mform instanceof moodleform
) {
192 echo $renderer->store_plugin_summaries($plugins);
193 echo $renderer->store_instance_summariers($stores, $plugins);
194 echo $renderer->definition_summaries($definitions, cache_administration_helper
::get_definition_actions($context));
195 echo $renderer->lock_summaries($locks);
197 $applicationstore = join(', ', $defaultmodestores[cache_store
::MODE_APPLICATION
]);
198 $sessionstore = join(', ', $defaultmodestores[cache_store
::MODE_SESSION
]);
199 $requeststore = join(', ', $defaultmodestores[cache_store
::MODE_REQUEST
]);
200 $editurl = new moodle_url('/cache/admin.php', array('action' => 'editmodemappings', 'sesskey' => sesskey()));
201 echo $renderer->mode_mappings($applicationstore, $sessionstore, $requeststore, $editurl);
204 echo $renderer->footer();