Merge branch 'QA_3_4'
[phpmyadmin-regexreplace.git] / libraries / mult_submits.inc.php
blob7c8f27cdc9197390be61bf5e0c5f6ceac7e892a7
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @package phpMyAdmin
6 */
7 if (! defined('PHPMYADMIN')) {
8 exit;
11 /**
12 * Prepares the work and runs some other scripts if required
14 if (! empty($submit_mult)
15 && $submit_mult != __('With selected:')
16 && (! empty($selected_db)
17 || ! empty($selected_tbl)
18 || ! empty($selected_fld)
19 || ! empty($rows_to_delete))) {
20 define('PMA_SUBMIT_MULT', 1);
21 if (isset($selected_db) && !empty($selected_db)) {
22 // coming from server database view - do something with selected databases
23 $selected = $selected_db;
24 $what = 'drop_db';
25 } elseif (isset($selected_tbl) && !empty($selected_tbl)) {
26 // coming from database structure view - do something with selected tables
27 if ($submit_mult == 'print') {
28 require './tbl_printview.php';
29 } else {
30 $selected = $selected_tbl;
31 switch ($submit_mult) {
32 case 'add_prefix_tbl':
33 case 'replace_prefix_tbl':
34 case 'copy_tbl_change_prefix':
35 case 'drop_db':
36 case 'drop_tbl':
37 case 'empty_tbl':
38 $what = $submit_mult;
39 break;
40 case 'check_tbl':
41 case 'optimize_tbl':
42 case 'repair_tbl':
43 case 'analyze_tbl':
44 $query_type = $submit_mult;
45 unset($submit_mult);
46 $mult_btn = __('Yes');
47 break;
48 case 'export':
49 unset($submit_mult);
50 require('db_export.php');
51 exit;
52 break;
53 } // end switch
55 } elseif (isset($selected_fld) && !empty($selected_fld)) {
56 // coming from table structure view - do something with selected columns/fileds
57 $selected = $selected_fld;
58 switch ($submit_mult) {
59 case 'drop':
60 $what = 'drop_fld';
61 break;
62 case 'primary':
63 // Gets table primary key
64 PMA_DBI_select_db($db);
65 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
66 $primary = '';
67 while ($row = PMA_DBI_fetch_assoc($result)) {
68 // Backups the list of primary keys
69 if ($row['Key_name'] == 'PRIMARY') {
70 $primary .= $row['Column_name'] . ', ';
72 } // end while
73 PMA_DBI_free_result($result);
74 if (empty($primary)) {
75 // no primary key, so we can safely create new
76 unset($submit_mult);
77 $query_type = 'primary_fld';
78 $mult_btn = __('Yes');
79 } else {
80 // primary key exists, so lets as user
81 $what = 'primary_fld';
83 break;
84 case 'index':
85 unset($submit_mult);
86 $query_type = 'index_fld';
87 $mult_btn = __('Yes');
88 break;
89 case 'unique':
90 unset($submit_mult);
91 $query_type = 'unique_fld';
92 $mult_btn = __('Yes');
93 break;
94 case 'ftext':
95 unset($submit_mult);
96 $query_type = 'fulltext_fld';
97 $mult_btn = __('Yes');
98 break;
99 case 'change':
100 require './tbl_alter.php';
101 break;
102 case 'browse':
103 // this should already be handled by tbl_structure.php
105 } else {
106 // coming from browsing - do something with selected rows
107 $what = 'row_delete';
108 $selected = $rows_to_delete;
110 } // end if
114 * Displays the confirmation form if required
116 if (!empty($submit_mult) && !empty($what)) {
117 unset($message);
119 require_once './libraries/header.inc.php';
120 if (strlen($table)) {
121 require './libraries/tbl_common.php';
122 $url_query .= '&amp;goto=tbl_sql.php&amp;back=tbl_sql.php';
123 require './libraries/tbl_info.inc.php';
124 require_once './libraries/tbl_links.inc.php';
125 } elseif (strlen($db)) {
126 require './libraries/db_common.inc.php';
127 require './libraries/db_info.inc.php';
128 } else {
129 require_once './libraries/server_common.inc.php';
130 require_once './libraries/server_links.inc.php';
133 // Builds the query
134 $full_query = '';
135 if ($what == 'drop_tbl') {
136 $full_query_views = '';
138 $selected_cnt = count($selected);
139 $i = 0;
140 foreach ($selected AS $idx => $sval) {
141 switch ($what) {
142 case 'row_delete':
143 $full_query .= htmlspecialchars($sval)
144 . ';<br />';
145 break;
146 case 'drop_db':
147 $full_query .= 'DROP DATABASE '
148 . PMA_backquote(htmlspecialchars($sval))
149 . ';<br />';
150 $reload = 1;
151 break;
153 case 'drop_tbl':
154 $current = $sval;
155 if (!empty($views) && in_array($current, $views)) {
156 $full_query_views .= (empty($full_query_views) ? 'DROP VIEW ' : ', ')
157 . PMA_backquote(htmlspecialchars($current));
158 } else {
159 $full_query .= (empty($full_query) ? 'DROP TABLE ' : ', ')
160 . PMA_backquote(htmlspecialchars($current));
162 break;
164 case 'empty_tbl':
165 $full_query .= 'TRUNCATE ';
166 $full_query .= PMA_backquote(htmlspecialchars($sval))
167 . ';<br />';
168 break;
170 case 'primary_fld':
171 if ($full_query == '') {
172 $full_query .= 'ALTER TABLE '
173 . PMA_backquote(htmlspecialchars($table))
174 . '<br />&nbsp;&nbsp;DROP PRIMARY KEY,'
175 . '<br />&nbsp;&nbsp; ADD PRIMARY KEY('
176 . '<br />&nbsp;&nbsp;&nbsp;&nbsp; '
177 . PMA_backquote(htmlspecialchars($sval))
178 . ',';
179 } else {
180 $full_query .= '<br />&nbsp;&nbsp;&nbsp;&nbsp; '
181 . PMA_backquote(htmlspecialchars($sval))
182 . ',';
184 if ($i == $selected_cnt-1) {
185 $full_query = preg_replace('@,$@', ');<br />', $full_query);
187 break;
189 case 'drop_fld':
190 if ($full_query == '') {
191 $full_query .= 'ALTER TABLE '
192 . PMA_backquote(htmlspecialchars($table));
194 $full_query .= '<br />&nbsp;&nbsp;DROP '
195 . PMA_backquote(htmlspecialchars($sval))
196 . ',';
197 if ($i == $selected_cnt - 1) {
198 $full_query = preg_replace('@,$@', ';<br />', $full_query);
200 break;
201 } // end switch
202 $i++;
204 if ($what == 'drop_tbl') {
205 if (!empty($full_query)) {
206 $full_query .= ';<br />' . "\n";
208 if (!empty($full_query_views)) {
209 $full_query .= $full_query_views . ';<br />' . "\n";
211 unset($full_query_views);
214 // Displays the confirmation form
215 $_url_params = array(
216 'query_type' => $what,
217 'reload' => (! empty($reload) ? 1 : 0),
219 if (strpos(' ' . $action, 'db_') == 1) {
220 $_url_params['db']= $db;
221 } elseif (strpos(' ' . $action, 'tbl_') == 1 || $what == 'row_delete') {
222 $_url_params['db']= $db;
223 $_url_params['table']= $table;
225 foreach ($selected as $idx => $sval) {
226 $_url_params['selected'][] = $sval;
228 if ($what == 'drop_tbl' && !empty($views)) {
229 foreach ($views as $current) {
230 $_url_params['views'][] = $current;
233 if ($what == 'row_delete') {
234 $_url_params['original_sql_query'] = $original_sql_query;
235 if (! empty($original_url_query)) {
236 $_url_params['original_url_query'] = $original_url_query;
240 <form action="<?php echo $action; ?>" method="post">
241 <?php
242 echo PMA_generate_common_hidden_inputs($_url_params);
244 <?php if ($what == 'replace_prefix_tbl' || $what == 'copy_tbl_change_prefix'){ ?>
245 <fieldset class = "input">
246 <legend><?php echo ($what == 'replace_prefix_tbl' ? __('Replace table prefix') : __('Copy table with prefix')) ?>:</legend>
247 <table>
248 <tr>
249 <td><?php echo __('From'); ?></td><td><input type="text" name="from_prefix" id="initialPrefix"</td>
250 </tr>
251 <tr>
252 <td><?php echo __('To'); ?> </td><td><input type="text" name="to_prefix" id="newPrefix"</td>
253 </tr>
254 </table>
255 </fieldset>
256 <fieldset class="tblFooters">
257 <button type="submit" name="mult_btn" value="<?php echo __('Yes'); ?>" id="buttonYes"><? echo __('Submit'); ?></button>
258 </fieldset>
259 <?php
261 elseif($what == 'add_prefix_tbl'){ ?>
262 <fieldset class = "input">
263 <legend><?php echo __('Add table prefix') ?>:</legend>
264 <table>
265 <tr>
266 <td><?php echo __('Add prefix'); ?></td> <td><input type="text" name="add_prefix" id="txtPrefix"</td>
267 </tr>
268 </table>
269 </fieldset>
270 <fieldset class="tblFooters">
271 <button type="submit" name="mult_btn" value="<?php echo __('Yes'); ?>" id="buttonYes"><? echo __('Submit'); ?></button>
272 </fieldset>
273 <?php
275 else { ?>
276 <fieldset class="confirmation">
277 <legend><?php echo ($what == 'drop_db' ? __('You are about to DESTROY a complete database!') . '&nbsp;' : '') . __('Do you really want to '); ?>:</legend>
278 <tt><?php echo $full_query; ?></tt>
279 </fieldset>
280 <fieldset class="tblFooters">
281 <input type="submit" name="mult_btn" value="<?php echo __('Yes'); ?>" id="buttonYes" />
282 <input type="submit" name="mult_btn" value="<?php echo __('No'); ?>" id="buttonNo" />
283 </fieldset>
284 <?php
286 require './libraries/footer.inc.php';
288 } // end if
292 * Executes the query - dropping rows, columns/fields, tables or dbs
294 elseif ($mult_btn == __('Yes')) {
296 if ($query_type == 'drop_db' || $query_type == 'drop_tbl' || $query_type == 'drop_fld') {
297 require_once './libraries/relation_cleanup.lib.php';
300 $sql_query = '';
301 if ($query_type == 'drop_tbl') {
302 $sql_query_views = '';
304 $selected_cnt = count($selected);
305 $run_parts = FALSE; // whether to run query after each pass
306 $use_sql = FALSE; // whether to include sql.php at the end (to display results)
308 if ($query_type == 'primary_fld') {
309 // Gets table primary key
310 PMA_DBI_select_db($db);
311 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
312 $primary = '';
313 while ($row = PMA_DBI_fetch_assoc($result)) {
314 // Backups the list of primary keys
315 if ($row['Key_name'] == 'PRIMARY') {
316 $primary .= $row['Column_name'] . ', ';
318 } // end while
319 PMA_DBI_free_result($result);
322 $rebuild_database_list = false;
324 for ($i = 0; $i < $selected_cnt; $i++) {
325 switch ($query_type) {
326 case 'row_delete':
327 $a_query = $selected[$i];
328 $run_parts = TRUE;
329 break;
331 case 'drop_db':
332 PMA_relationsCleanupDatabase($selected[$i]);
333 $a_query = 'DROP DATABASE '
334 . PMA_backquote($selected[$i]);
335 $reload = 1;
336 $run_parts = TRUE;
337 $rebuild_database_list = true;
338 break;
340 case 'drop_tbl':
341 PMA_relationsCleanupTable($db, $selected[$i]);
342 $current = $selected[$i];
343 if (!empty($views) && in_array($current, $views)) {
344 $sql_query_views .= (empty($sql_query_views) ? 'DROP VIEW ' : ', ')
345 . PMA_backquote($current);
346 } else {
347 $sql_query .= (empty($sql_query) ? 'DROP TABLE ' : ', ')
348 . PMA_backquote($current);
350 $reload = 1;
351 break;
353 case 'check_tbl':
354 $sql_query .= (empty($sql_query) ? 'CHECK TABLE ' : ', ')
355 . PMA_backquote($selected[$i]);
356 $use_sql = TRUE;
357 break;
359 case 'optimize_tbl':
360 $sql_query .= (empty($sql_query) ? 'OPTIMIZE TABLE ' : ', ')
361 . PMA_backquote($selected[$i]);
362 $use_sql = TRUE;
363 break;
365 case 'analyze_tbl':
366 $sql_query .= (empty($sql_query) ? 'ANALYZE TABLE ' : ', ')
367 . PMA_backquote($selected[$i]);
368 $use_sql = TRUE;
369 break;
371 case 'repair_tbl':
372 $sql_query .= (empty($sql_query) ? 'REPAIR TABLE ' : ', ')
373 . PMA_backquote($selected[$i]);
374 $use_sql = TRUE;
375 break;
377 case 'empty_tbl':
378 $a_query = 'TRUNCATE ';
379 $a_query .= PMA_backquote($selected[$i]);
380 $run_parts = TRUE;
381 break;
383 case 'drop_fld':
384 PMA_relationsCleanupColumn($db, $table, $selected[$i]);
385 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) : ',')
386 . ' DROP ' . PMA_backquote($selected[$i])
387 . (($i == $selected_cnt-1) ? ';' : '');
388 break;
390 case 'primary_fld':
391 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . (empty($primary) ? '' : ' DROP PRIMARY KEY,') . ' ADD PRIMARY KEY( ' : ', ')
392 . PMA_backquote($selected[$i])
393 . (($i == $selected_cnt-1) ? ');' : '');
394 break;
396 case 'index_fld':
397 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ' ADD INDEX( ' : ', ')
398 . PMA_backquote($selected[$i])
399 . (($i == $selected_cnt-1) ? ');' : '');
400 break;
402 case 'unique_fld':
403 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ' ADD UNIQUE( ' : ', ')
404 . PMA_backquote($selected[$i])
405 . (($i == $selected_cnt-1) ? ');' : '');
406 break;
408 case 'fulltext_fld':
409 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ' ADD FULLTEXT( ' : ', ')
410 . PMA_backquote($selected[$i])
411 . (($i == $selected_cnt-1) ? ');' : '');
412 break;
414 case 'add_prefix_tbl':
415 $newtablename = $add_prefix . $selected[$i];
416 $a_query = 'ALTER TABLE ' . PMA_backquote($selected[$i]) . ' RENAME ' . PMA_backquote($newtablename) ; // ADD PREFIX TO TABLE NAME
417 $run_parts = true;
418 break;
420 case 'replace_prefix_tbl':
421 $current = $selected[$i];
422 $newtablename = preg_replace("/^" . $from_prefix . "/" , $to_prefix , $current);
423 $a_query = 'ALTER TABLE ' . PMA_backquote($selected[$i]) . ' RENAME ' . PMA_backquote($newtablename) ; // CHANGE PREFIX PATTERN
424 $run_parts = true;
425 break;
427 case 'copy_tbl_change_prefix':
428 $current = $selected[$i];
429 $newtablename = preg_replace("/^" . $from_prefix . "/" , $to_prefix , $current);
430 $a_query = 'CREATE TABLE ' . PMA_backquote($newtablename) . ' SELECT * FROM ' . PMA_backquote($selected[$i]) ; // COPY TABLE AND CHANGE PREFIX PATTERN
431 $run_parts = true;
432 break;
434 } // end switch
436 // All "DROP TABLE", "DROP FIELD", "OPTIMIZE TABLE" and "REPAIR TABLE"
437 // statements will be run at once below
438 if ($run_parts) {
439 $sql_query .= $a_query . ';' . "\n";
440 if ($query_type != 'drop_db') {
441 PMA_DBI_select_db($db);
443 $result = PMA_DBI_query($a_query);
444 } // end if
445 } // end for
447 if ($query_type == 'drop_tbl') {
448 if (!empty($sql_query)) {
449 $sql_query .= ';';
450 } elseif (!empty($sql_query_views)) {
451 $sql_query = $sql_query_views . ';';
452 unset($sql_query_views);
456 if ($use_sql) {
457 require './sql.php';
458 } elseif (!$run_parts) {
459 PMA_DBI_select_db($db);
460 $result = PMA_DBI_try_query($sql_query);
461 if ($result && !empty($sql_query_views)) {
462 $sql_query .= ' ' . $sql_query_views . ';';
463 $result = PMA_DBI_try_query($sql_query_views);
464 unset($sql_query_views);
467 if (! $result) {
468 $message = PMA_Message::error(PMA_DBI_getError());
471 if ($rebuild_database_list) {
472 // avoid a problem with the database list navigator
473 // when dropping a db from server_databases
474 $GLOBALS['pma']->databases->build();
476 } else {
477 $message = PMA_Message::success(__('No change'));