Translation update done using Pootle.
[phpmyadmin/madhuracj.git] / libraries / mult_submits.inc.php
blobbccc7b4152ff99f1bda0fe41c2709c49c006ca4e
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 include './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 include '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 'spatial':
95 unset($submit_mult);
96 $query_type = 'spatial_fld';
97 $mult_btn = __('Yes');
98 break;
99 case 'ftext':
100 unset($submit_mult);
101 $query_type = 'fulltext_fld';
102 $mult_btn = __('Yes');
103 break;
104 case 'change':
105 include './tbl_alter.php';
106 break;
107 case 'browse':
108 // this should already be handled by tbl_structure.php
110 } else {
111 // coming from browsing - do something with selected rows
112 $what = 'row_delete';
113 $selected = $rows_to_delete;
115 } // end if
119 * Displays the confirmation form if required
121 if (!empty($submit_mult) && !empty($what)) {
122 unset($message);
124 include_once './libraries/header.inc.php';
125 if (strlen($table)) {
126 include './libraries/tbl_common.php';
127 $url_query .= '&amp;goto=tbl_sql.php&amp;back=tbl_sql.php';
128 include './libraries/tbl_info.inc.php';
129 include_once './libraries/tbl_links.inc.php';
130 } elseif (strlen($db)) {
131 include './libraries/db_common.inc.php';
132 include './libraries/db_info.inc.php';
133 } else {
134 include_once './libraries/server_common.inc.php';
135 include_once './libraries/server_links.inc.php';
138 // Builds the query
139 $full_query = '';
140 if ($what == 'drop_tbl') {
141 $full_query_views = '';
143 $selected_cnt = count($selected);
144 $i = 0;
145 foreach ($selected AS $idx => $sval) {
146 switch ($what) {
147 case 'row_delete':
148 $full_query .= htmlspecialchars($sval)
149 . ';<br />';
150 break;
151 case 'drop_db':
152 $full_query .= 'DROP DATABASE '
153 . PMA_backquote(htmlspecialchars($sval))
154 . ';<br />';
155 $reload = 1;
156 break;
158 case 'drop_tbl':
159 $current = $sval;
160 if (!empty($views) && in_array($current, $views)) {
161 $full_query_views .= (empty($full_query_views) ? 'DROP VIEW ' : ', ')
162 . PMA_backquote(htmlspecialchars($current));
163 } else {
164 $full_query .= (empty($full_query) ? 'DROP TABLE ' : ', ')
165 . PMA_backquote(htmlspecialchars($current));
167 break;
169 case 'empty_tbl':
170 $full_query .= 'TRUNCATE ';
171 $full_query .= PMA_backquote(htmlspecialchars($sval))
172 . ';<br />';
173 break;
175 case 'primary_fld':
176 if ($full_query == '') {
177 $full_query .= 'ALTER TABLE '
178 . PMA_backquote(htmlspecialchars($table))
179 . '<br />&nbsp;&nbsp;DROP PRIMARY KEY,'
180 . '<br />&nbsp;&nbsp; ADD PRIMARY KEY('
181 . '<br />&nbsp;&nbsp;&nbsp;&nbsp; '
182 . PMA_backquote(htmlspecialchars($sval))
183 . ',';
184 } else {
185 $full_query .= '<br />&nbsp;&nbsp;&nbsp;&nbsp; '
186 . PMA_backquote(htmlspecialchars($sval))
187 . ',';
189 if ($i == $selected_cnt-1) {
190 $full_query = preg_replace('@,$@', ');<br />', $full_query);
192 break;
194 case 'drop_fld':
195 if ($full_query == '') {
196 $full_query .= 'ALTER TABLE '
197 . PMA_backquote(htmlspecialchars($table));
199 $full_query .= '<br />&nbsp;&nbsp;DROP '
200 . PMA_backquote(htmlspecialchars($sval))
201 . ',';
202 if ($i == $selected_cnt - 1) {
203 $full_query = preg_replace('@,$@', ';<br />', $full_query);
205 break;
206 } // end switch
207 $i++;
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">
246 <?php
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>
252 <table>
253 <tr>
254 <td><?php echo __('From'); ?></td><td><input type="text" name="from_prefix" id="initialPrefix"</td>
255 </tr>
256 <tr>
257 <td><?php echo __('To'); ?> </td><td><input type="text" name="to_prefix" id="newPrefix"</td>
258 </tr>
259 </table>
260 </fieldset>
261 <fieldset class="tblFooters">
262 <button type="submit" name="mult_btn" value="<?php echo __('Yes'); ?>" id="buttonYes"><?php echo __('Submit'); ?></button>
263 </fieldset>
264 <?php
265 } elseif ($what == 'add_prefix_tbl') { ?>
266 <fieldset class = "input">
267 <legend><?php echo __('Add table prefix') ?>:</legend>
268 <table>
269 <tr>
270 <td><?php echo __('Add prefix'); ?></td> <td><input type="text" name="add_prefix" id="txtPrefix"</td>
271 </tr>
272 </table>
273 </fieldset>
274 <fieldset class="tblFooters">
275 <button type="submit" name="mult_btn" value="<?php echo __('Yes'); ?>" id="buttonYes"><?php echo __('Submit'); ?></button>
276 </fieldset>
277 <?php
278 } else { ?>
279 <fieldset class="confirmation">
280 <legend><?php echo ($what == 'drop_db' ? __('You are about to DESTROY a complete database!') . '&nbsp;' : '') . __('Do you really want to '); ?>:</legend>
281 <tt><?php echo $full_query; ?></tt>
282 </fieldset>
283 <fieldset class="tblFooters">
284 <input type="submit" name="mult_btn" value="<?php echo __('Yes'); ?>" id="buttonYes" />
285 <input type="submit" name="mult_btn" value="<?php echo __('No'); ?>" id="buttonNo" />
286 </fieldset>
287 <?php
289 include './libraries/footer.inc.php';
291 } elseif ($mult_btn == __('Yes')) {
293 * Executes the query - dropping rows, columns/fields, tables or dbs
295 if ($query_type == 'drop_db' || $query_type == 'drop_tbl' || $query_type == 'drop_fld') {
296 include_once './libraries/relation_cleanup.lib.php';
299 $sql_query = '';
300 if ($query_type == 'drop_tbl') {
301 $sql_query_views = '';
303 $selected_cnt = count($selected);
304 $run_parts = false; // whether to run query after each pass
305 $use_sql = false; // whether to include sql.php at the end (to display results)
307 if ($query_type == 'primary_fld') {
308 // Gets table primary key
309 PMA_DBI_select_db($db);
310 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
311 $primary = '';
312 while ($row = PMA_DBI_fetch_assoc($result)) {
313 // Backups the list of primary keys
314 if ($row['Key_name'] == 'PRIMARY') {
315 $primary .= $row['Column_name'] . ', ';
317 } // end while
318 PMA_DBI_free_result($result);
321 $rebuild_database_list = false;
323 for ($i = 0; $i < $selected_cnt; $i++) {
324 switch ($query_type) {
325 case 'row_delete':
326 $a_query = $selected[$i];
327 $run_parts = true;
328 break;
330 case 'drop_db':
331 PMA_relationsCleanupDatabase($selected[$i]);
332 $a_query = 'DROP DATABASE '
333 . PMA_backquote($selected[$i]);
334 $reload = 1;
335 $run_parts = true;
336 $rebuild_database_list = true;
337 break;
339 case 'drop_tbl':
340 PMA_relationsCleanupTable($db, $selected[$i]);
341 $current = $selected[$i];
342 if (!empty($views) && in_array($current, $views)) {
343 $sql_query_views .= (empty($sql_query_views) ? 'DROP VIEW ' : ', ')
344 . PMA_backquote($current);
345 } else {
346 $sql_query .= (empty($sql_query) ? 'DROP TABLE ' : ', ')
347 . PMA_backquote($current);
349 $reload = 1;
350 break;
352 case 'check_tbl':
353 $sql_query .= (empty($sql_query) ? 'CHECK TABLE ' : ', ')
354 . PMA_backquote($selected[$i]);
355 $use_sql = true;
356 break;
358 case 'optimize_tbl':
359 $sql_query .= (empty($sql_query) ? 'OPTIMIZE TABLE ' : ', ')
360 . PMA_backquote($selected[$i]);
361 $use_sql = true;
362 break;
364 case 'analyze_tbl':
365 $sql_query .= (empty($sql_query) ? 'ANALYZE TABLE ' : ', ')
366 . PMA_backquote($selected[$i]);
367 $use_sql = true;
368 break;
370 case 'repair_tbl':
371 $sql_query .= (empty($sql_query) ? 'REPAIR TABLE ' : ', ')
372 . PMA_backquote($selected[$i]);
373 $use_sql = true;
374 break;
376 case 'empty_tbl':
377 $a_query = 'TRUNCATE ';
378 $a_query .= PMA_backquote($selected[$i]);
379 $run_parts = true;
380 break;
382 case 'drop_fld':
383 PMA_relationsCleanupColumn($db, $table, $selected[$i]);
384 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) : ',')
385 . ' DROP ' . PMA_backquote($selected[$i])
386 . (($i == $selected_cnt-1) ? ';' : '');
387 break;
389 case 'primary_fld':
390 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . (empty($primary) ? '' : ' DROP PRIMARY KEY,') . ' ADD PRIMARY KEY( ' : ', ')
391 . PMA_backquote($selected[$i])
392 . (($i == $selected_cnt-1) ? ');' : '');
393 break;
395 case 'index_fld':
396 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ' ADD INDEX( ' : ', ')
397 . PMA_backquote($selected[$i])
398 . (($i == $selected_cnt-1) ? ');' : '');
399 break;
401 case 'unique_fld':
402 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ' ADD UNIQUE( ' : ', ')
403 . PMA_backquote($selected[$i])
404 . (($i == $selected_cnt-1) ? ');' : '');
405 break;
407 case 'spatial_fld':
408 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ' ADD SPATIAL( ' : ', ')
409 . PMA_backquote($selected[$i])
410 . (($i == $selected_cnt-1) ? ');' : '');
411 break;
413 case 'fulltext_fld':
414 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ' ADD FULLTEXT( ' : ', ')
415 . PMA_backquote($selected[$i])
416 . (($i == $selected_cnt-1) ? ');' : '');
417 break;
419 case 'add_prefix_tbl':
420 $newtablename = $add_prefix . $selected[$i];
421 $a_query = 'ALTER TABLE ' . PMA_backquote($selected[$i]) . ' RENAME ' . PMA_backquote($newtablename) ; // ADD PREFIX TO TABLE NAME
422 $run_parts = true;
423 break;
425 case 'replace_prefix_tbl':
426 $current = $selected[$i];
427 $newtablename = preg_replace("/^" . $from_prefix . "/", $to_prefix, $current);
428 $a_query = 'ALTER TABLE ' . PMA_backquote($selected[$i]) . ' RENAME ' . PMA_backquote($newtablename) ; // CHANGE PREFIX PATTERN
429 $run_parts = true;
430 break;
432 case 'copy_tbl_change_prefix':
433 $current = $selected[$i];
434 $newtablename = preg_replace("/^" . $from_prefix . "/", $to_prefix, $current);
435 $a_query = 'CREATE TABLE ' . PMA_backquote($newtablename) . ' SELECT * FROM ' . PMA_backquote($selected[$i]) ; // COPY TABLE AND CHANGE PREFIX PATTERN
436 $run_parts = true;
437 break;
439 } // end switch
441 // All "DROP TABLE", "DROP FIELD", "OPTIMIZE TABLE" and "REPAIR TABLE"
442 // statements will be run at once below
443 if ($run_parts) {
444 $sql_query .= $a_query . ';' . "\n";
445 if ($query_type != 'drop_db') {
446 PMA_DBI_select_db($db);
448 $result = PMA_DBI_query($a_query);
449 } // end if
450 } // end for
452 if ($query_type == 'drop_tbl') {
453 if (!empty($sql_query)) {
454 $sql_query .= ';';
455 } elseif (!empty($sql_query_views)) {
456 $sql_query = $sql_query_views . ';';
457 unset($sql_query_views);
461 if ($use_sql) {
462 include './sql.php';
463 } elseif (!$run_parts) {
464 PMA_DBI_select_db($db);
465 $result = PMA_DBI_try_query($sql_query);
466 if ($result && !empty($sql_query_views)) {
467 $sql_query .= ' ' . $sql_query_views . ';';
468 $result = PMA_DBI_try_query($sql_query_views);
469 unset($sql_query_views);
472 if (! $result) {
473 $message = PMA_Message::error(PMA_DBI_getError());
476 if ($rebuild_database_list) {
477 // avoid a problem with the database list navigator
478 // when dropping a db from server_databases
479 $GLOBALS['pma']->databases->build();
481 } else {
482 $message = PMA_Message::success(__('No change'));