[ Index ]

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

title

Body

[close]

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

   1  <?php // $Id: check_bigints.class.php,v 1.3 2007/10/10 05:25:27 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 check all the int(10) fields existing in the DB
  28  /// reporting about the ones not phisically implemented as BIGINTs
  29  /// and providing one SQL script to fix all them. Also, under MySQL,
  30  /// it performs one check of signed bigints. MDL-11038
  31  
  32  class check_bigints extends XMLDBAction {
  33  
  34      /**
  35       * Init method, every subclass will have its own
  36       */
  37      function init() {
  38          parent::init();
  39  
  40      /// Set own core attributes
  41  
  42      /// Set own custom attributes
  43  
  44      /// Get needed strings
  45          $this->loadStrings(array(
  46              'confirmcheckbigints' => 'xmldb',
  47              'ok' => '',
  48              'wrong' => 'xmldb',
  49              'table' => 'xmldb',
  50              'field' => 'xmldb',
  51              'searchresults' => 'xmldb',
  52              'wrongints' => 'xmldb',
  53              'completelogbelow' => 'xmldb',
  54              'nowrongintsfound' => 'xmldb',
  55              'yeswrongintsfound' => 'xmldb',
  56              'mysqlextracheckbigints' => 'xmldb',
  57              'yes' => '',
  58              'no' => '',
  59              'error' => '',
  60              'back' => 'xmldb'
  61  
  62          ));
  63      }
  64  
  65      /**
  66       * Invoke method, every class will have its own
  67       * returns true/false on completion, setting both
  68       * errormsg and output as necessary
  69       */
  70      function invoke() {
  71          parent::invoke();
  72  
  73          $result = true;
  74  
  75      /// Set own core attributes
  76          $this->does_generate = ACTION_GENERATE_HTML;
  77  
  78      /// These are always here
  79          global $CFG, $XMLDB, $db;
  80  
  81      /// And we nedd some ddl suff
  82          require_once ($CFG->libdir . '/ddllib.php');
  83  
  84      /// Here we'll acummulate all the wrong fields found
  85          $wrong_fields = array();
  86  
  87      /// Correct fields must be type bigint for MySQL and int8 for PostgreSQL
  88          switch ($CFG->dbfamily) {
  89              case 'mysql':
  90                  $correct_type = 'bigint';
  91                  break;
  92              case 'postgres':
  93                  $correct_type = 'int8';
  94                  break;
  95              default:
  96                  $correct_type = NULL;
  97          }
  98  
  99      /// Do the job, setting $result as needed
 100  
 101      /// Get the confirmed to decide what to do
 102          $confirmed = optional_param('confirmed', false, PARAM_BOOL);
 103  
 104      /// If  not confirmed, show confirmation box
 105          if (!$confirmed) {
 106              $o = '<table class="generalbox" border="0" cellpadding="5" cellspacing="0" id="notice">';
 107              $o.= '  <tr><td class="generalboxcontent">';
 108              $o.= '    <p class="centerpara">' . $this->str['confirmcheckbigints'] . '</p>';
 109              if ($CFG->dbfamily == 'mysql') {
 110                  $o.= '    <p class="centerpara">' . $this->str['mysqlextracheckbigints'] . '</p>';
 111              }
 112              $o.= '    <table class="boxaligncenter" cellpadding="20"><tr><td>';
 113              $o.= '      <div class="singlebutton">';
 114              $o.= '        <form action="index.php?action=check_bigints&amp;confirmed=yes" method="post"><fieldset class="invisiblefieldset">';
 115              $o.= '          <input type="submit" value="'. $this->str['yes'] .'" /></fieldset></form></div>';
 116              $o.= '      </td><td>';
 117              $o.= '      <div class="singlebutton">';
 118              $o.= '        <form action="index.php?action=main_view" method="post"><fieldset class="invisiblefieldset">';
 119              $o.= '          <input type="submit" value="'. $this->str['no'] .'" /></fieldset></form></div>';
 120              $o.= '      </td></tr>';
 121              $o.= '    </table>';
 122              $o.= '  </td></tr>';
 123              $o.= '</table>';
 124  
 125              $this->output = $o;
 126          } else {
 127          /// The back to edit table button
 128              $b = ' <p class="centerpara buttons">';
 129              $b .= '<a href="index.php">[' . $this->str['back'] . ']</a>';
 130              $b .= '</p>';
 131  
 132          /// Iterate over $XMLDB->dbdirs, loading their XML data to memory
 133              if ($XMLDB->dbdirs) {
 134                  $dbdirs =& $XMLDB->dbdirs;
 135                  $o='<ul>';
 136                  foreach ($dbdirs as $dbdir) {
 137                  /// Only if the directory exists
 138                      if (!$dbdir->path_exists) {
 139                          continue;
 140                      }
 141                  /// Load the XML file
 142                      $xmldb_file = new XMLDBFile($dbdir->path . '/install.xml');
 143                  /// Load the needed XMLDB generator
 144                      $classname = 'XMLDB' . $CFG->dbtype;
 145                      $generator = new $classname();
 146                      $generator->setPrefix($CFG->prefix);
 147  
 148                  /// Only if the file exists
 149                      if (!$xmldb_file->fileExists()) {
 150                          continue;
 151                      }
 152                  /// Load the XML contents to structure
 153                      $loaded = $xmldb_file->loadXMLStructure();
 154                      if (!$loaded || !$xmldb_file->isLoaded()) {
 155                          notify('Errors found in XMLDB file: '. $dbdir->path . '/install.xml');
 156                          continue;
 157                      }
 158                  /// Arriving here, everything is ok, get the XMLDB structure
 159                      $structure = $xmldb_file->getStructure();
 160                      $o.='    <li>' . str_replace($CFG->dirroot . '/', '', $dbdir->path . '/install.xml');
 161                  /// Getting tables
 162                      if ($xmldb_tables = $structure->getTables()) {
 163                          $o.='        <ul>';
 164                      /// Foreach table, process its fields
 165                          foreach ($xmldb_tables as $xmldb_table) {
 166                          /// Skip table if not exists
 167                              if (!table_exists($xmldb_table)) {
 168                                  continue;
 169                              }
 170                          /// Fetch metadata from phisical DB. All the columns info.
 171                              if ($metacolumns = $db->MetaColumns($CFG->prefix . $xmldb_table->getName())) {
 172                                  $metacolumns = array_change_key_case($metacolumns, CASE_LOWER);
 173                              } else {
 174                              //// Skip table if no metacolumns is available for it
 175                                  continue;
 176                              }
 177                          /// Table processing starts here
 178                              $o.='            <li>' . $xmldb_table->getName();
 179                          /// Get and process XMLDB fields
 180                              if ($xmldb_fields = $xmldb_table->getFields()) {
 181                                  $o.='        <ul>';
 182                                  foreach ($xmldb_fields as $xmldb_field) {
 183                                  /// If the field isn't integer(10), skip
 184                                      if ($xmldb_field->getType() != XMLDB_TYPE_INTEGER || $xmldb_field->getLength() != 10) {
 185                                          continue;
 186                                      }
 187                                  /// If the metadata for that column doesn't exist, skip
 188                                      if (!isset($metacolumns[$xmldb_field->getName()])) {
 189                                          continue;
 190                                      }
 191                                  /// To variable for better handling
 192                                      $metacolumn = $metacolumns[$xmldb_field->getName()];
 193                                  /// Going to check this field in DB
 194                                      $o.='            <li>' . $this->str['field'] . ': ' . $xmldb_field->getName() . ' ';
 195                                  /// Detect if the phisical field is wrong and, under mysql, check for incorrect signed fields too
 196                                      if ($metacolumn->type != $correct_type || ($CFG->dbfamily == 'mysql' && $xmldb_field->getUnsigned() && !$metacolumn->unsigned)) {
 197                                          $o.='<font color="red">' . $this->str['wrong'] . '</font>';
 198                                      /// Add the wrong field to the list
 199                                          $obj = new object;
 200                                          $obj->table = $xmldb_table;
 201                                          $obj->field = $xmldb_field;
 202                                          $wrong_fields[] = $obj;
 203                                      } else {
 204                                          $o.='<font color="green">' . $this->str['ok'] . '</font>';
 205                                      }
 206                                      $o.='</li>';
 207                                  }
 208                                  $o.='        </ul>';
 209                              }
 210                              $o.='    </li>';
 211                          }
 212                          $o.='        </ul>';
 213                      }
 214                      $o.='    </li>';
 215                  }
 216                  $o.='</ul>';
 217              }
 218  
 219          /// We have finished, let's show the results of the search
 220              $s = '';
 221              $r = '<table class="generalbox boxaligncenter boxwidthwide" border="0" cellpadding="5" cellspacing="0" id="results">';
 222              $r.= '  <tr><td class="generalboxcontent">';
 223              $r.= '    <h2 class="main">' . $this->str['searchresults'] . '</h2>';
 224              $r.= '    <p class="centerpara">' . $this->str['wrongints'] . ': ' . count($wrong_fields) . '</p>';
 225              $r.= '  </td></tr>';
 226              $r.= '  <tr><td class="generalboxcontent">';
 227  
 228          /// If we have found wrong integers inform about them
 229              if (count($wrong_fields)) {
 230                  $r.= '    <p class="centerpara">' . $this->str['yeswrongintsfound'] . '</p>';
 231                  $r.= '        <ul>';
 232                  foreach ($wrong_fields as $obj) {
 233                      $xmldb_table = $obj->table;
 234                      $xmldb_field = $obj->field;
 235                  /// MySQL directly supports this
 236                      if ($CFG->dbfamily == 'mysql') {
 237                          $sqlarr = $xmldb_table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $xmldb_field, true);
 238                  /// PostgreSQL (XMLDB implementation) is a bit, er... imperfect.
 239                      } else if ($CFG->dbfamily == 'postgres') {
 240                          $sqlarr = array('ALTER TABLE ' . $CFG->prefix . $xmldb_table->getName() .
 241                                    ' ALTER COLUMN ' . $xmldb_field->getName() . ' TYPE BIGINT;');
 242                      }
 243                      $r.= '            <li>' . $this->str['table'] . ': ' . $xmldb_table->getName() . '. ' .
 244                                                $this->str['field'] . ': ' . $xmldb_field->getName() . '</li>';
 245                  /// Add to output if we have sentences
 246                      if ($sqlarr) {
 247                          $s.= '<code>' . str_replace("\n", '<br />', implode('<br />', $sqlarr)) . '</code><br />';
 248                      }
 249                  }
 250                  $r.= '        </ul>';
 251              /// Add the SQL statements (all together)
 252                  $r.= '<hr />' . $s;
 253              } else {
 254                  $r.= '    <p class="centerpara">' . $this->str['nowrongintsfound'] . '</p>';
 255              }
 256              $r.= '  </td></tr>';
 257              $r.= '  <tr><td class="generalboxcontent">';
 258          /// Add the complete log message
 259              $r.= '    <p class="centerpara">' . $this->str['completelogbelow'] . '</p>';
 260              $r.= '  </td></tr>';
 261              $r.= '</table>';
 262  
 263              $this->output = $b . $r . $o;
 264          }
 265  
 266      /// Launch postaction if exists (leave this here!)
 267          if ($this->getPostAction() && $result) {
 268              return $this->launch($this->getPostAction());
 269          }
 270  
 271      /// Return ok if arrived here
 272          return $result;
 273      }
 274  }
 275  ?>


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