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