sorry, wrong version checked in
[phpmyadmin/arisferyanto.git] / tbl_indexes.php
blob8714c09c92123f28474b3f1d3535376d81a24880
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 /**
6 * Gets some core libraries
7 */
8 require_once('./libraries/common.lib.php');
9 require_once('./libraries/tbl_indexes.lib.php');
11 /**
12 * Defines the index types ("FULLTEXT" is available since MySQL 3.23.23)
14 $index_types = PMA_get_indextypes();
15 $index_types_cnt = count($index_types);
17 /**
18 * Ensures the db & table are valid, then loads headers and gets indexes
19 * informations.
20 * Skipped if this script is called by "tbl_properties.php"
22 if (!defined('PMA_IDX_INCLUDED')) {
23 // Not a valid db name -> back to the welcome page
24 if (!empty($db)) {
25 $is_db = PMA_DBI_select_db($db);
27 if (empty($db) || !$is_db) {
28 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . 'main.php?' . PMA_generate_common_url('', '', '&') . (isset($message) ? '&message=' . urlencode($message) : '') . '&reload=1');
29 exit;
31 // Not a valid table name -> back to the default db_details sub-page
32 if (!empty($table)) {
33 $is_table = PMA_DBI_query('SHOW TABLES LIKE \'' . PMA_sqlAddslashes($table, TRUE) . '\'', NULL, PMA_DBI_QUERY_STORE);
35 if (empty($table)
36 || !($is_table && PMA_DBI_num_rows($is_table))) {
37 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . $cfg['DefaultTabDatabase'] . '?' . PMA_generate_common_url($db, '', '&') . (isset($message) ? '&message=' . urlencode($message) : '') . '&reload=1');
38 exit;
39 } else if (isset($is_table)) {
40 PMA_DBI_free_result($is_table);
43 // Displays headers (if needed)
44 $js_to_run = ((isset($index) && isset($do_save_data)) ? 'functions.js' : 'indexes.js');
45 require_once('./header.inc.php');
46 } // end if
49 /**
50 * Gets fields and indexes informations
52 if (!defined('PMA_IDX_INCLUDED')) {
53 $err_url_0 = 'db_details.php?' . PMA_generate_common_url($db);
56 // Gets table keys and store them in arrays
57 $indexes = array();
58 $indexes_info = array();
59 $indexes_data = array();
60 // keys had already been grabbed in "tbl_properties.php"
61 if (!defined('PMA_IDX_INCLUDED')) {
62 $ret_keys = PMA_get_indexes($table, $err_url_0);
65 PMA_extract_indexes($ret_keys, $indexes, $indexes_info, $indexes_data);
67 // Get fields and stores their name/type
68 // fields had already been grabbed in "tbl_properties.php"
69 if (!defined('PMA_IDX_INCLUDED')) {
70 $fields_rs = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table) . ';');
71 $save_row = array();
72 while ($row = PMA_DBI_fetch_assoc($fields_rs)) {
73 $save_row[] = $row;
77 $fields_names = array();
78 $fields_types = array();
79 foreach ($save_row AS $saved_row_key => $row) {
80 $fields_names[] = $row['Field'];
81 // loic1: set or enum types: slashes single quotes inside options
82 if (preg_match('@^(set|enum)\((.+)\)$@i', $row['Type'], $tmp)) {
83 $tmp[2] = substr(preg_replace('@([^,])\'\'@', '\\1\\\'', ',' . $tmp[2]), 1);
84 $fields_types[] = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
85 } else {
86 $fields_types[] = $row['Type'];
88 } // end while
90 if ($fields_rs) {
91 PMA_DBI_free_result($fields_rs);
95 /**
96 * Do run the query to build the new index and moves back to
97 * "tbl_properties.php"
99 if (!defined('PMA_IDX_INCLUDED')
100 && (isset($index) && isset($do_save_data))) {
102 $err_url = 'tbl_indexes.php?' . PMA_generate_common_url($db, $table);
103 if (empty($old_index)) {
104 $err_url .= '&amp;create_index=1&amp;idx_num_fields=' . $idx_num_fields;
105 } else {
106 $err_url .= '&amp;index=' . urlencode($old_index);
109 if ($index_type == 'PRIMARY') {
110 if ($index == '') {
111 $index = 'PRIMARY';
112 } else if ($index != 'PRIMARY') {
113 PMA_mysqlDie($strPrimaryKeyName, '', FALSE, $err_url);
115 } else if ($index == 'PRIMARY') {
116 PMA_mysqlDie($strCantRenameIdxToPrimary, '', FALSE, $err_url);
120 // $sql_query is the one displayed in the query box
121 $sql_query = 'ALTER TABLE ' . PMA_backquote($table);
123 // Drops the old index
124 if (!empty($old_index)) {
125 if ($old_index == 'PRIMARY') {
126 $sql_query .= ' DROP PRIMARY KEY,';
127 } else {
128 $sql_query .= ' DROP INDEX ' . PMA_backquote($old_index) .',';
130 } // end if
132 // Builds the new one
133 switch ($index_type) {
134 case 'PRIMARY':
135 $sql_query .= ' ADD PRIMARY KEY (';
136 break;
137 case 'FULLTEXT':
138 $sql_query .= ' ADD FULLTEXT ' . (empty($index) ? '' : PMA_backquote($index)) . ' (';
139 break;
140 case 'UNIQUE':
141 $sql_query .= ' ADD UNIQUE ' . (empty($index) ? '' : PMA_backquote($index)) . ' (';
142 break;
143 case 'INDEX':
144 $sql_query .= ' ADD INDEX ' . (empty($index) ? '' : PMA_backquote($index)) . ' (';
145 break;
146 } // end switch
147 $index_fields = '';
148 foreach ($column AS $i => $name) {
149 if ($name != '--ignore--') {
150 $index_fields .= (empty($index_fields) ? '' : ',')
151 . PMA_backquote($name)
152 . (empty($sub_part[$i]) ? '' : '(' . $sub_part[$i] . ')');
154 } // end while
155 if (empty($index_fields)){
156 PMA_mysqlDie($strNoIndexPartsDefined, '', FALSE, $err_url);
157 } else {
158 $sql_query .= $index_fields . ')';
161 $result = PMA_DBI_query($sql_query);
162 $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered;
164 $active_page = 'tbl_properties_structure.php';
165 require('./tbl_properties_structure.php');
166 } // end builds the new index
170 * Edits an index or defines a new one
172 else if (!defined('PMA_IDX_INCLUDED')
173 && (isset($index) || isset($create_index))) {
175 // Prepares the form values
176 if (!isset($index)) {
177 $index = '';
179 if (!isset($old_index)){
180 $old_index = $index;
182 if (!isset($index_type)) {
183 $index_type = '';
185 if ($old_index == '' || !isset($indexes_info[$old_index])) {
186 $edited_index_info['Sequences'] = array();
187 $edited_index_data = array();
188 for ($i = 1; $i <= $idx_num_fields; $i++) {
189 $edited_index_info['Sequences'][] = $i;
190 $edited_index_data[$i] = array('Column_name' => '', 'Sub_part' => '');
191 } // end for
192 if ($old_index == ''
193 && !isset($indexes_info['PRIMARY'])
194 && ($index_type == '' || $index_type == 'PRIMARY')) {
195 $old_index = 'PRIMARY';
197 } else {
198 $edited_index_info = $indexes_info[$old_index];
199 $edited_index_data = $indexes_data[$old_index];
202 if ((PMA_MYSQL_INT_VERSION < 40002 && $edited_index_info['Comment'] == 'FULLTEXT')
203 || (PMA_MYSQL_INT_VERSION >= 40002 && $edited_index_info['Index_type'] == 'FULLTEXT')) {
204 $index_type = 'FULLTEXT';
205 } else if ($index == 'PRIMARY') {
206 $index_type = 'PRIMARY';
207 } else if ($edited_index_info['Non_unique'] == '0') {
208 $index_type = 'UNIQUE';
209 } else {
210 $index_type = 'INDEX';
212 } // end if... else...
214 if (isset($add_fields)) {
215 if (isset($prev_add_fields)) {
216 $added_fields += $prev_add_fields;
218 $field_cnt = count($edited_index_info['Sequences']) + 1;
219 for ($i = $field_cnt; $i < ($added_fields + $field_cnt); $i++) {
220 $edited_index_info['Sequences'][] = $i;
221 $edited_index_data[$i] = array('Column_name' => '', 'Sub_part' => '');
222 } // end for
224 // Restore entered values
225 foreach ($column AS $i => $name) {
226 if ($name != '--ignore--'){
227 $edited_index_data[$i+1]['Column_name'] = $name;
228 $edited_index_data[$i+1]['Sub_part'] = $sub_part[$i];
230 } // end while
231 } // end if
232 // end preparing form values
235 <!-- Build index form -->
236 <form action="./tbl_indexes.php" method="post" name="index_frm"
237 onsubmit="if (typeof(this.elements['index'].disabled) != 'undefined') {this.elements['index'].disabled = false}">
238 <table border="0" cellpadding="2" cellspacing="1">
239 <tr><td class="tblHeaders" colspan="2">
240 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
241 <?php
242 if (isset($create_index)) {
243 echo '<input type="hidden" name="create_index" value="1" />';
245 echo "\n";
247 <input type="hidden" name="old_index" value="<?php echo (isset($create_index) ? '' : htmlspecialchars($old_index)); ?>" />
248 <?php echo ' ' . (isset($create_index) ? $strCreateIndexTopic : $strModifyIndexTopic) . ' '; ?>
249 </th></tr>
252 <tr>
253 <td align="right"><b><?php echo $strIndexName; ?></b>&nbsp;</th>
254 <td>
255 <input type="text" name="index" value="<?php echo htmlspecialchars($index); ?>" size="25" onfocus="this.select()" />
256 </td>
257 </tr>
258 <tr><td align="right"><?php
259 if ($cfg['ErrorIconic']) {
260 echo '<img src="' . $pmaThemeImage . 's_warn.png" width="16" height="16" border="0" alt="Attention" />';
262 ?></td><td><?php echo $strPrimaryKeyWarning . "\n"; ?></td></tr>
263 <tr>
264 <td align="right"><b><?php echo $strIndexType; ?></b>&nbsp;</td>
265 <td>
266 <select name="index_type" onchange="return checkIndexName()">
267 <?php
268 echo "\n";
269 for ($i = 0; $i < $index_types_cnt; $i++) {
270 if ($index_types[$i] == 'PRIMARY') {
271 if ($index == 'PRIMARY' || !isset($indexes_info['PRIMARY'])) {
272 echo ' '
273 . '<option value="PRIMARY"' . (($index_type == 'PRIMARY') ? ' selected="selected"' : '') . '>PRIMARY</option>'
274 . "\n";
276 } else {
277 echo ' '
278 . '<option value="' . $index_types[$i] . '"' . (($index_type == $index_types[$i]) ? ' selected="selected"' : '') . '>'. $index_types[$i] . '</option>'
279 . "\n";
281 } // end if... else...
282 } // end for
284 </select>
285 <?php echo PMA_showMySQLDocu('SQL-Syntax', 'ALTER_TABLE') . "\n"; ?>
286 </td>
287 </tr>
289 <tr><td valign="top" align="right"><b><?php echo $strFields; ?> :</b>&nbsp;</td><td><table border="<?php echo $cfg['Border']; ?>" cellpadding="2" cellspacing="1">
290 <tr>
291 <th><?php echo $strField; ?></th>
292 <th><?php echo $strSize; ?></th>
293 </tr>
294 <?php
295 foreach ($edited_index_info['Sequences'] AS $row_no => $seq_index) {
296 $add_type = (is_array($fields_types) && count($fields_types) == count($fields_names));
297 $selected = $edited_index_data[$seq_index]['Column_name'];
298 if (!empty($edited_index_data[$seq_index]['Sub_part'])) {
299 $sub_part = ' value="' . $edited_index_data[$seq_index]['Sub_part'] . '"';
300 } else {
301 $sub_part = '';
303 $bgcolor = (($row_no % 2) ? $cfg['BgcolorOne'] : $cfg['BgcolorTwo']);
304 echo "\n";
306 <tr>
307 <td bgcolor="<?php echo $bgcolor; ?>">
308 <select name="column[]">
309 <option value="--ignore--"<?php if ('--ignore--' == $selected) echo ' selected="selected"'; ?>>
310 -- <?php echo $strIgnore; ?> --</option>
311 <?php
312 foreach ($fields_names AS $key => $val) {
313 if ($index_type != 'FULLTEXT'
314 || preg_match('@^(varchar|text|tinytext|mediumtext|longtext)@i', $fields_types[$key])) {
315 echo "\n" . ' '
316 . '<option value="' . htmlspecialchars($val) . '"' . (($val == $selected) ? ' selected="selected"' : '') . '>'
317 . htmlspecialchars($val) . (($add_type) ? ' [' . $fields_types[$key] . ']' : '' ) . '</option>' . "\n";
319 } // end while
320 echo "\n";
322 </select>
323 </td>
324 <td bgcolor="<?php echo $bgcolor; ?>">
325 <input type="text" size="5" name="sub_part[]"<?php echo $sub_part; ?> onfocus="this.select()" />
326 </td>
327 </tr>
328 <?php
329 } // end while
331 echo "\n";
333 <tr><td colspan="2"><?php
334 echo "\n";
335 if (isset($added_fields)) {
336 echo ' <input type="hidden" name="prev_add_fields" value="' . $added_fields . '" />';
338 if (isset($idx_num_fields)) {
339 echo ' <input type="hidden" name="idx_num_fields" value="' . $idx_num_fields . '" />' . "\n";
341 echo ' ' . "\n";
342 echo ' ' . sprintf($strAddToIndex, '<input type="text" name="added_fields" size="2" value="1" onfocus="this.select()" style="vertical-align: middle;" />') . "\n";
343 echo ' &nbsp;<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";
344 ?></td>
345 </tr>
346 </table></td></tr>
347 <tr><td colspan="2" class="tblFooters" align="center">
348 <input type="submit" name="do_save_data" value="<?php echo $strSave; ?>" /></td></tr>
349 </table>
350 </form>
351 <?php
352 } else {
354 * Display indexes
357 <!-- Indexes form -->
358 <form action="./tbl_indexes.php" method="post" onsubmit="return checkFormElementInRange(this, 'idx_num_fields', '<?php echo str_replace('\'', '\\\'', $GLOBALS['strInvalidColumnCount']); ?>', 1)">
359 <table border="0" cellpadding="2" cellspacing="1">
360 <tr><td class="tblHeaders" colspan="7">
361 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
362 <?php
363 echo "\n";
364 echo ' ' . $strIndexes . ':' . "\n";
365 echo ' ' . PMA_showMySQLDocu('optimization', 'optimizing-database-structure') . "\n";
366 ?></td></tr><?php
367 $edit_link_text = '';
368 $drop_link_text = '';
370 // We need to copy the value or else the == 'both' check will always return true
371 $propicon = (string)$cfg['PropertiesIconic'];
373 if ($cfg['PropertiesIconic'] === true || $propicon == 'both') {
374 $edit_link_text = '<img src="' . $pmaThemeImage . 'b_edit.png" width="16" height="16" hspace="2" border="0" title="' . $strEdit . '" alt="' . $strEdit . '" />';
375 $drop_link_text = '<img src="' . $pmaThemeImage . 'b_drop.png" width="16" height="16" hspace="2" border="0" title="' . $strDrop . '" alt="' . $strDrop . '" />';
377 if ($cfg['PropertiesIconic'] === false || $propicon == 'both') {
378 $edit_link_text .= $strEdit;
379 $drop_link_text .= $strDrop;
381 if ($propicon == 'both') {
382 $edit_link_text = '<nobr>' . $edit_link_text . '</nobr>';
383 $drop_link_text = '<nobr>' . $drop_link_text . '</nobr>';
386 if (count($ret_keys) > 0) {
388 <!--table border="<?php echo $cfg['Border']; ?>" cellpadding="2" cellspacing="1"-->
389 <tr>
390 <th><?php echo $strKeyname; ?></th>
391 <th><?php echo $strType; ?></th>
392 <th><?php echo $strCardinality; ?></th>
393 <th colspan="2"><?php echo $strAction; ?></th>
394 <th colspan="2"><?php echo $strField; ?></th>
395 </tr>
396 <?php
397 $idx_collection = PMA_show_indexes($table, $indexes, $indexes_info, $indexes_data, true);
398 echo PMA_check_indexes($idx_collection);
399 } // end display indexes
400 else {
401 // none indexes
402 echo "\n" . ' <tr><td colspan=7" align="center">' . "\n";
403 if ($cfg['ErrorIconic']) {
404 echo '<img src="' . $pmaThemeImage . 's_warn.png" width="16" height="16" border="0" alt="Warning" hspace="2" align="middle" />';
406 echo ' <b>' . $strNoIndex . '</b></td></tr>' . "\n\n";
409 echo '<tr><td colspan="7" class="tblFooters" nowrap="nowrap" align="center"> '
410 . sprintf($strCreateIndex, '<input type="text" size="2" name="idx_num_fields" value="1" style="vertical-align: middle;" />') . "\n";
411 echo ' &nbsp;<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";
412 echo '</td></tr> ';
414 </table></form>
415 <?php
416 } // end display indexes
420 * Displays the footer
422 echo "\n";
424 if (!defined('PMA_IDX_INCLUDED')){
425 require_once('./footer.inc.php');