1 |
efrain |
1 |
<?php
|
|
|
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 |
/**
|
|
|
18 |
* @package tool_xmldb
|
|
|
19 |
* @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
|
|
|
20 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
21 |
*/
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* This class will ask and retrofit all the information from one
|
|
|
25 |
* mysql table present in the Moodle DB to one xmldb_table structure
|
|
|
26 |
*
|
|
|
27 |
* @package tool_xmldb
|
|
|
28 |
* @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
|
|
|
29 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
30 |
*/
|
|
|
31 |
class new_table_from_mysql extends XMLDBAction {
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Init method, every subclass will have its own
|
|
|
35 |
*/
|
|
|
36 |
function init() {
|
|
|
37 |
parent::init();
|
|
|
38 |
|
11 |
efrain |
39 |
global $DB;
|
|
|
40 |
if ($DB->get_dbfamily() !== 'mysql') {
|
|
|
41 |
throw new moodle_exception('DB family not supported');
|
|
|
42 |
}
|
|
|
43 |
|
1 |
efrain |
44 |
// Set own custom attributes
|
|
|
45 |
|
|
|
46 |
// Get needed strings
|
|
|
47 |
$this->loadStrings(array(
|
|
|
48 |
'createtable' => 'tool_xmldb',
|
|
|
49 |
'aftertable' => 'tool_xmldb',
|
|
|
50 |
'create' => 'tool_xmldb',
|
|
|
51 |
'back' => 'tool_xmldb'
|
|
|
52 |
));
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Invoke method, every class will have its own
|
|
|
57 |
* returns true/false on completion, setting both
|
|
|
58 |
* errormsg and output as necessary
|
|
|
59 |
*/
|
|
|
60 |
function invoke() {
|
|
|
61 |
parent::invoke();
|
|
|
62 |
|
|
|
63 |
// Set own core attributes
|
|
|
64 |
$this->does_generate = ACTION_GENERATE_HTML;
|
|
|
65 |
|
|
|
66 |
// These are always here
|
|
|
67 |
global $CFG, $XMLDB, $DB, $OUTPUT;
|
|
|
68 |
|
|
|
69 |
// Do the job, setting result as needed
|
|
|
70 |
// Get the dir containing the file
|
|
|
71 |
$dirpath = required_param('dir', PARAM_PATH);
|
|
|
72 |
$dirpath = $CFG->dirroot . $dirpath;
|
|
|
73 |
|
|
|
74 |
// Get the correct dirs
|
|
|
75 |
if (!empty($XMLDB->dbdirs)) {
|
|
|
76 |
$dbdir = $XMLDB->dbdirs[$dirpath];
|
|
|
77 |
} else {
|
|
|
78 |
return false;
|
|
|
79 |
}
|
|
|
80 |
if (!empty($XMLDB->editeddirs)) {
|
|
|
81 |
$editeddir = $XMLDB->editeddirs[$dirpath];
|
|
|
82 |
$structure = $editeddir->xml_file->getStructure();
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
$tableparam = optional_param('table', NULL, PARAM_CLEAN);
|
|
|
86 |
|
|
|
87 |
// If no table, show form
|
|
|
88 |
if (!$tableparam) {
|
|
|
89 |
// No postaction here
|
|
|
90 |
$this->postaction = NULL;
|
|
|
91 |
// Get list of tables
|
|
|
92 |
$dbtables = $DB->get_tables();
|
|
|
93 |
$selecttables = array();
|
|
|
94 |
foreach ($dbtables as $dbtable) {
|
|
|
95 |
$i = $structure->findTableInArray($dbtable);
|
|
|
96 |
if ($i === NULL) {
|
|
|
97 |
$selecttables[$dbtable] = $dbtable;
|
|
|
98 |
}
|
|
|
99 |
}
|
|
|
100 |
// Get list of after tables
|
|
|
101 |
$aftertables = array();
|
|
|
102 |
if ($tables = $structure->getTables()) {
|
|
|
103 |
foreach ($tables as $aftertable) {
|
|
|
104 |
$aftertables[$aftertable->getName()] = $aftertable->getName();
|
|
|
105 |
}
|
|
|
106 |
}
|
|
|
107 |
if (!$selecttables) {
|
|
|
108 |
$this->errormsg = 'No tables available to be retrofitted';
|
|
|
109 |
return false;
|
|
|
110 |
}
|
|
|
111 |
// Now build the form
|
|
|
112 |
$o = '<form id="form" action="index.php" method="post">';
|
|
|
113 |
$o .= '<div>';
|
|
|
114 |
$o.= ' <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot, '', $dirpath) . '" />';
|
|
|
115 |
$o.= ' <input type="hidden" name ="action" value="new_table_from_mysql" />';
|
|
|
116 |
$o.= ' <input type="hidden" name ="postaction" value="edit_table" />';
|
|
|
117 |
$o.= ' <input type="hidden" name ="sesskey" value="' . sesskey() . '" />';
|
|
|
118 |
$o.= ' <table id="formelements" class="boxaligncenter" cellpadding="5">';
|
|
|
119 |
$o.= ' <tr><td><label for="menutable" accesskey="t">' . $this->str['createtable'] .' </label>' . html_writer::select($selecttables, 'table') . '<label for="menuafter" accesskey="a">' . $this->str['aftertable'] . ' </label>' .html_writer::select($aftertables, 'after') . '</td></tr>';
|
|
|
120 |
$o.= ' <tr><td colspan="2" align="center"><input type="submit" value="' .$this->str['create'] . '" /></td></tr>';
|
|
|
121 |
$o.= ' <tr><td colspan="2" align="center"><a href="index.php?action=edit_xml_file&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['back'] . ']</a></td></tr>';
|
|
|
122 |
$o.= ' </table>';
|
|
|
123 |
$o.= '</div></form>';
|
|
|
124 |
|
|
|
125 |
$this->output = $o;
|
|
|
126 |
|
|
|
127 |
|
|
|
128 |
// If table, retrofit information and, if everything works,
|
|
|
129 |
// go to the table edit action
|
|
|
130 |
} else {
|
11 |
efrain |
131 |
// Get some params (table is mandatory here).
|
|
|
132 |
$tableparam = required_param('table', PARAM_ALPHAEXT);
|
|
|
133 |
$afterparam = required_param('after', PARAM_ALPHAEXT);
|
1 |
efrain |
134 |
|
11 |
efrain |
135 |
if (empty($tableparam) || empty($afterparam)) {
|
|
|
136 |
throw new moodle_exception('Invalid param value detected.');
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
// Create one new xmldb_table.
|
1 |
efrain |
140 |
$table = new xmldb_table(strtolower(trim($tableparam)));
|
|
|
141 |
$table->setComment($table->getName() . ' table retrofitted from MySQL');
|
|
|
142 |
// Get fields info from ADODb
|
|
|
143 |
$dbfields = $DB->get_columns($tableparam);
|
|
|
144 |
if ($dbfields) {
|
|
|
145 |
foreach ($dbfields as $dbfield) {
|
|
|
146 |
// Create new XMLDB field
|
|
|
147 |
$field = new xmldb_field($dbfield->name);
|
|
|
148 |
// Set field with info retrofitted
|
|
|
149 |
$field->setFromADOField($dbfield);
|
|
|
150 |
// Add field to the table
|
|
|
151 |
$table->addField($field);
|
|
|
152 |
}
|
|
|
153 |
}
|
|
|
154 |
// Get PK, UK and indexes info from ADODb
|
|
|
155 |
$dbindexes = $DB->get_indexes($tableparam);
|
|
|
156 |
if ($dbindexes) {
|
|
|
157 |
foreach ($dbindexes as $indexname => $dbindex) {
|
|
|
158 |
// Add the indexname to the array
|
|
|
159 |
$dbindex['name'] = $indexname;
|
|
|
160 |
// We are handling one xmldb_key (primaries + uniques)
|
|
|
161 |
if ($dbindex['unique']) {
|
|
|
162 |
$key = new xmldb_key(strtolower($dbindex['name']));
|
|
|
163 |
// Set key with info retrofitted
|
|
|
164 |
$key->setFromADOKey($dbindex);
|
|
|
165 |
// Add key to the table
|
|
|
166 |
$table->addKey($key);
|
|
|
167 |
|
|
|
168 |
// We are handling one xmldb_index (non-uniques)
|
|
|
169 |
} else {
|
|
|
170 |
$index = new xmldb_index(strtolower($dbindex['name']));
|
|
|
171 |
// Set index with info retrofitted
|
|
|
172 |
$index->setFromADOIndex($dbindex);
|
|
|
173 |
// Add index to the table
|
|
|
174 |
$table->addIndex($index);
|
|
|
175 |
}
|
|
|
176 |
}
|
|
|
177 |
}
|
11 |
efrain |
178 |
// Finally, add the whole retrofitted table to the structure in the place specified.
|
1 |
efrain |
179 |
$structure->addTable($table, $afterparam);
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
// Launch postaction if exists (leave this here!)
|
11 |
efrain |
183 |
if ($this->getPostAction()) {
|
1 |
efrain |
184 |
return $this->launch($this->getPostAction());
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
// Return ok if arrived here
|
11 |
efrain |
188 |
return true;
|
1 |
efrain |
189 |
}
|
|
|
190 |
}
|
|
|
191 |
|