MDL-21432 backup - make itemname bigger so it can store file component/area named
[moodle.git] / admin / xmldb / actions / view_table_php / view_table_php.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 * This class will show the PHP needed (upgrade block) to perform
26 * the desired DDL action with the specified field/key/index
27 *
28 * @package xmldb-editor
29 * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 */
c5a6ae17 32class view_table_php extends XMLDBAction {
33
eef868d1 34 /**
c5a6ae17 35 * Init method, every subclass will have its own
36 */
37 function init() {
38 parent::init();
39
40 /// Set own custom attributes
f25d53a9 41 $this->sesskey_protected = false; // This action doesn't need sesskey protection
c5a6ae17 42
43 /// Get needed strings
44 $this->loadStrings(array(
45 'selectaction' => 'xmldb',
46 'selectfieldkeyindex' => 'xmldb',
47 'view' => 'xmldb',
eafc2b70 48 'table' => 'xmldb',
313b5a35 49 'selectonecommand' => 'xmldb',
eafc2b70 50 'selectonefieldkeyindex' => 'xmldb',
313b5a35 51 'mustselectonefield' => 'xmldb',
52 'mustselectonekey' => 'xmldb',
53 'mustselectoneindex' => 'xmldb',
c5a6ae17 54 'back' => 'xmldb'
55 ));
56 }
57
58 /**
59 * Invoke method, every class will have its own
60 * returns true/false on completion, setting both
61 * errormsg and output as necessary
62 */
63 function invoke() {
64 parent::invoke();
65
66 $result = true;
67
68 /// Set own core attributes
69 $this->does_generate = ACTION_GENERATE_HTML;
70
71 /// These are always here
b324b47e 72 global $CFG, $XMLDB, $OUTPUT;
c5a6ae17 73
74 /// Do the job, setting result as needed
75 /// Get the dir containing the file
76 $dirpath = required_param('dir', PARAM_PATH);
294ce987 77 $dirpath = $CFG->dirroot . $dirpath;
c5a6ae17 78
79 /// Get the correct dirs
80 if (!empty($XMLDB->dbdirs)) {
81 $dbdir =& $XMLDB->dbdirs[$dirpath];
82 } else {
83 return false;
84 }
85 if (!empty($XMLDB->editeddirs)) {
86 $editeddir =& $XMLDB->editeddirs[$dirpath];
87 $structure =& $editeddir->xml_file->getStructure();
88 }
89 /// ADD YOUR CODE HERE
90
91 $tableparam = required_param('table', PARAM_PATH);
eef868d1 92
c5a6ae17 93 $table =& $structure->getTable($tableparam);
94 $fields = $table->getFields();
95 $field = reset($fields);
96 $defaultfieldkeyindex = null;
97 if ($field) {
eafc2b70 98 $defaultfieldkeyindex = 'f#' . $field->getName();
c5a6ae17 99 }
eafc2b70 100 $keys = $table->getKeys();
101 $indexes = $table->getIndexes();
c5a6ae17 102
103 /// Get parameters
104 $commandparam = optional_param('command', 'add_field', PARAM_PATH);
3be91b01 105 $origfieldkeyindexparam = optional_param('fieldkeyindex', $defaultfieldkeyindex, PARAM_PATH);
106 $fieldkeyindexparam = preg_replace('/[fki]#/i', '', $origfieldkeyindexparam); ///Strip the initials
313b5a35 107 $fieldkeyindexinitial = substr($origfieldkeyindexparam, 0, 1); //To know what we have selected
c5a6ae17 108
109 /// The back to edit xml button
007456de 110 $b = ' <p class="centerpara buttons">';
eafc2b70 111 $b .= '<a href="index.php?action=edit_table&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&amp;table=' . $tableparam . '">[' . $this->str['back'] . ']</a>';
c5a6ae17 112 $b .= '</p>';
113 $o = $b;
114
eafc2b70 115 /// The table currently being edited
116 $o .= '<h3 class="main">' . $this->str['table'] . ': ' . s($tableparam) . '</h3>';
117
313b5a35 118 /// To indent the menu selections
119 $optionspacer = '&nbsp;&nbsp;&nbsp;';
120
c5a6ae17 121 /// Calculate the popup of commands
313b5a35 122 $commands = array('Fields',
123 $optionspacer . 'add_field',
124 $optionspacer . 'drop_field',
49173d25 125 $optionspacer . 'rename_field',
4fae686b 126 $optionspacer . 'change_field_type',
313b5a35 127 $optionspacer . 'change_field_precision',
128 $optionspacer . 'change_field_unsigned',
129 $optionspacer . 'change_field_notnull',
313b5a35 130 $optionspacer . 'change_field_default',
2a88f626 131 $optionspacer . 'drop_enum_from_field', /// TODO: Moodle 2.1 - Drop drop_enum_from_field
313b5a35 132 'Keys',
ceac2552 133 $optionspacer . 'add_key',
134 $optionspacer . 'drop_key',
135 $optionspacer . 'rename_key',
136 'Indexes',
137 $optionspacer . 'add_index',
138 $optionspacer . 'drop_index',
139 $optionspacer . 'rename_index');
c5a6ae17 140 foreach ($commands as $command) {
313b5a35 141 $popcommands[str_replace($optionspacer, '', $command)] = str_replace('_', ' ', $command);
c5a6ae17 142 }
eafc2b70 143 /// Calculate the popup of fields/keys/indexes
eafc2b70 144 if ($fields) {
3be91b01 145 $popfields['fieldshead'] = 'Fields';
eafc2b70 146 foreach ($fields as $field) {
3be91b01 147 $popfields['f#' . $field->getName()] = $optionspacer . $field->getName();
eafc2b70 148 }
149 }
150 if ($keys) {
3be91b01 151 $popfields['keyshead'] = 'Keys';
eafc2b70 152 foreach ($keys as $key) {
3be91b01 153 $popfields['k#' . $key->getName()] = $optionspacer . $key->getName();
eafc2b70 154 }
155 }
156 if ($indexes) {
3be91b01 157 $popfields['indexeshead'] = 'Indexes';
eafc2b70 158 foreach ($indexes as $index) {
3be91b01 159 $popfields['i#' . $index->getName()] = $optionspacer . $index->getName();
eafc2b70 160 }
c5a6ae17 161 }
eafc2b70 162
c5a6ae17 163 /// Now build the form
164 $o.= '<form id="form" action="index.php" method="post">';
007456de 165 $o.= '<div>';
c5a6ae17 166 $o.= ' <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot, '', $dirpath) . '" />';
eafc2b70 167 $o.= ' <input type="hidden" name ="table" value="' . s($tableparam) . '" />';
c5a6ae17 168 $o.= ' <input type="hidden" name ="action" value="view_table_php" />';
007456de 169 $o.= ' <table id="formelements" class="boxaligncenter" cellpadding="5">';
d776d59e 170 $o.= ' <tr><td><label for="action" accesskey="c">' . $this->str['selectaction'] .' </label>' . html_writer::select($popcommands, 'command', $commandparam, false) . '&nbsp;<label for="fieldkeyindex" accesskey="f">' . $this->str['selectfieldkeyindex'] . ' </label>' .html_writer::select($popfields, 'fieldkeyindex', $origfieldkeyindexparam, false) . '</td></tr>';
c5a6ae17 171 $o.= ' <tr><td colspan="2" align="center"><input type="submit" value="' .$this->str['view'] . '" /></td></tr>';
172 $o.= ' </table>';
007456de 173 $o.= '</div></form>';
eafc2b70 174
007456de 175 $o.= ' <table id="phpcode" class="boxaligncenter" cellpadding="5">';
c5a6ae17 176 $o.= ' <tr><td><textarea cols="80" rows="32">';
eafc2b70 177 /// Check we have selected some field/key/index from the popup
178 if ($fieldkeyindexparam == 'fieldshead' || $fieldkeyindexparam == 'keyshead' || $fieldkeyindexparam == 'indexeshead') {
179 $o.= s($this->str['selectonefieldkeyindex']);
313b5a35 180 /// Check we have selected some command from the popup
181 } else if ($commandparam == 'Fields' || $commandparam == 'Keys' || $commandparam == 'Indexes') {
182 $o.= s($this->str['selectonecommand']);
183 } else {
eafc2b70 184 /// Based on current params, call the needed function
185 switch ($commandparam) {
3be91b01 186 case 'add_field':
313b5a35 187 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
188 $o.= s($this->add_field_php($structure, $tableparam, $fieldkeyindexparam));
189 } else {
190 $o.= $this->str['mustselectonefield'];
191 }
eafc2b70 192 break;
3be91b01 193 case 'drop_field':
313b5a35 194 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
195 $o.= s($this->drop_field_php($structure, $tableparam, $fieldkeyindexparam));
196 } else {
197 $o.= $this->str['mustselectonefield'];
198 }
eafc2b70 199 break;
3be91b01 200 case 'rename_field':
313b5a35 201 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
202 $o.= s($this->rename_field_php($structure, $tableparam, $fieldkeyindexparam));
203 } else {
204 $o.= $this->str['mustselectonefield'];
205 }
eafc2b70 206 break;
4fae686b 207 case 'change_field_type':
208 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
209 $o.= s($this->change_field_type_php($structure, $tableparam, $fieldkeyindexparam));
210 } else {
211 $o.= $this->str['mustselectonefield'];
212 }
213 break;
1934b522 214 case 'change_field_precision':
313b5a35 215 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
216 $o.= s($this->change_field_precision_php($structure, $tableparam, $fieldkeyindexparam));
217 } else {
218 $o.= $this->str['mustselectonefield'];
219 }
1934b522 220 break;
221 case 'change_field_unsigned':
313b5a35 222 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
223 $o.= s($this->change_field_unsigned_php($structure, $tableparam, $fieldkeyindexparam));
224 } else {
225 $o.= $this->str['mustselectonefield'];
226 }
1934b522 227 break;
228 case 'change_field_notnull':
313b5a35 229 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
230 $o.= s($this->change_field_notnull_php($structure, $tableparam, $fieldkeyindexparam));
231 } else {
232 $o.= $this->str['mustselectonefield'];
233 }
1934b522 234 break;
2a88f626 235 case 'drop_enum_from_field': /// TODO: Moodle 2.1 - Drop drop_enum_from_field
49173d25 236 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
2a88f626 237 $o.= s($this->drop_enum_from_field_php($structure, $tableparam, $fieldkeyindexparam));
49173d25 238 } else {
239 $o.= $this->str['mustselectonefield'];
240 }
241 break;
1934b522 242 case 'change_field_default':
313b5a35 243 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
244 $o.= s($this->change_field_default_php($structure, $tableparam, $fieldkeyindexparam));
245 } else {
246 $o.= $this->str['mustselectonefield'];
247 }
1934b522 248 break;
ceac2552 249 case 'add_key':
250 if ($fieldkeyindexinitial == 'k') { //Only if we have got one key
251 $o.= s($this->add_key_php($structure, $tableparam, $fieldkeyindexparam));
252 } else {
253 $o.= $this->str['mustselectonekey'];
254 }
255 break;
256 case 'drop_key':
257 if ($fieldkeyindexinitial == 'k') { //Only if we have got one key
258 $o.= s($this->drop_key_php($structure, $tableparam, $fieldkeyindexparam));
259 } else {
260 $o.= $this->str['mustselectonekey'];
261 }
262 break;
263 case 'rename_key':
264 if ($fieldkeyindexinitial == 'k') { //Only if we have got one key
265 $o.= s($this->rename_key_php($structure, $tableparam, $fieldkeyindexparam));
266 } else {
267 $o.= $this->str['mustselectonekey'];
268 }
269 break;
270 case 'add_index':
271 if ($fieldkeyindexinitial == 'i') { //Only if we have got one index
272 $o.= s($this->add_index_php($structure, $tableparam, $fieldkeyindexparam));
273 } else {
274 $o.= $this->str['mustselectoneindex'];
275 }
276 break;
277 case 'drop_index':
278 if ($fieldkeyindexinitial == 'i') { //Only if we have got one index
279 $o.= s($this->drop_index_php($structure, $tableparam, $fieldkeyindexparam));
280 } else {
281 $o.= $this->str['mustselectoneindex'];
282 }
283 break;
284 case 'rename_index':
285 if ($fieldkeyindexinitial == 'i') { //Only if we have got one index
286 $o.= s($this->rename_index_php($structure, $tableparam, $fieldkeyindexparam));
287 } else {
288 $o.= $this->str['mustselectoneindex'];
289 }
290 break;
eafc2b70 291 }
c5a6ae17 292 }
293 $o.= '</textarea></td></tr>';
294 $o.= ' </table>';
295
296 $this->output = $o;
297
298 /// Launch postaction if exists (leave this here!)
eef868d1 299 if ($this->getPostAction() && $result) {
c5a6ae17 300 return $this->launch($this->getPostAction());
301 }
302
303 /// Return ok if arrived here
304 return $result;
305 }
306
307 /**
eef868d1 308 * This function will generate all the PHP code needed to
3be91b01 309 * create one field using XMLDB objects and functions
eef868d1 310 *
46293bd7 311 * @param xmldb_structure structure object containing all the info
3be91b01 312 * @param string table table name
313 * @param string field field name to be created
314 * @return string PHP code to be used to create the field
c5a6ae17 315 */
3be91b01 316 function add_field_php($structure, $table, $field) {
c5a6ae17 317
318 $result = '';
319 /// Validate if we can do it
320 if (!$table = $structure->getTable($table)) {
321 return false;
322 }
3be91b01 323 if (!$field = $table->getField($field)) {
324 return false;
325 }
c5a6ae17 326 if ($table->getAllErrors()) {
327 return false;
328 }
329
330 /// Add the standard PHP header
331 $result .= XMLDB_PHP_HEADER;
332
333 /// Add contents
334 $result .= XMLDB_LINEFEED;
3be91b01 335 $result .= ' /// Define field ' . $field->getName() . ' to be added to ' . $table->getName() . XMLDB_LINEFEED;
a8cb94f6 336 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
69b80cc2 337 $result .= ' $field = new xmldb_field(' . "'" . $field->getName() . "', " . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
c5a6ae17 338
339 /// Launch the proper DDL
340 $result .= XMLDB_LINEFEED;
82bba3e4 341 $result .= ' /// Conditionally launch add field ' . $field->getName() . XMLDB_LINEFEED;
cdb1e987 342 $result .= ' if (!$dbman->field_exists($table, $field)) {'. XMLDB_LINEFEED;
ef088e70 343 $result .= ' $dbman->add_field($table, $field);' . XMLDB_LINEFEED;
82bba3e4 344 $result .= ' }'. XMLDB_LINEFEED;
c5a6ae17 345
dcbef199 346 /// Add the proper upgrade_xxxx_savepoint call
347 $result .= $this->upgrade_savepoint_php ($structure);
348
c5a6ae17 349 /// Add standard PHP footer
350 $result .= XMLDB_PHP_FOOTER;
351
352 return $result;
353 }
354
355 /**
eef868d1 356 * This function will generate all the PHP code needed to
3be91b01 357 * drop one field using XMLDB objects and functions
eef868d1 358 *
46293bd7 359 * @param xmldb_structure structure object containing all the info
3be91b01 360 * @param string table table name
361 * @param string field field name to be dropped
362 * @return string PHP code to be used to drop the field
c5a6ae17 363 */
3be91b01 364 function drop_field_php($structure, $table, $field) {
c5a6ae17 365
366 $result = '';
367 /// Validate if we can do it
368 if (!$table = $structure->getTable($table)) {
369 return false;
370 }
3be91b01 371 if (!$field = $table->getField($field)) {
372 return false;
373 }
c5a6ae17 374 if ($table->getAllErrors()) {
375 return false;
376 }
377
378 /// Add the standard PHP header
379 $result .= XMLDB_PHP_HEADER;
380
381 /// Add contents
382 $result .= XMLDB_LINEFEED;
3be91b01 383 $result .= ' /// Define field ' . $field->getName() . ' to be dropped from ' . $table->getName() . XMLDB_LINEFEED;
a8cb94f6 384 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
385 $result .= ' $field = new xmldb_field(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
c5a6ae17 386
387 /// Launch the proper DDL
388 $result .= XMLDB_LINEFEED;
82bba3e4 389 $result .= ' /// Conditionally launch drop field ' . $field->getName() . XMLDB_LINEFEED;
cdb1e987 390 $result .= ' if ($dbman->field_exists($table, $field)) {' . XMLDB_LINEFEED;
ef088e70 391 $result .= ' $dbman->drop_field($table, $field);' . XMLDB_LINEFEED;
82bba3e4 392 $result .= ' }' . XMLDB_LINEFEED;
c5a6ae17 393
dcbef199 394 /// Add the proper upgrade_xxxx_savepoint call
395 $result .= $this->upgrade_savepoint_php ($structure);
396
c5a6ae17 397 /// Add standard PHP footer
398 $result .= XMLDB_PHP_FOOTER;
399
400 return $result;
401 }
402
403 /**
eef868d1 404 * This function will generate all the PHP code needed to
3be91b01 405 * rename one field using XMLDB objects and functions
eef868d1 406 *
46293bd7 407 * @param xmldb_structure structure object containing all the info
3be91b01 408 * @param string table table name
409 * @param string field field name to be renamed
ceac2552 410 * @return string PHP code to be used to rename the field
c5a6ae17 411 */
3be91b01 412 function rename_field_php($structure, $table, $field) {
c5a6ae17 413
414 $result = '';
415 /// Validate if we can do it
416 if (!$table = $structure->getTable($table)) {
417 return false;
418 }
3be91b01 419 if (!$field = $table->getField($field)) {
420 return false;
421 }
c5a6ae17 422 if ($table->getAllErrors()) {
423 return false;
424 }
425
426 /// Add the standard PHP header
427 $result .= XMLDB_PHP_HEADER;
428
429 /// Add contents
430 $result .= XMLDB_LINEFEED;
3be91b01 431 $result .= ' /// Rename field ' . $field->getName() . ' on table ' . $table->getName() . ' to NEWNAMEGOESHERE'. XMLDB_LINEFEED;
a8cb94f6 432 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
69b80cc2 433 $result .= ' $field = new xmldb_field(' . "'" . $field->getName() . "', " . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
c5a6ae17 434
435 /// Launch the proper DDL
436 $result .= XMLDB_LINEFEED;
3be91b01 437 $result .= ' /// Launch rename field ' . $field->getName() . XMLDB_LINEFEED;
ef088e70 438 $result .= ' $dbman->rename_field($table, $field, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED;
c5a6ae17 439
dcbef199 440 /// Add the proper upgrade_xxxx_savepoint call
441 $result .= $this->upgrade_savepoint_php ($structure);
442
c5a6ae17 443 /// Add standard PHP footer
444 $result .= XMLDB_PHP_FOOTER;
445
446 return $result;
447 }
3be91b01 448
4fae686b 449 /**
450 * This function will generate all the PHP code needed to
451 * change the type of one field using XMLDB objects and functions.
452 * Currently these conversions are supported:
453 * integer to char
454 * char to integer
455 * number to char
456 * char to number
457 * float to char
458 * char to float
459 *
46293bd7 460 * @param xmldb_structure structure object containing all the info
4fae686b 461 * @param string table table name
462 * @param string field field name to change precision
463 */
464 function change_field_type_php($structure, $table, $field) {
465
466 $result = '';
467 /// Validate if we can do it
468 if (!$table = $structure->getTable($table)) {
469 return false;
470 }
471 if (!$field = $table->getField($field)) {
472 return false;
473 }
474 if ($table->getAllErrors()) {
475 return false;
476 }
477
478 /// Calculate the type tip text
479 $type = $field->getXMLDBTypeName($field->getType());
480
481 /// Add the standard PHP header
482 $result .= XMLDB_PHP_HEADER;
483
484 /// Add contents
485 $result .= XMLDB_LINEFEED;
486 $result .= ' /// Changing type of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $type . XMLDB_LINEFEED;
a8cb94f6 487 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
69b80cc2 488 $result .= ' $field = new xmldb_field(' . "'" . $field->getName() . "', " . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
4fae686b 489
490 /// Launch the proper DDL
491 $result .= XMLDB_LINEFEED;
492 $result .= ' /// Launch change of type for field ' . $field->getName() . XMLDB_LINEFEED;
ef088e70 493 $result .= ' $dbman->change_field_type($table, $field);' . XMLDB_LINEFEED;
4fae686b 494
dcbef199 495 /// Add the proper upgrade_xxxx_savepoint call
496 $result .= $this->upgrade_savepoint_php ($structure);
497
4fae686b 498 /// Add standard PHP footer
499 $result .= XMLDB_PHP_FOOTER;
500
501 return $result;
502 }
503
1934b522 504 /**
505 * This function will generate all the PHP code needed to
506 * change the precision of one field using XMLDB objects and functions
507 *
46293bd7 508 * @param xmldb_structure structure object containing all the info
1934b522 509 * @param string table table name
510 * @param string field field name to change precision
511 */
512 function change_field_precision_php($structure, $table, $field) {
513
514 $result = '';
515 /// Validate if we can do it
516 if (!$table = $structure->getTable($table)) {
517 return false;
518 }
519 if (!$field = $table->getField($field)) {
520 return false;
521 }
522 if ($table->getAllErrors()) {
523 return false;
524 }
525
526 /// Calculate the precision tip text
527 $precision = '(' . $field->getLength();
528 if ($field->getDecimals()) {
529 $precision .= ', ' . $field->getDecimals();
530 }
531 $precision .= ')';
532
533 /// Add the standard PHP header
534 $result .= XMLDB_PHP_HEADER;
535
536 /// Add contents
537 $result .= XMLDB_LINEFEED;
538 $result .= ' /// Changing precision of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $precision . XMLDB_LINEFEED;
a8cb94f6 539 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
69b80cc2 540 $result .= ' $field = new xmldb_field(' . "'" . $field->getName() . "', " .$field->getPHP(true) . ');' . XMLDB_LINEFEED;
1934b522 541
542 /// Launch the proper DDL
543 $result .= XMLDB_LINEFEED;
544 $result .= ' /// Launch change of precision for field ' . $field->getName() . XMLDB_LINEFEED;
ef088e70 545 $result .= ' $dbman->change_field_precision($table, $field);' . XMLDB_LINEFEED;
1934b522 546
dcbef199 547 /// Add the proper upgrade_xxxx_savepoint call
548 $result .= $this->upgrade_savepoint_php ($structure);
549
1934b522 550 /// Add standard PHP footer
551 $result .= XMLDB_PHP_FOOTER;
552
553 return $result;
554 }
555
556 /**
557 * This function will generate all the PHP code needed to
558 * change the unsigned/signed of one field using XMLDB objects and functions
559 *
46293bd7 560 * @param xmldb_structure structure object containing all the info
1934b522 561 * @param string table table name
562 * @param string field field name to change unsigned/signed
563 */
564 function change_field_unsigned_php($structure, $table, $field) {
565
566 $result = '';
567 /// Validate if we can do it
568 if (!$table = $structure->getTable($table)) {
569 return false;
570 }
571 if (!$field = $table->getField($field)) {
572 return false;
573 }
574 if ($table->getAllErrors()) {
575 return false;
576 }
577
578 /// Calculate the unsigned tip text
579 $unsigned = $field->getUnsigned() ? 'unsigned' : 'signed';
580
581 /// Add the standard PHP header
582 $result .= XMLDB_PHP_HEADER;
583
584 /// Add contents
585 $result .= XMLDB_LINEFEED;
586 $result .= ' /// Changing sign of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $unsigned . XMLDB_LINEFEED;
a8cb94f6 587 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
69b80cc2 588 $result .= ' $field = new xmldb_field(' . "'" . $field->getName() . "', " . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
1934b522 589
590 /// Launch the proper DDL
591 $result .= XMLDB_LINEFEED;
592 $result .= ' /// Launch change of sign for field ' . $field->getName() . XMLDB_LINEFEED;
ef088e70 593 $result .= ' $dbman->change_field_unsigned($table, $field);' . XMLDB_LINEFEED;
1934b522 594
dcbef199 595 /// Add the proper upgrade_xxxx_savepoint call
596 $result .= $this->upgrade_savepoint_php ($structure);
597
1934b522 598 /// Add standard PHP footer
599 $result .= XMLDB_PHP_FOOTER;
600
601 return $result;
602 }
603
604 /**
605 * This function will generate all the PHP code needed to
606 * change the nullability of one field using XMLDB objects and functions
607 *
46293bd7 608 * @param xmldb_structure structure object containing all the info
1934b522 609 * @param string table table name
610 * @param string field field name to change null/not null
611 */
612 function change_field_notnull_php($structure, $table, $field) {
613
614 $result = '';
615 /// Validate if we can do it
616 if (!$table = $structure->getTable($table)) {
617 return false;
618 }
619 if (!$field = $table->getField($field)) {
620 return false;
621 }
622 if ($table->getAllErrors()) {
623 return false;
624 }
625
626 /// Calculate the notnull tip text
627 $notnull = $field->getNotnull() ? 'not null' : 'null';
628
629 /// Add the standard PHP header
630 $result .= XMLDB_PHP_HEADER;
631
632 /// Add contents
633 $result .= XMLDB_LINEFEED;
634 $result .= ' /// Changing nullability of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $notnull . XMLDB_LINEFEED;
a8cb94f6 635 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
69b80cc2 636 $result .= ' $field = new xmldb_field(' . "'" . $field->getName() . "', " . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
1934b522 637
638 /// Launch the proper DDL
639 $result .= XMLDB_LINEFEED;
640 $result .= ' /// Launch change of nullability for field ' . $field->getName() . XMLDB_LINEFEED;
ef088e70 641 $result .= ' $dbman->change_field_notnull($table, $field);' . XMLDB_LINEFEED;
1934b522 642
dcbef199 643 /// Add the proper upgrade_xxxx_savepoint call
644 $result .= $this->upgrade_savepoint_php ($structure);
645
1934b522 646 /// Add standard PHP footer
647 $result .= XMLDB_PHP_FOOTER;
648
649 return $result;
650 }
651
49173d25 652 /**
653 * This function will generate all the PHP code needed to
2a88f626 654 * drop the enum values (check constraint) of one field
49173d25 655 * using XMLDB objects and functions
656 *
2a88f626 657 * Note this function is here as part of the process of
658 * dropping enums completely from Moodle 2.0: MDL-18577
659 * and will be out in Moodle 2.1
660 * TODO: Moodle 2.1 - Drop drop_enum_from_field_php
661 *
46293bd7 662 * @param xmldb_structure structure object containing all the info
49173d25 663 * @param string table table name
664 * @param string field field name to change its enum
665 */
2a88f626 666 function drop_enum_from_field_php($structure, $table, $field) {
49173d25 667
668 $result = '';
669 /// Validate if we can do it
670 if (!$table = $structure->getTable($table)) {
671 return false;
672 }
673 if (!$field = $table->getField($field)) {
674 return false;
675 }
676 if ($table->getAllErrors()) {
677 return false;
678 }
679
49173d25 680 /// Add the standard PHP header
681 $result .= XMLDB_PHP_HEADER;
682
683 /// Add contents
684 $result .= XMLDB_LINEFEED;
2a88f626 685 $result .= ' /// Drop list of values (enum) from field ' . $field->getName() . ' on table ' . $table->getName() . XMLDB_LINEFEED;
a8cb94f6 686 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
69b80cc2 687 $result .= ' $field = new xmldb_field(' . "'" . $field->getName() . "', " . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
49173d25 688
689 /// Launch the proper DDL
690 $result .= XMLDB_LINEFEED;
2a88f626 691 $result .= ' /// Launch drop of list of values from field ' . $field->getName() . XMLDB_LINEFEED;
692 $result .= ' $dbman->drop_enum_from_field($table, $field);' . XMLDB_LINEFEED;
49173d25 693
dcbef199 694 /// Add the proper upgrade_xxxx_savepoint call
695 $result .= $this->upgrade_savepoint_php ($structure);
696
49173d25 697 /// Add standard PHP footer
698 $result .= XMLDB_PHP_FOOTER;
699
700 return $result;
701 }
702
1934b522 703 /**
704 * This function will generate all the PHP code needed to
705 * change the default of one field using XMLDB objects and functions
706 *
46293bd7 707 * @param xmldb_structure structure object containing all the info
1934b522 708 * @param string table table name
709 * @param string field field name to change null/not null
710 */
711 function change_field_default_php($structure, $table, $field) {
712
713 $result = '';
714 /// Validate if we can do it
715 if (!$table = $structure->getTable($table)) {
716 return false;
717 }
718 if (!$field = $table->getField($field)) {
719 return false;
720 }
721 if ($table->getAllErrors()) {
722 return false;
723 }
724
725 /// Calculate the default tip text
726 $default = $field->getDefault() === null ? 'drop it' : $field->getDefault();
727
728 /// Add the standard PHP header
729 $result .= XMLDB_PHP_HEADER;
730
731 /// Add contents
732 $result .= XMLDB_LINEFEED;
733 $result .= ' /// Changing the default of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $default . XMLDB_LINEFEED;
a8cb94f6 734 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
69b80cc2 735 $result .= ' $field = new xmldb_field(' . "'" . $field->getName() . "', " . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
1934b522 736
737 /// Launch the proper DDL
738 $result .= XMLDB_LINEFEED;
739 $result .= ' /// Launch change of default for field ' . $field->getName() . XMLDB_LINEFEED;
ef088e70 740 $result .= ' $dbman->change_field_default($table, $field);' . XMLDB_LINEFEED;
1934b522 741
dcbef199 742 /// Add the proper upgrade_xxxx_savepoint call
743 $result .= $this->upgrade_savepoint_php ($structure);
744
1934b522 745 /// Add standard PHP footer
746 $result .= XMLDB_PHP_FOOTER;
747
748 return $result;
749 }
750
ceac2552 751 /**
752 * This function will generate all the PHP code needed to
753 * create one key using XMLDB objects and functions
754 *
46293bd7 755 * @param xmldb_structure structure object containing all the info
ceac2552 756 * @param string table table name
757 * @param string key key name to be created
758 * @return string PHP code to be used to create the key
759 */
760 function add_key_php($structure, $table, $key) {
761
762 $result = '';
763 /// Validate if we can do it
764 if (!$table = $structure->getTable($table)) {
765 return false;
766 }
767 if (!$key = $table->getKey($key)) {
768 return false;
769 }
770 if ($table->getAllErrors()) {
771 return false;
772 }
773
774 /// Add the standard PHP header
775 $result .= XMLDB_PHP_HEADER;
776
777 /// Add contents
778 $result .= XMLDB_LINEFEED;
779 $result .= ' /// Define key ' . $key->getName() . ' ('. $key->getXMLDBKeyName($key->getType()) . ') to be added to ' . $table->getName() . XMLDB_LINEFEED;
a8cb94f6 780 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
69b80cc2 781 $result .= ' $key = new xmldb_key(' . "'" . $key->getName() . "', " . $key->getPHP(true) . ');' . XMLDB_LINEFEED;
ceac2552 782
783 /// Launch the proper DDL
784 $result .= XMLDB_LINEFEED;
785 $result .= ' /// Launch add key ' . $key->getName() . XMLDB_LINEFEED;
ef088e70 786 $result .= ' $dbman->add_key($table, $key);' . XMLDB_LINEFEED;
ceac2552 787
dcbef199 788 /// Add the proper upgrade_xxxx_savepoint call
789 $result .= $this->upgrade_savepoint_php ($structure);
790
ceac2552 791 /// Add standard PHP footer
792 $result .= XMLDB_PHP_FOOTER;
793
794 return $result;
795 }
796
797 /**
798 * This function will generate all the PHP code needed to
799 * drop one key using XMLDB objects and functions
800 *
46293bd7 801 * @param xmldb_structure structure object containing all the info
ceac2552 802 * @param string table table name
803 * @param string key key name to be dropped
804 * @return string PHP code to be used to drop the key
805 */
806 function drop_key_php($structure, $table, $key) {
807
808 $result = '';
809 /// Validate if we can do it
810 if (!$table = $structure->getTable($table)) {
811 return false;
812 }
813 if (!$key = $table->getKey($key)) {
814 return false;
815 }
816 if ($table->getAllErrors()) {
817 return false;
818 }
819
820 /// Add the standard PHP header
821 $result .= XMLDB_PHP_HEADER;
822
823 /// Add contents
824 $result .= XMLDB_LINEFEED;
825 $result .= ' /// Define key ' . $key->getName() . ' ('. $key->getXMLDBKeyName($key->getType()) . ') to be dropped form ' . $table->getName() . XMLDB_LINEFEED;
a8cb94f6 826 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
69b80cc2 827 $result .= ' $key = new xmldb_key(' . "'" . $key->getName() . "', " . $key->getPHP(true) . ');' . XMLDB_LINEFEED;
ceac2552 828
829 /// Launch the proper DDL
830 $result .= XMLDB_LINEFEED;
831 $result .= ' /// Launch drop key ' . $key->getName() . XMLDB_LINEFEED;
ef088e70 832 $result .= ' $dbman->drop_key($table, $key);' . XMLDB_LINEFEED;
ceac2552 833
dcbef199 834 /// Add the proper upgrade_xxxx_savepoint call
835 $result .= $this->upgrade_savepoint_php ($structure);
836
ceac2552 837 /// Add standard PHP footer
838 $result .= XMLDB_PHP_FOOTER;
839
840 return $result;
841 }
842
843 /**
844 * This function will generate all the PHP code needed to
845 * rename one key using XMLDB objects and functions
846 *
46293bd7 847 * @param xmldb_structure structure object containing all the info
ceac2552 848 * @param string table table name
849 * @param string key key name to be renamed
850 * @return string PHP code to be used to rename the key
851 */
852 function rename_key_php($structure, $table, $key) {
853
854 $result = '';
855 /// Validate if we can do it
856 if (!$table = $structure->getTable($table)) {
857 return false;
858 }
859 if (!$key = $table->getKey($key)) {
860 return false;
861 }
862 if ($table->getAllErrors()) {
863 return false;
864 }
865
49173d25 866 /// Prepend warning. This function isn't usable!
867 $result .= 'DON\'T USE THIS FUNCTION (IT\'S ONLY EXPERIMENTAL). SOME DBs DON\'T SUPPORT IT!' . XMLDB_LINEFEED . XMLDB_LINEFEED;
868
ceac2552 869 /// Add the standard PHP header
870 $result .= XMLDB_PHP_HEADER;
871
872 /// Add contents
873 $result .= XMLDB_LINEFEED;
874 $result .= ' /// Define key ' . $key->getName() . ' ('. $key->getXMLDBKeyName($key->getType()) . ') to be renamed to NEWNAMEGOESHERE' . XMLDB_LINEFEED;
a8cb94f6 875 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
69b80cc2 876 $result .= ' $key = new xmldb_key(' . "'" . $key->getName() . "', " . $key->getPHP(true) . ');' . XMLDB_LINEFEED;
ceac2552 877
878 /// Launch the proper DDL
879 $result .= XMLDB_LINEFEED;
880 $result .= ' /// Launch rename key ' . $key->getName() . XMLDB_LINEFEED;
ef088e70 881 $result .= ' $dbman->rename_key($table, $key, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED;
ceac2552 882
dcbef199 883 /// Add the proper upgrade_xxxx_savepoint call
884 $result .= $this->upgrade_savepoint_php ($structure);
885
ceac2552 886 /// Add standard PHP footer
887 $result .= XMLDB_PHP_FOOTER;
888
889 return $result;
890 }
891
892 /**
893 * This function will generate all the PHP code needed to
894 * create one index using XMLDB objects and functions
895 *
46293bd7 896 * @param xmldb_structure structure object containing all the info
ceac2552 897 * @param string table table name
898 * @param string index index name to be created
899 * @return string PHP code to be used to create the index
900 */
901 function add_index_php($structure, $table, $index) {
902
903 $result = '';
904 /// Validate if we can do it
905 if (!$table = $structure->getTable($table)) {
906 return false;
907 }
908 if (!$index = $table->getIndex($index)) {
909 return false;
910 }
911 if ($table->getAllErrors()) {
912 return false;
913 }
914
915 /// Add the standard PHP header
916 $result .= XMLDB_PHP_HEADER;
917
918 /// Add contents
919 $result .= XMLDB_LINEFEED;
920 $result .= ' /// Define index ' . $index->getName() . ' ('. ($index->getUnique() ? 'unique' : 'not unique') . ') to be added to ' . $table->getName() . XMLDB_LINEFEED;
a8cb94f6 921 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
69b80cc2 922 $result .= ' $index = new xmldb_index(' . "'" . $index->getName() . "', " . $index->getPHP(true) . ');' . XMLDB_LINEFEED;
ceac2552 923
924 /// Launch the proper DDL
925 $result .= XMLDB_LINEFEED;
82bba3e4 926 $result .= ' /// Conditionally launch add index ' . $index->getName() . XMLDB_LINEFEED;
9dbebd14 927 $result .= ' if (!$dbman->index_exists($table, $index)) {' . XMLDB_LINEFEED;
ef088e70 928 $result .= ' $dbman->add_index($table, $index);' . XMLDB_LINEFEED;
82bba3e4 929 $result .= ' }' . XMLDB_LINEFEED;
ceac2552 930
dcbef199 931 /// Add the proper upgrade_xxxx_savepoint call
932 $result .= $this->upgrade_savepoint_php ($structure);
933
ceac2552 934 /// Add standard PHP footer
935 $result .= XMLDB_PHP_FOOTER;
936
937 return $result;
938 }
939
940 /**
941 * This function will generate all the PHP code needed to
942 * drop one index using XMLDB objects and functions
943 *
46293bd7 944 * @param xmldb_structure structure object containing all the info
ceac2552 945 * @param string table table name
946 * @param string index index name to be dropped
947 * @return string PHP code to be used to drop the index
948 */
949 function drop_index_php($structure, $table, $index) {
950
951 $result = '';
952 /// Validate if we can do it
953 if (!$table = $structure->getTable($table)) {
954 return false;
955 }
956 if (!$index = $table->getIndex($index)) {
957 return false;
958 }
959 if ($table->getAllErrors()) {
960 return false;
961 }
962
963 /// Add the standard PHP header
964 $result .= XMLDB_PHP_HEADER;
965
966 /// Add contents
967 $result .= XMLDB_LINEFEED;
968 $result .= ' /// Define index ' . $index->getName() . ' ('. ($index->getUnique() ? 'unique' : 'not unique') . ') to be dropped form ' . $table->getName() . XMLDB_LINEFEED;
a8cb94f6 969 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
69b80cc2 970 $result .= ' $index = new xmldb_index(' . "'" . $index->getName() . "', " . $index->getPHP(true) . ');' . XMLDB_LINEFEED;
ceac2552 971
972 /// Launch the proper DDL
973 $result .= XMLDB_LINEFEED;
82bba3e4 974 $result .= ' /// Conditionally launch drop index ' . $index->getName() . XMLDB_LINEFEED;
9dbebd14 975 $result .= ' if ($dbman->index_exists($table, $index)) {' . XMLDB_LINEFEED;
ef088e70 976 $result .= ' $dbman->drop_index($table, $index);' . XMLDB_LINEFEED;
82bba3e4 977 $result .= ' }' . XMLDB_LINEFEED;
ceac2552 978
dcbef199 979 /// Add the proper upgrade_xxxx_savepoint call
980 $result .= $this->upgrade_savepoint_php ($structure);
981
ceac2552 982 /// Add standard PHP footer
983 $result .= XMLDB_PHP_FOOTER;
984
985 return $result;
986 }
987
988 /**
989 * This function will generate all the PHP code needed to
990 * rename one index using XMLDB objects and functions
991 *
46293bd7 992 * @param xmldb_structure structure object containing all the info
ceac2552 993 * @param string table table name
994 * @param string index index name to be renamed
995 * @return string PHP code to be used to rename the index
996 */
997 function rename_index_php($structure, $table, $index) {
998
999 $result = '';
1000 /// Validate if we can do it
1001 if (!$table = $structure->getTable($table)) {
1002 return false;
1003 }
1004 if (!$index = $table->getIndex($index)) {
1005 return false;
1006 }
1007 if ($table->getAllErrors()) {
1008 return false;
1009 }
1010
49173d25 1011 /// Prepend warning. This function isn't usable!
1012 $result .= 'DON\'T USE THIS FUNCTION (IT\'S ONLY EXPERIMENTAL). SOME DBs DON\'T SUPPORT IT!' . XMLDB_LINEFEED . XMLDB_LINEFEED;
1013
ceac2552 1014 /// Add the standard PHP header
1015 $result .= XMLDB_PHP_HEADER;
1016
1017 /// Add contents
1018 $result .= XMLDB_LINEFEED;
1019 $result .= ' /// Define index ' . $index->getName() . ' ('. ($index->getUnique() ? 'unique' : 'not unique') . ') to be renamed to NEWNAMEGOESHERE' . XMLDB_LINEFEED;
a8cb94f6 1020 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
69b80cc2 1021 $result .= ' $index = new xmldb_index(' . "'" . $index->getName() . "', " . $index->getPHP(true) . ');' . XMLDB_LINEFEED;
ceac2552 1022
1023 /// Launch the proper DDL
1024 $result .= XMLDB_LINEFEED;
1025 $result .= ' /// Launch rename index ' . $index->getName() . XMLDB_LINEFEED;
ef088e70 1026 $result .= ' $dbman->rename_index($table, $index, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED;
ceac2552 1027
dcbef199 1028 /// Add the proper upgrade_xxxx_savepoint call
1029 $result .= $this->upgrade_savepoint_php ($structure);
1030
ceac2552 1031 /// Add standard PHP footer
1032 $result .= XMLDB_PHP_FOOTER;
1033
1034 return $result;
1035 }
1036
c5a6ae17 1037}
0df0df23 1038