bug #1669459 [interface] Do not automatically mark PDF schema rows to delete
[phpmyadmin/dkf.git] / libraries / mult_submits.inc.php
blobfe614e7a63354442b531489d6d3c46909708fcd3
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 view')) {
28 require './tbl_printview.php';
29 } else {
30 $selected = $selected_tbl;
31 switch ($submit_mult) {
32 case 'drop_db':
33 $what = 'drop_db';
34 break;
35 case __('Drop'):
36 $what = 'drop_tbl';
37 break;
38 case __('Empty'):
39 $what = 'empty_tbl';
40 break;
41 case __('Check table'):
42 unset($submit_mult);
43 $query_type = 'check_tbl';
44 $mult_btn = __('Yes');
45 break;
46 case __('Optimize table'):
47 unset($submit_mult);
48 $query_type = 'optimize_tbl';
49 $mult_btn = __('Yes');
50 break;
51 case __('Repair table'):
52 unset($submit_mult);
53 $query_type = 'repair_tbl';
54 $mult_btn = __('Yes');
55 break;
56 case __('Analyze table'):
57 unset($submit_mult);
58 $query_type = 'analyze_tbl';
59 $mult_btn = __('Yes');
60 break;
61 case __('Export'):
62 unset($submit_mult);
63 require('db_export.php');
64 exit;
65 break;
66 } // end switch
68 } elseif (isset($selected_fld) && !empty($selected_fld)) {
69 // coming from table structure view - do something with selected columns/fileds
70 $selected = $selected_fld;
71 switch ($submit_mult) {
72 case __('Drop'):
73 $what = 'drop_fld';
74 break;
75 case __('Primary'):
76 // Gets table primary key
77 PMA_DBI_select_db($db);
78 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
79 $primary = '';
80 while ($row = PMA_DBI_fetch_assoc($result)) {
81 // Backups the list of primary keys
82 if ($row['Key_name'] == 'PRIMARY') {
83 $primary .= $row['Column_name'] . ', ';
85 } // end while
86 PMA_DBI_free_result($result);
87 if (empty($primary)) {
88 // no primary key, so we can safely create new
89 unset($submit_mult);
90 $query_type = 'primary_fld';
91 $mult_btn = __('Yes');
92 } else {
93 // primary key exists, so lets as user
94 $what = 'primary_fld';
96 break;
97 case __('Index'):
98 unset($submit_mult);
99 $query_type = 'index_fld';
100 $mult_btn = __('Yes');
101 break;
102 case __('Unique'):
103 unset($submit_mult);
104 $query_type = 'unique_fld';
105 $mult_btn = __('Yes');
106 break;
107 case __('Fulltext'):
108 unset($submit_mult);
109 $query_type = 'fulltext_fld';
110 $mult_btn = __('Yes');
111 break;
112 case __('Change'):
113 require './tbl_alter.php';
114 break;
115 case __('Browse'):
116 // this should already be handled by tbl_structure.php
118 } else {
119 // coming from borwsing - do something with selected rows
120 $what = 'row_delete';
121 $selected = $rows_to_delete;
123 } // end if
127 * Displays the confirmation form if required
129 if (!empty($submit_mult) && !empty($what)) {
130 unset($message);
132 require_once './libraries/header.inc.php';
133 if (strlen($table)) {
134 require './libraries/tbl_common.php';
135 $url_query .= '&amp;goto=tbl_sql.php&amp;back=tbl_sql.php';
136 require './libraries/tbl_info.inc.php';
137 require_once './libraries/tbl_links.inc.php';
138 } elseif (strlen($db)) {
139 require './libraries/db_common.inc.php';
140 require './libraries/db_info.inc.php';
141 } else {
142 require_once './libraries/server_common.inc.php';
143 require_once './libraries/server_links.inc.php';
146 // Builds the query
147 $full_query = '';
148 if ($what == 'drop_tbl') {
149 $full_query_views = '';
151 $selected_cnt = count($selected);
152 $i = 0;
153 foreach ($selected AS $idx => $sval) {
154 switch ($what) {
155 case 'row_delete':
156 $full_query .= htmlspecialchars($sval)
157 . ';<br />';
158 break;
159 case 'drop_db':
160 $full_query .= 'DROP DATABASE '
161 . PMA_backquote(htmlspecialchars($sval))
162 . ';<br />';
163 $reload = 1;
164 break;
166 case 'drop_tbl':
167 $current = $sval;
168 if (!empty($views) && in_array($current, $views)) {
169 $full_query_views .= (empty($full_query_views) ? 'DROP VIEW ' : ', ')
170 . PMA_backquote(htmlspecialchars($current));
171 } else {
172 $full_query .= (empty($full_query) ? 'DROP TABLE ' : ', ')
173 . PMA_backquote(htmlspecialchars($current));
175 break;
177 case 'empty_tbl':
178 $full_query .= 'TRUNCATE ';
179 $full_query .= PMA_backquote(htmlspecialchars($sval))
180 . ';<br />';
181 break;
183 case 'primary_fld':
184 if ($full_query == '') {
185 $full_query .= 'ALTER TABLE '
186 . PMA_backquote(htmlspecialchars($table))
187 . '<br />&nbsp;&nbsp;DROP PRIMARY KEY,'
188 . '<br />&nbsp;&nbsp; ADD PRIMARY KEY('
189 . '<br />&nbsp;&nbsp;&nbsp;&nbsp; '
190 . PMA_backquote(htmlspecialchars($sval))
191 . ',';
192 } else {
193 $full_query .= '<br />&nbsp;&nbsp;&nbsp;&nbsp; '
194 . PMA_backquote(htmlspecialchars($sval))
195 . ',';
197 if ($i == $selected_cnt-1) {
198 $full_query = preg_replace('@,$@', ');<br />', $full_query);
200 break;
202 case 'drop_fld':
203 if ($full_query == '') {
204 $full_query .= 'ALTER TABLE '
205 . PMA_backquote(htmlspecialchars($table));
207 $full_query .= '<br />&nbsp;&nbsp;DROP '
208 . PMA_backquote(htmlspecialchars($sval))
209 . ',';
210 if ($i == $selected_cnt - 1) {
211 $full_query = preg_replace('@,$@', ';<br />', $full_query);
213 break;
214 } // end switch
215 $i++;
217 if ($what == 'drop_tbl') {
218 if (!empty($full_query)) {
219 $full_query .= ';<br />' . "\n";
221 if (!empty($full_query_views)) {
222 $full_query .= $full_query_views . ';<br />' . "\n";
224 unset($full_query_views);
227 // Displays the confirmation form
228 $_url_params = array(
229 'query_type' => $what,
230 'reload' => (! empty($reload) ? 1 : 0),
232 if (strpos(' ' . $action, 'db_') == 1) {
233 $_url_params['db']= $db;
234 } elseif (strpos(' ' . $action, 'tbl_') == 1 || $what == 'row_delete') {
235 $_url_params['db']= $db;
236 $_url_params['table']= $table;
238 foreach ($selected as $idx => $sval) {
239 $_url_params['selected'][] = $sval;
241 if ($what == 'drop_tbl' && !empty($views)) {
242 foreach ($views as $current) {
243 $_url_params['views'][] = $current;
246 if ($what == 'row_delete') {
247 $_url_params['original_sql_query'] = $original_sql_query;
248 $_url_params['original_url_query'] = $original_url_query;
251 <form action="<?php echo $action; ?>" method="post">
252 <?php
253 echo PMA_generate_common_hidden_inputs($_url_params);
255 <fieldset class="confirmation">
256 <legend><?php echo ($what == 'drop_db' ? __('You are about to DESTROY a complete database!') . '&nbsp;' : '') . __('Do you really want to '); ?>:</legend>
257 <tt><?php echo $full_query; ?></tt>
258 </fieldset>
259 <fieldset class="tblFooters">
260 <input type="submit" name="mult_btn" value="<?php echo __('Yes'); ?>" id="buttonYes" />
261 <input type="submit" name="mult_btn" value="<?php echo __('No'); ?>" id="buttonNo" />
262 </fieldset>
263 <?php
264 require './libraries/footer.inc.php';
265 } // end if
269 * Executes the query - dropping rows, columns/fields, tables or dbs
271 elseif ($mult_btn == __('Yes')) {
273 if ($query_type == 'drop_db' || $query_type == 'drop_tbl' || $query_type == 'drop_fld') {
274 require_once './libraries/relation_cleanup.lib.php';
277 $sql_query = '';
278 if ($query_type == 'drop_tbl') {
279 $sql_query_views = '';
281 $selected_cnt = count($selected);
282 $run_parts = FALSE; // whether to run query after each pass
283 $use_sql = FALSE; // whether to include sql.php at the end (to display results)
285 if ($query_type == 'primary_fld') {
286 // Gets table primary key
287 PMA_DBI_select_db($db);
288 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
289 $primary = '';
290 while ($row = PMA_DBI_fetch_assoc($result)) {
291 // Backups the list of primary keys
292 if ($row['Key_name'] == 'PRIMARY') {
293 $primary .= $row['Column_name'] . ', ';
295 } // end while
296 PMA_DBI_free_result($result);
299 $rebuild_database_list = false;
301 for ($i = 0; $i < $selected_cnt; $i++) {
302 switch ($query_type) {
303 case 'row_delete':
304 $a_query = $selected[$i];
305 $run_parts = TRUE;
306 break;
308 case 'drop_db':
309 PMA_relationsCleanupDatabase($selected[$i]);
310 $a_query = 'DROP DATABASE '
311 . PMA_backquote($selected[$i]);
312 $reload = 1;
313 $run_parts = TRUE;
314 $rebuild_database_list = true;
315 break;
317 case 'drop_tbl':
318 PMA_relationsCleanupTable($db, $selected[$i]);
319 $current = $selected[$i];
320 if (!empty($views) && in_array($current, $views)) {
321 $sql_query_views .= (empty($sql_query_views) ? 'DROP VIEW ' : ', ')
322 . PMA_backquote($current);
323 } else {
324 $sql_query .= (empty($sql_query) ? 'DROP TABLE ' : ', ')
325 . PMA_backquote($current);
327 $reload = 1;
328 break;
330 case 'check_tbl':
331 $sql_query .= (empty($sql_query) ? 'CHECK TABLE ' : ', ')
332 . PMA_backquote($selected[$i]);
333 $use_sql = TRUE;
334 break;
336 case 'optimize_tbl':
337 $sql_query .= (empty($sql_query) ? 'OPTIMIZE TABLE ' : ', ')
338 . PMA_backquote($selected[$i]);
339 $use_sql = TRUE;
340 break;
342 case 'analyze_tbl':
343 $sql_query .= (empty($sql_query) ? 'ANALYZE TABLE ' : ', ')
344 . PMA_backquote($selected[$i]);
345 $use_sql = TRUE;
346 break;
348 case 'repair_tbl':
349 $sql_query .= (empty($sql_query) ? 'REPAIR TABLE ' : ', ')
350 . PMA_backquote($selected[$i]);
351 $use_sql = TRUE;
352 break;
354 case 'empty_tbl':
355 $a_query = 'TRUNCATE ';
356 $a_query .= PMA_backquote(htmlspecialchars($selected[$i]));
357 $run_parts = TRUE;
358 break;
360 case 'drop_fld':
361 PMA_relationsCleanupColumn($db, $table, $selected[$i]);
362 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) : ',')
363 . ' DROP ' . PMA_backquote($selected[$i])
364 . (($i == $selected_cnt-1) ? ';' : '');
365 break;
367 case 'primary_fld':
368 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . (empty($primary) ? '' : ' DROP PRIMARY KEY,') . ' ADD PRIMARY KEY( ' : ', ')
369 . PMA_backquote($selected[$i])
370 . (($i == $selected_cnt-1) ? ');' : '');
371 break;
373 case 'index_fld':
374 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ' ADD INDEX( ' : ', ')
375 . PMA_backquote($selected[$i])
376 . (($i == $selected_cnt-1) ? ');' : '');
377 break;
379 case 'unique_fld':
380 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ' ADD UNIQUE( ' : ', ')
381 . PMA_backquote($selected[$i])
382 . (($i == $selected_cnt-1) ? ');' : '');
383 break;
385 case 'fulltext_fld':
386 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ' ADD FULLTEXT( ' : ', ')
387 . PMA_backquote($selected[$i])
388 . (($i == $selected_cnt-1) ? ');' : '');
389 break;
390 } // end switch
392 // All "DROP TABLE", "DROP FIELD", "OPTIMIZE TABLE" and "REPAIR TABLE"
393 // statements will be run at once below
394 if ($run_parts) {
395 $sql_query .= $a_query . ';' . "\n";
396 if ($query_type != 'drop_db') {
397 PMA_DBI_select_db($db);
399 $result = PMA_DBI_query($a_query);
400 } // end if
401 } // end for
403 if ($query_type == 'drop_tbl') {
404 if (!empty($sql_query)) {
405 $sql_query .= ';';
406 } elseif (!empty($sql_query_views)) {
407 $sql_query = $sql_query_views . ';';
408 unset($sql_query_views);
412 if ($use_sql) {
413 require './sql.php';
414 } elseif (!$run_parts) {
415 PMA_DBI_select_db($db);
416 $result = PMA_DBI_try_query($sql_query);
417 if ($result && !empty($sql_query_views)) {
418 $sql_query .= ' ' . $sql_query_views . ';';
419 $result = PMA_DBI_try_query($sql_query_views);
420 unset($sql_query_views);
423 if (! $result) {
424 $message = PMA_Message::error(PMA_DBI_getError());
427 if ($rebuild_database_list) {
428 // avoid a problem with the database list navigator
429 // when dropping a db from server_databases
430 $GLOBALS['pma']->databases->build();
432 } else {
433 $message = PMA_Message::success(__('No change'));