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 = 'fulltext_fld';
97 $mult_btn = __('Yes');
100 require './tbl_alter.php';
103 // this should already be handled by tbl_structure.php
106 // coming from browsing - do something with selected rows
107 $what = 'row_delete';
108 $selected = $rows_to_delete;
114 * Displays the confirmation form if required
116 if (!empty($submit_mult) && !empty($what)) {
119 require_once './libraries/header.inc.php';
120 if (strlen($table)) {
121 require './libraries/tbl_common.php';
122 $url_query .= '&goto=tbl_sql.php&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';
129 require_once './libraries/server_common.inc.php';
130 require_once './libraries/server_links.inc.php';
135 if ($what == 'drop_tbl') {
136 $full_query_views = '';
138 $selected_cnt = count($selected);
140 foreach ($selected AS $idx => $sval) {
143 $full_query .= htmlspecialchars($sval)
147 $full_query .= 'DROP DATABASE '
148 . PMA_backquote(htmlspecialchars($sval))
155 if (!empty($views) && in_array($current, $views)) {
156 $full_query_views .= (empty($full_query_views) ?
'DROP VIEW ' : ', ')
157 . PMA_backquote(htmlspecialchars($current));
159 $full_query .= (empty($full_query) ?
'DROP TABLE ' : ', ')
160 . PMA_backquote(htmlspecialchars($current));
165 $full_query .= 'TRUNCATE ';
166 $full_query .= PMA_backquote(htmlspecialchars($sval))
171 if ($full_query == '') {
172 $full_query .= 'ALTER TABLE '
173 . PMA_backquote(htmlspecialchars($table))
174 . '<br /> DROP PRIMARY KEY,'
175 . '<br /> ADD PRIMARY KEY('
176 . '<br /> '
177 . PMA_backquote(htmlspecialchars($sval))
180 $full_query .= '<br /> '
181 . PMA_backquote(htmlspecialchars($sval))
184 if ($i == $selected_cnt-1) {
185 $full_query = preg_replace('@,$@', ');<br />', $full_query);
190 if ($full_query == '') {
191 $full_query .= 'ALTER TABLE '
192 . PMA_backquote(htmlspecialchars($table));
194 $full_query .= '<br /> DROP '
195 . PMA_backquote(htmlspecialchars($sval))
197 if ($i == $selected_cnt - 1) {
198 $full_query = preg_replace('@,$@', ';<br />', $full_query);
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">
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
>
249 <td
><?php
echo __('From'); ?
></td
><td
><input type
="text" name
="from_prefix" id
="initialPrefix"</td
>
252 <td
><?php
echo __('To'); ?
> </td
><td
><input type
="text" name
="to_prefix" id
="newPrefix"</td
>
256 <fieldset
class="tblFooters">
257 <button type
="submit" name
="mult_btn" value
="<?php echo __('Yes'); ?>" id
="buttonYes"><?
echo __('Submit'); ?
></button
>
261 elseif($what == 'add_prefix_tbl'){ ?
>
262 <fieldset
class = "input">
263 <legend
><?php
echo __('Add table prefix') ?
>:</legend
>
266 <td
><?php
echo __('Add prefix'); ?
></td
> <td
><input type
="text" name
="add_prefix" id
="txtPrefix"</td
>
270 <fieldset
class="tblFooters">
271 <button type
="submit" name
="mult_btn" value
="<?php echo __('Yes'); ?>" id
="buttonYes"><?
echo __('Submit'); ?
></button
>
276 <fieldset
class="confirmation">
277 <legend
><?php
echo ($what == 'drop_db' ?
__('You are about to DESTROY a complete database!') . ' ' : '') . __('Do you really want to '); ?
>:</legend
>
278 <tt
><?php
echo $full_query; ?
></tt
>
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" />
286 require './libraries/footer.inc.php';
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';
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) . ';');
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'] . ', ';
319 PMA_DBI_free_result($result);
322 $rebuild_database_list = false;
324 for ($i = 0; $i < $selected_cnt; $i++
) {
325 switch ($query_type) {
327 $a_query = $selected[$i];
332 PMA_relationsCleanupDatabase($selected[$i]);
333 $a_query = 'DROP DATABASE '
334 . PMA_backquote($selected[$i]);
337 $rebuild_database_list = true;
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);
347 $sql_query .= (empty($sql_query) ?
'DROP TABLE ' : ', ')
348 . PMA_backquote($current);
354 $sql_query .= (empty($sql_query) ?
'CHECK TABLE ' : ', ')
355 . PMA_backquote($selected[$i]);
360 $sql_query .= (empty($sql_query) ?
'OPTIMIZE TABLE ' : ', ')
361 . PMA_backquote($selected[$i]);
366 $sql_query .= (empty($sql_query) ?
'ANALYZE TABLE ' : ', ')
367 . PMA_backquote($selected[$i]);
372 $sql_query .= (empty($sql_query) ?
'REPAIR TABLE ' : ', ')
373 . PMA_backquote($selected[$i]);
378 $a_query = 'TRUNCATE ';
379 $a_query .= PMA_backquote($selected[$i]);
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) ?
';' : '');
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) ?
');' : '');
397 $sql_query .= (empty($sql_query) ?
'ALTER TABLE ' . PMA_backquote($table) . ' ADD INDEX( ' : ', ')
398 . PMA_backquote($selected[$i])
399 . (($i == $selected_cnt-1) ?
');' : '');
403 $sql_query .= (empty($sql_query) ?
'ALTER TABLE ' . PMA_backquote($table) . ' ADD UNIQUE( ' : ', ')
404 . PMA_backquote($selected[$i])
405 . (($i == $selected_cnt-1) ?
');' : '');
409 $sql_query .= (empty($sql_query) ?
'ALTER TABLE ' . PMA_backquote($table) . ' ADD FULLTEXT( ' : ', ')
410 . PMA_backquote($selected[$i])
411 . (($i == $selected_cnt-1) ?
');' : '');
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
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
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
436 // All "DROP TABLE", "DROP FIELD", "OPTIMIZE TABLE" and "REPAIR TABLE"
437 // statements will be run at once below
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);
447 if ($query_type == 'drop_tbl') {
448 if (!empty($sql_query)) {
450 } elseif (!empty($sql_query_views)) {
451 $sql_query = $sql_query_views . ';';
452 unset($sql_query_views);
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);
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();
477 $message = PMA_Message
::success(__('No change'));