db_create.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. *
  5. * @package PhpMyAdmin
  6. */
  7. /**
  8. * Gets some core libraries
  9. */
  10. require_once 'libraries/common.inc.php';
  11. require_once 'libraries/mysql_charsets.lib.php';
  12. if (! PMA_DRIZZLE) {
  13. include_once 'libraries/replication.inc.php';
  14. }
  15. require 'libraries/build_html_for_db.lib.php';
  16. /**
  17. * Sets globals from $_POST
  18. */
  19. $post_params = array(
  20. 'db_collation',
  21. 'new_db'
  22. );
  23. foreach ($post_params as $one_post_param) {
  24. if (isset($_POST[$one_post_param])) {
  25. $GLOBALS[$one_post_param] = $_POST[$one_post_param];
  26. }
  27. }
  28. PMA_Util::checkParameters(array('new_db'));
  29. /**
  30. * Defines the url to return to in case of error in a sql statement
  31. */
  32. $err_url = 'index.php?' . PMA_generate_common_url();
  33. /**
  34. * Builds and executes the db creation sql query
  35. */
  36. $sql_query = 'CREATE DATABASE ' . PMA_Util::backquote($new_db);
  37. if (! empty($db_collation)) {
  38. list($db_charset) = explode('_', $db_collation);
  39. if (in_array($db_charset, $mysql_charsets)
  40. && in_array($db_collation, $mysql_collations[$db_charset])
  41. ) {
  42. $sql_query .= ' DEFAULT' . PMA_generateCharsetQueryPart($db_collation);
  43. }
  44. $db_collation_for_ajax = $db_collation;
  45. unset($db_charset, $db_collation);
  46. }
  47. $sql_query .= ';';
  48. $result = PMA_DBI_try_query($sql_query);
  49. if (! $result) {
  50. $message = PMA_Message::rawError(PMA_DBI_getError());
  51. // avoid displaying the not-created db name in header or navi panel
  52. $GLOBALS['db'] = '';
  53. $GLOBALS['table'] = '';
  54. /**
  55. * If in an Ajax request, just display the message with {@link PMA_Response}
  56. */
  57. if ($GLOBALS['is_ajax_request'] == true) {
  58. $response = PMA_Response::getInstance();
  59. $response->isSuccess(false);
  60. $response->addJSON('message', $message);
  61. } else {
  62. include_once 'index.php';
  63. }
  64. } else {
  65. $message = PMA_Message::success(__('Database %1$s has been created.'));
  66. $message->addParam($new_db);
  67. $GLOBALS['db'] = $new_db;
  68. /**
  69. * If in an Ajax request, build the output and send it
  70. */
  71. if ($GLOBALS['is_ajax_request'] == true) {
  72. //Construct the html for the new database, so that it can be appended to
  73. // the list of databases on server_databases.php
  74. /**
  75. * Build the array to be passed to {@link PMA_generate_common_url}
  76. * to generate the links
  77. *
  78. * @global array $GLOBALS['db_url_params']
  79. * @name $db_url_params
  80. */
  81. $db_url_params['db'] = $new_db;
  82. $is_superuser = PMA_isSuperuser();
  83. $column_order = PMA_getColumnOrder();
  84. $url_query = PMA_generate_common_url($new_db);
  85. /**
  86. * String that will contain the output HTML
  87. * @name $new_db_string
  88. */
  89. $new_db_string = '<tr>';
  90. if (empty($db_collation_for_ajax)) {
  91. $db_collation_for_ajax = PMA_getServerCollation();
  92. }
  93. // $dbstats comes from the create table dialog
  94. if (! empty($dbstats)) {
  95. $current = array(
  96. 'SCHEMA_NAME' => $new_db,
  97. 'DEFAULT_COLLATION_NAME' => $db_collation_for_ajax,
  98. 'SCHEMA_TABLES' => '0',
  99. 'SCHEMA_TABLE_ROWS' => '0',
  100. 'SCHEMA_DATA_LENGTH' => '0',
  101. 'SCHEMA_MAX_DATA_LENGTH' => '0',
  102. 'SCHEMA_INDEX_LENGTH' => '0',
  103. 'SCHEMA_LENGTH' => '0',
  104. 'SCHEMA_DATA_FREE' => '0'
  105. );
  106. } else {
  107. $current = array(
  108. 'SCHEMA_NAME' => $new_db
  109. );
  110. }
  111. list($column_order, $generated_html) = PMA_buildHtmlForDb(
  112. $current, $is_superuser, $url_query,
  113. $column_order, $replication_types, $replication_info
  114. );
  115. $new_db_string .= $generated_html;
  116. $new_db_string .= '</tr>';
  117. $response = PMA_Response::getInstance();
  118. $response->addJSON('message', $message);
  119. $response->addJSON('new_db_string', $new_db_string);
  120. $response->addJSON(
  121. 'sql_query',
  122. PMA_Util::getMessage(
  123. null, $sql_query, 'success'
  124. )
  125. );
  126. } else {
  127. include_once '' . $cfg['DefaultTabDatabase'];
  128. }
  129. }
  130. ?>