3 // vim: expandtab sw=4 ts=4 sts=4:
7 * Gets some core libraries
9 require_once('./libraries/grab_globals.lib.php');
10 require_once('./libraries/common.lib.php');
14 * Defines the index types ("FULLTEXT" is available since MySQL 3.23.23)
25 * Ensures the db & table are valid, then loads headers and gets indexes
27 * Skipped if this script is called by "tbl_properties.php"
29 if (!defined('PMA_IDX_INCLUDED')) {
30 // Not a valid db name -> back to the welcome page
32 $is_db = @PMA_mysql_select_db
($db);
34 if (empty($db) ||
!$is_db) {
35 header('Location: ' . $cfg['PmaAbsoluteUri'] . 'main.php?' . PMA_generate_common_url('', '', '&') . (isset($message) ?
'&message=' . urlencode($message) : '') . '&reload=1');
38 // Not a valid table name -> back to the default db_details sub-page
40 $is_table = @PMA_mysql_query
('SHOW TABLES LIKE \'' . PMA_sqlAddslashes($table, TRUE) . '\'');
43 ||
!($is_table && @mysql_numrows
($is_table))) {
44 header('Location: ' . $cfg['PmaAbsoluteUri'] . $cfg['DefaultTabDatabase'] . '?' . PMA_generate_common_url($db, '', '&') . (isset($message) ?
'&message=' . urlencode($message) : '') . '&reload=1');
46 } else if (isset($is_table)) {
47 mysql_free_result($is_table);
50 // Displays headers (if needed)
51 $js_to_run = ((isset($index) && isset($do_save_data)) ?
'functions.js' : 'indexes.js');
52 require_once('./header.inc.php');
57 * Gets fields and indexes informations
59 if (defined('PMA_IDX_INCLUDED')) {
60 $err_url_0 = 'db_details.php?' . PMA_generate_common_url($db);
63 // Gets table keys and store them in arrays
66 $indexes_info = array();
67 $indexes_data = array();
68 // keys had already been grabbed in "tbl_properties.php"
69 if (defined('PMA_IDX_INCLUDED')) {
70 $idx_cnt = count($ret_keys);
72 $local_query = 'SHOW KEYS FROM ' . PMA_backquote($table);
73 $result = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url_0);
74 $idx_cnt = mysql_num_rows($result);
77 for ($i = 0; $i < $idx_cnt; $i++
) {
78 $row = (defined('PMA_IDX_INCLUDED') ?
$ret_keys[$i] : PMA_mysql_fetch_array($result));
80 if ($row['Key_name'] != $prev_index ){
81 $indexes[] = $row['Key_name'];
82 $prev_index = $row['Key_name'];
84 $indexes_info[$row['Key_name']]['Sequences'][] = $row['Seq_in_index'];
85 $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique'];
86 if (isset($row['Cardinality'])) {
87 $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
89 // I don't know what does following column mean....
90 // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed'];
91 $indexes_info[$row['Key_name']]['Comment'] = (isset($row['Comment']))
94 $indexes_info[$row['Key_name']]['Index_type'] = (isset($row['Index_type']))
98 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name'];
99 if (isset($row['Sub_part'])) {
100 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
104 if (defined('PMA_IDX_INCLUDED')) {
106 } else if ($result) {
107 mysql_free_result($result);
110 // Get fields and stores their name/type
111 // fields had already been grabbed in "tbl_properties.php"
112 if (defined('PMA_IDX_INCLUDED')) {
113 mysql_data_seek($fields_rs, 0);
115 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($table);
116 $fields_rs = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url_0);
117 $fields_cnt = mysql_num_rows($fields_rs);
120 $fields_names = array();
121 $fields_types = array();
122 while ($row = PMA_mysql_fetch_array($fields_rs)) {
123 $fields_names[] = $row['Field'];
124 // loic1: set or enum types: slashes single quotes inside options
125 if (preg_match('@^(set|enum)\((.+)\)$@i', $row['Type'], $tmp)) {
126 $tmp[2] = substr(preg_replace('@([^,])\'\'@', '\\1\\\'', ',' . $tmp[2]), 1);
127 $fields_types[] = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
129 $fields_types[] = $row['Type'];
134 mysql_free_result($fields_rs);
139 * Do run the query to build the new index and moves back to
140 * "tbl_properties.php"
142 if (!defined('PMA_IDX_INCLUDED')
143 && (isset($index) && isset($do_save_data))) {
145 $err_url = 'tbl_indexes.php?' . PMA_generate_common_url($db, $table);
146 if (empty($old_index)) {
147 $err_url .= '&create_index=1&idx_num_fields=' . $idx_num_fields;
149 $err_url .= '&index=' . urlencode($old_index);
152 if ($index_type == 'PRIMARY') {
155 } else if ($index != 'PRIMARY') {
156 PMA_mysqlDie($strPrimaryKeyName, '', FALSE, $err_url);
158 } else if ($index == 'PRIMARY') {
159 PMA_mysqlDie($strCantRenameIdxToPrimary, '', FALSE, $err_url);
163 // $sql_query is the one displayed in the query box
164 $sql_query = 'ALTER TABLE ' . PMA_backquote($table);
166 // Drops the old index
167 if (!empty($old_index)) {
168 if ($old_index == 'PRIMARY') {
169 $sql_query .= ' DROP PRIMARY KEY,';
171 $sql_query .= ' DROP INDEX ' . PMA_backquote($old_index) .',';
175 // Builds the new one
176 switch ($index_type) {
178 $sql_query .= ' ADD PRIMARY KEY (';
181 $sql_query .= ' ADD FULLTEXT ' . (empty($index) ?
'' : PMA_backquote($index)) . ' (';
184 $sql_query .= ' ADD UNIQUE ' . (empty($index) ?
'' : PMA_backquote($index)) . ' (';
187 $sql_query .= ' ADD INDEX ' . (empty($index) ?
'' : PMA_backquote($index)) . ' (';
191 foreach($column AS $i => $name) {
192 if ($name != '--ignore--') {
193 $index_fields .= (empty($index_fields) ?
'' : ',')
194 . PMA_backquote($name)
195 . (empty($sub_part[$i]) ?
'' : '(' . $sub_part[$i] . ')');
198 if (empty($index_fields)){
199 PMA_mysqlDie($strNoIndexPartsDefined, '', FALSE, $err_url);
201 $sql_query .= $index_fields . ')';
204 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', $sql_query, FALSE, $err_url);
205 $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered;
207 $active_page = 'tbl_properties_structure.php';
208 require('./tbl_properties_structure.php');
209 } // end builds the new index
213 * Edits an index or defines a new one
215 else if (!defined('PMA_IDX_INCLUDED')
216 && (isset($index) ||
isset($create_index))) {
218 // Prepares the form values
219 if (!isset($index)) {
222 if (!isset($old_index)){
225 if (!isset($index_type)) {
228 if ($old_index == '' ||
!isset($indexes_info[$old_index])) {
229 $edited_index_info['Sequences'] = array();
230 $edited_index_data = array();
231 for ($i = 1; $i <= $idx_num_fields; $i++
) {
232 $edited_index_info['Sequences'][] = $i;
233 $edited_index_data[$i] = array('Column_name' => '', 'Sub_part' => '');
236 && !isset($indexes_info['PRIMARY'])
237 && ($index_type == '' ||
$index_type == 'PRIMARY')) {
238 $old_index = 'PRIMARY';
241 $edited_index_info = $indexes_info[$old_index];
242 $edited_index_data = $indexes_data[$old_index];
245 if ((PMA_MYSQL_INT_VERSION
< 40002 && $edited_index_info['Comment'] == 'FULLTEXT')
246 ||
(PMA_MYSQL_INT_VERSION
>= 40002 && $edited_index_info['Index_type'] == 'FULLTEXT')) {
247 $index_type = 'FULLTEXT';
248 } else if ($index == 'PRIMARY') {
249 $index_type = 'PRIMARY';
250 } else if ($edited_index_info['Non_unique'] == '0') {
251 $index_type = 'UNIQUE';
253 $index_type = 'INDEX';
255 } // end if... else...
257 if (isset($add_fields)) {
258 if (isset($prev_add_fields)) {
259 $added_fields +
= $prev_add_fields;
261 $field_cnt = count($edited_index_info['Sequences']) +
1;
262 for ($i = $field_cnt; $i < ($added_fields +
$field_cnt); $i++
) {
263 $edited_index_info['Sequences'][] = $i;
264 $edited_index_data[$i] = array('Column_name' => '', 'Sub_part' => '');
267 // Restore entered values
268 foreach($column AS $i => $name) {
269 if ($name != '--ignore--'){
270 $edited_index_data[$i+
1]['Column_name'] = $name;
271 $edited_index_data[$i+
1]['Sub_part'] = $sub_part[$i];
275 // end preparing form values
278 <!-- Build index form
-->
279 <form action
="tbl_indexes.php" method
="post" name
="index_frm"
280 onsubmit
="if (typeof(this.elements['index'].disabled) != 'undefined') {this.elements['index'].disabled = false}">
281 <?php
echo PMA_generate_common_hidden_inputs($db, $table); ?
>
283 if (isset($create_index)) {
284 echo '<input type="hidden" name="create_index" value="1" />';
288 <input type
="hidden" name
="old_index" value
="<?php echo (isset($create_index) ? '' : htmlspecialchars($old_index)); ?>" />
289 <b
><?php
echo '------ ' . (isset($create_index) ?
$strCreateIndexTopic : $strModifyIndexTopic) . ' ------'; ?
></b
>
294 <td
><?php
echo $strIndexName; ?
> 
;</td
>
296 <input type
="text" name
="index" value
="<?php echo htmlspecialchars($index); ?>" class="textfield" onfocus
="this.select()" />
297  
;<?php
echo $strPrimaryKeyWarning . "\n"; ?
>
301 <td
><?php
echo $strIndexType; ?
> 
;</td
>
303 <select name
="index_type" onchange
="return checkIndexName()">
306 for ($i = 0; $i < $index_types_cnt; $i++
) {
307 if ($index_types[$i] == 'PRIMARY') {
308 if ($index == 'PRIMARY' ||
!isset($indexes_info['PRIMARY'])) {
310 . '<option value="PRIMARY"' . (($index_type == 'PRIMARY') ?
' selected="selected"' : '') . '>PRIMARY</option>'
315 . '<option value="' . $index_types[$i] . '"' . (($index_type == $index_types[$i]) ?
' selected="selected"' : '') . '>'. $index_types[$i] . '</option>'
318 } // end if... else...
322 <?php
echo PMA_showMySQLDocu('Reference', 'ALTER_TABLE') . "\n"; ?
>
327 <table border
="<?php echo $cfg['Border']; ?>" cellpadding
="5">
329 <th
><?php
echo $strField; ?
></th
>
330 <th
><?php
echo $strSize; ?
></th
>
333 foreach($edited_index_info['Sequences'] AS $row_no => $seq_index) {
334 $add_type = (is_array($fields_types) && count($fields_types) == count($fields_names));
335 $selected = $edited_index_data[$seq_index]['Column_name'];
336 if (!empty($edited_index_data[$seq_index]['Sub_part'])) {
337 $sub_part = ' value="' . $edited_index_data[$seq_index]['Sub_part'] . '"';
341 $bgcolor = (($row_no %
2) ?
$cfg['BgcolorOne'] : $cfg['BgcolorTwo']);
345 <td bgcolor
="<?php echo $bgcolor; ?>">
346 <select name
="column[]">
347 <option value
="--ignore--"<?php
if ('--ignore--' == $selected) echo ' selected="selected"'; ?
>>
348 -- <?php
echo $strIgnore; ?
> --</option
>
350 foreach($fields_names AS $key => $val) {
351 if ($index_type != 'FULLTEXT'
352 ||
preg_match('@^(varchar|text|tinytext|mediumtext|longtext)@i', $fields_types[$key])) {
354 . '<option value="' . htmlspecialchars($val) . '"' . (($val == $selected) ?
' selected="selected"' : '') . '>'
355 . htmlspecialchars($val) . (($add_type) ?
' [' . $fields_types[$key] . ']' : '' ) . '</option>' . "\n";
362 <td bgcolor
="<?php echo $bgcolor; ?>">
363 <input type
="text" size
="5" name
="sub_part[]"<?php
echo $sub_part; ?
> onfocus
="this.select()" />
373 <input type
="submit" name
="do_save_data" value
="<?php echo $strSave; ?>" /><br
/>
377 if (isset($added_fields)) {
378 echo ' <input type="hidden" name="prev_add_fields" value="' . $added_fields . '" />';
380 if (isset($idx_num_fields)) {
381 echo ' <input type="hidden" name="idx_num_fields" value="' . $idx_num_fields . '" />' . "\n";
383 echo ' <hr /><br />' . "\n";
384 echo ' ' . sprintf($strAddToIndex, '<input type="text" name="added_fields" size="4" value="1" class="textfield" onfocus="this.select()" />') . "\n";
385 echo ' <input type="submit" name="add_fields" value="' . $strGo . '" onclick="return checkFormElementInRange(this.form, \'added_fields\', 1)" />' . "\n";
392 <!-- Indexes form
-->
393 <form action
="tbl_indexes.php" method
="post">
394 <?php
echo PMA_generate_common_hidden_inputs($db, $table); ?
>
397 echo ' ' . $strIndexes . ' :' . "\n";
398 echo ' ' . PMA_showMySQLDocu('MySQL_Optimisation', 'Optimising_Database_Structure') . '<br />' ."\n";
402 <table border
="<?php echo $cfg['Border']; ?>">
404 <th
><?php
echo $strKeyname; ?
></th
>
405 <th
><?php
echo $strType; ?
></th
>
406 <th
><?php
echo $strCardinality; ?
></th
>
407 <th colspan
="2"><?php
echo $strAction; ?
></th
>
408 <th colspan
="2"><?php
echo $strField; ?
></th
>
412 foreach($indexes AS $index_no => $index_name) {
413 $cell_bgd = (($index_no %
2) ?
$cfg['BgcolorOne'] : $cfg['BgcolorTwo']);
414 $index_td = ' <td bgcolor="' . $cell_bgd . '" rowspan="' . count($indexes_info[$index_name]['Sequences']) . '">' . "\n";
417 . ' ' . htmlspecialchars($index_name) . "\n"
420 if ((PMA_MYSQL_INT_VERSION
< 40002 && $indexes_info[$index_name]['Comment'] == 'FULLTEXT')
421 ||
(PMA_MYSQL_INT_VERSION
>= 40002 && $indexes_info[$index_name]['Index_type'] == 'FULLTEXT')) {
422 $index_type = 'FULLTEXT';
423 } else if ($index_name == 'PRIMARY') {
424 $index_type = 'PRIMARY';
425 } else if ($indexes_info[$index_name]['Non_unique'] == '0') {
426 $index_type = 'UNIQUE';
428 $index_type = 'INDEX';
431 . ' ' . $index_type . "\n"
434 echo str_replace('">' . "\n", '" align="right">' . "\n", $index_td)
435 . ' ' . (isset($indexes_info[$index_name]['Cardinality']) ?
$indexes_info[$index_name]['Cardinality'] : $strNone) . ' ' . "\n"
439 . ' <a href="tbl_indexes.php?' . $url_query . '&index=' . urlencode($index_name) . '">' . ($cfg['PropertiesIconic'] ?
'<img src="./images/button_edit.png" width="12" height="13" hspace="7" border="0" title="' . $strEdit . '" alt="' . $strEdit . '">' : $strEdit) . '</a>' . "\n"
442 if ($index_name == 'PRIMARY') {
443 $local_query = urlencode('ALTER TABLE ' . PMA_backquote($table) . ' DROP PRIMARY KEY');
444 $js_msg = 'ALTER TABLE ' . PMA_jsFormat($table) . ' DROP PRIMARY KEY';
445 $zero_rows = urlencode($strPrimaryKeyHasBeenDropped);
447 $local_query = urlencode('ALTER TABLE ' . PMA_backquote($table) . ' DROP INDEX ' . PMA_backquote($index_name));
448 $js_msg = 'ALTER TABLE ' . PMA_jsFormat($table) . ' DROP INDEX ' . PMA_jsFormat($index_name);
449 $zero_rows = urlencode(sprintf($strIndexHasBeenDropped, htmlspecialchars($index_name)));
452 . ' <a href="sql.php?' . $url_query . '&sql_query=' . $local_query . '&zero_rows=' . $zero_rows . '" onclick="return confirmLink(this, \'' . $js_msg . '\')">' . ($cfg['PropertiesIconic'] ?
'<img src="./images/button_drop.png" width="11" height="12" hspace="7" border="0" title="' . $strDrop . '" alt="' . $strDrop . '">' : $strDrop) . '</a>' . "\n"
455 foreach($indexes_info[$index_name]['Sequences'] AS $row_no => $seq_index) {
459 if (!empty($indexes_data[$index_name][$seq_index]['Sub_part'])) {
460 echo ' <td bgcolor="' . $cell_bgd . '">' . "\n"
461 . ' ' . $indexes_data[$index_name][$seq_index]['Column_name'] . "\n"
463 echo ' <td align="right" bgcolor="' . $cell_bgd . '">' . "\n"
464 . ' ' . $indexes_data[$index_name][$seq_index]['Sub_part'] . "\n"
466 echo ' </tr>' . "\n";
468 echo ' <td bgcolor="' . $cell_bgd . '" colspan="2">' . "\n"
469 . ' ' . htmlspecialchars($indexes_data[$index_name][$seq_index]['Column_name']) . "\n"
471 echo ' </tr>' . "\n";
479 } // end display indexes
482 echo "\n" . ' <br />' . "\n";
483 echo ' <i>' . $strNoIndex . '</i><br /><br />' . "\n\n";
486 echo ' ' . sprintf($strCreateIndex, '<input type="text" size="4" name="idx_num_fields" value="1" class="textfield" />') . "\n";
487 echo ' <input type="submit" name="create_index" value="' . $strGo . '" onclick="return checkFormElementInRange(this.form, \'idx_num_fields\', 1)" />' . "\n";
489 } // end display indexes
497 * Displays the footer
501 if (!defined('PMA_IDX_INCLUDED')){
502 require_once('./footer.inc.php');