MDL-29029 move dbtransfer to admin tools
[moodle.git] / admin / xmldb / actions / XMLDBCheckAction.class.php
CommitLineData
bd79ec56 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * @package xmldb-editor
20 * @copyright 2008 onwards Tim Hunt
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
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 *
28 * @package xmldb-editor
29 * @copyright 2008 onwards Tim Hunt
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 */
9f5e5dee 32abstract class XMLDBCheckAction extends XMLDBAction {
33 /**
34 * This string is displayed with a yes/no choice before the report is run.
35 * You must set this to the name of a lang string in xmldb.php before calling init.
36 */
37 protected $introstr = '';
38
39 /**
40 * Init method, every subclass will have its own
41 */
42 function init() {
43 parent::init();
44
45 /// Set own core attributes
46
47 /// Set own custom attributes
48
49 /// Get needed strings
50 $this->loadStrings(array(
51 $this->introstr => 'xmldb',
52 'ok' => '',
53 'wrong' => 'xmldb',
54 'table' => 'xmldb',
55 'field' => 'xmldb',
56 'searchresults' => 'xmldb',
57 'completelogbelow' => 'xmldb',
58 'yes' => '',
59 'no' => '',
60 'error' => '',
61 'back' => 'xmldb'
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
8fbce1c8 79 global $CFG, $XMLDB, $DB, $OUTPUT;
9f5e5dee 80
81 /// And we nedd some ddl suff
82 $dbman = $DB->get_manager();
83
84 /// Here we'll acummulate all the wrong fields found
85 $problemsfound = 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[$this->introstr] . '</p>';
97 $o.= ' <table class="boxaligncenter" cellpadding="20"><tr><td>';
98 $o.= ' <div class="singlebutton">';
f25d53a9 99 $o.= ' <form action="index.php?action=' . $this->title . '&amp;confirmed=yes&amp;sesskey=' . sesskey() . '" method="post"><fieldset class="invisiblefieldset">';
9f5e5dee 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
127 $xmldb_file = new xmldb_file($dbdir->path . '/install.xml');
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()) {
8fbce1c8 136 echo $OUTPUT->notification('Errors found in XMLDB file: '. $dbdir->path . '/install.xml');
9f5e5dee 137 continue;
138 }
139 /// Arriving here, everything is ok, get the XMLDB structure
140 $structure = $xmldb_file->getStructure();
141
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
149 if (!$dbman->table_exists($xmldb_table)) {
150 continue;
151 }
eab8ed9f 152 /// Fetch metadata from physical DB. All the columns info.
9f5e5dee 153 if (!$metacolumns = $DB->get_columns($xmldb_table->getName())) {
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 /// Do the specific check.
160 list($output, $newproblems) = $this->check_table($xmldb_table, $metacolumns);
161 $o.=$output;
162 $problemsfound = array_merge($problemsfound, $newproblems);
163 $o.=' </li>';
bd79ec56 164 /// Give the script some more time (resetting to current if exists)
165 if ($currenttl = @ini_get('max_execution_time')) {
166 @ini_set('max_execution_time',$currenttl);
167 }
9f5e5dee 168 }
169 $o.=' </ul>';
170 }
171 $o.=' </li>';
172 }
173 $o.='</ul>';
174 }
175
176 /// Create a report of the problems found.
177 $r = $this->display_results($problemsfound);
178
179 /// Combine the various bits of output.
180 $this->output = $b . $r . $o;
181 }
182
183 /// Launch postaction if exists (leave this here!)
184 if ($this->getPostAction() && $result) {
185 return $this->launch($this->getPostAction());
186 }
187
188 /// Return ok if arrived here
189 return $result;
190 }
191
192 /**
193 * Do the checks necessary on one particular table.
194 *
195 * @param xmldb_table $xmldb_table the table definition from the install.xml file.
196 * @param array $metacolumns the column information read from the database.
197 * @return array an array with two elements: First, some additional progress output,
198 * for example a list (<ul>) of the things check each with an one work ok/not ok summary.
199 * Second, an array giving the details of any problems found. These arrays
8926f844 200 * for all tables will be aggregated, and then passed to
9f5e5dee 201 */
202 abstract protected function check_table(xmldb_table $xmldb_table, array $metacolumns);
203
204 /**
205 * Display a list of the problems found.
206 *
207 * @param array $problems_found an aggregation of all the problems found by
208 * all the check_table calls.
209 * @return string a display of all the problems found as HTML.
210 */
211 abstract protected function display_results(array $problems_found);
212}