Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   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   tool_xmldb
  20   * @copyright 2011 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 check all the varchar2() columns
  26   * in the Moodle installed DB, looking for incorrect (INT)
  27   * length semanticas providing one SQL script to fix all
  28   * them by changing to cross-db (CHAR) length semantics.
  29   * See MDL-29322 for more details.
  30   *
  31   * @package    tool_xmldb
  32   * @copyright 2011 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  33   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class check_oracle_semantics extends XMLDBCheckAction {
  36  
  37      /**
  38       * Init method, every subclass will have its own
  39       */
  40      function init() {
  41          $this->introstr = 'confirmcheckoraclesemantics';
  42          parent::init();
  43  
  44          // Set own core attributes
  45  
  46          // Set own custom attributes
  47  
  48          // Get needed strings
  49          $this->loadStrings(array(
  50              'wrongoraclesemantics' => 'tool_xmldb',
  51              'nowrongoraclesemanticsfound' => 'tool_xmldb',
  52              'yeswrongoraclesemanticsfound' => 'tool_xmldb',
  53              'expected' => 'tool_xmldb',
  54              'actual' => 'tool_xmldb',
  55          ));
  56      }
  57  
  58      protected function check_table(xmldb_table $xmldb_table, array $metacolumns) {
  59          global $DB;
  60          $o = '';
  61          $wrong_fields = array();
  62  
  63          // Get and process XMLDB fields
  64          if ($xmldb_fields = $xmldb_table->getFields()) {
  65              $o .= '<ul>';
  66              foreach ($xmldb_fields as $xmldb_field) {
  67  
  68                  // Get the type of the column, we only will process CHAR (VARCHAR2) ones
  69                  if ($xmldb_field->getType() != XMLDB_TYPE_CHAR) {
  70                      continue;
  71                  }
  72  
  73                  $o.='<li>' . $this->str['field'] . ': ' . $xmldb_field->getName() . ' ';
  74  
  75                  // Get current semantic from dictionary, we only will process B (BYTE) ones
  76                  // suplying the SQL code to change them to C (CHAR) semantic
  77                  $params = array(
  78                      'table_name' => core_text::strtoupper($DB->get_prefix() . $xmldb_table->getName()),
  79                      'column_name' => core_text::strtoupper($xmldb_field->getName()),
  80                      'data_type' => 'VARCHAR2');
  81                  $currentsemantic = $DB->get_field_sql('
  82                      SELECT char_used
  83                        FROM user_tab_columns
  84                       WHERE table_name = :table_name
  85                         AND column_name = :column_name
  86                         AND data_type = :data_type', $params);
  87  
  88                  // If using byte semantics, we'll need to change them to char semantics
  89                  if ($currentsemantic == 'B') {
  90                      $info = '(' . $this->str['expected'] . " 'CHAR', " . $this->str['actual'] . " 'BYTE')";
  91                      $o .= '<font color="red">' . $this->str['wrong'] . " $info</font>";
  92                      // Add the wrong field to the list
  93                      $obj = new stdClass();
  94                      $obj->table = $xmldb_table;
  95                      $obj->field = $xmldb_field;
  96                      $wrong_fields[] = $obj;
  97                  } else {
  98                      $o .= '<font color="green">' . $this->str['ok'] . '</font>';
  99                  }
 100                  $o .= '</li>';
 101              }
 102              $o .= '</ul>';
 103          }
 104  
 105          return array($o, $wrong_fields);
 106      }
 107  
 108      protected function display_results(array $wrong_fields) {
 109          global $DB;
 110          $dbman = $DB->get_manager();
 111  
 112          $s = '';
 113          $r = '<table class="generaltable boxaligncenter boxwidthwide" border="0" cellpadding="5" cellspacing="0" id="results">';
 114          $r.= '  <tr><td class="generalboxcontent">';
 115          $r.= '    <h2 class="main">' . $this->str['searchresults'] . '</h2>';
 116          $r.= '    <p class="centerpara">' . $this->str['wrongoraclesemantics'] . ': ' . count($wrong_fields) . '</p>';
 117          $r.= '  </td></tr>';
 118          $r.= '  <tr><td class="generalboxcontent">';
 119  
 120          // If we have found wrong defaults inform about them
 121          if (count($wrong_fields)) {
 122              $r.= '    <p class="centerpara">' . $this->str['yeswrongoraclesemanticsfound'] . '</p>';
 123              $r.= '        <ul>';
 124              foreach ($wrong_fields as $obj) {
 125                  $xmldb_table = $obj->table;
 126                  $xmldb_field = $obj->field;
 127  
 128                  $r.= '            <li>' . $this->str['table'] . ': ' . $xmldb_table->getName() . '. ' .
 129                                            $this->str['field'] . ': ' . $xmldb_field->getName() . ', ' .
 130                                            $this->str['expected'] . ' ' . "'CHAR'" . ' ' .
 131                                            $this->str['actual'] . ' ' . "'BYTE'" . '</li>';
 132  
 133                  $sql = 'ALTER TABLE ' . $DB->get_prefix() . $xmldb_table->getName() . ' MODIFY ' .
 134                         $xmldb_field->getName() . ' VARCHAR2(' . $xmldb_field->getLength() . ' CHAR)';
 135                  $sql = $dbman->generator->getEndedStatements($sql);
 136                  $s.= '<code>' . str_replace("\n", '<br />', $sql) . '</code><br />';
 137              }
 138              $r.= '        </ul>';
 139              // Add the SQL statements (all together)
 140              $r.= '<hr />' . $s;
 141          } else {
 142              $r.= '    <p class="centerpara">' . $this->str['nowrongoraclesemanticsfound'] . '</p>';
 143          }
 144          $r.= '  </td></tr>';
 145          $r.= '  <tr><td class="generalboxcontent">';
 146          // Add the complete log message
 147          $r.= '    <p class="centerpara">' . $this->str['completelogbelow'] . '</p>';
 148          $r.= '  </td></tr>';
 149          $r.= '</table>';
 150  
 151          return $r;
 152      }
 153  }