[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

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

   1  <?php // $Id: check_indexes.class.php,v 1.6 2007/10/10 05:25:22 nicolasconnault Exp $
   2  
   3  ///////////////////////////////////////////////////////////////////////////
   4  //                                                                       //
   5  // NOTICE OF COPYRIGHT                                                   //
   6  //                                                                       //
   7  // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
   8  //          http://moodle.com                                            //
   9  //                                                                       //
  10  // Copyright (C) 1999 onwards Martin Dougiamas        http://dougiamas.com  //
  11  //           (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com  //
  12  //                                                                       //
  13  // This program is free software; you can redistribute it and/or modify  //
  14  // it under the terms of the GNU General Public License as published by  //
  15  // the Free Software Foundation; either version 2 of the License, or     //
  16  // (at your option) any later version.                                   //
  17  //                                                                       //
  18  // This program is distributed in the hope that it will be useful,       //
  19  // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
  20  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
  21  // GNU General Public License for more details:                          //
  22  //                                                                       //
  23  //          http://www.gnu.org/copyleft/gpl.html                         //
  24  //                                                                       //
  25  ///////////////////////////////////////////////////////////////////////////
  26  
  27  /// This class will compare all the indexes found in the XMLDB definitions
  28  /// with the phisical DB implementation, reporting about all the missing
  29  /// indexes to be created to be 100% ok.
  30  
  31  class check_indexes extends XMLDBAction {
  32  
  33      /**
  34       * Init method, every subclass will have its own
  35       */
  36      function init() {
  37          parent::init();
  38  
  39      /// Set own core attributes
  40  
  41      /// Set own custom attributes
  42  
  43      /// Get needed strings
  44          $this->loadStrings(array(
  45              'confirmcheckindexes' => 'xmldb',
  46              'ok' => '',
  47              'missing' => 'xmldb',
  48              'table' => 'xmldb',
  49              'key' => 'xmldb',
  50              'index' => 'xmldb',
  51              'searchresults' => 'xmldb',
  52              'missingindexes' => 'xmldb',
  53              'completelogbelow' => 'xmldb',
  54              'nomissingindexesfound' => 'xmldb',
  55              'yesmissingindexesfound' => 'xmldb',
  56              'yes' => '',
  57              'no' => '',
  58              'back' => 'xmldb'
  59  
  60          ));
  61      }
  62  
  63      /**
  64       * Invoke method, every class will have its own
  65       * returns true/false on completion, setting both
  66       * errormsg and output as necessary
  67       */
  68      function invoke() {
  69          parent::invoke();
  70  
  71          $result = true;
  72  
  73      /// Set own core attributes
  74          $this->does_generate = ACTION_GENERATE_HTML;
  75  
  76      /// These are always here
  77          global $CFG, $XMLDB;
  78  
  79      /// And we nedd some ddl suff
  80          require_once ($CFG->libdir . '/ddllib.php');
  81  
  82      /// Here we'll acummulate all the missing indexes found
  83          $missing_indexes = array();
  84  
  85      /// Do the job, setting $result as needed
  86  
  87      /// Get the confirmed to decide what to do
  88          $confirmed = optional_param('confirmed', false, PARAM_BOOL);
  89  
  90      /// If  not confirmed, show confirmation box
  91          if (!$confirmed) {
  92              $o = '<table class="generalbox" border="0" cellpadding="5" cellspacing="0" id="notice">';
  93              $o.= '  <tr><td class="generalboxcontent">';
  94              $o.= '    <p class="centerpara">' . $this->str['confirmcheckindexes'] . '</p>';
  95              $o.= '    <table class="boxaligncenter" cellpadding="20"><tr><td>';
  96              $o.= '      <div class="singlebutton">';
  97              $o.= '        <form action="index.php?action=check_indexes&amp;confirmed=yes" method="post"><fieldset class="invisiblefieldset">';
  98              $o.= '          <input type="submit" value="'. $this->str['yes'] .'" /></fieldset></form></div>';
  99              $o.= '      </td><td>';
 100              $o.= '      <div class="singlebutton">';
 101              $o.= '        <form action="index.php?action=main_view" method="post"><fieldset class="invisiblefieldset">';
 102              $o.= '          <input type="submit" value="'. $this->str['no'] .'" /></fieldset></form></div>';
 103              $o.= '      </td></tr>';
 104              $o.= '    </table>';
 105              $o.= '  </td></tr>';
 106              $o.= '</table>';
 107  
 108              $this->output = $o;
 109          } else {
 110          /// The back to edit table button
 111              $b = ' <p class="centerpara buttons">';
 112              $b .= '<a href="index.php">[' . $this->str['back'] . ']</a>';
 113              $b .= '</p>';
 114  
 115          /// Iterate over $XMLDB->dbdirs, loading their XML data to memory
 116              if ($XMLDB->dbdirs) {
 117                  $dbdirs =& $XMLDB->dbdirs;
 118                  $o='<ul>';
 119                  foreach ($dbdirs as $dbdir) {
 120                  /// Only if the directory exists
 121                      if (!$dbdir->path_exists) {
 122                          continue;
 123                      }
 124                  /// Load the XML file
 125                      $xmldb_file = new XMLDBFile($dbdir->path . '/install.xml');
 126                  /// Load the needed XMLDB generator
 127                      $classname = 'XMLDB' . $CFG->dbtype;
 128                      $generator = new $classname();
 129                      $generator->setPrefix($CFG->prefix);
 130  
 131                  /// Only if the file exists
 132                      if (!$xmldb_file->fileExists()) {
 133                          continue;
 134                      }
 135                  /// Load the XML contents to structure
 136                      $loaded = $xmldb_file->loadXMLStructure();
 137                      if (!$loaded || !$xmldb_file->isLoaded()) {
 138                          notify('Errors found in XMLDB file: '. $dbdir->path . '/install.xml');
 139                          continue;
 140                      }
 141                  /// Arriving here, everything is ok, get the XMLDB structure
 142                      $structure = $xmldb_file->getStructure();
 143                      $o.='    <li>' . str_replace($CFG->dirroot . '/', '', $dbdir->path . '/install.xml');
 144                  /// Getting tables
 145                      if ($xmldb_tables = $structure->getTables()) {
 146                          $o.='        <ul>';
 147                      /// Foreach table, process its indexes and keys
 148                          foreach ($xmldb_tables as $xmldb_table) {
 149                          /// Skip table if not exists
 150                              if (!table_exists($xmldb_table)) {
 151                                  continue;
 152                              }
 153                              $o.='            <li>' . $xmldb_table->getName();
 154                          /// Keys
 155                              if ($xmldb_keys = $xmldb_table->getKeys()) {
 156                                  $o.='        <ul>';
 157                                  foreach ($xmldb_keys as $xmldb_key) {
 158                                      $o.='            <li>' . $this->str['key'] . ': ' . $xmldb_key->readableInfo() . ' ';
 159                                  /// Primaries are skipped
 160                                      if ($xmldb_key->getType() == XMLDB_KEY_PRIMARY) {
 161                                          $o.='<font color="green">' . $this->str['ok'] . '</font></li>';
 162                                          continue;
 163                                      }
 164                                  /// If we aren't creating the keys or the key is a XMLDB_KEY_FOREIGN (not underlying index generated
 165                                  /// automatically by the RDBMS) create the underlying (created by us) index (if doesn't exists)
 166                                      if (!$generator->getKeySQL($xmldb_table, $xmldb_key) || $xmldb_key->getType() == XMLDB_KEY_FOREIGN) {
 167                                      /// Create the interim index
 168                                          $xmldb_index = new XMLDBIndex('anyname');
 169                                          $xmldb_index->setFields($xmldb_key->getFields());
 170                                          switch ($xmldb_key->getType()) {
 171                                              case XMLDB_KEY_UNIQUE:
 172                                              case XMLDB_KEY_FOREIGN_UNIQUE:
 173                                                  $xmldb_index->setUnique(true);
 174                                                  break;
 175                                              case XMLDB_KEY_FOREIGN:
 176                                                  $xmldb_index->setUnique(false);
 177                                                  break;
 178                                          }
 179                                      /// Check if the index exists in DB
 180                                          if (index_exists($xmldb_table, $xmldb_index)) {
 181                                              $o.='<font color="green">' . $this->str['ok'] . '</font>';
 182                                          } else {
 183                                              $o.='<font color="red">' . $this->str['missing'] . '</font>';
 184                                          /// Add the missing index to the list
 185                                              $obj = new object;
 186                                              $obj->table = $xmldb_table;
 187                                              $obj->index = $xmldb_index;
 188                                              $missing_indexes[] = $obj;
 189                                          }
 190                                      }
 191                                      $o.='</li>';
 192                                  }
 193                                  $o.='        </ul>';
 194                              }
 195                          /// Indexes
 196                              if ($xmldb_indexes = $xmldb_table->getIndexes()) {
 197                                  $o.='        <ul>';
 198                                  foreach ($xmldb_indexes as $xmldb_index) {
 199                                      $o.='            <li>' . $this->str['index'] . ': ' . $xmldb_index->readableInfo() . ' ';
 200                                  /// Check if the index exists in DB
 201                                      if (index_exists($xmldb_table, $xmldb_index)) {
 202                                          $o.='<font color="green">' . $this->str['ok'] . '</font>';
 203                                      } else {
 204                                          $o.='<font color="red">' . $this->str['missing'] . '</font>';
 205                                      /// Add the missing index to the list
 206                                          $obj = new object;
 207                                          $obj->table = $xmldb_table;
 208                                          $obj->index = $xmldb_index;
 209                                          $missing_indexes[] = $obj;
 210                                      }
 211                                      $o.='</li>';
 212                                  }
 213                                  $o.='        </ul>';
 214                              }
 215                              $o.='    </li>';
 216                          }
 217                          $o.='        </ul>';
 218                      }
 219                      $o.='    </li>';
 220                  }
 221                  $o.='</ul>';
 222              }
 223  
 224          /// We have finished, let's show the results of the search
 225              $s = '';
 226              $r = '<table class="generalbox boxaligncenter boxwidthwide" border="0" cellpadding="5" cellspacing="0" id="results">';
 227              $r.= '  <tr><td class="generalboxcontent">';
 228              $r.= '    <h2 class="main">' . $this->str['searchresults'] . '</h2>';
 229              $r.= '    <p class="centerpara">' . $this->str['missingindexes'] . ': ' . count($missing_indexes) . '</p>';
 230              $r.= '  </td></tr>';
 231              $r.= '  <tr><td class="generalboxcontent">';
 232  
 233          /// If we have found missing indexes inform about them
 234              if (count($missing_indexes)) {
 235                  $r.= '    <p class="centerpara">' . $this->str['yesmissingindexesfound'] . '</p>';
 236                  $r.= '        <ul>';
 237                  foreach ($missing_indexes as $obj) {
 238                      $xmldb_table = $obj->table;
 239                      $xmldb_index = $obj->index;
 240                      $sqlarr = $xmldb_table->getAddIndexSQL($CFG->dbtype, $CFG->prefix, $xmldb_index, true);
 241                      $r.= '            <li>' . $this->str['table'] . ': ' . $xmldb_table->getName() . '. ' .
 242                                                $this->str['index'] . ': ' . $xmldb_index->readableInfo() . '</li>';
 243                      $s.= '<code>' . str_replace("\n", '<br />', implode('<br />', $sqlarr)) . '</code><br />';
 244                      
 245                  }
 246                  $r.= '        </ul>';
 247              /// Add the SQL statements (all together)
 248                  $r.= '<hr />' . $s;
 249              } else {
 250                  $r.= '    <p class="centerpara">' . $this->str['nomissingindexesfound'] . '</p>';
 251              }
 252              $r.= '  </td></tr>';
 253              $r.= '  <tr><td class="generalboxcontent">';
 254          /// Add the complete log message
 255              $r.= '    <p class="centerpara">' . $this->str['completelogbelow'] . '</p>';
 256              $r.= '  </td></tr>';
 257              $r.= '</table>';
 258  
 259              $this->output = $b . $r . $o;
 260          }
 261  
 262      /// Launch postaction if exists (leave this here!)
 263          if ($this->getPostAction() && $result) {
 264              return $this->launch($this->getPostAction());
 265          }
 266  
 267      /// Return ok if arrived here
 268          return $result;
 269      }
 270  }
 271  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7