Documentation TYPO3 par Ameos |
00001 <?php 00002 /* $Id: tbl_indexes.php,v 2.3 2003/11/26 22:52:24 rabus Exp $ */ 00003 // vim: expandtab sw=4 ts=4 sts=4: 00004 00005 00009 require_once('./libraries/grab_globals.lib.php'); 00010 require_once('./libraries/common.lib.php'); 00011 00012 00016 $index_types_cnt = 4; 00017 $index_types = array( 00018 'PRIMARY', 00019 'INDEX', 00020 'UNIQUE', 00021 'FULLTEXT' 00022 ); 00023 00029 if (!defined('PMA_IDX_INCLUDED')) { 00030 // Not a valid db name -> back to the welcome page 00031 if (!empty($db)) { 00032 $is_db = @PMA_mysql_select_db($db); 00033 } 00034 if (empty($db) || !$is_db) { 00035 header('Location: ' . $cfg['PmaAbsoluteUri'] . 'main.php?' . PMA_generate_common_url('', '', '&') . (isset($message) ? '&message=' . urlencode($message) : '') . '&reload=1'); 00036 exit; 00037 } 00038 // Not a valid table name -> back to the default db_details sub-page 00039 if (!empty($table)) { 00040 $is_table = @PMA_mysql_query('SHOW TABLES LIKE \'' . PMA_sqlAddslashes($table, TRUE) . '\''); 00041 } 00042 if (empty($table) 00043 || !($is_table && @mysql_numrows($is_table))) { 00044 header('Location: ' . $cfg['PmaAbsoluteUri'] . $cfg['DefaultTabDatabase'] . '?' . PMA_generate_common_url($db, '', '&') . (isset($message) ? '&message=' . urlencode($message) : '') . '&reload=1'); 00045 exit; 00046 } else if (isset($is_table)) { 00047 mysql_free_result($is_table); 00048 } 00049 00050 // Displays headers (if needed) 00051 $js_to_run = ((isset($index) && isset($do_save_data)) ? 'functions.js' : 'indexes.js'); 00052 require_once('./header.inc.php'); 00053 } // end if 00054 00055 00059 if (defined('PMA_IDX_INCLUDED')) { 00060 $err_url_0 = 'db_details.php?' . PMA_generate_common_url($db); 00061 } 00062 00063 // Gets table keys and store them in arrays 00064 $indexes = array(); 00065 $prev_index = ''; 00066 $indexes_info = array(); 00067 $indexes_data = array(); 00068 // keys had already been grabbed in "tbl_properties.php" 00069 if (defined('PMA_IDX_INCLUDED')) { 00070 $idx_cnt = count($ret_keys); 00071 } else { 00072 $local_query = 'SHOW KEYS FROM ' . PMA_backquote($table); 00073 $result = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url_0); 00074 $idx_cnt = mysql_num_rows($result); 00075 } 00076 00077 for ($i = 0; $i < $idx_cnt; $i++) { 00078 $row = (defined('PMA_IDX_INCLUDED') ? $ret_keys[$i] : PMA_mysql_fetch_array($result)); 00079 00080 if ($row['Key_name'] != $prev_index ){ 00081 $indexes[] = $row['Key_name']; 00082 $prev_index = $row['Key_name']; 00083 } 00084 $indexes_info[$row['Key_name']]['Sequences'][] = $row['Seq_in_index']; 00085 $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique']; 00086 if (isset($row['Cardinality'])) { 00087 $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality']; 00088 } 00089 // I don't know what does following column mean.... 00090 // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed']; 00091 $indexes_info[$row['Key_name']]['Comment'] = (isset($row['Comment'])) 00092 ? $row['Comment'] 00093 : ''; 00094 $indexes_info[$row['Key_name']]['Index_type'] = (isset($row['Index_type'])) 00095 ? $row['Index_type'] 00096 : ''; 00097 00098 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name']; 00099 if (isset($row['Sub_part'])) { 00100 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part']; 00101 } 00102 } // end while 00103 00104 if (defined('PMA_IDX_INCLUDED')) { 00105 unset($ret_keys); 00106 } else if ($result) { 00107 mysql_free_result($result); 00108 } 00109 00110 // Get fields and stores their name/type 00111 // fields had already been grabbed in "tbl_properties.php" 00112 if (defined('PMA_IDX_INCLUDED')) { 00113 mysql_data_seek($fields_rs, 0); 00114 } else { 00115 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($table); 00116 $fields_rs = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url_0); 00117 $fields_cnt = mysql_num_rows($fields_rs); 00118 } 00119 00120 $fields_names = array(); 00121 $fields_types = array(); 00122 while ($row = PMA_mysql_fetch_array($fields_rs)) { 00123 $fields_names[] = $row['Field']; 00124 // loic1: set or enum types: slashes single quotes inside options 00125 if (preg_match('@^(set|enum)\((.+)\)$@i', $row['Type'], $tmp)) { 00126 $tmp[2] = substr(preg_replace('@([^,])\'\'@', '\\1\\\'', ',' . $tmp[2]), 1); 00127 $fields_types[] = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')'; 00128 } else { 00129 $fields_types[] = $row['Type']; 00130 } 00131 } // end while 00132 00133 if ($fields_rs) { 00134 mysql_free_result($fields_rs); 00135 } 00136 00137 00142 if (!defined('PMA_IDX_INCLUDED') 00143 && (isset($index) && isset($do_save_data))) { 00144 00145 $err_url = 'tbl_indexes.php?' . PMA_generate_common_url($db, $table); 00146 if (empty($old_index)) { 00147 $err_url .= '&create_index=1&idx_num_fields=' . $idx_num_fields; 00148 } else { 00149 $err_url .= '&index=' . urlencode($old_index); 00150 } 00151 00152 if ($index_type == 'PRIMARY') { 00153 if ($index == '') { 00154 $index = 'PRIMARY'; 00155 } else if ($index != 'PRIMARY') { 00156 PMA_mysqlDie($strPrimaryKeyName, '', FALSE, $err_url); 00157 } 00158 } else if ($index == 'PRIMARY') { 00159 PMA_mysqlDie($strCantRenameIdxToPrimary, '', FALSE, $err_url); 00160 } 00161 00162 00163 // $sql_query is the one displayed in the query box 00164 $sql_query = 'ALTER TABLE ' . PMA_backquote($table); 00165 00166 // Drops the old index 00167 if (!empty($old_index)) { 00168 if ($old_index == 'PRIMARY') { 00169 $sql_query .= ' DROP PRIMARY KEY,'; 00170 } else { 00171 $sql_query .= ' DROP INDEX ' . PMA_backquote($old_index) .','; 00172 } 00173 } // end if 00174 00175 // Builds the new one 00176 switch ($index_type) { 00177 case 'PRIMARY': 00178 $sql_query .= ' ADD PRIMARY KEY ('; 00179 break; 00180 case 'FULLTEXT': 00181 $sql_query .= ' ADD FULLTEXT ' . (empty($index) ? '' : PMA_backquote($index)) . ' ('; 00182 break; 00183 case 'UNIQUE': 00184 $sql_query .= ' ADD UNIQUE ' . (empty($index) ? '' : PMA_backquote($index)) . ' ('; 00185 break; 00186 case 'INDEX': 00187 $sql_query .= ' ADD INDEX ' . (empty($index) ? '' : PMA_backquote($index)) . ' ('; 00188 break; 00189 } // end switch 00190 $index_fields = ''; 00191 foreach($column AS $i => $name) { 00192 if ($name != '--ignore--') { 00193 $index_fields .= (empty($index_fields) ? '' : ',') 00194 . PMA_backquote($name) 00195 . (empty($sub_part[$i]) ? '' : '(' . $sub_part[$i] . ')'); 00196 } 00197 } // end while 00198 if (empty($index_fields)){ 00199 PMA_mysqlDie($strNoIndexPartsDefined, '', FALSE, $err_url); 00200 } else { 00201 $sql_query .= $index_fields . ')'; 00202 } 00203 00204 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', $sql_query, FALSE, $err_url); 00205 $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered; 00206 00207 $active_page = 'tbl_properties_structure.php'; 00208 require('./tbl_properties_structure.php'); 00209 } // end builds the new index 00210 00211 00215 else if (!defined('PMA_IDX_INCLUDED') 00216 && (isset($index) || isset($create_index))) { 00217 00218 // Prepares the form values 00219 if (!isset($index)) { 00220 $index = ''; 00221 } 00222 if (!isset($old_index)){ 00223 $old_index = $index; 00224 } 00225 if (!isset($index_type)) { 00226 $index_type = ''; 00227 } 00228 if ($old_index == '' || !isset($indexes_info[$old_index])) { 00229 $edited_index_info['Sequences'] = array(); 00230 $edited_index_data = array(); 00231 for ($i = 1; $i <= $idx_num_fields; $i++) { 00232 $edited_index_info['Sequences'][] = $i; 00233 $edited_index_data[$i] = array('Column_name' => '', 'Sub_part' => ''); 00234 } // end for 00235 if ($old_index == '' 00236 && !isset($indexes_info['PRIMARY']) 00237 && ($index_type == '' || $index_type == 'PRIMARY')) { 00238 $old_index = 'PRIMARY'; 00239 } 00240 } else { 00241 $edited_index_info = $indexes_info[$old_index]; 00242 $edited_index_data = $indexes_data[$old_index]; 00243 00244 00245 if ((PMA_MYSQL_INT_VERSION < 40002 && $edited_index_info['Comment'] == 'FULLTEXT') 00246 || (PMA_MYSQL_INT_VERSION >= 40002 && $edited_index_info['Index_type'] == 'FULLTEXT')) { 00247 $index_type = 'FULLTEXT'; 00248 } else if ($index == 'PRIMARY') { 00249 $index_type = 'PRIMARY'; 00250 } else if ($edited_index_info['Non_unique'] == '0') { 00251 $index_type = 'UNIQUE'; 00252 } else { 00253 $index_type = 'INDEX'; 00254 } 00255 } // end if... else... 00256 00257 if (isset($add_fields)) { 00258 if (isset($prev_add_fields)) { 00259 $added_fields += $prev_add_fields; 00260 } 00261 $field_cnt = count($edited_index_info['Sequences']) + 1; 00262 for ($i = $field_cnt; $i < ($added_fields + $field_cnt); $i++) { 00263 $edited_index_info['Sequences'][] = $i; 00264 $edited_index_data[$i] = array('Column_name' => '', 'Sub_part' => ''); 00265 } // end for 00266 00267 // Restore entered values 00268 foreach($column AS $i => $name) { 00269 if ($name != '--ignore--'){ 00270 $edited_index_data[$i+1]['Column_name'] = $name; 00271 $edited_index_data[$i+1]['Sub_part'] = $sub_part[$i]; 00272 } 00273 } // end while 00274 } // end if 00275 // end preparing form values 00276 ?> 00277 00278 <!-- Build index form --> 00279 <form action="tbl_indexes.php" method="post" name="index_frm" 00280 onsubmit="if (typeof(this.elements['index'].disabled) != 'undefined') {this.elements['index'].disabled = false}"> 00281 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?> 00282 <?php 00283 if (isset($create_index)) { 00284 echo '<input type="hidden" name="create_index" value="1" />'; 00285 } 00286 echo "\n"; 00287 ?> 00288 <input type="hidden" name="old_index" value="<?php echo (isset($create_index) ? '' : htmlspecialchars($old_index)); ?>" /> 00289 <b><?php echo '------ ' . (isset($create_index) ? $strCreateIndexTopic : $strModifyIndexTopic) . ' ------'; ?></b> 00290 <br /><br /> 00291 00292 <table border="0"> 00293 <tr> 00294 <td><?php echo $strIndexName; ?> </td> 00295 <td> 00296 <input type="text" name="index" value="<?php echo htmlspecialchars($index); ?>" class="textfield" onfocus="this.select()" /> 00297 <?php echo $strPrimaryKeyWarning . "\n"; ?> 00298 </td> 00299 </tr> 00300 <tr> 00301 <td><?php echo $strIndexType; ?> </td> 00302 <td> 00303 <select name="index_type" onchange="return checkIndexName()"> 00304 <?php 00305 echo "\n"; 00306 for ($i = 0; $i < $index_types_cnt; $i++) { 00307 if ($index_types[$i] == 'PRIMARY') { 00308 if ($index == 'PRIMARY' || !isset($indexes_info['PRIMARY'])) { 00309 echo ' ' 00310 . '<option value="PRIMARY"' . (($index_type == 'PRIMARY') ? ' selected="selected"' : '') . '>PRIMARY</option>' 00311 . "\n"; 00312 } 00313 } else { 00314 echo ' ' 00315 . '<option value="' . $index_types[$i] . '"' . (($index_type == $index_types[$i]) ? ' selected="selected"' : '') . '>'. $index_types[$i] . '</option>' 00316 . "\n"; 00317 00318 } // end if... else... 00319 } // end for 00320 ?> 00321 </select> 00322 <?php echo PMA_showMySQLDocu('Reference', 'ALTER_TABLE') . "\n"; ?> 00323 </td> 00324 </tr> 00325 </table><br /> 00326 00327 <table border="<?php echo $cfg['Border']; ?>" cellpadding="5"> 00328 <tr> 00329 <th><?php echo $strField; ?></th> 00330 <th><?php echo $strSize; ?></th> 00331 </tr> 00332 <?php 00333 foreach($edited_index_info['Sequences'] AS $row_no => $seq_index) { 00334 $add_type = (is_array($fields_types) && count($fields_types) == count($fields_names)); 00335 $selected = $edited_index_data[$seq_index]['Column_name']; 00336 if (!empty($edited_index_data[$seq_index]['Sub_part'])) { 00337 $sub_part = ' value="' . $edited_index_data[$seq_index]['Sub_part'] . '"'; 00338 } else { 00339 $sub_part = ''; 00340 } 00341 $bgcolor = (($row_no % 2) ? $cfg['BgcolorOne'] : $cfg['BgcolorTwo']); 00342 echo "\n"; 00343 ?> 00344 <tr> 00345 <td bgcolor="<?php echo $bgcolor; ?>"> 00346 <select name="column[]"> 00347 <option value="--ignore--"<?php if ('--ignore--' == $selected) echo ' selected="selected"'; ?>> 00348 -- <?php echo $strIgnore; ?> --</option> 00349 <?php 00350 foreach($fields_names AS $key => $val) { 00351 if ($index_type != 'FULLTEXT' 00352 || preg_match('@^(varchar|text|tinytext|mediumtext|longtext)@i', $fields_types[$key])) { 00353 echo "\n" . ' ' 00354 . '<option value="' . htmlspecialchars($val) . '"' . (($val == $selected) ? ' selected="selected"' : '') . '>' 00355 . htmlspecialchars($val) . (($add_type) ? ' [' . $fields_types[$key] . ']' : '' ) . '</option>' . "\n"; 00356 } 00357 } // end while 00358 echo "\n"; 00359 ?> 00360 </select> 00361 </td> 00362 <td bgcolor="<?php echo $bgcolor; ?>"> 00363 <input type="text" size="5" name="sub_part[]"<?php echo $sub_part; ?> onfocus="this.select()" /> 00364 </td> 00365 </tr> 00366 <?php 00367 } // end while 00368 00369 echo "\n"; 00370 ?> 00371 </table><br /> 00372 00373 <input type="submit" name="do_save_data" value="<?php echo $strSave; ?>" /><br /> 00374 00375 <?php 00376 echo "\n"; 00377 if (isset($added_fields)) { 00378 echo ' <input type="hidden" name="prev_add_fields" value="' . $added_fields . '" />'; 00379 } 00380 if (isset($idx_num_fields)) { 00381 echo ' <input type="hidden" name="idx_num_fields" value="' . $idx_num_fields . '" />' . "\n"; 00382 } 00383 echo ' <hr /><br />' . "\n"; 00384 echo ' ' . sprintf($strAddToIndex, '<input type="text" name="added_fields" size="4" value="1" class="textfield" onfocus="this.select()" />') . "\n"; 00385 echo ' <input type="submit" name="add_fields" value="' . $strGo . '" onclick="return checkFormElementInRange(this.form, \'added_fields\', 1)" />' . "\n"; 00386 00387 } else { 00391 ?> 00392 <!-- Indexes form --> 00393 <form action="tbl_indexes.php" method="post"> 00394 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?> 00395 <?php 00396 echo "\n"; 00397 echo ' ' . $strIndexes . ' :' . "\n"; 00398 echo ' ' . PMA_showMySQLDocu('MySQL_Optimisation', 'Optimising_Database_Structure') . '<br />' ."\n"; 00399 00400 if ($idx_cnt > 0) { 00401 ?> 00402 <table border="<?php echo $cfg['Border']; ?>"> 00403 <tr> 00404 <th><?php echo $strKeyname; ?></th> 00405 <th><?php echo $strType; ?></th> 00406 <th><?php echo $strCardinality; ?></th> 00407 <th colspan="2"><?php echo $strAction; ?></th> 00408 <th colspan="2"><?php echo $strField; ?></th> 00409 </tr> 00410 <?php 00411 echo "\n"; 00412 foreach($indexes AS $index_no => $index_name) { 00413 $cell_bgd = (($index_no % 2) ? $cfg['BgcolorOne'] : $cfg['BgcolorTwo']); 00414 $index_td = ' <td bgcolor="' . $cell_bgd . '" rowspan="' . count($indexes_info[$index_name]['Sequences']) . '">' . "\n"; 00415 echo ' <tr>' . "\n"; 00416 echo $index_td 00417 . ' ' . htmlspecialchars($index_name) . "\n" 00418 . ' </td>' . "\n"; 00419 00420 if ((PMA_MYSQL_INT_VERSION < 40002 && $indexes_info[$index_name]['Comment'] == 'FULLTEXT') 00421 || (PMA_MYSQL_INT_VERSION >= 40002 && $indexes_info[$index_name]['Index_type'] == 'FULLTEXT')) { 00422 $index_type = 'FULLTEXT'; 00423 } else if ($index_name == 'PRIMARY') { 00424 $index_type = 'PRIMARY'; 00425 } else if ($indexes_info[$index_name]['Non_unique'] == '0') { 00426 $index_type = 'UNIQUE'; 00427 } else { 00428 $index_type = 'INDEX'; 00429 } 00430 echo $index_td 00431 . ' ' . $index_type . "\n" 00432 . ' </td>' . "\n"; 00433 00434 echo str_replace('">' . "\n", '" align="right">' . "\n", $index_td) 00435 . ' ' . (isset($indexes_info[$index_name]['Cardinality']) ? $indexes_info[$index_name]['Cardinality'] : $strNone) . ' ' . "\n" 00436 . ' </td>' . "\n"; 00437 00438 echo $index_td 00439 . ' <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" 00440 . ' </td>' . "\n"; 00441 00442 if ($index_name == 'PRIMARY') { 00443 $local_query = urlencode('ALTER TABLE ' . PMA_backquote($table) . ' DROP PRIMARY KEY'); 00444 $js_msg = 'ALTER TABLE ' . PMA_jsFormat($table) . ' DROP PRIMARY KEY'; 00445 $zero_rows = urlencode($strPrimaryKeyHasBeenDropped); 00446 } else { 00447 $local_query = urlencode('ALTER TABLE ' . PMA_backquote($table) . ' DROP INDEX ' . PMA_backquote($index_name)); 00448 $js_msg = 'ALTER TABLE ' . PMA_jsFormat($table) . ' DROP INDEX ' . PMA_jsFormat($index_name); 00449 $zero_rows = urlencode(sprintf($strIndexHasBeenDropped, htmlspecialchars($index_name))); 00450 } 00451 echo $index_td 00452 . ' <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" 00453 . ' </td>' . "\n"; 00454 00455 foreach($indexes_info[$index_name]['Sequences'] AS $row_no => $seq_index) { 00456 if ($row_no > 0) { 00457 echo ' <tr>' . "\n"; 00458 } 00459 if (!empty($indexes_data[$index_name][$seq_index]['Sub_part'])) { 00460 echo ' <td bgcolor="' . $cell_bgd . '">' . "\n" 00461 . ' ' . $indexes_data[$index_name][$seq_index]['Column_name'] . "\n" 00462 . ' </td>' . "\n"; 00463 echo ' <td align="right" bgcolor="' . $cell_bgd . '">' . "\n" 00464 . ' ' . $indexes_data[$index_name][$seq_index]['Sub_part'] . "\n" 00465 . ' </td>' . "\n"; 00466 echo ' </tr>' . "\n"; 00467 } else { 00468 echo ' <td bgcolor="' . $cell_bgd . '" colspan="2">' . "\n" 00469 . ' ' . htmlspecialchars($indexes_data[$index_name][$seq_index]['Column_name']) . "\n" 00470 . ' </td>' . "\n"; 00471 echo ' </tr>' . "\n"; 00472 } 00473 } // end while 00474 } // end while 00475 ?> 00476 </table><br /> 00477 <?php 00478 echo "\n\n"; 00479 } // end display indexes 00480 else { 00481 // none indexes 00482 echo "\n" . ' <br />' . "\n"; 00483 echo ' <i>' . $strNoIndex . '</i><br /><br />' . "\n\n"; 00484 } 00485 00486 echo ' ' . sprintf($strCreateIndex, '<input type="text" size="4" name="idx_num_fields" value="1" class="textfield" />') . "\n"; 00487 echo ' <input type="submit" name="create_index" value="' . $strGo . '" onclick="return checkFormElementInRange(this.form, \'idx_num_fields\', 1)" />' . "\n"; 00488 echo ' '; 00489 } // end display indexes 00490 00491 ?> 00492 </form> 00493 00494 00495 <?php 00499 echo "\n"; 00500 00501 if (!defined('PMA_IDX_INCLUDED')){ 00502 require_once('./footer.inc.php'); 00503 } 00504 ?>