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_DBI_select_db($db);
34 if (empty($db) ||
!$is_db) {
35 PMA_sendHeaderLocation($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_DBI_query('SHOW TABLES LIKE \'' . PMA_sqlAddslashes($table, TRUE) . '\'', NULL, PMA_DBI_QUERY_STORE
);
43 ||
!($is_table && PMA_DBI_num_rows($is_table))) {
44 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . $cfg['DefaultTabDatabase'] . '?' . PMA_generate_common_url($db, '', '&') . (isset($message) ?
'&message=' . urlencode($message) : '') . '&reload=1');
46 } else if (isset($is_table)) {
47 PMA_DBI_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 $local_query = 'SHOW KEYS FROM ' . PMA_backquote($table);
71 $result = PMA_DBI_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url_0);
73 while ($row = PMA_DBI_fetch_assoc($result)) {
76 PMA_DBI_free_result($result);
79 foreach ($ret_keys as $row) {
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 // Get fields and stores their name/type
105 // fields had already been grabbed in "tbl_properties.php"
106 if (!defined('PMA_IDX_INCLUDED')) {
107 $fields_rs = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table) . ';');
109 while ($row = PMA_DBI_fetch_assoc($fields_rs)) {
114 $fields_names = array();
115 $fields_types = array();
116 foreach ($save_row AS $saved_row_key => $row) {
117 $fields_names[] = $row['Field'];
118 // loic1: set or enum types: slashes single quotes inside options
119 if (preg_match('@^(set|enum)\((.+)\)$@i', $row['Type'], $tmp)) {
120 $tmp[2] = substr(preg_replace('@([^,])\'\'@', '\\1\\\'', ',' . $tmp[2]), 1);
121 $fields_types[] = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
123 $fields_types[] = $row['Type'];
128 PMA_DBI_free_result($fields_rs);
133 * Do run the query to build the new index and moves back to
134 * "tbl_properties.php"
136 if (!defined('PMA_IDX_INCLUDED')
137 && (isset($index) && isset($do_save_data))) {
139 $err_url = 'tbl_indexes.php?' . PMA_generate_common_url($db, $table);
140 if (empty($old_index)) {
141 $err_url .= '&create_index=1&idx_num_fields=' . $idx_num_fields;
143 $err_url .= '&index=' . urlencode($old_index);
146 if ($index_type == 'PRIMARY') {
149 } else if ($index != 'PRIMARY') {
150 PMA_mysqlDie($strPrimaryKeyName, '', FALSE, $err_url);
152 } else if ($index == 'PRIMARY') {
153 PMA_mysqlDie($strCantRenameIdxToPrimary, '', FALSE, $err_url);
157 // $sql_query is the one displayed in the query box
158 $sql_query = 'ALTER TABLE ' . PMA_backquote($table);
160 // Drops the old index
161 if (!empty($old_index)) {
162 if ($old_index == 'PRIMARY') {
163 $sql_query .= ' DROP PRIMARY KEY,';
165 $sql_query .= ' DROP INDEX ' . PMA_backquote($old_index) .',';
169 // Builds the new one
170 switch ($index_type) {
172 $sql_query .= ' ADD PRIMARY KEY (';
175 $sql_query .= ' ADD FULLTEXT ' . (empty($index) ?
'' : PMA_backquote($index)) . ' (';
178 $sql_query .= ' ADD UNIQUE ' . (empty($index) ?
'' : PMA_backquote($index)) . ' (';
181 $sql_query .= ' ADD INDEX ' . (empty($index) ?
'' : PMA_backquote($index)) . ' (';
185 foreach ($column AS $i => $name) {
186 if ($name != '--ignore--') {
187 $index_fields .= (empty($index_fields) ?
'' : ',')
188 . PMA_backquote($name)
189 . (empty($sub_part[$i]) ?
'' : '(' . $sub_part[$i] . ')');
192 if (empty($index_fields)){
193 PMA_mysqlDie($strNoIndexPartsDefined, '', FALSE, $err_url);
195 $sql_query .= $index_fields . ')';
198 $result = PMA_DBI_query($sql_query);
199 $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered;
201 $active_page = 'tbl_properties_structure.php';
202 require('./tbl_properties_structure.php');
203 } // end builds the new index
207 * Edits an index or defines a new one
209 else if (!defined('PMA_IDX_INCLUDED')
210 && (isset($index) ||
isset($create_index))) {
212 // Prepares the form values
213 if (!isset($index)) {
216 if (!isset($old_index)){
219 if (!isset($index_type)) {
222 if ($old_index == '' ||
!isset($indexes_info[$old_index])) {
223 $edited_index_info['Sequences'] = array();
224 $edited_index_data = array();
225 for ($i = 1; $i <= $idx_num_fields; $i++
) {
226 $edited_index_info['Sequences'][] = $i;
227 $edited_index_data[$i] = array('Column_name' => '', 'Sub_part' => '');
230 && !isset($indexes_info['PRIMARY'])
231 && ($index_type == '' ||
$index_type == 'PRIMARY')) {
232 $old_index = 'PRIMARY';
235 $edited_index_info = $indexes_info[$old_index];
236 $edited_index_data = $indexes_data[$old_index];
239 if ((PMA_MYSQL_INT_VERSION
< 40002 && $edited_index_info['Comment'] == 'FULLTEXT')
240 ||
(PMA_MYSQL_INT_VERSION
>= 40002 && $edited_index_info['Index_type'] == 'FULLTEXT')) {
241 $index_type = 'FULLTEXT';
242 } else if ($index == 'PRIMARY') {
243 $index_type = 'PRIMARY';
244 } else if ($edited_index_info['Non_unique'] == '0') {
245 $index_type = 'UNIQUE';
247 $index_type = 'INDEX';
249 } // end if... else...
251 if (isset($add_fields)) {
252 if (isset($prev_add_fields)) {
253 $added_fields +
= $prev_add_fields;
255 $field_cnt = count($edited_index_info['Sequences']) +
1;
256 for ($i = $field_cnt; $i < ($added_fields +
$field_cnt); $i++
) {
257 $edited_index_info['Sequences'][] = $i;
258 $edited_index_data[$i] = array('Column_name' => '', 'Sub_part' => '');
261 // Restore entered values
262 foreach ($column AS $i => $name) {
263 if ($name != '--ignore--'){
264 $edited_index_data[$i+
1]['Column_name'] = $name;
265 $edited_index_data[$i+
1]['Sub_part'] = $sub_part[$i];
269 // end preparing form values
272 <!-- Build index form
-->
273 <form action
="./tbl_indexes.php" method
="post" name
="index_frm"
274 onsubmit
="if (typeof(this.elements['index'].disabled) != 'undefined') {this.elements['index'].disabled = false}">
275 <table border
="0" cellpadding
="2" cellspacing
="1">
276 <tr
><td
class="tblHeaders" colspan
="2">
277 <?php
echo PMA_generate_common_hidden_inputs($db, $table); ?
>
279 if (isset($create_index)) {
280 echo '<input type="hidden" name="create_index" value="1" />';
284 <input type
="hidden" name
="old_index" value
="<?php echo (isset($create_index) ? '' : htmlspecialchars($old_index)); ?>" />
285 <?php
echo ' ' . (isset($create_index) ?
$strCreateIndexTopic : $strModifyIndexTopic) . ' '; ?
>
290 <td align
="right"><b
><?php
echo $strIndexName; ?
></b
> 
;</th
>
292 <input type
="text" name
="index" value
="<?php echo htmlspecialchars($index); ?>" size
="25" onfocus
="this.select()" />
295 <tr
><td align
="right"><?php
296 if ($cfg['ErrorIconic']) {
297 echo '<img src="' . $pmaThemeImage . 's_warn.png" width="16" height="16" border="0" alt="Attention" />';
299 ?
></td
><td
><?php
echo $strPrimaryKeyWarning . "\n"; ?
></td
></tr
>
301 <td align
="right"><b
><?php
echo $strIndexType; ?
></b
> 
;</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"; ?
>
326 <tr
><td valign
="top" align
="right"><b
><?php
echo $strFields; ?
> :</b
> 
;</td
><td
><table border
="<?php echo $cfg['Border']; ?>" cellpadding
="2" cellspacing
="1">
328 <th
><?php
echo $strField; ?
></th
>
329 <th
><?php
echo $strSize; ?
></th
>
332 foreach ($edited_index_info['Sequences'] AS $row_no => $seq_index) {
333 $add_type = (is_array($fields_types) && count($fields_types) == count($fields_names));
334 $selected = $edited_index_data[$seq_index]['Column_name'];
335 if (!empty($edited_index_data[$seq_index]['Sub_part'])) {
336 $sub_part = ' value="' . $edited_index_data[$seq_index]['Sub_part'] . '"';
340 $bgcolor = (($row_no %
2) ?
$cfg['BgcolorOne'] : $cfg['BgcolorTwo']);
344 <td bgcolor
="<?php echo $bgcolor; ?>">
345 <select name
="column[]">
346 <option value
="--ignore--"<?php
if ('--ignore--' == $selected) echo ' selected="selected"'; ?
>>
347 -- <?php
echo $strIgnore; ?
> --</option
>
349 foreach ($fields_names AS $key => $val) {
350 if ($index_type != 'FULLTEXT'
351 ||
preg_match('@^(varchar|text|tinytext|mediumtext|longtext)@i', $fields_types[$key])) {
353 . '<option value="' . htmlspecialchars($val) . '"' . (($val == $selected) ?
' selected="selected"' : '') . '>'
354 . htmlspecialchars($val) . (($add_type) ?
' [' . $fields_types[$key] . ']' : '' ) . '</option>' . "\n";
361 <td bgcolor
="<?php echo $bgcolor; ?>">
362 <input type
="text" size
="5" name
="sub_part[]"<?php
echo $sub_part; ?
> onfocus
="this.select()" />
370 <tr
><td colspan
="2"><?php
372 if (isset($added_fields)) {
373 echo ' <input type="hidden" name="prev_add_fields" value="' . $added_fields . '" />';
375 if (isset($idx_num_fields)) {
376 echo ' <input type="hidden" name="idx_num_fields" value="' . $idx_num_fields . '" />' . "\n";
379 echo ' ' . sprintf($strAddToIndex, '<input type="text" name="added_fields" size="2" value="1" onfocus="this.select()" style="vertical-align: middle;" />') . "\n";
380 echo ' <input type="submit" name="add_fields" value="' . $strGo . '" onclick="return checkFormElementInRange(this.form, \'added_fields\', 1)" style="vertical-align: middle;" />' . "\n";
384 <tr
><td colspan
="2" class="tblFooters" align
="center">
385 <input type
="submit" name
="do_save_data" value
="<?php echo $strSave; ?>" /></td
></tr
>
394 <!-- Indexes form
-->
395 <form action
="./tbl_indexes.php" method
="post">
396 <table border
="0" cellpadding
="2" cellspacing
="1">
397 <tr
><td
class="tblHeaders" colspan
="7">
398 <?php
echo PMA_generate_common_hidden_inputs($db, $table); ?
>
401 echo ' ' . $strIndexes . ':' . "\n";
402 echo ' ' . PMA_showMySQLDocu('MySQL_Optimisation', 'Optimising_Database_Structure') . "\n";
404 $edit_link_text = '';
405 $drop_link_text = '';
407 // We need to copy the value or else the == 'both' check will always return true
408 $propicon = (string)$cfg['PropertiesIconic'];
410 if ($cfg['PropertiesIconic'] === true ||
$propicon == 'both') {
411 $edit_link_text = '<img src="' . $pmaThemeImage . 'b_edit.png" width="16" height="16" hspace="2" border="0" title="' . $strEdit . '" alt="' . $strEdit . '" />';
412 $drop_link_text = '<img src="' . $pmaThemeImage . 'b_drop.png" width="16" height="16" hspace="2" border="0" title="' . $strDrop . '" alt="' . $strDrop . '" />';
414 //print_r($cfg['PropertiesIconic']);
415 if ($cfg['PropertiesIconic'] === false ||
$propicon == 'both') {
416 $edit_link_text .= $strEdit;
417 $drop_link_text .= $strDrop;
419 if ($propicon == 'both') {
420 $edit_link_text = '<nobr>' . $edit_link_text . '</nobr>';
421 $drop_link_text = '<nobr>' . $drop_link_text . '</nobr>';
424 if (count($ret_keys) > 0) {
426 <!--table border
="<?php echo $cfg['Border']; ?>" cellpadding
="2" cellspacing
="1"-->
428 <th
><?php
echo $strKeyname; ?
></th
>
429 <th
><?php
echo $strType; ?
></th
>
430 <th
><?php
echo $strCardinality; ?
></th
>
431 <th colspan
="2"><?php
echo $strAction; ?
></th
>
432 <th colspan
="2"><?php
echo $strField; ?
></th
>
436 foreach ($indexes AS $index_no => $index_name) {
437 $cell_bgd = (($index_no %
2) ?
$cfg['BgcolorOne'] : $cfg['BgcolorTwo']);
438 $index_td = ' <td bgcolor="' . $cell_bgd . '" rowspan="' . count($indexes_info[$index_name]['Sequences']) . '">' . "\n";
441 . ' ' . htmlspecialchars($index_name) . "\n"
444 if ((PMA_MYSQL_INT_VERSION
< 40002 && $indexes_info[$index_name]['Comment'] == 'FULLTEXT')
445 ||
(PMA_MYSQL_INT_VERSION
>= 40002 && $indexes_info[$index_name]['Index_type'] == 'FULLTEXT')) {
446 $index_type = 'FULLTEXT';
447 } else if ($index_name == 'PRIMARY') {
448 $index_type = 'PRIMARY';
449 } else if ($indexes_info[$index_name]['Non_unique'] == '0') {
450 $index_type = 'UNIQUE';
452 $index_type = 'INDEX';
455 . ' ' . $index_type . "\n"
458 echo str_replace('">' . "\n", '" align="right">' . "\n", $index_td)
459 . ' ' . (isset($indexes_info[$index_name]['Cardinality']) ?
$indexes_info[$index_name]['Cardinality'] : $strNone) . ' ' . "\n"
463 . ' <a href="tbl_indexes.php?' . $url_query . '&index=' . urlencode($index_name) . '">' . $edit_link_text . '</a>' . "\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.php?' . $url_query . '&sql_query=' . $local_query . '&zero_rows=' . $zero_rows . '" onclick="return confirmLink(this, \'' . $js_msg . '\')">' . $drop_link_text . '</a>' . "\n"
479 foreach ($indexes_info[$index_name]['Sequences'] AS $row_no => $seq_index) {
483 if (!empty($indexes_data[$index_name][$seq_index]['Sub_part'])) {
484 echo ' <td bgcolor="' . $cell_bgd . '">' . "\n"
485 . ' ' . $indexes_data[$index_name][$seq_index]['Column_name'] . "\n"
487 echo ' <td align="right" bgcolor="' . $cell_bgd . '">' . "\n"
488 . ' ' . $indexes_data[$index_name][$seq_index]['Sub_part'] . "\n"
490 echo ' </tr>' . "\n";
492 echo ' <td bgcolor="' . $cell_bgd . '" colspan="2">' . "\n"
493 . ' ' . htmlspecialchars($indexes_data[$index_name][$seq_index]['Column_name']) . "\n"
495 echo ' </tr>' . "\n";
503 } // end display indexes
506 echo "\n" . ' <tr><td colspan=7" align="center">' . "\n";
507 if ($cfg['ErrorIconic']) {
508 echo '<img src="' . $pmaThemeImage . 's_warn.png" width="16" height="16" border="0" alt="Warning" hspace="2" align="middle" />';
510 echo ' <b>' . $strNoIndex . '</b></td></tr>' . "\n\n";
513 echo '<tr><td colspan="7" class="tblFooters" nowrap="nowrap" align="center"> '
514 . sprintf($strCreateIndex, '<input type="text" size="2" name="idx_num_fields" value="1" style="vertical-align: middle;" />') . "\n";
515 echo ' <input type="submit" name="create_index" value="' . $strGo . '" onclick="return checkFormElementInRange(this.form, \'idx_num_fields\', 1)" style="vertical-align: middle;" />' . "\n";
520 } // end display indexes
524 * Displays the footer
528 if (!defined('PMA_IDX_INCLUDED')){
529 require_once('./footer.inc.php');