db_operations.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * handles miscellaneous db operations:
  5. * - move/rename
  6. * - copy
  7. * - changing collation
  8. * - changing comment
  9. * - adding tables
  10. * - viewing PDF schemas
  11. *
  12. * @package PhpMyAdmin
  13. */
  14. /**
  15. * requirements
  16. */
  17. require_once 'libraries/common.inc.php';
  18. require_once 'libraries/mysql_charsets.inc.php';
  19. /**
  20. * functions implementation for this script
  21. */
  22. require_once 'libraries/operations.lib.php';
  23. // add a javascript file for jQuery functions to handle Ajax actions
  24. $response = PMA_Response::getInstance();
  25. $header = $response->getHeader();
  26. $scripts = $header->getScripts();
  27. $scripts->addFile('db_operations.js');
  28. /**
  29. * Rename/move or copy database
  30. */
  31. if (strlen($db)
  32. && (! empty($_REQUEST['db_rename']) || ! empty($_REQUEST['db_copy']))
  33. ) {
  34. if (! empty($_REQUEST['db_rename'])) {
  35. $move = true;
  36. } else {
  37. $move = false;
  38. }
  39. if (! isset($_REQUEST['newname']) || ! strlen($_REQUEST['newname'])) {
  40. $message = PMA_Message::error(__('The database name is empty!'));
  41. } else {
  42. $sql_query = ''; // in case target db exists
  43. $_error = false;
  44. if ($move
  45. || (isset($_REQUEST['create_database_before_copying'])
  46. && $_REQUEST['create_database_before_copying'])
  47. ) {
  48. $sql_query = PMA_getSqlQueryAndCreateDbBeforeCopy();
  49. }
  50. // here I don't use DELIMITER because it's not part of the
  51. // language; I have to send each statement one by one
  52. // to avoid selecting alternatively the current and new db
  53. // we would need to modify the CREATE definitions to qualify
  54. // the db name
  55. PMA_runProcedureAndFunctionDefinitions($db);
  56. // go back to current db, just in case
  57. $GLOBALS['dbi']->selectDb($db);
  58. $tables_full = $GLOBALS['dbi']->getTablesFull($db);
  59. include_once "libraries/plugin_interface.lib.php";
  60. // remove all foreign key constraints, otherwise we can get errors
  61. $export_sql_plugin = PMA_getPlugin(
  62. "export",
  63. "sql",
  64. 'libraries/plugins/export/',
  65. array(
  66. 'single_table' => isset($single_table),
  67. 'export_type' => 'database'
  68. )
  69. );
  70. $GLOBALS['sql_constraints_query_full_db']
  71. = PMA_getSqlConstraintsQueryForFullDb(
  72. $tables_full, $export_sql_plugin, $move, $db
  73. );
  74. $views = PMA_getViewsAndCreateSqlViewStandIn(
  75. $tables_full, $export_sql_plugin, $db
  76. );
  77. list($sql_query, $_error) = PMA_getSqlQueryForCopyTable(
  78. $tables_full, $sql_query, $move, $db
  79. );
  80. // handle the views
  81. if (! $_error) {
  82. $_error = PMA_handleTheViews($views, $move, $db);
  83. }
  84. unset($views);
  85. // now that all tables exist, create all the accumulated constraints
  86. if (! $_error && count($GLOBALS['sql_constraints_query_full_db']) > 0) {
  87. PMA_createAllAccumulatedConstraints();
  88. }
  89. if (! PMA_DRIZZLE && PMA_MYSQL_INT_VERSION >= 50100) {
  90. // here DELIMITER is not used because it's not part of the
  91. // language; each statement is sent one by one
  92. PMA_runEventDefinitionsForDb($db);
  93. }
  94. // go back to current db, just in case
  95. $GLOBALS['dbi']->selectDb($db);
  96. // Duplicate the bookmarks for this db (done once for each db)
  97. PMA_duplicateBookmarks($_error, $db);
  98. if (! $_error && $move) {
  99. /**
  100. * cleanup pmadb stuff for this db
  101. */
  102. include_once 'libraries/relation_cleanup.lib.php';
  103. PMA_relationsCleanupDatabase($db);
  104. // if someday the RENAME DATABASE reappears, do not DROP
  105. $local_query = 'DROP DATABASE ' . PMA_Util::backquote($db) . ';';
  106. $sql_query .= "\n" . $local_query;
  107. $GLOBALS['dbi']->query($local_query);
  108. $message = PMA_Message::success(
  109. __('Database %1$s has been renamed to %2$s.')
  110. );
  111. $message->addParam($db);
  112. $message->addParam($_REQUEST['newname']);
  113. } elseif (! $_error) {
  114. $message = PMA_Message::success(
  115. __('Database %1$s has been copied to %2$s.')
  116. );
  117. $message->addParam($db);
  118. $message->addParam($_REQUEST['newname']);
  119. }
  120. $reload = true;
  121. /* Change database to be used */
  122. if (! $_error && $move) {
  123. $db = $_REQUEST['newname'];
  124. } elseif (! $_error) {
  125. if (isset($_REQUEST['switch_to_new'])
  126. && $_REQUEST['switch_to_new'] == 'true'
  127. ) {
  128. $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
  129. $db = $_REQUEST['newname'];
  130. } else {
  131. $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
  132. }
  133. }
  134. if ($_error && ! isset($message)) {
  135. $message = PMA_Message::error();
  136. }
  137. }
  138. /**
  139. * Database has been successfully renamed/moved. If in an Ajax request,
  140. * generate the output with {@link PMA_Response} and exit
  141. */
  142. if ($GLOBALS['is_ajax_request'] == true) {
  143. $response = PMA_Response::getInstance();
  144. $response->isSuccess($message->isSuccess());
  145. $response->addJSON('message', $message);
  146. $response->addJSON('newname', $_REQUEST['newname']);
  147. $response->addJSON(
  148. 'sql_query',
  149. PMA_Util::getMessage(null, $sql_query)
  150. );
  151. $response->addJSON('db', $db);
  152. exit;
  153. }
  154. }
  155. /**
  156. * Settings for relations stuff
  157. */
  158. $cfgRelation = PMA_getRelationsParam();
  159. /**
  160. * Check if comments were updated
  161. * (must be done before displaying the menu tabs)
  162. */
  163. if (isset($_REQUEST['comment'])) {
  164. PMA_setDbComment($db, $_REQUEST['comment']);
  165. }
  166. require 'libraries/db_common.inc.php';
  167. $url_query .= '&amp;goto=db_operations.php';
  168. // Gets the database structure
  169. $sub_part = '_structure';
  170. require 'libraries/db_info.inc.php';
  171. echo "\n";
  172. if (isset($message)) {
  173. echo PMA_Util::getMessage($message, $sql_query);
  174. unset($message);
  175. }
  176. $_REQUEST['db_collation'] = PMA_getDbCollation($db);
  177. $is_information_schema = $GLOBALS['dbi']->isSystemSchema($db);
  178. $response->addHTML('<div id="boxContainer" data-box-width="300">');
  179. if (!$is_information_schema) {
  180. if ($cfgRelation['commwork']) {
  181. /**
  182. * database comment
  183. */
  184. $response->addHTML(PMA_getHtmlForDatabaseComment($db));
  185. }
  186. $response->addHTML('<div class="operations_half_width">');
  187. ob_start();
  188. include 'libraries/display_create_table.lib.php';
  189. $content = ob_get_contents();
  190. ob_end_clean();
  191. $response->addHTML($content);
  192. $response->addHTML('</div>');
  193. /**
  194. * rename database
  195. */
  196. if ($db != 'mysql') {
  197. $response->addHTML(PMA_getHtmlForRenameDatabase($db));
  198. }
  199. // Drop link if allowed
  200. // Don't even try to drop information_schema.
  201. // You won't be able to. Believe me. You won't.
  202. // Don't allow to easily drop mysql database, RFE #1327514.
  203. if (($is_superuser || $GLOBALS['cfg']['AllowUserDropDatabase'])
  204. && ! $db_is_system_schema
  205. && (PMA_DRIZZLE || $db != 'mysql')
  206. ) {
  207. $response->addHTML(PMA_getHtmlForDropDatabaseLink($db));
  208. }
  209. /**
  210. * Copy database
  211. */
  212. $response->addHTML(PMA_getHtmlForCopyDatabase($db));
  213. /**
  214. * Change database charset
  215. */
  216. $response->addHTML(PMA_getHtmlForChangeDatabaseCharset($db, $table));
  217. if ($num_tables > 0
  218. && ! $cfgRelation['allworks']
  219. && $cfg['PmaNoRelation_DisableWarning'] == false
  220. ) {
  221. $message = PMA_Message::notice(
  222. __('The phpMyAdmin configuration storage has been deactivated. To find out why click %shere%s.')
  223. );
  224. $message->addParam(
  225. '<a href="' . $cfg['PmaAbsoluteUri']
  226. . 'chk_rel.php?' . $url_query . '">',
  227. false
  228. );
  229. $message->addParam('</a>', false);
  230. /* Show error if user has configured something, notice elsewhere */
  231. if (!empty($cfg['Servers'][$server]['pmadb'])) {
  232. $message->isError(true);
  233. }
  234. $response->addHTML('<div class="operations_full_width">');
  235. $response->addHTML($message->getDisplay());
  236. $response->addHTML('</div>');
  237. } // end if
  238. } // end if (!$is_information_schema)
  239. $response->addHTML('</div>');
  240. // not sure about displaying the PDF dialog in case db is information_schema
  241. if ($cfgRelation['pdfwork'] && $num_tables > 0) {
  242. // We only show this if we find something in the new pdf_pages table
  243. $test_query = '
  244. SELECT *
  245. FROM ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
  246. . '.' . PMA_Util::backquote($cfgRelation['pdf_pages']) . '
  247. WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\'';
  248. $test_rs = PMA_queryAsControlUser(
  249. $test_query,
  250. false,
  251. PMA_DatabaseInterface::QUERY_STORE
  252. );
  253. /*
  254. * Export Relational Schema View
  255. */
  256. $response->addHTML(PMA_getHtmlForExportRelationalSchemaView($url_query));
  257. } // end if
  258. ?>