2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * functions for multi submit forms
6 * @usedby mult_submits.inc.php
10 use PMA\libraries\Table
;
11 use PMA\libraries\URL
;
16 * @param string $what mult submit type
17 * @param bool $reload is reload
18 * @param string $action action type
19 * @param string $db database name
20 * @param string $table table name
21 * @param array $selected selected rows(table,db)
22 * @param array $views table views
23 * @param string $original_sql_query original sql query
24 * @param string $original_url_query original url query
28 function PMA_getUrlParams(
29 $what, $reload, $action, $db, $table, $selected, $views,
30 $original_sql_query, $original_url_query
33 'query_type' => $what,
34 'reload' => (! empty($reload) ?
1 : 0),
36 if (mb_strpos(' ' . $action, 'db_') == 1) {
37 $_url_params['db']= $db;
38 } elseif (mb_strpos(' ' . $action, 'tbl_') == 1
39 ||
$what == 'row_delete'
41 $_url_params['db']= $db;
42 $_url_params['table']= $table;
44 foreach ($selected as $sval) {
45 if ($what == 'row_delete') {
46 $_url_params['selected'][] = 'DELETE FROM '
47 . PMA\libraries\Util
::backquote($table)
48 . ' WHERE ' . $sval . ' LIMIT 1;';
50 $_url_params['selected'][] = $sval;
53 if ($what == 'drop_tbl' && !empty($views)) {
54 foreach ($views as $current) {
55 $_url_params['views'][] = $current;
58 if ($what == 'row_delete') {
59 $_url_params['original_sql_query'] = $original_sql_query;
60 if (! empty($original_url_query)) {
61 $_url_params['original_url_query'] = $original_url_query;
69 * Builds or execute queries for multiple elements, depending on $query_type
71 * @param string $query_type query type
72 * @param array $selected selected tables
73 * @param string $db db name
74 * @param string $table table name
75 * @param array $views table views
76 * @param string $primary table primary
77 * @param string $from_prefix from prefix original
78 * @param string $to_prefix to prefix original
82 function PMA_buildOrExecuteQueryForMulti(
83 $query_type, $selected, $db, $table, $views, $primary,
84 $from_prefix, $to_prefix
86 $rebuild_database_list = false;
90 $sql_query_views = null;
91 // whether to run query after each pass
93 // whether to execute the query at the end (to display results)
94 $execute_query_later = false;
97 if ($query_type == 'drop_tbl') {
98 $sql_query_views = '';
101 $selected_cnt = count($selected);
105 for ($i = 0; $i < $selected_cnt; $i++
) {
106 switch ($query_type) {
109 $a_query = $selected[$i];
114 PMA_relationsCleanupDatabase($selected[$i]);
115 $a_query = 'DROP DATABASE '
116 . PMA\libraries\Util
::backquote($selected[$i]);
119 $rebuild_database_list = true;
123 PMA_relationsCleanupTable($db, $selected[$i]);
124 $current = $selected[$i];
125 if (!empty($views) && in_array($current, $views)) {
126 $sql_query_views .= (empty($sql_query_views) ?
'DROP VIEW ' : ', ')
127 . PMA\libraries\Util
::backquote($current);
129 $sql_query .= (empty($sql_query) ?
'DROP TABLE ' : ', ')
130 . PMA\libraries\Util
::backquote($current);
136 $sql_query .= (empty($sql_query) ?
'CHECK TABLE ' : ', ')
137 . PMA\libraries\Util
::backquote($selected[$i]);
138 $execute_query_later = true;
142 $sql_query .= (empty($sql_query) ?
'OPTIMIZE TABLE ' : ', ')
143 . PMA\libraries\Util
::backquote($selected[$i]);
144 $execute_query_later = true;
148 $sql_query .= (empty($sql_query) ?
'ANALYZE TABLE ' : ', ')
149 . PMA\libraries\Util
::backquote($selected[$i]);
150 $execute_query_later = true;
154 $sql_query .= (empty($sql_query) ?
'CHECKSUM TABLE ' : ', ')
155 . PMA\libraries\Util
::backquote($selected[$i]);
156 $execute_query_later = true;
160 $sql_query .= (empty($sql_query) ?
'REPAIR TABLE ' : ', ')
161 . PMA\libraries\Util
::backquote($selected[$i]);
162 $execute_query_later = true;
167 $a_query = 'TRUNCATE ';
168 $a_query .= PMA\libraries\Util
::backquote($selected[$i]);
173 PMA_relationsCleanupColumn($db, $table, $selected[$i]);
174 $sql_query .= (empty($sql_query)
175 ?
'ALTER TABLE ' . PMA\libraries\Util
::backquote($table)
177 . ' DROP ' . PMA\libraries\Util
::backquote($selected[$i])
178 . (($i == $selected_cnt-1) ?
';' : '');
182 $sql_query .= (empty($sql_query)
183 ?
'ALTER TABLE ' . PMA\libraries\Util
::backquote($table)
186 : ' DROP PRIMARY KEY,') . ' ADD PRIMARY KEY( '
188 . PMA\libraries\Util
::backquote($selected[$i])
189 . (($i == $selected_cnt-1) ?
');' : '');
193 $sql_query .= (empty($sql_query)
194 ?
'ALTER TABLE ' . PMA\libraries\Util
::backquote($table)
197 . PMA\libraries\Util
::backquote($selected[$i])
198 . (($i == $selected_cnt-1) ?
');' : '');
202 $sql_query .= (empty($sql_query)
203 ?
'ALTER TABLE ' . PMA\libraries\Util
::backquote($table)
206 . PMA\libraries\Util
::backquote($selected[$i])
207 . (($i == $selected_cnt-1) ?
');' : '');
211 $sql_query .= (empty($sql_query)
212 ?
'ALTER TABLE ' . PMA\libraries\Util
::backquote($table)
215 . PMA\libraries\Util
::backquote($selected[$i])
216 . (($i == $selected_cnt-1) ?
');' : '');
220 $sql_query .= (empty($sql_query)
221 ?
'ALTER TABLE ' . PMA\libraries\Util
::backquote($table)
224 . PMA\libraries\Util
::backquote($selected[$i])
225 . (($i == $selected_cnt-1) ?
');' : '');
228 case 'add_prefix_tbl':
229 $newtablename = $_POST['add_prefix'] . $selected[$i];
230 // ADD PREFIX TO TABLE NAME
231 $a_query = 'ALTER TABLE '
232 . PMA\libraries\Util
::backquote($selected[$i])
234 . PMA\libraries\Util
::backquote($newtablename);
238 case 'replace_prefix_tbl':
239 $current = $selected[$i];
240 $subFromPrefix = mb_substr(
243 mb_strlen($from_prefix)
245 if ($subFromPrefix == $from_prefix) {
246 $newtablename = $to_prefix
249 mb_strlen($from_prefix)
252 $newtablename = $current;
254 // CHANGE PREFIX PATTERN
255 $a_query = 'ALTER TABLE '
256 . PMA\libraries\Util
::backquote($selected[$i])
258 . PMA\libraries\Util
::backquote($newtablename);
262 case 'copy_tbl_change_prefix':
266 $current = $selected[$i];
267 $newtablename = $to_prefix .
268 mb_substr($current, mb_strlen($from_prefix));
270 // COPY TABLE AND CHANGE PREFIX PATTERN
272 $db, $current, $db, $newtablename,
273 'data', false, 'one_table'
280 Table
::moveCopy($db, $selected[$i], $_POST['target_db'], $selected[$i], $_POST['what'], false, 'one_table');
281 if (isset($_POST['adjust_privileges']) && !empty($_POST['adjust_privileges'])) {
282 include_once 'operations.lib.php';
283 PMA_AdjustPrivileges_copyTable($db, $selected[$i], $_POST['target_db'], $selected[$i]);
288 // All "DROP TABLE", "DROP FIELD", "OPTIMIZE TABLE" and "REPAIR TABLE"
289 // statements will be run at once below
290 if ($run_parts && !$copy_tbl) {
291 $sql_query .= $a_query . ';' . "\n";
292 if ($query_type != 'drop_db') {
293 $GLOBALS['dbi']->selectDb($db);
295 $result = $GLOBALS['dbi']->query($a_query);
297 if ($query_type == 'drop_db') {
298 PMA_clearTransformations($selected[$i]);
299 } elseif ($query_type == 'drop_tbl') {
300 PMA_clearTransformations($db, $selected[$i]);
301 } else if ($query_type == 'drop_fld') {
302 PMA_clearTransformations($db, $table, $selected[$i]);
307 if ($deletes && ! empty($_REQUEST['pos'])) {
308 $_REQUEST['pos'] = PMA_calculatePosForLastPage(
309 $db, $table, isset($_REQUEST['pos']) ?
$_REQUEST['pos'] : null
314 $result, $rebuild_database_list, $reload,
315 $run_parts, $execute_query_later, $sql_query, $sql_query_views
320 * Gets HTML for copy tables form
322 * @param string $action action type
323 * @param array $_url_params URL params
327 function PMA_getHtmlForCopyMultipleTables($action, $_url_params)
329 $html = '<form id="ajax_form" action="' . $action . '" method="post">';
330 $html .= URL
::getHiddenInputs($_url_params);
331 $html .= '<fieldset class = "input">';
332 $databases_list = $GLOBALS['dblist']->databases
;
333 foreach ($databases_list as $key => $db_name)
334 if ($db_name == $GLOBALS['db']){
335 $databases_list->offsetUnset($key);
338 $html .= '<strong><label for="db_name_dropdown">' . __('Database') . ':</label></strong>';
339 $html .= '<select id="db_name_dropdown" class="halfWidth" name="target_db" >'
340 . $databases_list->getHtmlOptions(true, false)
343 $html .= '<strong><label>' . __('Options') . ':</label></strong><br>';
344 $html .= '<input type="radio" id ="what_structure" value="structure" name="what"></input>';
345 $html .= '<label for="what_structure">' . __('Structure only') . '</label><br>';
346 $html .= '<input type="radio" id ="what_data" value="data" name="what" checked="checked"></input>';
347 $html .= '<label for="what_data">' . __('Structure and data') . '</label><br>';
348 $html .= '<input type="radio" id ="what_dataonly" value="dataonly" name="what"></input>';
349 $html .= '<label for="what_dataonly">' . __('Data only') . '</label><br><br>';
350 $html .= '<input type="checkbox" id="checkbox_drop" value="1" name="drop_if_exists"></input>';
351 $html .= '<label for="checkbox_drop">' . __('Add DROP TABLE') . '</label><br>';
352 $html .= '<input type="checkbox" id="checkbox_auto_increment_cp" value="1" name="sql_auto_increment"></input>';
353 $html .= '<label for="checkbox_auto_increment_cp">' . __('Add AUTO INCREMENT value') . '</label><br>';
354 $html .= '<input type="checkbox" id="checkbox_constraints" value="1" name="sql_auto_increment" checked="checked"></input>';
355 $html .= '<label for="checkbox_constraints">' . __('Add constraints') . '</label><br><br>';
356 $html .= '<input name="adjust_privileges" value="1" id="checkbox_adjust_privileges" checked="checked" type="checkbox"></input>';
357 $html .= '<label for="checkbox_adjust_privileges">' . __('Adjust privileges') . '<a href="./doc/html/faq.html#faq6-39" target="documentation"><img src="themes/dot.gif" title="Documentation" alt="Documentation" class="icon ic_b_help"></a></label>';
358 $html .= '</fieldset>';
359 $html .= '<input type="hidden" name="mult_btn" value="' . __('Yes') . '" />';
365 * Gets HTML for replace_prefix_tbl or copy_tbl_change_prefix
367 * @param string $action action type
368 * @param array $_url_params URL params
372 function PMA_getHtmlForReplacePrefixTable($action, $_url_params)
374 $html = '<form id="ajax_form" action="' . $action . '" method="post">';
375 $html .= URL
::getHiddenInputs($_url_params);
376 $html .= '<fieldset class = "input">';
379 $html .= '<td>' . __('From') . '</td>';
381 $html .= '<input type="text" name="from_prefix" id="initialPrefix" />';
385 $html .= '<td>' . __('To') . '</td>';
387 $html .= '<input type="text" name="to_prefix" id="newPrefix" />';
391 $html .= '</fieldset>';
392 $html .= '<input type="hidden" name="mult_btn" value="' . __('Yes') . '" />';
399 * Gets HTML for add_prefix_tbl
401 * @param string $action action type
402 * @param array $_url_params URL params
406 function PMA_getHtmlForAddPrefixTable($action, $_url_params)
408 $html = '<form id="ajax_form" action="' . $action . '" method="post">';
409 $html .= URL
::getHiddenInputs($_url_params);
410 $html .= '<fieldset class = "input">';
413 $html .= '<td>' . __('Add prefix') . '</td>';
415 $html .= '<input type="text" name="add_prefix" id="txtPrefix" />';
420 $html .= '</fieldset>';
421 $html .= '<input type="hidden" name="mult_btn" value="' . __('Yes') . '" />';
428 * Gets HTML for other mult_submits actions
430 * @param string $what mult_submit type
431 * @param string $action action type
432 * @param array $_url_params URL params
433 * @param string $full_query full sql query string
437 function PMA_getHtmlForOtherActions($what, $action, $_url_params, $full_query)
439 $html = '<form action="' . $action . '" method="post">';
440 $html .= URL
::getHiddenInputs($_url_params);
441 $html .= '<fieldset class="confirmation">';
443 if ($what == 'drop_db') {
444 $html .= __('You are about to DESTROY a complete database!') . ' ';
446 $html .= __('Do you really want to execute the following query?');
447 $html .= '<input type="submit" name="mult_btn" value="'
448 . __('Yes') . '" />';
449 $html .= '<input type="submit" name="mult_btn" value="'
451 $html .= '</legend>';
452 $html .= '<code>' . $full_query . '</code>';
453 $html .= '</fieldset>';
454 $html .= '<fieldset class="tblFooters">';
455 // Display option to disable foreign key checks while dropping tables
456 if ($what === 'drop_tbl' ||
$what === 'empty_tbl' ||
$what === 'row_delete') {
457 $html .= '<div id="foreignkeychk">';
458 $html .= PMA\libraries\Util
::getFKCheckbox();
461 $html .= '<input id="buttonYes" type="submit" name="mult_btn" value="'
462 . __('Yes') . '" />';
463 $html .= '<input id="buttonNo" type="submit" name="mult_btn" value="'
465 $html .= '</fieldset>';
472 * Get query string from Selected
474 * @param string $what mult_submit type
475 * @param string $table table name
476 * @param array $selected the selected columns
477 * @param array $views table views
481 function PMA_getQueryFromSelected($what, $table, $selected, $views)
484 $full_query_views = null;
487 if ($what == 'drop_tbl') {
488 $full_query_views = '';
491 $selected_cnt = count($selected);
493 foreach ($selected as $sval) {
496 $full_query .= 'DELETE FROM '
497 . PMA\libraries\Util
::backquote(htmlspecialchars($table))
498 // Do not append a "LIMIT 1" clause here
499 // (it's not binlog friendly).
500 // We don't need the clause because the calling panel permits
501 // this feature only when there is a unique index.
502 . ' WHERE ' . htmlspecialchars($sval)
506 $full_query .= 'DROP DATABASE '
507 . PMA\libraries\Util
::backquote(htmlspecialchars($sval))
514 if (!empty($views) && in_array($current, $views)) {
515 $full_query_views .= (empty($full_query_views) ?
'DROP VIEW ' : ', ')
516 . PMA\libraries\Util
::backquote(htmlspecialchars($current));
518 $full_query .= (empty($full_query) ?
'DROP TABLE ' : ', ')
519 . PMA\libraries\Util
::backquote(htmlspecialchars($current));
524 $full_query .= 'TRUNCATE ';
525 $full_query .= PMA\libraries\Util
::backquote(htmlspecialchars($sval))
530 if ($full_query == '') {
531 $full_query .= 'ALTER TABLE '
532 . PMA\libraries\Util
::backquote(htmlspecialchars($table))
533 . '<br /> DROP PRIMARY KEY,'
534 . '<br /> ADD PRIMARY KEY('
535 . '<br /> '
536 . PMA\libraries\Util
::backquote(htmlspecialchars($sval))
539 $full_query .= '<br /> '
540 . PMA\libraries\Util
::backquote(htmlspecialchars($sval))
543 if ($i == $selected_cnt-1) {
544 $full_query = preg_replace('@,$@', ');<br />', $full_query);
549 if ($full_query == '') {
550 $full_query .= 'ALTER TABLE '
551 . PMA\libraries\Util
::backquote(htmlspecialchars($table));
553 $full_query .= '<br /> DROP '
554 . PMA\libraries\Util
::backquote(htmlspecialchars($sval))
556 if ($i == $selected_cnt - 1) {
557 $full_query = preg_replace('@,$@', ';<br />', $full_query);
564 if ($what == 'drop_tbl') {
565 if (!empty($full_query)) {
566 $full_query .= ';<br />' . "\n";
568 if (!empty($full_query_views)) {
569 $full_query .= $full_query_views . ';<br />' . "\n";
571 unset($full_query_views);
574 $full_query_views = isset($full_query_views)?
$full_query_views : null;
576 return array($full_query, $reload, $full_query_views);