acknowledgments update
[openemr.git] / phpmyadmin / tbl_structure.php
blob1999883ce2363d724520fb06881f505bf4468151
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
7 * @package PhpMyAdmin
8 */
10 /**
13 require_once 'libraries/common.inc.php';
14 require_once 'libraries/mysql_charsets.lib.php';
16 /**
17 * Function implementations for this script
19 require_once 'libraries/structure.lib.php';
20 require_once 'libraries/index.lib.php';
22 $response = PMA_Response::getInstance();
23 $header = $response->getHeader();
24 $scripts = $header->getScripts();
25 $scripts->addFile('tbl_structure.js');
26 $scripts->addFile('indexes.js');
28 /**
29 * Handle column moving
31 if (isset($_REQUEST['move_columns'])
32 && is_array($_REQUEST['move_columns'])
33 && $response->isAjax()
34 ) {
35 PMA_moveColumns($db, $table);
36 exit;
39 /**
40 * A click on Change has been made for one column
42 if (isset($_REQUEST['change_column'])) {
43 PMA_displayHtmlForColumnChange($db, $table, null, 'tbl_structure.php');
44 exit;
46 /**
47 * Modifications have been submitted -> updates the table
49 if (isset($_REQUEST['do_save_data'])) {
50 $regenerate = PMA_updateColumns($db, $table);
51 if ($regenerate) {
52 // This happens when updating failed
53 // @todo: do something appropriate
54 } else {
55 // continue to show the table's structure
56 unset($_REQUEST['selected']);
57 unset($_REQUEST['true_selected']);
61 /**
62 * handle multiple field commands if required
64 * submit_mult_*_x comes from IE if <input type="img" ...> is used
66 if (isset($_REQUEST['submit_mult_change_x'])) {
67 $submit_mult = 'change';
68 } elseif (isset($_REQUEST['submit_mult_drop_x'])) {
69 $submit_mult = 'drop';
70 } elseif (isset($_REQUEST['submit_mult_primary_x'])) {
71 $submit_mult = 'primary';
72 } elseif (isset($_REQUEST['submit_mult_index_x'])) {
73 $submit_mult = 'index';
74 } elseif (isset($_REQUEST['submit_mult_unique_x'])) {
75 $submit_mult = 'unique';
76 } elseif (isset($_REQUEST['submit_mult_spatial_x'])) {
77 $submit_mult = 'spatial';
78 } elseif (isset($_REQUEST['submit_mult_fulltext_x'])) {
79 $submit_mult = 'ftext';
80 } elseif (isset($_REQUEST['submit_mult_browse_x'])) {
81 $submit_mult = 'browse';
82 } elseif (isset($_REQUEST['submit_mult'])) {
83 $submit_mult = $_REQUEST['submit_mult'];
84 } elseif (isset($_REQUEST['mult_btn']) && $_REQUEST['mult_btn'] == __('Yes')) {
85 $submit_mult = 'row_delete';
86 if (isset($_REQUEST['selected'])) {
87 $_REQUEST['selected_fld'] = $_REQUEST['selected'];
90 if (! empty($submit_mult)) {
91 if (isset($_REQUEST['selected_fld'])) {
92 $err_url = 'tbl_structure.php?' . PMA_generate_common_url($db, $table);
93 if ($submit_mult == 'browse') {
94 // browsing the table displaying only selected fields/columns
95 $GLOBALS['active_page'] = 'sql.php';
96 $sql_query = '';
97 foreach ($_REQUEST['selected_fld'] as $idx => $sval) {
98 if ($sql_query == '') {
99 $sql_query .= 'SELECT ' . PMA_Util::backquote($sval);
100 } else {
101 $sql_query .= ', ' . PMA_Util::backquote($sval);
104 $sql_query .= ' FROM ' . PMA_Util::backquote($db)
105 . '.' . PMA_Util::backquote($table);
106 include 'sql.php';
107 exit;
108 } else {
109 // handle multiple field commands
110 // handle confirmation of deleting multiple fields/columns
111 $action = 'tbl_structure.php';
112 include 'libraries/mult_submits.inc.php';
114 * if $submit_mult == 'change', execution will have stopped
115 * at this point
118 if (empty($message)) {
119 $message = PMA_Message::success();
122 } else {
123 $response = PMA_Response::getInstance();
124 $response->isSuccess(false);
125 $response->addJSON('message', __('No column selected.'));
130 * Gets the relation settings
132 $cfgRelation = PMA_getRelationsParam();
135 * Runs common work
137 require_once 'libraries/tbl_common.inc.php';
138 $url_query .= '&amp;goto=tbl_structure.php&amp;back=tbl_structure.php';
139 $url_params['goto'] = 'tbl_structure.php';
140 $url_params['back'] = 'tbl_structure.php';
142 // Check column names for MySQL reserved words
143 if ($cfg['ReservedWordDisableWarning'] === false) {
144 $pma_table = new PMA_Table($table, $db);
145 $columns = $pma_table->getReservedColumnNames();
146 if (! empty($columns)) {
147 foreach ($columns as $column) {
148 $msg = PMA_message::notice(
149 __('The column name \'%s\' is a MySQL reserved keyword.')
151 $msg->addParam($column);
152 $response->addHTML($msg);
158 * Prepares the table structure display
163 * Gets tables informations
165 require_once 'libraries/tbl_info.inc.php';
167 require_once 'libraries/Index.class.php';
169 // 2. Gets table keys and retains them
170 // @todo should be: $server->db($db)->table($table)->primary()
171 $primary = PMA_Index::getPrimary($table, $db);
173 $columns_with_unique_index = array();
174 foreach (PMA_Index::getFromTable($table, $db) as $index) {
175 if ($index->isUnique() && $index->getChoice() == 'UNIQUE') {
176 $columns = $index->getColumns();
177 foreach ($columns as $column_name => $dummy) {
178 $columns_with_unique_index[$column_name] = 1;
182 unset($index, $columns, $column_name, $dummy);
184 // 3. Get fields
185 $fields = (array) PMA_DBI_get_columns($db, $table, null, true);
187 // Get more complete field information
188 // For now, this is done just for MySQL 4.1.2+ new TIMESTAMP options
189 // but later, if the analyser returns more information, it
190 // could be executed for any MySQL version and replace
191 // the info given by SHOW FULL COLUMNS FROM.
193 // We also need this to correctly learn if a TIMESTAMP is NOT NULL, since
194 // SHOW FULL COLUMNS or INFORMATION_SCHEMA incorrectly says NULL
195 // and SHOW CREATE TABLE says NOT NULL (tested
196 // in MySQL 4.0.25 and 5.0.21, http://bugs.mysql.com/20910).
198 $show_create_table = PMA_DBI_fetch_value(
199 'SHOW CREATE TABLE ' . PMA_Util::backquote($db) . '.'
200 . PMA_Util::backquote($table),
201 0, 1
203 $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
206 * prepare table infos
208 // action titles (image or string)
209 $titles = PMA_getActionTitlesArray();
211 // hidden action titles (image and string)
212 $hidden_titles = PMA_getHiddenTitlesArray();
215 * Displays the table structure ('show table' works correct since 3.23.03)
217 /* TABLE INFORMATION */
218 // table header
221 $HideStructureActions = '';
222 if ($GLOBALS['cfg']['PropertiesIconic'] !== true
223 && $GLOBALS['cfg']['HideStructureActions'] === true
225 $HideStructureActions .= ' HideStructureActions';
228 $html_form = '<form method="post" action="tbl_structure.php" name="fieldsForm" '
229 . 'id="fieldsForm" class="ajax' . $HideStructureActions . '">';
231 $response->addHTML($html_form);
232 $response->addHTML(PMA_generate_common_hidden_inputs($db, $table));
234 $tabletype = '<input type="hidden" name="table_type" value=';
235 if ($db_is_information_schema) {
236 $tabletype .= '"information_schema" />';
237 } else if ($tbl_is_view) {
238 $tabletype .= '"view" />';
239 } else {
240 $tabletype .= '"table" />';
242 $response->addHTML($tabletype);
244 $tablestructure = '<table id="tablestructure" class="data">';
245 $response->addHTML($tablestructure);
248 $response->addHTML(
249 PMA_getHtmlForTableStructureHeader(
250 $db_is_information_schema,
251 $tbl_is_view
255 $response->addHTML('<tbody>');
257 // table body
259 // prepare comments
260 $comments_map = array();
261 $mime_map = array();
263 if ($GLOBALS['cfg']['ShowPropertyComments']) {
264 include_once 'libraries/transformations.lib.php';
265 $comments_map = PMA_getComments($db, $table);
266 if ($cfgRelation['mimework'] && $cfg['BrowseMIME']) {
267 $mime_map = PMA_getMIME($db, $table, true);
271 $rownum = 0;
272 $columns_list = array();
273 $save_row = array();
274 $odd_row = true;
275 foreach ($fields as $row) {
276 $save_row[] = $row;
277 $rownum++;
278 $columns_list[] = $row['Field'];
280 $type = $row['Type'];
281 $extracted_columnspec = PMA_Util::extractColumnSpec($row['Type']);
283 if ('set' == $extracted_columnspec['type']
284 || 'enum' == $extracted_columnspec['type']
286 $type_nowrap = '';
287 } else {
288 $type_nowrap = ' class="nowrap"';
290 $type = $extracted_columnspec['print_type'];
291 if (empty($type)) {
292 $type = ' ';
295 $field_charset = '';
296 if ($extracted_columnspec['can_contain_collation']
297 && ! empty($row['Collation'])
299 $field_charset = $row['Collation'];
302 // Display basic mimetype [MIME]
303 if ($cfgRelation['commwork']
304 && $cfgRelation['mimework']
305 && $cfg['BrowseMIME']
306 && isset($mime_map[$row['Field']]['mimetype'])
308 $type_mime = '<br />MIME: '
309 . str_replace('_', '/', $mime_map[$row['Field']]['mimetype']);
310 } else {
311 $type_mime = '';
314 $attribute = $extracted_columnspec['attribute'];
316 // prepare a common variable to reuse below; however,
317 // in case of a VIEW, $analyzed_sql[0]['create_table_fields'] is empty
318 if (isset($analyzed_sql[0]['create_table_fields'][$row['Field']])) {
319 $tempField = $analyzed_sql[0]['create_table_fields'][$row['Field']];
320 } else {
321 $tempField = array();
324 // MySQL 4.1.2+ TIMESTAMP options
325 // (if on_update_current_timestamp is set, then it's TRUE)
326 if (isset($tempField['on_update_current_timestamp'])) {
327 $attribute = 'on update CURRENT_TIMESTAMP';
330 // here, we have a TIMESTAMP that SHOW FULL COLUMNS reports as having the
331 // NULL attribute, but SHOW CREATE TABLE says the contrary. Believe
332 // the latter.
333 if (! empty($tempField['type'])
334 && $tempField['type'] == 'TIMESTAMP'
335 && $tempField['timestamp_not_null']
337 $row['Null'] = '';
341 if (! isset($row['Default'])) {
342 if ($row['Null'] == 'YES') {
343 $row['Default'] = '<i>NULL</i>';
345 } else {
346 $row['Default'] = htmlspecialchars($row['Default']);
349 $field_encoded = urlencode($row['Field']);
350 $field_name = htmlspecialchars($row['Field']);
351 $displayed_field_name = $field_name;
353 // underline commented fields and display a hover-title (CSS only)
355 if (isset($comments_map[$row['Field']])) {
356 $displayed_field_name = '<span class="commented_column" title="'
357 . htmlspecialchars($comments_map[$row['Field']]) . '">'
358 . $field_name . '</span>';
361 if ($primary && $primary->hasColumn($field_name)) {
362 $displayed_field_name = '<u>' . $field_name . '</u>';
364 $response->addHTML(
365 '<tr class="' . ($odd_row ? 'odd': 'even') . '">'
367 $odd_row = !$odd_row;
369 $response->addHTML(
370 PMA_getHtmlTableStructureRow(
371 $row, $rownum, $displayed_field_name,
372 $type_nowrap, $extracted_columnspec, $type_mime,
373 $field_charset, $attribute, $tbl_is_view,
374 $db_is_information_schema, $url_query, $field_encoded, $titles, $table
378 if (! $tbl_is_view && ! $db_is_information_schema) {
379 $response->addHTML(
380 PMA_getHtmlForActionsInTableStructure(
381 $type, $tbl_storage_engine, $primary,
382 $field_name, $url_query, $titles, $row, $rownum,
383 $hidden_titles, $columns_with_unique_index
386 } // end if (! $tbl_is_view && ! $db_is_information_schema)
388 $response->addHTML('</tr>');
390 unset($field_charset);
391 } // end foreach
393 $response->addHTML('</tbody></table>');
395 $response->addHTML(
396 PMA_getHtmlForCheckAllTableColumn(
397 $pmaThemeImage, $text_dir, $tbl_is_view,
398 $db_is_information_schema, $tbl_storage_engine
402 $response->addHTML(
403 '</form><hr />'
405 $response->addHTML(
406 PMA_getHtmlDivForMoveColumnsDialog()
410 * Work on the table
413 if ($tbl_is_view) {
414 $response->addHTML(PMA_getHtmlForEditView($url_params));
416 $response->addHTML(
417 PMA_getHtmlForOptionalActionLinks(
418 $url_query, $tbl_is_view, $db_is_information_schema,
419 $tbl_storage_engine, $cfgRelation
423 if (! $tbl_is_view && ! $db_is_information_schema) {
424 $response->addHTML('<br />');
425 $response->addHTML(PMA_getHtmlForAddColumn($columns_list));
426 $response->addHTML(
427 '<div id="index_div" class="ajax" >'
432 * Displays indexes
435 if (! $tbl_is_view
436 && ! $db_is_information_schema
437 && 'ARCHIVE' != $tbl_storage_engine
439 //return the list of index
440 $response->addJSON('indexes_list', PMA_Index::getView($GLOBALS['table'], $GLOBALS['db']));
441 $response->addHTML(PMA_getHtmlForDisplayIndexes());
445 * Displays Space usage and row statistics
447 // BEGIN - Calc Table Space
448 // Get valid statistics whatever is the table type
449 if ($cfg['ShowStats']) {
450 //get table stats in HTML format
451 $tablestats = PMA_getHtmlForDisplayTableStats(
452 $showtable, $table_info_num_rows, $tbl_is_view,
453 $db_is_information_schema, $tbl_storage_engine,
454 $url_query, $tbl_collation
456 //returning the response in JSON format to be used by Ajax
457 $response->addJSON('tableStat', $tablestats);
458 $response->addHTML($tablestats);
460 // END - Calc Table Space
462 $response->addHTML(
463 '<div class="clearfloat"></div>'