tbl_operations.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. *
  5. * @package PhpMyAdmin
  6. */
  7. /**
  8. *
  9. */
  10. require_once 'libraries/common.inc.php';
  11. /**
  12. * functions implementation for this script
  13. */
  14. require_once 'libraries/operations.lib.php';
  15. $pma_table = new PMA_Table($GLOBALS['table'], $GLOBALS['db']);
  16. $response = PMA_Response::getInstance();
  17. /**
  18. * Runs common work
  19. */
  20. require 'libraries/tbl_common.inc.php';
  21. $url_query .= '&amp;goto=tbl_operations.php&amp;back=tbl_operations.php';
  22. $url_params['goto'] = $url_params['back'] = 'tbl_operations.php';
  23. /**
  24. * Gets relation settings
  25. */
  26. $cfgRelation = PMA_getRelationsParam();
  27. /**
  28. * Gets available MySQL charsets and storage engines
  29. */
  30. require_once 'libraries/mysql_charsets.lib.php';
  31. require_once 'libraries/StorageEngine.class.php';
  32. /**
  33. * Class for partition management
  34. */
  35. require_once 'libraries/Partition.class.php';
  36. // reselect current db (needed in some cases probably due to
  37. // the calling of relation.lib.php)
  38. PMA_DBI_select_db($GLOBALS['db']);
  39. /**
  40. * Gets tables informations
  41. */
  42. require 'libraries/tbl_info.inc.php';
  43. // define some variables here, for improved syntax in the conditionals
  44. $is_myisam_or_aria = $is_isam = $is_innodb = $is_berkeleydb = $is_aria = $is_pbxt = false;
  45. // set initial value of these variables, based on the current table engine
  46. list($is_myisam_or_aria, $is_innodb, $is_isam,
  47. $is_berkeleydb, $is_aria, $is_pbxt
  48. ) = PMA_setGlobalVariablesForEngine($tbl_storage_engine);
  49. if ($is_aria) {
  50. // the value for transactional can be implicit
  51. // (no create option found, in this case it means 1)
  52. // or explicit (option found with a value of 0 or 1)
  53. // ($transactional may have been set by libraries/tbl_info.inc.php,
  54. // from the $create_options)
  55. $transactional = (isset($transactional) && $transactional == '0')
  56. ? '0'
  57. : '1';
  58. $page_checksum = (isset($page_checksum)) ? $page_checksum : '';
  59. }
  60. $reread_info = false;
  61. $table_alters = array();
  62. /**
  63. * If the table has to be moved to some other database
  64. */
  65. if (isset($_REQUEST['submit_move']) || isset($_REQUEST['submit_copy'])) {
  66. $_message = '';
  67. include_once 'tbl_move_copy.php';
  68. }
  69. /**
  70. * If the table has to be maintained
  71. */
  72. if (isset($_REQUEST['table_maintenance'])) {
  73. include_once 'sql.php';
  74. unset($result);
  75. }
  76. /**
  77. * Updates table comment, type and options if required
  78. */
  79. if (isset($_REQUEST['submitoptions'])) {
  80. $_message = '';
  81. $warning_messages = array();
  82. if (isset($_REQUEST['new_name'])) {
  83. if ($pma_table->rename($_REQUEST['new_name'])) {
  84. $_message .= $pma_table->getLastMessage();
  85. $result = true;
  86. $GLOBALS['table'] = $pma_table->getName();
  87. $reread_info = true;
  88. $reload = true;
  89. } else {
  90. $_message .= $pma_table->getLastError();
  91. $result = false;
  92. }
  93. }
  94. if (! empty($_REQUEST['new_tbl_storage_engine'])
  95. && strtolower($_REQUEST['new_tbl_storage_engine'])
  96. !== strtolower($tbl_storage_engine)
  97. ) {
  98. $new_tbl_storage_engine = $_REQUEST['new_tbl_storage_engine'];
  99. // reset the globals for the new engine
  100. list($is_myisam_or_aria, $is_innodb, $is_isam,
  101. $is_berkeleydb, $is_aria, $is_pbxt
  102. ) = PMA_setGlobalVariablesForEngine($new_tbl_storage_engine);
  103. if ($is_aria) {
  104. $transactional = (isset($transactional) && $transactional == '0')
  105. ? '0'
  106. : '1';
  107. $page_checksum = (isset($page_checksum)) ? $page_checksum : '';
  108. }
  109. } else {
  110. $new_tbl_storage_engine = '';
  111. }
  112. $table_alters = PMA_getTableAltersArray(
  113. $is_myisam_or_aria, $is_isam, $pack_keys,
  114. (empty($checksum) ? '0' : '1'),
  115. $is_aria,
  116. ((isset($page_checksum)) ? $page_checksum : ''),
  117. (empty($delay_key_write) ? '0' : '1'),
  118. $is_innodb, $is_pbxt, $row_format,
  119. $new_tbl_storage_engine,
  120. ((isset($transactional) && $transactional == '0') ? '0' : '1'),
  121. $tbl_collation
  122. );
  123. if (count($table_alters) > 0) {
  124. $sql_query = 'ALTER TABLE '
  125. . PMA_Util::backquote($GLOBALS['table']);
  126. $sql_query .= "\r\n" . implode("\r\n", $table_alters);
  127. $sql_query .= ';';
  128. $result .= PMA_DBI_query($sql_query) ? true : false;
  129. $reread_info = true;
  130. unset($table_alters);
  131. $warning_messages = PMA_getWarningMessagesArray();
  132. }
  133. }
  134. /**
  135. * Reordering the table has been requested by the user
  136. */
  137. if (isset($_REQUEST['submitorderby']) && ! empty($_REQUEST['order_field'])) {
  138. list($sql_query, $result) = PMA_getQueryAndResultForReorderingTable();
  139. } // end if
  140. /**
  141. * A partition operation has been requested by the user
  142. */
  143. if (isset($_REQUEST['submit_partition'])
  144. && ! empty($_REQUEST['partition_operation'])
  145. ) {
  146. list($sql_query, $result) = PMA_getQueryAndResultForPartition();
  147. } // end if
  148. if ($reread_info) {
  149. // to avoid showing the old value (for example the AUTO_INCREMENT) after
  150. // a change, clear the cache
  151. PMA_Table::$cache = array();
  152. $page_checksum = $checksum = $delay_key_write = 0;
  153. include 'libraries/tbl_info.inc.php';
  154. }
  155. unset($reread_info);
  156. if (isset($result) && empty($message_to_show)) {
  157. // set to success by default, because result set could be empty
  158. // (for example, a table rename)
  159. $_type = 'success';
  160. if (empty($_message)) {
  161. $_message = $result
  162. ? PMA_Message::success(
  163. __('Your SQL query has been executed successfully')
  164. )
  165. : PMA_Message::error(__('Error'));
  166. // $result should exist, regardless of $_message
  167. $_type = $result ? 'success' : 'error';
  168. if (isset($GLOBALS['ajax_request'])
  169. && $GLOBALS['ajax_request'] == true
  170. ) {
  171. $response = PMA_Response::getInstance();
  172. $response->isSuccess($_message->isSuccess());
  173. $response->addJSON('message', $_message);
  174. $response->addJSON(
  175. 'sql_query', PMA_Util::getMessage(null, $sql_query)
  176. );
  177. exit;
  178. }
  179. }
  180. if (! empty($warning_messages)) {
  181. $_message = new PMA_Message;
  182. $_message->addMessages($warning_messages);
  183. $_message->isError(true);
  184. if ($GLOBALS['ajax_request'] == true) {
  185. $response = PMA_Response::getInstance();
  186. $response->isSuccess(false);
  187. $response->addJSON('message', $_message);
  188. exit;
  189. }
  190. unset($warning_messages);
  191. }
  192. $response->addHTML(
  193. PMA_Util::getMessage($_message, $sql_query, $_type)
  194. );
  195. unset($_message, $_type);
  196. }
  197. $url_params['goto']
  198. = $url_params['back']
  199. = 'tbl_operations.php';
  200. /**
  201. * Get columns names
  202. */
  203. $columns = PMA_DBI_get_columns($GLOBALS['db'], $GLOBALS['table']);
  204. /**
  205. * Displays the page
  206. */
  207. /**
  208. * Order the table
  209. */
  210. $hideOrderTable = false;
  211. // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
  212. // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
  213. // InnoDB always orders table rows according to such an index if one is present.
  214. if ($tbl_storage_engine == 'INNODB') {
  215. include_once 'libraries/Index.class.php';
  216. $indexes = PMA_Index::getFromTable($GLOBALS['table'], $GLOBALS['db']);
  217. foreach ($indexes as $name => $idx) {
  218. if ($name == 'PRIMARY') {
  219. $hideOrderTable = true;
  220. break;
  221. } elseif (! $idx->getNonUnique()) {
  222. $notNull = true;
  223. foreach ($idx->getColumns() as $column) {
  224. if ($column->getNull()) {
  225. $notNull = false;
  226. break;
  227. }
  228. }
  229. if ($notNull) {
  230. $hideOrderTable = true;
  231. break;
  232. }
  233. }
  234. }
  235. }
  236. if (! $hideOrderTable) {
  237. $response->addHTML(PMA_getHtmlForOrderTheTable($columns));
  238. }
  239. /**
  240. * Move table
  241. */
  242. $response->addHTML(PMA_getHtmlForMoveTable());
  243. if (strstr($show_comment, '; InnoDB free') === false) {
  244. if (strstr($show_comment, 'InnoDB free') === false) {
  245. // only user entered comment
  246. $comment = $show_comment;
  247. } else {
  248. // here we have just InnoDB generated part
  249. $comment = '';
  250. }
  251. } else {
  252. // remove InnoDB comment from end, just the minimal part (*? is non greedy)
  253. $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
  254. }
  255. // PACK_KEYS: MyISAM or ISAM
  256. // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
  257. // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
  258. // Here should be version check for InnoDB, however it is supported
  259. // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
  260. // check for version
  261. $response->addHTML(
  262. PMA_getTableOptionDiv(
  263. $comment, $tbl_collation, $tbl_storage_engine,
  264. $is_myisam_or_aria, $is_isam, $pack_keys,
  265. $auto_increment,
  266. (empty($delay_key_write) ? '0' : '1'),
  267. ((isset($transactional) && $transactional == '0') ? '0' : '1'),
  268. ((isset($page_checksum)) ? $page_checksum : ''),
  269. $is_innodb, $is_pbxt, $is_aria, (empty($checksum) ? '0' : '1')
  270. )
  271. );
  272. /**
  273. * Copy table
  274. */
  275. $response->addHTML(PMA_getHtmlForCopytable());
  276. $response->addHTML('<br class="clearfloat"/>');
  277. /**
  278. * Table maintenance
  279. */
  280. $response->addHTML(
  281. PMA_getHtmlForTableMaintenance(
  282. $is_myisam_or_aria,
  283. $is_innodb,
  284. $is_berkeleydb,
  285. $url_params
  286. )
  287. );
  288. if (! (isset($db_is_information_schema) && $db_is_information_schema)) {
  289. $truncate_table_url_params = array();
  290. $drop_table_url_params = array();
  291. if (! $tbl_is_view
  292. && ! (isset($db_is_information_schema) && $db_is_information_schema)
  293. ) {
  294. $this_sql_query = 'TRUNCATE TABLE '
  295. . PMA_Util::backquote($GLOBALS['table']);
  296. $truncate_table_url_params = array_merge(
  297. $url_params,
  298. array(
  299. 'sql_query' => $this_sql_query,
  300. 'goto' => 'tbl_structure.php',
  301. 'reload' => '1',
  302. 'message_to_show' => sprintf(
  303. __('Table %s has been emptied'),
  304. htmlspecialchars($table)
  305. ),
  306. )
  307. );
  308. }
  309. if (! (isset($db_is_information_schema) && $db_is_information_schema)) {
  310. $this_sql_query = 'DROP TABLE '
  311. . PMA_Util::backquote($GLOBALS['table']);
  312. $drop_table_url_params = array_merge(
  313. $url_params,
  314. array(
  315. 'sql_query' => $this_sql_query,
  316. 'goto' => 'db_operations.php',
  317. 'reload' => '1',
  318. 'purge' => '1',
  319. 'message_to_show' => sprintf(
  320. ($tbl_is_view
  321. ? __('View %s has been dropped')
  322. : __('Table %s has been dropped')
  323. ),
  324. htmlspecialchars($table)
  325. ),
  326. // table name is needed to avoid running
  327. // PMA_relationsCleanupDatabase() on the whole db later
  328. 'table' => $GLOBALS['table'],
  329. )
  330. );
  331. }
  332. $response->addHTML(
  333. PMA_getHtmlForDeleteDataOrTable(
  334. $truncate_table_url_params,
  335. $drop_table_url_params
  336. )
  337. );
  338. }
  339. $response->addHTML('<br class="clearfloat">');
  340. if (PMA_Partition::havePartitioning()) {
  341. $partition_names = PMA_Partition::getPartitionNames($db, $table);
  342. // show the Partition maintenance section only if we detect a partition
  343. if (! is_null($partition_names[0])) {
  344. $response->addHTML(
  345. PMA_getHtmlForPartitionMaintenance($partition_names, $url_params)
  346. );
  347. } // end if
  348. } // end if
  349. unset($partition_names);
  350. // Referential integrity check
  351. // The Referential integrity check was intended for the non-InnoDB
  352. // tables for which the relations are defined in pmadb
  353. // so I assume that if the current table is InnoDB, I don't display
  354. // this choice (InnoDB maintains integrity by itself)
  355. if ($cfgRelation['relwork'] && ! $is_innodb) {
  356. PMA_DBI_select_db($GLOBALS['db']);
  357. $foreign = PMA_getForeigners($GLOBALS['db'], $GLOBALS['table']);
  358. if ($foreign) {
  359. $response->addHTML(
  360. PMA_getHtmlForReferentialIntegrityCheck($foreign, $url_params)
  361. );
  362. } // end if ($foreign)
  363. } // end if (!empty($cfg['Server']['relation']))
  364. ?>