Documentation TYPO3 par Ameos |
00001 <?php 00002 /* $Id: tbl_indexes.php,v 2.24 2005/07/10 20:35:26 nijel Exp $ */ 00003 // vim: expandtab sw=4 ts=4 sts=4: 00004 00008 require_once('./libraries/grab_globals.lib.php'); 00009 require_once('./libraries/common.lib.php'); 00010 require_once('./libraries/tbl_indexes.lib.php'); 00011 00015 $index_types = PMA_get_indextypes(); 00016 $index_types_cnt = count($index_types); 00017 00023 if (!defined('PMA_IDX_INCLUDED')) { 00024 // Not a valid db name -> back to the welcome page 00025 if (!empty($db)) { 00026 $is_db = PMA_DBI_select_db($db); 00027 } 00028 if (empty($db) || !$is_db) { 00029 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . 'main.php?' . PMA_generate_common_url('', '', '&') . (isset($message) ? '&message=' . urlencode($message) : '') . '&reload=1'); 00030 exit; 00031 } 00032 // Not a valid table name -> back to the default db_details sub-page 00033 if (!empty($table)) { 00034 $is_table = PMA_DBI_query('SHOW TABLES LIKE \'' . PMA_sqlAddslashes($table, TRUE) . '\'', NULL, PMA_DBI_QUERY_STORE); 00035 } 00036 if (empty($table) 00037 || !($is_table && PMA_DBI_num_rows($is_table))) { 00038 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . $cfg['DefaultTabDatabase'] . '?' . PMA_generate_common_url($db, '', '&') . (isset($message) ? '&message=' . urlencode($message) : '') . '&reload=1'); 00039 exit; 00040 } else if (isset($is_table)) { 00041 PMA_DBI_free_result($is_table); 00042 } 00043 00044 // Displays headers (if needed) 00045 $js_to_run = ((isset($index) && isset($do_save_data)) ? 'functions.js' : 'indexes.js'); 00046 require_once('./header.inc.php'); 00047 } // end if 00048 00049 00053 if (!defined('PMA_IDX_INCLUDED')) { 00054 $err_url_0 = 'db_details.php?' . PMA_generate_common_url($db); 00055 } 00056 00057 // Gets table keys and store them in arrays 00058 $indexes = array(); 00059 $indexes_info = array(); 00060 $indexes_data = array(); 00061 // keys had already been grabbed in "tbl_properties.php" 00062 if (!defined('PMA_IDX_INCLUDED')) { 00063 $ret_keys = PMA_get_indexes($table, $err_url_0); 00064 } 00065 00066 PMA_extract_indexes($ret_keys, $indexes, $indexes_info, $indexes_data); 00067 00068 // Get fields and stores their name/type 00069 // fields had already been grabbed in "tbl_properties.php" 00070 if (!defined('PMA_IDX_INCLUDED')) { 00071 $fields_rs = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table) . ';'); 00072 $save_row = array(); 00073 while ($row = PMA_DBI_fetch_assoc($fields_rs)) { 00074 $save_row[] = $row; 00075 } 00076 } 00077 00078 $fields_names = array(); 00079 $fields_types = array(); 00080 foreach ($save_row AS $saved_row_key => $row) { 00081 $fields_names[] = $row['Field']; 00082 // loic1: set or enum types: slashes single quotes inside options 00083 if (preg_match('@^(set|enum)\((.+)\)$@i', $row['Type'], $tmp)) { 00084 $tmp[2] = substr(preg_replace('@([^,])\'\'@', '\\1\\\'', ',' . $tmp[2]), 1); 00085 $fields_types[] = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')'; 00086 } else { 00087 $fields_types[] = $row['Type']; 00088 } 00089 } // end while 00090 00091 if ($fields_rs) { 00092 PMA_DBI_free_result($fields_rs); 00093 } 00094 00095 00100 if (!defined('PMA_IDX_INCLUDED') 00101 && (isset($index) && isset($do_save_data))) { 00102 00103 $err_url = 'tbl_indexes.php?' . PMA_generate_common_url($db, $table); 00104 if (empty($old_index)) { 00105 $err_url .= '&create_index=1&idx_num_fields=' . $idx_num_fields; 00106 } else { 00107 $err_url .= '&index=' . urlencode($old_index); 00108 } 00109 00110 if ($index_type == 'PRIMARY') { 00111 if ($index == '') { 00112 $index = 'PRIMARY'; 00113 } else if ($index != 'PRIMARY') { 00114 PMA_mysqlDie($strPrimaryKeyName, '', FALSE, $err_url); 00115 } 00116 } else if ($index == 'PRIMARY') { 00117 PMA_mysqlDie($strCantRenameIdxToPrimary, '', FALSE, $err_url); 00118 } 00119 00120 00121 // $sql_query is the one displayed in the query box 00122 $sql_query = 'ALTER TABLE ' . PMA_backquote($table); 00123 00124 // Drops the old index 00125 if (!empty($old_index)) { 00126 if ($old_index == 'PRIMARY') { 00127 $sql_query .= ' DROP PRIMARY KEY,'; 00128 } else { 00129 $sql_query .= ' DROP INDEX ' . PMA_backquote($old_index) .','; 00130 } 00131 } // end if 00132 00133 // Builds the new one 00134 switch ($index_type) { 00135 case 'PRIMARY': 00136 $sql_query .= ' ADD PRIMARY KEY ('; 00137 break; 00138 case 'FULLTEXT': 00139 $sql_query .= ' ADD FULLTEXT ' . (empty($index) ? '' : PMA_backquote($index)) . ' ('; 00140 break; 00141 case 'UNIQUE': 00142 $sql_query .= ' ADD UNIQUE ' . (empty($index) ? '' : PMA_backquote($index)) . ' ('; 00143 break; 00144 case 'INDEX': 00145 $sql_query .= ' ADD INDEX ' . (empty($index) ? '' : PMA_backquote($index)) . ' ('; 00146 break; 00147 } // end switch 00148 $index_fields = ''; 00149 foreach ($column AS $i => $name) { 00150 if ($name != '--ignore--') { 00151 $index_fields .= (empty($index_fields) ? '' : ',') 00152 . PMA_backquote($name) 00153 . (empty($sub_part[$i]) ? '' : '(' . $sub_part[$i] . ')'); 00154 } 00155 } // end while 00156 if (empty($index_fields)){ 00157 PMA_mysqlDie($strNoIndexPartsDefined, '', FALSE, $err_url); 00158 } else { 00159 $sql_query .= $index_fields . ')'; 00160 } 00161 00162 $result = PMA_DBI_query($sql_query); 00163 $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered; 00164 00165 $active_page = 'tbl_properties_structure.php'; 00166 require('./tbl_properties_structure.php'); 00167 } // end builds the new index 00168 00169 00173 else if (!defined('PMA_IDX_INCLUDED') 00174 && (isset($index) || isset($create_index))) { 00175 00176 // Prepares the form values 00177 if (!isset($index)) { 00178 $index = ''; 00179 } 00180 if (!isset($old_index)){ 00181 $old_index = $index; 00182 } 00183 if (!isset($index_type)) { 00184 $index_type = ''; 00185 } 00186 if ($old_index == '' || !isset($indexes_info[$old_index])) { 00187 $edited_index_info['Sequences'] = array(); 00188 $edited_index_data = array(); 00189 for ($i = 1; $i <= $idx_num_fields; $i++) { 00190 $edited_index_info['Sequences'][] = $i; 00191 $edited_index_data[$i] = array('Column_name' => '', 'Sub_part' => ''); 00192 } // end for 00193 if ($old_index == '' 00194 && !isset($indexes_info['PRIMARY']) 00195 && ($index_type == '' || $index_type == 'PRIMARY')) { 00196 $old_index = 'PRIMARY'; 00197 } 00198 } else { 00199 $edited_index_info = $indexes_info[$old_index]; 00200 $edited_index_data = $indexes_data[$old_index]; 00201 00202 00203 if ((PMA_MYSQL_INT_VERSION < 40002 && $edited_index_info['Comment'] == 'FULLTEXT') 00204 || (PMA_MYSQL_INT_VERSION >= 40002 && $edited_index_info['Index_type'] == 'FULLTEXT')) { 00205 $index_type = 'FULLTEXT'; 00206 } else if ($index == 'PRIMARY') { 00207 $index_type = 'PRIMARY'; 00208 } else if ($edited_index_info['Non_unique'] == '0') { 00209 $index_type = 'UNIQUE'; 00210 } else { 00211 $index_type = 'INDEX'; 00212 } 00213 } // end if... else... 00214 00215 if (isset($add_fields)) { 00216 if (isset($prev_add_fields)) { 00217 $added_fields += $prev_add_fields; 00218 } 00219 $field_cnt = count($edited_index_info['Sequences']) + 1; 00220 for ($i = $field_cnt; $i < ($added_fields + $field_cnt); $i++) { 00221 $edited_index_info['Sequences'][] = $i; 00222 $edited_index_data[$i] = array('Column_name' => '', 'Sub_part' => ''); 00223 } // end for 00224 00225 // Restore entered values 00226 foreach ($column AS $i => $name) { 00227 if ($name != '--ignore--'){ 00228 $edited_index_data[$i+1]['Column_name'] = $name; 00229 $edited_index_data[$i+1]['Sub_part'] = $sub_part[$i]; 00230 } 00231 } // end while 00232 } // end if 00233 // end preparing form values 00234 ?> 00235 00236 <!-- Build index form --> 00237 <form action="./tbl_indexes.php" method="post" name="index_frm" 00238 onsubmit="if (typeof(this.elements['index'].disabled) != 'undefined') {this.elements['index'].disabled = false}"> 00239 <table border="0" cellpadding="2" cellspacing="1"> 00240 <tr><td class="tblHeaders" colspan="2"> 00241 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?> 00242 <?php 00243 if (isset($create_index)) { 00244 echo '<input type="hidden" name="create_index" value="1" />'; 00245 } 00246 echo "\n"; 00247 ?> 00248 <input type="hidden" name="old_index" value="<?php echo (isset($create_index) ? '' : htmlspecialchars($old_index)); ?>" /> 00249 <?php echo ' ' . (isset($create_index) ? $strCreateIndexTopic : $strModifyIndexTopic) . ' '; ?> 00250 </th></tr> 00251 00252 00253 <tr> 00254 <td align="right"><b><?php echo $strIndexName; ?></b> </th> 00255 <td> 00256 <input type="text" name="index" value="<?php echo htmlspecialchars($index); ?>" size="25" onfocus="this.select()" /> 00257 </td> 00258 </tr> 00259 <tr><td align="right"><?php 00260 if ($cfg['ErrorIconic']) { 00261 echo '<img src="' . $pmaThemeImage . 's_warn.png" width="16" height="16" border="0" alt="Attention" />'; 00262 } 00263 ?></td><td><?php echo $strPrimaryKeyWarning . "\n"; ?></td></tr> 00264 <tr> 00265 <td align="right"><b><?php echo $strIndexType; ?></b> </td> 00266 <td> 00267 <select name="index_type" onchange="return checkIndexName()"> 00268 <?php 00269 echo "\n"; 00270 for ($i = 0; $i < $index_types_cnt; $i++) { 00271 if ($index_types[$i] == 'PRIMARY') { 00272 if ($index == 'PRIMARY' || !isset($indexes_info['PRIMARY'])) { 00273 echo ' ' 00274 . '<option value="PRIMARY"' . (($index_type == 'PRIMARY') ? ' selected="selected"' : '') . '>PRIMARY</option>' 00275 . "\n"; 00276 } 00277 } else { 00278 echo ' ' 00279 . '<option value="' . $index_types[$i] . '"' . (($index_type == $index_types[$i]) ? ' selected="selected"' : '') . '>'. $index_types[$i] . '</option>' 00280 . "\n"; 00281 00282 } // end if... else... 00283 } // end for 00284 ?> 00285 </select> 00286 <?php echo PMA_showMySQLDocu('Reference', 'ALTER_TABLE') . "\n"; ?> 00287 </td> 00288 </tr> 00289 00290 <tr><td valign="top" align="right"><b><?php echo $strFields; ?> :</b> </td><td><table border="<?php echo $cfg['Border']; ?>" cellpadding="2" cellspacing="1"> 00291 <tr> 00292 <th><?php echo $strField; ?></th> 00293 <th><?php echo $strSize; ?></th> 00294 </tr> 00295 <?php 00296 foreach ($edited_index_info['Sequences'] AS $row_no => $seq_index) { 00297 $add_type = (is_array($fields_types) && count($fields_types) == count($fields_names)); 00298 $selected = $edited_index_data[$seq_index]['Column_name']; 00299 if (!empty($edited_index_data[$seq_index]['Sub_part'])) { 00300 $sub_part = ' value="' . $edited_index_data[$seq_index]['Sub_part'] . '"'; 00301 } else { 00302 $sub_part = ''; 00303 } 00304 $bgcolor = (($row_no % 2) ? $cfg['BgcolorOne'] : $cfg['BgcolorTwo']); 00305 echo "\n"; 00306 ?> 00307 <tr> 00308 <td bgcolor="<?php echo $bgcolor; ?>"> 00309 <select name="column[]"> 00310 <option value="--ignore--"<?php if ('--ignore--' == $selected) echo ' selected="selected"'; ?>> 00311 -- <?php echo $strIgnore; ?> --</option> 00312 <?php 00313 foreach ($fields_names AS $key => $val) { 00314 if ($index_type != 'FULLTEXT' 00315 || preg_match('@^(varchar|text|tinytext|mediumtext|longtext)@i', $fields_types[$key])) { 00316 echo "\n" . ' ' 00317 . '<option value="' . htmlspecialchars($val) . '"' . (($val == $selected) ? ' selected="selected"' : '') . '>' 00318 . htmlspecialchars($val) . (($add_type) ? ' [' . $fields_types[$key] . ']' : '' ) . '</option>' . "\n"; 00319 } 00320 } // end while 00321 echo "\n"; 00322 ?> 00323 </select> 00324 </td> 00325 <td bgcolor="<?php echo $bgcolor; ?>"> 00326 <input type="text" size="5" name="sub_part[]"<?php echo $sub_part; ?> onfocus="this.select()" /> 00327 </td> 00328 </tr> 00329 <?php 00330 } // end while 00331 00332 echo "\n"; 00333 ?> 00334 <tr><td colspan="2"><?php 00335 echo "\n"; 00336 if (isset($added_fields)) { 00337 echo ' <input type="hidden" name="prev_add_fields" value="' . $added_fields . '" />'; 00338 } 00339 if (isset($idx_num_fields)) { 00340 echo ' <input type="hidden" name="idx_num_fields" value="' . $idx_num_fields . '" />' . "\n"; 00341 } 00342 echo ' ' . "\n"; 00343 echo ' ' . sprintf($strAddToIndex, '<input type="text" name="added_fields" size="2" value="1" onfocus="this.select()" style="vertical-align: middle;" />') . "\n"; 00344 echo ' <input type="submit" name="add_fields" value="' . $strGo . '" onclick="return checkFormElementInRange(this.form, \'added_fields\', \'' . str_replace('\'', '\\\'', $GLOBALS['strInvalidColumnCount']) . '\', 1)" style="vertical-align: middle;" />' . "\n"; 00345 ?></td> 00346 </tr> 00347 </table></td></tr> 00348 <tr><td colspan="2" class="tblFooters" align="center"> 00349 <input type="submit" name="do_save_data" value="<?php echo $strSave; ?>" /></td></tr> 00350 </table> 00351 </form> 00352 <?php 00353 } else { 00357 ?> 00358 <!-- Indexes form --> 00359 <form action="./tbl_indexes.php" method="post" onsubmit="return checkFormElementInRange(this, 'idx_num_fields', '<?php echo str_replace('\'', '\\\'', $GLOBALS['strInvalidColumnCount']); ?>', 1)"> 00360 <table border="0" cellpadding="2" cellspacing="1"> 00361 <tr><td class="tblHeaders" colspan="7"> 00362 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?> 00363 <?php 00364 echo "\n"; 00365 echo ' ' . $strIndexes . ':' . "\n"; 00366 echo ' ' . PMA_showMySQLDocu('MySQL_Optimisation', 'Optimising_Database_Structure') . "\n"; 00367 ?></td></tr><?php 00368 $edit_link_text = ''; 00369 $drop_link_text = ''; 00370 00371 // We need to copy the value or else the == 'both' check will always return true 00372 $propicon = (string)$cfg['PropertiesIconic']; 00373 00374 if ($cfg['PropertiesIconic'] === true || $propicon == 'both') { 00375 $edit_link_text = '<img src="' . $pmaThemeImage . 'b_edit.png" width="16" height="16" hspace="2" border="0" title="' . $strEdit . '" alt="' . $strEdit . '" />'; 00376 $drop_link_text = '<img src="' . $pmaThemeImage . 'b_drop.png" width="16" height="16" hspace="2" border="0" title="' . $strDrop . '" alt="' . $strDrop . '" />'; 00377 } 00378 if ($cfg['PropertiesIconic'] === false || $propicon == 'both') { 00379 $edit_link_text .= $strEdit; 00380 $drop_link_text .= $strDrop; 00381 } 00382 if ($propicon == 'both') { 00383 $edit_link_text = '<nobr>' . $edit_link_text . '</nobr>'; 00384 $drop_link_text = '<nobr>' . $drop_link_text . '</nobr>'; 00385 } 00386 00387 if (count($ret_keys) > 0) { 00388 ?> 00389 <!--table border="<?php echo $cfg['Border']; ?>" cellpadding="2" cellspacing="1"--> 00390 <tr> 00391 <th><?php echo $strKeyname; ?></th> 00392 <th><?php echo $strType; ?></th> 00393 <th><?php echo $strCardinality; ?></th> 00394 <th colspan="2"><?php echo $strAction; ?></th> 00395 <th colspan="2"><?php echo $strField; ?></th> 00396 </tr> 00397 <?php 00398 $idx_collection = PMA_show_indexes($table, $indexes, $indexes_info, $indexes_data, true); 00399 echo PMA_check_indexes($idx_collection); 00400 } // end display indexes 00401 else { 00402 // none indexes 00403 echo "\n" . ' <tr><td colspan=7" align="center">' . "\n"; 00404 if ($cfg['ErrorIconic']) { 00405 echo '<img src="' . $pmaThemeImage . 's_warn.png" width="16" height="16" border="0" alt="Warning" hspace="2" align="middle" />'; 00406 } 00407 echo ' <b>' . $strNoIndex . '</b></td></tr>' . "\n\n"; 00408 } 00409 00410 echo '<tr><td colspan="7" class="tblFooters" nowrap="nowrap" align="center"> ' 00411 . sprintf($strCreateIndex, '<input type="text" size="2" name="idx_num_fields" value="1" style="vertical-align: middle;" />') . "\n"; 00412 echo ' <input type="submit" name="create_index" value="' . $strGo . '" onclick="return checkFormElementInRange(this.form, \'idx_num_fields\', \'' . str_replace('\'', '\\\'', $GLOBALS['strInvalidColumnCount']) . '\', 1)" style="vertical-align: middle;" />' . "\n"; 00413 echo '</td></tr> '; 00414 ?> 00415 </table></form> 00416 <?php 00417 } // end display indexes 00418 00419 00423 echo "\n"; 00424 00425 if (!defined('PMA_IDX_INCLUDED')){ 00426 require_once('./footer.inc.php'); 00427 } 00428 ?>