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_properties.php"
22 if (!defined('PMA_IDX_INCLUDED')) {
23 // Not a valid db name -> back to the welcome page
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');
31 // Not a valid table name -> back to the default db_details sub-page
33 $is_table = PMA_DBI_query('SHOW TABLES LIKE \'' . PMA_sqlAddslashes($table, TRUE) . '\'', NULL, PMA_DBI_QUERY_STORE
);
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');
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');
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
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) . ';');
72 while ($row = PMA_DBI_fetch_assoc($fields_rs)) {
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]) . ')';
86 $fields_types[] = $row['Type'];
91 PMA_DBI_free_result($fields_rs);
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 .= '&create_index=1&idx_num_fields=' . $idx_num_fields;
106 $err_url .= '&index=' . urlencode($old_index);
109 if ($index_type == '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,';
128 $sql_query .= ' DROP INDEX ' . PMA_backquote($old_index) .',';
132 // Builds the new one
133 switch ($index_type) {
135 $sql_query .= ' ADD PRIMARY KEY (';
138 $sql_query .= ' ADD FULLTEXT ' . (empty($index) ?
'' : PMA_backquote($index)) . ' (';
141 $sql_query .= ' ADD UNIQUE ' . (empty($index) ?
'' : PMA_backquote($index)) . ' (';
144 $sql_query .= ' ADD INDEX ' . (empty($index) ?
'' : PMA_backquote($index)) . ' (';
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] . ')');
155 if (empty($index_fields)){
156 PMA_mysqlDie($strNoIndexPartsDefined, '', FALSE, $err_url);
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)) {
179 if (!isset($old_index)){
182 if (!isset($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' => '');
193 && !isset($indexes_info['PRIMARY'])
194 && ($index_type == '' ||
$index_type == 'PRIMARY')) {
195 $old_index = 'PRIMARY';
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';
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' => '');
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];
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); ?
>
242 if (isset($create_index)) {
243 echo '<input type="hidden" name="create_index" value="1" />';
247 <input type
="hidden" name
="old_index" value
="<?php echo (isset($create_index) ? '' : htmlspecialchars($old_index)); ?>" />
248 <?php
echo ' ' . (isset($create_index) ?
$strCreateIndexTopic : $strModifyIndexTopic) . ' '; ?
>
253 <td align
="right"><b
><?php
echo $strIndexName; ?
></b
> 
;</th
>
255 <input type
="text" name
="index" value
="<?php echo htmlspecialchars($index); ?>" size
="25" onfocus
="this.select()" />
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
>
264 <td align
="right"><b
><?php
echo $strIndexType; ?
></b
> 
;</td
>
266 <select name
="index_type" onchange
="return checkIndexName()">
269 for ($i = 0; $i < $index_types_cnt; $i++
) {
270 if ($index_types[$i] == 'PRIMARY') {
271 if ($index == 'PRIMARY' ||
!isset($indexes_info['PRIMARY'])) {
273 . '<option value="PRIMARY"' . (($index_type == 'PRIMARY') ?
' selected="selected"' : '') . '>PRIMARY</option>'
278 . '<option value="' . $index_types[$i] . '"' . (($index_type == $index_types[$i]) ?
' selected="selected"' : '') . '>'. $index_types[$i] . '</option>'
281 } // end if... else...
285 <?php
echo PMA_showMySQLDocu('SQL-Syntax', 'ALTER_TABLE') . "\n"; ?
>
289 <tr
><td valign
="top" align
="right"><b
><?php
echo $strFields; ?
> :</b
> 
;</td
><td
><table border
="<?php echo $cfg['Border']; ?>" cellpadding
="2" cellspacing
="1">
291 <th
><?php
echo $strField; ?
></th
>
292 <th
><?php
echo $strSize; ?
></th
>
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'] . '"';
303 $bgcolor = (($row_no %
2) ?
$cfg['BgcolorOne'] : $cfg['BgcolorTwo']);
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
>
312 foreach ($fields_names AS $key => $val) {
313 if ($index_type != 'FULLTEXT'
314 ||
preg_match('@^(varchar|text|tinytext|mediumtext|longtext)@i', $fields_types[$key])) {
316 . '<option value="' . htmlspecialchars($val) . '"' . (($val == $selected) ?
' selected="selected"' : '') . '>'
317 . htmlspecialchars($val) . (($add_type) ?
' [' . $fields_types[$key] . ']' : '' ) . '</option>' . "\n";
324 <td bgcolor
="<?php echo $bgcolor; ?>">
325 <input type
="text" size
="5" name
="sub_part[]"<?php
echo $sub_part; ?
> onfocus
="this.select()" />
333 <tr
><td colspan
="2"><?php
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";
342 echo ' ' . sprintf($strAddToIndex, '<input type="text" name="added_fields" size="2" value="1" onfocus="this.select()" style="vertical-align: middle;" />') . "\n";
343 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";
347 <tr
><td colspan
="2" class="tblFooters" align
="center">
348 <input type
="submit" name
="do_save_data" value
="<?php echo $strSave; ?>" /></td
></tr
>
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); ?
>
364 echo ' ' . $strIndexes . ':' . "\n";
365 echo ' ' . PMA_showMySQLDocu('optimization', 'optimizing-database-structure') . "\n";
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"-->
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
>
397 $idx_collection = PMA_show_indexes($table, $indexes, $indexes_info, $indexes_data, true);
398 echo PMA_check_indexes($idx_collection);
399 } // end display 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 ' <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";
416 } // end display indexes
420 * Displays the footer
424 if (!defined('PMA_IDX_INCLUDED')){
425 require_once('./footer.inc.php');