mirrored from git://git.moodle.org/moodle.git
/
XMLDBCheckAction.class.php
211 lines (186 loc) · 8.52 KB
/
XMLDBCheckAction.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package tool_xmldb
* @copyright 2008 onwards Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* This is a base class for the various actions that interate over all the
* tables and check some aspect of their definition.
*
* @package tool_xmldb
* @copyright 2008 onwards Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class XMLDBCheckAction extends XMLDBAction {
/**
* @var string This string is displayed with a yes/no choice before the report is run.
* You must set this to the name of a lang string in xmldb.php before calling init.
*/
protected $introstr = '';
/**
* Init method, every subclass will have its own
*/
function init() {
parent::init();
// Set own core attributes
// Set own custom attributes
// Get needed strings
$this->loadStrings(array(
$this->introstr => 'tool_xmldb',
'ok' => '',
'wrong' => 'tool_xmldb',
'table' => 'tool_xmldb',
'field' => 'tool_xmldb',
'searchresults' => 'tool_xmldb',
'completelogbelow' => 'tool_xmldb',
'yes' => '',
'no' => '',
'error' => '',
'back' => 'tool_xmldb'
));
}
/**
* Invoke method, every class will have its own
* returns true/false on completion, setting both
* errormsg and output as necessary
*/
function invoke() {
parent::invoke();
$result = true;
// Set own core attributes
$this->does_generate = ACTION_GENERATE_HTML;
// These are always here
global $CFG, $XMLDB, $DB, $OUTPUT;
// And we nedd some ddl suff
$dbman = $DB->get_manager();
// Here we'll acummulate all the wrong fields found
$problemsfound = array();
// Do the job, setting $result as needed
// Get the confirmed to decide what to do
$confirmed = optional_param('confirmed', false, PARAM_BOOL);
// If not confirmed, show confirmation box
if (!$confirmed) {
$o = '<table class="generaltable" border="0" cellpadding="5" cellspacing="0" id="notice">';
$o.= ' <tr><td class="generalboxcontent">';
$o.= ' <p class="centerpara">' . $this->str[$this->introstr] . '</p>';
$o.= ' <table class="boxaligncenter" cellpadding="20"><tr><td>';
$o.= ' <div class="singlebutton">';
$o.= ' <form action="index.php?action=' . $this->title . '&confirmed=yes&sesskey=' . sesskey() . '" method="post"><fieldset class="invisiblefieldset">';
$o.= ' <input type="submit" value="'. $this->str['yes'] .'" /></fieldset></form></div>';
$o.= ' </td><td>';
$o.= ' <div class="singlebutton">';
$o.= ' <form action="index.php?action=main_view" method="post"><fieldset class="invisiblefieldset">';
$o.= ' <input type="submit" value="'. $this->str['no'] .'" /></fieldset></form></div>';
$o.= ' </td></tr>';
$o.= ' </table>';
$o.= ' </td></tr>';
$o.= '</table>';
$this->output = $o;
} else {
// The back to edit table button
$b = ' <p class="centerpara buttons">';
$b .= '<a href="index.php">[' . $this->str['back'] . ']</a>';
$b .= '</p>';
// Iterate over $XMLDB->dbdirs, loading their XML data to memory
if ($XMLDB->dbdirs) {
$dbdirs = $XMLDB->dbdirs;
$o='<ul>';
foreach ($dbdirs as $dbdir) {
// Only if the directory exists
if (!$dbdir->path_exists) {
continue;
}
// Load the XML file
$xmldb_file = new xmldb_file($dbdir->path . '/install.xml');
// Only if the file exists
if (!$xmldb_file->fileExists()) {
continue;
}
// Load the XML contents to structure
$loaded = $xmldb_file->loadXMLStructure();
if (!$loaded || !$xmldb_file->isLoaded()) {
echo $OUTPUT->notification('Errors found in XMLDB file: '. $dbdir->path . '/install.xml');
continue;
}
// Arriving here, everything is ok, get the XMLDB structure
$structure = $xmldb_file->getStructure();
$o.=' <li>' . str_replace($CFG->dirroot . '/', '', $dbdir->path . '/install.xml');
// Getting tables
if ($xmldb_tables = $structure->getTables()) {
$o.=' <ul>';
// Foreach table, process its fields
foreach ($xmldb_tables as $xmldb_table) {
// Skip table if not exists
if (!$dbman->table_exists($xmldb_table)) {
continue;
}
// Fetch metadata from physical DB. All the columns info.
if (!$metacolumns = $DB->get_columns($xmldb_table->getName())) {
// / Skip table if no metacolumns is available for it
continue;
}
// Table processing starts here
$o.=' <li>' . $xmldb_table->getName();
// Do the specific check.
list($output, $newproblems) = $this->check_table($xmldb_table, $metacolumns);
$o.=$output;
$problemsfound = array_merge($problemsfound, $newproblems);
$o.=' </li>';
// Give the script some more time (resetting to current if exists)
if ($currenttl = @ini_get('max_execution_time')) {
@ini_set('max_execution_time',$currenttl);
}
}
$o.=' </ul>';
}
$o.=' </li>';
}
$o.='</ul>';
}
// Create a report of the problems found.
$r = $this->display_results($problemsfound);
// Combine the various bits of output.
$this->output = $b . $r . $o;
}
// Launch postaction if exists (leave this here!)
if ($this->getPostAction() && $result) {
return $this->launch($this->getPostAction());
}
// Return ok if arrived here
return $result;
}
/**
* Do the checks necessary on one particular table.
*
* @param xmldb_table $xmldb_table the table definition from the install.xml file.
* @param array $metacolumns the column information read from the database.
* @return array an array with two elements: First, some additional progress output,
* for example a list (<ul>) of the things check each with an one work ok/not ok summary.
* Second, an array giving the details of any problems found. These arrays
* for all tables will be aggregated, and then passed to
*/
abstract protected function check_table(xmldb_table $xmldb_table, array $metacolumns);
/**
* Display a list of the problems found.
*
* @param array $problems_found an aggregation of all the problems found by
* all the check_table calls.
* @return string a display of all the problems found as HTML.
*/
abstract protected function display_results(array $problems_found);
}