mirrored from git://git.moodle.org/moodle.git
/
view_structure_php.class.php
316 lines (272 loc) · 11.7 KB
/
view_structure_php.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package tool
* @subpackage xmldb
* @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* This class will show the PHP needed (upgrade block) to perform
* the desired DDL action with the specified table
*
* @package tool
* @subpackage xmldb
* @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class view_structure_php extends XMLDBAction {
/**
* Init method, every subclass will have its own
*/
function init() {
parent::init();
// Set own custom attributes
$this->sesskey_protected = false; // This action doesn't need sesskey protection
// Get needed strings
$this->loadStrings(array(
'selectaction' => 'tool_xmldb',
'selecttable' => 'tool_xmldb',
'view' => 'tool_xmldb',
'back' => 'tool_xmldb'
));
}
/**
* Invoke method, every class will have its own
* returns true/false on completion, setting both
* errormsg and output as necessary
*/
function invoke() {
parent::invoke();
$result = true;
// Set own core attributes
$this->does_generate = ACTION_GENERATE_HTML;
// These are always here
global $CFG, $XMLDB, $OUTPUT;
// Do the job, setting result as needed
// Get the dir containing the file
$dirpath = required_param('dir', PARAM_PATH);
$dirpath = $CFG->dirroot . $dirpath;
// Get the correct dirs
if (!empty($XMLDB->dbdirs)) {
$dbdir =& $XMLDB->dbdirs[$dirpath];
} else {
return false;
}
if (!empty($XMLDB->editeddirs)) {
$editeddir =& $XMLDB->editeddirs[$dirpath];
$structure =& $editeddir->xml_file->getStructure();
}
$tables =& $structure->getTables();
$table = reset($tables);
$defaulttable = null;
if ($table) {
$defaulttable = $table->getName();
}
// Get parameters
$commandparam = optional_param('command', 'create_table', PARAM_PATH);
$tableparam = optional_param('table', $defaulttable, PARAM_PATH);
// The back to edit xml button
$b = ' <p class="centerpara buttons">';
$b .= '<a href="index.php?action=edit_xml_file&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['back'] . ']</a>';
$b .= '</p>';
$o = $b;
// Calculate the popup of commands
$commands = array('create_table',
'drop_table',
'rename_table');
foreach ($commands as $command) {
$popcommands[$command] = str_replace('_', ' ', $command);
}
// Calculate the popup of tables
foreach ($tables as $table) {
$poptables[$table->getName()] = $table->getName();
}
// Now build the form
$o.= '<form id="form" action="index.php" method="post">';
$o.='<div>';
$o.= ' <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot, '', $dirpath) . '" />';
$o.= ' <input type="hidden" name ="action" value="view_structure_php" />';
$o.= ' <table id="formelements" class="boxaligncenter" cellpadding="5">';
$o.= ' <tr><td><label for="menucommand" accesskey="c">' . $this->str['selectaction'] .' </label>' . html_writer::select($popcommands, 'command', $commandparam, false) . ' <label for="menutable" accesskey="t">' . $this->str['selecttable'] . ' </label>' .html_writer::select($poptables, 'table', $tableparam, false) . '</td></tr>';
$o.= ' <tr><td colspan="2" align="center"><input type="submit" value="' .$this->str['view'] . '" /></td></tr>';
$o.= ' </table>';
$o.= '</div></form>';
$o.= ' <table id="phpcode" class="boxaligncenter" cellpadding="5">';
$o.= ' <tr><td><textarea cols="80" rows="32">';
// Based on current params, call the needed function
switch ($commandparam) {
case 'create_table':
$o.= s($this->create_table_php($structure, $tableparam));
break;
case 'drop_table':
$o.= s($this->drop_table_php($structure, $tableparam));
break;
case 'rename_table':
$o.= s($this->rename_table_php($structure, $tableparam));
break;
}
$o.= '</textarea></td></tr>';
$o.= ' </table>';
$this->output = $o;
// Launch postaction if exists (leave this here!)
if ($this->getPostAction() && $result) {
return $this->launch($this->getPostAction());
}
// Return ok if arrived here
return $result;
}
/**
* This function will generate all the PHP code needed to
* create one table using XMLDB objects and functions
*
* @param xmldb_structure structure object containing all the info
* @param string table table code to be created
* @return string PHP code to be used to create the table
*/
function create_table_php($structure, $table) {
$result = '';
// Validate if we can do it
if (!$table = $structure->getTable($table)) {
return false;
}
if ($table->getAllErrors()) {
return false;
}
// Add the standard PHP header
$result .= XMLDB_PHP_HEADER;
// Add contents
$result .= XMLDB_LINEFEED;
$result .= ' // Define table ' . $table->getName() . ' to be created' . XMLDB_LINEFEED;
$result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
$result .= XMLDB_LINEFEED;
$result .= ' // Adding fields to table ' . $table->getName() . XMLDB_LINEFEED;
// Iterate over each field
foreach ($table->getFields() as $field) {
// The field header, with name
$result .= ' $table->add_field(' . "'" . $field->getName() . "', ";
// The field PHP specs
$result .= $field->getPHP(false);
// The end of the line
$result .= ');' . XMLDB_LINEFEED;
}
// Iterate over each key
if ($keys = $table->getKeys()) {
$result .= XMLDB_LINEFEED;
$result .= ' // Adding keys to table ' . $table->getName() . XMLDB_LINEFEED;
foreach ($keys as $key) {
// The key header, with name
$result .= ' $table->add_key(' . "'" . $key->getName() . "', ";
// The key PHP specs
$result .= $key->getPHP();
// The end of the line
$result .= ');' . XMLDB_LINEFEED;
}
}
// Iterate over each index
if ($indexes = $table->getIndexes()) {
$result .= XMLDB_LINEFEED;
$result .= ' // Adding indexes to table ' . $table->getName() . XMLDB_LINEFEED;
foreach ($indexes as $index) {
// The index header, with name
$result .= ' $table->add_index(' . "'" . $index->getName() . "', ";
// The index PHP specs
$result .= $index->getPHP();
// The end of the line
$result .= ');' . XMLDB_LINEFEED;
}
}
// Launch the proper DDL
$result .= XMLDB_LINEFEED;
$result .= ' // Conditionally launch create table for ' . $table->getName() . XMLDB_LINEFEED;
$result .= ' if (!$dbman->table_exists($table)) {' . XMLDB_LINEFEED;
$result .= ' $dbman->create_table($table);' . XMLDB_LINEFEED;
$result .= ' }' . XMLDB_LINEFEED;
// Add the proper upgrade_xxxx_savepoint call
$result .= $this->upgrade_savepoint_php ($structure);
// Add standard PHP footer
$result .= XMLDB_PHP_FOOTER;
return $result;
}
/**
* This function will generate all the PHP code needed to
* drop one table using XMLDB objects and functions
*
* @param xmldb_structure structure object containing all the info
* @param string table table code to be dropped
* @return string PHP code to be used to drop the table
*/
function drop_table_php($structure, $table) {
$result = '';
// Validate if we can do it
if (!$table = $structure->getTable($table)) {
return false;
}
if ($table->getAllErrors()) {
return false;
}
// Add the standard PHP header
$result .= XMLDB_PHP_HEADER;
// Add contents
$result .= XMLDB_LINEFEED;
$result .= ' // Define table ' . $table->getName() . ' to be dropped' . XMLDB_LINEFEED;
$result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
// Launch the proper DDL
$result .= XMLDB_LINEFEED;
$result .= ' // Conditionally launch drop table for ' . $table->getName() . XMLDB_LINEFEED;
$result .= ' if ($dbman->table_exists($table)) {' . XMLDB_LINEFEED;
$result .= ' $dbman->drop_table($table);' . XMLDB_LINEFEED;
$result .= ' }' . XMLDB_LINEFEED;
// Add the proper upgrade_xxxx_savepoint call
$result .= $this->upgrade_savepoint_php ($structure);
// Add standard PHP footer
$result .= XMLDB_PHP_FOOTER;
return $result;
}
/**
* This function will generate all the PHP code needed to
* rename one table using XMLDB objects and functions
*
* @param xmldb_structure structure object containing all the info
* @param string table table code to be renamed
* @return string PHP code to be used to rename the table
*/
function rename_table_php($structure, $table) {
$result = '';
// Validate if we can do it
if (!$table = $structure->getTable($table)) {
return false;
}
if ($table->getAllErrors()) {
return false;
}
// Add the standard PHP header
$result .= XMLDB_PHP_HEADER;
// Add contents
$result .= XMLDB_LINEFEED;
$result .= ' // Define table ' . $table->getName() . ' to be renamed to NEWNAMEGOESHERE' . XMLDB_LINEFEED;
$result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
// Launch the proper DDL
$result .= XMLDB_LINEFEED;
$result .= ' // Launch rename table for ' . $table->getName() . XMLDB_LINEFEED;
$result .= ' $dbman->rename_table($table, ' . "'NEWNAMEGOESHERE'" . ');' . XMLDB_LINEFEED;
// Add the proper upgrade_xxxx_savepoint call
$result .= $this->upgrade_savepoint_php ($structure);
// Add standard PHP footer
$result .= XMLDB_PHP_FOOTER;
return $result;
}
}