MDL-63252 tool_xmldb: convert default to float to avoid false positives
[moodle.git] / admin / tool / xmldb / actions / check_defaults / check_defaults.class.php
CommitLineData
0df0df23 1<?php
f25d53a9
EL
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/>.
dfac8649 16
f25d53a9 17/**
85d6dd38 18 * @package tool_xmldb
b007a5d0
PS
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
f25d53a9 21 */
dfac8649 22
94904cc3 23defined('MOODLE_INTERNAL') || die();
24
f25d53a9
EL
25/**
26 * This class will check all the default values existing in the DB
27 * match those specified in the xml specs
28 * and providing one SQL script to fix all them.
29 *
85d6dd38 30 * @package tool_xmldb
b007a5d0
PS
31 * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
f25d53a9 33 */
9f5e5dee 34class check_defaults extends XMLDBCheckAction {
dfac8649 35
36 /**
37 * Init method, every subclass will have its own
38 */
94904cc3 39 public function init() {
9f5e5dee 40 $this->introstr = 'confirmcheckdefaults';
dfac8649 41 parent::init();
42
94904cc3 43 // Set own core attributes.
dfac8649 44
94904cc3 45 // Set own custom attributes.
dfac8649 46
94904cc3 47 // Get needed strings.
dfac8649 48 $this->loadStrings(array(
b007a5d0
PS
49 'wrongdefaults' => 'tool_xmldb',
50 'nowrongdefaultsfound' => 'tool_xmldb',
51 'yeswrongdefaultsfound' => 'tool_xmldb',
52 'expected' => 'tool_xmldb',
53 'actual' => 'tool_xmldb',
dfac8649 54 ));
55 }
56
94904cc3 57 protected function check_table(xmldb_table $xmldbtable, array $metacolumns) {
9f5e5dee 58 $o = '';
94904cc3 59 $wrongfields = array();
dfac8649 60
94904cc3 61 // Get and process XMLDB fields.
62 if ($xmldbfields = $xmldbtable->getFields()) {
63 $o .= ' <ul>';
64 foreach ($xmldbfields as $xmldbfield) {
dfac8649 65
94904cc3 66 // Get the default value for the field.
67 $xmldbdefault = $xmldbfield->getDefault();
f33e1ed4 68
12f7d77c 69 // Char fields with not null currently have default '' when actually installed.
70 if ($xmldbdefault === null && $xmldbfield->getType() === XMLDB_TYPE_CHAR &&
71 $xmldbfield->getNotNull()) {
72 $xmldbdefault = '';
73 }
74 if ($xmldbdefault !== null) {
75 $xmldbdefault = (string)$xmldbdefault;
76 }
77
94904cc3 78 // If the metadata for that column doesn't exist or 'id' field found, skip.
79 if (!isset($metacolumns[$xmldbfield->getName()]) or $xmldbfield->getName() == 'id') {
9f5e5dee 80 continue;
81 }
dfac8649 82
94904cc3 83 // To variable for better handling.
84 $metacolumn = $metacolumns[$xmldbfield->getName()];
dfac8649 85
94904cc3 86 // Going to check this field in DB.
87 $o .= ' <li>' . $this->str['field'] . ': ' . $xmldbfield->getName() . ' ';
f33e1ed4 88
94904cc3 89 // Get the value of the physical default (or blank if there isn't one).
90 if ($metacolumn->has_default == 1) {
9f5e5dee 91 $physicaldefault = $metacolumn->default_value;
94904cc3 92 } else {
12f7d77c 93 $physicaldefault = null;
9f5e5dee 94 }
f33e1ed4 95
3d03d2a0
SM
96 // For number fields there are issues with type differences, so let's convert
97 // everything to a float.
98 if ($xmldbfield->getType() === XMLDB_TYPE_NUMBER) {
99 if ($physicaldefault !== null) {
100 $physicaldefault = (float) $physicaldefault;
101 }
102 if ($xmldbdefault !== null) {
103 $xmldbdefault = (float) $xmldbdefault;
104 }
105 }
106
94904cc3 107 // There *is* a default and it's wrong.
12f7d77c 108 if ($physicaldefault !== $xmldbdefault) {
109 $xmldbtext = self::display_default($xmldbdefault);
110 $physicaltext = self::display_default($physicaldefault);
111 $info = "({$this->str['expected']} {$xmldbtext}, {$this->str['actual']} {$physicaltext})";
94904cc3 112 $o .= '<font color="red">' . $this->str['wrong'] . " $info</font>";
113 // Add the wrong field to the list.
a226a972 114 $obj = new stdClass();
94904cc3 115 $obj->table = $xmldbtable;
116 $obj->field = $xmldbfield;
9f5e5dee 117 $obj->physicaldefault = $physicaldefault;
118 $obj->xmldbdefault = $xmldbdefault;
94904cc3 119 $wrongfields[] = $obj;
9f5e5dee 120 } else {
94904cc3 121 $o .= '<font color="green">' . $this->str['ok'] . '</font>';
dfac8649 122 }
94904cc3 123 $o .= '</li>';
dfac8649 124 }
94904cc3 125 $o .= ' </ul>';
9f5e5dee 126 }
dfac8649 127
94904cc3 128 return array($o, $wrongfields);
9f5e5dee 129 }
f33e1ed4 130
12f7d77c 131 /**
132 * Converts a default value suitable for display.
133 *
134 * @param string|null $defaultvalue Default value
135 * @return string Displayed version
136 */
137 protected static function display_default($defaultvalue) {
138 if ($defaultvalue === null) {
139 return '-';
140 } else {
141 return "'" . s($defaultvalue) . "'";
142 }
143 }
144
94904cc3 145 protected function display_results(array $wrongfields) {
9f5e5dee 146 global $DB;
147 $dbman = $DB->get_manager();
dfac8649 148
9f5e5dee 149 $s = '';
ac6efbaf 150 $r = '<table class="generaltable boxaligncenter boxwidthwide" border="0" cellpadding="5" cellspacing="0" id="results">';
94904cc3 151 $r .= ' <tr><td class="generalboxcontent">';
152 $r .= ' <h2 class="main">' . $this->str['searchresults'] . '</h2>';
153 $r .= ' <p class="centerpara">' . $this->str['wrongdefaults'] . ': ' . count($wrongfields) . '</p>';
154 $r .= ' </td></tr>';
155 $r .= ' <tr><td class="generalboxcontent">';
156
157 // If we have found wrong defaults inform about them.
158 if (count($wrongfields)) {
159 $r .= ' <p class="centerpara">' . $this->str['yeswrongdefaultsfound'] . '</p>';
160 $r .= ' <ul>';
161 foreach ($wrongfields as $obj) {
162 $xmldbtable = $obj->table;
163 $xmldbfield = $obj->field;
12f7d77c 164 $physicaltext = self::display_default($obj->physicaldefault);
165 $xmldbtext = self::display_default($obj->xmldbdefault);
9f5e5dee 166
94904cc3 167 // Get the alter table command.
168 $sqlarr = $dbman->generator->getAlterFieldSQL($xmldbtable, $xmldbfield);
9f5e5dee 169
94904cc3 170 $r .= ' <li>' . $this->str['table'] . ': ' . $xmldbtable->getName() . '. ' .
171 $this->str['field'] . ': ' . $xmldbfield->getName() . ', ' .
172 $this->str['expected'] . ' ' . $xmldbtext . ' ' .
173 $this->str['actual'] . ' ' . $physicaltext . '</li>';
174 // Add to output if we have sentences.
9f5e5dee 175 if ($sqlarr) {
176 $sqlarr = $dbman->generator->getEndedStatements($sqlarr);
94904cc3 177 $s .= '<code>' . str_replace("\n", '<br />', implode('<br />', $sqlarr)) . '</code><br />';
dfac8649 178 }
dfac8649 179 }
94904cc3 180 $r .= ' </ul>';
181 // Add the SQL statements (all together).
182 $r .= '<hr />' . $s;
9f5e5dee 183 } else {
94904cc3 184 $r .= ' <p class="centerpara">' . $this->str['nowrongdefaultsfound'] . '</p>';
dfac8649 185 }
94904cc3 186 $r .= ' </td></tr>';
187 $r .= ' <tr><td class="generalboxcontent">';
188 // Add the complete log message.
189 $r .= ' <p class="centerpara">' . $this->str['completelogbelow'] . '</p>';
190 $r .= ' </td></tr>';
191 $r .= '</table>';
9f5e5dee 192
193 return $r;
dfac8649 194 }
195}