sort order and tablename
[phpmyadmin/crack.git] / tbl_indexes.php3
blobaa63c706e32f0868a40da73a11c7083d614b467b
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
6 /**
7 * Gets some core libraries
8 */
9 if (!defined('PMA_GRAB_GLOBALS_INCLUDED')) {
10 include('./libraries/grab_globals.lib.php3');
12 if (!defined('PMA_COMMON_LIB_INCLUDED')) {
13 include('./libraries/common.lib.php3');
17 /**
18 * Defines the index types ("FULLTEXT" is available since MySQL 3.23.23)
20 $index_types_cnt = 3;
21 $index_types = array(
22 'PRIMARY',
23 'INDEX',
24 'UNIQUE'
26 if (PMA_MYSQL_INT_VERSION >= 32323) {
27 $index_types[] = 'FULLTEXT';
28 $index_types_cnt++;
32 /**
33 * Ensures the db & table are valid, then loads headers and gets indexes
34 * informations.
35 * Skipped if this script is called by "tbl_properties.php3"
37 if (!defined('PMA_IDX_INCLUDED')) {
38 // Not a valid db name -> back to the welcome page
39 if (!empty($db)) {
40 $is_db = @PMA_mysql_select_db($db);
42 if (empty($db) || !$is_db) {
43 header('Location: ' . $cfg['PmaAbsoluteUri'] . 'main.php3?lang=' . $lang . '&convcharset=' . $convcharset . '&server=' . $server . (isset($message) ? '&message=' . urlencode($message) : '') . '&reload=1');
44 exit();
46 // Not a valid table name -> back to the default db_details sub-page
47 if (!empty($table)) {
48 $is_table = @PMA_mysql_query('SHOW TABLES LIKE \'' . PMA_sqlAddslashes($table, TRUE) . '\'');
50 if (empty($table)
51 || !($is_table && @mysql_numrows($is_table))) {
52 header('Location: ' . $cfg['PmaAbsoluteUri'] . $cfg['DefaultTabDatabase'] . '?lang=' . $lang . '&convcharset=' . $convcharset . '&server=' . $server .'&db=' . urlencode($db) . (isset($message) ? '&message=' . urlencode($message) : '') . '&reload=1');
53 exit();
54 } else if (isset($is_table)) {
55 mysql_free_result($is_table);
58 // Displays headers (if needed)
59 $js_to_run = ((isset($index) && isset($do_save_data)) ? 'functions.js' : 'indexes.js');
60 include('./header.inc.php3');
61 } // end if
64 /**
65 * Gets fields and indexes informations
67 if (defined('PMA_IDX_INCLUDED')) {
68 $err_url_0 = 'db_details.php3'
69 . '?lang=' . $lang
70 . '&amp;convcharset=' . $convcharset
71 . '&amp;server=' . $server
72 . '&amp;db=' . urlencode($db);
75 // Gets table keys and store them in arrays
76 $indexes = array();
77 $prev_index = '';
78 $indexes_info = array();
79 $indexes_data = array();
80 // keys had already been grabbed in "tbl_properties.php3"
81 if (defined('PMA_IDX_INCLUDED')) {
82 $idx_cnt = count($ret_keys);
83 } else {
84 $local_query = 'SHOW KEYS FROM ' . PMA_backquote($table);
85 $result = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url_0);
86 $idx_cnt = mysql_num_rows($result);
89 for ($i = 0; $i < $idx_cnt; $i++) {
90 $row = (defined('PMA_IDX_INCLUDED') ? $ret_keys[$i] : PMA_mysql_fetch_array($result));
92 if ($row['Key_name'] != $prev_index ){
93 $indexes[] = $row['Key_name'];
94 $prev_index = $row['Key_name'];
96 $indexes_info[$row['Key_name']]['Sequences'][] = $row['Seq_in_index'];
97 $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique'];
98 if (isset($row['Cardinality'])) {
99 $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
101 // I don't know what does following column mean....
102 // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed'];
103 $indexes_info[$row['Key_name']]['Comment'] = (isset($row['Comment']))
104 ? $row['Comment']
105 : '';
107 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name'];
108 if (isset($row['Sub_part'])) {
109 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
111 } // end while
113 if (defined('PMA_IDX_INCLUDED')) {
114 unset($ret_keys);
115 } else if ($result) {
116 mysql_free_result($result);
119 // Get fields and stores their name/type
120 // fields had already been grabbed in "tbl_properties.php3"
121 if (defined('PMA_IDX_INCLUDED')) {
122 mysql_data_seek($fields_rs, 0);
123 } else {
124 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($table);
125 $fields_rs = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url_0);
126 $fields_cnt = mysql_num_rows($fields_rs);
129 $fields_names = array();
130 $fields_types = array();
131 while ($row = PMA_mysql_fetch_array($fields_rs)) {
132 $fields_names[] = $row['Field'];
133 // loic1: set or enum types: slashes single quotes inside options
134 if (eregi('^(set|enum)\((.+)\)$', $row['Type'], $tmp)) {
135 $tmp[2] = substr(ereg_replace('([^,])\'\'', '\\1\\\'', ',' . $tmp[2]), 1);
136 $fields_types[] = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
137 } else {
138 $fields_types[] = $row['Type'];
140 } // end while
142 if ($fields_rs) {
143 mysql_free_result($fields_rs);
148 * Stipslashes some variables if required
150 if (get_magic_quotes_gpc()) {
151 if (isset($index)) {
152 $index = stripslashes($index);
154 if (isset($old_index)) {
155 $old_index = stripslashes($old_index);
157 } // end if
161 * Do run the query to build the new index and moves back to
162 * "tbl_properties.php3"
164 if (!defined('PMA_IDX_INCLUDED')
165 && (isset($index) && isset($do_save_data))) {
167 $err_url = 'tbl_indexes.php3'
168 . '?lang=' . $lang
169 . '&amp;convcharset=' . $convcharset
170 . '&amp;server=' . $server
171 . '&amp;db=' . urlencode($db)
172 . '&amp;table=' . urlencode($table);
173 if (empty($old_index)) {
174 $err_url .= '&amp;create_index=1&amp;idx_num_fields=' . $idx_num_fields;
175 } else {
176 $err_url .= '&amp;index=' . urlencode($old_index);
179 if ($index_type == 'PRIMARY') {
180 if ($index == '') {
181 $index = 'PRIMARY';
182 } else if ($index != 'PRIMARY') {
183 PMA_mysqlDie($strPrimaryKeyName, '', FALSE, $err_url);
185 } else if ($index == 'PRIMARY') {
186 PMA_mysqlDie($strCantRenameIdxToPrimary, '', FALSE, $err_url);
190 // $sql_query is the one displayed in the query box, don't use it when you
191 // need to generate a query in this script
192 $local_query = 'ALTER TABLE ' . PMA_backquote($table);
194 // Drops the old index
195 if (!empty($old_index)) {
196 if ($old_index == 'PRIMARY') {
197 $local_query .= ' DROP PRIMARY KEY,';
198 } else {
199 $local_query .= ' DROP INDEX ' . PMA_backquote($old_index) .',';
201 } // end if
203 // Builds the new one
204 switch ($index_type) {
205 case 'PRIMARY':
206 $local_query .= ' ADD PRIMARY KEY (';
207 break;
208 case 'FULLTEXT':
209 $local_query .= ' ADD FULLTEXT ' . (empty($index) ? '' : PMA_backquote($index)) . ' (';
210 break;
211 case 'UNIQUE':
212 $local_query .= ' ADD UNIQUE ' . (empty($index) ? '' : PMA_backquote($index)) . ' (';
213 break;
214 case 'INDEX':
215 $local_query .= ' ADD INDEX ' . (empty($index) ? '' : PMA_backquote($index)) . ' (';
216 break;
217 } // end switch
218 $index_fields = '';
219 while (list($i, $name) = each($column)) {
220 if ($name != '--ignore--') {
221 $index_fields .= (empty($index_fields) ? '' : ',')
222 . PMA_backquote(get_magic_quotes_gpc() ? stripslashes($name) : $name)
223 . (empty($sub_part[$i]) ? '' : '(' . $sub_part[$i] . ')');
225 } // end while
226 if (empty($index_fields)){
227 PMA_mysqlDie($strNoIndexPartsDefined, '', FALSE, $err_url);
228 } else {
229 $local_query .= $index_fields . ')';
232 $result = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, FALSE, $err_url);
233 $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered;
235 include('./tbl_properties.php3');
236 exit();
237 } // end builds the new index
241 * Edits an index or defines a new one
243 else if (!defined('PMA_IDX_INCLUDED')
244 && (isset($index) || isset($create_index))) {
246 // Prepares the form values
247 if (!isset($index)) {
248 $index = '';
250 if (!isset($old_index)){
251 $old_index = $index;
253 if (!isset($index_type)) {
254 $index_type = '';
256 if ($old_index == '' || !isset($indexes_info[$old_index])) {
257 $edited_index_info['Sequences'] = array();
258 $edited_index_data = array();
259 for ($i = 1; $i <= $idx_num_fields; $i++) {
260 $edited_index_info['Sequences'][] = $i;
261 $edited_index_data[$i] = array('Column_name' => '', 'Sub_part' => '');
262 } // end for
263 if ($old_index == ''
264 && !isset($indexes_info['PRIMARY'])
265 && ($index_type == '' || $index_type == 'PRIMARY')) {
266 $old_index = 'PRIMARY';
268 } else {
269 $edited_index_info = $indexes_info[$old_index];
270 $edited_index_data = $indexes_data[$old_index];
271 if ($edited_index_info['Comment'] == 'FULLTEXT') {
272 $index_type = 'FULLTEXT';
273 } else if ($index == 'PRIMARY') {
274 $index_type = 'PRIMARY';
275 } else if ($edited_index_info['Non_unique'] == '0') {
276 $index_type = 'UNIQUE';
277 } else {
278 $index_type = 'INDEX';
280 } // end if... else...
282 if (isset($add_fields)) {
283 if (isset($prev_add_fields)) {
284 $added_fields += $prev_add_fields;
286 $field_cnt = count($edited_index_info['Sequences']) + 1;
287 for ($i = $field_cnt; $i < ($added_fields + $field_cnt); $i++) {
288 $edited_index_info['Sequences'][] = $i;
289 $edited_index_data[$i] = array('Column_name' => '', 'Sub_part' => '');
290 } // end for
292 // Restore entered values
293 while (list($i, $name) = each($column)) {
294 if ($name != '--ignore--'){
295 $edited_index_data[$i+1]['Column_name'] = $name;
296 $edited_index_data[$i+1]['Sub_part'] = $sub_part[$i];
298 } // end while
299 } // end if
300 // end preparing form values
303 <!-- Build index form -->
304 <form action="tbl_indexes.php3" method="post" name="index_frm"
305 onsubmit="if (typeof(this.elements['index'].disabled) != 'undefined') {this.elements['index'].disabled = false}">
306 <input type="hidden" name="lang" value="<?php echo $lang; ?>" />
307 <input type="hidden" name="convcharset" value="<?php echo $convcharset; ?>" />
308 <input type="hidden" name="server" value="<?php echo $server; ?>" />
309 <input type="hidden" name="db" value="<?php echo $db; ?>" />
310 <input type="hidden" name="table" value="<?php echo $table; ?>" />
311 <?php
312 if (isset($create_index)) {
313 echo '<input type="hidden" name="create_index" value="1" />';
315 echo "\n";
317 <input type="hidden" name="old_index" value="<?php echo (isset($create_index) ? '' : $old_index); ?>" />
318 <b><?php echo '------ ' . (isset($create_index) ? $strCreateIndexTopic : $strModifyIndexTopic) . ' ------'; ?></b>
319 <br /><br />
321 <table border="0">
322 <tr>
323 <td><?php echo $strIndexName; ?>&nbsp;</td>
324 <td>
325 <input type="text" name="index" value="<?php echo htmlspecialchars($index); ?>" class="textfield" onfocus="this.select()" />
326 &nbsp;<?php echo $strPrimaryKeyWarning . "\n"; ?>
327 </td>
328 </tr>
329 <tr>
330 <td><?php echo $strIndexType; ?>&nbsp;</td>
331 <td>
332 <select name="index_type" onchange="return checkIndexName()">
333 <?php
334 echo "\n";
335 for ($i = 0; $i < $index_types_cnt; $i++) {
336 if ($index_types[$i] == 'PRIMARY') {
337 if ($index == 'PRIMARY' || !isset($indexes_info['PRIMARY'])) {
338 echo ' '
339 . '<option value="PRIMARY"' . (($index_type == 'PRIMARY') ? ' selected="selected"' : '') . '>PRIMARY</option>'
340 . "\n";
342 } else {
343 echo ' '
344 . '<option value="' . $index_types[$i] . '"' . (($index_type == $index_types[$i]) ? ' selected="selected"' : '') . '>'. $index_types[$i] . '</option>'
345 . "\n";
347 } // end if... else...
348 } // end for
350 </select>&nbsp;
351 <?php echo PMA_showMySQLDocu('Reference', 'ALTER_TABLE') . "\n"; ?>
352 </td>
353 </tr>
354 </table><br />
356 <table border="<?php echo $cfg['Border']; ?>" cellpadding="5">
357 <tr>
358 <th><?php echo $strField; ?></th>
359 <th><?php echo $strSize; ?></th>
360 </tr>
361 <?php
362 while (list($row_no, $seq_index) = each($edited_index_info['Sequences'])) {
363 $add_type = (is_array($fields_types) && count($fields_types) == count($fields_names));
364 $selected = $edited_index_data[$seq_index]['Column_name'];
365 if (!empty($edited_index_data[$seq_index]['Sub_part'])) {
366 $sub_part = ' value="' . $edited_index_data[$seq_index]['Sub_part'] . '"';
367 } else {
368 $sub_part = '';
370 $bgcolor = (($row_no % 2) ? $cfg['BgcolorOne'] : $cfg['BgcolorTwo']);
371 echo "\n";
373 <tr>
374 <td bgcolor="<?php echo $bgcolor; ?>">
375 <select name="column[]">
376 <option value="--ignore--"<?php if ('--ignore--' == $selected) echo ' selected="selected"'; ?>>
377 -- <?php echo $strIgnore; ?> --</option>
378 <?php
379 reset($fields_names);
380 while (list($key, $val) = each($fields_names)) {
381 if ($index_type != 'FULLTEXT'
382 || eregi('^(varchar|text|tinytext|mediumtext|longtext)', $fields_types[$key])) {
383 echo "\n" . ' '
384 . '<option value="' . htmlspecialchars($val) . '"' . (($val == $selected) ? ' selected="selected"' : '') . '>'
385 . htmlspecialchars($val) . (($add_type) ? ' [' . $fields_types[$key] . ']' : '' ) . '</option>' . "\n";
387 } // end while
388 echo "\n";
390 </select>
391 </td>
392 <td bgcolor="<?php echo $bgcolor; ?>">
393 <input type="text" size="5" name="sub_part[]"<?php echo $sub_part; ?> onfocus="this.select()" />
394 </td>
395 </tr>
396 <?php
397 } // end while
399 echo "\n";
401 </table><br />
403 <input type="submit" name="do_save_data" value="<?php echo $strSave; ?>" /><br />
405 <?php
406 echo "\n";
407 if (isset($added_fields)) {
408 echo ' <input type="hidden" name="prev_add_fields" value="' . $added_fields . '" />';
410 if (isset($idx_num_fields)) {
411 echo ' <input type="hidden" name="idx_num_fields" value="' . $idx_num_fields . '" />' . "\n";
413 echo ' <hr /><br />' . "\n";
414 echo ' ' . sprintf($strAddToIndex, '<input type="text" name="added_fields" size="4" value="1" class="textfield" onfocus="this.select()" />') . "\n";
415 echo ' &nbsp;<input type="submit" name="add_fields" value="' . $strGo . '" onclick="return checkFormElementInRange(this.form, \'added_fields\', 1)" />' . "\n";
417 } else {
419 * Display indexes
422 <!-- Indexes form -->
423 <form action="tbl_indexes.php3" method="post">
424 <input type="hidden" name="lang" value="<?php echo $lang; ?>" />
425 <input type="hidden" name="convcharset" value="<?php echo $convcharset; ?>" />
426 <input type="hidden" name="server" value="<?php echo $server; ?>" />
427 <input type="hidden" name="db" value="<?php echo $db; ?>" />
428 <input type="hidden" name="table" value="<?php echo $table; ?>" />
429 <?php
430 echo "\n";
431 echo ' ' . $strIndexes . '&nbsp;:' . "\n";
432 echo ' ' . PMA_showMySQLDocu('MySQL_Optimisation', 'Optimising_Database_Structure') . '<br />' ."\n";
434 if ($idx_cnt > 0) {
436 <table border="<?php echo $cfg['Border']; ?>">
437 <tr>
438 <th><?php echo $strKeyname; ?></th>
439 <th><?php echo $strType; ?></th>
440 <th><?php echo $strCardinality; ?></th>
441 <th colspan="2"><?php echo $strAction; ?></th>
442 <th colspan="2"><?php echo $strField; ?></th>
443 </tr>
444 <?php
445 echo "\n";
446 while (list($index_no, $index_name) = each($indexes)) {
447 $cell_bgd = (($index_no % 2) ? $cfg['BgcolorOne'] : $cfg['BgcolorTwo']);
448 $index_td = ' <td bgcolor="' . $cell_bgd . '" rowspan="' . count($indexes_info[$index_name]['Sequences']) . '">' . "\n";
449 echo ' <tr>' . "\n";
450 echo $index_td
451 . ' ' . htmlspecialchars($index_name) . "\n"
452 . ' </td>' . "\n";
454 if ($indexes_info[$index_name]['Comment'] == 'FULLTEXT') {
455 $index_type = 'FULLTEXT';
456 } else if ($index_name == 'PRIMARY') {
457 $index_type = 'PRIMARY';
458 } else if ($indexes_info[$index_name]['Non_unique'] == '0') {
459 $index_type = 'UNIQUE';
460 } else {
461 $index_type = 'INDEX';
463 echo $index_td
464 . ' ' . $index_type . "\n"
465 . ' </td>' . "\n";
467 echo str_replace('">' . "\n", '" align="right">' . "\n", $index_td)
468 . ' ' . (isset($indexes_info[$index_name]['Cardinality']) ? $indexes_info[$index_name]['Cardinality'] : $strNone) . '&nbsp;' . "\n"
469 . ' </td>' . "\n";
471 if ($index_name == 'PRIMARY') {
472 $local_query = urlencode('ALTER TABLE ' . PMA_backquote($table) . ' DROP PRIMARY KEY');
473 $js_msg = 'ALTER TABLE ' . PMA_jsFormat($table) . ' DROP PRIMARY KEY';
474 $zero_rows = urlencode($strPrimaryKeyHasBeenDropped);
475 } else {
476 $local_query = urlencode('ALTER TABLE ' . PMA_backquote($table) . ' DROP INDEX ' . PMA_backquote($index_name));
477 $js_msg = 'ALTER TABLE ' . PMA_jsFormat($table) . ' DROP INDEX ' . PMA_jsFormat($index_name);
478 $zero_rows = urlencode(sprintf($strIndexHasBeenDropped, htmlspecialchars($index_name)));
480 echo $index_td
481 . ' <a href="sql.php3?' . $url_query . '&amp;sql_query=' . $local_query . '&amp;zero_rows=' . $zero_rows . '" onclick="return confirmLink(this, \'' . $js_msg . '\')">' . $strDrop . '</a>' . "\n"
482 . ' </td>' . "\n";
484 echo $index_td
485 . ' <a href="tbl_indexes.php3?' . $url_query . '&amp;index=' . urlencode($index_name) . '">' . $strEdit . '</a>' . "\n"
486 . ' </td>' . "\n";
488 while (list($row_no, $seq_index) = each($indexes_info[$index_name]['Sequences'])) {
489 if ($row_no > 0) {
490 echo ' <tr>' . "\n";
492 if (!empty($indexes_data[$index_name][$seq_index]['Sub_part'])) {
493 echo ' <td bgcolor="' . $cell_bgd . '">' . "\n"
494 . ' ' . $indexes_data[$index_name][$seq_index]['Column_name'] . "\n"
495 . ' </td>' . "\n";
496 echo ' <td align="right" bgcolor="' . $cell_bgd . '">' . "\n"
497 . ' ' . $indexes_data[$index_name][$seq_index]['Sub_part'] . "\n"
498 . ' </td>' . "\n";
499 echo ' </tr>' . "\n";
500 } else {
501 echo ' <td bgcolor="' . $cell_bgd . '" colspan="2">' . "\n"
502 . ' ' . $indexes_data[$index_name][$seq_index]['Column_name'] . "\n"
503 . ' </td>' . "\n";
504 echo ' </tr>' . "\n";
506 } // end while
507 } // end while
509 </table><br />
510 <?php
511 echo "\n\n";
512 } // end display indexes
513 else {
514 // none indexes
515 echo "\n" . ' <br />' . "\n";
516 echo ' <i>' . $strNoIndex . '</i><br /><br />' . "\n\n";
519 echo ' ' . sprintf($strCreateIndex, '<input type="text" size="4" name="idx_num_fields" value="1" class="textfield" />') . "\n";
520 echo ' &nbsp;<input type="submit" name="create_index" value="' . $strGo . '" onclick="return checkFormElementInRange(this.form, \'idx_num_fields\', 1)" />' . "\n";
521 echo ' ';
522 } // end display indexes
525 </form>
528 <?php
530 * Displays the footer
532 echo "\n";
534 if (!defined('PMA_IDX_INCLUDED')){
535 include('./footer.inc.php3');