db_operations.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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.lib.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. PMA_DBI_select_db($db);
  58. $tables_full = PMA_DBI_get_tables_full($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. PMA_DBI_select_db($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. PMA_DBI_query($local_query);
  108. $message = PMA_Message::success(__('Database %1$s has been renamed to %2$s'));
  109. $message->addParam($db);
  110. $message->addParam($_REQUEST['newname']);
  111. } elseif (! $_error) {
  112. $message = PMA_Message::success(__('Database %1$s has been copied to %2$s'));
  113. $message->addParam($db);
  114. $message->addParam($_REQUEST['newname']);
  115. }
  116. $reload = true;
  117. /* Change database to be used */
  118. if (! $_error && $move) {
  119. $db = $_REQUEST['newname'];
  120. } elseif (! $_error) {
  121. if (isset($_REQUEST['switch_to_new'])
  122. && $_REQUEST['switch_to_new'] == 'true'
  123. ) {
  124. $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
  125. $db = $_REQUEST['newname'];
  126. } else {
  127. $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
  128. }
  129. }
  130. if ($_error && ! isset($message)) {
  131. $message = PMA_Message::error();
  132. }
  133. }
  134. /**
  135. * Database has been successfully renamed/moved. If in an Ajax request,
  136. * generate the output with {@link PMA_Response} and exit
  137. */
  138. if ($GLOBALS['is_ajax_request'] == true) {
  139. $response = PMA_Response::getInstance();
  140. $response->isSuccess($message->isSuccess());
  141. $response->addJSON('message', $message);
  142. $response->addJSON('newname', $_REQUEST['newname']);
  143. $response->addJSON(
  144. 'sql_query',
  145. PMA_Util::getMessage(null, $sql_query)
  146. );
  147. $response->addJSON('db', $db);
  148. exit;
  149. }
  150. }
  151. /**
  152. * Settings for relations stuff
  153. */
  154. $cfgRelation = PMA_getRelationsParam();
  155. /**
  156. * Check if comments were updated
  157. * (must be done before displaying the menu tabs)
  158. */
  159. if (isset($_REQUEST['comment'])) {
  160. PMA_setDbComment($db, $_REQUEST['comment']);
  161. }
  162. /**
  163. * Prepares the tables list if the user where not redirected to this script
  164. * because there is no table in the database ($is_info is true)
  165. */
  166. if (empty($is_info)) {
  167. include 'libraries/db_common.inc.php';
  168. $url_query .= '&amp;goto=db_operations.php';
  169. // Gets the database structure
  170. $sub_part = '_structure';
  171. include 'libraries/db_info.inc.php';
  172. echo "\n";
  173. if (isset($message)) {
  174. echo PMA_Util::getMessage($message, $sql_query);
  175. unset($message);
  176. }
  177. }
  178. $_REQUEST['db_collation'] = PMA_getDbCollation($db);
  179. $is_information_schema = PMA_is_system_schema($db);
  180. if (!$is_information_schema) {
  181. if ($cfgRelation['commwork']) {
  182. /**
  183. * database comment
  184. */
  185. $response->addHTML(PMA_getHtmlForDatabaseComment($db));
  186. }
  187. $response->addHTML('<div class="operations_half_width">');
  188. ob_start();
  189. include 'libraries/display_create_table.lib.php';
  190. $content = ob_get_contents();
  191. ob_end_clean();
  192. $response->addHTML($content);
  193. $response->addHTML('</div>');
  194. /**
  195. * rename database
  196. */
  197. if ($db != 'mysql') {
  198. $response->addHTML(PMA_getHtmlForRenameDatabase($db));
  199. }
  200. // Drop link if allowed
  201. // Don't even try to drop information_schema.
  202. // You won't be able to. Believe me. You won't.
  203. // Don't allow to easily drop mysql database, RFE #1327514.
  204. if (($is_superuser || $GLOBALS['cfg']['AllowUserDropDatabase'])
  205. && ! $db_is_information_schema
  206. && (PMA_DRIZZLE || $db != 'mysql')
  207. ) {
  208. $response->addHTML(PMA_getHtmlForDropDatabaseLink($db));
  209. }
  210. /**
  211. * Copy database
  212. */
  213. $response->addHTML(PMA_getHtmlForCopyDatabase($db));
  214. /**
  215. * Change database charset
  216. */
  217. $response->addHTML(PMA_getHtmlForChangeDatabaseCharset($db, $table));
  218. if ($num_tables > 0
  219. && ! $cfgRelation['allworks']
  220. && $cfg['PmaNoRelation_DisableWarning'] == false
  221. ) {
  222. $message = PMA_Message::notice(
  223. __('The phpMyAdmin configuration storage has been deactivated. To find out why click %shere%s.')
  224. );
  225. $message->addParam(
  226. '<a href="' . $cfg['PmaAbsoluteUri'] . '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. // not sure about displaying the PDF dialog in case db is information_schema
  240. if ($cfgRelation['pdfwork'] && $num_tables > 0) {
  241. // We only show this if we find something in the new pdf_pages table
  242. $test_query = '
  243. SELECT *
  244. FROM ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
  245. . '.' . PMA_Util::backquote($cfgRelation['pdf_pages']) . '
  246. WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\'';
  247. $test_rs = PMA_queryAsControlUser($test_query, null, PMA_DBI_QUERY_STORE);
  248. /*
  249. * Export Relational Schema View
  250. */
  251. $response->addHTML(PMA_getHtmlForExportRelationalSchemaView($url_query));
  252. } // end if
  253. ?>