MDL-25290 cache: Added optional CFG setting to control location of cache config file
[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
38$stores = cache_administration_helper::get_store_summaries();
39$plugins = cache_administration_helper::get_plugin_summaries();
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) {
51 case 'rescandefinitions' :
52 cache_config_writer::update_definitions();
53 redirect($PAGE->url);
54 break;
55 case 'addstore' :
56 $plugin = required_param('plugin', PARAM_PLUGIN);
57 $mform = cache_administration_helper::get_add_store_form($plugin);
58 $title = get_string('addstore', 'cache', $plugins[$plugin]['name']);
59 if ($mform->is_cancelled()) {
60 rediect($PAGE->url);
61 } else if ($data = $mform->get_data()) {
62 $config = cache_administration_helper::get_store_configuration_from_data($data);
63 $writer = cache_config_writer::instance();
167ad91e
SH
64 unset($config['lock']);
65 foreach ($writer->get_locks() as $lock => $lockconfig) {
66 if ($lock == $data->lock) {
67 $config['lock'] = $data->lock;
68 }
69 }
8139ad13
SH
70 $writer->add_plugin_instance($data->name, $data->plugin, $config);
71 redirect($PAGE->url, get_string('addstoresuccess', 'cache', $plugins[$plugin]['name']), 5);
72 }
73 break;
74 case 'editstore' :
75 $plugin = required_param('plugin', PARAM_PLUGIN);
76 $store = required_param('store', PARAM_TEXT);
77 $mform = cache_administration_helper::get_edit_store_form($plugin, $store);
78 $title = get_string('addstore', 'cache', $plugins[$plugin]['name']);
79 if ($mform->is_cancelled()) {
80 rediect($PAGE->url);
81 } else if ($data = $mform->get_data()) {
82 $config = cache_administration_helper::get_store_configuration_from_data($data);
83 $writer = cache_config_writer::instance();
167ad91e
SH
84 unset($config['lock']);
85 foreach ($writer->get_locks() as $lock => $lockconfig) {
86 if ($lock == $data->lock) {
87 $config['lock'] = $data->lock;
88 }
89 }
8139ad13
SH
90 $writer->edit_plugin_instance($data->name, $data->plugin, $config);
91 redirect($PAGE->url, get_string('editstoresuccess', 'cache', $plugins[$plugin]['name']), 5);
92 }
93 break;
94 case 'deletestore' :
95 $store = required_param('store', PARAM_TEXT);
96 $confirm = optional_param('confirm', false, PARAM_BOOL);
97
98 if (!array_key_exists($store, $stores)) {
99 $notifysuccess = false;
100 $notification = get_string('invalidstore');
101 } else if ($stores[$store]['mappings'] > 0) {
102 $notifysuccess = false;
103 $notification = get_string('deletestorehasmappings');
104 }
105
106 if ($notifysuccess) {
107 if (!$confirm) {
108 $title = get_string('confirmstoredeletion', 'cache');
109 $url = new moodle_url($PAGE->url, array('store' => $store, 'confirm' => 1, 'action' => $action, 'sesskey' => sesskey()));
110 $button = new single_button($url, get_string('deletestore', 'cache'));
111
112 $PAGE->set_title($title);
113 $PAGE->set_heading($SITE->fullname);
114 echo $OUTPUT->header();
115 echo $OUTPUT->heading($title);
116 echo $OUTPUT->confirm(get_string('deletestoreconfirmation', 'cache', $stores[$store]['name']), $button, $PAGE->url);
117 echo $OUTPUT->footer();
118 exit;
119 } else {
120 $writer = cache_config_writer::instance();
121 $writer->delete_store($store);
122 redirect($PAGE->url, get_string('deletestoresuccess', 'cache'), 5);
123 }
124 }
125 break;
126 case 'editdefinitionmapping' :
127 $definition = required_param('definition', PARAM_TEXT);
128 $title = get_string('editdefinitionmappings', 'cache', $definition);
129 $mform = new cache_definition_mappings_form($PAGE->url, array('definition' => $definition));
130 if ($mform->is_cancelled()) {
131 redirect($PAGE->url);
132 } else if ($data = $mform->get_data()) {
133 $writer = cache_config_writer::instance();
134 $mappings = array();
135 foreach ($data->mappings as $mapping) {
136 if (!empty($mapping)) {
137 $mappings[] = $mapping;
138 }
139 }
140 $writer->set_definition_mappings($definition, $mappings);
141 redirect($PAGE->url);
142 }
143 break;
144 case 'editmodemappings': // Edit default mode mappings
145 $mform = new cache_mode_mappings_form(null, $stores);
146 $mform->set_data(array(
147 'mode_'.cache_store::MODE_APPLICATION => key($defaultmodestores[cache_store::MODE_APPLICATION]),
148 'mode_'.cache_store::MODE_SESSION => key($defaultmodestores[cache_store::MODE_SESSION]),
149 'mode_'.cache_store::MODE_REQUEST => key($defaultmodestores[cache_store::MODE_REQUEST]),
150 ));
151 if ($mform->is_cancelled()) {
152 redirect($PAGE->url);
153 } else if ($data = $mform->get_data()) {
154 $mappings = array(
155 cache_store::MODE_APPLICATION => array($data->{'mode_'.cache_store::MODE_APPLICATION}),
156 cache_store::MODE_SESSION => array($data->{'mode_'.cache_store::MODE_SESSION}),
157 cache_store::MODE_REQUEST => array($data->{'mode_'.cache_store::MODE_REQUEST}),
158 );
159 $writer = cache_config_writer::instance();
160 $writer->set_mode_mappings($mappings);
161 redirect($PAGE->url);
162 }
163 break;
164
165 case 'purge': // Purge a store cache
166 // TODO
167 break;
168 }
169}
170
171$PAGE->set_title($title);
172$PAGE->set_heading($SITE->fullname);
173$renderer = $PAGE->get_renderer('core_cache');
174
175echo $renderer->header();
176echo $renderer->heading($title);
177
178if (!is_null($notification)) {
179 echo $renderer->notification($notification, ($notifysuccess)?'notifysuccess' : 'notifyproblem');
180}
181
182if ($mform instanceof moodleform) {
183 $mform->display();
184} else {
185 echo $renderer->plugin_summaries($plugins);
186 echo $renderer->store_summariers($stores, $plugins);
187 echo $renderer->definition_summaries($definitions, cache_administration_helper::get_definition_actions($context));
167ad91e 188 echo $renderer->lock_summaries($locks);
8139ad13
SH
189
190 $applicationstore = join(', ', $defaultmodestores[cache_store::MODE_APPLICATION]);
191 $sessionstore = join(', ', $defaultmodestores[cache_store::MODE_SESSION]);
192 $requeststore = join(', ', $defaultmodestores[cache_store::MODE_REQUEST]);
193 $editurl = new moodle_url('/cache/admin.php', array('action' => 'editmodemappings', 'sesskey' => sesskey()));
194 echo $renderer->mode_mappings($applicationstore, $sessionstore, $requeststore, $editurl);
195}
196
197echo $renderer->footer();