[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/tool/xmldb/actions/check_indexes/ -> check_indexes.class.php (source)

   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 compare all the indexes found in the XMLDB definitions
  25   * with the physical DB implementation, reporting about all the missing
  26   * indexes to be created to be 100% ok.
  27   *
  28   * @package    tool_xmldb
  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   */
  32  class check_indexes extends XMLDBCheckAction {
  33  
  34      /**
  35       * Init method, every subclass will have its own
  36       */
  37      function init() {
  38          $this->introstr = 'confirmcheckindexes';
  39          parent::init();
  40  
  41          // Set own core attributes
  42  
  43          // Set own custom attributes
  44  
  45          // Get needed strings
  46          $this->loadStrings(array(
  47              'missing' => 'tool_xmldb',
  48              'key' => 'tool_xmldb',
  49              'index' => 'tool_xmldb',
  50              'missingindexes' => 'tool_xmldb',
  51              'nomissingindexesfound' => 'tool_xmldb',
  52              'yesmissingindexesfound' => 'tool_xmldb',
  53          ));
  54      }
  55  
  56      protected function check_table(xmldb_table $xmldb_table, array $metacolumns) {
  57          global $DB;
  58          $dbman = $DB->get_manager();
  59  
  60          $o = '';
  61          $missing_indexes = array();
  62  
  63          // Keys
  64          if ($xmldb_keys = $xmldb_table->getKeys()) {
  65              $o.='        <ul>';
  66              foreach ($xmldb_keys as $xmldb_key) {
  67                  $o.='            <li>' . $this->str['key'] . ': ' . $xmldb_key->readableInfo() . ' ';
  68                  // Primaries are skipped
  69                  if ($xmldb_key->getType() == XMLDB_KEY_PRIMARY) {
  70                      $o.='<font color="green">' . $this->str['ok'] . '</font></li>';
  71                      continue;
  72                  }
  73                  // If we aren't creating the keys or the key is a XMLDB_KEY_FOREIGN (not underlying index generated
  74                  // automatically by the RDBMS) create the underlying (created by us) index (if doesn't exists)
  75                  if (!$dbman->generator->getKeySQL($xmldb_table, $xmldb_key) || $xmldb_key->getType() == XMLDB_KEY_FOREIGN) {
  76                      // Create the interim index
  77                      $xmldb_index = new xmldb_index('anyname');
  78                      $xmldb_index->setFields($xmldb_key->getFields());
  79                      switch ($xmldb_key->getType()) {
  80                          case XMLDB_KEY_UNIQUE:
  81                          case XMLDB_KEY_FOREIGN_UNIQUE:
  82                              $xmldb_index->setUnique(true);
  83                              break;
  84                          case XMLDB_KEY_FOREIGN:
  85                              $xmldb_index->setUnique(false);
  86                              break;
  87                      }
  88                      // Check if the index exists in DB
  89                      if ($dbman->index_exists($xmldb_table, $xmldb_index)) {
  90                          $o.='<font color="green">' . $this->str['ok'] . '</font>';
  91                      } else {
  92                          $o.='<font color="red">' . $this->str['missing'] . '</font>';
  93                          // Add the missing index to the list
  94                          $obj = new stdClass();
  95                          $obj->table = $xmldb_table;
  96                          $obj->index = $xmldb_index;
  97                          $missing_indexes[] = $obj;
  98                      }
  99                  }
 100                  $o.='</li>';
 101              }
 102              $o.='        </ul>';
 103          }
 104          // Indexes
 105          if ($xmldb_indexes = $xmldb_table->getIndexes()) {
 106              $o.='        <ul>';
 107              foreach ($xmldb_indexes as $xmldb_index) {
 108                  $o.='            <li>' . $this->str['index'] . ': ' . $xmldb_index->readableInfo() . ' ';
 109                  // Check if the index exists in DB
 110                  if ($dbman->index_exists($xmldb_table, $xmldb_index)) {
 111                      $o.='<font color="green">' . $this->str['ok'] . '</font>';
 112                  } else {
 113                      $o.='<font color="red">' . $this->str['missing'] . '</font>';
 114                      // Add the missing index to the list
 115                      $obj = new stdClass();
 116                      $obj->table = $xmldb_table;
 117                      $obj->index = $xmldb_index;
 118                      $missing_indexes[] = $obj;
 119                  }
 120                  $o.='</li>';
 121              }
 122              $o.='        </ul>';
 123          }
 124  
 125          return array($o, $missing_indexes);
 126      }
 127  
 128      protected function display_results(array $missing_indexes) {
 129          global $DB;
 130          $dbman = $DB->get_manager();
 131  
 132          $s = '';
 133          $r = '<table class="generaltable boxaligncenter boxwidthwide" border="0" cellpadding="5" cellspacing="0" id="results">';
 134          $r.= '  <tr><td class="generalboxcontent">';
 135          $r.= '    <h2 class="main">' . $this->str['searchresults'] . '</h2>';
 136          $r.= '    <p class="centerpara">' . $this->str['missingindexes'] . ': ' . count($missing_indexes) . '</p>';
 137          $r.= '  </td></tr>';
 138          $r.= '  <tr><td class="generalboxcontent">';
 139  
 140          // If we have found missing indexes inform about them
 141          if (count($missing_indexes)) {
 142              $r.= '    <p class="centerpara">' . $this->str['yesmissingindexesfound'] . '</p>';
 143              $r.= '        <ul>';
 144              foreach ($missing_indexes as $obj) {
 145                  $xmldb_table = $obj->table;
 146                  $xmldb_index = $obj->index;
 147                  $sqlarr = $dbman->generator->getAddIndexSQL($xmldb_table, $xmldb_index);
 148                  $r.= '            <li>' . $this->str['table'] . ': ' . $xmldb_table->getName() . '. ' .
 149                                            $this->str['index'] . ': ' . $xmldb_index->readableInfo() . '</li>';
 150                  $sqlarr = $dbman->generator->getEndedStatements($sqlarr);
 151                  $s.= '<code>' . str_replace("\n", '<br />', implode('<br />', $sqlarr)) . '</code><br />';
 152  
 153              }
 154              $r.= '        </ul>';
 155              // Add the SQL statements (all together)
 156              $r.= '<hr />' . $s;
 157          } else {
 158              $r.= '    <p class="centerpara">' . $this->str['nomissingindexesfound'] . '</p>';
 159          }
 160          $r.= '  </td></tr>';
 161          $r.= '  <tr><td class="generalboxcontent">';
 162          // Add the complete log message
 163          $r.= '    <p class="centerpara">' . $this->str['completelogbelow'] . '</p>';
 164          $r.= '  </td></tr>';
 165          $r.= '</table>';
 166  
 167          return $r;
 168      }
 169  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1