MDL-15352: doesn't use anymore mnet system function, implemeted new mnet function...
[moodle.git] / admin / xmldb / actions / check_defaults / check_defaults.class.php
CommitLineData
dfac8649 1<?php // $Id$
2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.com //
9// //
46293bd7 10// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
dfac8649 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 default values existing in the DB
28/// match those specified in the xml specs
f33e1ed4 29/// and providing one SQL script to fix all them.
dfac8649 30
31class check_defaults extends XMLDBAction {
32
33 /**
34 * Init method, every subclass will have its own
35 */
36 function init() {
37 parent::init();
38
39 /// Set own core attributes
40
41 /// Set own custom attributes
42
43 /// Get needed strings
44 $this->loadStrings(array(
45 'confirmcheckdefaults' => 'xmldb',
46 'ok' => '',
47 'wrong' => 'xmldb',
48 'table' => 'xmldb',
49 'field' => 'xmldb',
50 'searchresults' => 'xmldb',
51 'wrongdefaults' => 'xmldb',
52 'completelogbelow' => 'xmldb',
53 'nowrongdefaultsfound' => 'xmldb',
54 'yeswrongdefaultsfound' => 'xmldb',
55 'yes' => '',
56 'no' => '',
57 'error' => '',
ac1dc984 58 'expected' => 'xmldb',
59 'actual' => 'xmldb',
dfac8649 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
f33e1ed4 79 global $CFG, $XMLDB, $DB;
dfac8649 80
81 /// And we nedd some ddl suff
f33e1ed4 82 $dbman = $DB->get_manager();
dfac8649 83
84 /// Here we'll acummulate all the wrong fields found
85 $wrong_fields = array();
86
87 /// Do the job, setting $result as needed
88
89 /// Get the confirmed to decide what to do
90 $confirmed = optional_param('confirmed', false, PARAM_BOOL);
91
92 /// If not confirmed, show confirmation box
93 if (!$confirmed) {
94 $o = '<table class="generalbox" border="0" cellpadding="5" cellspacing="0" id="notice">';
95 $o.= ' <tr><td class="generalboxcontent">';
96 $o.= ' <p class="centerpara">' . $this->str['confirmcheckdefaults'] . '</p>';
97 $o.= ' <table class="boxaligncenter" cellpadding="20"><tr><td>';
98 $o.= ' <div class="singlebutton">';
99 $o.= ' <form action="index.php?action=check_defaults&amp;confirmed=yes" method="post"><fieldset class="invisiblefieldset">';
100 $o.= ' <input type="submit" value="'. $this->str['yes'] .'" /></fieldset></form></div>';
101 $o.= ' </td><td>';
102 $o.= ' <div class="singlebutton">';
103 $o.= ' <form action="index.php?action=main_view" method="post"><fieldset class="invisiblefieldset">';
104 $o.= ' <input type="submit" value="'. $this->str['no'] .'" /></fieldset></form></div>';
105 $o.= ' </td></tr>';
106 $o.= ' </table>';
107 $o.= ' </td></tr>';
108 $o.= '</table>';
109
110 $this->output = $o;
111 } else {
112 /// The back to edit table button
113 $b = ' <p class="centerpara buttons">';
114 $b .= '<a href="index.php">[' . $this->str['back'] . ']</a>';
115 $b .= '</p>';
116
117 /// Iterate over $XMLDB->dbdirs, loading their XML data to memory
118 if ($XMLDB->dbdirs) {
119 $dbdirs =& $XMLDB->dbdirs;
120 $o='<ul>';
121 foreach ($dbdirs as $dbdir) {
122 /// Only if the directory exists
123 if (!$dbdir->path_exists) {
124 continue;
125 }
126 /// Load the XML file
46293bd7 127 $xmldb_file = new xmldb_file($dbdir->path . '/install.xml');
dfac8649 128
129 /// Only if the file exists
130 if (!$xmldb_file->fileExists()) {
131 continue;
132 }
133 /// Load the XML contents to structure
134 $loaded = $xmldb_file->loadXMLStructure();
135 if (!$loaded || !$xmldb_file->isLoaded()) {
136 notify('Errors found in XMLDB file: '. $dbdir->path . '/install.xml');
137 continue;
138 }
139 /// Arriving here, everything is ok, get the XMLDB structure
140 $structure = $xmldb_file->getStructure();
f33e1ed4 141
dfac8649 142 $o.=' <li>' . str_replace($CFG->dirroot . '/', '', $dbdir->path . '/install.xml');
143 /// Getting tables
144 if ($xmldb_tables = $structure->getTables()) {
145 $o.=' <ul>';
146 /// Foreach table, process its fields
147 foreach ($xmldb_tables as $xmldb_table) {
148 /// Skip table if not exists
f33e1ed4 149 if (!$dbman->table_exists($xmldb_table)) {
dfac8649 150 continue;
151 }
152 /// Fetch metadata from phisical DB. All the columns info.
f33e1ed4 153 if (!$metacolumns = $DB->get_columns($xmldb_table->getName())) {
dfac8649 154 //// Skip table if no metacolumns is available for it
155 continue;
156 }
157 /// Table processing starts here
158 $o.=' <li>' . $xmldb_table->getName();
159 /// Get and process XMLDB fields
160 if ($xmldb_fields = $xmldb_table->getFields()) {
161 $o.=' <ul>';
162 foreach ($xmldb_fields as $xmldb_field) {
dfac8649 163
f33e1ed4 164 // Get the default value for the field
165 $xmldbdefault = $xmldb_field->getDefault();
dfac8649 166
f33e1ed4 167 /// If the metadata for that column doesn't exist or 'id' field found, skip
168 if (!isset($metacolumns[$xmldb_field->getName()]) or $xmldb_field->getName() == 'id') {
169 continue;
170 }
dfac8649 171
f33e1ed4 172 /// To variable for better handling
173 $metacolumn = $metacolumns[$xmldb_field->getName()];
dfac8649 174
f33e1ed4 175 /// Going to check this field in DB
176 $o.=' <li>' . $this->str['field'] . ': ' . $xmldb_field->getName() . ' ';
177
178 // get the value of the physical default (or blank if there isn't one)
179 if ($metacolumn->has_default==1) {
180 $physicaldefault = $metacolumn->default_value;
181 }
182 else {
183 $physicaldefault = '';
184 }
185
186 // there *is* a default and it's wrong
187 if ($physicaldefault != $xmldbdefault) {
ac1dc984 188 $info = '('.$this->str['expected']." '$xmldbdefault', ".$this->str['actual'].
189 " '$physicaldefault')";
dfac8649 190 $o.='<font color="red">' . $this->str['wrong'] . " $info</font>";
191 /// Add the wrong field to the list
192 $obj = new object;
193 $obj->table = $xmldb_table;
194 $obj->field = $xmldb_field;
195 $obj->physicaldefault = $physicaldefault;
196 $obj->xmldbdefault = $xmldbdefault;
197 $wrong_fields[] = $obj;
198 } else {
199 $o.='<font color="green">' . $this->str['ok'] . '</font>';
200 }
201 $o.='</li>';
202 }
203 $o.=' </ul>';
204 }
205 $o.=' </li>';
206 }
207 $o.=' </ul>';
208 }
209 $o.=' </li>';
210 }
211 $o.='</ul>';
212 }
213
214 /// We have finished, let's show the results of the search
215 $s = '';
216 $r = '<table class="generalbox boxaligncenter boxwidthwide" border="0" cellpadding="5" cellspacing="0" id="results">';
217 $r.= ' <tr><td class="generalboxcontent">';
218 $r.= ' <h2 class="main">' . $this->str['searchresults'] . '</h2>';
219 $r.= ' <p class="centerpara">' . $this->str['wrongdefaults'] . ': ' . count($wrong_fields) . '</p>';
220 $r.= ' </td></tr>';
221 $r.= ' <tr><td class="generalboxcontent">';
222
223 /// If we have found wrong defaults inform about them
224 if (count($wrong_fields)) {
225 $r.= ' <p class="centerpara">' . $this->str['yeswrongdefaultsfound'] . '</p>';
226 $r.= ' <ul>';
227 foreach ($wrong_fields as $obj) {
228 $xmldb_table = $obj->table;
229 $xmldb_field = $obj->field;
230 $physicaldefault = $obj->physicaldefault;
231 $xmldbdefault = $obj->xmldbdefault;
f33e1ed4 232
dfac8649 233 // get the alter table command
f33e1ed4 234 $sqlarr = $dbman->generator->getAlterFieldSQL($xmldb_table, $xmldb_field);
dfac8649 235
236 $r.= ' <li>' . $this->str['table'] . ': ' . $xmldb_table->getName() . '. ' .
237 $this->str['field'] . ': ' . $xmldb_field->getName() . ', ' .
ac1dc984 238 $this->str['expected'] . ' ' . "'$xmldbdefault'" . ' ' .
239 $this->str['actual'] . ' ' . "'$physicaldefault'" . '</li>';
dfac8649 240 /// Add to output if we have sentences
241 if ($sqlarr) {
f33e1ed4 242 $sqlarr = $dbman->generator->getEndedStatements($sqlarr);
dfac8649 243 $s.= '<code>' . str_replace("\n", '<br />', implode('<br />', $sqlarr)) . '</code><br />';
244 }
245 }
246 $r.= ' </ul>';
247 /// Add the SQL statements (all together)
248 $r.= '<hr />' . $s;
249 } else {
25b05ce5 250 $r.= ' <p class="centerpara">' . $this->str['nowrongdefaultsfound'] . '</p>';
dfac8649 251 }
252 $r.= ' </td></tr>';
253 $r.= ' <tr><td class="generalboxcontent">';
254 /// Add the complete log message
255 $r.= ' <p class="centerpara">' . $this->str['completelogbelow'] . '</p>';
256 $r.= ' </td></tr>';
257 $r.= '</table>';
258
259 $this->output = $b . $r . $o;
260 }
261
262 /// Launch postaction if exists (leave this here!)
263 if ($this->getPostAction() && $result) {
264 return $this->launch($this->getPostAction());
265 }
266
267 /// Return ok if arrived here
268 return $result;
269 }
270}
271?>