PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/ReferenceCode/phpmyadmin/tbl_structure.php

https://gitlab.com/ctheilman92/Aging-In-Place
PHP | 469 lines | 306 code | 64 blank | 99 comment | 72 complexity | eba96cd4c80ab91b4d35169b4e8dff7a MD5 | raw file
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Displays table structure infos like fields/columns, indexes, size, rows
  5. * and allows manipulation of indexes and columns/fields
  6. *
  7. * @package PhpMyAdmin
  8. */
  9. /**
  10. *
  11. */
  12. require_once 'libraries/common.inc.php';
  13. require_once 'libraries/mysql_charsets.lib.php';
  14. /**
  15. * Function implementations for this script
  16. */
  17. require_once 'libraries/structure.lib.php';
  18. require_once 'libraries/index.lib.php';
  19. $response = PMA_Response::getInstance();
  20. $header = $response->getHeader();
  21. $scripts = $header->getScripts();
  22. $scripts->addFile('tbl_structure.js');
  23. $scripts->addFile('indexes.js');
  24. /**
  25. * Handle column moving
  26. */
  27. if (isset($_REQUEST['move_columns'])
  28. && is_array($_REQUEST['move_columns'])
  29. && $response->isAjax()
  30. ) {
  31. PMA_moveColumns($db, $table);
  32. exit;
  33. }
  34. /**
  35. * A click on Change has been made for one column
  36. */
  37. if (isset($_REQUEST['change_column'])) {
  38. PMA_displayHtmlForColumnChange($db, $table, null, 'tbl_structure.php');
  39. exit;
  40. }
  41. /**
  42. * Modifications have been submitted -> updates the table
  43. */
  44. if (isset($_REQUEST['do_save_data'])) {
  45. $regenerate = PMA_updateColumns($db, $table);
  46. if ($regenerate) {
  47. // This happens when updating failed
  48. // @todo: do something appropriate
  49. } else {
  50. // continue to show the table's structure
  51. unset($_REQUEST['selected']);
  52. unset($_REQUEST['true_selected']);
  53. }
  54. }
  55. /**
  56. * handle multiple field commands if required
  57. *
  58. * submit_mult_*_x comes from IE if <input type="img" ...> is used
  59. */
  60. if (isset($_REQUEST['submit_mult_change_x'])) {
  61. $submit_mult = 'change';
  62. } elseif (isset($_REQUEST['submit_mult_drop_x'])) {
  63. $submit_mult = 'drop';
  64. } elseif (isset($_REQUEST['submit_mult_primary_x'])) {
  65. $submit_mult = 'primary';
  66. } elseif (isset($_REQUEST['submit_mult_index_x'])) {
  67. $submit_mult = 'index';
  68. } elseif (isset($_REQUEST['submit_mult_unique_x'])) {
  69. $submit_mult = 'unique';
  70. } elseif (isset($_REQUEST['submit_mult_spatial_x'])) {
  71. $submit_mult = 'spatial';
  72. } elseif (isset($_REQUEST['submit_mult_fulltext_x'])) {
  73. $submit_mult = 'ftext';
  74. } elseif (isset($_REQUEST['submit_mult_browse_x'])) {
  75. $submit_mult = 'browse';
  76. } elseif (isset($_REQUEST['submit_mult'])) {
  77. $submit_mult = $_REQUEST['submit_mult'];
  78. } elseif (isset($_REQUEST['mult_btn']) && $_REQUEST['mult_btn'] == __('Yes')) {
  79. $submit_mult = 'row_delete';
  80. if (isset($_REQUEST['selected'])) {
  81. $_REQUEST['selected_fld'] = $_REQUEST['selected'];
  82. }
  83. }
  84. if (! empty($submit_mult)) {
  85. if (isset($_REQUEST['selected_fld'])) {
  86. $err_url = 'tbl_structure.php?' . PMA_generate_common_url($db, $table);
  87. if ($submit_mult == 'browse') {
  88. // browsing the table displaying only selected fields/columns
  89. $GLOBALS['active_page'] = 'sql.php';
  90. $sql_query = '';
  91. foreach ($_REQUEST['selected_fld'] as $idx => $sval) {
  92. if ($sql_query == '') {
  93. $sql_query .= 'SELECT ' . PMA_Util::backquote($sval);
  94. } else {
  95. $sql_query .= ', ' . PMA_Util::backquote($sval);
  96. }
  97. }
  98. $sql_query .= ' FROM ' . PMA_Util::backquote($db)
  99. . '.' . PMA_Util::backquote($table);
  100. include 'sql.php';
  101. exit;
  102. } else {
  103. // handle multiple field commands
  104. // handle confirmation of deleting multiple fields/columns
  105. $action = 'tbl_structure.php';
  106. include 'libraries/mult_submits.inc.php';
  107. /**
  108. * if $submit_mult == 'change', execution will have stopped
  109. * at this point
  110. */
  111. if (empty($message)) {
  112. $message = PMA_Message::success();
  113. }
  114. }
  115. } else {
  116. $response = PMA_Response::getInstance();
  117. $response->isSuccess(false);
  118. $response->addJSON('message', __('No column selected.'));
  119. }
  120. }
  121. /**
  122. * Gets the relation settings
  123. */
  124. $cfgRelation = PMA_getRelationsParam();
  125. /**
  126. * Runs common work
  127. */
  128. require_once 'libraries/tbl_common.inc.php';
  129. $url_query .= '&amp;goto=tbl_structure.php&amp;back=tbl_structure.php';
  130. $url_params['goto'] = 'tbl_structure.php';
  131. $url_params['back'] = 'tbl_structure.php';
  132. // Check column names for MySQL reserved words
  133. if ($cfg['ReservedWordDisableWarning'] === false) {
  134. $pma_table = new PMA_Table($table, $db);
  135. $columns = $pma_table->getReservedColumnNames();
  136. if (! empty($columns)) {
  137. foreach ($columns as $column) {
  138. $msg = PMA_message::notice(
  139. __('The column name \'%s\' is a MySQL reserved keyword.')
  140. );
  141. $msg->addParam($column);
  142. $response->addHTML($msg);
  143. }
  144. }
  145. }
  146. /**
  147. * Prepares the table structure display
  148. */
  149. /**
  150. * Gets tables informations
  151. */
  152. require_once 'libraries/tbl_info.inc.php';
  153. require_once 'libraries/Index.class.php';
  154. // 2. Gets table keys and retains them
  155. // @todo should be: $server->db($db)->table($table)->primary()
  156. $primary = PMA_Index::getPrimary($table, $db);
  157. $columns_with_unique_index = array();
  158. foreach (PMA_Index::getFromTable($table, $db) as $index) {
  159. if ($index->isUnique() && $index->getChoice() == 'UNIQUE') {
  160. $columns = $index->getColumns();
  161. foreach ($columns as $column_name => $dummy) {
  162. $columns_with_unique_index[$column_name] = 1;
  163. }
  164. }
  165. }
  166. unset($index, $columns, $column_name, $dummy);
  167. // 3. Get fields
  168. $fields = (array) PMA_DBI_get_columns($db, $table, null, true);
  169. // Get more complete field information
  170. // For now, this is done just for MySQL 4.1.2+ new TIMESTAMP options
  171. // but later, if the analyser returns more information, it
  172. // could be executed for any MySQL version and replace
  173. // the info given by SHOW FULL COLUMNS FROM.
  174. //
  175. // We also need this to correctly learn if a TIMESTAMP is NOT NULL, since
  176. // SHOW FULL COLUMNS or INFORMATION_SCHEMA incorrectly says NULL
  177. // and SHOW CREATE TABLE says NOT NULL (tested
  178. // in MySQL 4.0.25 and 5.0.21, http://bugs.mysql.com/20910).
  179. $show_create_table = PMA_DBI_fetch_value(
  180. 'SHOW CREATE TABLE ' . PMA_Util::backquote($db) . '.'
  181. . PMA_Util::backquote($table),
  182. 0, 1
  183. );
  184. $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
  185. /**
  186. * prepare table infos
  187. */
  188. // action titles (image or string)
  189. $titles = PMA_getActionTitlesArray();
  190. // hidden action titles (image and string)
  191. $hidden_titles = PMA_getHiddenTitlesArray();
  192. /**
  193. * Displays the table structure ('show table' works correct since 3.23.03)
  194. */
  195. /* TABLE INFORMATION */
  196. // table header
  197. $HideStructureActions = '';
  198. if (in_array(
  199. $GLOBALS['cfg']['ActionLinksMode'],
  200. array('text', 'both')
  201. )
  202. && $GLOBALS['cfg']['HideStructureActions'] === true
  203. ) {
  204. $HideStructureActions .= ' HideStructureActions';
  205. }
  206. $html_form = '<form method="post" action="tbl_structure.php" name="fieldsForm" '
  207. . 'id="fieldsForm" class="ajax' . $HideStructureActions . '">';
  208. $response->addHTML($html_form);
  209. $response->addHTML(PMA_generate_common_hidden_inputs($db, $table));
  210. $tabletype = '<input type="hidden" name="table_type" value=';
  211. if ($db_is_information_schema) {
  212. $tabletype .= '"information_schema" />';
  213. } else if ($tbl_is_view) {
  214. $tabletype .= '"view" />';
  215. } else {
  216. $tabletype .= '"table" />';
  217. }
  218. $response->addHTML($tabletype);
  219. $tablestructure = '<table id="tablestructure" class="data">';
  220. $response->addHTML($tablestructure);
  221. $response->addHTML(
  222. PMA_getHtmlForTableStructureHeader(
  223. $db_is_information_schema,
  224. $tbl_is_view
  225. )
  226. );
  227. $response->addHTML('<tbody>');
  228. // table body
  229. // prepare comments
  230. $comments_map = array();
  231. $mime_map = array();
  232. if ($GLOBALS['cfg']['ShowPropertyComments']) {
  233. include_once 'libraries/transformations.lib.php';
  234. $comments_map = PMA_getComments($db, $table);
  235. if ($cfgRelation['mimework'] && $cfg['BrowseMIME']) {
  236. $mime_map = PMA_getMIME($db, $table, true);
  237. }
  238. }
  239. $rownum = 0;
  240. $columns_list = array();
  241. $save_row = array();
  242. $odd_row = true;
  243. foreach ($fields as $row) {
  244. $save_row[] = $row;
  245. $rownum++;
  246. $columns_list[] = $row['Field'];
  247. $type = $row['Type'];
  248. $extracted_columnspec = PMA_Util::extractColumnSpec($row['Type']);
  249. if ('set' == $extracted_columnspec['type']
  250. || 'enum' == $extracted_columnspec['type']
  251. ) {
  252. $type_nowrap = '';
  253. } else {
  254. $type_nowrap = ' class="nowrap"';
  255. }
  256. $type = $extracted_columnspec['print_type'];
  257. if (empty($type)) {
  258. $type = ' ';
  259. }
  260. $field_charset = '';
  261. if ($extracted_columnspec['can_contain_collation']
  262. && ! empty($row['Collation'])
  263. ) {
  264. $field_charset = $row['Collation'];
  265. }
  266. // Display basic mimetype [MIME]
  267. if ($cfgRelation['commwork']
  268. && $cfgRelation['mimework']
  269. && $cfg['BrowseMIME']
  270. && isset($mime_map[$row['Field']]['mimetype'])
  271. ) {
  272. $type_mime = '<br />MIME: '
  273. . str_replace('_', '/', $mime_map[$row['Field']]['mimetype']);
  274. } else {
  275. $type_mime = '';
  276. }
  277. $attribute = $extracted_columnspec['attribute'];
  278. // prepare a common variable to reuse below; however,
  279. // in case of a VIEW, $analyzed_sql[0]['create_table_fields'] is empty
  280. if (isset($analyzed_sql[0]['create_table_fields'][$row['Field']])) {
  281. $tempField = $analyzed_sql[0]['create_table_fields'][$row['Field']];
  282. } else {
  283. $tempField = array();
  284. }
  285. // MySQL 4.1.2+ TIMESTAMP options
  286. // (if on_update_current_timestamp is set, then it's TRUE)
  287. if (isset($tempField['on_update_current_timestamp'])) {
  288. $attribute = 'on update CURRENT_TIMESTAMP';
  289. }
  290. // here, we have a TIMESTAMP that SHOW FULL COLUMNS reports as having the
  291. // NULL attribute, but SHOW CREATE TABLE says the contrary. Believe
  292. // the latter.
  293. if (! empty($tempField['type'])
  294. && $tempField['type'] == 'TIMESTAMP'
  295. && $tempField['timestamp_not_null']
  296. ) {
  297. $row['Null'] = '';
  298. }
  299. if (! isset($row['Default'])) {
  300. if ($row['Null'] == 'YES') {
  301. $row['Default'] = '<i>NULL</i>';
  302. }
  303. } else {
  304. $row['Default'] = htmlspecialchars($row['Default']);
  305. }
  306. $field_encoded = urlencode($row['Field']);
  307. $field_name = htmlspecialchars($row['Field']);
  308. $displayed_field_name = $field_name;
  309. // underline commented fields and display a hover-title (CSS only)
  310. if (isset($comments_map[$row['Field']])) {
  311. $displayed_field_name = '<span class="commented_column" title="'
  312. . htmlspecialchars($comments_map[$row['Field']]) . '">'
  313. . $field_name . '</span>';
  314. }
  315. if ($primary && $primary->hasColumn($field_name)) {
  316. $displayed_field_name = '<u>' . $field_name . '</u>';
  317. }
  318. $response->addHTML(
  319. '<tr class="' . ($odd_row ? 'odd': 'even') . '">'
  320. );
  321. $odd_row = !$odd_row;
  322. $response->addHTML(
  323. PMA_getHtmlTableStructureRow(
  324. $row, $rownum, $displayed_field_name,
  325. $type_nowrap, $extracted_columnspec, $type_mime,
  326. $field_charset, $attribute, $tbl_is_view,
  327. $db_is_information_schema, $url_query, $field_encoded, $titles, $table
  328. )
  329. );
  330. if (! $tbl_is_view && ! $db_is_information_schema) {
  331. $response->addHTML(
  332. PMA_getHtmlForActionsInTableStructure(
  333. $type, $tbl_storage_engine, $primary,
  334. $field_name, $url_query, $titles, $row, $rownum,
  335. $hidden_titles, $columns_with_unique_index
  336. )
  337. );
  338. } // end if (! $tbl_is_view && ! $db_is_information_schema)
  339. $response->addHTML('</tr>');
  340. unset($field_charset);
  341. } // end foreach
  342. $response->addHTML('</tbody></table>');
  343. $response->addHTML(
  344. PMA_getHtmlForCheckAllTableColumn(
  345. $pmaThemeImage, $text_dir, $tbl_is_view,
  346. $db_is_information_schema, $tbl_storage_engine
  347. )
  348. );
  349. $response->addHTML(
  350. '</form><hr />'
  351. );
  352. $response->addHTML(
  353. PMA_getHtmlDivForMoveColumnsDialog()
  354. );
  355. /**
  356. * Work on the table
  357. */
  358. if ($tbl_is_view) {
  359. $response->addHTML(PMA_getHtmlForEditView($url_params));
  360. }
  361. $response->addHTML(
  362. PMA_getHtmlForOptionalActionLinks(
  363. $url_query, $tbl_is_view, $db_is_information_schema,
  364. $tbl_storage_engine, $cfgRelation
  365. )
  366. );
  367. if (! $tbl_is_view && ! $db_is_information_schema) {
  368. $response->addHTML('<br />');
  369. $response->addHTML(PMA_getHtmlForAddColumn($columns_list));
  370. $response->addHTML(
  371. '<div id="index_div" class="ajax" >'
  372. );
  373. }
  374. /**
  375. * Displays indexes
  376. */
  377. if (! $tbl_is_view
  378. && ! $db_is_information_schema
  379. && 'ARCHIVE' != $tbl_storage_engine
  380. ) {
  381. //return the list of index
  382. $response->addJSON('indexes_list', PMA_Index::getView($GLOBALS['table'], $GLOBALS['db']));
  383. $response->addHTML(PMA_getHtmlForDisplayIndexes());
  384. }
  385. /**
  386. * Displays Space usage and row statistics
  387. */
  388. // BEGIN - Calc Table Space
  389. // Get valid statistics whatever is the table type
  390. if ($cfg['ShowStats']) {
  391. //get table stats in HTML format
  392. $tablestats = PMA_getHtmlForDisplayTableStats(
  393. $showtable, $table_info_num_rows, $tbl_is_view,
  394. $db_is_information_schema, $tbl_storage_engine,
  395. $url_query, $tbl_collation
  396. );
  397. //returning the response in JSON format to be used by Ajax
  398. $response->addJSON('tableStat', $tablestats);
  399. $response->addHTML($tablestats);
  400. }
  401. // END - Calc Table Space
  402. $response->addHTML(
  403. '<div class="clearfloat"></div>'
  404. );
  405. ?>