6 * Gets some core libraries
8 require('./libraries/grab_globals.lib.php3');
9 require('./libraries/common.lib.php3');
13 * Defines the index types ("FULLTEXT" is available since MySQL 3.23.23)
21 if (PMA_MYSQL_INT_VERSION
>= 32323) {
22 $index_types[] = 'FULLTEXT';
28 * Ensures the db & table are valid, then loads headers and gets indexes
30 * Skipped if this script is called by "tbl_properties.php3"
32 if (!defined('PMA_IDX_INCLUDED')) {
33 // Not a valid db name -> back to the welcome page
35 $is_db = @PMA_mysql_select_db
($db);
37 if (empty($db) ||
!$is_db) {
38 header('Location: ' . $cfg['PmaAbsoluteUri'] . 'main.php3?lang=' . $lang . '&convcharset=' . $convcharset . '&server=' . $server . (isset($message) ?
'&message=' . urlencode($message) : '') . '&reload=1');
41 // Not a valid table name -> back to the db_details.php3
43 $is_table = @PMA_mysql_query
('SHOW TABLES LIKE \'' . PMA_sqlAddslashes($table, TRUE) . '\'');
46 ||
!($is_table && @mysql_numrows
($is_table))) {
47 header('Location: ' . $cfg['PmaAbsoluteUri'] . 'db_details.php3?lang=' . $lang . '&convcharset=' . $convcharset . '&server=' . $server .'&db=' . urlencode($db) . (isset($message) ?
'&message=' . urlencode($message) : '') . '&reload=1');
49 } else if (isset($is_table)) {
50 mysql_free_result($is_table);
53 // Displays headers (if needed)
54 $js_to_run = ((isset($index) && isset($do_save_data)) ?
'functions.js' : 'indexes.js');
55 include('./header.inc.php3');
60 * Gets fields and indexes informations
62 if (defined('PMA_IDX_INCLUDED')) {
63 $err_url_0 = 'db_details.php3'
65 . '&convcharset=' . $convcharset
66 . '&server=' . $server
67 . '&db=' . urlencode($db);
70 // Gets table keys and store them in arrays
73 $indexes_info = array();
74 $indexes_data = array();
75 // keys had already been grabbed in "tbl_properties.php3"
76 if (defined('PMA_IDX_INCLUDED')) {
77 $idx_cnt = count($ret_keys);
79 $local_query = 'SHOW KEYS FROM ' . PMA_backquote($table);
80 $result = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url_0);
81 $idx_cnt = mysql_num_rows($result);
84 for ($i = 0; $i < $idx_cnt; $i++
) {
85 $row = (defined('PMA_IDX_INCLUDED') ?
$ret_keys[$i] : PMA_mysql_fetch_array($result));
87 if ($row['Key_name'] != $prev_index ){
88 $indexes[] = $row['Key_name'];
89 $prev_index = $row['Key_name'];
91 $indexes_info[$row['Key_name']]['Sequences'][] = $row['Seq_in_index'];
92 $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique'];
93 if (isset($row['Cardinality'])) {
94 $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
96 // I don't know what does following column mean....
97 // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed'];
98 $indexes_info[$row['Key_name']]['Comment'] = (isset($row['Comment']))
102 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name'];
103 if (isset($row['Sub_part'])) {
104 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
108 if (defined('PMA_IDX_INCLUDED')) {
110 } else if ($result) {
111 mysql_free_result($result);
114 // Get fields and stores their name/type
115 // fields had already been grabbed in "tbl_properties.php3"
116 if (defined('PMA_IDX_INCLUDED')) {
117 mysql_data_seek($fields_rs, 0);
119 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($table);
120 $fields_rs = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url_0);
121 $fields_cnt = mysql_num_rows($fields_rs);
124 $fields_names = array();
125 $fields_types = array();
126 while ($row = PMA_mysql_fetch_array($fields_rs)) {
127 $fields_names[] = $row['Field'];
128 // loic1: set or enum types: slashes single quotes inside options
129 if (eregi('^(set|enum)\((.+)\)$', $row['Type'], $tmp)) {
130 $tmp[2] = substr(ereg_replace('([^,])\'\'', '\\1\\\'', ',' . $tmp[2]), 1);
131 $fields_types[] = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
133 $fields_types[] = $row['Type'];
138 mysql_free_result($fields_rs);
143 * Stipslashes some variables if required
145 if (get_magic_quotes_gpc()) {
147 $index = stripslashes($index);
149 if (isset($old_index)) {
150 $old_index = stripslashes($old_index);
156 * Do run the query to build the new index and moves back to
157 * "tbl_properties.php3"
159 if (!defined('PMA_IDX_INCLUDED')
160 && (isset($index) && isset($do_save_data))) {
162 $err_url = 'tbl_indexes.php3'
164 . '&convcharset=' . $convcharset
165 . '&server=' . $server
166 . '&db=' . urlencode($db)
167 . '&table=' . urlencode($table);
168 if (empty($old_index)) {
169 $err_url .= '&create_index=1&idx_num_fields=' . $idx_num_fields;
171 $err_url .= '&index=' . urlencode($old_index);
174 if ($index_type == 'PRIMARY') {
177 } else if ($index != 'PRIMARY') {
178 PMA_mysqlDie($strPrimaryKeyName, '', FALSE, $err_url);
180 } else if ($index == 'PRIMARY') {
181 PMA_mysqlDie($strCantRenameIdxToPrimary, '', FALSE, $err_url);
185 // $sql_query is the one displayed in the query box, don't use it when you
186 // need to generate a query in this script
187 $local_query = 'ALTER TABLE ' . PMA_backquote($table);
189 // Drops the old index
190 if (!empty($old_index)) {
191 if ($old_index == 'PRIMARY') {
192 $local_query .= ' DROP PRIMARY KEY,';
194 $local_query .= ' DROP INDEX ' . PMA_backquote($old_index) .',';
198 // Builds the new one
199 switch ($index_type) {
201 $local_query .= ' ADD PRIMARY KEY (';
204 $local_query .= ' ADD FULLTEXT ' . (empty($index) ?
'' : PMA_backquote($index)) . ' (';
207 $local_query .= ' ADD UNIQUE ' . (empty($index) ?
'' : PMA_backquote($index)) . ' (';
210 $local_query .= ' ADD INDEX ' . (empty($index) ?
'' : PMA_backquote($index)) . ' (';
214 while (list($i, $name) = each($column)) {
215 if ($name != '--ignore--') {
216 $index_fields .= (empty($index_fields) ?
'' : ',')
217 . PMA_backquote(get_magic_quotes_gpc() ?
stripslashes($name) : $name)
218 . (empty($sub_part[$i]) ?
'' : '(' . $sub_part[$i] . ')');
221 if (empty($index_fields)){
222 PMA_mysqlDie($strNoIndexPartsDefined, '', FALSE, $err_url);
224 $local_query .= $index_fields . ')';
227 $result = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, FALSE, $err_url);
228 $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered;
230 include('./tbl_properties.php3');
232 } // end builds the new index
236 * Edits an index or defines a new one
238 else if (!defined('PMA_IDX_INCLUDED')
239 && (isset($index) ||
isset($create_index))) {
241 // Prepares the form values
242 if (!isset($index)) {
245 if (!isset($old_index)){
248 if (!isset($index_type)) {
251 if ($old_index == '' ||
!isset($indexes_info[$old_index])) {
252 $edited_index_info['Sequences'] = array();
253 $edited_index_data = array();
254 for ($i = 1; $i <= $idx_num_fields; $i++
) {
255 $edited_index_info['Sequences'][] = $i;
256 $edited_index_data[$i] = array('Column_name' => '', 'Sub_part' => '');
259 && !isset($indexes_info['PRIMARY'])
260 && ($index_type == '' ||
$index_type == 'PRIMARY')) {
261 $old_index = 'PRIMARY';
264 $edited_index_info = $indexes_info[$old_index];
265 $edited_index_data = $indexes_data[$old_index];
266 if ($edited_index_info['Comment'] == 'FULLTEXT') {
267 $index_type = 'FULLTEXT';
268 } else if ($index == 'PRIMARY') {
269 $index_type = 'PRIMARY';
270 } else if ($edited_index_info['Non_unique'] == '0') {
271 $index_type = 'UNIQUE';
273 $index_type = 'INDEX';
275 } // end if... else...
277 if (isset($add_fields)) {
278 if (isset($prev_add_fields)) {
279 $added_fields +
= $prev_add_fields;
281 $field_cnt = count($edited_index_info['Sequences']) +
1;
282 for ($i = $field_cnt; $i < ($added_fields +
$field_cnt); $i++
) {
283 $edited_index_info['Sequences'][] = $i;
284 $edited_index_data[$i] = array('Column_name' => '', 'Sub_part' => '');
287 // Restore entered values
288 while (list($i, $name) = each($column)) {
289 if ($name != '--ignore--'){
290 $edited_index_data[$i+
1]['Column_name'] = $name;
291 $edited_index_data[$i+
1]['Sub_part'] = $sub_part[$i];
295 // end preparing form values
298 <!-- Build index form
-->
299 <form action
="tbl_indexes.php3" method
="post" name
="index_frm"
300 onsubmit
="if (typeof(this.elements['index'].disabled) != 'undefined') {this.elements['index'].disabled = false}">
301 <input type
="hidden" name
="lang" value
="<?php echo $lang; ?>" />
302 <input type
="hidden" name
="convcharset" value
="<?php echo $convcharset; ?>" />
303 <input type
="hidden" name
="server" value
="<?php echo $server; ?>" />
304 <input type
="hidden" name
="db" value
="<?php echo $db; ?>" />
305 <input type
="hidden" name
="table" value
="<?php echo $table; ?>" />
307 if (isset($create_index)) {
308 echo '<input type="hidden" name="create_index" value="1" />';
312 <input type
="hidden" name
="old_index" value
="<?php echo (isset($create_index) ? '' : $old_index); ?>" />
313 <b
><?php
echo '------ ' . (isset($create_index) ?
$strCreateIndexTopic : $strModifyIndexTopic) . ' ------'; ?
></b
>
318 <td
><?php
echo $strIndexName; ?
> 
;</td
>
320 <input type
="text" name
="index" value
="<?php echo htmlspecialchars($index); ?>" class="textfield" onfocus
="this.select()" />
321  
;<?php
echo $strPrimaryKeyWarning . "\n"; ?
>
325 <td
><?php
echo $strIndexType; ?
> 
;</td
>
327 <select name
="index_type" onchange
="return checkIndexName()">
330 for ($i = 0; $i < $index_types_cnt; $i++
) {
331 if ($index_types[$i] == 'PRIMARY') {
332 if ($index == 'PRIMARY' ||
!isset($indexes_info['PRIMARY'])) {
334 . '<option value="PRIMARY"' . (($index_type == 'PRIMARY') ?
' selected="selected"' : '') . '>PRIMARY</option>'
339 . '<option value="' . $index_types[$i] . '"' . (($index_type == $index_types[$i]) ?
' selected="selected"' : '') . '>'. $index_types[$i] . '</option>'
342 } // end if... else...
346 <?php
echo PMA_showDocuShort('A/L/ALTER_TABLE.html') . "\n"; ?
>
351 <table border
="<?php echo $cfg['Border']; ?>" cellpadding
="5">
353 <th
><?php
echo $strField; ?
></th
>
354 <th
><?php
echo $strSize; ?
></th
>
357 while (list($row_no, $seq_index) = each($edited_index_info['Sequences'])) {
358 $add_type = (is_array($fields_types) && count($fields_types) == count($fields_names));
359 $selected = $edited_index_data[$seq_index]['Column_name'];
360 if (!empty($edited_index_data[$seq_index]['Sub_part'])) {
361 $sub_part = ' value="' . $edited_index_data[$seq_index]['Sub_part'] . '"';
365 $bgcolor = (($row_no %
2) ?
$cfg['BgcolorOne'] : $cfg['BgcolorTwo']);
369 <td bgcolor
="<?php echo $bgcolor; ?>">
370 <select name
="column[]">
371 <option value
="--ignore--"<?php
if ('--ignore--' == $selected) echo ' selected="selected"'; ?
>>
372 -- <?php
echo $strIgnore; ?
> --</option
>
374 reset($fields_names);
375 while (list($key, $val) = each($fields_names)) {
376 if ($index_type != 'FULLTEXT'
377 ||
eregi('^(varchar|text|tinytext|mediumtext|longtext)', $fields_types[$key])) {
379 . '<option value="' . htmlspecialchars($val) . '"' . (($val == $selected) ?
' selected="selected"' : '') . '>'
380 . htmlspecialchars($val) . (($add_type) ?
' [' . $fields_types[$key] . ']' : '' ) . '</option>' . "\n";
387 <td bgcolor
="<?php echo $bgcolor; ?>">
388 <input type
="text" size
="5" name
="sub_part[]"<?php
echo $sub_part; ?
> onfocus
="this.select()" />
398 <input type
="submit" name
="do_save_data" value
="<?php echo $strSave; ?>" /><br
/>
402 if (isset($added_fields)) {
403 echo ' <input type="hidden" name="prev_add_fields" value="' . $added_fields . '" />';
405 if (isset($idx_num_fields)) {
406 echo ' <input type="hidden" name="idx_num_fields" value="' . $idx_num_fields . '" />' . "\n";
408 echo ' <hr /><br />' . "\n";
409 echo ' ' . sprintf($strAddToIndex, '<input type="text" name="added_fields" size="4" value="1" class="textfield" onfocus="this.select()" />') . "\n";
410 echo ' <input type="submit" name="add_fields" value="' . $strGo . '" onclick="return checkFormElementInRange(this.form, \'added_fields\', 1)" />' . "\n";
417 <!-- Indexes form
-->
418 <form action
="tbl_indexes.php3" method
="post">
419 <input type
="hidden" name
="lang" value
="<?php echo $lang; ?>" />
420 <input type
="hidden" name
="convcharset" value
="<?php echo $convcharset; ?>" />
421 <input type
="hidden" name
="server" value
="<?php echo $server; ?>" />
422 <input type
="hidden" name
="db" value
="<?php echo $db; ?>" />
423 <input type
="hidden" name
="table" value
="<?php echo $table; ?>" />
426 echo ' ' . $strIndexes . ' :' . "\n";
427 echo ' ' . PMA_showDocuShort('O/p/Optimising_Database_Structure.html') . '<br />' ."\n";
431 <table border
="<?php echo $cfg['Border']; ?>">
433 <th
><?php
echo $strKeyname; ?
></th
>
434 <th
><?php
echo $strType; ?
></th
>
435 <th
><?php
echo $strCardinality; ?
></th
>
436 <th colspan
="2"><?php
echo $strAction; ?
></th
>
437 <th colspan
="2"><?php
echo $strField; ?
></th
>
441 while (list($index_no, $index_name) = each($indexes)) {
442 $cell_bgd = (($index_no %
2) ?
$cfg['BgcolorOne'] : $cfg['BgcolorTwo']);
443 $index_td = ' <td bgcolor="' . $cell_bgd . '" rowspan="' . count($indexes_info[$index_name]['Sequences']) . '">' . "\n";
446 . ' ' . htmlspecialchars($index_name) . "\n"
449 if ($indexes_info[$index_name]['Comment'] == 'FULLTEXT') {
450 $index_type = 'FULLTEXT';
451 } else if ($index_name == 'PRIMARY') {
452 $index_type = 'PRIMARY';
453 } else if ($indexes_info[$index_name]['Non_unique'] == '0') {
454 $index_type = 'UNIQUE';
456 $index_type = 'INDEX';
459 . ' ' . $index_type . "\n"
462 echo str_replace('">' . "\n", '" align="right">' . "\n", $index_td)
463 . ' ' . (isset($indexes_info[$index_name]['Cardinality']) ?
$indexes_info[$index_name]['Cardinality'] : $strNone) . ' ' . "\n"
466 if ($index_name == 'PRIMARY') {
467 $local_query = urlencode('ALTER TABLE ' . PMA_backquote($table) . ' DROP PRIMARY KEY');
468 $js_msg = 'ALTER TABLE ' . PMA_jsFormat($table) . ' DROP PRIMARY KEY';
469 $zero_rows = urlencode($strPrimaryKeyHasBeenDropped);
471 $local_query = urlencode('ALTER TABLE ' . PMA_backquote($table) . ' DROP INDEX ' . PMA_backquote($index_name));
472 $js_msg = 'ALTER TABLE ' . PMA_jsFormat($table) . ' DROP INDEX ' . PMA_jsFormat($index_name);
473 $zero_rows = urlencode(sprintf($strIndexHasBeenDropped, htmlspecialchars($index_name)));
476 . ' <a href="sql.php3?' . $url_query . '&sql_query=' . $local_query . '&zero_rows=' . $zero_rows . '" onclick="return confirmLink(this, \'' . $js_msg . '\')">' . $strDrop . '</a>' . "\n"
480 . ' <a href="tbl_indexes.php3?' . $url_query . '&index=' . urlencode($index_name) . '">' . $strEdit . '</a>' . "\n"
483 while (list($row_no, $seq_index) = each($indexes_info[$index_name]['Sequences'])) {
487 if (!empty($indexes_data[$index_name][$seq_index]['Sub_part'])) {
488 echo ' <td bgcolor="' . $cell_bgd . '">' . "\n"
489 . ' ' . $indexes_data[$index_name][$seq_index]['Column_name'] . "\n"
491 echo ' <td align="right" bgcolor="' . $cell_bgd . '">' . "\n"
492 . ' ' . $indexes_data[$index_name][$seq_index]['Sub_part'] . "\n"
494 echo ' </tr>' . "\n";
496 echo ' <td bgcolor="' . $cell_bgd . '" colspan="2">' . "\n"
497 . ' ' . $indexes_data[$index_name][$seq_index]['Column_name'] . "\n"
499 echo ' </tr>' . "\n";
507 } // end display indexes
510 echo "\n" . ' <br />' . "\n";
511 echo ' <i>' . $strNoIndex . '</i><br /><br />' . "\n\n";
514 echo ' ' . sprintf($strCreateIndex, '<input type="text" size="4" name="idx_num_fields" value="1" class="textfield" />') . "\n";
515 echo ' <input type="submit" name="create_index" value="' . $strGo . '" onclick="return checkFormElementInRange(this.form, \'idx_num_fields\', 1)" />' . "\n";
517 } // end display indexes
525 * Displays the footer
529 if (!defined('PMA_IDX_INCLUDED')){
530 include('./footer.inc.php3');