PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/tool/xmldb/actions/view_table_php/view_table_php.class.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 921 lines | 528 code | 135 blank | 258 comment | 103 complexity | 53d6aff1ee911856ea4ab380e712f0bf MD5 | raw file
  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. * @package tool_xmldb
  18. * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  19. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  20. */
  21. /**
  22. * This class will show the PHP needed (upgrade block) to perform
  23. * the desired DDL action with the specified field/key/index
  24. *
  25. * @package tool_xmldb
  26. * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  27. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28. */
  29. class view_table_php extends XMLDBAction {
  30. /**
  31. * Init method, every subclass will have its own
  32. */
  33. function init() {
  34. parent::init();
  35. // Set own custom attributes
  36. $this->sesskey_protected = false; // This action doesn't need sesskey protection
  37. // Get needed strings
  38. $this->loadStrings(array(
  39. 'selectaction' => 'tool_xmldb',
  40. 'selectfieldkeyindex' => 'tool_xmldb',
  41. 'view' => 'tool_xmldb',
  42. 'table' => 'tool_xmldb',
  43. 'selectonecommand' => 'tool_xmldb',
  44. 'selectonefieldkeyindex' => 'tool_xmldb',
  45. 'mustselectonefield' => 'tool_xmldb',
  46. 'mustselectonekey' => 'tool_xmldb',
  47. 'mustselectoneindex' => 'tool_xmldb',
  48. 'back' => 'tool_xmldb'
  49. ));
  50. }
  51. /**
  52. * Invoke method, every class will have its own
  53. * returns true/false on completion, setting both
  54. * errormsg and output as necessary
  55. */
  56. function invoke() {
  57. parent::invoke();
  58. $result = true;
  59. // Set own core attributes
  60. $this->does_generate = ACTION_GENERATE_HTML;
  61. // These are always here
  62. global $CFG, $XMLDB, $OUTPUT;
  63. // Do the job, setting result as needed
  64. // Get the dir containing the file
  65. $dirpath = required_param('dir', PARAM_PATH);
  66. $dirpath = $CFG->dirroot . $dirpath;
  67. // Get the correct dirs
  68. if (!empty($XMLDB->dbdirs)) {
  69. $dbdir = $XMLDB->dbdirs[$dirpath];
  70. } else {
  71. return false;
  72. }
  73. if (!empty($XMLDB->editeddirs)) {
  74. $editeddir = $XMLDB->editeddirs[$dirpath];
  75. $structure = $editeddir->xml_file->getStructure();
  76. }
  77. $tableparam = required_param('table', PARAM_PATH);
  78. $table = $structure->getTable($tableparam);
  79. $fields = $table->getFields();
  80. $field = reset($fields);
  81. $defaultfieldkeyindex = null;
  82. if ($field) {
  83. $defaultfieldkeyindex = 'f#' . $field->getName();
  84. }
  85. $keys = $table->getKeys();
  86. $indexes = $table->getIndexes();
  87. // Get parameters
  88. $commandparam = optional_param('command', 'add_field', PARAM_PATH);
  89. $origfieldkeyindexparam = optional_param('fieldkeyindex', $defaultfieldkeyindex, PARAM_PATH);
  90. $fieldkeyindexparam = preg_replace('/[fki]#/i', '', $origfieldkeyindexparam); // Strip the initials
  91. $fieldkeyindexinitial = substr($origfieldkeyindexparam, 0, 1); //To know what we have selected
  92. // The back to edit xml button
  93. $b = ' <p class="centerpara buttons">';
  94. $b .= '<a href="index.php?action=edit_table&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&amp;table=' . $tableparam . '">[' . $this->str['back'] . ']</a>';
  95. $b .= '</p>';
  96. $o = $b;
  97. // The table currently being edited
  98. $o .= '<h3 class="main">' . $this->str['table'] . ': ' . s($tableparam) . '</h3>';
  99. // To indent the menu selections
  100. $optionspacer = '&nbsp;&nbsp;&nbsp;';
  101. // Calculate the popup of commands
  102. $commands = array('Fields',
  103. $optionspacer . 'add_field',
  104. $optionspacer . 'drop_field',
  105. $optionspacer . 'rename_field',
  106. $optionspacer . 'change_field_type',
  107. $optionspacer . 'change_field_precision',
  108. $optionspacer . 'change_field_notnull',
  109. $optionspacer . 'change_field_default',
  110. 'Keys',
  111. $optionspacer . 'add_key',
  112. $optionspacer . 'drop_key',
  113. $optionspacer . 'rename_key',
  114. 'Indexes',
  115. $optionspacer . 'add_index',
  116. $optionspacer . 'drop_index',
  117. $optionspacer . 'rename_index');
  118. foreach ($commands as $command) {
  119. $popcommands[str_replace($optionspacer, '', $command)] = str_replace('_', ' ', $command);
  120. }
  121. // Calculate the popup of fields/keys/indexes
  122. if ($fields) {
  123. $popfields['fieldshead'] = 'Fields';
  124. foreach ($fields as $field) {
  125. $popfields['f#' . $field->getName()] = $optionspacer . $field->getName();
  126. }
  127. }
  128. if ($keys) {
  129. $popfields['keyshead'] = 'Keys';
  130. foreach ($keys as $key) {
  131. $popfields['k#' . $key->getName()] = $optionspacer . $key->getName();
  132. }
  133. }
  134. if ($indexes) {
  135. $popfields['indexeshead'] = 'Indexes';
  136. foreach ($indexes as $index) {
  137. $popfields['i#' . $index->getName()] = $optionspacer . $index->getName();
  138. }
  139. }
  140. // Now build the form
  141. $o.= '<form id="form" action="index.php" method="post">';
  142. $o.= '<div>';
  143. $o.= ' <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot, '', $dirpath) . '" />';
  144. $o.= ' <input type="hidden" name ="table" value="' . s($tableparam) . '" />';
  145. $o.= ' <input type="hidden" name ="action" value="view_table_php" />';
  146. $o.= ' <table id="formelements" class="boxaligncenter" cellpadding="5">';
  147. $o.= ' <tr><td><label for="menucommand" accesskey="c">' . $this->str['selectaction'] .' </label>' . html_writer::select($popcommands, 'command', $commandparam, false) . '&nbsp;<label for="menufieldkeyindex" accesskey="f">' . $this->str['selectfieldkeyindex'] . ' </label>' .html_writer::select($popfields, 'fieldkeyindex', $origfieldkeyindexparam, false) . '</td></tr>';
  148. $o.= ' <tr><td colspan="2" align="center"><input type="submit" value="' .$this->str['view'] . '" /></td></tr>';
  149. $o.= ' </table>';
  150. $o.= '</div></form>';
  151. $o.= ' <table id="phpcode" class="boxaligncenter" cellpadding="5">';
  152. $o.= ' <tr><td><textarea cols="80" rows="32">';
  153. // Check we have selected some field/key/index from the popup
  154. if ($fieldkeyindexparam == 'fieldshead' || $fieldkeyindexparam == 'keyshead' || $fieldkeyindexparam == 'indexeshead') {
  155. $o.= s($this->str['selectonefieldkeyindex']);
  156. // Check we have selected some command from the popup
  157. } else if ($commandparam == 'Fields' || $commandparam == 'Keys' || $commandparam == 'Indexes') {
  158. $o.= s($this->str['selectonecommand']);
  159. } else {
  160. // Based on current params, call the needed function
  161. switch ($commandparam) {
  162. case 'add_field':
  163. if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
  164. $o.= s($this->add_field_php($structure, $tableparam, $fieldkeyindexparam));
  165. } else {
  166. $o.= $this->str['mustselectonefield'];
  167. }
  168. break;
  169. case 'drop_field':
  170. if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
  171. $o.= s($this->drop_field_php($structure, $tableparam, $fieldkeyindexparam));
  172. } else {
  173. $o.= $this->str['mustselectonefield'];
  174. }
  175. break;
  176. case 'rename_field':
  177. if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
  178. $o.= s($this->rename_field_php($structure, $tableparam, $fieldkeyindexparam));
  179. } else {
  180. $o.= $this->str['mustselectonefield'];
  181. }
  182. break;
  183. case 'change_field_type':
  184. if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
  185. $o.= s($this->change_field_type_php($structure, $tableparam, $fieldkeyindexparam));
  186. } else {
  187. $o.= $this->str['mustselectonefield'];
  188. }
  189. break;
  190. case 'change_field_precision':
  191. if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
  192. $o.= s($this->change_field_precision_php($structure, $tableparam, $fieldkeyindexparam));
  193. } else {
  194. $o.= $this->str['mustselectonefield'];
  195. }
  196. break;
  197. case 'change_field_notnull':
  198. if ($fieldkeyindexinitial == 'f') { // Only if we have got one field
  199. $o.= s($this->change_field_notnull_php($structure, $tableparam, $fieldkeyindexparam));
  200. } else {
  201. $o.= $this->str['mustselectonefield'];
  202. }
  203. break;
  204. case 'change_field_default':
  205. if ($fieldkeyindexinitial == 'f') { // Only if we have got one field
  206. $o.= s($this->change_field_default_php($structure, $tableparam, $fieldkeyindexparam));
  207. } else {
  208. $o.= $this->str['mustselectonefield'];
  209. }
  210. break;
  211. case 'add_key':
  212. if ($fieldkeyindexinitial == 'k') { // Only if we have got one key
  213. $o.= s($this->add_key_php($structure, $tableparam, $fieldkeyindexparam));
  214. } else {
  215. $o.= $this->str['mustselectonekey'];
  216. }
  217. break;
  218. case 'drop_key':
  219. if ($fieldkeyindexinitial == 'k') { // Only if we have got one key
  220. $o.= s($this->drop_key_php($structure, $tableparam, $fieldkeyindexparam));
  221. } else {
  222. $o.= $this->str['mustselectonekey'];
  223. }
  224. break;
  225. case 'rename_key':
  226. if ($fieldkeyindexinitial == 'k') { // Only if we have got one key
  227. $o.= s($this->rename_key_php($structure, $tableparam, $fieldkeyindexparam));
  228. } else {
  229. $o.= $this->str['mustselectonekey'];
  230. }
  231. break;
  232. case 'add_index':
  233. if ($fieldkeyindexinitial == 'i') { // Only if we have got one index
  234. $o.= s($this->add_index_php($structure, $tableparam, $fieldkeyindexparam));
  235. } else {
  236. $o.= $this->str['mustselectoneindex'];
  237. }
  238. break;
  239. case 'drop_index':
  240. if ($fieldkeyindexinitial == 'i') { // Only if we have got one index
  241. $o.= s($this->drop_index_php($structure, $tableparam, $fieldkeyindexparam));
  242. } else {
  243. $o.= $this->str['mustselectoneindex'];
  244. }
  245. break;
  246. case 'rename_index':
  247. if ($fieldkeyindexinitial == 'i') { // Only if we have got one index
  248. $o.= s($this->rename_index_php($structure, $tableparam, $fieldkeyindexparam));
  249. } else {
  250. $o.= $this->str['mustselectoneindex'];
  251. }
  252. break;
  253. }
  254. }
  255. $o.= '</textarea></td></tr>';
  256. $o.= ' </table>';
  257. $this->output = $o;
  258. // Launch postaction if exists (leave this here!)
  259. if ($this->getPostAction() && $result) {
  260. return $this->launch($this->getPostAction());
  261. }
  262. // Return ok if arrived here
  263. return $result;
  264. }
  265. /**
  266. * This function will generate all the PHP code needed to
  267. * create one field using XMLDB objects and functions
  268. *
  269. * @param xmldb_structure structure object containing all the info
  270. * @param string table table name
  271. * @param string field field name to be created
  272. * @return string PHP code to be used to create the field
  273. */
  274. function add_field_php($structure, $table, $field) {
  275. $result = '';
  276. // Validate if we can do it
  277. if (!$table = $structure->getTable($table)) {
  278. return false;
  279. }
  280. if (!$field = $table->getField($field)) {
  281. return false;
  282. }
  283. if ($table->getAllErrors()) {
  284. return false;
  285. }
  286. // Add the standard PHP header
  287. $result .= XMLDB_PHP_HEADER;
  288. // Add contents
  289. $result .= XMLDB_LINEFEED;
  290. $result .= ' // Define field ' . $field->getName() . ' to be added to ' . $table->getName() . '.' . XMLDB_LINEFEED;
  291. $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
  292. $result .= ' $field = new xmldb_field(' . "'" . $field->getName() . "', " . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
  293. // Launch the proper DDL
  294. $result .= XMLDB_LINEFEED;
  295. $result .= ' // Conditionally launch add field ' . $field->getName() . '.' . XMLDB_LINEFEED;
  296. $result .= ' if (!$dbman->field_exists($table, $field)) {'. XMLDB_LINEFEED;
  297. $result .= ' $dbman->add_field($table, $field);' . XMLDB_LINEFEED;
  298. $result .= ' }'. XMLDB_LINEFEED;
  299. // Add the proper upgrade_xxxx_savepoint call
  300. $result .= $this->upgrade_savepoint_php ($structure);
  301. // Add standard PHP footer
  302. $result .= XMLDB_PHP_FOOTER;
  303. return $result;
  304. }
  305. /**
  306. * This function will generate all the PHP code needed to
  307. * drop one field using XMLDB objects and functions
  308. *
  309. * @param xmldb_structure structure object containing all the info
  310. * @param string table table name
  311. * @param string field field name to be dropped
  312. * @return string PHP code to be used to drop the field
  313. */
  314. function drop_field_php($structure, $table, $field) {
  315. $result = '';
  316. // Validate if we can do it
  317. if (!$table = $structure->getTable($table)) {
  318. return false;
  319. }
  320. if (!$field = $table->getField($field)) {
  321. return false;
  322. }
  323. if ($table->getAllErrors()) {
  324. return false;
  325. }
  326. // Add the standard PHP header
  327. $result .= XMLDB_PHP_HEADER;
  328. // Add contents
  329. $result .= XMLDB_LINEFEED;
  330. $result .= ' // Define field ' . $field->getName() . ' to be dropped from ' . $table->getName() . '.' . XMLDB_LINEFEED;
  331. $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
  332. $result .= ' $field = new xmldb_field(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
  333. // Launch the proper DDL
  334. $result .= XMLDB_LINEFEED;
  335. $result .= ' // Conditionally launch drop field ' . $field->getName() . '.' . XMLDB_LINEFEED;
  336. $result .= ' if ($dbman->field_exists($table, $field)) {' . XMLDB_LINEFEED;
  337. $result .= ' $dbman->drop_field($table, $field);' . XMLDB_LINEFEED;
  338. $result .= ' }' . XMLDB_LINEFEED;
  339. // Add the proper upgrade_xxxx_savepoint call
  340. $result .= $this->upgrade_savepoint_php ($structure);
  341. // Add standard PHP footer
  342. $result .= XMLDB_PHP_FOOTER;
  343. return $result;
  344. }
  345. /**
  346. * This function will generate all the PHP code needed to
  347. * rename one field using XMLDB objects and functions
  348. *
  349. * @param xmldb_structure structure object containing all the info
  350. * @param string table table name
  351. * @param string field field name to be renamed
  352. * @return string PHP code to be used to rename the field
  353. */
  354. function rename_field_php($structure, $table, $field) {
  355. $result = '';
  356. // Validate if we can do it
  357. if (!$table = $structure->getTable($table)) {
  358. return false;
  359. }
  360. if (!$field = $table->getField($field)) {
  361. return false;
  362. }
  363. if ($table->getAllErrors()) {
  364. return false;
  365. }
  366. // Add the standard PHP header
  367. $result .= XMLDB_PHP_HEADER;
  368. // Add contents
  369. $result .= XMLDB_LINEFEED;
  370. $result .= ' // Rename field ' . $field->getName() . ' on table ' . $table->getName() . ' to NEWNAMEGOESHERE.'. XMLDB_LINEFEED;
  371. $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
  372. $result .= ' $field = new xmldb_field(' . "'" . $field->getName() . "', " . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
  373. // Launch the proper DDL
  374. $result .= XMLDB_LINEFEED;
  375. $result .= ' // Launch rename field ' . $field->getName() . '.' . XMLDB_LINEFEED;
  376. $result .= ' $dbman->rename_field($table, $field, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED;
  377. // Add the proper upgrade_xxxx_savepoint call
  378. $result .= $this->upgrade_savepoint_php ($structure);
  379. // Add standard PHP footer
  380. $result .= XMLDB_PHP_FOOTER;
  381. return $result;
  382. }
  383. /**
  384. * This function will generate all the PHP code needed to
  385. * change the type of one field using XMLDB objects and functions.
  386. * Currently these conversions are supported:
  387. * integer to char
  388. * char to integer
  389. * number to char
  390. * char to number
  391. * float to char
  392. * char to float
  393. *
  394. * @param xmldb_structure structure object containing all the info
  395. * @param string table table name
  396. * @param string field field name to change precision
  397. */
  398. function change_field_type_php($structure, $table, $field) {
  399. $result = '';
  400. // Validate if we can do it
  401. if (!$table = $structure->getTable($table)) {
  402. return false;
  403. }
  404. if (!$field = $table->getField($field)) {
  405. return false;
  406. }
  407. if ($table->getAllErrors()) {
  408. return false;
  409. }
  410. // Calculate the type tip text
  411. $type = $field->getXMLDBTypeName($field->getType());
  412. // Add the standard PHP header
  413. $result .= XMLDB_PHP_HEADER;
  414. // Add contents
  415. $result .= XMLDB_LINEFEED;
  416. $result .= ' // Changing type of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $type . '.' . XMLDB_LINEFEED;
  417. $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
  418. $result .= ' $field = new xmldb_field(' . "'" . $field->getName() . "', " . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
  419. // Launch the proper DDL
  420. $result .= XMLDB_LINEFEED;
  421. $result .= ' // Launch change of type for field ' . $field->getName() . '.' . XMLDB_LINEFEED;
  422. $result .= ' $dbman->change_field_type($table, $field);' . XMLDB_LINEFEED;
  423. // Add the proper upgrade_xxxx_savepoint call
  424. $result .= $this->upgrade_savepoint_php ($structure);
  425. // Add standard PHP footer
  426. $result .= XMLDB_PHP_FOOTER;
  427. return $result;
  428. }
  429. /**
  430. * This function will generate all the PHP code needed to
  431. * change the precision of one field using XMLDB objects and functions
  432. *
  433. * @param xmldb_structure structure object containing all the info
  434. * @param string table table name
  435. * @param string field field name to change precision
  436. */
  437. function change_field_precision_php($structure, $table, $field) {
  438. $result = '';
  439. // Validate if we can do it
  440. if (!$table = $structure->getTable($table)) {
  441. return false;
  442. }
  443. if (!$field = $table->getField($field)) {
  444. return false;
  445. }
  446. if ($table->getAllErrors()) {
  447. return false;
  448. }
  449. // Calculate the precision tip text
  450. $precision = '(' . $field->getLength();
  451. if ($field->getDecimals()) {
  452. $precision .= ', ' . $field->getDecimals();
  453. }
  454. $precision .= ')';
  455. // Add the standard PHP header
  456. $result .= XMLDB_PHP_HEADER;
  457. // Add contents
  458. $result .= XMLDB_LINEFEED;
  459. $result .= ' // Changing precision of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $precision . '.' . XMLDB_LINEFEED;
  460. $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
  461. $result .= ' $field = new xmldb_field(' . "'" . $field->getName() . "', " . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
  462. // Launch the proper DDL
  463. $result .= XMLDB_LINEFEED;
  464. $result .= ' // Launch change of precision for field ' . $field->getName() . '.' . XMLDB_LINEFEED;
  465. $result .= ' $dbman->change_field_precision($table, $field);' . XMLDB_LINEFEED;
  466. // Add the proper upgrade_xxxx_savepoint call
  467. $result .= $this->upgrade_savepoint_php ($structure);
  468. // Add standard PHP footer
  469. $result .= XMLDB_PHP_FOOTER;
  470. return $result;
  471. }
  472. /**
  473. * This function will generate all the PHP code needed to
  474. * change the nullability of one field using XMLDB objects and functions
  475. *
  476. * @param xmldb_structure structure object containing all the info
  477. * @param string table table name
  478. * @param string field field name to change null/not null
  479. */
  480. function change_field_notnull_php($structure, $table, $field) {
  481. $result = '';
  482. // Validate if we can do it
  483. if (!$table = $structure->getTable($table)) {
  484. return false;
  485. }
  486. if (!$field = $table->getField($field)) {
  487. return false;
  488. }
  489. if ($table->getAllErrors()) {
  490. return false;
  491. }
  492. // Calculate the notnull tip text
  493. $notnull = $field->getNotnull() ? 'not null' : 'null';
  494. // Add the standard PHP header
  495. $result .= XMLDB_PHP_HEADER;
  496. // Add contents
  497. $result .= XMLDB_LINEFEED;
  498. $result .= ' // Changing nullability of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $notnull . '.' . XMLDB_LINEFEED;
  499. $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
  500. $result .= ' $field = new xmldb_field(' . "'" . $field->getName() . "', " . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
  501. // Launch the proper DDL
  502. $result .= XMLDB_LINEFEED;
  503. $result .= ' // Launch change of nullability for field ' . $field->getName() . '.' . XMLDB_LINEFEED;
  504. $result .= ' $dbman->change_field_notnull($table, $field);' . XMLDB_LINEFEED;
  505. // Add the proper upgrade_xxxx_savepoint call
  506. $result .= $this->upgrade_savepoint_php ($structure);
  507. // Add standard PHP footer
  508. $result .= XMLDB_PHP_FOOTER;
  509. return $result;
  510. }
  511. /**
  512. * This function will generate all the PHP code needed to
  513. * change the default of one field using XMLDB objects and functions
  514. *
  515. * @param xmldb_structure structure object containing all the info
  516. * @param string table table name
  517. * @param string field field name to change null/not null
  518. */
  519. function change_field_default_php($structure, $table, $field) {
  520. $result = '';
  521. // Validate if we can do it
  522. if (!$table = $structure->getTable($table)) {
  523. return false;
  524. }
  525. if (!$field = $table->getField($field)) {
  526. return false;
  527. }
  528. if ($table->getAllErrors()) {
  529. return false;
  530. }
  531. // Calculate the default tip text
  532. $default = $field->getDefault() === null ? 'drop it' : $field->getDefault();
  533. // Add the standard PHP header
  534. $result .= XMLDB_PHP_HEADER;
  535. // Add contents
  536. $result .= XMLDB_LINEFEED;
  537. $result .= ' // Changing the default of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $default . '.' . XMLDB_LINEFEED;
  538. $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
  539. $result .= ' $field = new xmldb_field(' . "'" . $field->getName() . "', " . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
  540. // Launch the proper DDL
  541. $result .= XMLDB_LINEFEED;
  542. $result .= ' // Launch change of default for field ' . $field->getName() . '.' . XMLDB_LINEFEED;
  543. $result .= ' $dbman->change_field_default($table, $field);' . XMLDB_LINEFEED;
  544. // Add the proper upgrade_xxxx_savepoint call
  545. $result .= $this->upgrade_savepoint_php ($structure);
  546. // Add standard PHP footer
  547. $result .= XMLDB_PHP_FOOTER;
  548. return $result;
  549. }
  550. /**
  551. * This function will generate all the PHP code needed to
  552. * create one key using XMLDB objects and functions
  553. *
  554. * @param xmldb_structure structure object containing all the info
  555. * @param string table table name
  556. * @param string key key name to be created
  557. * @return string PHP code to be used to create the key
  558. */
  559. function add_key_php($structure, $table, $key) {
  560. $result = '';
  561. // Validate if we can do it
  562. if (!$table = $structure->getTable($table)) {
  563. return false;
  564. }
  565. if (!$key = $table->getKey($key)) {
  566. return false;
  567. }
  568. if ($table->getAllErrors()) {
  569. return false;
  570. }
  571. // Add the standard PHP header
  572. $result .= XMLDB_PHP_HEADER;
  573. // Add contents
  574. $result .= XMLDB_LINEFEED;
  575. $result .= ' // Define key ' . $key->getName() . ' ('. $key->getXMLDBKeyName($key->getType()) . ') to be added to ' . $table->getName() . '.' . XMLDB_LINEFEED;
  576. $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
  577. $result .= ' $key = new xmldb_key(' . "'" . $key->getName() . "', " . $key->getPHP(true) . ');' . XMLDB_LINEFEED;
  578. // Launch the proper DDL
  579. $result .= XMLDB_LINEFEED;
  580. $result .= ' // Launch add key ' . $key->getName() . '.' . XMLDB_LINEFEED;
  581. $result .= ' $dbman->add_key($table, $key);' . XMLDB_LINEFEED;
  582. // Add the proper upgrade_xxxx_savepoint call
  583. $result .= $this->upgrade_savepoint_php ($structure);
  584. // Add standard PHP footer
  585. $result .= XMLDB_PHP_FOOTER;
  586. return $result;
  587. }
  588. /**
  589. * This function will generate all the PHP code needed to
  590. * drop one key using XMLDB objects and functions
  591. *
  592. * @param xmldb_structure structure object containing all the info
  593. * @param string table table name
  594. * @param string key key name to be dropped
  595. * @return string PHP code to be used to drop the key
  596. */
  597. function drop_key_php($structure, $table, $key) {
  598. $result = '';
  599. // Validate if we can do it
  600. if (!$table = $structure->getTable($table)) {
  601. return false;
  602. }
  603. if (!$key = $table->getKey($key)) {
  604. return false;
  605. }
  606. if ($table->getAllErrors()) {
  607. return false;
  608. }
  609. // Add the standard PHP header
  610. $result .= XMLDB_PHP_HEADER;
  611. // Add contents
  612. $result .= XMLDB_LINEFEED;
  613. $result .= ' // Define key ' . $key->getName() . ' ('. $key->getXMLDBKeyName($key->getType()) . ') to be dropped form ' . $table->getName() . '.' . XMLDB_LINEFEED;
  614. $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
  615. $result .= ' $key = new xmldb_key(' . "'" . $key->getName() . "', " . $key->getPHP(true) . ');' . XMLDB_LINEFEED;
  616. // Launch the proper DDL
  617. $result .= XMLDB_LINEFEED;
  618. $result .= ' // Launch drop key ' . $key->getName() . '.' . XMLDB_LINEFEED;
  619. $result .= ' $dbman->drop_key($table, $key);' . XMLDB_LINEFEED;
  620. // Add the proper upgrade_xxxx_savepoint call
  621. $result .= $this->upgrade_savepoint_php ($structure);
  622. // Add standard PHP footer
  623. $result .= XMLDB_PHP_FOOTER;
  624. return $result;
  625. }
  626. /**
  627. * This function will generate all the PHP code needed to
  628. * rename one key using XMLDB objects and functions
  629. *
  630. * @param xmldb_structure structure object containing all the info
  631. * @param string table table name
  632. * @param string key key name to be renamed
  633. * @return string PHP code to be used to rename the key
  634. */
  635. function rename_key_php($structure, $table, $key) {
  636. $result = '';
  637. // Validate if we can do it
  638. if (!$table = $structure->getTable($table)) {
  639. return false;
  640. }
  641. if (!$key = $table->getKey($key)) {
  642. return false;
  643. }
  644. if ($table->getAllErrors()) {
  645. return false;
  646. }
  647. // Prepend warning. This function isn't usable!
  648. $result .= 'DON\'T USE THIS FUNCTION (IT\'S ONLY EXPERIMENTAL). SOME DBs DON\'T SUPPORT IT!' . XMLDB_LINEFEED . XMLDB_LINEFEED;
  649. // Add the standard PHP header
  650. $result .= XMLDB_PHP_HEADER;
  651. // Add contents
  652. $result .= XMLDB_LINEFEED;
  653. $result .= ' // Define key ' . $key->getName() . ' ('. $key->getXMLDBKeyName($key->getType()) . ') to be renamed to NEWNAMEGOESHERE.' . XMLDB_LINEFEED;
  654. $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
  655. $result .= ' $key = new xmldb_key(' . "'" . $key->getName() . "', " . $key->getPHP(true) . ');' . XMLDB_LINEFEED;
  656. // Launch the proper DDL
  657. $result .= XMLDB_LINEFEED;
  658. $result .= ' // Launch rename key ' . $key->getName() . '.' . XMLDB_LINEFEED;
  659. $result .= ' $dbman->rename_key($table, $key, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED;
  660. // Add the proper upgrade_xxxx_savepoint call
  661. $result .= $this->upgrade_savepoint_php ($structure);
  662. // Add standard PHP footer
  663. $result .= XMLDB_PHP_FOOTER;
  664. return $result;
  665. }
  666. /**
  667. * This function will generate all the PHP code needed to
  668. * create one index using XMLDB objects and functions
  669. *
  670. * @param xmldb_structure structure object containing all the info
  671. * @param string table table name
  672. * @param string index index name to be created
  673. * @return string PHP code to be used to create the index
  674. */
  675. function add_index_php($structure, $table, $index) {
  676. $result = '';
  677. // Validate if we can do it
  678. if (!$table = $structure->getTable($table)) {
  679. return false;
  680. }
  681. if (!$index = $table->getIndex($index)) {
  682. return false;
  683. }
  684. if ($table->getAllErrors()) {
  685. return false;
  686. }
  687. // Add the standard PHP header
  688. $result .= XMLDB_PHP_HEADER;
  689. // Add contents
  690. $result .= XMLDB_LINEFEED;
  691. $result .= ' // Define index ' . $index->getName() . ' ('. ($index->getUnique() ? 'unique' : 'not unique') . ') to be added to ' . $table->getName() . '.' . XMLDB_LINEFEED;
  692. $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
  693. $result .= ' $index = new xmldb_index(' . "'" . $index->getName() . "', " . $index->getPHP(true) . ');' . XMLDB_LINEFEED;
  694. // Launch the proper DDL
  695. $result .= XMLDB_LINEFEED;
  696. $result .= ' // Conditionally launch add index ' . $index->getName() . '.' . XMLDB_LINEFEED;
  697. $result .= ' if (!$dbman->index_exists($table, $index)) {' . XMLDB_LINEFEED;
  698. $result .= ' $dbman->add_index($table, $index);' . XMLDB_LINEFEED;
  699. $result .= ' }' . XMLDB_LINEFEED;
  700. // Add the proper upgrade_xxxx_savepoint call
  701. $result .= $this->upgrade_savepoint_php ($structure);
  702. // Add standard PHP footer
  703. $result .= XMLDB_PHP_FOOTER;
  704. return $result;
  705. }
  706. /**
  707. * This function will generate all the PHP code needed to
  708. * drop one index using XMLDB objects and functions
  709. *
  710. * @param xmldb_structure structure object containing all the info
  711. * @param string table table name
  712. * @param string index index name to be dropped
  713. * @return string PHP code to be used to drop the index
  714. */
  715. function drop_index_php($structure, $table, $index) {
  716. $result = '';
  717. // Validate if we can do it
  718. if (!$table = $structure->getTable($table)) {
  719. return false;
  720. }
  721. if (!$index = $table->getIndex($index)) {
  722. return false;
  723. }
  724. if ($table->getAllErrors()) {
  725. return false;
  726. }
  727. // Add the standard PHP header
  728. $result .= XMLDB_PHP_HEADER;
  729. // Add contents
  730. $result .= XMLDB_LINEFEED;
  731. $result .= ' // Define index ' . $index->getName() . ' ('. ($index->getUnique() ? 'unique' : 'not unique') . ') to be dropped form ' . $table->getName() . '.' . XMLDB_LINEFEED;
  732. $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
  733. $result .= ' $index = new xmldb_index(' . "'" . $index->getName() . "', " . $index->getPHP(true) . ');' . XMLDB_LINEFEED;
  734. // Launch the proper DDL
  735. $result .= XMLDB_LINEFEED;
  736. $result .= ' // Conditionally launch drop index ' . $index->getName() . '.' . XMLDB_LINEFEED;
  737. $result .= ' if ($dbman->index_exists($table, $index)) {' . XMLDB_LINEFEED;
  738. $result .= ' $dbman->drop_index($table, $index);' . XMLDB_LINEFEED;
  739. $result .= ' }' . XMLDB_LINEFEED;
  740. // Add the proper upgrade_xxxx_savepoint call
  741. $result .= $this->upgrade_savepoint_php ($structure);
  742. // Add standard PHP footer
  743. $result .= XMLDB_PHP_FOOTER;
  744. return $result;
  745. }
  746. /**
  747. * This function will generate all the PHP code needed to
  748. * rename one index using XMLDB objects and functions
  749. *
  750. * @param xmldb_structure structure object containing all the info
  751. * @param string table table name
  752. * @param string index index name to be renamed
  753. * @return string PHP code to be used to rename the index
  754. */
  755. function rename_index_php($structure, $table, $index) {
  756. $result = '';
  757. // Validate if we can do it
  758. if (!$table = $structure->getTable($table)) {
  759. return false;
  760. }
  761. if (!$index = $table->getIndex($index)) {
  762. return false;
  763. }
  764. if ($table->getAllErrors()) {
  765. return false;
  766. }
  767. // Prepend warning. This function isn't usable!
  768. $result .= 'DON\'T USE THIS FUNCTION (IT\'S ONLY EXPERIMENTAL). SOME DBs DON\'T SUPPORT IT!' . XMLDB_LINEFEED . XMLDB_LINEFEED;
  769. // Add the standard PHP header
  770. $result .= XMLDB_PHP_HEADER;
  771. // Add contents
  772. $result .= XMLDB_LINEFEED;
  773. $result .= ' // Define index ' . $index->getName() . ' ('. ($index->getUnique() ? 'unique' : 'not unique') . ') to be renamed to NEWNAMEGOESHERE.' . XMLDB_LINEFEED;
  774. $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
  775. $result .= ' $index = new xmldb_index(' . "'" . $index->getName() . "', " . $index->getPHP(true) . ');' . XMLDB_LINEFEED;
  776. // Launch the proper DDL
  777. $result .= XMLDB_LINEFEED;
  778. $result .= ' // Launch rename index ' . $index->getName() . '.' . XMLDB_LINEFEED;
  779. $result .= ' $dbman->rename_index($table, $index, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED;
  780. // Add the proper upgrade_xxxx_savepoint call
  781. $result .= $this->upgrade_savepoint_php ($structure);
  782. // Add standard PHP footer
  783. $result .= XMLDB_PHP_FOOTER;
  784. return $result;
  785. }
  786. }