sorry, wrong version checked in
[phpmyadmin/arisferyanto.git] / libraries / tbl_indexes.lib.php
blob9359a48020507c3f62a8d05e7cd51a04b37dc016
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 /**
6 * Return a list of all index types
8 * @access public
9 * @return array Index types
10 * @author Garvin Hicking (pma@supergarv.de)
13 function PMA_get_indextypes() {
14 return array(
15 'PRIMARY',
16 'INDEX',
17 'UNIQUE',
18 'FULLTEXT'
22 /**
23 * Function to get all index information from a certain table
25 * @param string Table name
26 * @param string Error URL
28 * @access public
29 * @return array Index keys
31 function PMA_get_indexes($tbl_name, $err_url_0 = '') {
32 $tbl_local_query = 'SHOW KEYS FROM ' . PMA_backquote($tbl_name);
33 $tbl_result = PMA_DBI_query($tbl_local_query) or PMA_mysqlDie('', $tbl_local_query, '', $err_url_0);
34 $tbl_ret_keys = array();
35 while ($tbl_row = PMA_DBI_fetch_assoc($tbl_result)) {
36 $tbl_ret_keys[] = $tbl_row;
38 PMA_DBI_free_result($tbl_result);
40 return $tbl_ret_keys;
43 /**
44 * Function to check over array of indexes and look for common problems
46 * @param array Array of indexes
47 * @param boolean Whether to output HTML in table layout
49 * @access public
50 * @return string Output HTML
51 * @author Garvin Hicking (pma@supergarv.de)
53 function PMA_check_indexes($idx_collection, $table = true) {
54 $index_types = PMA_get_indextypes();
55 $output = '';
56 if (is_array($idx_collection) && isset($idx_collection['ALL'])) {
57 foreach($idx_collection['ALL'] AS $w_keyname => $w_count) {
58 if (isset($idx_collection['PRIMARY'][$w_keyname]) && (isset($idx_collection['INDEX'][$w_keyname]) || isset($idx_collection['UNIQUE'][$w_keyname]))) {
59 $output .= PMA_index_warning(sprintf($GLOBALS['strIndexWarningPrimary'], htmlspecialchars($w_keyname)), $table);
60 } elseif (isset($idx_collection['UNIQUE'][$w_keyname]) && isset($idx_collection['INDEX'][$w_keyname])) {
61 $output .= PMA_index_warning(sprintf($GLOBALS['strIndexWarningUnique'], htmlspecialchars($w_keyname)), $table);
64 foreach($index_types AS $index_type) {
65 if (isset($idx_collection[$index_type][$w_keyname]) && $idx_collection[$index_type][$w_keyname] > 1) {
66 $output .= PMA_index_warning(sprintf($GLOBALS['strIndexWarningMultiple'], $index_type, htmlspecialchars($w_keyname)), $table);
72 return $output;
75 /**
76 * Loop array of returned index keys and extract key information to
77 * seperate arrays. Those arrays are passed by reference.
79 * @param array Referenced Array of indexes
80 * @param array Referenced return array
81 * @param array Referenced return array
82 * @param array Referenced return array
84 * @access public
85 * @return boolean void
86 * @author Garvin Hicking (pma@supergarv.de)
88 function PMA_extract_indexes(&$ret_keys, &$indexes, &$indexes_info, &$indexes_data) {
89 if (!is_array($ret_keys)) {
90 return false;
93 $prev_index = '';
94 foreach ($ret_keys as $row) {
95 if ($row['Key_name'] != $prev_index ){
96 $indexes[] = $row['Key_name'];
97 $prev_index = $row['Key_name'];
100 $indexes_info[$row['Key_name']]['Sequences'][] = $row['Seq_in_index'];
101 $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique'];
103 if (isset($row['Cardinality'])) {
104 $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
107 // I don't know what does following column mean....
108 // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed'];
109 $indexes_info[$row['Key_name']]['Comment'] = (isset($row['Comment']))
110 ? $row['Comment']
111 : '';
112 $indexes_info[$row['Key_name']]['Index_type'] = (isset($row['Index_type']))
113 ? $row['Index_type']
114 : '';
116 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name'];
117 if (isset($row['Sub_part'])) {
118 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
120 } // end while
122 return true;
126 * Show index data and prepare returned collection array for index
127 * key checks.
129 * @param string The tablename
130 * @param array Referenced Array of indexes
131 * @param array Referenced info array
132 * @param array Referenced data array
133 * @param boolean Output HTML code, or just return collection array?
135 * @access public
136 * @return array Index collection array
137 * @author Garvin Hicking (pma@supergarv.de)
139 function PMA_show_indexes($table, &$indexes, &$indexes_info, &$indexes_data, $display_html = true, $print_mode = false) {
140 $idx_collection = array();
141 foreach ($indexes AS $index_no => $index_name) {
142 if ($display_html) {
143 if ($print_mode) {
144 $index_td = ' <td class="print" rowspan="' . count($indexes_info[$index_name]['Sequences']) . '">' . "\n";
145 } else {
146 $cell_bgd = (($index_no % 2) ? $GLOBALS['cfg']['BgcolorOne'] : $GLOBALS['cfg']['BgcolorTwo']);
147 $index_td = ' <td bgcolor="' . $cell_bgd . '" rowspan="' . count($indexes_info[$index_name]['Sequences']) . '">' . "\n";
149 echo ' <tr>' . "\n";
150 echo $index_td
151 . ' ' . htmlspecialchars($index_name) . "\n"
152 . ' </td>' . "\n";
155 if ((PMA_MYSQL_INT_VERSION < 40002 && $indexes_info[$index_name]['Comment'] == 'FULLTEXT')
156 || (PMA_MYSQL_INT_VERSION >= 40002 && $indexes_info[$index_name]['Index_type'] == 'FULLTEXT')) {
157 $index_type = 'FULLTEXT';
158 } else if ($index_name == 'PRIMARY') {
159 $index_type = 'PRIMARY';
160 } else if ($indexes_info[$index_name]['Non_unique'] == '0') {
161 $index_type = 'UNIQUE';
162 } else {
163 $index_type = 'INDEX';
166 if ($display_html) {
167 echo $index_td
168 . ' ' . $index_type . "\n"
169 . ' </td>' . "\n";
171 echo str_replace('">' . "\n", '" align="right">' . "\n", $index_td)
172 . ' ' . (isset($indexes_info[$index_name]['Cardinality']) ? $indexes_info[$index_name]['Cardinality'] : $GLOBALS['strNone']) . '&nbsp;' . "\n"
173 . ' </td>' . "\n";
175 if (!$print_mode) {
176 echo $index_td
177 . ' <a href="tbl_indexes.php?' . $GLOBALS['url_query'] . '&amp;index=' . urlencode($index_name) . '">' . $GLOBALS['edit_link_text'] . '</a>' . "\n"
178 . ' </td>' . "\n";
181 if ($index_name == 'PRIMARY') {
182 $local_query = urlencode('ALTER TABLE ' . PMA_backquote($table) . ' DROP PRIMARY KEY');
183 $js_msg = 'ALTER TABLE ' . PMA_jsFormat($table) . ' DROP PRIMARY KEY';
184 $zero_rows = urlencode($GLOBALS['strPrimaryKeyHasBeenDropped']);
185 } else {
186 $local_query = urlencode('ALTER TABLE ' . PMA_backquote($table) . ' DROP INDEX ' . PMA_backquote($index_name));
187 $js_msg = 'ALTER TABLE ' . PMA_jsFormat($table) . ' DROP INDEX ' . PMA_jsFormat($index_name);
188 $zero_rows = urlencode(sprintf($GLOBALS['strIndexHasBeenDropped'], htmlspecialchars($index_name)));
191 if (!$print_mode) {
192 echo $index_td
193 . ' <a href="sql.php?' . $GLOBALS['url_query'] . '&amp;sql_query=' . $local_query . '&amp;zero_rows=' . $zero_rows . '" onclick="return confirmLink(this, \'' . $js_msg . '\')">' . $GLOBALS['drop_link_text'] . '</a>' . "\n"
194 . ' </td>' . "\n";
198 foreach ($indexes_info[$index_name]['Sequences'] AS $row_no => $seq_index) {
199 $col_name = $indexes_data[$index_name][$seq_index]['Column_name'];
200 if ($row_no == 0) {
201 if (isset($idx_collection[$index_type][$col_name])) {
202 $idx_collection[$index_type][$col_name]++;
203 } else {
204 $idx_collection[$index_type][$col_name] = 1;
207 if (isset($idx_collection['ALL'][$col_name])) {
208 $idx_collection['ALL'][$col_name]++;
209 } else {
210 $idx_collection['ALL'][$col_name] = 1;
214 if ($display_html) {
215 if ($row_no > 0) {
216 echo ' <tr>' . "\n";
219 if ($print_mode) {
220 $bgcolor = 'class="print"';
221 } else {
222 $bgcolor = 'bgcolor="' . $cell_bgd . '"';
225 if (!empty($indexes_data[$index_name][$seq_index]['Sub_part'])) {
226 echo ' <td ' . $bgcolor . '>' . "\n"
227 . ' ' . $col_name . "\n"
228 . ' </td>' . "\n";
229 echo ' <td align="right" ' . $bgcolor . '>' . "\n"
230 . ' ' . $indexes_data[$index_name][$seq_index]['Sub_part'] . "\n"
231 . ' </td>' . "\n";
232 echo ' </tr>' . "\n";
233 } else {
234 echo ' <td ' . $bgcolor . ' colspan="2">' . "\n"
235 . ' ' . htmlspecialchars($col_name) . "\n"
236 . ' </td>' . "\n";
237 echo ' </tr>' . "\n";
240 } // end while
241 } // end while
243 return $idx_collection;
247 * Function to emit a index warning
249 * @param string Message string
250 * @param boolean Whether to output HTML in table layout
252 * @access public
253 * @output string Output HTML
254 * @author Garvin Hicking (pma@supergarv.de)
256 function PMA_index_warning($string, $table = true) {
257 $output = '';
258 if ($table) {
259 $output .= "\n" . ' <tr><td colspan=7">' . "\n";
262 if ($GLOBALS['cfg']['ErrorIconic']) {
263 $output .= '<img src="' . $GLOBALS['pmaThemeImage'] . 's_warn.png" width="16" height="16" border="0" alt="Warning" hspace="2" align="middle" />';
266 $output .= ' <b>' . $string . '</b>';
268 if ($table) {
269 $output .= '</td></tr>';
270 } else {
271 $output .= '<br />';
274 $output .= "\n\n";
275 return $output;