1 |
efrain |
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 |
* reporting about the ones not physically implemented as BIGINTs
|
|
|
25 |
* and providing one SQL script to fix all them. MDL-11038
|
|
|
26 |
*
|
|
|
27 |
* @package tool_xmldb
|
|
|
28 |
* @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
|
|
|
29 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
30 |
*/
|
|
|
31 |
class check_bigints extends XMLDBCheckAction {
|
|
|
32 |
/**
|
|
|
33 |
* Init method, every subclass will have its own
|
|
|
34 |
*/
|
|
|
35 |
function init() {
|
|
|
36 |
global $DB;
|
|
|
37 |
|
|
|
38 |
$this->introstr = 'confirmcheckbigints';
|
|
|
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 |
'wrongints' => 'tool_xmldb',
|
|
|
48 |
'nowrongintsfound' => 'tool_xmldb',
|
|
|
49 |
'yeswrongintsfound' => 'tool_xmldb',
|
|
|
50 |
));
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
protected function check_table(xmldb_table $xmldb_table, array $metacolumns) {
|
|
|
54 |
$o = '';
|
|
|
55 |
$wrong_fields = array();
|
|
|
56 |
|
|
|
57 |
// Get and process XMLDB fields
|
|
|
58 |
if ($xmldb_fields = $xmldb_table->getFields()) {
|
|
|
59 |
$o.=' <ul>';
|
|
|
60 |
foreach ($xmldb_fields as $xmldb_field) {
|
|
|
61 |
// If the field isn't integer(10), skip
|
|
|
62 |
if ($xmldb_field->getType() != XMLDB_TYPE_INTEGER) {
|
|
|
63 |
continue;
|
|
|
64 |
}
|
|
|
65 |
// If the metadata for that column doesn't exist, skip
|
|
|
66 |
if (!isset($metacolumns[$xmldb_field->getName()])) {
|
|
|
67 |
continue;
|
|
|
68 |
}
|
|
|
69 |
$minlength = $xmldb_field->getLength();
|
|
|
70 |
if ($minlength > 18) {
|
|
|
71 |
// Anything above 18 is borked, just ignore it here.
|
|
|
72 |
$minlength = 18;
|
|
|
73 |
}
|
|
|
74 |
// To variable for better handling
|
|
|
75 |
$metacolumn = $metacolumns[$xmldb_field->getName()];
|
|
|
76 |
// Going to check this field in DB
|
|
|
77 |
$o.=' <li>' . $this->str['field'] . ': ' . $xmldb_field->getName() . ' ';
|
|
|
78 |
// Detect if the physical field is wrong
|
|
|
79 |
if (($metacolumn->meta_type != 'I' and $metacolumn->meta_type != 'R') or $metacolumn->max_length < $minlength) {
|
|
|
80 |
$o.='<font color="red">' . $this->str['wrong'] . '</font>';
|
|
|
81 |
// Add the wrong field to the list
|
|
|
82 |
$obj = new stdClass();
|
|
|
83 |
$obj->table = $xmldb_table;
|
|
|
84 |
$obj->field = $xmldb_field;
|
|
|
85 |
$wrong_fields[] = $obj;
|
|
|
86 |
} else {
|
|
|
87 |
$o.='<font color="green">' . $this->str['ok'] . '</font>';
|
|
|
88 |
}
|
|
|
89 |
$o.='</li>';
|
|
|
90 |
}
|
|
|
91 |
$o.=' </ul>';
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
return array($o, $wrong_fields);
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
protected function display_results(array $wrong_fields) {
|
|
|
98 |
global $DB;
|
|
|
99 |
$dbman = $DB->get_manager();
|
|
|
100 |
|
|
|
101 |
$s = '';
|
|
|
102 |
$r = '<table class="generaltable boxaligncenter boxwidthwide" border="0" cellpadding="5" cellspacing="0" id="results">';
|
|
|
103 |
$r.= ' <tr><td class="generalboxcontent">';
|
|
|
104 |
$r.= ' <h2 class="main">' . $this->str['searchresults'] . '</h2>';
|
|
|
105 |
$r.= ' <p class="centerpara">' . $this->str['wrongints'] . ': ' . count($wrong_fields) . '</p>';
|
|
|
106 |
$r.= ' </td></tr>';
|
|
|
107 |
$r.= ' <tr><td class="generalboxcontent">';
|
|
|
108 |
|
|
|
109 |
// If we have found wrong integers inform about them
|
|
|
110 |
if (count($wrong_fields)) {
|
|
|
111 |
$r.= ' <p class="centerpara">' . $this->str['yeswrongintsfound'] . '</p>';
|
|
|
112 |
$r.= ' <ul>';
|
|
|
113 |
foreach ($wrong_fields as $obj) {
|
|
|
114 |
$xmldb_table = $obj->table;
|
|
|
115 |
$xmldb_field = $obj->field;
|
|
|
116 |
$sqlarr = $dbman->generator->getAlterFieldSQL($xmldb_table, $xmldb_field);
|
|
|
117 |
$r.= ' <li>' . $this->str['table'] . ': ' . $xmldb_table->getName() . '. ' .
|
|
|
118 |
$this->str['field'] . ': ' . $xmldb_field->getName() . '</li>';
|
|
|
119 |
// Add to output if we have sentences
|
|
|
120 |
if ($sqlarr) {
|
|
|
121 |
$sqlarr = $dbman->generator->getEndedStatements($sqlarr);
|
|
|
122 |
$s.= '<code>' . str_replace("\n", '<br />', implode('<br />', $sqlarr)). '</code><br />';
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
$r.= ' </ul>';
|
|
|
126 |
// Add the SQL statements (all together)
|
|
|
127 |
$r.= '<hr />' . $s;
|
|
|
128 |
} else {
|
|
|
129 |
$r.= ' <p class="centerpara">' . $this->str['nowrongintsfound'] . '</p>';
|
|
|
130 |
}
|
|
|
131 |
$r.= ' </td></tr>';
|
|
|
132 |
$r.= ' <tr><td class="generalboxcontent">';
|
|
|
133 |
// Add the complete log message
|
|
|
134 |
$r.= ' <p class="centerpara">' . $this->str['completelogbelow'] . '</p>';
|
|
|
135 |
$r.= ' </td></tr>';
|
|
|
136 |
$r.= '</table>';
|
|
|
137 |
|
|
|
138 |
return $r;
|
|
|
139 |
}
|
|
|
140 |
}
|