update
[phpmyadmin/crack.git] / tbl_indexes.php3
blobd787536831d8f8c834f782aac1d629595660f475
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?' . PMA_generate_common_url('', '', '&') . (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'] . '?' . PMA_generate_common_url($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?' . PMA_generate_common_url($db);
71 // Gets table keys and store them in arrays
72 $indexes = array();
73 $prev_index = '';
74 $indexes_info = array();
75 $indexes_data = array();
76 // keys had already been grabbed in "tbl_properties.php3"
77 if (defined('PMA_IDX_INCLUDED')) {
78 $idx_cnt = count($ret_keys);
79 } else {
80 $local_query = 'SHOW KEYS FROM ' . PMA_backquote($table);
81 $result = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url_0);
82 $idx_cnt = mysql_num_rows($result);
85 for ($i = 0; $i < $idx_cnt; $i++) {
86 $row = (defined('PMA_IDX_INCLUDED') ? $ret_keys[$i] : PMA_mysql_fetch_array($result));
88 if ($row['Key_name'] != $prev_index ){
89 $indexes[] = $row['Key_name'];
90 $prev_index = $row['Key_name'];
92 $indexes_info[$row['Key_name']]['Sequences'][] = $row['Seq_in_index'];
93 $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique'];
94 if (isset($row['Cardinality'])) {
95 $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
97 // I don't know what does following column mean....
98 // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed'];
99 $indexes_info[$row['Key_name']]['Comment'] = (isset($row['Comment']))
100 ? $row['Comment']
101 : '';
102 $indexes_info[$row['Key_name']]['Index_type'] = (isset($row['Index_type']))
103 ? $row['Index_type']
104 : '';
106 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name'];
107 if (isset($row['Sub_part'])) {
108 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
110 } // end while
112 if (defined('PMA_IDX_INCLUDED')) {
113 unset($ret_keys);
114 } else if ($result) {
115 mysql_free_result($result);
118 // Get fields and stores their name/type
119 // fields had already been grabbed in "tbl_properties.php3"
120 if (defined('PMA_IDX_INCLUDED')) {
121 mysql_data_seek($fields_rs, 0);
122 } else {
123 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($table);
124 $fields_rs = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url_0);
125 $fields_cnt = mysql_num_rows($fields_rs);
128 $fields_names = array();
129 $fields_types = array();
130 while ($row = PMA_mysql_fetch_array($fields_rs)) {
131 $fields_names[] = $row['Field'];
132 // loic1: set or enum types: slashes single quotes inside options
133 if (eregi('^(set|enum)\((.+)\)$', $row['Type'], $tmp)) {
134 $tmp[2] = substr(ereg_replace('([^,])\'\'', '\\1\\\'', ',' . $tmp[2]), 1);
135 $fields_types[] = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
136 } else {
137 $fields_types[] = $row['Type'];
139 } // end while
141 if ($fields_rs) {
142 mysql_free_result($fields_rs);
147 * Stipslashes some variables if required
149 if (get_magic_quotes_gpc()) {
150 if (isset($index)) {
151 $index = stripslashes($index);
153 if (isset($old_index)) {
154 $old_index = stripslashes($old_index);
156 } // end if
160 * Do run the query to build the new index and moves back to
161 * "tbl_properties.php3"
163 if (!defined('PMA_IDX_INCLUDED')
164 && (isset($index) && isset($do_save_data))) {
166 $err_url = 'tbl_indexes.php3?' . PMA_generate_common_url($db, $table);
167 if (empty($old_index)) {
168 $err_url .= '&amp;create_index=1&amp;idx_num_fields=' . $idx_num_fields;
169 } else {
170 $err_url .= '&amp;index=' . urlencode($old_index);
173 if ($index_type == 'PRIMARY') {
174 if ($index == '') {
175 $index = 'PRIMARY';
176 } else if ($index != 'PRIMARY') {
177 PMA_mysqlDie($strPrimaryKeyName, '', FALSE, $err_url);
179 } else if ($index == 'PRIMARY') {
180 PMA_mysqlDie($strCantRenameIdxToPrimary, '', FALSE, $err_url);
184 // $sql_query is the one displayed in the query box, don't use it when you
185 // need to generate a query in this script
186 $local_query = 'ALTER TABLE ' . PMA_backquote($table);
188 // Drops the old index
189 if (!empty($old_index)) {
190 if ($old_index == 'PRIMARY') {
191 $local_query .= ' DROP PRIMARY KEY,';
192 } else {
193 $local_query .= ' DROP INDEX ' . PMA_backquote($old_index) .',';
195 } // end if
197 // Builds the new one
198 switch ($index_type) {
199 case 'PRIMARY':
200 $local_query .= ' ADD PRIMARY KEY (';
201 break;
202 case 'FULLTEXT':
203 $local_query .= ' ADD FULLTEXT ' . (empty($index) ? '' : PMA_backquote($index)) . ' (';
204 break;
205 case 'UNIQUE':
206 $local_query .= ' ADD UNIQUE ' . (empty($index) ? '' : PMA_backquote($index)) . ' (';
207 break;
208 case 'INDEX':
209 $local_query .= ' ADD INDEX ' . (empty($index) ? '' : PMA_backquote($index)) . ' (';
210 break;
211 } // end switch
212 $index_fields = '';
213 while (list($i, $name) = each($column)) {
214 if ($name != '--ignore--') {
215 $index_fields .= (empty($index_fields) ? '' : ',')
216 . PMA_backquote(get_magic_quotes_gpc() ? stripslashes($name) : $name)
217 . (empty($sub_part[$i]) ? '' : '(' . $sub_part[$i] . ')');
219 } // end while
220 if (empty($index_fields)){
221 PMA_mysqlDie($strNoIndexPartsDefined, '', FALSE, $err_url);
222 } else {
223 $local_query .= $index_fields . ')';
226 $result = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, FALSE, $err_url);
227 $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered;
229 include('./tbl_properties.php3');
230 exit();
231 } // end builds the new index
235 * Edits an index or defines a new one
237 else if (!defined('PMA_IDX_INCLUDED')
238 && (isset($index) || isset($create_index))) {
240 // Prepares the form values
241 if (!isset($index)) {
242 $index = '';
244 if (!isset($old_index)){
245 $old_index = $index;
247 if (!isset($index_type)) {
248 $index_type = '';
250 if ($old_index == '' || !isset($indexes_info[$old_index])) {
251 $edited_index_info['Sequences'] = array();
252 $edited_index_data = array();
253 for ($i = 1; $i <= $idx_num_fields; $i++) {
254 $edited_index_info['Sequences'][] = $i;
255 $edited_index_data[$i] = array('Column_name' => '', 'Sub_part' => '');
256 } // end for
257 if ($old_index == ''
258 && !isset($indexes_info['PRIMARY'])
259 && ($index_type == '' || $index_type == 'PRIMARY')) {
260 $old_index = 'PRIMARY';
262 } else {
263 $edited_index_info = $indexes_info[$old_index];
264 $edited_index_data = $indexes_data[$old_index];
265 if ($edited_index_info['Comment'] == 'FULLTEXT') {
266 $index_type = 'FULLTEXT';
267 } else if ($index == 'PRIMARY') {
268 $index_type = 'PRIMARY';
269 } else if ($edited_index_info['Non_unique'] == '0') {
270 $index_type = 'UNIQUE';
271 } else {
272 $index_type = 'INDEX';
274 } // end if... else...
276 if (isset($add_fields)) {
277 if (isset($prev_add_fields)) {
278 $added_fields += $prev_add_fields;
280 $field_cnt = count($edited_index_info['Sequences']) + 1;
281 for ($i = $field_cnt; $i < ($added_fields + $field_cnt); $i++) {
282 $edited_index_info['Sequences'][] = $i;
283 $edited_index_data[$i] = array('Column_name' => '', 'Sub_part' => '');
284 } // end for
286 // Restore entered values
287 while (list($i, $name) = each($column)) {
288 if ($name != '--ignore--'){
289 $edited_index_data[$i+1]['Column_name'] = $name;
290 $edited_index_data[$i+1]['Sub_part'] = $sub_part[$i];
292 } // end while
293 } // end if
294 // end preparing form values
297 <!-- Build index form -->
298 <form action="tbl_indexes.php3" method="post" name="index_frm"
299 onsubmit="if (typeof(this.elements['index'].disabled) != 'undefined') {this.elements['index'].disabled = false}">
300 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
301 <?php
302 if (isset($create_index)) {
303 echo '<input type="hidden" name="create_index" value="1" />';
305 echo "\n";
307 <input type="hidden" name="old_index" value="<?php echo (isset($create_index) ? '' : $old_index); ?>" />
308 <b><?php echo '------ ' . (isset($create_index) ? $strCreateIndexTopic : $strModifyIndexTopic) . ' ------'; ?></b>
309 <br /><br />
311 <table border="0">
312 <tr>
313 <td><?php echo $strIndexName; ?>&nbsp;</td>
314 <td>
315 <input type="text" name="index" value="<?php echo htmlspecialchars($index); ?>" class="textfield" onfocus="this.select()" />
316 &nbsp;<?php echo $strPrimaryKeyWarning . "\n"; ?>
317 </td>
318 </tr>
319 <tr>
320 <td><?php echo $strIndexType; ?>&nbsp;</td>
321 <td>
322 <select name="index_type" onchange="return checkIndexName()">
323 <?php
324 echo "\n";
325 for ($i = 0; $i < $index_types_cnt; $i++) {
326 if ($index_types[$i] == 'PRIMARY') {
327 if ($index == 'PRIMARY' || !isset($indexes_info['PRIMARY'])) {
328 echo ' '
329 . '<option value="PRIMARY"' . (($index_type == 'PRIMARY') ? ' selected="selected"' : '') . '>PRIMARY</option>'
330 . "\n";
332 } else {
333 echo ' '
334 . '<option value="' . $index_types[$i] . '"' . (($index_type == $index_types[$i]) ? ' selected="selected"' : '') . '>'. $index_types[$i] . '</option>'
335 . "\n";
337 } // end if... else...
338 } // end for
340 </select>&nbsp;
341 <?php echo PMA_showMySQLDocu('Reference', 'ALTER_TABLE') . "\n"; ?>
342 </td>
343 </tr>
344 </table><br />
346 <table border="<?php echo $cfg['Border']; ?>" cellpadding="5">
347 <tr>
348 <th><?php echo $strField; ?></th>
349 <th><?php echo $strSize; ?></th>
350 </tr>
351 <?php
352 while (list($row_no, $seq_index) = each($edited_index_info['Sequences'])) {
353 $add_type = (is_array($fields_types) && count($fields_types) == count($fields_names));
354 $selected = $edited_index_data[$seq_index]['Column_name'];
355 if (!empty($edited_index_data[$seq_index]['Sub_part'])) {
356 $sub_part = ' value="' . $edited_index_data[$seq_index]['Sub_part'] . '"';
357 } else {
358 $sub_part = '';
360 $bgcolor = (($row_no % 2) ? $cfg['BgcolorOne'] : $cfg['BgcolorTwo']);
361 echo "\n";
363 <tr>
364 <td bgcolor="<?php echo $bgcolor; ?>">
365 <select name="column[]">
366 <option value="--ignore--"<?php if ('--ignore--' == $selected) echo ' selected="selected"'; ?>>
367 -- <?php echo $strIgnore; ?> --</option>
368 <?php
369 reset($fields_names);
370 while (list($key, $val) = each($fields_names)) {
371 if ($index_type != 'FULLTEXT'
372 || eregi('^(varchar|text|tinytext|mediumtext|longtext)', $fields_types[$key])) {
373 echo "\n" . ' '
374 . '<option value="' . htmlspecialchars($val) . '"' . (($val == $selected) ? ' selected="selected"' : '') . '>'
375 . htmlspecialchars($val) . (($add_type) ? ' [' . $fields_types[$key] . ']' : '' ) . '</option>' . "\n";
377 } // end while
378 echo "\n";
380 </select>
381 </td>
382 <td bgcolor="<?php echo $bgcolor; ?>">
383 <input type="text" size="5" name="sub_part[]"<?php echo $sub_part; ?> onfocus="this.select()" />
384 </td>
385 </tr>
386 <?php
387 } // end while
389 echo "\n";
391 </table><br />
393 <input type="submit" name="do_save_data" value="<?php echo $strSave; ?>" /><br />
395 <?php
396 echo "\n";
397 if (isset($added_fields)) {
398 echo ' <input type="hidden" name="prev_add_fields" value="' . $added_fields . '" />';
400 if (isset($idx_num_fields)) {
401 echo ' <input type="hidden" name="idx_num_fields" value="' . $idx_num_fields . '" />' . "\n";
403 echo ' <hr /><br />' . "\n";
404 echo ' ' . sprintf($strAddToIndex, '<input type="text" name="added_fields" size="4" value="1" class="textfield" onfocus="this.select()" />') . "\n";
405 echo ' &nbsp;<input type="submit" name="add_fields" value="' . $strGo . '" onclick="return checkFormElementInRange(this.form, \'added_fields\', 1)" />' . "\n";
407 } else {
409 * Display indexes
412 <!-- Indexes form -->
413 <form action="tbl_indexes.php3" method="post">
414 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
415 <?php
416 echo "\n";
417 echo ' ' . $strIndexes . '&nbsp;:' . "\n";
418 echo ' ' . PMA_showMySQLDocu('MySQL_Optimisation', 'Optimising_Database_Structure') . '<br />' ."\n";
420 if ($idx_cnt > 0) {
422 <table border="<?php echo $cfg['Border']; ?>">
423 <tr>
424 <th><?php echo $strKeyname; ?></th>
425 <th><?php echo $strType; ?></th>
426 <th><?php echo $strCardinality; ?></th>
427 <th colspan="2"><?php echo $strAction; ?></th>
428 <th colspan="2"><?php echo $strField; ?></th>
429 </tr>
430 <?php
431 echo "\n";
432 while (list($index_no, $index_name) = each($indexes)) {
433 $cell_bgd = (($index_no % 2) ? $cfg['BgcolorOne'] : $cfg['BgcolorTwo']);
434 $index_td = ' <td bgcolor="' . $cell_bgd . '" rowspan="' . count($indexes_info[$index_name]['Sequences']) . '">' . "\n";
435 echo ' <tr>' . "\n";
436 echo $index_td
437 . ' ' . htmlspecialchars($index_name) . "\n"
438 . ' </td>' . "\n";
440 if ((PMA_MYSQL_INT_VERSION >= 32323 && PMA_MYSQL_INT_VERSION < 40002 && $indexes_info[$index_name]['Comment'] == 'FULLTEXT')
441 || (PMA_MYSQL_INT_VERSION >= 40002 && $indexes_info[$index_name]['Index_type'] == 'FULLTEXT')) {
442 $index_type = 'FULLTEXT';
443 } else if ($index_name == 'PRIMARY') {
444 $index_type = 'PRIMARY';
445 } else if ($indexes_info[$index_name]['Non_unique'] == '0') {
446 $index_type = 'UNIQUE';
447 } else {
448 $index_type = 'INDEX';
450 echo $index_td
451 . ' ' . $index_type . "\n"
452 . ' </td>' . "\n";
454 echo str_replace('">' . "\n", '" align="right">' . "\n", $index_td)
455 . ' ' . (isset($indexes_info[$index_name]['Cardinality']) ? $indexes_info[$index_name]['Cardinality'] : $strNone) . '&nbsp;' . "\n"
456 . ' </td>' . "\n";
458 if ($index_name == 'PRIMARY') {
459 $local_query = urlencode('ALTER TABLE ' . PMA_backquote($table) . ' DROP PRIMARY KEY');
460 $js_msg = 'ALTER TABLE ' . PMA_jsFormat($table) . ' DROP PRIMARY KEY';
461 $zero_rows = urlencode($strPrimaryKeyHasBeenDropped);
462 } else {
463 $local_query = urlencode('ALTER TABLE ' . PMA_backquote($table) . ' DROP INDEX ' . PMA_backquote($index_name));
464 $js_msg = 'ALTER TABLE ' . PMA_jsFormat($table) . ' DROP INDEX ' . PMA_jsFormat($index_name);
465 $zero_rows = urlencode(sprintf($strIndexHasBeenDropped, htmlspecialchars($index_name)));
467 echo $index_td
468 . ' <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"
469 . ' </td>' . "\n";
471 echo $index_td
472 . ' <a href="tbl_indexes.php3?' . $url_query . '&amp;index=' . urlencode($index_name) . '">' . $strEdit . '</a>' . "\n"
473 . ' </td>' . "\n";
475 while (list($row_no, $seq_index) = each($indexes_info[$index_name]['Sequences'])) {
476 if ($row_no > 0) {
477 echo ' <tr>' . "\n";
479 if (!empty($indexes_data[$index_name][$seq_index]['Sub_part'])) {
480 echo ' <td bgcolor="' . $cell_bgd . '">' . "\n"
481 . ' ' . $indexes_data[$index_name][$seq_index]['Column_name'] . "\n"
482 . ' </td>' . "\n";
483 echo ' <td align="right" bgcolor="' . $cell_bgd . '">' . "\n"
484 . ' ' . $indexes_data[$index_name][$seq_index]['Sub_part'] . "\n"
485 . ' </td>' . "\n";
486 echo ' </tr>' . "\n";
487 } else {
488 echo ' <td bgcolor="' . $cell_bgd . '" colspan="2">' . "\n"
489 . ' ' . $indexes_data[$index_name][$seq_index]['Column_name'] . "\n"
490 . ' </td>' . "\n";
491 echo ' </tr>' . "\n";
493 } // end while
494 } // end while
496 </table><br />
497 <?php
498 echo "\n\n";
499 } // end display indexes
500 else {
501 // none indexes
502 echo "\n" . ' <br />' . "\n";
503 echo ' <i>' . $strNoIndex . '</i><br /><br />' . "\n\n";
506 echo ' ' . sprintf($strCreateIndex, '<input type="text" size="4" name="idx_num_fields" value="1" class="textfield" />') . "\n";
507 echo ' &nbsp;<input type="submit" name="create_index" value="' . $strGo . '" onclick="return checkFormElementInRange(this.form, \'idx_num_fields\', 1)" />' . "\n";
508 echo ' ';
509 } // end display indexes
512 </form>
515 <?php
517 * Displays the footer
519 echo "\n";
521 if (!defined('PMA_IDX_INCLUDED')){
522 include('./footer.inc.php3');