name = $name; $this->real_name = $name; } if ($type === Node::CONTAINER) { $this->type = Node::CONTAINER; } $this->is_group = (bool)$is_group; } /** * Adds a child node to this node * * @param Node $child A child node * * @return void */ public function addChild($child) { $this->children[] = $child; $child->parent = $this; } /** * Returns a child node given it's name * * @param string $name The name of requested child * @param bool $real_name Whether to use the "real_name" * instead of "name" in comparisons * * @return false|Node The requested child node or false, * if the requested node cannot be found */ public function getChild($name, $real_name = false) { if ($real_name) { foreach ($this->children as $child) { if ($child->real_name == $name) { return $child; } } } else { foreach ($this->children as $child) { if ($child->name == $name) { return $child; } } } return false; } /** * Removes a child node from this node * * @param string $name The name of child to be removed * * @return void */ public function removeChild($name) { foreach ($this->children as $key => $child) { if ($child->name == $name) { unset($this->children[$key]); break; } } } /** * Retreives the parents for a node * * @param bool $self Whether to include the Node itself in the results * @param bool $containers Whether to include nodes of type CONTAINER * @param bool $groups Whether to include nodes which have $group == true * * @return array An array of parent Nodes */ public function parents($self = false, $containers = false, $groups = false) { $parents = array(); if ($self && ($this->type != Node::CONTAINER || $containers) && ($this->is_group != true || $groups) ) { $parents[] = $this; $self = false; } $parent = $this->parent; while (isset($parent)) { if ( ($parent->type != Node::CONTAINER || $containers) && ($parent->is_group != true || $groups) ) { $parents[] = $parent; } $parent = $parent->parent; } return $parents; } /** * Returns the actual parent of a node. If used twice on an index or columns * node, it will return the table and database nodes. The names of the returned * nodes can be used in SQL queries, etc... * * @return Node */ public function realParent() { $retval = $this->parents(); if (count($retval) > 0) { return $retval[0]; } else { return false; } } /** * This function checks if the node has children nodes associated with it * * @param bool $count_empty_containers Whether to count empty child * containers as valid children * * @return bool Whether the node has child nodes */ public function hasChildren($count_empty_containers = true) { $retval = false; if ($count_empty_containers) { if (count($this->children)) { $retval = true; } } else { foreach ($this->children as $child) { if ($child->type == Node::OBJECT || $child->hasChildren(false)) { $retval = true; break; } } } return $retval; } /** * Returns true the node has some siblings (other nodes on the same tree level, * in the same branch), false otherwise. The only exception is for nodes on * the third level of the tree (columns and indexes), for which the function * always returns true. This is because we want to render the containers * for these nodes * * @return bool */ public function hasSiblings() { $retval = false; $paths = $this->getPaths(); if (count($paths['aPath_clean']) > 3) { $retval = true; } else { foreach ($this->parent->children as $child) { if ($child !== $this && ($child->type == Node::OBJECT || $child->hasChildren(false)) ) { $retval = true; break; } } } return $retval; } /** * Returns the number of child nodes that a node has associated with it * * @return int The number of children nodes */ public function numChildren() { $retval = 0; foreach ($this->children as $child) { if ($child->type == Node::OBJECT) { $retval++; } else { $retval += $child->numChildren(); } } return $retval; } /** * Returns the actual path and the virtual paths for a node * both as clean arrays and base64 encoded strings * * @return array */ public function getPaths() { $aPath = array(); $aPath_clean = array(); foreach ($this->parents(true, true, false) as $parent) { $aPath[] = base64_encode($parent->real_name); $aPath_clean[] = $parent->real_name; } $aPath = implode('.', array_reverse($aPath)); $aPath_clean = array_reverse($aPath_clean); $vPath = array(); $vPath_clean = array(); foreach ($this->parents(true, true, true) as $parent) { $vPath[] = base64_encode($parent->name); $vPath_clean[] = $parent->name; } $vPath = implode('.', array_reverse($vPath)); $vPath_clean = array_reverse($vPath_clean); return array( 'aPath' => $aPath, 'aPath_clean' => $aPath_clean, 'vPath' => $vPath, 'vPath_clean' => $vPath_clean ); } /** * Returns the names of children of type $type present inside this container * This method is overridden by the Node_Database and Node_Table classes * * @param string $type The type of item we are looking for * ('tables', 'views', etc) * @param int $pos The offset of the list within the results * @param string $searchClause A string used to filter the results of the query * * @return array */ public function getData($type, $pos, $searchClause = '') { if ($type == 'databases' && ! empty($GLOBALS['cfg']['Server']['only_db']) ) { $db_list = $GLOBALS['cfg']['Server']['only_db']; $query = "SELECT * FROM ( SELECT '"; if (is_string($db_list)) { $db_list = array($db_list); } if (count($db_list)) { $query .= implode("' UNION ALL SELECT '", $db_list); $query .= "' "; } return $GLOBALS['dbi']->fetchResult($query . ") as alias"); } else { $query = "SELECT `SCHEMA_NAME` "; $query .= "FROM `INFORMATION_SCHEMA`.`SCHEMATA` "; $query .= $this->_getWhereClause($searchClause); $query .= "ORDER BY `SCHEMA_NAME` ASC "; $query .= "LIMIT $pos, {$GLOBALS['cfg']['MaxNavigationItems']}"; return $GLOBALS['dbi']->fetchResult($query); } } /** * Returns the number of children of type $type present inside this container * This method is overridden by the Node_Database and Node_Table classes * * @param string $type The type of item we are looking for * ('tables', 'views', etc) * @param string $searchClause A string used to filter the results of the query * * @return int */ public function getPresence($type = '', $searchClause = '') { $query = "SELECT COUNT(*) "; $query .= "FROM `INFORMATION_SCHEMA`.`SCHEMATA` "; $query .= $this->_getWhereClause($searchClause); $retval = (int)$GLOBALS['dbi']->fetchValue($query); return $retval; } /** * Returns the WHERE clause depending on the $searchClause parameter * and the hide_db directive * * @param string $searchClause A string used to filter the results of the query * * @return string */ private function _getWhereClause($searchClause = '') { $whereClause = "WHERE TRUE "; if (! empty($searchClause)) { $whereClause .= "AND `SCHEMA_NAME` LIKE '%"; $whereClause .= PMA_Util::sqlAddSlashes( $searchClause, true ); $whereClause .= "%' "; } if (! empty($GLOBALS['cfg']['Server']['hide_db'])) { $whereClause .= "AND `SCHEMA_NAME` NOT REGEXP '" . $GLOBALS['cfg']['Server']['hide_db'] . "' "; } if (! empty($GLOBALS['cfg']['Server']['only_db'])) { if (is_string($GLOBALS['cfg']['Server']['only_db'])) { $GLOBALS['cfg']['Server']['only_db'] = array( $GLOBALS['cfg']['Server']['only_db'] ); } $whereClause .= "AND ("; $subClauses = array(); foreach ($GLOBALS['cfg']['Server']['only_db'] as $each_only_db) { $subClauses[] = " `SCHEMA_NAME` LIKE '" . $each_only_db . "' "; } $whereClause .= implode("OR", $subClauses) . ")"; } return $whereClause; } /** * Returns HTML for control buttons displayed infront of a node * * @return String HTML for control buttons */ public function getHtmlForControlButtons() { return ''; } } ?>