|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00034 class check_indexes extends XMLDBCheckAction { 00035 00039 function init() { 00040 $this->introstr = 'confirmcheckindexes'; 00041 parent::init(); 00042 00043 // Set own core attributes 00044 00045 // Set own custom attributes 00046 00047 // Get needed strings 00048 $this->loadStrings(array( 00049 'missing' => 'tool_xmldb', 00050 'key' => 'tool_xmldb', 00051 'index' => 'tool_xmldb', 00052 'missingindexes' => 'tool_xmldb', 00053 'nomissingindexesfound' => 'tool_xmldb', 00054 'yesmissingindexesfound' => 'tool_xmldb', 00055 )); 00056 } 00057 00058 protected function check_table(xmldb_table $xmldb_table, array $metacolumns) { 00059 global $DB; 00060 $dbman = $DB->get_manager(); 00061 00062 $o = ''; 00063 $missing_indexes = array(); 00064 00065 // Keys 00066 if ($xmldb_keys = $xmldb_table->getKeys()) { 00067 $o.=' <ul>'; 00068 foreach ($xmldb_keys as $xmldb_key) { 00069 $o.=' <li>' . $this->str['key'] . ': ' . $xmldb_key->readableInfo() . ' '; 00070 // Primaries are skipped 00071 if ($xmldb_key->getType() == XMLDB_KEY_PRIMARY) { 00072 $o.='<font color="green">' . $this->str['ok'] . '</font></li>'; 00073 continue; 00074 } 00075 // If we aren't creating the keys or the key is a XMLDB_KEY_FOREIGN (not underlying index generated 00076 // automatically by the RDBMS) create the underlying (created by us) index (if doesn't exists) 00077 if (!$dbman->generator->getKeySQL($xmldb_table, $xmldb_key) || $xmldb_key->getType() == XMLDB_KEY_FOREIGN) { 00078 // Create the interim index 00079 $xmldb_index = new xmldb_index('anyname'); 00080 $xmldb_index->setFields($xmldb_key->getFields()); 00081 switch ($xmldb_key->getType()) { 00082 case XMLDB_KEY_UNIQUE: 00083 case XMLDB_KEY_FOREIGN_UNIQUE: 00084 $xmldb_index->setUnique(true); 00085 break; 00086 case XMLDB_KEY_FOREIGN: 00087 $xmldb_index->setUnique(false); 00088 break; 00089 } 00090 // Check if the index exists in DB 00091 if ($dbman->index_exists($xmldb_table, $xmldb_index)) { 00092 $o.='<font color="green">' . $this->str['ok'] . '</font>'; 00093 } else { 00094 $o.='<font color="red">' . $this->str['missing'] . '</font>'; 00095 // Add the missing index to the list 00096 $obj = new stdClass(); 00097 $obj->table = $xmldb_table; 00098 $obj->index = $xmldb_index; 00099 $missing_indexes[] = $obj; 00100 } 00101 } 00102 $o.='</li>'; 00103 } 00104 $o.=' </ul>'; 00105 } 00106 // Indexes 00107 if ($xmldb_indexes = $xmldb_table->getIndexes()) { 00108 $o.=' <ul>'; 00109 foreach ($xmldb_indexes as $xmldb_index) { 00110 $o.=' <li>' . $this->str['index'] . ': ' . $xmldb_index->readableInfo() . ' '; 00111 // Check if the index exists in DB 00112 if ($dbman->index_exists($xmldb_table, $xmldb_index)) { 00113 $o.='<font color="green">' . $this->str['ok'] . '</font>'; 00114 } else { 00115 $o.='<font color="red">' . $this->str['missing'] . '</font>'; 00116 // Add the missing index to the list 00117 $obj = new stdClass(); 00118 $obj->table = $xmldb_table; 00119 $obj->index = $xmldb_index; 00120 $missing_indexes[] = $obj; 00121 } 00122 $o.='</li>'; 00123 } 00124 $o.=' </ul>'; 00125 } 00126 00127 return array($o, $missing_indexes); 00128 } 00129 00130 protected function display_results(array $missing_indexes) { 00131 global $DB; 00132 $dbman = $DB->get_manager(); 00133 00134 $s = ''; 00135 $r = '<table class="generalbox boxaligncenter boxwidthwide" border="0" cellpadding="5" cellspacing="0" id="results">'; 00136 $r.= ' <tr><td class="generalboxcontent">'; 00137 $r.= ' <h2 class="main">' . $this->str['searchresults'] . '</h2>'; 00138 $r.= ' <p class="centerpara">' . $this->str['missingindexes'] . ': ' . count($missing_indexes) . '</p>'; 00139 $r.= ' </td></tr>'; 00140 $r.= ' <tr><td class="generalboxcontent">'; 00141 00142 // If we have found missing indexes inform about them 00143 if (count($missing_indexes)) { 00144 $r.= ' <p class="centerpara">' . $this->str['yesmissingindexesfound'] . '</p>'; 00145 $r.= ' <ul>'; 00146 foreach ($missing_indexes as $obj) { 00147 $xmldb_table = $obj->table; 00148 $xmldb_index = $obj->index; 00149 $sqlarr = $dbman->generator->getAddIndexSQL($xmldb_table, $xmldb_index); 00150 $r.= ' <li>' . $this->str['table'] . ': ' . $xmldb_table->getName() . '. ' . 00151 $this->str['index'] . ': ' . $xmldb_index->readableInfo() . '</li>'; 00152 $sqlarr = $dbman->generator->getEndedStatements($sqlarr); 00153 $s.= '<code>' . str_replace("\n", '<br />', implode('<br />', $sqlarr)) . '</code><br />'; 00154 00155 } 00156 $r.= ' </ul>'; 00157 // Add the SQL statements (all together) 00158 $r.= '<hr />' . $s; 00159 } else { 00160 $r.= ' <p class="centerpara">' . $this->str['nomissingindexesfound'] . '</p>'; 00161 } 00162 $r.= ' </td></tr>'; 00163 $r.= ' <tr><td class="generalboxcontent">'; 00164 // Add the complete log message 00165 $r.= ' <p class="centerpara">' . $this->str['completelogbelow'] . '</p>'; 00166 $r.= ' </td></tr>'; 00167 $r.= '</table>'; 00168 00169 return $r; 00170 } 00171 }