Feature Request #3187076
[phpmyadmin-regexreplace.git] / libraries / Index.class.php
blob6fc0bec097c8b8a51717b47a192a188a1f2e21a4
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * holds the database index class
6 * @package phpMyAdmin
7 */
9 /**
10 * @since phpMyAdmin 3.0.0
12 * @package phpMyAdmin
14 class PMA_Index
16 /**
17 * Class-wide storage container for indexes (caching, singleton)
19 * @var array
21 protected static $_registry = array();
23 /**
24 * @var string The name of the schema
26 protected $_schema = '';
28 /**
29 * @var string The name of the table
31 protected $_table = '';
33 /**
34 * @var string The name of the index
36 protected $_name = '';
38 /**
39 * Columns in index
41 * @var array
43 protected $_columns = array();
45 /**
46 * The index method used (BTREE, FULLTEXT, HASH, RTREE).
48 * @var string
50 protected $_type = '';
52 /**
53 * The index choice (PRIMARY, UNIQUE, INDEX, FULLTEXT)
55 * @var string
57 protected $_choice = '';
59 /**
60 * Various remarks.
62 * @var string
64 protected $_remarks = '';
66 /**
67 * Any comment provided for the index with a COMMENT attribute when the
68 * index was created.
70 * @var string
72 protected $_comment = '';
74 /**
75 * @var integer 0 if the index cannot contain duplicates, 1 if it can.
77 protected $_non_unique = 0;
79 /**
80 * Indicates how the key is packed. NULL if it is not.
82 * @var string
84 protected $_packed = null;
86 /**
87 * Constructor
89 * @uses $this->set()
90 * @param array $params
92 public function __construct($params = array())
94 $this->set($params);
97 static public function singleton($schema, $table, $index_name = '')
99 PMA_Index::_loadIndexes($table, $schema);
100 if (! isset(PMA_Index::$_registry[$schema][$table][$index_name])) {
101 $index = new PMA_Index;
102 if (strlen($index_name)) {
103 $index->setName($index_name);
104 PMA_Index::$_registry[$schema][$table][$index->getName()] = $index;
106 return $index;
107 } else {
108 return PMA_Index::$_registry[$schema][$table][$index_name];
113 * returns an array with all indexes from the given table
115 * @uses PMA_Index::_loadIndexes()
116 * @uses PMA_Index::$_registry
117 * @param string $table
118 * @param string $schema
119 * @return array
121 static public function getFromTable($table, $schema)
123 PMA_Index::_loadIndexes($table, $schema);
125 if (isset(PMA_Index::$_registry[$schema][$table])) {
126 return PMA_Index::$_registry[$schema][$table];
127 } else {
128 return array();
133 * return primary if set, false otherwise
135 * @uses PMA_Index::_loadIndexes()
136 * @uses PMA_Index::$_registry
137 * @param string $table
138 * @param string $schema
139 * @return mixed primary index or false if no one exists
141 static public function getPrimary($table, $schema)
143 PMA_Index::_loadIndexes($table, $schema);
145 if (isset(PMA_Index::$_registry[$schema][$table]['PRIMARY'])) {
146 return PMA_Index::$_registry[$schema][$table]['PRIMARY'];
147 } else {
148 return false;
153 * Load index data for table
155 * @uses PMA_Index::$_registry
156 * @uses PMA_DBI_fetch_result()
157 * @uses PMA_backquote()
158 * @uses PMA_Index
159 * @uses PMA_Index->addColumn()
160 * @param string $table
161 * @param string $schema
162 * @return boolean
164 static protected function _loadIndexes($table, $schema)
166 if (isset(PMA_Index::$_registry[$schema][$table])) {
167 return true;
170 $_raw_indexes = PMA_DBI_fetch_result('SHOW INDEX FROM ' . PMA_backquote($schema) . '.' . PMA_backquote($table));
171 foreach ($_raw_indexes as $_each_index) {
172 $_each_index['Schema'] = $schema;
173 if (! isset(PMA_Index::$_registry[$schema][$table][$_each_index['Key_name']])) {
174 $key = new PMA_Index($_each_index);
175 PMA_Index::$_registry[$schema][$table][$_each_index['Key_name']] = $key;
176 } else {
177 $key = PMA_Index::$_registry[$schema][$table][$_each_index['Key_name']];
180 $key->addColumn($_each_index);
183 return true;
187 * Add column to index
189 * @uses $this->_columns
190 * @uses PMA_Index_Column
191 * @param array $params column params
193 public function addColumn($params)
195 if (strlen($params['Column_name'])) {
196 $this->_columns[$params['Column_name']] = new PMA_Index_Column($params);
200 public function addColumns($columns)
202 $_columns = array();
204 if (isset($columns['names'])) {
205 // coming from form
206 // $columns[names][]
207 // $columns[sub_parts][]
208 foreach ($columns['names'] as $key => $name) {
209 $_columns[] = array(
210 'Column_name' => $name,
211 'Sub_part' => $columns['sub_parts'][$key],
214 } else {
215 // coming from SHOW INDEXES
216 // $columns[][name]
217 // $columns[][sub_part]
218 // ...
219 $_columns = $columns;
222 foreach ($_columns as $column) {
223 $this->addColumn($column);
228 * Returns true if $column indexed in this index
230 * @uses $this->_columns
231 * @param string $column
232 * @return boolean
234 public function hasColumn($column)
236 return isset($this->_columns[$column]);
239 public function set($params)
241 if (isset($params['columns'])) {
242 $this->addColumns($params['columns']);
244 if (isset($params['Schema'])) {
245 $this->_schema = $params['Schema'];
247 if (isset($params['Table'])) {
248 $this->_table = $params['Table'];
250 if (isset($params['Key_name'])) {
251 $this->_name = $params['Key_name'];
253 if (isset($params['Index_type'])) {
254 $this->_type = $params['Index_type'];
256 if (isset($params['Comment'])) {
257 $this->_remarks = $params['Comment'];
259 if (isset($params['Index_comment'])) {
260 $this->_comment = $params['Index_comment'];
262 if (isset($params['Non_unique'])) {
263 $this->_non_unique = $params['Non_unique'];
265 if (isset($params['Packed'])) {
266 $this->_packed = $params['Packed'];
268 if ('PRIMARY' == $this->_name) {
269 $this->_choice = 'PRIMARY';
270 } elseif ('FULLTEXT' == $this->_type) {
271 $this->_choice = 'FULLTEXT';
272 } elseif ('0' == $this->_non_unique) {
273 $this->_choice = 'UNIQUE';
274 } else {
275 $this->_choice = 'INDEX';
279 public function getColumnCount()
281 return count($this->_columns);
284 public function getComment()
286 return $this->_comment;
289 public function getRemarks()
291 return $this->_remarks;
294 public function getComments()
296 $comments = $this->getRemarks();
297 if (strlen($comments)) {
298 $comments .= "\n";
300 $comments .= $this->getComment();
302 return $comments;
305 public function getType()
307 return $this->_type;
310 public function getChoice()
312 return $this->_choice;
316 * Return a list of all index choices
318 * @return array index choices
320 static public function getIndexChoices()
322 return array(
323 'PRIMARY',
324 'INDEX',
325 'UNIQUE',
326 'FULLTEXT',
330 public function generateIndexSelector()
332 $html_options = '';
334 foreach (PMA_Index::getIndexChoices() as $each_index_choice) {
335 if ($each_index_choice === 'PRIMARY'
336 && $this->_choice !== 'PRIMARY'
337 && PMA_Index::getPrimary($this->_table, $this->_schema)) {
338 // skip PRIMARY if there is already one in the table
339 continue;
341 $html_options .= '<option value="' . $each_index_choice . '"'
342 . (($this->_choice == $each_index_choice) ? ' selected="selected"' : '')
343 . '>'. $each_index_choice . '</option>' . "\n";
346 return $html_options;
349 public function getPacked()
351 return $this->_packed;
354 public function isPacked($as_text = false)
356 if ($as_text) {
357 $r = array(
358 '0' => __('No'),
359 '1' => __('Yes'),
361 } else {
362 $r = array(
363 '0' => false,
364 '1' => true,
368 if (null === $this->_packed) {
369 return $r[0];
372 return $this->_packed;
375 public function getNonUnique()
377 return $this->_non_unique;
380 public function isUnique($as_text = false)
382 if ($as_text) {
383 $r = array(
384 '0' => __('Yes'),
385 '1' => __('No'),
387 } else {
388 $r = array(
389 '0' => true,
390 '1' => false,
394 return $r[$this->_non_unique];
397 public function getName()
399 return $this->_name;
402 public function setName($name)
404 $this->_name = (string) $name;
407 public function getColumns()
409 return $this->_columns;
413 * Show index data
415 * @param string $table The tablename
416 * @param array $indexes_info Referenced info array
417 * @param array $indexes_data Referenced data array
418 * @param boolean $print_mode
419 * @access public
420 * @return array Index collection array
422 static public function getView($table, $schema, $print_mode = false)
424 $indexes = PMA_Index::getFromTable($table, $schema);
426 if (count($indexes) < 1) {
427 return PMA_Message::warning(__('No index defined!'))->getDisplay();
430 $r = '';
432 $r .= '<h2>' . __('Indexes') . ': ';
433 $r .= PMA_showMySQLDocu('optimization', 'optimizing-database-structure');
434 $r .= '</h2>';
435 $r .= '<table>';
436 $r .= '<thead>';
437 $r .= '<tr>';
438 if (! $print_mode) {
439 $r .= '<th colspan="2">' . __('Action') . '</th>';
441 $r .= '<th>' . __('Keyname') . '</th>';
442 $r .= '<th>' . __('Type') . '</th>';
443 $r .= '<th>' . __('Unique') . '</th>';
444 $r .= '<th>' . __('Packed') . '</th>';
445 $r .= '<th>' . __('Column') . '</th>';
446 $r .= '<th>' . __('Cardinality') . '</th>';
447 $r .= '<th>' . __('Collation') . '</th>';
448 $r .= '<th>' . __('Null') . '</th>';
449 $r .= '<th>' . __('Comment') . '</th>';
450 $r .= '</tr>';
451 $r .= '</thead>';
452 $r .= '<tbody>';
454 $odd_row = true;
455 foreach ($indexes as $index) {
456 $row_span = ' rowspan="' . $index->getColumnCount() . '" ';
458 $r .= '<tr class="' . ($odd_row ? 'odd' : 'even') . '">';
460 if (! $print_mode) {
461 $this_params = $GLOBALS['url_params'];
462 $this_params['index'] = $index->getName();
463 $r .= '<td ' . $row_span . '>'
464 . ' <a href="tbl_indexes.php' . PMA_generate_common_url($this_params)
465 . '">' . PMA_getIcon('b_edit.png', __('Edit')) . '</a>'
466 . '</td>' . "\n";
468 $this_params = $GLOBALS['url_params'];
469 if ($index->getName() == 'PRIMARY') {
470 $this_params['sql_query'] = 'ALTER TABLE ' . PMA_backquote($table) . ' DROP PRIMARY KEY';
471 $this_params['message_to_show'] = __('The primary key has been dropped');
472 $js_msg = PMA_jsFormat('ALTER TABLE ' . $table . ' DROP PRIMARY KEY');
473 } else {
474 $this_params['sql_query'] = 'ALTER TABLE ' . PMA_backquote($table) . ' DROP INDEX ' . PMA_backquote($index->getName());
475 $this_params['message_to_show'] = sprintf(__('Index %s has been dropped'), $index->getName());
476 $js_msg = PMA_jsFormat('ALTER TABLE ' . $table . ' DROP INDEX ' . $index->getName());
479 $r .= '<td ' . $row_span . '>';
480 $r .= '<input type="hidden" class="drop_primary_key_index_msg" value="' . $js_msg . '" />';
481 $r .= ' <a ';
482 if ($GLOBALS['cfg']['AjaxEnable']) {
483 $r .= 'class="drop_primary_key_index_anchor" ';
485 $r .= ' href="sql.php' . PMA_generate_common_url($this_params)
486 . '" >'
487 . PMA_getIcon('b_drop.png', __('Drop')) . '</a>'
488 . '</td>' . "\n";
491 $r .= '<th ' . $row_span . '>' . htmlspecialchars($index->getName()) . '</th>';
492 $r .= '<td ' . $row_span . '>' . htmlspecialchars($index->getType()) . '</td>';
493 $r .= '<td ' . $row_span . '>' . $index->isUnique(true) . '</td>';
494 $r .= '<td ' . $row_span . '>' . $index->isPacked(true) . '</td>';
496 foreach ($index->getColumns() as $column) {
497 if ($column->getSeqInIndex() > 1) {
498 $r .= '<tr class="' . ($odd_row ? 'odd' : 'even') . '">';
500 $r .= '<td>' . htmlspecialchars($column->getName());
501 if ($column->getSubPart()) {
502 $r .= ' (' . $column->getSubPart() . ')';
504 $r .= '</td>';
505 $r .= '<td>' . htmlspecialchars($column->getCardinality()) . '</td>';
506 $r .= '<td>' . htmlspecialchars($column->getCollation()) . '</td>';
507 $r .= '<td>' . htmlspecialchars($column->getNull()) . '</td>';
509 if ($column->getSeqInIndex() == 1) {
510 $r .= '<td ' . $row_span . '>'
511 . htmlspecialchars($index->getComments()) . '</td>';
513 $r .= '</tr>';
514 } // end foreach $index['Sequences']
516 $odd_row = ! $odd_row;
517 } // end while
518 $r .= '</tbody>';
519 $r .= '</table>';
521 if (! $print_mode) {
522 $r .= PMA_Index::findDuplicates($table, $schema);
525 return $r;
528 public function getCompareData()
530 $data = array(
531 // 'Non_unique' => $this->_non_unique,
532 'Packed' => $this->_packed,
533 'Index_type' => $this->_type,
536 foreach ($this->_columns as $column) {
537 $data['columns'][] = $column->getCompareData();
540 return $data;
544 * Function to check over array of indexes and look for common problems
546 * @uses is_string()
547 * @uses is_array()
548 * @uses count()
549 * @uses array_pop()
550 * @uses reset()
551 * @uses current()
552 * @access public
553 * @param string name of table
554 * @return string Output HTML
556 static public function findDuplicates($table, $schema)
558 $indexes = PMA_Index::getFromTable($table, $schema);
560 $output = '';
562 // count($indexes) < 2:
563 // there is no need to check if there less than two indexes
564 if (count($indexes) < 2) {
565 return $output;
568 // remove last index from stack and ...
569 while ($while_index = array_pop($indexes)) {
570 // ... compare with every remaining index in stack
571 foreach ($indexes as $each_index) {
572 if ($each_index->getCompareData() !== $while_index->getCompareData()) {
573 continue;
576 // did not find any difference
577 // so it makes no sense to have this two equal indexes
579 $message = PMA_Message::warning(__('The indexes %1$s and %2$s seem to be equal and one of them could possibly be removed.'));
580 $message->addParam($each_index->getName());
581 $message->addParam($while_index->getName());
582 $output .= $message->getDisplay();
584 // there is no need to check any further indexes if we have already
585 // found that this one has a duplicate
586 continue 2;
589 return $output;
594 * @package phpMyAdmin
596 class PMA_Index_Column
599 * @var string The column name
601 protected $_name = '';
604 * @var integer The column sequence number in the index, starting with 1.
606 protected $_seq_in_index = 1;
609 * @var string How the column is sorted in the index. “A” (Ascending) or NULL (Not sorted)
611 protected $_collation = null;
614 * The number of indexed characters if the column is only partly indexed,
615 * NULL if the entire column is indexed.
617 * @var integer
619 protected $_sub_part = null;
622 * Contains YES if the column may contain NULL.
623 * If not, the column contains NO.
625 * @var string
627 protected $_null = '';
630 * An estimate of the number of unique values in the index. This is updated
631 * by running ANALYZE TABLE or myisamchk -a. Cardinality is counted based on
632 * statistics stored as integers, so the value is not necessarily exact even
633 * for small tables. The higher the cardinality, the greater the chance that
634 * MySQL uses the index when doing joins.
636 * @var integer
638 protected $_cardinality = 0;
640 public function __construct($params = array())
642 $this->set($params);
645 public function set($params)
647 if (isset($params['Column_name'])) {
648 $this->_name = $params['Column_name'];
650 if (isset($params['Seq_in_index'])) {
651 $this->_seq_in_index = $params['Seq_in_index'];
653 if (isset($params['Collation'])) {
654 $this->_collation = $params['Collation'];
656 if (isset($params['Cardinality'])) {
657 $this->_cardinality = $params['Cardinality'];
659 if (isset($params['Sub_part'])) {
660 $this->_sub_part = $params['Sub_part'];
662 if (isset($params['Null'])) {
663 $this->_null = $params['Null'];
667 public function getName()
669 return $this->_name;
672 public function getCollation()
674 return $this->_collation;
677 public function getCardinality()
679 return $this->_cardinality;
682 public function getNull()
684 return $this->_null;
687 public function getSeqInIndex()
689 return $this->_seq_in_index;
692 public function getSubPart()
694 return $this->_sub_part;
697 public function getCompareData()
699 return array(
700 'Column_name' => $this->_name,
701 'Seq_in_index' => $this->_seq_in_index,
702 'Collation' => $this->_collation,
703 'Sub_part' => $this->_sub_part,
704 'Null' => $this->_null,