MDL-15252 Fixed regression: XMLDB action using optionally defined strings
[moodle.git] / admin / xmldb / actions / XMLDBAction.class.php
CommitLineData
11e167a9 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * @package xmldb-editor
20 * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
24/**
25 * Main xmldb action clasee
26 *
27 * Main xmldb action class. It implements all the basic
28 * functionalities to be shared by each action.
29 *
30 * @package xmldb-editor
31 * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
8c15ae99 34class XMLDBAction {
35
36 var $does_generate; //Type of value returned by the invoke method
37 //ACTION_GENERATE_HTML have contents to show
38 //set by each specialized invoke
39
40 var $title; //Title of the Action (class name, by default)
41 //set by parent init automatically
42
43 var $str; //Strings used by the action
44 //set by each specialized init, calling loadStrings
45
46 var $output; //Output of the action
47 //set by each specialized invoke, get with getOutput
48
49 var $errormsg; //Last Error produced. Check when any invoke returns false
50 //get with getError
51
52 var $postaction; //Action to execute at the end of the invoke script
53
f25d53a9
EL
54 var $sesskey_protected; // Actions must be protected by sesskey mechanishm
55
8c15ae99 56 /**
57 * Constructor
58 */
59 function XMLDBAction() {
60 $this->init();
61 }
62
63 /**
64 * Constructor to keep PHP5 happy
65 */
66 function __construct() {
eef868d1 67 $this->XMLDBAction();
8c15ae99 68 }
69
eef868d1 70 /**
71 * Init method, every subclass will have its own,
8c15ae99 72 * always calling the parent one
73 */
74 function init() {
75 $this->does_generate = ACTION_NONE;
76 $this->title = strtolower(get_class($this));
77 $this->str = array();
78 $this->output = NULL;
79 $this->errormsg = NULL;
80 $this->subaction = NULL;
f25d53a9 81 $this->sesskey_protected = true;
8c15ae99 82 }
eef868d1 83
8c15ae99 84 /**
85 * returns the type of output of the file
86 */
87 function getDoesGenerate() {
88 return $this->does_generate;
89 }
90
91 /**
92 * getError method, returns the last error string.
93 * Used if the invoke() methods returns false
94 */
95 function getError() {
96 return $this->errormsg;
97 }
98
99 /**
100 * getOutput method, returns the output generated by the action.
101 * Used after execution of the invoke() methods if they return true
102 */
103 function getOutput() {
104 return $this->output;
105 }
106
107 /**
eef868d1 108 * getPostAtion method, returns the action to launch after executing
8c15ae99 109 * another one
110 */
111 function getPostAction() {
112 return $this->postaction;
113 }
114
eef868d1 115 /**
8c15ae99 116 * getTitle method returns the title of the action (that is part
117 * of the $str array attribute
118 */
119 function getTitle() {
120 return $this->str['title'];
121 }
122
123 /**
124 * loadStrings method, loads the required strings specified in the
125 * array parameter
126 */
127 function loadStrings($strings) {
128 /// Load some commonly used strings
55c6372e
DM
129 if (get_string_manager()->string_exists($this->title, 'xmldb')) {
130 $this->str['title'] = get_string($this->title, 'xmldb');
131 } else {
132 $this->str['title'] = $this->title;
133 }
8c15ae99 134
135 /// Now process the $strings array loading it in the $str atribute
136 if ($strings) {
137 foreach ($strings as $key => $module) {
138 $this->str[$key] = get_string($key, $module);
139 }
140 }
141 }
142
143 /**
f25d53a9
EL
144 * main invoke method, it sets the postaction attribute
145 * if possible and checks sesskey_protected if needed
8c15ae99 146 */
147 function invoke() {
148
149 global $SESSION;
eef868d1 150
f25d53a9
EL
151 /// Sesskey protection
152 if ($this->sesskey_protected) {
153 require_sesskey();
154 }
155
8c15ae99 156 /// If we are used any dir, save it in the lastused session object
157 /// Some actions can use it to perform positioning
077505db 158 if ($lastused = optional_param ('dir', NULL, PARAM_PATH)) {
294ce987 159 $SESSION->lastused = $lastused;
8c15ae99 160 }
eef868d1 161
8c15ae99 162 $this->postaction = optional_param ('postaction', NULL, PARAM_ALPHAEXT);
163 /// Avoid being recursive
164 if ($this->title == $this->postaction) {
165 $this->postaction = NULL;
166 }
167 }
168
169 /**
170 * launch method, used to easily call invoke methods between actions
171 */
172 function launch($action) {
173
174 global $CFG;
175
176 /// Get the action path and invoke it
177 $actionsroot = "$CFG->dirroot/$CFG->admin/xmldb/actions";
178 $actionclass = $action . '.class.php';
179 $actionpath = "$actionsroot/$action/$actionclass";
180
181 /// Load and invoke the proper action
182 $result = false;
183 if (file_exists($actionpath) && is_readable($actionpath)) {
184 require_once($actionpath);
eef868d1 185 if ($xmldb_action = new $action) {
8c15ae99 186 $result = $xmldb_action->invoke();
187 if ($result) {
188 if ($xmldb_action->does_generate != ACTION_NONE &&
189 $xmldb_action->getOutput()) {
190 $this->does_generate = $xmldb_action->does_generate;
191 $this->title = $xmldb_action->title;
192 $this->str = $xmldb_action->str;
193 $this->output .= $xmldb_action->getOutput();
194 }
195 } else {
196 $this->errormsg = $xmldb_action->getError();
197 }
198 } else {
199 $this->errormsg = "Error: cannot instantiate class (actions/$action/$actionclass)";
200 }
201 } else {
202 $this->errormsg = "Error: wrong action specified ($action)";
203 }
204 return $result;
205 }
11e167a9 206
207 /**
208 * This function will generate the PHP code needed to
209 * implement the upgrade_xxxx_savepoint() php calls in
210 * upgrade code generated from the editor. It's used by
211 * the view_structure_php and view_table_php actions
212 *
213 * @param xmldb_structure structure object containing all the info
214 * @return string PHP code to be used to stabilish a savepoint
215 */
17da2e6f 216 function upgrade_savepoint_php($structure) {
11e167a9 217
218 $path = $structure->getPath();
219
220 /// Trim "db" from path
221 $path = dirname($path);
222
11e167a9 223 /// Get pluginname, plugindir and plugintype
224 $pluginname = basename($path);
225 if ($path == 'lib') { /// exception for lib (not proper plugin)
226 $plugindir = 'lib';
227 $plugintype = 'lib';
228 } else { /// rest of plugins
17da2e6f 229 //TODO: this is not nice and may fail, plugintype should be passed around somehow instead
230 $plugintypes = get_plugin_types(false);
11e167a9 231 $plugindir = dirname($path);
17da2e6f 232 $plugindir = str_replace('\\', '/', $plugindir);
11e167a9 233 $plugintype = array_search($plugindir, $plugintypes);
234 }
235
236 $result = '';
237
238 switch ($plugintype ) {
239 case 'lib': /// has own savepoint function
240 $result = XMLDB_LINEFEED .
241 ' /// Main savepoint reached' . XMLDB_LINEFEED .
242 ' upgrade_main_savepoint($result, XXXXXXXXXX);' . XMLDB_LINEFEED;
243 break;
244 case 'mod': /// has own savepoint function
245 $result = XMLDB_LINEFEED .
246 ' /// ' . $pluginname . ' savepoint reached' . XMLDB_LINEFEED .
247 ' upgrade_mod_savepoint($result, XXXXXXXXXX, ' . "'$pluginname'" . ');' . XMLDB_LINEFEED;
248 break;
249 case 'block': /// has own savepoint function
250 $result = XMLDB_LINEFEED .
251 ' /// ' . $pluginname . ' savepoint reached' . XMLDB_LINEFEED .
252 ' upgrade_block_savepoint($result, XXXXXXXXXX, ' . "'$pluginname'" . ');' . XMLDB_LINEFEED;
253 break;
254 default: /// rest of plugins
255 $result = XMLDB_LINEFEED .
256 ' /// ' . $pluginname . ' savepoint reached' . XMLDB_LINEFEED .
257 ' upgrade_plugin_savepoint($result, XXXXXXXXXX, ' . "'$plugintype'" . ', ' . "'$pluginname'" . ');' . XMLDB_LINEFEED;
258 }
259 return $result;
260 }
8c15ae99 261}