Translated using Weblate (Estonian)
[phpmyadmin.git] / libraries / mult_submits.lib.php
blob77da42d5af3de4f443950682dfdfa2f02b11f6f3
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * functions for multi submit forms
6 * @usedby mult_submits.inc.php
8 * @package PhpMyAdmin
9 */
10 use PMA\libraries\Table;
11 use PMA\libraries\URL;
13 /**
14 * Gets url params
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
26 * @return array
28 function PMA_getUrlParams(
29 $what, $reload, $action, $db, $table, $selected, $views,
30 $original_sql_query, $original_url_query
31 ) {
32 $_url_params = array(
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'
40 ) {
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;';
49 } else {
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;
65 return $_url_params;
68 /**
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
80 * @return array
82 function PMA_buildOrExecuteQueryForMulti(
83 $query_type, $selected, $db, $table, $views, $primary,
84 $from_prefix, $to_prefix
85 ) {
86 $rebuild_database_list = false;
87 $reload = null;
88 $a_query = null;
89 $sql_query = '';
90 $sql_query_views = null;
91 // whether to run query after each pass
92 $run_parts = false;
93 // whether to execute the query at the end (to display results)
94 $execute_query_later = false;
95 $result = null;
97 if ($query_type == 'drop_tbl') {
98 $sql_query_views = '';
101 $selected_cnt = count($selected);
102 $deletes = false;
103 $copy_tbl =false;
105 for ($i = 0; $i < $selected_cnt; $i++) {
106 switch ($query_type) {
107 case 'row_delete':
108 $deletes = true;
109 $a_query = $selected[$i];
110 $run_parts = true;
111 break;
113 case 'drop_db':
114 PMA_relationsCleanupDatabase($selected[$i]);
115 $a_query = 'DROP DATABASE '
116 . PMA\libraries\Util::backquote($selected[$i]);
117 $reload = 1;
118 $run_parts = true;
119 $rebuild_database_list = true;
120 break;
122 case 'drop_tbl':
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);
128 } else {
129 $sql_query .= (empty($sql_query) ? 'DROP TABLE ' : ', ')
130 . PMA\libraries\Util::backquote($current);
132 $reload = 1;
133 break;
135 case 'check_tbl':
136 $sql_query .= (empty($sql_query) ? 'CHECK TABLE ' : ', ')
137 . PMA\libraries\Util::backquote($selected[$i]);
138 $execute_query_later = true;
139 break;
141 case 'optimize_tbl':
142 $sql_query .= (empty($sql_query) ? 'OPTIMIZE TABLE ' : ', ')
143 . PMA\libraries\Util::backquote($selected[$i]);
144 $execute_query_later = true;
145 break;
147 case 'analyze_tbl':
148 $sql_query .= (empty($sql_query) ? 'ANALYZE TABLE ' : ', ')
149 . PMA\libraries\Util::backquote($selected[$i]);
150 $execute_query_later = true;
151 break;
153 case 'checksum_tbl':
154 $sql_query .= (empty($sql_query) ? 'CHECKSUM TABLE ' : ', ')
155 . PMA\libraries\Util::backquote($selected[$i]);
156 $execute_query_later = true;
157 break;
159 case 'repair_tbl':
160 $sql_query .= (empty($sql_query) ? 'REPAIR TABLE ' : ', ')
161 . PMA\libraries\Util::backquote($selected[$i]);
162 $execute_query_later = true;
163 break;
165 case 'empty_tbl':
166 $deletes = true;
167 $a_query = 'TRUNCATE ';
168 $a_query .= PMA\libraries\Util::backquote($selected[$i]);
169 $run_parts = true;
170 break;
172 case 'drop_fld':
173 PMA_relationsCleanupColumn($db, $table, $selected[$i]);
174 $sql_query .= (empty($sql_query)
175 ? 'ALTER TABLE ' . PMA\libraries\Util::backquote($table)
176 : ',')
177 . ' DROP ' . PMA\libraries\Util::backquote($selected[$i])
178 . (($i == $selected_cnt-1) ? ';' : '');
179 break;
181 case 'primary_fld':
182 $sql_query .= (empty($sql_query)
183 ? 'ALTER TABLE ' . PMA\libraries\Util::backquote($table)
184 . (empty($primary)
185 ? ''
186 : ' DROP PRIMARY KEY,') . ' ADD PRIMARY KEY( '
187 : ', ')
188 . PMA\libraries\Util::backquote($selected[$i])
189 . (($i == $selected_cnt-1) ? ');' : '');
190 break;
192 case 'index_fld':
193 $sql_query .= (empty($sql_query)
194 ? 'ALTER TABLE ' . PMA\libraries\Util::backquote($table)
195 . ' ADD INDEX( '
196 : ', ')
197 . PMA\libraries\Util::backquote($selected[$i])
198 . (($i == $selected_cnt-1) ? ');' : '');
199 break;
201 case 'unique_fld':
202 $sql_query .= (empty($sql_query)
203 ? 'ALTER TABLE ' . PMA\libraries\Util::backquote($table)
204 . ' ADD UNIQUE( '
205 : ', ')
206 . PMA\libraries\Util::backquote($selected[$i])
207 . (($i == $selected_cnt-1) ? ');' : '');
208 break;
210 case 'spatial_fld':
211 $sql_query .= (empty($sql_query)
212 ? 'ALTER TABLE ' . PMA\libraries\Util::backquote($table)
213 . ' ADD SPATIAL( '
214 : ', ')
215 . PMA\libraries\Util::backquote($selected[$i])
216 . (($i == $selected_cnt-1) ? ');' : '');
217 break;
219 case 'fulltext_fld':
220 $sql_query .= (empty($sql_query)
221 ? 'ALTER TABLE ' . PMA\libraries\Util::backquote($table)
222 . ' ADD FULLTEXT( '
223 : ', ')
224 . PMA\libraries\Util::backquote($selected[$i])
225 . (($i == $selected_cnt-1) ? ');' : '');
226 break;
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])
233 . ' RENAME '
234 . PMA\libraries\Util::backquote($newtablename);
235 $run_parts = true;
236 break;
238 case 'replace_prefix_tbl':
239 $current = $selected[$i];
240 $subFromPrefix = mb_substr(
241 $current,
243 mb_strlen($from_prefix)
245 if ($subFromPrefix == $from_prefix) {
246 $newtablename = $to_prefix
247 . mb_substr(
248 $current,
249 mb_strlen($from_prefix)
251 } else {
252 $newtablename = $current;
254 // CHANGE PREFIX PATTERN
255 $a_query = 'ALTER TABLE '
256 . PMA\libraries\Util::backquote($selected[$i])
257 . ' RENAME '
258 . PMA\libraries\Util::backquote($newtablename);
259 $run_parts = true;
260 break;
262 case 'copy_tbl_change_prefix':
263 $run_parts = true;
264 $copy_tbl = true;
266 $current = $selected[$i];
267 $newtablename = $to_prefix .
268 mb_substr($current, mb_strlen($from_prefix));
270 // COPY TABLE AND CHANGE PREFIX PATTERN
271 Table::moveCopy(
272 $db, $current, $db, $newtablename,
273 'data', false, 'one_table'
275 break;
277 case 'copy_tbl':
278 $run_parts = true;
279 $copy_tbl = true;
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]);
285 break;
286 } // end switch
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]);
304 } // end if
305 } // end for
307 if ($deletes && ! empty($_REQUEST['pos'])) {
308 $_REQUEST['pos'] = PMA_calculatePosForLastPage(
309 $db, $table, isset($_REQUEST['pos']) ? $_REQUEST['pos'] : null
313 return array(
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
325 * @return string
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);
336 break;
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)
341 . '</select>';
342 $html .= '<br><br>';
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') . '" />';
360 $html .= '</form>';
361 return $html;
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
370 * @return string
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">';
377 $html .= '<table>';
378 $html .= '<tr>';
379 $html .= '<td>' . __('From') . '</td>';
380 $html .= '<td>';
381 $html .= '<input type="text" name="from_prefix" id="initialPrefix" />';
382 $html .= '</td>';
383 $html .= '</tr>';
384 $html .= '<tr>';
385 $html .= '<td>' . __('To') . '</td>';
386 $html .= '<td>';
387 $html .= '<input type="text" name="to_prefix" id="newPrefix" />';
388 $html .= '</td>';
389 $html .= '</tr>';
390 $html .= '</table>';
391 $html .= '</fieldset>';
392 $html .= '<input type="hidden" name="mult_btn" value="' . __('Yes') . '" />';
393 $html .= '</form>';
395 return $html;
399 * Gets HTML for add_prefix_tbl
401 * @param string $action action type
402 * @param array $_url_params URL params
404 * @return string
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">';
411 $html .= '<table>';
412 $html .= '<tr>';
413 $html .= '<td>' . __('Add prefix') . '</td>';
414 $html .= '<td>';
415 $html .= '<input type="text" name="add_prefix" id="txtPrefix" />';
416 $html .= '</td>';
417 $html .= '</tr>';
418 $html .= '<tr>';
419 $html .= '</table>';
420 $html .= '</fieldset>';
421 $html .= '<input type="hidden" name="mult_btn" value="' . __('Yes') . '" />';
422 $html .= '</form>';
424 return $html;
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
435 * @return 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">';
442 $html .= '<legend>';
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="'
450 . __('No') . '" />';
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();
459 $html .= '</div>';
461 $html .= '<input id="buttonYes" type="submit" name="mult_btn" value="'
462 . __('Yes') . '" />';
463 $html .= '<input id="buttonNo" type="submit" name="mult_btn" value="'
464 . __('No') . '" />';
465 $html .= '</fieldset>';
466 $html .= '</form>';
468 return $html;
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
479 * @return array
481 function PMA_getQueryFromSelected($what, $table, $selected, $views)
483 $reload = false;
484 $full_query_views = null;
485 $full_query = '';
487 if ($what == 'drop_tbl') {
488 $full_query_views = '';
491 $selected_cnt = count($selected);
492 $i = 0;
493 foreach ($selected as $sval) {
494 switch ($what) {
495 case 'row_delete':
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)
503 . ';<br />';
504 break;
505 case 'drop_db':
506 $full_query .= 'DROP DATABASE '
507 . PMA\libraries\Util::backquote(htmlspecialchars($sval))
508 . ';<br />';
509 $reload = true;
510 break;
512 case 'drop_tbl':
513 $current = $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));
517 } else {
518 $full_query .= (empty($full_query) ? 'DROP TABLE ' : ', ')
519 . PMA\libraries\Util::backquote(htmlspecialchars($current));
521 break;
523 case 'empty_tbl':
524 $full_query .= 'TRUNCATE ';
525 $full_query .= PMA\libraries\Util::backquote(htmlspecialchars($sval))
526 . ';<br />';
527 break;
529 case 'primary_fld':
530 if ($full_query == '') {
531 $full_query .= 'ALTER TABLE '
532 . PMA\libraries\Util::backquote(htmlspecialchars($table))
533 . '<br />&nbsp;&nbsp;DROP PRIMARY KEY,'
534 . '<br />&nbsp;&nbsp; ADD PRIMARY KEY('
535 . '<br />&nbsp;&nbsp;&nbsp;&nbsp; '
536 . PMA\libraries\Util::backquote(htmlspecialchars($sval))
537 . ',';
538 } else {
539 $full_query .= '<br />&nbsp;&nbsp;&nbsp;&nbsp; '
540 . PMA\libraries\Util::backquote(htmlspecialchars($sval))
541 . ',';
543 if ($i == $selected_cnt-1) {
544 $full_query = preg_replace('@,$@', ');<br />', $full_query);
546 break;
548 case 'drop_fld':
549 if ($full_query == '') {
550 $full_query .= 'ALTER TABLE '
551 . PMA\libraries\Util::backquote(htmlspecialchars($table));
553 $full_query .= '<br />&nbsp;&nbsp;DROP '
554 . PMA\libraries\Util::backquote(htmlspecialchars($sval))
555 . ',';
556 if ($i == $selected_cnt - 1) {
557 $full_query = preg_replace('@,$@', ';<br />', $full_query);
559 break;
560 } // end switch
561 $i++;
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);