|
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_bigints extends XMLDBCheckAction { 00035 private $correct_type; 00036 private $dbfamily; 00037 00041 function init() { 00042 global $DB; 00043 00044 $this->introstr = 'confirmcheckbigints'; 00045 parent::init(); 00046 00047 // Set own core attributes 00048 00049 // Set own custom attributes 00050 00051 // Get needed strings 00052 $this->loadStrings(array( 00053 'wrongints' => 'tool_xmldb', 00054 'nowrongintsfound' => 'tool_xmldb', 00055 'yeswrongintsfound' => 'tool_xmldb', 00056 'mysqlextracheckbigints' => 'tool_xmldb', 00057 )); 00058 00059 // Correct fields must be type bigint for MySQL and int8 for PostgreSQL 00060 $this->dbfamily = $DB->get_dbfamily(); 00061 switch ($this->dbfamily) { 00062 case 'mysql': 00063 $this->correct_type = 'bigint'; 00064 break; 00065 case 'postgres': 00066 $this->correct_type = 'int8'; 00067 break; 00068 default: 00069 $this->correct_type = NULL; 00070 } 00071 } 00072 00073 protected function check_table(xmldb_table $xmldb_table, array $metacolumns) { 00074 $o = ''; 00075 $wrong_fields = array(); 00076 00077 // Get and process XMLDB fields 00078 if ($xmldb_fields = $xmldb_table->getFields()) { 00079 $o.=' <ul>'; 00080 foreach ($xmldb_fields as $xmldb_field) { 00081 // If the field isn't integer(10), skip 00082 if ($xmldb_field->getType() != XMLDB_TYPE_INTEGER || $xmldb_field->getLength() != 10) { 00083 continue; 00084 } 00085 // If the metadata for that column doesn't exist, skip 00086 if (!isset($metacolumns[$xmldb_field->getName()])) { 00087 continue; 00088 } 00089 // To variable for better handling 00090 $metacolumn = $metacolumns[$xmldb_field->getName()]; 00091 // Going to check this field in DB 00092 $o.=' <li>' . $this->str['field'] . ': ' . $xmldb_field->getName() . ' '; 00093 // Detect if the physical field is wrong and, under mysql, check for incorrect signed fields too 00094 if ($metacolumn->type != $this->correct_type || ($this->dbfamily == 'mysql' && $xmldb_field->getUnsigned() && !$metacolumn->unsigned)) { 00095 $o.='<font color="red">' . $this->str['wrong'] . '</font>'; 00096 // Add the wrong field to the list 00097 $obj = new stdClass(); 00098 $obj->table = $xmldb_table; 00099 $obj->field = $xmldb_field; 00100 $wrong_fields[] = $obj; 00101 } else { 00102 $o.='<font color="green">' . $this->str['ok'] . '</font>'; 00103 } 00104 $o.='</li>'; 00105 } 00106 $o.=' </ul>'; 00107 } 00108 00109 return array($o, $wrong_fields); 00110 } 00111 00112 protected function display_results(array $wrong_fields) { 00113 global $DB; 00114 $dbman = $DB->get_manager(); 00115 00116 $s = ''; 00117 $r = '<table class="generalbox boxaligncenter boxwidthwide" border="0" cellpadding="5" cellspacing="0" id="results">'; 00118 $r.= ' <tr><td class="generalboxcontent">'; 00119 $r.= ' <h2 class="main">' . $this->str['searchresults'] . '</h2>'; 00120 $r.= ' <p class="centerpara">' . $this->str['wrongints'] . ': ' . count($wrong_fields) . '</p>'; 00121 $r.= ' </td></tr>'; 00122 $r.= ' <tr><td class="generalboxcontent">'; 00123 00124 // If we have found wrong integers inform about them 00125 if (count($wrong_fields)) { 00126 $r.= ' <p class="centerpara">' . $this->str['yeswrongintsfound'] . '</p>'; 00127 $r.= ' <ul>'; 00128 foreach ($wrong_fields as $obj) { 00129 $xmldb_table = $obj->table; 00130 $xmldb_field = $obj->field; 00131 // MySQL directly supports this 00132 00133 // TODO: move this hack to generators!! 00134 00135 if ($this->dbfamily == 'mysql') { 00136 $sqlarr = $dbman->generator->getAlterFieldSQL($xmldb_table, $xmldb_field); 00137 // PostgreSQL (XMLDB implementation) is a bit, er... imperfect. 00138 } else if ($this->dbfamily == 'postgres') { 00139 $sqlarr = array('ALTER TABLE ' . $DB->get_prefix() . $xmldb_table->getName() . 00140 ' ALTER COLUMN ' . $xmldb_field->getName() . ' TYPE BIGINT;'); 00141 } 00142 $r.= ' <li>' . $this->str['table'] . ': ' . $xmldb_table->getName() . '. ' . 00143 $this->str['field'] . ': ' . $xmldb_field->getName() . '</li>'; 00144 // Add to output if we have sentences 00145 if ($sqlarr) { 00146 $sqlarr = $dbman->generator->getEndedStatements($sqlarr); 00147 $s.= '<code>' . str_replace("\n", '<br />', implode('<br />', $sqlarr)). '</code><br />'; 00148 } 00149 } 00150 $r.= ' </ul>'; 00151 // Add the SQL statements (all together) 00152 $r.= '<hr />' . $s; 00153 } else { 00154 $r.= ' <p class="centerpara">' . $this->str['nowrongintsfound'] . '</p>'; 00155 } 00156 $r.= ' </td></tr>'; 00157 $r.= ' <tr><td class="generalboxcontent">'; 00158 // Add the complete log message 00159 $r.= ' <p class="centerpara">' . $this->str['completelogbelow'] . '</p>'; 00160 $r.= ' </td></tr>'; 00161 $r.= '</table>'; 00162 00163 return $r; 00164 } 00165 }