Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * @package    tool_xmldb
  19   * @copyright  2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  20   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21   */
  22  
  23  /**
  24   * This class will show all the reserved words in a format suitable to
  25   * be pasted to: http://docs.moodle.org/en/XMLDB_reserved_words and
  26   * http://docs.moodle.org/en/Database_reserved_words
  27   * Also, it introspects te DB looking for such words and informing about
  28   *
  29   * @package    tool_xmldb
  30   * @copyright  2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  31   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class view_reserved_words extends XMLDBAction {
  34  
  35      /**
  36       * Init method, every subclass will have its own
  37       */
  38      function init() {
  39          parent::init();
  40  
  41          // Set own custom attributes
  42      $this->sesskey_protected = false; // This action doesn't need sesskey protection
  43  
  44          // Get needed strings
  45          $this->loadStrings(array(
  46              'listreservedwords' => 'tool_xmldb',
  47              'wrongreservedwords' => 'tool_xmldb',
  48              'table' => 'tool_xmldb',
  49              'field' => 'tool_xmldb',
  50              'back' => 'tool_xmldb'
  51          ));
  52      }
  53  
  54      /**
  55       * Invoke method, every class will have its own
  56       * returns true/false on completion, setting both
  57       * errormsg and output as necessary
  58       */
  59      function invoke() {
  60          parent::invoke();
  61  
  62          $result = true;
  63  
  64          // Set own core attributes
  65          $this->does_generate = ACTION_GENERATE_HTML;
  66  
  67          // These are always here
  68          global $CFG, $XMLDB, $DB;
  69  
  70          // Calculate list of available SQL generators
  71          require_once("$CFG->libdir/ddl/sql_generator.php");
  72          $reserved_words = sql_generator::getAllReservedWords();
  73  
  74          // Now, calculate, looking into current DB (with AdoDB Metadata), which fields are
  75          // in the list of reserved words
  76          $wronguses = array();
  77          $dbtables = $DB->get_tables();
  78          if ($dbtables) {
  79              foreach ($dbtables as $table) {
  80                  if (array_key_exists($table, $reserved_words)) {
  81                      $wronguses[] = $this->str['table'] . ' - ' . $table . ' (' . implode(', ',$reserved_words[$table]) . ')';
  82  
  83                  }
  84                  $dbfields = $DB->get_columns($table);
  85                  if ($dbfields) {
  86                      foreach ($dbfields as $dbfield) {
  87                          if (array_key_exists($dbfield->name, $reserved_words)) {
  88                              $wronguses[] = $this->str['field'] . ' - ' . $table . '->' . $dbfield->name . ' (' . implode(', ',$reserved_words[$dbfield->name]) . ')';
  89                          }
  90                      }
  91                  }
  92              }
  93          }
  94  
  95          // Sort the wrong uses
  96          sort($wronguses);
  97  
  98          // The back to edit table button
  99          $b = ' <p class="centerpara buttons">';
 100          $b .= '<a href="index.php">[' . $this->str['back'] . ']</a>';
 101          $b .= '</p>';
 102          $o = $b;
 103  
 104          // The list of currently wrong field names
 105          if ($wronguses) {
 106              $o.= '    <table id="formelements" class="boxaligncenter" cellpadding="5">';
 107              $o.= '      <tr><td align="center"><font color="red">' . $this->str['wrongreservedwords'] . '</font></td></tr>';
 108              $o.= '      <tr><td>';
 109              $o.= '        <ul><li>' . implode('</li><li>', $wronguses) . '</li></ul>';
 110              $o.= '      </td></tr>';
 111              $o.= '    </table>';
 112          }
 113  
 114          // The textarea showing all the reserved words
 115          $o.= '    <table id="formelements" class="boxaligncenter" cellpadding="5">';
 116          $o.= '      <tr><td align="center">' . $this->str['listreservedwords'].'</td></tr>';
 117          $o .= '      <tr><td><textarea cols="80" rows="32" class="form-control">';
 118          $o.= s(implode(', ', array_keys($reserved_words)));
 119          $o.= '</textarea></td></tr>';
 120          $o.= '    </table>';
 121  
 122          $this->output = $o;
 123  
 124          // Launch postaction if exists (leave this here!)
 125          if ($this->getPostAction() && $result) {
 126              return $this->launch($this->getPostAction());
 127          }
 128  
 129          // Return ok if arrived here
 130          return $result;
 131      }
 132  }
 133