Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / admin / xmldb / actions / view_reserved_words / view_reserved_words.class.php
blobc24cce12bb0d7fad5899d9ca111640798eb4faf2
1 <?php // $Id$
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 ///////////////////////////////////////////////////////////////////////////
27 /// This class will show all the reserved words in a format suitable to
28 /// be pasted to: http://docs.moodle.org/en/XMLDB_reserved_words and
29 /// http://docs.moodle.org/en/Database_reserved_words
30 /// Also, it introspects te DB looking for such words and informing about
32 class view_reserved_words extends XMLDBAction {
34 /**
35 * Init method, every subclass will have its own
37 function init() {
38 parent::init();
40 /// Set own custom attributes
42 /// Get needed strings
43 $this->loadStrings(array(
44 'listreservedwords' => 'xmldb',
45 'wrongreservedwords' => 'xmldb',
46 'table' => 'xmldb',
47 'field' => 'xmldb',
48 'back' => 'xmldb'
49 ));
52 /**
53 * Invoke method, every class will have its own
54 * returns true/false on completion, setting both
55 * errormsg and output as necessary
57 function invoke() {
58 parent::invoke();
60 $result = true;
62 /// Set own core attributes
63 $this->does_generate = ACTION_GENERATE_HTML;
65 /// These are always here
66 global $CFG, $XMLDB, $db;
68 /// Calculate list of available SQL generators
69 $plugins = get_list_of_plugins('lib/xmldb/classes/generators');
70 $reserved_words = array();
71 $reserved_words_bydb = array();
72 foreach($plugins as $plugin) {
73 $classname = 'XMLDB' . $plugin;
74 $generator = new $classname();
75 $reserved_words = array_merge($reserved_words, $generator->getReservedWords());
76 $reserved_words_bydb[$plugin] = $generator->getReservedWords();
78 sort($reserved_words);
79 $reserved_words = array_unique($reserved_words);
81 /// Now, calculate, looking into current DB (with AdoDB Metadata), which fields are
82 /// in the list of reserved words
83 $wronguses = array();
84 $dbtables = $db->MetaTables('TABLES');
85 if ($dbtables) {
86 foreach ($dbtables as $dbtable) {
87 $table = str_replace($CFG->prefix, '', $dbtable);
88 if (in_array($table, $reserved_words)) {
89 $list_of_db = array();
90 foreach ($reserved_words_bydb as $key=>$words) {
91 if (in_array($table, $words)) {
92 $list_of_db[] = $key;
95 $wronguses[] = $this->str['table'] . ' - ' . $table . ' (' . implode(', ',$list_of_db) . ')';
98 $dbfields = $db->MetaColumns($dbtable);
99 if ($dbfields) {
100 foreach ($dbfields as $dbfield) {
101 if (in_array($dbfield->name, $reserved_words)) {
102 $list_of_db = array();
103 foreach ($reserved_words_bydb as $key=>$words) {
104 if (in_array($dbfield->name, $words)) {
105 $list_of_db[] = $key;
108 $wronguses[] = $this->str['field'] . ' - ' . $table . '->' . $dbfield->name . ' (' . implode(', ',$list_of_db) . ')';
115 /// Sort the wrong uses
116 sort($wronguses);
118 /// The back to edit table button
119 $b = ' <p class="centerpara buttons">';
120 $b .= '<a href="index.php">[' . $this->str['back'] . ']</a>';
121 $b .= '</p>';
122 $o = $b;
124 /// The list of currently wrong field names
125 if ($wronguses) {
126 $o.= ' <table id="formelements" class="boxaligncenter" cellpadding="5">';
127 $o.= ' <tr><td align="center"><font color="red">' . $this->str['wrongreservedwords'] . '</font></td></tr>';
128 $o.= ' <tr><td>';
129 $o.= ' <ul><li>' . implode('</li><li>', $wronguses) . '</li></ul>';
130 $o.= ' </td></tr>';
131 $o.= ' </table>';
134 /// The textarea showing all the reserved words
135 $o.= ' <table id="formelements" class="boxaligncenter" cellpadding="5">';
136 $o.= ' <tr><td align="center">' . $this->str['listreservedwords'].'</td></tr>';
137 $o.= ' <tr><td><textarea cols="80" rows="32">';
138 $o.= s(implode(', ', $reserved_words));
139 $o.= '</textarea></td></tr>';
140 $o.= ' </table>';
142 $this->output = $o;
144 /// Launch postaction if exists (leave this here!)
145 if ($this->getPostAction() && $result) {
146 return $this->launch($this->getPostAction());
149 /// Return ok if arrived here
150 return $result;