[ Index ]
 

Code source de phpMyAdmin 2.10.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/ -> db_operations.php (source)

   1  <?php
   2  /* $Id: db_operations.php 9696 2006-11-13 08:30:25Z nijel $ */
   3  // vim: expandtab sw=4 ts=4 sts=4:
   4  
   5  /**
   6   * handles miscellaneous db operations:
   7   *  - move/rename
   8   *  - copy
   9   *  - changing collation
  10   *  - changing comment
  11   *  - adding tables
  12   *  - viewing PDF schemas
  13   */
  14  
  15  /**
  16   * requirements
  17   */
  18  require_once  './libraries/common.lib.php';
  19  require_once  './libraries/Table.class.php';
  20  require_once  './libraries/mysql_charsets.lib.php';
  21  
  22  /**
  23   * Rename/move or copy database
  24   */
  25  if (isset($db) &&
  26      ((isset($db_rename) && $db_rename == 'true') ||
  27      (isset($db_copy) && $db_copy == 'true'))) {
  28  
  29      if (isset($db_rename) && $db_rename == 'true') {
  30          $move = true;
  31      } else {
  32          $move = false;
  33      }
  34  
  35      if (!isset($newname) || !strlen($newname)) {
  36          $message = $strDatabaseEmpty;
  37      } else {
  38          $sql_query = ''; // in case target db exists
  39          if ($move ||
  40             (isset($create_database_before_copying) && $create_database_before_copying)) {
  41              $local_query = 'CREATE DATABASE ' . PMA_backquote($newname);
  42              if (isset($db_collation)) {
  43                  $local_query .= ' DEFAULT' . PMA_generateCharsetQueryPart($db_collation);
  44              }
  45              $local_query .= ';';
  46              $sql_query = $local_query;
  47              PMA_DBI_query($local_query);
  48              // rebuild the database list because PMA_Table::moveCopy
  49              // checks in this list if the target db exists
  50              $GLOBALS['PMA_List_Database']->build();
  51          }
  52  
  53          if (isset($GLOBALS['add_constraints'])) {
  54              $GLOBALS['sql_constraints_query_full_db'] = '';
  55          }
  56  
  57          $tables_full = PMA_DBI_get_tables_full($db);
  58          $views = array();
  59          foreach ($tables_full as $table => $tmp) {
  60              // to be able to rename a db containing views, we
  61              // first collect in $views all the views we find and we
  62              // will handle them after the tables
  63              /**
  64               * @todo support a view of a view
  65               */
  66              if (PMA_Table::isView($db, $table)) {
  67                  $views[] = $table;
  68                  continue;
  69              }
  70  
  71              $back = $sql_query;
  72              $sql_query = '';
  73  
  74              // value of $what for this table only
  75              $this_what = $what;
  76  
  77              if (!isset($tables_full[$table]['Engine'])) {
  78                  $tables_full[$table]['Engine'] = $tables_full[$table]['Type'];
  79              }
  80              // do not copy the data from a Merge table
  81              // note: on the calling FORM, 'data' means 'structure and data'
  82              if ($tables_full[$table]['Engine'] == 'MRG_MyISAM') {
  83                  if ($this_what == 'data') {
  84                      $this_what = 'structure';
  85                  }
  86                  if ($this_what == 'dataonly') {
  87                      $this_what = 'nocopy';
  88                  }
  89              }
  90  
  91              if ($this_what != 'nocopy') {
  92                  PMA_Table::moveCopy($db, $table, $newname, $table,
  93                      isset($this_what) ? $this_what : 'data', $move, 'db_copy');
  94                  if (isset($GLOBALS['add_constraints'])) {
  95                      $GLOBALS['sql_constraints_query_full_db'] .= $GLOBALS['sql_constraints_query'];
  96                      unset($GLOBALS['sql_constraints_query']);
  97                  }
  98              }
  99  
 100              $sql_query = $back . $sql_query;
 101          } // end (foreach)
 102          unset($table);
 103  
 104          // handle the views
 105          foreach ($views as $view) {
 106              PMA_Table::moveCopy($db, $view, $newname, $view,
 107                  'structure', $move, 'db_copy');
 108          }
 109          unset($view, $views);
 110  
 111          // now that all tables exist, create all the accumulated constraints
 112          if (isset($GLOBALS['add_constraints'])) {
 113              /**
 114               * @todo this works with mysqli but not with mysql, because
 115               * mysql extension does not accept more than one statement; maybe
 116               * interface with the sql import plugin that handles statement delimiter
 117               */
 118              PMA_DBI_query($GLOBALS['sql_constraints_query_full_db']);
 119  
 120              // and prepare to display them
 121              $GLOBALS['sql_query'] .= "\n" . $GLOBALS['sql_constraints_query_full_db'];
 122              unset($GLOBALS['sql_constraints_query_full_db']);
 123          }
 124  
 125          // Duplicate the bookmarks for this db (done once for each db)
 126          if ($db != $newname) {
 127              $get_fields = array('user', 'label', 'query');
 128              $where_fields = array('dbase' => $db);
 129              $new_fields = array('dbase' => $newname);
 130              PMA_Table::duplicateInfo('bookmarkwork', 'bookmark', $get_fields,
 131                  $where_fields, $new_fields);
 132          }
 133  
 134          if ($move) {
 135              // cleanup pmadb stuff for this db
 136              require_once  './libraries/relation_cleanup.lib.php';
 137              PMA_relationsCleanupDatabase($db);
 138  
 139              $local_query = 'DROP DATABASE ' . PMA_backquote($db) . ';';
 140              $sql_query .= "\n" . $local_query;
 141              PMA_DBI_query($local_query);
 142              $message    = sprintf($strRenameDatabaseOK, htmlspecialchars($db),
 143                  htmlspecialchars($newname));
 144          } else {
 145              $message    = sprintf($strCopyDatabaseOK, htmlspecialchars($db),
 146                  htmlspecialchars($newname));
 147          }
 148          $reload     = true;
 149  
 150          /* Change database to be used */
 151          if ($move) {
 152              $db         = $newname;
 153          } else {
 154              if (isset($switch_to_new) && $switch_to_new == 'true') {
 155                  PMA_setCookie('pma_switch_to_new', 'true');
 156                  $db         = $newname;
 157              } else {
 158                  PMA_setCookie('pma_switch_to_new', '');
 159              }
 160          }
 161      }
 162  }
 163  /**
 164   * Settings for relations stuff
 165   */
 166  
 167  require_once  './libraries/relation.lib.php';
 168  $cfgRelation = PMA_getRelationsParam();
 169  
 170  /**
 171   * Check if comments were updated
 172   * (must be done before displaying the menu tabs)
 173   */
 174  if ($cfgRelation['commwork'] && isset($db_comment) && $db_comment == 'true') {
 175      PMA_SetComment($db, '', '(db_comment)', $comment);
 176  }
 177  
 178  /**
 179   * Prepares the tables list if the user where not redirected to this script
 180   * because there is no table in the database ($is_info is true)
 181   */
 182  if (empty($is_info)) {
 183      require  './libraries/db_common.inc.php';
 184      $url_query .= '&amp;goto=db_operations.php';
 185  
 186      // Gets the database structure
 187      $sub_part = '_structure';
 188      require  './libraries/db_info.inc.php';
 189      echo "\n";
 190  }
 191  
 192  if (PMA_MYSQL_INT_VERSION >= 40101) {
 193      $db_collation = PMA_getDbCollation($db);
 194  }
 195  if (PMA_MYSQL_INT_VERSION < 50002
 196    || (PMA_MYSQL_INT_VERSION >= 50002 && $db != 'information_schema')) {
 197      $is_information_schema = false;
 198  } else {
 199      $is_information_schema = true;
 200  }
 201  
 202  if (!$is_information_schema) {
 203  
 204      require  './libraries/display_create_table.lib.php';
 205  
 206      if ($cfgRelation['commwork']) {
 207          /**
 208           * database comment
 209           */
 210          ?>
 211      <form method="post" action="db_operations.php">
 212      <?php echo PMA_generate_common_hidden_inputs($db); ?>
 213      <input type="hidden" name="db_comment" value="true" />
 214      <fieldset>
 215          <legend>
 216          <?php
 217          if ($cfg['PropertiesIconic']) {
 218              echo '<img class="icon" src="' . $pmaThemeImage . 'b_comment.png"'
 219                  .' alt="" border="0" width="16" height="16" hspace="2" align="middle" />';
 220          }
 221          echo $strDBComment;
 222          $comment = PMA_getComments($db);
 223          ?>
 224          </legend>
 225          <input type="text" name="comment" class="textfield" size="30"
 226              value="<?php
 227              echo (isset($comment) && is_array($comment)
 228                  ? htmlspecialchars(implode(' ', $comment))
 229                  : ''); ?>" />
 230          <input type="submit" value="<?php echo $strGo; ?>" />
 231      </fieldset>
 232      </form>
 233          <?php
 234      }
 235      /**
 236       * rename database
 237       */
 238      ?>
 239      <form method="post" action="db_operations.php"
 240          onsubmit="return emptyFormElements(this, 'newname')">
 241      <input type="hidden" name="what" value="data" />
 242      <input type="hidden" name="db_rename" value="true" />
 243      <?php echo PMA_generate_common_hidden_inputs($db); ?>
 244      <fieldset>
 245          <legend>
 246      <?php
 247      if ($cfg['PropertiesIconic']) {
 248          echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
 249              .' alt="" width="16" height="16" />';
 250      }
 251      echo $strDBRename . ':';
 252      ?>
 253          </legend>
 254          <input type="text" name="newname" size="30" class="textfield" value="" />
 255          <input type="submit" value="<?php echo $strGo; ?>" />
 256      </fieldset>
 257      </form>
 258  
 259      <?php
 260      /**
 261       * Copy database
 262       */
 263      ?>
 264      <form method="post" action="db_operations.php"
 265          onsubmit="return emptyFormElements(this, 'newname')">
 266      <?php
 267      if (isset($db_collation)) {
 268          echo '<input type="hidden" name="db_collation" value="' . $db_collation
 269              .'" />' . "\n";
 270      }
 271      echo '<input type="hidden" name="db_copy" value="true" />' . "\n";
 272      echo PMA_generate_common_hidden_inputs($db);
 273      ?>
 274      <fieldset>
 275          <legend>
 276      <?php
 277      if ($cfg['PropertiesIconic']) {
 278          echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
 279              .' alt="" width="16" height="16" />';
 280      }
 281      echo $strDBCopy . ':';
 282      if (PMA_MYSQL_INT_VERSION >= 50000) {
 283          $drop_clause = 'DROP TABLE / DROP VIEW';
 284      } else {
 285          $drop_clause = 'DROP TABLE';
 286      }
 287      ?>
 288          </legend>
 289          <input type="text" name="newname" size="30" class="textfield" value="" /><br />
 290          <input type="radio" name="what" value="structure"
 291              id="radio_copy_structure" style="vertical-align: middle" />
 292          <label for="radio_copy_structure"><?php echo $strStrucOnly; ?></label><br />
 293          <input type="radio" name="what" value="data" id="radio_copy_data"
 294              checked="checked" style="vertical-align: middle" />
 295          <label for="radio_copy_data"><?php echo $strStrucData; ?></label><br />
 296          <input type="radio" name="what" value="dataonly"
 297              id="radio_copy_dataonly" style="vertical-align: middle" />
 298          <label for="radio_copy_dataonly"><?php echo $strDataOnly; ?></label><br />
 299  
 300          <input type="checkbox" name="create_database_before_copying" value="1"
 301              id="checkbox_create_database_before_copying"
 302              style="vertical-align: middle" checked="checked" />
 303          <label for="checkbox_create_database_before_copying">
 304              <?php echo $strCreateDatabaseBeforeCopying; ?></label><br />
 305          <input type="checkbox" name="drop_if_exists" value="true"
 306              id="checkbox_drop" style="vertical-align: middle" />
 307          <label for="checkbox_drop"><?php echo sprintf($strAddClause, $drop_clause); ?></label><br />
 308          <input type="checkbox" name="sql_auto_increment" value="1"
 309              id="checkbox_auto_increment" style="vertical-align: middle" />
 310          <label for="checkbox_auto_increment">
 311              <?php echo $strAddAutoIncrement; ?></label><br />
 312          <input type="checkbox" name="add_constraints" value="1"
 313              id="checkbox_constraints" style="vertical-align: middle" />
 314          <label for="checkbox_constraints">
 315              <?php echo $strAddConstraints; ?></label><br />
 316      <?php
 317      unset($drop_clause);
 318  
 319      if (isset($_COOKIE) && isset($_COOKIE['pma_switch_to_new'])
 320        && $_COOKIE['pma_switch_to_new'] == 'true') {
 321          $pma_switch_to_new = 'true';
 322      }
 323      ?>
 324          <input type="checkbox" name="switch_to_new" value="true"
 325              id="checkbox_switch"
 326              <?php echo ((isset($pma_switch_to_new) && $pma_switch_to_new == 'true') ? ' checked="checked"' : ''); ?>
 327              style="vertical-align: middle" />
 328          <label for="checkbox_switch"><?php echo $strSwitchToDatabase; ?></label>
 329      </fieldset>
 330      <fieldset class="tblFooters">
 331          <input type="submit" name="submit_copy" value="<?php echo $strGo; ?>" />
 332      </fieldset>
 333      </form>
 334  
 335      <?php
 336      /**
 337       * Change database charset
 338       */
 339      if (PMA_MYSQL_INT_VERSION >= 40101) {
 340      // MySQL supports setting default charsets / collations for databases since
 341      // version 4.1.1.
 342          echo '<form method="post" action="./db_operations.php">' . "\n"
 343             . PMA_generate_common_hidden_inputs($db, isset($table) ? $table : '')
 344             . '<fieldset>' . "\n"
 345             . '    <legend>';
 346          if ($cfg['PropertiesIconic']) {
 347              echo '<img class="icon" src="' . $pmaThemeImage . 's_asci.png"'
 348                  .' alt="" width="16" height="16" />';
 349          }
 350          echo '    <label for="select_db_collation">' . $strCollation . ':</label>' . "\n"
 351             . '    </legend>' . "\n"
 352             . PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_COLLATION,
 353                  'db_collation', 'select_db_collation', $db_collation, false, 3)
 354             . '    <input type="submit" name="submitcollation"'
 355             . ' value="' . $strGo . '" style="vertical-align: middle" />' . "\n"
 356             . '</fieldset>' . "\n"
 357             . '</form>' . "\n";
 358      }
 359  
 360      if ($num_tables > 0
 361        && !$cfgRelation['allworks'] && $cfg['PmaNoRelation_DisableWarning'] == false) {
 362          /* Show error if user has configured something, notice elsewhere */
 363          if (!empty($cfg['Servers'][$server]['pmadb'])) {
 364              echo '<div class="error"><h1>' . $strError . '</h1>';
 365          } else {
 366              echo '<div class="notice">';
 367          }
 368          printf($strRelationNotWorking, '<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">', '</a>');
 369          echo '</div>';
 370      } // end if
 371  } // end if (!$is_information_schema)
 372  
 373  
 374  // not sure about leaving the PDF dialog for information_schema
 375  if ($num_tables > 0 && isset($table)) {
 376      $takeaway = $url_query . '&amp;table=' . urlencode($table);
 377  } else {
 378      $takeaway = '';
 379  }
 380  
 381  if ($cfgRelation['pdfwork'] && $num_tables > 0) { ?>
 382      <!-- Work on PDF Pages -->
 383  
 384      <?php
 385      // We only show this if we find something in the new pdf_pages table
 386  
 387      $test_query = '
 388           SELECT *
 389             FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages']) . '
 390            WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'';
 391      $test_rs    = PMA_query_as_cu($test_query, null, PMA_DBI_QUERY_STORE);
 392  
 393      if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) { ?>
 394      <!-- PDF schema -->
 395      <form method="post" action="pdf_schema.php">
 396      <fieldset>
 397          <legend>
 398          <?php
 399          echo PMA_generate_common_hidden_inputs($db);
 400          if ($cfg['PropertiesIconic']) {
 401              echo '<img class="icon" src="' . $pmaThemeImage . 'b_view.png"'
 402                  .' alt="" width="16" height="16" />';
 403          }
 404          echo $strDisplayPDF;
 405          ?>:
 406          </legend>
 407          <label for="pdf_page_number_opt"><?php echo $strPageNumber; ?></label>
 408          <select name="pdf_page_number" id="pdf_page_number_opt">
 409          <?php
 410          while ($pages = @PMA_DBI_fetch_assoc($test_rs)) {
 411              echo '                <option value="' . $pages['page_nr'] . '">'
 412                  . $pages['page_nr'] . ': ' . $pages['page_descr'] . '</option>' . "\n";
 413          } // end while
 414          PMA_DBI_free_result($test_rs);
 415          unset($test_rs);
 416          ?>
 417          </select><br />
 418  
 419          <input type="checkbox" name="show_grid" id="show_grid_opt" />
 420          <label for="show_grid_opt"><?php echo $strShowGrid; ?></label><br />
 421          <input type="checkbox" name="show_color" id="show_color_opt"
 422              checked="checked" />
 423          <label for="show_color_opt"><?php echo $strShowColor; ?></label><br />
 424          <input type="checkbox" name="show_table_dimension" id="show_table_dim_opt" />
 425          <label for="show_table_dim_opt"><?php echo $strShowTableDimension; ?>
 426              </label><br />
 427          <input type="checkbox" name="all_tab_same_wide" id="all_tab_same_wide" />
 428          <label for="all_tab_same_wide"><?php echo $strAllTableSameWidth; ?>
 429              </label><br />
 430          <input type="checkbox" name="with_doc" id="with_doc" checked="checked" />
 431          <label for="with_doc"><?php echo $strDataDict; ?></label><br />
 432  
 433          <label for="orientation_opt"><?php echo $strShowDatadictAs; ?></label>
 434          <select name="orientation" id="orientation_opt">
 435              <option value="L"><?php echo $strLandscape;?></option>
 436              <option value="P"><?php echo $strPortrait;?></option>
 437          </select><br />
 438  
 439          <label for="paper_opt"><?php echo $strPaperSize; ?></label>
 440          <select name="paper" id="paper_opt">
 441          <?php
 442              foreach ($cfg['PDFPageSizes'] AS $key => $val) {
 443                  echo '<option value="' . $val . '"';
 444                  if ($val == $cfg['PDFDefaultPageSize']) {
 445                      echo ' selected="selected"';
 446                  }
 447                  echo ' >' . $val . '</option>' . "\n";
 448              }
 449          ?>
 450          </select>
 451      </fieldset>
 452      <fieldset class="tblFooters">
 453          <input type="submit" value="<?php echo $strGo; ?>" />
 454      </fieldset>
 455      </form>
 456          <?php
 457      }   // end if
 458      ?>
 459      <ul>
 460          <li>
 461      <?php
 462          echo '<a href="pdf_pages.php?' . $takeaway . '">';
 463          if ($cfg['PropertiesIconic']) {
 464              echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
 465                  .' alt="" width="16" height="16" />';
 466          }
 467          echo $strEditPDFPages . '</a>';
 468      ?>
 469          </li>
 470      </ul>
 471      <?php
 472  } // end if
 473  
 474  /**
 475   * Displays the footer
 476   */
 477  require_once  './libraries/footer.inc.php';
 478  ?>


Généré le : Mon Nov 26 15:18:20 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics