2
/* $Id: tbl_indexes.php 9602 2006-10-25 12:25:01Z nijel $ */
3
// vim: expandtab sw=4 ts=4 sts=4:
6
* Gets some core libraries
8
require_once('./libraries/common.lib.php');
9
require_once('./libraries/tbl_indexes.lib.php');
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);
18
* Ensures the db & table are valid, then loads headers and gets indexes
20
* Skipped if this script is called by "tbl_sql.php"
22
if (!defined('PMA_IDX_INCLUDED')) {
23
// Not a valid db name -> back to the welcome page
24
if ( isset($db) && strlen($db) ) {
25
$is_db = PMA_DBI_select_db($db);
27
if ( !isset($db) || !strlen($db) || !$is_db ) {
28
$uri_params = array( 'reload' => '1' );
29
if ( isset($message) ) {
30
$uri_params['message'] = $message;
32
PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . 'main.php'
33
. PMA_generate_common_url($uri_params, '&'));
36
// Not a valid table name -> back to the default db sub-page
37
if ( isset($table) && strlen($table) ) {
38
$is_table = PMA_DBI_query('SHOW TABLES LIKE \''
39
. PMA_sqlAddslashes($table, TRUE) . '\'', null, PMA_DBI_QUERY_STORE);
41
if ( ! isset($table) || ! strlen($table)
42
|| !( $is_table && PMA_DBI_num_rows($is_table) ) ) {
43
$uri_params = array( 'reload' => '1', 'db' => $db );
44
if ( isset($message) ) {
45
$uri_params['message'] = $message;
47
PMA_sendHeaderLocation($cfg['PmaAbsoluteUri']
48
. $cfg['DefaultTabDatabase']
49
. PMA_generate_common_url($uri_params, '&'));
51
} elseif ( isset($is_table) ) {
52
PMA_DBI_free_result($is_table);
55
// Displays headers (if needed)
56
$js_to_run = isset($index) && isset($do_save_data)
59
require_once('./libraries/header.inc.php');
64
* Gets fields and indexes informations
66
if (!defined('PMA_IDX_INCLUDED')) {
67
$err_url_0 = 'db_sql.php?' . PMA_generate_common_url($db);
70
// Gets table keys and store them in arrays
72
$indexes_info = array();
73
$indexes_data = array();
74
// keys had already been grabbed in "tbl_sql.php"
75
if (!defined('PMA_IDX_INCLUDED')) {
76
$ret_keys = PMA_get_indexes($table, $err_url_0);
79
PMA_extract_indexes($ret_keys, $indexes, $indexes_info, $indexes_data);
81
// Get fields and stores their name/type
82
// fields had already been grabbed in "tbl_sql.php"
83
if (!defined('PMA_IDX_INCLUDED')) {
84
$fields_rs = PMA_DBI_query('SHOW FIELDS FROM '
85
. PMA_backquote($table) . ';');
87
while ($row = PMA_DBI_fetch_assoc($fields_rs)) {
92
$fields_names = array();
93
$fields_types = array();
94
foreach ($save_row AS $saved_row_key => $row) {
95
$fields_names[] = $row['Field'];
96
// loic1: set or enum types: slashes single quotes inside options
97
if (preg_match('@^(set|enum)\((.+)\)$@i', $row['Type'], $tmp)) {
98
$tmp[2] = substr(preg_replace('@([^,])\'\'@', '\\1\\\'',
100
$fields_types[] = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
102
$fields_types[] = $row['Type'];
107
PMA_DBI_free_result($fields_rs);
112
* Do run the query to build the new index and moves back to
115
if (!defined('PMA_IDX_INCLUDED')
116
&& (isset($index) && isset($do_save_data))) {
118
$err_url = 'tbl_indexes.php?' . PMA_generate_common_url($db, $table);
119
if (empty($old_index)) {
120
$err_url .= '&create_index=1&idx_num_fields=' . $idx_num_fields;
122
$err_url .= '&index=' . urlencode($old_index);
125
if ($index_type == 'PRIMARY') {
128
} elseif ($index != 'PRIMARY') {
129
PMA_mysqlDie($strPrimaryKeyName, '', FALSE, $err_url);
131
} elseif ($index == 'PRIMARY') {
132
PMA_mysqlDie($strCantRenameIdxToPrimary, '', FALSE, $err_url);
136
// $sql_query is the one displayed in the query box
137
$sql_query = 'ALTER TABLE ' . PMA_backquote($table);
139
// Drops the old index
140
if (!empty($old_index)) {
141
if ($old_index == 'PRIMARY') {
142
$sql_query .= ' DROP PRIMARY KEY,';
144
$sql_query .= ' DROP INDEX ' . PMA_backquote($old_index) .',';
148
// Builds the new one
149
switch ($index_type) {
151
$sql_query .= ' ADD PRIMARY KEY (';
154
$sql_query .= ' ADD FULLTEXT '
155
. (empty($index) ? '' : PMA_backquote($index)) . ' (';
158
$sql_query .= ' ADD UNIQUE '
159
. (empty($index) ? '' : PMA_backquote($index)) . ' (';
162
$sql_query .= ' ADD INDEX '
163
. (empty($index) ? '' : PMA_backquote($index)) . ' (';
167
foreach ($column AS $i => $name) {
168
if ($name != '--ignore--') {
169
$index_fields .= (empty($index_fields) ? '' : ',')
170
. PMA_backquote($name)
171
. (empty($sub_part[$i])
173
: '(' . $sub_part[$i] . ')');
176
if (empty($index_fields)){
177
PMA_mysqlDie($strNoIndexPartsDefined, '', FALSE, $err_url);
179
$sql_query .= $index_fields . ')';
182
$result = PMA_DBI_query($sql_query);
183
$message = $strTable . ' ' . htmlspecialchars($table) . ' '
184
. $strHasBeenAltered;
186
$active_page = 'tbl_structure.php';
187
require('./tbl_structure.php');
188
} // end builds the new index
192
* Edits an index or defines a new one
194
elseif (!defined('PMA_IDX_INCLUDED')
195
&& (isset($index) || isset($create_index))) {
197
// Prepares the form values
198
if (!isset($index)) {
201
if (!isset($old_index)){
204
if (!isset($index_type)) {
207
if ($old_index == '' || !isset($indexes_info[$old_index])) {
208
$edited_index_info['Sequences'] = array();
209
$edited_index_data = array();
210
for ($i = 1; $i <= $idx_num_fields; $i++) {
211
$edited_index_info['Sequences'][] = $i;
212
$edited_index_data[$i] = array('Column_name' => '',
216
&& !isset($indexes_info['PRIMARY'])
217
&& ($index_type == '' || $index_type == 'PRIMARY')) {
218
$old_index = 'PRIMARY';
221
$edited_index_info = $indexes_info[$old_index];
222
$edited_index_data = $indexes_data[$old_index];
225
if ((PMA_MYSQL_INT_VERSION < 40002
226
&& $edited_index_info['Comment'] == 'FULLTEXT')
227
|| (PMA_MYSQL_INT_VERSION >= 40002
228
&& $edited_index_info['Index_type'] == 'FULLTEXT')) {
229
$index_type = 'FULLTEXT';
230
} elseif ($index == 'PRIMARY') {
231
$index_type = 'PRIMARY';
232
} elseif ($edited_index_info['Non_unique'] == '0') {
233
$index_type = 'UNIQUE';
235
$index_type = 'INDEX';
237
} // end if... else...
239
if (isset($add_fields)) {
240
if (isset($prev_add_fields)) {
241
$added_fields += $prev_add_fields;
243
$field_cnt = count($edited_index_info['Sequences']) + 1;
244
for ($i = $field_cnt; $i < ($added_fields + $field_cnt); $i++) {
245
$edited_index_info['Sequences'][] = $i;
246
$edited_index_data[$i] = array('Column_name' => '',
250
// Restore entered values
251
foreach ($column AS $i => $name) {
252
if ($name != '--ignore--'){
253
$edited_index_data[$i+1]['Column_name'] = $name;
254
$edited_index_data[$i+1]['Sub_part'] = $sub_part[$i];
258
// end preparing form values
261
<div style="float: left;">
262
<form action="./tbl_indexes.php" method="post" name="index_frm"
263
onsubmit="if (typeof(this.elements['index'].disabled) != 'undefined') {
264
this.elements['index'].disabled = false}">
265
<?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
267
if (isset($create_index)) {
268
echo '<input type="hidden" name="create_index" value="1" />' . "\n";
270
if (isset($added_fields)) {
271
echo ' <input type="hidden" name="prev_add_fields" value="'
272
. $added_fields . '" />' . "\n";
274
if (isset($idx_num_fields)) {
275
echo ' <input type="hidden" name="idx_num_fields" value="'
276
. $idx_num_fields . '" />' . "\n";
279
<input type="hidden" name="old_index" value="<?php
280
echo (isset($create_index) ? '' : htmlspecialchars($old_index)); ?>" />
285
echo (isset($create_index) ? $strCreateIndexTopic : $strModifyIndexTopic);
289
<div class="formelement">
290
<label for="input_index_name"><?php echo $strIndexName; ?></label>
291
<input type="text" name="index" id="input_index_name" size="25"
292
value="<?php echo htmlspecialchars($index); ?>" onfocus="this.select()" />
295
<div class="formelement">
296
<label for="select_index_type"><?php echo $strIndexType; ?></label>
297
<select name="index_type" id="select_index_type" onchange="return checkIndexName()">
299
for ($i = 0; $i < $index_types_cnt; $i++) {
300
if ($index_types[$i] == 'PRIMARY') {
301
if ($index == 'PRIMARY' || !isset($indexes_info['PRIMARY'])) {
303
. '<option value="PRIMARY"'
304
. (($index_type == 'PRIMARY') ? ' selected="selected"' : '')
305
. '>PRIMARY</option>' . "\n";
309
. '<option value="' . $index_types[$i] . '"'
310
. (($index_type == $index_types[$i]) ? ' selected="selected"' : '')
311
. '>'. $index_types[$i] . '</option>' . "\n";
313
} // end if... else...
317
<?php echo PMA_showMySQLDocu('SQL-Syntax', 'ALTER_TABLE'); ?>
321
<br class="clearfloat" />
322
<div class="warning"><?php echo $strPrimaryKeyWarning; ?></div>
326
<tr><th><?php echo $strField; ?></th>
327
<th><?php echo $strSize; ?></th>
333
foreach ($edited_index_info['Sequences'] as $seq_index) {
334
$add_type = (is_array($fields_types) && count($fields_types) == count($fields_names));
335
$selected = $edited_index_data[$seq_index]['Column_name'];
336
if (!empty($edited_index_data[$seq_index]['Sub_part'])) {
337
$sub_part = ' value="' . $edited_index_data[$seq_index]['Sub_part'] . '"';
343
<tr class="<?php echo $odd_row ? 'odd' : 'even'; ?>">
344
<td><select name="column[]">
345
<option value="--ignore--"
346
<?php if ('--ignore--' == $selected) { echo ' selected="selected"'; } ?>>
347
-- <?php echo $strIgnore; ?> --</option>
349
foreach ($fields_names AS $key => $val) {
350
if ($index_type != 'FULLTEXT'
351
|| preg_match('@^(varchar|text|tinytext|mediumtext|longtext)@i', $fields_types[$key])) {
353
. '<option value="' . htmlspecialchars($val) . '"'
354
. (($val == $selected) ? ' selected="selected"' : '') . '>'
355
. htmlspecialchars($val) . (($add_type) ? ' ['
356
. $fields_types[$key] . ']' : '' ) . '</option>' . "\n";
358
} // end foreach $fields_names
363
<td><input type="text" size="5" onfocus="this.select()"
364
name="sub_part[]"<?php echo $sub_part; ?> />
368
$odd_row = !$odd_row;
369
} // end foreach $edited_index_info['Sequences']
375
<fieldset class="tblFooters">
376
<input type="submit" name="do_save_data" value="<?php echo $strSave; ?>" />
379
echo ' ' . sprintf($strAddToIndex,
380
'<input type="text" name="added_fields" size="2" value="1"'
381
.' onfocus="this.select()" />') . "\n";
382
echo ' <input type="submit" name="add_fields" value="' . $strGo . '"'
383
.' onclick="return checkFormElementInRange(this.form,'
384
.' \'added_fields\', \''
385
. str_replace('\'', '\\\'', $GLOBALS['strInvalidColumnCount'])
386
. '\', 1)" />' . "\n";
397
<form action="./tbl_indexes.php" method="post"
398
onsubmit="return checkFormElementInRange(this, 'idx_num_fields',
399
'<?php echo str_replace('\'', '\\\'', $GLOBALS['strInvalidColumnCount']); ?>',
402
echo PMA_generate_common_hidden_inputs( $db, $table );
404
<table id="table_indexes" class="data">
405
<caption class="tblHeaders">
407
echo $strIndexes . ':' . "\n";
408
echo ' ' . PMA_showMySQLDocu('optimization',
409
'optimizing-database-structure');
415
if ( count($ret_keys) > 0) {
416
$edit_link_text = '';
417
$drop_link_text = '';
419
// We need to copy the value or else the == 'both' check will always
421
$propicon = (string) $cfg['PropertiesIconic'];
423
if ($cfg['PropertiesIconic'] === true || $propicon == 'both') {
424
$edit_link_text = '<img class="icon" src="' . $pmaThemeImage
425
. 'b_edit.png" width="16" height="16" title="' . $strEdit
426
. '" alt="' . $strEdit . '" />';
427
$drop_link_text = '<img class="icon" src="' . $pmaThemeImage
428
. 'b_drop.png" width="16" height="16" title="' . $strDrop
429
. '" alt="' . $strDrop . '" />';
431
if ($cfg['PropertiesIconic'] === false || $propicon == 'both') {
432
$edit_link_text .= $strEdit;
433
$drop_link_text .= $strDrop;
435
if ($propicon == 'both') {
436
$edit_link_text = '<nobr>' . $edit_link_text . '</nobr>';
437
$drop_link_text = '<nobr>' . $drop_link_text . '</nobr>';
442
<tr><th><?php echo $strKeyname; ?></th>
443
<th><?php echo $strType; ?></th>
444
<th><?php echo $strCardinality; ?></th>
445
<th colspan="2"><?php echo $strAction; ?></th>
446
<th colspan="2"><?php echo $strField; ?></th>
451
$idx_collection = PMA_show_indexes($table, $indexes, $indexes_info,
452
$indexes_data, true);
453
echo PMA_check_indexes($idx_collection);
454
} // end display indexes
458
.'<tr><td colspan="7"><div class="warning">' . $strNoIndex
459
.'</div></td></tr>' . "\n";
463
<tr class="tblFooters"><td colspan="7">
464
<?php echo sprintf($strCreateIndex,
465
'<input type="text" size="2" name="idx_num_fields" value="1" />'); ?>
466
<input type="submit" name="create_index" value="<?php echo $strGo; ?>"
467
onclick="return checkFormElementInRange(this.form,
469
'<?php echo str_replace('\'', '\\\'', $GLOBALS['strInvalidColumnCount']); ?>',
476
} // end display indexes
480
* Displays the footer
484
if (!defined('PMA_IDX_INCLUDED')){
485
require_once('./libraries/footer.inc.php');