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