2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * display information about indexes in a table
10 * Gets some core libraries
12 require_once './libraries/common.inc.php';
13 require_once './libraries/tbl_indexes.lib.php';
16 * Ensures the db & table are valid, then loads headers and gets indexes
18 * Skipped if this script is called by "tbl_sql.php"
20 if (!defined('PMA_IDX_INCLUDED')) {
21 // Not a valid db name -> back to the welcome page
23 $is_db = PMA_DBI_select_db($db);
25 if (!strlen($db) ||
!$is_db) {
26 $uri_params = array('reload' => '1');
27 if (isset($message)) {
28 $uri_params['message'] = $message;
30 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . 'main.php'
31 . PMA_generate_common_url($uri_params, '&'));
34 // Not a valid table name -> back to the default db sub-page
36 $is_table = PMA_DBI_query('SHOW TABLES LIKE \''
37 . PMA_sqlAddslashes($table, TRUE) . '\'', null, PMA_DBI_QUERY_STORE
);
40 ||
!($is_table && PMA_DBI_num_rows($is_table))) {
41 $uri_params = array('reload' => '1', 'db' => $db);
42 if (isset($message)) {
43 $uri_params['message'] = $message;
45 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri']
46 . $cfg['DefaultTabDatabase']
47 . PMA_generate_common_url($uri_params, '&'));
49 } elseif (isset($is_table)) {
50 PMA_DBI_free_result($is_table);
53 // Displays headers (if needed)
54 $js_to_run = isset($index) && isset($do_save_data)
57 require_once './libraries/header.inc.php';
62 * Gets fields and indexes informations
64 if (!defined('PMA_IDX_INCLUDED')) {
65 $err_url_0 = 'db_sql.php?' . PMA_generate_common_url($db);
68 // Gets table keys and store them in arrays
70 $indexes_info = array();
71 $indexes_data = array();
72 // keys had already been grabbed in "tbl_sql.php"
73 if (!defined('PMA_IDX_INCLUDED')) {
74 $ret_keys = PMA_get_indexes($table, $err_url_0);
77 PMA_extract_indexes($ret_keys, $indexes, $indexes_info, $indexes_data);
79 // Get fields and stores their name/type
80 // fields had already been grabbed in "tbl_sql.php"
81 if (!defined('PMA_IDX_INCLUDED')) {
82 $fields_rs = PMA_DBI_query('SHOW FIELDS FROM '
83 . PMA_backquote($table) . ';');
85 while ($row = PMA_DBI_fetch_assoc($fields_rs)) {
90 $fields_names = array();
91 $fields_types = array();
92 foreach ($save_row AS $saved_row_key => $row) {
93 $fields_names[] = $row['Field'];
94 // loic1: set or enum types: slashes single quotes inside options
95 if (preg_match('@^(set|enum)\((.+)\)$@i', $row['Type'], $tmp)) {
96 $tmp[2] = substr(preg_replace('@([^,])\'\'@', '\\1\\\'',
98 $fields_types[] = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
100 $fields_types[] = $row['Type'];
105 PMA_DBI_free_result($fields_rs);
110 * Do run the query to build the new index and moves back to
113 if (!defined('PMA_IDX_INCLUDED')
114 && (isset($index) && isset($do_save_data))) {
116 $err_url = 'tbl_indexes.php?' . PMA_generate_common_url($db, $table);
117 if (empty($old_index)) {
118 $err_url .= '&create_index=1&idx_num_fields=' . $idx_num_fields;
120 $err_url .= '&index=' . urlencode($old_index);
123 if ($index_type == 'PRIMARY') {
126 } elseif ($index != 'PRIMARY') {
127 PMA_mysqlDie($strPrimaryKeyName, '', FALSE, $err_url);
129 } elseif ($index == 'PRIMARY') {
130 PMA_mysqlDie($strCantRenameIdxToPrimary, '', FALSE, $err_url);
134 // $sql_query is the one displayed in the query box
135 $sql_query = 'ALTER TABLE ' . PMA_backquote($table);
137 // Drops the old index
138 if (!empty($old_index)) {
139 if ($old_index == 'PRIMARY') {
140 $sql_query .= ' DROP PRIMARY KEY,';
142 $sql_query .= ' DROP INDEX ' . PMA_backquote($old_index) .',';
146 // Builds the new one
147 switch ($index_type) {
149 $sql_query .= ' ADD PRIMARY KEY (';
152 $sql_query .= ' ADD FULLTEXT '
153 . (empty($index) ?
'' : PMA_backquote($index)) . ' (';
156 $sql_query .= ' ADD UNIQUE '
157 . (empty($index) ?
'' : PMA_backquote($index)) . ' (';
160 $sql_query .= ' ADD INDEX '
161 . (empty($index) ?
'' : PMA_backquote($index)) . ' (';
165 foreach ($column AS $i => $name) {
166 if ($name != '--ignore--') {
167 $index_fields .= (empty($index_fields) ?
'' : ',')
168 . PMA_backquote($name)
169 . (empty($sub_part[$i])
171 : '(' . $sub_part[$i] . ')');
174 if (empty($index_fields)){
175 PMA_mysqlDie($strNoIndexPartsDefined, '', FALSE, $err_url);
177 $sql_query .= $index_fields . ')';
180 $result = PMA_DBI_query($sql_query);
181 $message = $strTable . ' ' . htmlspecialchars($table) . ' '
182 . $strHasBeenAltered;
184 $active_page = 'tbl_structure.php';
185 require './tbl_structure.php';
186 } // end builds the new index
190 * Edits an index or defines a new one
192 elseif (!defined('PMA_IDX_INCLUDED')
193 && (isset($index) ||
isset($create_index))) {
195 // Prepares the form values
196 if (!isset($index)) {
199 if (!isset($old_index)){
202 if (!isset($index_type)) {
205 if ($old_index == '' ||
!isset($indexes_info[$old_index])) {
206 $edited_index_info['Sequences'] = array();
207 $edited_index_data = array();
208 for ($i = 1; $i <= $idx_num_fields; $i++
) {
209 $edited_index_info['Sequences'][] = $i;
210 $edited_index_data[$i] = array('Column_name' => '',
214 && !isset($indexes_info['PRIMARY'])
215 && ($index_type == '' ||
$index_type == 'PRIMARY')) {
216 $old_index = 'PRIMARY';
219 $edited_index_info = $indexes_info[$old_index];
220 $edited_index_data = $indexes_data[$old_index];
223 if ((PMA_MYSQL_INT_VERSION
< 40002
224 && $edited_index_info['Comment'] == 'FULLTEXT')
225 ||
(PMA_MYSQL_INT_VERSION
>= 40002
226 && $edited_index_info['Index_type'] == 'FULLTEXT')) {
227 $index_type = 'FULLTEXT';
228 } elseif ($index == 'PRIMARY') {
229 $index_type = 'PRIMARY';
230 } elseif ($edited_index_info['Non_unique'] == '0') {
231 $index_type = 'UNIQUE';
233 $index_type = 'INDEX';
235 } // end if... else...
237 if (isset($add_fields)) {
238 if (isset($prev_add_fields)) {
239 $added_fields +
= $prev_add_fields;
241 $field_cnt = count($edited_index_info['Sequences']) +
1;
242 for ($i = $field_cnt; $i < ($added_fields +
$field_cnt); $i++
) {
243 $edited_index_info['Sequences'][] = $i;
244 $edited_index_data[$i] = array('Column_name' => '',
248 // Restore entered values
249 foreach ($column AS $i => $name) {
250 if ($name != '--ignore--'){
251 $edited_index_data[$i+
1]['Column_name'] = $name;
252 $edited_index_data[$i+
1]['Sub_part'] = $sub_part[$i];
256 // end preparing form values
259 <div style
="float: left;">
260 <form action
="./tbl_indexes.php" method
="post" name
="index_frm"
261 onsubmit
="if (typeof(this.elements['index'].disabled) != 'undefined') {
262 this.elements['index'].disabled = false}">
263 <?php
echo PMA_generate_common_hidden_inputs($db, $table); ?
>
265 if (isset($create_index)) {
266 echo '<input type="hidden" name="create_index" value="1" />' . "\n";
268 if (isset($added_fields)) {
269 echo ' <input type="hidden" name="prev_add_fields" value="'
270 . $added_fields . '" />' . "\n";
272 if (isset($idx_num_fields)) {
273 echo ' <input type="hidden" name="idx_num_fields" value="'
274 . $idx_num_fields . '" />' . "\n";
277 <input type
="hidden" name
="old_index" value
="<?php
278 echo (isset($create_index) ? '' : htmlspecialchars($old_index)); ?>" />
283 echo (isset($create_index) ?
$strCreateIndexTopic : $strModifyIndexTopic);
287 <div
class="formelement">
288 <label
for="input_index_name"><?php
echo $strIndexName; ?
></label
>
289 <input type
="text" name
="index" id
="input_index_name" size
="25"
290 value
="<?php echo htmlspecialchars($index); ?>" onfocus
="this.select()" />
293 <div
class="formelement">
294 <label
for="select_index_type"><?php
echo $strIndexType; ?
></label
>
295 <select name
="index_type" id
="select_index_type" onchange
="return checkIndexName()">
297 foreach (PMA_get_indextypes() as $each_index_type) {
298 if ($each_index_type === 'PRIMARY'
299 && $index !== 'PRIMARY'
300 && isset($indexes_info['PRIMARY'])) {
301 // skip PRIMARY if there is already one in the table
305 . '<option value="' . $each_index_type . '"'
306 . (($index_type == $each_index_type) ?
' selected="selected"' : '')
307 . '>'. $each_index_type . '</option>' . "\n";
311 <?php
echo PMA_showMySQLDocu('SQL-Syntax', 'ALTER_TABLE'); ?
>
315 <br
class="clearfloat" />
316 <div
class="warning"><?php
echo $strPrimaryKeyWarning; ?
></div
>
320 <tr
><th
><?php
echo $strField; ?
></th
>
321 <th
><?php
echo $strSize; ?
></th
>
327 foreach ($edited_index_info['Sequences'] as $seq_index) {
328 $add_type = (is_array($fields_types) && count($fields_types) == count($fields_names));
329 $selected = $edited_index_data[$seq_index]['Column_name'];
330 if (!empty($edited_index_data[$seq_index]['Sub_part'])) {
331 $sub_part = ' value="' . $edited_index_data[$seq_index]['Sub_part'] . '"';
337 <tr
class="<?php echo $odd_row ? 'odd' : 'even'; ?>">
338 <td
><select name
="column[]">
339 <option value
="--ignore--"
340 <?php
if ('--ignore--' == $selected) { echo ' selected="selected"'; } ?
>>
341 -- <?php
echo $strIgnore; ?
> --</option
>
343 foreach ($fields_names AS $key => $val) {
344 if ($index_type != 'FULLTEXT'
345 ||
preg_match('@^(varchar|text|tinytext|mediumtext|longtext)@i', $fields_types[$key])) {
347 . '<option value="' . htmlspecialchars($val) . '"'
348 . (($val == $selected) ?
' selected="selected"' : '') . '>'
349 . htmlspecialchars($val) . (($add_type) ?
' ['
350 . $fields_types[$key] . ']' : '') . '</option>' . "\n";
352 } // end foreach $fields_names
357 <td
><input type
="text" size
="5" onfocus
="this.select()"
358 name
="sub_part[]"<?php
echo $sub_part; ?
> />
362 $odd_row = !$odd_row;
363 } // end foreach $edited_index_info['Sequences']
369 <fieldset
class="tblFooters">
370 <input type
="submit" name
="do_save_data" value
="<?php echo $strSave; ?>" />
373 echo ' ' . sprintf($strAddToIndex,
374 '<input type="text" name="added_fields" size="2" value="1"'
375 .' onfocus="this.select()" />') . "\n";
376 echo ' <input type="submit" name="add_fields" value="' . $strGo . '"'
377 .' onclick="return checkFormElementInRange(this.form,'
378 .' \'added_fields\', \''
379 . str_replace('\'', '\\\'', $GLOBALS['strInvalidColumnCount'])
380 . '\', 1)" />' . "\n";
391 <form action
="./tbl_indexes.php" method
="post"
392 onsubmit
="return checkFormElementInRange(this, 'idx_num_fields',
393 '<?php echo str_replace('\'', '\\\'', $GLOBALS['strInvalidColumnCount']); ?>',
396 echo PMA_generate_common_hidden_inputs($db, $table);
398 <table id
="table_indexes" class="data">
399 <caption
class="tblHeaders">
401 echo $strIndexes . ':' . "\n";
402 echo ' ' . PMA_showMySQLDocu('optimization',
403 'optimizing-database-structure');
409 if (count($ret_keys) > 0) {
410 $edit_link_text = '';
411 $drop_link_text = '';
413 if ($cfg['PropertiesIconic'] === true ||
$cfg['PropertiesIconic'] === 'both') {
414 $edit_link_text = '<img class="icon" src="' . $pmaThemeImage
415 . 'b_edit.png" width="16" height="16" title="' . $strEdit
416 . '" alt="' . $strEdit . '" />';
417 $drop_link_text = '<img class="icon" src="' . $pmaThemeImage
418 . 'b_drop.png" width="16" height="16" title="' . $strDrop
419 . '" alt="' . $strDrop . '" />';
421 if ($cfg['PropertiesIconic'] === false ||
$cfg['PropertiesIconic'] === 'both') {
422 $edit_link_text .= $strEdit;
423 $drop_link_text .= $strDrop;
425 if ($cfg['PropertiesIconic'] === 'both') {
426 $edit_link_text = '<nobr>' . $edit_link_text . '</nobr>';
427 $drop_link_text = '<nobr>' . $drop_link_text . '</nobr>';
432 <tr
><th
><?php
echo $strKeyname; ?
></th
>
433 <th
><?php
echo $strType; ?
></th
>
434 <th
><?php
echo $strCardinality; ?
></th
>
435 <th colspan
="2"><?php
echo $strAction; ?
></th
>
436 <th colspan
="2"><?php
echo $strField; ?
></th
>
441 $idx_collection = PMA_show_indexes($table, $indexes, $indexes_info,
442 $indexes_data, true);
443 echo PMA_check_indexes($ret_keys);
444 } // end display indexes
448 .'<tr><td colspan="7"><div class="warning">' . $strNoIndex
449 .'</div></td></tr>' . "\n";
453 <tr
class="tblFooters"><td colspan
="7">
454 <?php
echo sprintf($strCreateIndex,
455 '<input type="text" size="2" name="idx_num_fields" value="1" />'); ?
>
456 <input type
="submit" name
="create_index" value
="<?php echo $strGo; ?>"
457 onclick
="return checkFormElementInRange(this.form,
459 '<?php echo str_replace('\'', '\\\'', $GLOBALS['strInvalidColumnCount']); ?>',
466 } // end display indexes
470 * Displays the footer
474 if (!defined('PMA_IDX_INCLUDED')){
475 require_once './libraries/footer.inc.php';