1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
|
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* functions for multi submit forms
*
* @usedby mult_submits.inc.php
*
* @package PhpMyAdmin
*/
if (! defined('PHPMYADMIN')) {
exit;
}
/**
* Gets url params
*
* @param string $what mult submit type
* @param bool $reload is reload
* @param string $action action type
* @param string $db database name
* @param string $table table name
* @param array $selected selected rows(table,db)
* @param array $views table views
* @param string $original_sql_query original sql query
* @param string $original_url_query original url query
*
* @return array
*/
function PMA_getUrlParams(
$what, $reload, $action, $db, $table, $selected, $views,
$original_sql_query, $original_url_query
) {
$_url_params = array(
'query_type' => $what,
'reload' => (! empty($reload) ? 1 : 0),
);
if (strpos(' ' . $action, 'db_') == 1) {
$_url_params['db']= $db;
} elseif (strpos(' ' . $action, 'tbl_') == 1 || $what == 'row_delete') {
$_url_params['db']= $db;
$_url_params['table']= $table;
}
foreach ($selected as $sval) {
if ($what == 'row_delete') {
$_url_params['selected'][] = 'DELETE FROM '
. PMA_Util::backquote($db) . '.' . PMA_Util::backquote($table)
. ' WHERE ' . urldecode($sval) . ' LIMIT 1;';
} else {
$_url_params['selected'][] = $sval;
}
}
if ($what == 'drop_tbl' && !empty($views)) {
foreach ($views as $current) {
$_url_params['views'][] = $current;
}
}
if ($what == 'row_delete') {
$_url_params['original_sql_query'] = $original_sql_query;
if (! empty($original_url_query)) {
$_url_params['original_url_query'] = $original_url_query;
}
}
return $_url_params;
}
/**
* Gets query results from
*
* @param string $query_type query type
* @param array $selected selected tables
* @param string $db db name
* @param string $table table name
* @param string $views table views
* @param string $primary table primary
* @param string $from_prefix from prefix original
* @param string $to_prefix to prefix original
*
* @return array
*/
function PMA_getQueryStrFromSelected(
$query_type, $selected, $db, $table, $views, $primary,
$from_prefix, $to_prefix
) {
$rebuild_database_list = false;
$reload = null;
$a_query = null;
$sql_query = '';
$sql_query_views = null;
// whether to run query after each pass
$run_parts = false;
// whether to execute the query at the end (to display results)
$use_sql = false;
$result = null;
if ($query_type == 'drop_tbl') {
$sql_query_views = '';
}
$selected_cnt = count($selected);
$deletes = false;
for ($i = 0; $i < $selected_cnt; $i++) {
switch ($query_type) {
case 'row_delete':
$deletes = true;
$a_query = $selected[$i];
$run_parts = true;
break;
case 'drop_db':
PMA_relationsCleanupDatabase($selected[$i]);
$a_query = 'DROP DATABASE '
. PMA_Util::backquote($selected[$i]);
$reload = 1;
$run_parts = true;
$rebuild_database_list = true;
break;
case 'drop_tbl':
PMA_relationsCleanupTable($db, $selected[$i]);
$current = $selected[$i];
if (!empty($views) && in_array($current, $views)) {
$sql_query_views .= (empty($sql_query_views) ? 'DROP VIEW ' : ', ')
. PMA_Util::backquote($current);
} else {
$sql_query .= (empty($sql_query) ? 'DROP TABLE ' : ', ')
. PMA_Util::backquote($current);
}
$reload = 1;
break;
case 'check_tbl':
$sql_query .= (empty($sql_query) ? 'CHECK TABLE ' : ', ')
. PMA_Util::backquote($selected[$i]);
$use_sql = true;
break;
case 'optimize_tbl':
$sql_query .= (empty($sql_query) ? 'OPTIMIZE TABLE ' : ', ')
. PMA_Util::backquote($selected[$i]);
$use_sql = true;
break;
case 'analyze_tbl':
$sql_query .= (empty($sql_query) ? 'ANALYZE TABLE ' : ', ')
. PMA_Util::backquote($selected[$i]);
$use_sql = true;
break;
case 'repair_tbl':
$sql_query .= (empty($sql_query) ? 'REPAIR TABLE ' : ', ')
. PMA_Util::backquote($selected[$i]);
$use_sql = true;
break;
case 'empty_tbl':
$deletes = true;
$a_query = 'TRUNCATE ';
$a_query .= PMA_Util::backquote($selected[$i]);
$run_parts = true;
break;
case 'drop_fld':
PMA_relationsCleanupColumn($db, $table, $selected[$i]);
$sql_query .= (empty($sql_query)
? 'ALTER TABLE ' . PMA_Util::backquote($table)
: ',')
. ' DROP ' . PMA_Util::backquote($selected[$i])
. (($i == $selected_cnt-1) ? ';' : '');
break;
case 'primary_fld':
$sql_query .= (empty($sql_query)
? 'ALTER TABLE ' . PMA_Util::backquote($table) . (empty($primary)
? ''
: ' DROP PRIMARY KEY,') . ' ADD PRIMARY KEY( '
: ', ')
. PMA_Util::backquote($selected[$i])
. (($i == $selected_cnt-1) ? ');' : '');
break;
case 'index_fld':
$sql_query .= (empty($sql_query)
? 'ALTER TABLE ' . PMA_Util::backquote($table) . ' ADD INDEX( '
: ', ')
. PMA_Util::backquote($selected[$i])
. (($i == $selected_cnt-1) ? ');' : '');
break;
case 'unique_fld':
$sql_query .= (empty($sql_query)
? 'ALTER TABLE ' . PMA_Util::backquote($table) . ' ADD UNIQUE( '
: ', ')
. PMA_Util::backquote($selected[$i])
. (($i == $selected_cnt-1) ? ');' : '');
break;
case 'spatial_fld':
$sql_query .= (empty($sql_query)
? 'ALTER TABLE ' . PMA_Util::backquote($table) . ' ADD SPATIAL( '
: ', ')
. PMA_Util::backquote($selected[$i])
. (($i == $selected_cnt-1) ? ');' : '');
break;
case 'fulltext_fld':
$sql_query .= (empty($sql_query)
? 'ALTER TABLE ' . PMA_Util::backquote($table) . ' ADD FULLTEXT( '
: ', ')
. PMA_Util::backquote($selected[$i])
. (($i == $selected_cnt-1) ? ');' : '');
break;
case 'add_prefix_tbl':
$newtablename = $_POST['add_prefix'] . $selected[$i];
// ADD PREFIX TO TABLE NAME
$a_query = 'ALTER TABLE '
. PMA_Util::backquote($selected[$i])
. ' RENAME '
. PMA_Util::backquote($newtablename);
$run_parts = true;
break;
case 'replace_prefix_tbl':
$current = $selected[$i];
if (substr($current, 0, strlen($from_prefix)) == $from_prefix) {
$newtablename = $to_prefix . substr($current, strlen($from_prefix));
} else {
$newtablename = $current;
}
// CHANGE PREFIX PATTERN
$a_query = 'ALTER TABLE '
. PMA_Util::backquote($selected[$i])
. ' RENAME '
. PMA_Util::backquote($newtablename);
$run_parts = true;
break;
case 'copy_tbl_change_prefix':
$current = $selected[$i];
if (substr($current, 0, strlen($from_prefix)) == $from_prefix) {
$newtablename = $to_prefix . substr($current, strlen($from_prefix));
} else {
$newtablename = $current;
}
$newtablename = $to_prefix . substr($current, strlen($from_prefix));
// COPY TABLE AND CHANGE PREFIX PATTERN
$a_query = 'CREATE TABLE '
. PMA_Util::backquote($newtablename)
. ' SELECT * FROM '
. PMA_Util::backquote($selected[$i]);
$run_parts = true;
break;
} // end switch
// All "DROP TABLE", "DROP FIELD", "OPTIMIZE TABLE" and "REPAIR TABLE"
// statements will be run at once below
if ($run_parts) {
$sql_query .= $a_query . ';' . "\n";
if ($query_type != 'drop_db') {
$GLOBALS['dbi']->selectDb($db);
}
$result = $GLOBALS['dbi']->query($a_query);
if ($query_type == 'drop_db') {
PMA_clearTransformations($selected[$i]);
} elseif ($query_type == 'drop_tbl') {
PMA_clearTransformations($db, $selected[$i]);
} else if ($query_type == 'drop_fld') {
PMA_clearTransformations($db, $table, $selected[$i]);
}
} // end if
} // end for
if ($deletes && ! empty($_REQUEST['pos'])) {
$_REQUEST['pos'] = PMA_calculatePosForLastPage(
$db, $table, $_REQUEST['pos']
);
}
return array(
$result, $rebuild_database_list, $reload,
$run_parts, $use_sql, $sql_query, $sql_query_views
);
}
/**
* Gets table primary key
*
* @param string $db name of db
* @param string $table name of table
*
* @return string
*/
function PMA_getKeyForTablePrimary($db, $table)
{
$GLOBALS['dbi']->selectDb($db);
$result = $GLOBALS['dbi']->query(
'SHOW KEYS FROM ' . PMA_Util::backquote($table) . ';'
);
$primary = '';
while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
// Backups the list of primary keys
if ($row['Key_name'] == 'PRIMARY') {
$primary .= $row['Column_name'] . ', ';
}
} // end while
$GLOBALS['dbi']->freeResult($result);
return $primary;
}
/**
* Gets HTML for replace_prefix_tbl or copy_tbl_change_prefix
*
* @param string $what mult_submit type
* @param string $action action type
* @param array $_url_params URL params
*
* @return string
*/
function PMA_getHtmlForReplacePrefixTable($what, $action, $_url_params)
{
$html = '<form action="' . $action . '" method="post">';
$html .= PMA_URL_getHiddenInputs($_url_params);
$html .= '<fieldset class = "input">';
$html .= '<legend>';
if ($what == 'replace_prefix_tbl') {
$html .= __('Replace table prefix:');
} else {
$html .= __('Copy table with prefix:');
}
$html .= '</legend>';
$html .= '<table>';
$html .= '<tr>';
$html .= '<td>' . __('From') . '</td>';
$html .= '<td>';
$html .= '<input type="text" name="from_prefix" id="initialPrefix" />';
$html .= '</td>';
$html .= '</tr>';
$html .= '<tr>';
$html .= '<td>' . __('To') . '</td>';
$html .= '<td>';
$html .= '<input type="text" name="to_prefix" id="newPrefix" />';
$html .= '</td>';
$html .= '</tr>';
$html .= '</table>';
$html .= '</fieldset>';
$html .= '<fieldset class="tblFooters">';
$html .= '<input type="hidden" name="mult_btn" value="' . __('Yes') . '" />';
$html .= '<input type="submit" value="' . __('Submit') . '" id="buttonYes" />';
$html .= '</fieldset>';
$html .= '</form>';
return $html;
}
/**
* Gets HTML for add_prefix_tbl
*
* @param string $action action type
* @param array $_url_params URL params
*
* @return string
*/
function PMA_getHtmlForAddPrefixTable($action, $_url_params)
{
$html = '<form action="' . $action . '" method="post">';
$html .= PMA_URL_getHiddenInputs($_url_params);
$html .= '<fieldset class = "input">';
$html .= '<legend>' . __('Add table prefix:') . '</legend>';
$html .= '<table>';
$html .= '<tr>';
$html .= '<td>' . __('Add prefix') . '</td>';
$html .= '<td>';
$html .= '<input type="text" name="add_prefix" id="txtPrefix" />';
$html .= '</td>';
$html .= '</tr>';
$html .= '<tr>';
$html .= '</table>';
$html .= '</fieldset>';
$html .= '<fieldset class="tblFooters">';
$html .= '<input type="hidden" name="mult_btn" value="' . __('Yes') . '" />';
$html .= '<input type="submit" value="' . __('Submit') . '" id="buttonYes" />';
$html .= '</fieldset>';
$html .= '</form>';
return $html;
}
/**
* Gets HTML for other mult_submits actions
*
* @param string $what mult_submit type
* @param string $action action type
* @param array $_url_params URL params
* @param array $full_query full sql query string
*
* @return string
*/
function PMA_getHtmlForOtherActions($what, $action, $_url_params, $full_query)
{
$html = '<fieldset class="confirmation">';
$html .= '<legend>';
if ($what == 'drop_db') {
$html .= __('You are about to DESTROY a complete database!') . ' ';
}
$html .= __('Do you really want to execute the following query?');
if ($what == 'row_delete') {
$response = array('Yes','No');
foreach ($response as $resp) {
$html .= '<form action="' . $action . '" method="post">';
$html .= PMA_URL_getHiddenInputs($_url_params);
$html .= '<input type="hidden" name="mult_btn" value="'
. __($resp) . '" />';
$html .= '<input type="submit" value="' . __($resp) . '" />';
$html .= '</form>';
}
}
$html .= '</legend>';
$html .= '<code>' . $full_query . '</code>';
$html .= '</fieldset>';
$html .= '<fieldset class="tblFooters">';
$html .= '<form action="' . $action . '" method="post">';
$html .= PMA_URL_getHiddenInputs($_url_params);
// Display option to disable foreign key checks while dropping tables
if ($what == 'drop_tbl') {
$html .= '<div id="foreignkeychk">';
$html .= '<span class="fkc_switch">';
$html .= __('Foreign key check:');
$html .= '</span>';
$html .= '<span class="checkbox">';
$html .= '<input type="checkbox" name="fk_check" value="1" '
. 'id="fkc_checkbox"';
$default_fk_check_value = $GLOBALS['dbi']->fetchValue(
'SHOW VARIABLES LIKE \'foreign_key_checks\';', 0, 1
) == 'ON';
if ($default_fk_check_value) {
$html .= ' checked="checked"';
}
$html .= '/></span>';
$html .= '<span id="fkc_status" class="fkc_switch">';
$html .= ($default_fk_check_value) ? __('(Enabled)') : __('(Disabled)');
$html .= '</span>';
$html .= '</div>';
}
$html .= '<input type="hidden" name="mult_btn" value="' . __('Yes') . '" />';
$html .= '<input type="submit" value="' . __('Yes') . '" id="buttonYes" />';
$html .= '</form>';
$html .= '<form action="' . $action . '" method="post">';
$html .= PMA_URL_getHiddenInputs($_url_params);
$html .= '<input type="hidden" name="mult_btn" value="' . __('No') . '" />';
$html .= '<input type="submit" value="' . __('No') . '" id="buttonNo" />';
$html .= '</form>';
$html .= '</fieldset>';
return $html;
}
/**
* Get List of information for Submit Mult
*
* @param string $submit_mult mult_submit type
* @param string $db dtabase name
* @param array $table table name
* @param array $selected the selected columns
* @param array $action action type
*
* @return array()
*/
function PMA_getDataForSubmitMult($submit_mult, $db, $table, $selected, $action)
{
$what = null;
$query_type = null;
$is_unset_submit_mult = false;
$mult_btn = null;
switch ($submit_mult) {
case 'drop':
$what = 'drop_fld';
break;
case 'primary':
// Gets table primary key
$primary = PMA_getKeyForTablePrimary($db, $table);
if (empty($primary)) {
// no primary key, so we can safely create new
$is_unset_submit_mult = true;
$query_type = 'primary_fld';
$mult_btn = __('Yes');
} else {
// primary key exists, so lets as user
$what = 'primary_fld';
}
break;
case 'index':
$is_unset_submit_mult = true;
$query_type = 'index_fld';
$mult_btn = __('Yes');
break;
case 'unique':
$is_unset_submit_mult = true;
$query_type = 'unique_fld';
$mult_btn = __('Yes');
break;
case 'spatial':
$is_unset_submit_mult = true;
$query_type = 'spatial_fld';
$mult_btn = __('Yes');
break;
case 'ftext':
$is_unset_submit_mult = true;
$query_type = 'fulltext_fld';
$mult_btn = __('Yes');
break;
case 'change':
PMA_displayHtmlForColumnChange($db, $table, $selected, $action);
// execution stops here but PMA_Response correctly finishes
// the rendering
exit;
case 'browse':
// this should already be handled by tbl_structure.php
}
return array($what, $query_type, $is_unset_submit_mult, $mult_btn);
}
/**
* Get query string from Selected
*
* @param string $what mult_submit type
* @param string $db dtabase name
* @param array $table table name
* @param array $selected the selected columns
* @param array $action action type
* @param array $views table views
*
* @return array()
*/
function PMA_getQueryFromSelected($what, $db, $table, $selected, $action, $views)
{
$reload = null;
$full_query_views = null;
$full_query = '';
if ($what == 'drop_tbl') {
$full_query_views = '';
}
$selected_cnt = count($selected);
$i = 0;
foreach ($selected as $sval) {
switch ($what) {
case 'row_delete':
$full_query .= 'DELETE FROM '
. PMA_Util::backquote(htmlspecialchars($db))
. '.' . PMA_Util::backquote(htmlspecialchars($table))
// Do not append a "LIMIT 1" clause here
// (it's not binlog friendly).
// We don't need the clause because the calling panel permits
// this feature only when there is a unique index.
. ' WHERE ' . urldecode(htmlspecialchars($sval))
. ';<br />';
break;
case 'drop_db':
$full_query .= 'DROP DATABASE '
. PMA_Util::backquote(htmlspecialchars($sval))
. ';<br />';
$reload = 1;
break;
case 'drop_tbl':
$current = $sval;
if (!empty($views) && in_array($current, $views)) {
$full_query_views .= (empty($full_query_views) ? 'DROP VIEW ' : ', ')
. PMA_Util::backquote(htmlspecialchars($current));
} else {
$full_query .= (empty($full_query) ? 'DROP TABLE ' : ', ')
. PMA_Util::backquote(htmlspecialchars($current));
}
break;
case 'empty_tbl':
$full_query .= 'TRUNCATE ';
$full_query .= PMA_Util::backquote(htmlspecialchars($sval))
. ';<br />';
break;
case 'primary_fld':
if ($full_query == '') {
$full_query .= 'ALTER TABLE '
. PMA_Util::backquote(htmlspecialchars($table))
. '<br /> DROP PRIMARY KEY,'
. '<br /> ADD PRIMARY KEY('
. '<br /> '
. PMA_Util::backquote(htmlspecialchars($sval))
. ',';
} else {
$full_query .= '<br /> '
. PMA_Util::backquote(htmlspecialchars($sval))
. ',';
}
if ($i == $selected_cnt-1) {
$full_query = preg_replace('@,$@', ');<br />', $full_query);
}
break;
case 'drop_fld':
if ($full_query == '') {
$full_query .= 'ALTER TABLE '
. PMA_Util::backquote(htmlspecialchars($table));
}
$full_query .= '<br /> DROP '
. PMA_Util::backquote(htmlspecialchars($sval))
. ',';
if ($i == $selected_cnt - 1) {
$full_query = preg_replace('@,$@', ';<br />', $full_query);
}
break;
} // end switch
$i++;
}
if ($what == 'drop_tbl') {
if (!empty($full_query)) {
$full_query .= ';<br />' . "\n";
}
if (!empty($full_query_views)) {
$full_query .= $full_query_views . ';<br />' . "\n";
}
unset($full_query_views);
}
$full_query_views = isset($full_query_views)? $full_query_views : null;
return array($full_query, $reload, $full_query_views);
}
?>
|