2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Functionality for the navigation tree in the left frame
6 * @package PhpMyAdmin-Navigation
8 if (! defined('PHPMYADMIN')) {
13 * The Node is the building block for the collapsible navigation tree
15 * @package PhpMyAdmin-Navigation
20 * @var int Defines a possible node type
25 * @var int Defines a possible node type
30 * @var string A non-unique identifier for the node
31 * This may be trimmed when grouping nodes
36 * @var string A non-unique identifier for the node
37 * This will never change after being assigned
39 public $real_name = "";
42 * @var int May be one of CONTAINER or OBJECT
44 public $type = Node
::OBJECT;
47 * @var bool Whether this object has been created while grouping nodes
48 * Only relevant if the node is of type CONTAINER
53 * @var bool Whether to add a "display: none;" CSS
54 * rule to the node when rendering it
56 public $visible = false;
59 * @var Node A reference to the parent object of
60 * this node, NULL for the root node.
65 * @var Node[] An array of Node objects that are
66 * direct children of this node
68 public $children = array();
71 * @var Mixed A string used to group nodes, or an array of strings
72 * Only relevant if the node is of type CONTAINER
74 public $separator = '';
77 * @var int How many time to recursively apply the grouping function
78 * Only relevant if the node is of type CONTAINER
80 public $separator_depth = 1;
83 * @var string An IMG tag, used when rendering the node
88 * @var Array An array of A tags, used when rendering the node
89 * The indexes in the array may be 'icon' and 'text'
94 * @var string HTML title
99 * @var string Extra CSS classes for the node
101 public $classes = '';
104 * @var bool Whether this node is a link for creating new objects
106 public $isNew = false;
109 * @var int The position for the pagination of
110 * the branch at the second level of the tree
115 * @var int The position for the pagination of
116 * the branch at the third level of the tree
121 * Initialises the class by setting the mandatory variables
123 * @param string $name An identifier for the new node
124 * @param int $type Type of node, may be one of CONTAINER or OBJECT
125 * @param bool $is_group Whether this object has been created
126 * while grouping nodes
128 public function __construct($name, $type = Node
::OBJECT, $is_group = false)
130 if (! empty($name)) {
132 $this->real_name
= $name;
134 if ($type === Node
::CONTAINER
) {
135 $this->type
= Node
::CONTAINER
;
137 $this->is_group
= (bool)$is_group;
141 * Adds a child node to this node
143 * @param Node $child A child node
147 public function addChild($child)
149 $this->children
[] = $child;
150 $child->parent
= $this;
154 * Returns a child node given it's name
156 * @param string $name The name of requested child
157 * @param bool $real_name Whether to use the "real_name"
158 * instead of "name" in comparisons
160 * @return false|Node The requested child node or false,
161 * if the requested node cannot be found
163 public function getChild($name, $real_name = false)
166 foreach ($this->children
as $child) {
167 if ($child->real_name
== $name) {
172 foreach ($this->children
as $child) {
173 if ($child->name
== $name) {
182 * Removes a child node from this node
184 * @param string $name The name of child to be removed
188 public function removeChild($name)
190 foreach ($this->children
as $key => $child) {
191 if ($child->name
== $name) {
192 unset($this->children
[$key]);
199 * Retrieves the parents for a node
201 * @param bool $self Whether to include the Node itself in the results
202 * @param bool $containers Whether to include nodes of type CONTAINER
203 * @param bool $groups Whether to include nodes which have $group == true
205 * @return array An array of parent Nodes
207 public function parents($self = false, $containers = false, $groups = false)
211 && ($this->type
!= Node
::CONTAINER ||
$containers)
212 && (!$this->is_group ||
$groups)
216 $parent = $this->parent
;
217 while (isset($parent)) {
218 if (($parent->type
!= Node
::CONTAINER ||
$containers)
219 && (!$parent->is_group ||
$groups)
221 $parents[] = $parent;
223 $parent = $parent->parent
;
229 * Returns the actual parent of a node. If used twice on an index or columns
230 * node, it will return the table and database nodes. The names of the returned
231 * nodes can be used in SQL queries, etc...
235 public function realParent()
237 $retval = $this->parents();
238 if (count($retval) <= 0) {
246 * This function checks if the node has children nodes associated with it
248 * @param bool $count_empty_containers Whether to count empty child
249 * containers as valid children
251 * @return bool Whether the node has child nodes
253 public function hasChildren($count_empty_containers = true)
256 if ($count_empty_containers) {
257 if (count($this->children
)) {
261 foreach ($this->children
as $child) {
262 if ($child->type
== Node
::OBJECT ||
$child->hasChildren(false)) {
272 * Returns true if the node has some siblings (other nodes on the same tree
273 * level, in the same branch), false otherwise.
274 * The only exception is for nodes on
275 * the third level of the tree (columns and indexes), for which the function
276 * always returns true. This is because we want to render the containers
281 public function hasSiblings()
284 $paths = $this->getPaths();
285 if (count($paths['aPath_clean']) > 3) {
290 foreach ($this->parent
->children
as $child) {
292 && ($child->type
== Node
::OBJECT ||
$child->hasChildren(false))
302 * Returns the number of child nodes that a node has associated with it
304 * @return int The number of children nodes
306 public function numChildren()
309 foreach ($this->children
as $child) {
310 if ($child->type
== Node
::OBJECT) {
313 $retval +
= $child->numChildren();
320 * Returns the actual path and the virtual paths for a node
321 * both as clean arrays and base64 encoded strings
325 public function getPaths()
328 $aPath_clean = array();
329 foreach ($this->parents(true, true, false) as $parent) {
330 $aPath[] = base64_encode($parent->real_name
);
331 $aPath_clean[] = $parent->real_name
;
333 $aPath = implode('.', array_reverse($aPath));
334 $aPath_clean = array_reverse($aPath_clean);
337 $vPath_clean = array();
338 foreach ($this->parents(true, true, true) as $parent) {
339 $vPath[] = base64_encode($parent->name
);
340 $vPath_clean[] = $parent->name
;
342 $vPath = implode('.', array_reverse($vPath));
343 $vPath_clean = array_reverse($vPath_clean);
347 'aPath_clean' => $aPath_clean,
349 'vPath_clean' => $vPath_clean
354 * Returns the names of children of type $type present inside this container
355 * This method is overridden by the Node_Database and Node_Table classes
357 * @param string $type The type of item we are looking for
358 * ('tables', 'views', etc)
359 * @param int $pos The offset of the list within the results
360 * @param string $searchClause A string used to filter the results of the query
364 public function getData($type, $pos, $searchClause = '')
366 $maxItems = $GLOBALS['cfg']['FirstLevelNavigationItems'];
367 if (!$GLOBALS['cfg']['NavigationTreeEnableGrouping']
368 ||
!$GLOBALS['cfg']['ShowDatabasesNavigationAsTree']
370 if (isset($GLOBALS['cfg']['Server']['DisableIS'])
371 && ! $GLOBALS['cfg']['Server']['DisableIS']
373 $query = "SELECT `SCHEMA_NAME` ";
374 $query .= "FROM `INFORMATION_SCHEMA`.`SCHEMATA` ";
375 $query .= $this->_getWhereClause('SCHEMA_NAME', $searchClause);
376 $query .= "ORDER BY `SCHEMA_NAME` ";
377 $query .= "LIMIT $pos, $maxItems";
378 $retval = $GLOBALS['dbi']->fetchResult($query);
382 if ($GLOBALS['dbs_to_test'] === false) {
384 $query = "SHOW DATABASES ";
385 $query .= $this->_getWhereClause('Database', $searchClause);
386 $handle = $GLOBALS['dbi']->tryQuery($query);
387 if ($handle === false) {
392 if (!$GLOBALS['dbi']->dataSeek($handle, $pos)) {
396 while ($arr = $GLOBALS['dbi']->fetchArray($handle)) {
397 if ($count < $maxItems) {
410 foreach ($this->_getDatabasesToSearch($searchClause) as $db) {
411 $query = "SHOW DATABASES LIKE '" . $db . "'";
412 $handle = $GLOBALS['dbi']->tryQuery($query);
413 if ($handle === false) {
417 while ($arr = $GLOBALS['dbi']->fetchArray($handle)) {
418 if ($this->_isHideDb($arr[0])) {
421 if (in_array($arr[0], $retval)) {
425 if ($pos <= 0 && $count < $maxItems) {
436 $dbSeparator = $GLOBALS['cfg']['NavigationTreeDbSeparator'];
437 if (isset($GLOBALS['cfg']['Server']['DisableIS'])
438 && ! $GLOBALS['cfg']['Server']['DisableIS']
440 $query = "SELECT `SCHEMA_NAME` ";
441 $query .= "FROM `INFORMATION_SCHEMA`.`SCHEMATA`, ";
443 $query .= "SELECT DB_first_level ";
445 $query .= "SELECT DISTINCT SUBSTRING_INDEX(SCHEMA_NAME, ";
446 $query .= "'$dbSeparator', 1) ";
447 $query .= "DB_first_level ";
448 $query .= "FROM INFORMATION_SCHEMA.SCHEMATA ";
449 $query .= $this->_getWhereClause('SCHEMA_NAME', $searchClause);
451 $query .= "ORDER BY DB_first_level ASC ";
452 $query .= "LIMIT $pos, $maxItems";
454 $query .= $this->_getWhereClause('SCHEMA_NAME', $searchClause);
455 $query .= "AND 1 = LOCATE(CONCAT(DB_first_level, ";
456 $query .= "'$dbSeparator'), ";
457 $query .= "CONCAT(SCHEMA_NAME, ";
458 $query .= "'$dbSeparator')) ";
459 $query .= "ORDER BY SCHEMA_NAME ASC";
460 $retval = $GLOBALS['dbi']->fetchResult($query);
464 if ($GLOBALS['dbs_to_test'] === false) {
465 $query = "SHOW DATABASES ";
466 $query .= $this->_getWhereClause('Database', $searchClause);
467 $handle = $GLOBALS['dbi']->tryQuery($query);
469 if ($handle !== false) {
470 $prefixMap = array();
471 $total = $pos +
$maxItems;
472 while ($arr = $GLOBALS['dbi']->fetchArray($handle)) {
473 $prefix = strstr($arr[0], $dbSeparator, true);
474 if ($prefix === false) {
477 $prefixMap[$prefix] = 1;
478 if (sizeof($prefixMap) == $total) {
482 $prefixes = array_slice(array_keys($prefixMap), $pos);
485 $query = "SHOW DATABASES ";
486 $query .= $this->_getWhereClause('Database', $searchClause);
488 $subClauses = array();
489 foreach ($prefixes as $prefix) {
490 $subClauses[] = " LOCATE('"
491 . PMA_Util
::sqlAddSlashes($prefix) . $dbSeparator . "', "
492 . "CONCAT(`Database`, '" . $dbSeparator . "')) = 1 ";
494 $query .= implode("OR", $subClauses) . ")";
495 $retval = $GLOBALS['dbi']->fetchResult($query);
500 $prefixMap = array();
501 $total = $pos +
$maxItems;
502 foreach ($this->_getDatabasesToSearch($searchClause) as $db) {
503 $query = "SHOW DATABASES LIKE '" . $db . "'";
504 $handle = $GLOBALS['dbi']->tryQuery($query);
505 if ($handle === false) {
509 while ($arr = $GLOBALS['dbi']->fetchArray($handle)) {
510 if ($this->_isHideDb($arr[0])) {
513 $prefix = strstr($arr[0], $dbSeparator, true);
514 if ($prefix === false) {
517 $prefixMap[$prefix] = 1;
518 if (sizeof($prefixMap) == $total) {
523 $prefixes = array_slice(array_keys($prefixMap), $pos);
525 foreach ($this->_getDatabasesToSearch($searchClause) as $db) {
526 $query = "SHOW DATABASES LIKE '" . $db . "'";
527 $handle = $GLOBALS['dbi']->tryQuery($query);
528 if ($handle === false) {
532 while ($arr = $GLOBALS['dbi']->fetchArray($handle)) {
533 if ($this->_isHideDb($arr[0])) {
536 if (in_array($arr[0], $retval)) {
540 foreach ($prefixes as $prefix) {
541 $starts_with = strpos(
542 $arr[0] . $dbSeparator,
543 $prefix . $dbSeparator
558 * Returns the number of children of type $type present inside this container
559 * This method is overridden by the Node_Database and Node_Table classes
561 * @param string $type The type of item we are looking for
562 * ('tables', 'views', etc)
563 * @param string $searchClause A string used to filter the results of the query
567 public function getPresence($type = '', $searchClause = '')
569 if (!$GLOBALS['cfg']['NavigationTreeEnableGrouping']
570 ||
!$GLOBALS['cfg']['ShowDatabasesNavigationAsTree']
572 if (isset($GLOBALS['cfg']['Server']['DisableIS'])
573 && ! $GLOBALS['cfg']['Server']['DisableIS']
575 $query = "SELECT COUNT(*) ";
576 $query .= "FROM INFORMATION_SCHEMA.SCHEMATA ";
577 $query .= $this->_getWhereClause('SCHEMA_NAME', $searchClause);
578 $retval = (int)$GLOBALS['dbi']->fetchValue($query);
582 if ($GLOBALS['dbs_to_test'] === false) {
583 $query = "SHOW DATABASES ";
584 $query .= $this->_getWhereClause('Database', $searchClause);
585 $retval = $GLOBALS['dbi']->numRows(
586 $GLOBALS['dbi']->tryQuery($query)
592 foreach ($this->_getDatabasesToSearch($searchClause) as $db) {
593 $query = "SHOW DATABASES LIKE '" . $db . "'";
594 $retval +
= $GLOBALS['dbi']->numRows(
595 $GLOBALS['dbi']->tryQuery($query)
601 $dbSeparator = $GLOBALS['cfg']['NavigationTreeDbSeparator'];
602 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
603 $query = "SELECT COUNT(*) ";
605 $query .= "SELECT DISTINCT SUBSTRING_INDEX(SCHEMA_NAME, ";
606 $query .= "'$dbSeparator', 1) ";
607 $query .= "DB_first_level ";
608 $query .= "FROM INFORMATION_SCHEMA.SCHEMATA ";
609 $query .= $this->_getWhereClause('SCHEMA_NAME', $searchClause);
611 $retval = (int)$GLOBALS['dbi']->fetchValue($query);
615 if ($GLOBALS['dbs_to_test'] !== false) {
616 $prefixMap = array();
617 foreach ($this->_getDatabasesToSearch($searchClause) as $db) {
618 $query = "SHOW DATABASES LIKE '" . $db . "'";
619 $handle = $GLOBALS['dbi']->tryQuery($query);
620 if ($handle === false) {
624 while ($arr = $GLOBALS['dbi']->fetchArray($handle)) {
625 if ($this->_isHideDb($arr[0])) {
628 $prefix = strstr($arr[0], $dbSeparator, true);
629 if ($prefix === false) {
632 $prefixMap[$prefix] = 1;
635 $retval = count($prefixMap);
639 $prefixMap = array();
640 $query = "SHOW DATABASES ";
641 $query .= $this->_getWhereClause('Database', $searchClause);
642 $handle = $GLOBALS['dbi']->tryQuery($query);
643 if ($handle !== false) {
644 while ($arr = $GLOBALS['dbi']->fetchArray($handle)) {
645 $prefix = strstr($arr[0], $dbSeparator, true);
646 if ($prefix === false) {
649 $prefixMap[$prefix] = 1;
652 $retval = count($prefixMap);
658 * Detemines whether a given database should be hidden according to 'hide_db'
660 * @param string $db database name
662 * @return boolean whether to hide
664 private function _isHideDb($db)
666 if (! empty($GLOBALS['cfg']['Server']['hide_db'])
667 && preg_match('/' . $GLOBALS['cfg']['Server']['hide_db'] . '/', $db)
675 * Get the list of databases for 'SHOW DATABASES LIKE' queries.
676 * If a search clause is set it gets the highest priority while only_db gets
677 * the next priority. In case both are empty list of databases determined by
680 * @param string $searchClause search clause
682 * @return array array of databases
684 private function _getDatabasesToSearch($searchClause)
686 if (! empty($searchClause)) {
688 "%" . PMA_Util
::sqlAddSlashes($searchClause, true) . "%"
690 } elseif (! empty($GLOBALS['cfg']['Server']['only_db'])) {
691 $databases = $GLOBALS['cfg']['Server']['only_db'];
692 } elseif (! empty($GLOBALS['dbs_to_test'])) {
693 $databases = $GLOBALS['dbs_to_test'];
700 * Returns the WHERE clause depending on the $searchClause parameter
701 * and the hide_db directive
703 * @param string $columnName Column name of the column having database names
704 * @param string $searchClause A string used to filter the results of the query
708 private function _getWhereClause($columnName, $searchClause = '')
710 $whereClause = "WHERE TRUE ";
711 if (! empty($searchClause)) {
712 $whereClause .= "AND " . PMA_Util
::backquote($columnName) . " LIKE '%";
713 $whereClause .= PMA_Util
::sqlAddSlashes(
716 $whereClause .= "%' ";
719 if (! empty($GLOBALS['cfg']['Server']['hide_db'])) {
720 $whereClause .= "AND " . PMA_Util
::backquote($columnName)
721 . " NOT REGEXP '" . $GLOBALS['cfg']['Server']['hide_db'] . "' ";
724 if (! empty($GLOBALS['cfg']['Server']['only_db'])) {
725 if (is_string($GLOBALS['cfg']['Server']['only_db'])) {
726 $GLOBALS['cfg']['Server']['only_db'] = array(
727 $GLOBALS['cfg']['Server']['only_db']
730 $whereClause .= "AND (";
731 $subClauses = array();
732 foreach ($GLOBALS['cfg']['Server']['only_db'] as $each_only_db) {
733 $subClauses[] = " " . PMA_Util
::backquote($columnName) . " LIKE '"
734 . $each_only_db . "' ";
736 $whereClause .= implode("OR", $subClauses) . ")";
742 * Returns HTML for control buttons displayed infront of a node
744 * @return String HTML for control buttons
746 public function getHtmlForControlButtons()
752 * Returns CSS classes for a node
754 * @param boolean $match Whether the node matched loaded tree
756 * @return String with html classes.
758 public function getCssClasses($match)
760 if (! $GLOBALS['cfg']['NavigationTreeEnableExpansion']
765 $result = array('expander');
767 if ($this->is_group ||
$match) {
768 $result[] = 'loaded';
770 if ($this->type
== Node
::CONTAINER
) {
771 $result[] = 'container';
774 return implode(' ', $result);
778 * Returns icon for the node
780 * @param boolean $match Whether the node matched loaded tree
782 * @return String with image name
784 public function getIcon($match)
786 if (! $GLOBALS['cfg']['NavigationTreeEnableExpansion']
789 } elseif ($match && ! $this->is_group
) {
790 $this->visible
= true;
791 return PMA_Util
::getImage('b_minus.png');
793 return PMA_Util
::getImage('b_plus.png', __('Expand/Collapse'));
798 * Gets the count of hidden elements for each database
800 * @return array array containing the count of hidden elements for each database
802 public function getNavigationHidingData()
804 $cfgRelation = PMA_getRelationsParam();
805 if ($cfgRelation['navwork']) {
806 $navTable = PMA_Util
::backquote($cfgRelation['db'])
807 . "." . PMA_Util
::backquote($cfgRelation['navigationhiding']);
808 $sqlQuery = "SELECT `db_name`, COUNT(*) AS `count` FROM " . $navTable
809 . " WHERE `username`='"
810 . PMA_Util
::sqlAddSlashes($GLOBALS['cfg']['Server']['user']) . "'"
811 . " GROUP BY `db_name`";
812 $counts = $GLOBALS['dbi']->fetchResult(
813 $sqlQuery, 'db_name', 'count', $GLOBALS['controllink']