2 /* vim: expandtab sw=4 ts=4 sts=4: */
4 * display information about indexes in a table
10 * Gets some core libraries
12 require_once './libraries/common.inc.php';
13 require_once './libraries/tbl_indexes.lib.php';
16 * Ensures the db & table are valid, then loads headers and gets indexes
18 * Skipped if this script is called by "tbl_sql.php"
20 if (!defined('PMA_IDX_INCLUDED')) {
21 // Not a valid db name -> back to the welcome page
23 $is_db = PMA_DBI_select_db($db);
25 if (!strlen($db) ||
!$is_db) {
26 $uri_params = array('reload' => '1');
27 if (isset($message)) {
28 $uri_params['message'] = $message;
30 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . 'main.php'
31 . PMA_generate_common_url($uri_params, '&'));
34 // Not a valid table name -> back to the default db sub-page
36 $is_table = PMA_DBI_query('SHOW TABLES LIKE \''
37 . PMA_sqlAddslashes($table, TRUE) . '\'', null, PMA_DBI_QUERY_STORE
);
40 ||
!($is_table && PMA_DBI_num_rows($is_table))) {
41 $uri_params = array('reload' => '1', 'db' => $db);
42 if (isset($message)) {
43 $uri_params['message'] = $message;
45 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri']
46 . $cfg['DefaultTabDatabase']
47 . PMA_generate_common_url($uri_params, '&'));
49 } elseif (isset($is_table)) {
50 PMA_DBI_free_result($is_table);
53 // Displays headers (if needed)
54 $GLOBALS['js_include'][] = 'functions.js';
55 $GLOBALS['js_include'][] = 'indexes.js';
56 require_once './libraries/header.inc.php';
61 * Gets fields and indexes informations
63 if (!defined('PMA_IDX_INCLUDED')) {
64 $err_url_0 = 'db_sql.php?' . PMA_generate_common_url($db);
67 // Gets table keys and store them in arrays
69 $indexes_info = array();
70 $indexes_data = array();
71 // keys had already been grabbed in "tbl_sql.php"
72 if (!defined('PMA_IDX_INCLUDED')) {
73 $ret_keys = PMA_get_indexes($table, $err_url_0);
76 PMA_extract_indexes($ret_keys, $indexes, $indexes_info, $indexes_data);
78 // Get fields and stores their name/type
79 // fields had already been grabbed in "tbl_sql.php"
80 if (!defined('PMA_IDX_INCLUDED')) {
81 $fields_rs = PMA_DBI_query('SHOW FIELDS FROM '
82 . PMA_backquote($table) . ';');
84 while ($row = PMA_DBI_fetch_assoc($fields_rs)) {
89 $fields_names = array();
90 $fields_types = array();
91 foreach ($save_row AS $saved_row_key => $row) {
92 $fields_names[] = $row['Field'];
93 // loic1: set or enum types: slashes single quotes inside options
94 if (preg_match('@^(set|enum)\((.+)\)$@i', $row['Type'], $tmp)) {
95 $tmp[2] = substr(preg_replace('@([^,])\'\'@', '\\1\\\'',
97 $fields_types[] = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
99 $fields_types[] = $row['Type'];
104 PMA_DBI_free_result($fields_rs);
109 * Do run the query to build the new index and moves back to
112 if (!defined('PMA_IDX_INCLUDED')
113 && (isset($index) && isset($do_save_data))) {
115 $err_url = 'tbl_indexes.php?' . PMA_generate_common_url($db, $table);
116 if (empty($old_index)) {
117 $err_url .= '&create_index=1&idx_num_fields=' . $idx_num_fields;
119 $err_url .= '&index=' . urlencode($old_index);
122 if ($index_type == 'PRIMARY') {
125 } elseif ($index != 'PRIMARY') {
126 PMA_mysqlDie($strPrimaryKeyName, '', FALSE, $err_url);
128 } elseif ($index == 'PRIMARY') {
129 PMA_mysqlDie($strCantRenameIdxToPrimary, '', FALSE, $err_url);
133 // $sql_query is the one displayed in the query box
134 $sql_query = 'ALTER TABLE ' . PMA_backquote($table);
136 // Drops the old index
137 if (!empty($old_index)) {
138 if ($old_index == 'PRIMARY') {
139 $sql_query .= ' DROP PRIMARY KEY,';
141 $sql_query .= ' DROP INDEX ' . PMA_backquote($old_index) .',';
145 // Builds the new one
146 switch ($index_type) {
148 $sql_query .= ' ADD PRIMARY KEY (';
151 $sql_query .= ' ADD FULLTEXT '
152 . (empty($index) ?
'' : PMA_backquote($index)) . ' (';
155 $sql_query .= ' ADD UNIQUE '
156 . (empty($index) ?
'' : PMA_backquote($index)) . ' (';
159 $sql_query .= ' ADD INDEX '
160 . (empty($index) ?
'' : PMA_backquote($index)) . ' (';
164 foreach ($column AS $i => $name) {
165 if ($name != '--ignore--') {
166 $index_fields .= (empty($index_fields) ?
'' : ',')
167 . PMA_backquote($name)
168 . (empty($sub_part[$i])
170 : '(' . $sub_part[$i] . ')');
173 if (empty($index_fields)){
174 PMA_mysqlDie($strNoIndexPartsDefined, '', FALSE, $err_url);
176 $sql_query .= $index_fields . ')';
179 $result = PMA_DBI_query($sql_query);
180 $message = PMA_Message
::success('strTableAlteredSuccessfully');
181 $message->addParam($table);
183 $active_page = 'tbl_structure.php';
184 require './tbl_structure.php';
185 } // end builds the new index
189 * Edits an index or defines a new one
191 elseif (!defined('PMA_IDX_INCLUDED')
192 && (isset($index) ||
isset($create_index))) {
194 // Prepares the form values
195 if (!isset($index)) {
198 if (!isset($old_index)){
201 if (!isset($index_type)) {
204 if ($old_index == '' ||
!isset($indexes_info[$old_index])) {
205 $edited_index_info['Sequences'] = array();
206 $edited_index_data = array();
207 for ($i = 1; $i <= $idx_num_fields; $i++
) {
208 $edited_index_info['Sequences'][] = $i;
209 $edited_index_data[$i] = array('Column_name' => '',
213 && !isset($indexes_info['PRIMARY'])
214 && ($index_type == '' ||
$index_type == 'PRIMARY')) {
215 $old_index = 'PRIMARY';
218 $edited_index_info = $indexes_info[$old_index];
219 $edited_index_data = $indexes_data[$old_index];
222 if ($edited_index_info['Index_type'] == 'FULLTEXT') {
223 $index_type = 'FULLTEXT';
224 } elseif ($index == 'PRIMARY') {
225 $index_type = 'PRIMARY';
226 } elseif ($edited_index_info['Non_unique'] == '0') {
227 $index_type = 'UNIQUE';
229 $index_type = 'INDEX';
231 } // end if... else...
233 if (isset($add_fields)) {
234 if (isset($prev_add_fields)) {
235 $added_fields +
= $prev_add_fields;
237 $field_cnt = count($edited_index_info['Sequences']) +
1;
238 for ($i = $field_cnt; $i < ($added_fields +
$field_cnt); $i++
) {
239 $edited_index_info['Sequences'][] = $i;
240 $edited_index_data[$i] = array('Column_name' => '',
244 // Restore entered values
245 foreach ($column AS $i => $name) {
246 if ($name != '--ignore--'){
247 $edited_index_data[$i+
1]['Column_name'] = $name;
248 $edited_index_data[$i+
1]['Sub_part'] = $sub_part[$i];
252 // end preparing form values
255 <div style
="float: left;">
256 <form action
="./tbl_indexes.php" method
="post" name
="index_frm"
257 onsubmit
="if (typeof(this.elements['index'].disabled) != 'undefined') {
258 this.elements['index'].disabled = false}">
259 <?php
echo PMA_generate_common_hidden_inputs($db, $table); ?
>
261 if (isset($create_index)) {
262 echo '<input type="hidden" name="create_index" value="1" />' . "\n";
264 if (isset($added_fields)) {
265 echo ' <input type="hidden" name="prev_add_fields" value="'
266 . $added_fields . '" />' . "\n";
268 if (isset($idx_num_fields)) {
269 echo ' <input type="hidden" name="idx_num_fields" value="'
270 . $idx_num_fields . '" />' . "\n";
273 <input type
="hidden" name
="old_index" value
="<?php
274 echo (isset($create_index) ? '' : htmlspecialchars($old_index)); ?>" />
279 echo (isset($create_index) ?
$strCreateIndexTopic : $strModifyIndexTopic);
283 <div
class="formelement">
284 <label
for="input_index_name"><?php
echo $strIndexName; ?
></label
>
285 <input type
="text" name
="index" id
="input_index_name" size
="25"
286 value
="<?php echo htmlspecialchars($index); ?>" onfocus
="this.select()" />
289 <div
class="formelement">
290 <label
for="select_index_type"><?php
echo $strIndexType; ?
></label
>
291 <select name
="index_type" id
="select_index_type" onchange
="return checkIndexName()">
293 foreach (PMA_get_indextypes() as $each_index_type) {
294 if ($each_index_type === 'PRIMARY'
295 && $index !== 'PRIMARY'
296 && isset($indexes_info['PRIMARY'])) {
297 // skip PRIMARY if there is already one in the table
301 . '<option value="' . $each_index_type . '"'
302 . (($index_type == $each_index_type) ?
' selected="selected"' : '')
303 . '>'. $each_index_type . '</option>' . "\n";
307 <?php
echo PMA_showMySQLDocu('SQL-Syntax', 'ALTER_TABLE'); ?
>
311 <br
class="clearfloat" />
313 PMA_Message
::warning('strPrimaryKeyWarning')->display();
318 <tr
><th
><?php
echo $strField; ?
></th
>
319 <th
><?php
echo $strSize; ?
></th
>
325 foreach ($edited_index_info['Sequences'] as $seq_index) {
326 $add_type = (is_array($fields_types) && count($fields_types) == count($fields_names));
327 $selected = $edited_index_data[$seq_index]['Column_name'];
328 if (!empty($edited_index_data[$seq_index]['Sub_part'])) {
329 $sub_part = ' value="' . $edited_index_data[$seq_index]['Sub_part'] . '"';
335 <tr
class="<?php echo $odd_row ? 'odd' : 'even'; ?>">
336 <td
><select name
="column[]">
337 <option value
="--ignore--"
338 <?php
if ('--ignore--' == $selected) { echo ' selected="selected"'; } ?
>>
339 -- <?php
echo $strIgnore; ?
> --</option
>
341 foreach ($fields_names AS $key => $val) {
342 if ($index_type != 'FULLTEXT'
343 ||
preg_match('@^(varchar|text|tinytext|mediumtext|longtext)@i', $fields_types[$key])) {
345 . '<option value="' . htmlspecialchars($val) . '"'
346 . (($val == $selected) ?
' selected="selected"' : '') . '>'
347 . htmlspecialchars($val) . (($add_type) ?
' ['
348 . $fields_types[$key] . ']' : '') . '</option>' . "\n";
350 } // end foreach $fields_names
355 <td
><input type
="text" size
="5" onfocus
="this.select()"
356 name
="sub_part[]"<?php
echo $sub_part; ?
> />
360 $odd_row = !$odd_row;
361 } // end foreach $edited_index_info['Sequences']
367 <fieldset
class="tblFooters">
368 <input type
="submit" name
="do_save_data" value
="<?php echo $strSave; ?>" />
371 echo ' ' . sprintf($strAddToIndex,
372 '<input type="text" name="added_fields" size="2" value="1"'
373 .' onfocus="this.select()" />') . "\n";
374 echo ' <input type="submit" name="add_fields" value="' . $strGo . '"'
375 .' onclick="return checkFormElementInRange(this.form,'
376 .' \'added_fields\', \''
377 . str_replace('\'', '\\\'', $GLOBALS['strInvalidColumnCount'])
378 . '\', 1)" />' . "\n";
389 <form action
="./tbl_indexes.php" method
="post"
390 onsubmit
="return checkFormElementInRange(this, 'idx_num_fields',
391 '<?php echo str_replace('\'', '\\\'', $GLOBALS['strInvalidColumnCount']); ?>',
394 echo PMA_generate_common_hidden_inputs($db, $table);
396 <table id
="table_indexes" class="data">
397 <caption
class="tblHeaders">
399 echo $strIndexes . ':' . "\n";
400 echo ' ' . PMA_showMySQLDocu('optimization',
401 'optimizing-database-structure');
407 if (count($ret_keys) > 0) {
408 $edit_link_text = '';
409 $drop_link_text = '';
411 if ($cfg['PropertiesIconic'] === true ||
$cfg['PropertiesIconic'] === 'both') {
412 $edit_link_text = '<img class="icon" src="' . $pmaThemeImage
413 . 'b_edit.png" width="16" height="16" title="' . $strEdit
414 . '" alt="' . $strEdit . '" />';
415 $drop_link_text = '<img class="icon" src="' . $pmaThemeImage
416 . 'b_drop.png" width="16" height="16" title="' . $strDrop
417 . '" alt="' . $strDrop . '" />';
419 if ($cfg['PropertiesIconic'] === false ||
$cfg['PropertiesIconic'] === 'both') {
420 $edit_link_text .= $strEdit;
421 $drop_link_text .= $strDrop;
423 if ($cfg['PropertiesIconic'] === 'both') {
424 $edit_link_text = '<nobr>' . $edit_link_text . '</nobr>';
425 $drop_link_text = '<nobr>' . $drop_link_text . '</nobr>';
430 <tr
><th
><?php
echo $strKeyname; ?
></th
>
431 <th
><?php
echo $strType; ?
></th
>
432 <th
><?php
echo $strCardinality; ?
></th
>
433 <th colspan
="2"><?php
echo $strAction; ?
></th
>
434 <th colspan
="2"><?php
echo $strField; ?
></th
>
439 $idx_collection = PMA_show_indexes($table, $indexes, $indexes_info,
440 $indexes_data, true);
441 echo PMA_check_indexes($ret_keys);
442 } // end display indexes
446 .'<tr><td colspan="7">';
447 PMA_Message
::warning('strNoIndex')->display();
448 echo '</td></tr>' . "\n";
452 <tr
class="tblFooters"><td colspan
="7">
453 <?php
echo sprintf($strCreateIndex,
454 '<input type="text" size="2" name="idx_num_fields" value="1" />'); ?
>
455 <input type
="submit" name
="create_index" value
="<?php echo $strGo; ?>"
456 onclick
="return checkFormElementInRange(this.form,
458 '<?php echo str_replace('\'', '\\\'', $GLOBALS['strInvalidColumnCount']); ?>',
465 } // end display indexes
469 * Displays the footer
473 if (!defined('PMA_IDX_INCLUDED')){
474 require_once './libraries/footer.inc.php';