Merge branch 'QA_3_3'
[phpmyadmin/dkf.git] / libraries / mult_submits.inc.php
blob81db844f3d3e52efc6619bbcf6113bd3aa666909
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @version $Id$
6 * @package phpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
13 * Prepares the work and runs some other scripts if required
15 if (! empty($submit_mult)
16 && $submit_mult != __('With selected:')
17 && (! empty($selected_db)
18 || ! empty($selected_tbl)
19 || ! empty($selected_fld)
20 || ! empty($rows_to_delete))) {
21 define('PMA_SUBMIT_MULT', 1);
22 if (isset($selected_db) && !empty($selected_db)) {
23 // coming from server database view - do something with selected databases
24 $selected = $selected_db;
25 $what = 'drop_db';
26 } elseif (isset($selected_tbl) && !empty($selected_tbl)) {
27 // coming from database structure view - do something with selected tables
28 if ($submit_mult == __('Print view')) {
29 require './tbl_printview.php';
30 } else {
31 $selected = $selected_tbl;
32 switch ($submit_mult) {
33 case 'drop_db':
34 $what = 'drop_db';
35 break;
36 case __('Drop'):
37 $what = 'drop_tbl';
38 break;
39 case __('Empty'):
40 $what = 'empty_tbl';
41 break;
42 case __('Check table'):
43 unset($submit_mult);
44 $query_type = 'check_tbl';
45 $mult_btn = __('Yes');
46 break;
47 case __('Optimize table'):
48 unset($submit_mult);
49 $query_type = 'optimize_tbl';
50 $mult_btn = __('Yes');
51 break;
52 case __('Repair table'):
53 unset($submit_mult);
54 $query_type = 'repair_tbl';
55 $mult_btn = __('Yes');
56 break;
57 case __('Analyze table'):
58 unset($submit_mult);
59 $query_type = 'analyze_tbl';
60 $mult_btn = __('Yes');
61 break;
62 case __('Export'):
63 unset($submit_mult);
64 require('db_export.php');
65 exit;
66 break;
67 } // end switch
69 } elseif (isset($selected_fld) && !empty($selected_fld)) {
70 // coming from table structure view - do something with selected columns/fileds
71 $selected = $selected_fld;
72 switch ($submit_mult) {
73 case __('Drop'):
74 $what = 'drop_fld';
75 break;
76 case __('Primary'):
77 // Gets table primary key
78 PMA_DBI_select_db($db);
79 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
80 $primary = '';
81 while ($row = PMA_DBI_fetch_assoc($result)) {
82 // Backups the list of primary keys
83 if ($row['Key_name'] == 'PRIMARY') {
84 $primary .= $row['Column_name'] . ', ';
86 } // end while
87 PMA_DBI_free_result($result);
88 if (empty($primary)) {
89 // no primary key, so we can safely create new
90 unset($submit_mult);
91 $query_type = 'primary_fld';
92 $mult_btn = __('Yes');
93 } else {
94 // primary key exists, so lets as user
95 $what = 'primary_fld';
97 break;
98 case __('Index'):
99 unset($submit_mult);
100 $query_type = 'index_fld';
101 $mult_btn = __('Yes');
102 break;
103 case __('Unique'):
104 unset($submit_mult);
105 $query_type = 'unique_fld';
106 $mult_btn = __('Yes');
107 break;
108 case __('Fulltext'):
109 unset($submit_mult);
110 $query_type = 'fulltext_fld';
111 $mult_btn = __('Yes');
112 break;
113 case __('Change'):
114 require './tbl_alter.php';
115 break;
116 case __('Browse'):
117 // this should already be handled by tbl_structure.php
119 } else {
120 // coming from borwsing - do something with selected rows
121 $what = 'row_delete';
122 $selected = $rows_to_delete;
124 } // end if
128 * Displays the confirmation form if required
130 if (!empty($submit_mult) && !empty($what)) {
131 $GLOBALS['js_include'][] = 'functions.js';
132 unset($message);
134 require_once './libraries/header.inc.php';
135 if (strlen($table)) {
136 require './libraries/tbl_common.php';
137 $url_query .= '&amp;goto=tbl_sql.php&amp;back=tbl_sql.php';
138 require './libraries/tbl_info.inc.php';
139 require_once './libraries/tbl_links.inc.php';
140 } elseif (strlen($db)) {
141 require './libraries/db_common.inc.php';
142 require './libraries/db_info.inc.php';
143 } else {
144 require_once './libraries/server_common.inc.php';
145 require_once './libraries/server_links.inc.php';
148 // Builds the query
149 $full_query = '';
150 if ($what == 'drop_tbl') {
151 $full_query_views = '';
153 $selected_cnt = count($selected);
154 $i = 0;
155 foreach ($selected AS $idx => $sval) {
156 switch ($what) {
157 case 'row_delete':
158 $full_query .= htmlspecialchars($sval)
159 . ';<br />';
160 break;
161 case 'drop_db':
162 $full_query .= 'DROP DATABASE '
163 . PMA_backquote(htmlspecialchars($sval))
164 . ';<br />';
165 $reload = 1;
166 break;
168 case 'drop_tbl':
169 $current = $sval;
170 if (!empty($views) && in_array($current, $views)) {
171 $full_query_views .= (empty($full_query_views) ? 'DROP VIEW ' : ', ')
172 . PMA_backquote(htmlspecialchars($current));
173 } else {
174 $full_query .= (empty($full_query) ? 'DROP TABLE ' : ', ')
175 . PMA_backquote(htmlspecialchars($current));
177 break;
179 case 'empty_tbl':
180 $full_query .= 'TRUNCATE ';
181 $full_query .= PMA_backquote(htmlspecialchars($sval))
182 . ';<br />';
183 break;
185 case 'primary_fld':
186 if ($full_query == '') {
187 $full_query .= 'ALTER TABLE '
188 . PMA_backquote(htmlspecialchars($table))
189 . '<br />&nbsp;&nbsp;DROP PRIMARY KEY,'
190 . '<br />&nbsp;&nbsp; ADD PRIMARY KEY('
191 . '<br />&nbsp;&nbsp;&nbsp;&nbsp; '
192 . PMA_backquote(htmlspecialchars($sval))
193 . ',';
194 } else {
195 $full_query .= '<br />&nbsp;&nbsp;&nbsp;&nbsp; '
196 . PMA_backquote(htmlspecialchars($sval))
197 . ',';
199 if ($i == $selected_cnt-1) {
200 $full_query = preg_replace('@,$@', ');<br />', $full_query);
202 break;
204 case 'drop_fld':
205 if ($full_query == '') {
206 $full_query .= 'ALTER TABLE '
207 . PMA_backquote(htmlspecialchars($table));
209 $full_query .= '<br />&nbsp;&nbsp;DROP '
210 . PMA_backquote(htmlspecialchars($sval))
211 . ',';
212 if ($i == $selected_cnt - 1) {
213 $full_query = preg_replace('@,$@', ';<br />', $full_query);
215 break;
216 } // end switch
217 $i++;
219 if ($what == 'drop_tbl') {
220 if (!empty($full_query)) {
221 $full_query .= ';<br />' . "\n";
223 if (!empty($full_query_views)) {
224 $full_query .= $full_query_views . ';<br />' . "\n";
226 unset($full_query_views);
229 // Displays the confirmation form
230 $_url_params = array(
231 'query_type' => $what,
232 'reload' => (! empty($reload) ? 1 : 0),
234 if (strpos(' ' . $action, 'db_') == 1) {
235 $_url_params['db']= $db;
236 } elseif (strpos(' ' . $action, 'tbl_') == 1 || $what == 'row_delete') {
237 $_url_params['db']= $db;
238 $_url_params['table']= $table;
240 foreach ($selected as $idx => $sval) {
241 $_url_params['selected'][] = $sval;
243 if ($what == 'drop_tbl' && !empty($views)) {
244 foreach ($views as $current) {
245 $_url_params['views'][] = $current;
248 if ($what == 'row_delete') {
249 $_url_params['original_sql_query'] = $original_sql_query;
250 $_url_params['original_url_query'] = $original_url_query;
253 <form action="<?php echo $action; ?>" method="post">
254 <?php
255 echo PMA_generate_common_hidden_inputs($_url_params);
257 <fieldset class="confirmation">
258 <legend><?php echo ($what == 'drop_db' ? __('You are about to DESTROY a complete database!') . '&nbsp;' : '') . __('Do you really want to '); ?>:</legend>
259 <tt><?php echo $full_query; ?></tt>
260 </fieldset>
261 <fieldset class="tblFooters">
262 <input type="submit" name="mult_btn" value="<?php echo __('Yes'); ?>" id="buttonYes" />
263 <input type="submit" name="mult_btn" value="<?php echo __('No'); ?>" id="buttonNo" />
264 </fieldset>
265 <?php
266 require_once './libraries/footer.inc.php';
267 } // end if
271 * Executes the query - dropping rows, columns/fields, tables or dbs
273 elseif ($mult_btn == __('Yes')) {
275 if ($query_type == 'drop_db' || $query_type == 'drop_tbl' || $query_type == 'drop_fld') {
276 require_once './libraries/relation_cleanup.lib.php';
279 $sql_query = '';
280 if ($query_type == 'drop_tbl') {
281 $sql_query_views = '';
283 $selected_cnt = count($selected);
284 $run_parts = FALSE; // whether to run query after each pass
285 $use_sql = FALSE; // whether to include sql.php at the end (to display results)
287 if ($query_type == 'primary_fld') {
288 // Gets table primary key
289 PMA_DBI_select_db($db);
290 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
291 $primary = '';
292 while ($row = PMA_DBI_fetch_assoc($result)) {
293 // Backups the list of primary keys
294 if ($row['Key_name'] == 'PRIMARY') {
295 $primary .= $row['Column_name'] . ', ';
297 } // end while
298 PMA_DBI_free_result($result);
301 $rebuild_database_list = false;
303 for ($i = 0; $i < $selected_cnt; $i++) {
304 switch ($query_type) {
305 case 'row_delete':
306 $a_query = $selected[$i];
307 $run_parts = TRUE;
308 break;
310 case 'drop_db':
311 PMA_relationsCleanupDatabase($selected[$i]);
312 $a_query = 'DROP DATABASE '
313 . PMA_backquote($selected[$i]);
314 $reload = 1;
315 $run_parts = TRUE;
316 $rebuild_database_list = true;
317 break;
319 case 'drop_tbl':
320 PMA_relationsCleanupTable($db, $selected[$i]);
321 $current = $selected[$i];
322 if (!empty($views) && in_array($current, $views)) {
323 $sql_query_views .= (empty($sql_query_views) ? 'DROP VIEW ' : ', ')
324 . PMA_backquote($current);
325 } else {
326 $sql_query .= (empty($sql_query) ? 'DROP TABLE ' : ', ')
327 . PMA_backquote($current);
329 $reload = 1;
330 break;
332 case 'check_tbl':
333 $sql_query .= (empty($sql_query) ? 'CHECK TABLE ' : ', ')
334 . PMA_backquote($selected[$i]);
335 $use_sql = TRUE;
336 break;
338 case 'optimize_tbl':
339 $sql_query .= (empty($sql_query) ? 'OPTIMIZE TABLE ' : ', ')
340 . PMA_backquote($selected[$i]);
341 $use_sql = TRUE;
342 break;
344 case 'analyze_tbl':
345 $sql_query .= (empty($sql_query) ? 'ANALYZE TABLE ' : ', ')
346 . PMA_backquote($selected[$i]);
347 $use_sql = TRUE;
348 break;
350 case 'repair_tbl':
351 $sql_query .= (empty($sql_query) ? 'REPAIR TABLE ' : ', ')
352 . PMA_backquote($selected[$i]);
353 $use_sql = TRUE;
354 break;
356 case 'empty_tbl':
357 $a_query = 'TRUNCATE ';
358 $a_query .= PMA_backquote(htmlspecialchars($selected[$i]));
359 $run_parts = TRUE;
360 break;
362 case 'drop_fld':
363 PMA_relationsCleanupColumn($db, $table, $selected[$i]);
364 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) : ',')
365 . ' DROP ' . PMA_backquote($selected[$i])
366 . (($i == $selected_cnt-1) ? ';' : '');
367 break;
369 case 'primary_fld':
370 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . (empty($primary) ? '' : ' DROP PRIMARY KEY,') . ' ADD PRIMARY KEY( ' : ', ')
371 . PMA_backquote($selected[$i])
372 . (($i == $selected_cnt-1) ? ');' : '');
373 break;
375 case 'index_fld':
376 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ' ADD INDEX( ' : ', ')
377 . PMA_backquote($selected[$i])
378 . (($i == $selected_cnt-1) ? ');' : '');
379 break;
381 case 'unique_fld':
382 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ' ADD UNIQUE( ' : ', ')
383 . PMA_backquote($selected[$i])
384 . (($i == $selected_cnt-1) ? ');' : '');
385 break;
387 case 'fulltext_fld':
388 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ' ADD FULLTEXT( ' : ', ')
389 . PMA_backquote($selected[$i])
390 . (($i == $selected_cnt-1) ? ');' : '');
391 break;
392 } // end switch
394 // All "DROP TABLE", "DROP FIELD", "OPTIMIZE TABLE" and "REPAIR TABLE"
395 // statements will be run at once below
396 if ($run_parts) {
397 $sql_query .= $a_query . ';' . "\n";
398 if ($query_type != 'drop_db') {
399 PMA_DBI_select_db($db);
401 $result = PMA_DBI_query($a_query);
402 } // end if
403 } // end for
405 if ($query_type == 'drop_tbl') {
406 if (!empty($sql_query)) {
407 $sql_query .= ';';
408 } elseif (!empty($sql_query_views)) {
409 $sql_query = $sql_query_views . ';';
410 unset($sql_query_views);
414 if ($use_sql) {
415 require './sql.php';
416 } elseif (!$run_parts) {
417 PMA_DBI_select_db($db);
418 $result = PMA_DBI_try_query($sql_query);
419 if ($result && !empty($sql_query_views)) {
420 $sql_query .= ' ' . $sql_query_views . ';';
421 $result = PMA_DBI_try_query($sql_query_views);
422 unset($sql_query_views);
425 if (! $result) {
426 $message = PMA_Message::error(PMA_DBI_getError());
429 if ($rebuild_database_list) {
430 // avoid a problem with the database list navigator
431 // when dropping a db from server_databases
432 $GLOBALS['pma']->databases->build();
434 } else {
435 $message = PMA_Message::success('strNoModification');