MDL-19349 upgrade_plugin_savepoint() added 2 suggestions
[moodle.git] / admin / xmldb / actions / view_structure_php / view_structure_php.class.php
CommitLineData
b0c7d558 1<?php // $Id$
2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.com //
9// //
dcbef199 10// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
b0c7d558 11// (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
12// //
13// This program is free software; you can redistribute it and/or modify //
14// it under the terms of the GNU General Public License as published by //
15// the Free Software Foundation; either version 2 of the License, or //
16// (at your option) any later version. //
17// //
18// This program is distributed in the hope that it will be useful, //
19// but WITHOUT ANY WARRANTY; without even the implied warranty of //
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
21// GNU General Public License for more details: //
22// //
23// http://www.gnu.org/copyleft/gpl.html //
24// //
25///////////////////////////////////////////////////////////////////////////
26
27/// This class will show the PHP needed to perform the desired action
28/// with the specified table.
29
30class view_structure_php extends XMLDBAction {
31
eef868d1 32 /**
b0c7d558 33 * Init method, every subclass will have its own
34 */
35 function init() {
36 parent::init();
37
38 /// Set own custom attributes
39
40 /// Get needed strings
41 $this->loadStrings(array(
42 'selectaction' => 'xmldb',
43 'selecttable' => 'xmldb',
44 'view' => 'xmldb',
45 'back' => 'xmldb'
46 ));
47 }
48
49 /**
50 * Invoke method, every class will have its own
51 * returns true/false on completion, setting both
52 * errormsg and output as necessary
53 */
54 function invoke() {
55 parent::invoke();
56
57 $result = true;
58
59 /// Set own core attributes
60 $this->does_generate = ACTION_GENERATE_HTML;
61
62 /// These are always here
63 global $CFG, $XMLDB;
64
65 /// Do the job, setting result as needed
66 /// Get the dir containing the file
67 $dirpath = required_param('dir', PARAM_PATH);
294ce987 68 $dirpath = $CFG->dirroot . $dirpath;
b0c7d558 69
70 /// Get the correct dirs
71 if (!empty($XMLDB->dbdirs)) {
72 $dbdir =& $XMLDB->dbdirs[$dirpath];
73 } else {
74 return false;
75 }
76 if (!empty($XMLDB->editeddirs)) {
77 $editeddir =& $XMLDB->editeddirs[$dirpath];
78 $structure =& $editeddir->xml_file->getStructure();
79 }
80 /// ADD YOUR CODE HERE
eef868d1 81
b0c7d558 82 $tables =& $structure->getTables();
83 $table = reset($tables);
84 $defaulttable = null;
85 if ($table) {
86 $defaulttable = $table->getName();
87 }
88
89 /// Get parameters
90 $commandparam = optional_param('command', 'create_table', PARAM_PATH);
91 $tableparam = optional_param('table', $defaulttable, PARAM_PATH);
92
93 /// The back to edit xml button
007456de 94 $b = ' <p class="centerpara buttons">';
b0c7d558 95 $b .= '<a href="index.php?action=edit_xml_file&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['back'] . ']</a>';
96 $b .= '</p>';
97 $o = $b;
98
99 /// Calculate the popup of commands
eef868d1 100 $commands = array('create_table',
b0c7d558 101 'drop_table',
ef856b2d 102 'rename_table');
b0c7d558 103 foreach ($commands as $command) {
104 $popcommands[$command] = str_replace('_', ' ', $command);
105 }
106 /// Calculate the popup of tables
107 foreach ($tables as $table) {
108 $poptables[$table->getName()] = $table->getName();
109 }
110 /// Now build the form
111 $o.= '<form id="form" action="index.php" method="post">';
007456de 112 $o.='<div>';
b0c7d558 113 $o.= ' <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot, '', $dirpath) . '" />';
114 $o.= ' <input type="hidden" name ="action" value="view_structure_php" />';
007456de 115 $o.= ' <table id="formelements" class="boxaligncenter" cellpadding="5">';
b0c7d558 116 $o.= ' <tr><td><label for="action" accesskey="c">' . $this->str['selectaction'] .' </label>' . choose_from_menu($popcommands, 'command', $commandparam, '', '', 0, true) . '&nbsp;<label for="table" accesskey="t">' . $this->str['selecttable'] . ' </label>' .choose_from_menu($poptables, 'table', $tableparam, '', '', 0, true) . '</td></tr>';
117 $o.= ' <tr><td colspan="2" align="center"><input type="submit" value="' .$this->str['view'] . '" /></td></tr>';
118 $o.= ' </table>';
007456de 119 $o.= '</div></form>';
120 $o.= ' <table id="phpcode" class="boxaligncenter" cellpadding="5">';
b0c7d558 121 $o.= ' <tr><td><textarea cols="80" rows="32">';
122 /// Based on current params, call the needed function
123 switch ($commandparam) {
124 case 'create_table':
125 $o.= s($this->create_table_php($structure, $tableparam));
126 break;
127 case 'drop_table':
128 $o.= s($this->drop_table_php($structure, $tableparam));
129 break;
130 case 'rename_table':
131 $o.= s($this->rename_table_php($structure, $tableparam));
132 break;
133 }
134 $o.= '</textarea></td></tr>';
135 $o.= ' </table>';
136
137 $this->output = $o;
138
139 /// Launch postaction if exists (leave this here!)
eef868d1 140 if ($this->getPostAction() && $result) {
b0c7d558 141 return $this->launch($this->getPostAction());
142 }
143
144 /// Return ok if arrived here
145 return $result;
146 }
147
148 /**
eef868d1 149 * This function will generate all the PHP code needed to
b0c7d558 150 * create one table using XMLDB objects and functions
eef868d1 151 *
46293bd7 152 * @param xmldb_structure structure object containing all the info
b0c7d558 153 * @param string table table code to be created
154 * @return string PHP code to be used to create the table
155 */
156 function create_table_php($structure, $table) {
157
158 $result = '';
159 /// Validate if we can do it
3ab430fb 160 if (!$table = $structure->getTable($table)) {
161 return false;
162 }
163 if ($table->getAllErrors()) {
164 return false;
165 }
b0c7d558 166
167 /// Add the standard PHP header
168 $result .= XMLDB_PHP_HEADER;
169
170 /// Add contents
3ab430fb 171 $result .= XMLDB_LINEFEED;
cb78437d 172 $result .= ' /// Define table ' . $table->getName() . ' to be created' . XMLDB_LINEFEED;
a8cb94f6 173 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
3ab430fb 174 $result .= XMLDB_LINEFEED;
175 $result .= ' /// Adding fields to table ' . $table->getName() . XMLDB_LINEFEED;
176 /// Iterate over each field
177 foreach ($table->getFields() as $field) {
178 /// The field header, with name
5eaa41e8 179 $result .= ' $table->add_field(' . "'" . $field->getName() . "', ";
3ab430fb 180 /// The field PHP specs
181 $result .= $field->getPHP(false);
182 /// The end of the line
183 $result .= ');' . XMLDB_LINEFEED;
184 }
185 /// Iterate over each key
186 if ($keys = $table->getKeys()) {
187 $result .= XMLDB_LINEFEED;
188 $result .= ' /// Adding keys to table ' . $table->getName() . XMLDB_LINEFEED;
189 foreach ($keys as $key) {
190 /// The key header, with name
5eaa41e8 191 $result .= ' $table->add_key(' . "'" . $key->getName() . "', ";
3ab430fb 192 /// The key PHP specs
193 $result .= $key->getPHP();
194 /// The end of the line
195 $result .= ');' . XMLDB_LINEFEED;
196 }
197 }
198 /// Iterate over each index
199 if ($indexes = $table->getIndexes()) {
200 $result .= XMLDB_LINEFEED;
201 $result .= ' /// Adding indexes to table ' . $table->getName() . XMLDB_LINEFEED;
202 foreach ($indexes as $index) {
203 /// The index header, with name
5eaa41e8 204 $result .= ' $table->add_index(' . "'" . $index->getName() . "', ";
3ab430fb 205 /// The index PHP specs
206 $result .= $index->getPHP();
207 /// The end of the line
208 $result .= ');' . XMLDB_LINEFEED;
209 }
210 }
211
212 /// Launch the proper DDL
213 $result .= XMLDB_LINEFEED;
70080fde 214 $result .= ' /// Conditionally launch create table for ' . $table->getName() . XMLDB_LINEFEED;
63dd5a62 215 $result .= ' if (!$dbman->table_exists($table)) {' . XMLDB_LINEFEED;
ef088e70 216 $result .= ' $dbman->create_table($table);' . XMLDB_LINEFEED;
70080fde 217 $result .= ' }' . XMLDB_LINEFEED;
b0c7d558 218
dcbef199 219 /// Add the proper upgrade_xxxx_savepoint call
220 $result .= $this->upgrade_savepoint_php ($structure);
221
b0c7d558 222 /// Add standard PHP footer
223 $result .= XMLDB_PHP_FOOTER;
224
225 return $result;
226 }
227
228 /**
eef868d1 229 * This function will generate all the PHP code needed to
b0c7d558 230 * drop one table using XMLDB objects and functions
eef868d1 231 *
46293bd7 232 * @param xmldb_structure structure object containing all the info
b0c7d558 233 * @param string table table code to be dropped
234 * @return string PHP code to be used to drop the table
235 */
236 function drop_table_php($structure, $table) {
237
238 $result = '';
239 /// Validate if we can do it
3ab430fb 240 if (!$table = $structure->getTable($table)) {
241 return false;
242 }
243 if ($table->getAllErrors()) {
244 return false;
245 }
b0c7d558 246
247 /// Add the standard PHP header
248 $result .= XMLDB_PHP_HEADER;
249
250 /// Add contents
386c23c9 251 $result .= XMLDB_LINEFEED;
cb78437d 252 $result .= ' /// Define table ' . $table->getName() . ' to be dropped' . XMLDB_LINEFEED;
a8cb94f6 253 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
386c23c9 254
255 /// Launch the proper DDL
256 $result .= XMLDB_LINEFEED;
70080fde 257 $result .= ' /// Conditionally launch drop table for ' . $table->getName() . XMLDB_LINEFEED;
63dd5a62 258 $result .= ' if ($dbman->table_exists($table)) {' . XMLDB_LINEFEED;
ef088e70 259 $result .= ' $dbman->drop_table($table);' . XMLDB_LINEFEED;
70080fde 260 $result .= ' }' . XMLDB_LINEFEED;
b0c7d558 261
dcbef199 262 /// Add the proper upgrade_xxxx_savepoint call
263 $result .= $this->upgrade_savepoint_php ($structure);
264
b0c7d558 265 /// Add standard PHP footer
266 $result .= XMLDB_PHP_FOOTER;
267
268 return $result;
269 }
270
271 /**
eef868d1 272 * This function will generate all the PHP code needed to
b0c7d558 273 * rename one table using XMLDB objects and functions
eef868d1 274 *
46293bd7 275 * @param xmldb_structure structure object containing all the info
b0c7d558 276 * @param string table table code to be renamed
277 * @return string PHP code to be used to rename the table
278 */
279 function rename_table_php($structure, $table) {
280
281 $result = '';
282 /// Validate if we can do it
3ab430fb 283 if (!$table = $structure->getTable($table)) {
284 return false;
285 }
286 if ($table->getAllErrors()) {
287 return false;
288 }
b0c7d558 289
290 /// Add the standard PHP header
291 $result .= XMLDB_PHP_HEADER;
292
293 /// Add contents
386c23c9 294 $result .= XMLDB_LINEFEED;
cb78437d 295 $result .= ' /// Define table ' . $table->getName() . ' to be renamed to NEWNAMEGOESHERE' . XMLDB_LINEFEED;
a8cb94f6 296 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
386c23c9 297
298 /// Launch the proper DDL
299 $result .= XMLDB_LINEFEED;
300 $result .= ' /// Launch rename table for ' . $table->getName() . XMLDB_LINEFEED;
ef088e70 301 $result .= ' $dbman->rename_table($table, ' . "'NEWNAMEGOESHERE'" . ');' . XMLDB_LINEFEED;
b0c7d558 302
dcbef199 303 /// Add the proper upgrade_xxxx_savepoint call
304 $result .= $this->upgrade_savepoint_php ($structure);
305
b0c7d558 306 /// Add standard PHP footer
307 $result .= XMLDB_PHP_FOOTER;
308
309 return $result;
310 }
dcbef199 311
312 /**
313 * This function will generate the PHP code needed to
314 * implement the upgrade_xxxx_savepoint() php calls in
315 * upgrade code generated from the editor
316 *
46293bd7 317 * @param xmldb_structure structure object containing all the info
dcbef199 318 * @return string PHP code to be used to stabilish a savepoint
319 */
320 function upgrade_savepoint_php ($structure) {
321
322 $path = $structure->getPath();
323
324 $result = '';
325
84774c55 326 /// Split path into components
327 $patharr = explode('/', $path);
328
329 switch ($patharr[0]) {
330 case 'lib':
dcbef199 331 $result = XMLDB_LINEFEED .
332 ' /// Main savepoint reached' . XMLDB_LINEFEED .
333 ' upgrade_main_savepoint($result, XXXXXXXXXX);' . XMLDB_LINEFEED;
334 break;
84774c55 335 case 'mod':
336 $result = XMLDB_LINEFEED .
337 ' /// ' . $patharr[1] . ' savepoint reached' . XMLDB_LINEFEED .
338 ' upgrade_mod_savepoint($result, XXXXXXXXXX, ' . "'$patharr[1]'" . ');' . XMLDB_LINEFEED;
339 break;
dcbef199 340 }
341 return $result;
342 }
b0c7d558 343}
344?>