MDL-29029 move dbtransfer to admin tools
[moodle.git] / admin / xmldb / actions / check_defaults / check_defaults.class.php
CommitLineData
0df0df23 1<?php
dfac8649 2
f25d53a9
EL
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/>.
dfac8649 17
f25d53a9
EL
18/**
19 * @package xmldb-editor
20 * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
dfac8649 23
f25d53a9
EL
24/**
25 * This class will check all the default values existing in the DB
26 * match those specified in the xml specs
27 * and providing one SQL script to fix all them.
28 *
29 * @package xmldb-editor
30 * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 */
9f5e5dee 33class check_defaults extends XMLDBCheckAction {
dfac8649 34
35 /**
36 * Init method, every subclass will have its own
37 */
38 function init() {
9f5e5dee 39 $this->introstr = 'confirmcheckdefaults';
dfac8649 40 parent::init();
41
42 /// Set own core attributes
43
44 /// Set own custom attributes
45
46 /// Get needed strings
47 $this->loadStrings(array(
dfac8649 48 'wrongdefaults' => 'xmldb',
dfac8649 49 'nowrongdefaultsfound' => 'xmldb',
50 'yeswrongdefaultsfound' => 'xmldb',
ac1dc984 51 'expected' => 'xmldb',
52 'actual' => 'xmldb',
dfac8649 53 ));
54 }
55
9f5e5dee 56 protected function check_table(xmldb_table $xmldb_table, array $metacolumns) {
57 $o = '';
dfac8649 58 $wrong_fields = array();
59
9f5e5dee 60 /// Get and process XMLDB fields
61 if ($xmldb_fields = $xmldb_table->getFields()) {
62 $o.=' <ul>';
63 foreach ($xmldb_fields as $xmldb_field) {
dfac8649 64
9f5e5dee 65 // Get the default value for the field
66 $xmldbdefault = $xmldb_field->getDefault();
f33e1ed4 67
9f5e5dee 68 /// If the metadata for that column doesn't exist or 'id' field found, skip
69 if (!isset($metacolumns[$xmldb_field->getName()]) or $xmldb_field->getName() == 'id') {
70 continue;
71 }
dfac8649 72
9f5e5dee 73 /// To variable for better handling
74 $metacolumn = $metacolumns[$xmldb_field->getName()];
dfac8649 75
9f5e5dee 76 /// Going to check this field in DB
77 $o.=' <li>' . $this->str['field'] . ': ' . $xmldb_field->getName() . ' ';
f33e1ed4 78
9f5e5dee 79 // get the value of the physical default (or blank if there isn't one)
80 if ($metacolumn->has_default==1) {
81 $physicaldefault = $metacolumn->default_value;
82 }
83 else {
84 $physicaldefault = '';
85 }
f33e1ed4 86
9f5e5dee 87 // there *is* a default and it's wrong
88 if ($physicaldefault != $xmldbdefault) {
89 $info = '('.$this->str['expected']." '$xmldbdefault', ".$this->str['actual'].
90 " '$physicaldefault')";
91 $o.='<font color="red">' . $this->str['wrong'] . " $info</font>";
92 /// Add the wrong field to the list
a226a972 93 $obj = new stdClass();
9f5e5dee 94 $obj->table = $xmldb_table;
95 $obj->field = $xmldb_field;
96 $obj->physicaldefault = $physicaldefault;
97 $obj->xmldbdefault = $xmldbdefault;
98 $wrong_fields[] = $obj;
99 } else {
100 $o.='<font color="green">' . $this->str['ok'] . '</font>';
dfac8649 101 }
9f5e5dee 102 $o.='</li>';
dfac8649 103 }
9f5e5dee 104 $o.=' </ul>';
105 }
dfac8649 106
9f5e5dee 107 return array($o, $wrong_fields);
108 }
f33e1ed4 109
9f5e5dee 110 protected function display_results(array $wrong_fields) {
111 global $DB;
112 $dbman = $DB->get_manager();
dfac8649 113
9f5e5dee 114 $s = '';
115 $r = '<table class="generalbox boxaligncenter boxwidthwide" border="0" cellpadding="5" cellspacing="0" id="results">';
116 $r.= ' <tr><td class="generalboxcontent">';
117 $r.= ' <h2 class="main">' . $this->str['searchresults'] . '</h2>';
118 $r.= ' <p class="centerpara">' . $this->str['wrongdefaults'] . ': ' . count($wrong_fields) . '</p>';
119 $r.= ' </td></tr>';
120 $r.= ' <tr><td class="generalboxcontent">';
121
122 /// If we have found wrong defaults inform about them
123 if (count($wrong_fields)) {
124 $r.= ' <p class="centerpara">' . $this->str['yeswrongdefaultsfound'] . '</p>';
125 $r.= ' <ul>';
126 foreach ($wrong_fields as $obj) {
127 $xmldb_table = $obj->table;
128 $xmldb_field = $obj->field;
129 $physicaldefault = $obj->physicaldefault;
130 $xmldbdefault = $obj->xmldbdefault;
131
132 // get the alter table command
133 $sqlarr = $dbman->generator->getAlterFieldSQL($xmldb_table, $xmldb_field);
134
135 $r.= ' <li>' . $this->str['table'] . ': ' . $xmldb_table->getName() . '. ' .
136 $this->str['field'] . ': ' . $xmldb_field->getName() . ', ' .
137 $this->str['expected'] . ' ' . "'$xmldbdefault'" . ' ' .
138 $this->str['actual'] . ' ' . "'$physicaldefault'" . '</li>';
139 /// Add to output if we have sentences
140 if ($sqlarr) {
141 $sqlarr = $dbman->generator->getEndedStatements($sqlarr);
142 $s.= '<code>' . str_replace("\n", '<br />', implode('<br />', $sqlarr)) . '</code><br />';
dfac8649 143 }
dfac8649 144 }
9f5e5dee 145 $r.= ' </ul>';
146 /// Add the SQL statements (all together)
147 $r.= '<hr />' . $s;
148 } else {
149 $r.= ' <p class="centerpara">' . $this->str['nowrongdefaultsfound'] . '</p>';
dfac8649 150 }
9f5e5dee 151 $r.= ' </td></tr>';
152 $r.= ' <tr><td class="generalboxcontent">';
153 /// Add the complete log message
154 $r.= ' <p class="centerpara">' . $this->str['completelogbelow'] . '</p>';
155 $r.= ' </td></tr>';
156 $r.= '</table>';
157
158 return $r;
dfac8649 159 }
160}