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