2 /* vim: set expandtab sw=4 ts=4 sts=4: */
7 if (! defined('PHPMYADMIN')) {
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;
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';
30 $selected = $selected_tbl;
31 switch ($submit_mult) {
32 case 'add_prefix_tbl':
33 case 'replace_prefix_tbl':
34 case 'copy_tbl_change_prefix':
44 $query_type = $submit_mult;
46 $mult_btn = __('Yes');
50 require('db_export.php');
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) {
63 // Gets table primary key
64 PMA_DBI_select_db($db);
65 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
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'] . ', ';
73 PMA_DBI_free_result($result);
74 if (empty($primary)) {
75 // no primary key, so we can safely create new
77 $query_type = 'primary_fld';
78 $mult_btn = __('Yes');
80 // primary key exists, so lets as user
81 $what = 'primary_fld';
86 $query_type = 'index_fld';
87 $mult_btn = __('Yes');
91 $query_type = 'unique_fld';
92 $mult_btn = __('Yes');
96 $query_type = 'spatial_fld';
97 $mult_btn = __('Yes');
101 $query_type = 'fulltext_fld';
102 $mult_btn = __('Yes');
105 require './tbl_alter.php';
108 // this should already be handled by tbl_structure.php
111 // coming from browsing - do something with selected rows
112 $what = 'row_delete';
113 $selected = $rows_to_delete;
119 * Displays the confirmation form if required
121 if (!empty($submit_mult) && !empty($what)) {
124 require_once './libraries/header.inc.php';
125 if (strlen($table)) {
126 require './libraries/tbl_common.php';
127 $url_query .= '&goto=tbl_sql.php&back=tbl_sql.php';
128 require './libraries/tbl_info.inc.php';
129 require_once './libraries/tbl_links.inc.php';
130 } elseif (strlen($db)) {
131 require './libraries/db_common.inc.php';
132 require './libraries/db_info.inc.php';
134 require_once './libraries/server_common.inc.php';
135 require_once './libraries/server_links.inc.php';
140 if ($what == 'drop_tbl') {
141 $full_query_views = '';
143 $selected_cnt = count($selected);
145 foreach ($selected AS $idx => $sval) {
148 $full_query .= htmlspecialchars($sval)
152 $full_query .= 'DROP DATABASE '
153 . PMA_backquote(htmlspecialchars($sval))
160 if (!empty($views) && in_array($current, $views)) {
161 $full_query_views .= (empty($full_query_views) ?
'DROP VIEW ' : ', ')
162 . PMA_backquote(htmlspecialchars($current));
164 $full_query .= (empty($full_query) ?
'DROP TABLE ' : ', ')
165 . PMA_backquote(htmlspecialchars($current));
170 $full_query .= 'TRUNCATE ';
171 $full_query .= PMA_backquote(htmlspecialchars($sval))
176 if ($full_query == '') {
177 $full_query .= 'ALTER TABLE '
178 . PMA_backquote(htmlspecialchars($table))
179 . '<br /> DROP PRIMARY KEY,'
180 . '<br /> ADD PRIMARY KEY('
181 . '<br /> '
182 . PMA_backquote(htmlspecialchars($sval))
185 $full_query .= '<br /> '
186 . PMA_backquote(htmlspecialchars($sval))
189 if ($i == $selected_cnt-1) {
190 $full_query = preg_replace('@,$@', ');<br />', $full_query);
195 if ($full_query == '') {
196 $full_query .= 'ALTER TABLE '
197 . PMA_backquote(htmlspecialchars($table));
199 $full_query .= '<br /> DROP '
200 . PMA_backquote(htmlspecialchars($sval))
202 if ($i == $selected_cnt - 1) {
203 $full_query = preg_replace('@,$@', ';<br />', $full_query);
209 if ($what == 'drop_tbl') {
210 if (!empty($full_query)) {
211 $full_query .= ';<br />' . "\n";
213 if (!empty($full_query_views)) {
214 $full_query .= $full_query_views . ';<br />' . "\n";
216 unset($full_query_views);
219 // Displays the confirmation form
220 $_url_params = array(
221 'query_type' => $what,
222 'reload' => (! empty($reload) ?
1 : 0),
224 if (strpos(' ' . $action, 'db_') == 1) {
225 $_url_params['db']= $db;
226 } elseif (strpos(' ' . $action, 'tbl_') == 1 ||
$what == 'row_delete') {
227 $_url_params['db']= $db;
228 $_url_params['table']= $table;
230 foreach ($selected as $idx => $sval) {
231 $_url_params['selected'][] = $sval;
233 if ($what == 'drop_tbl' && !empty($views)) {
234 foreach ($views as $current) {
235 $_url_params['views'][] = $current;
238 if ($what == 'row_delete') {
239 $_url_params['original_sql_query'] = $original_sql_query;
240 if (! empty($original_url_query)) {
241 $_url_params['original_url_query'] = $original_url_query;
245 <form action
="<?php echo $action; ?>" method
="post">
247 echo PMA_generate_common_hidden_inputs($_url_params);
249 <?php
if ($what == 'replace_prefix_tbl' ||
$what == 'copy_tbl_change_prefix') { ?
>
250 <fieldset
class = "input">
251 <legend
><?php
echo ($what == 'replace_prefix_tbl' ?
__('Replace table prefix') : __('Copy table with prefix')) ?
>:</legend
>
254 <td
><?php
echo __('From'); ?
></td
><td
><input type
="text" name
="from_prefix" id
="initialPrefix"</td
>
257 <td
><?php
echo __('To'); ?
> </td
><td
><input type
="text" name
="to_prefix" id
="newPrefix"</td
>
261 <fieldset
class="tblFooters">
262 <button type
="submit" name
="mult_btn" value
="<?php echo __('Yes'); ?>" id
="buttonYes"><?php
echo __('Submit'); ?
></button
>
266 elseif ($what == 'add_prefix_tbl') { ?
>
267 <fieldset
class = "input">
268 <legend
><?php
echo __('Add table prefix') ?
>:</legend
>
271 <td
><?php
echo __('Add prefix'); ?
></td
> <td
><input type
="text" name
="add_prefix" id
="txtPrefix"</td
>
275 <fieldset
class="tblFooters">
276 <button type
="submit" name
="mult_btn" value
="<?php echo __('Yes'); ?>" id
="buttonYes"><?php
echo __('Submit'); ?
></button
>
281 <fieldset
class="confirmation">
282 <legend
><?php
echo ($what == 'drop_db' ?
__('You are about to DESTROY a complete database!') . ' ' : '') . __('Do you really want to '); ?
>:</legend
>
283 <tt
><?php
echo $full_query; ?
></tt
>
285 <fieldset
class="tblFooters">
286 <input type
="submit" name
="mult_btn" value
="<?php echo __('Yes'); ?>" id
="buttonYes" />
287 <input type
="submit" name
="mult_btn" value
="<?php echo __('No'); ?>" id
="buttonNo" />
291 require './libraries/footer.inc.php';
297 * Executes the query - dropping rows, columns/fields, tables or dbs
299 elseif ($mult_btn == __('Yes')) {
301 if ($query_type == 'drop_db' ||
$query_type == 'drop_tbl' ||
$query_type == 'drop_fld') {
302 require_once './libraries/relation_cleanup.lib.php';
306 if ($query_type == 'drop_tbl') {
307 $sql_query_views = '';
309 $selected_cnt = count($selected);
310 $run_parts = false; // whether to run query after each pass
311 $use_sql = false; // whether to include sql.php at the end (to display results)
313 if ($query_type == 'primary_fld') {
314 // Gets table primary key
315 PMA_DBI_select_db($db);
316 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
318 while ($row = PMA_DBI_fetch_assoc($result)) {
319 // Backups the list of primary keys
320 if ($row['Key_name'] == 'PRIMARY') {
321 $primary .= $row['Column_name'] . ', ';
324 PMA_DBI_free_result($result);
327 $rebuild_database_list = false;
329 for ($i = 0; $i < $selected_cnt; $i++
) {
330 switch ($query_type) {
332 $a_query = $selected[$i];
337 PMA_relationsCleanupDatabase($selected[$i]);
338 $a_query = 'DROP DATABASE '
339 . PMA_backquote($selected[$i]);
342 $rebuild_database_list = true;
346 PMA_relationsCleanupTable($db, $selected[$i]);
347 $current = $selected[$i];
348 if (!empty($views) && in_array($current, $views)) {
349 $sql_query_views .= (empty($sql_query_views) ?
'DROP VIEW ' : ', ')
350 . PMA_backquote($current);
352 $sql_query .= (empty($sql_query) ?
'DROP TABLE ' : ', ')
353 . PMA_backquote($current);
359 $sql_query .= (empty($sql_query) ?
'CHECK TABLE ' : ', ')
360 . PMA_backquote($selected[$i]);
365 $sql_query .= (empty($sql_query) ?
'OPTIMIZE TABLE ' : ', ')
366 . PMA_backquote($selected[$i]);
371 $sql_query .= (empty($sql_query) ?
'ANALYZE TABLE ' : ', ')
372 . PMA_backquote($selected[$i]);
377 $sql_query .= (empty($sql_query) ?
'REPAIR TABLE ' : ', ')
378 . PMA_backquote($selected[$i]);
383 $a_query = 'TRUNCATE ';
384 $a_query .= PMA_backquote($selected[$i]);
389 PMA_relationsCleanupColumn($db, $table, $selected[$i]);
390 $sql_query .= (empty($sql_query) ?
'ALTER TABLE ' . PMA_backquote($table) : ',')
391 . ' DROP ' . PMA_backquote($selected[$i])
392 . (($i == $selected_cnt-1) ?
';' : '');
396 $sql_query .= (empty($sql_query) ?
'ALTER TABLE ' . PMA_backquote($table) . (empty($primary) ?
'' : ' DROP PRIMARY KEY,') . ' ADD PRIMARY KEY( ' : ', ')
397 . PMA_backquote($selected[$i])
398 . (($i == $selected_cnt-1) ?
');' : '');
402 $sql_query .= (empty($sql_query) ?
'ALTER TABLE ' . PMA_backquote($table) . ' ADD INDEX( ' : ', ')
403 . PMA_backquote($selected[$i])
404 . (($i == $selected_cnt-1) ?
');' : '');
408 $sql_query .= (empty($sql_query) ?
'ALTER TABLE ' . PMA_backquote($table) . ' ADD UNIQUE( ' : ', ')
409 . PMA_backquote($selected[$i])
410 . (($i == $selected_cnt-1) ?
');' : '');
414 $sql_query .= (empty($sql_query) ?
'ALTER TABLE ' . PMA_backquote($table) . ' ADD SPATIAL( ' : ', ')
415 . PMA_backquote($selected[$i])
416 . (($i == $selected_cnt-1) ?
');' : '');
420 $sql_query .= (empty($sql_query) ?
'ALTER TABLE ' . PMA_backquote($table) . ' ADD FULLTEXT( ' : ', ')
421 . PMA_backquote($selected[$i])
422 . (($i == $selected_cnt-1) ?
');' : '');
425 case 'add_prefix_tbl':
426 $newtablename = $add_prefix . $selected[$i];
427 $a_query = 'ALTER TABLE ' . PMA_backquote($selected[$i]) . ' RENAME ' . PMA_backquote($newtablename) ; // ADD PREFIX TO TABLE NAME
431 case 'replace_prefix_tbl':
432 $current = $selected[$i];
433 $newtablename = preg_replace("/^" . $from_prefix . "/" , $to_prefix , $current);
434 $a_query = 'ALTER TABLE ' . PMA_backquote($selected[$i]) . ' RENAME ' . PMA_backquote($newtablename) ; // CHANGE PREFIX PATTERN
438 case 'copy_tbl_change_prefix':
439 $current = $selected[$i];
440 $newtablename = preg_replace("/^" . $from_prefix . "/" , $to_prefix , $current);
441 $a_query = 'CREATE TABLE ' . PMA_backquote($newtablename) . ' SELECT * FROM ' . PMA_backquote($selected[$i]) ; // COPY TABLE AND CHANGE PREFIX PATTERN
447 // All "DROP TABLE", "DROP FIELD", "OPTIMIZE TABLE" and "REPAIR TABLE"
448 // statements will be run at once below
450 $sql_query .= $a_query . ';' . "\n";
451 if ($query_type != 'drop_db') {
452 PMA_DBI_select_db($db);
454 $result = PMA_DBI_query($a_query);
458 if ($query_type == 'drop_tbl') {
459 if (!empty($sql_query)) {
461 } elseif (!empty($sql_query_views)) {
462 $sql_query = $sql_query_views . ';';
463 unset($sql_query_views);
469 } elseif (!$run_parts) {
470 PMA_DBI_select_db($db);
471 $result = PMA_DBI_try_query($sql_query);
472 if ($result && !empty($sql_query_views)) {
473 $sql_query .= ' ' . $sql_query_views . ';';
474 $result = PMA_DBI_try_query($sql_query_views);
475 unset($sql_query_views);
479 $message = PMA_Message
::error(PMA_DBI_getError());
482 if ($rebuild_database_list) {
483 // avoid a problem with the database list navigator
484 // when dropping a db from server_databases
485 $GLOBALS['pma']->databases
->build();
488 $message = PMA_Message
::success(__('No change'));