2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Functionality for the navigation tree
6 * @package PhpMyAdmin-Navigation
8 if (! defined('PHPMYADMIN')) {
13 * Represents a database node in the navigation tree
15 * @package PhpMyAdmin-Navigation
17 class Node_Database
extends Node
20 * The number of hidden items in this database
24 protected $hiddenCount = 0;
27 * Initialises the class
29 * @param string $name An identifier for the new node
30 * @param int $type Type of node, may be one of CONTAINER or OBJECT
31 * @param bool $is_group Whether this object has been created
32 * while grouping nodes
34 public function __construct($name, $type = Node
::OBJECT, $is_group = false)
36 parent
::__construct($name, $type, $is_group);
37 $this->icon
= PMA_Util
::getImage(
39 __('Database operations')
42 $script_name = PMA_Util
::getScriptNameForOption(
43 $GLOBALS['cfg']['DefaultTabDatabase'], 'database'
46 'text' => $script_name
47 . '?server=' . $GLOBALS['server']
48 . '&db=%1$s&token=' . $_SESSION[' PMA_token '],
49 'icon' => 'db_operations.php?server=' . $GLOBALS['server']
50 . '&db=%1$s&token=' . $_SESSION[' PMA_token '],
51 'title' => __('Structure')
53 $this->classes
= 'database';
57 * Returns the number of children of type $type present inside this container
58 * This method is overridden by the Node_Database and Node_Table classes
60 * @param string $type The type of item we are looking for
61 * ('tables', 'views', etc)
62 * @param string $searchClause A string used to filter the results of
64 * @param boolean $singleItem Whether to get presence of a single known
65 * item or false in none
69 public function getPresence($type = '', $searchClause = '', $singleItem = false)
74 $retval = $this->_getTableCount($searchClause, $singleItem);
77 $retval = $this->_getViewCount($searchClause, $singleItem);
80 $retval = $this->_getProcedureCount($searchClause, $singleItem);
83 $retval = $this->_getFunctionCount($searchClause, $singleItem);
86 $retval = $this->_getEventCount($searchClause, $singleItem);
95 * Returns the number of tables or views present inside this database
97 * @param string $which tables|views
98 * @param string $searchClause A string used to filter the results of
100 * @param boolean $singleItem Whether to get presence of a single known
101 * item or false in none
105 private function _getTableOrViewCount($which, $searchClause, $singleItem)
107 $db = $this->real_name
;
108 if ($which == 'tables') {
114 if (! $GLOBALS['cfg']['Server']['DisableIS'] || PMA_DRIZZLE
) {
115 $db = PMA_Util
::sqlAddSlashes($db);
116 $query = "SELECT COUNT(*) ";
117 $query .= "FROM `INFORMATION_SCHEMA`.`TABLES` ";
118 $query .= "WHERE `TABLE_SCHEMA`='$db' ";
120 $query .= "AND `TABLE_TYPE`" . $condition . "'BASE' ";
122 $query .= "AND `TABLE_TYPE`" . $condition . "'BASE TABLE' ";
124 if (! empty($searchClause)) {
125 $query .= "AND " . $this->_getWhereClauseForSearch(
126 $searchClause, $singleItem, 'TABLE_NAME'
129 $retval = (int)$GLOBALS['dbi']->fetchValue($query);
131 $query = "SHOW FULL TABLES FROM ";
132 $query .= PMA_Util
::backquote($db);
133 $query .= " WHERE `Table_type`" . $condition . "'BASE TABLE' ";
134 if (! empty($searchClause)) {
135 $query .= "AND " . $this->_getWhereClauseForSearch(
136 $searchClause, $singleItem, 'Tables_in_' . $db
139 $retval = $GLOBALS['dbi']->numRows(
140 $GLOBALS['dbi']->tryQuery($query)
147 * Returns the number of tables present inside this database
149 * @param string $searchClause A string used to filter the results of
151 * @param boolean $singleItem Whether to get presence of a single known
152 * item or false in none
156 private function _getTableCount($searchClause, $singleItem)
158 return $this->_getTableOrViewCount(
159 'tables', $searchClause, $singleItem
164 * Returns the number of views present inside this database
166 * @param string $searchClause A string used to filter the results of
168 * @param boolean $singleItem Whether to get presence of a single known
169 * item or false in none
173 private function _getViewCount($searchClause, $singleItem)
175 return $this->_getTableOrViewCount(
176 'views', $searchClause, $singleItem
181 * Returns the number of procedures present inside this database
183 * @param string $searchClause A string used to filter the results of
185 * @param boolean $singleItem Whether to get presence of a single known
186 * item or false in none
190 private function _getProcedureCount($searchClause, $singleItem)
192 $db = $this->real_name
;
193 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
194 $db = PMA_Util
::sqlAddSlashes($db);
195 $query = "SELECT COUNT(*) ";
196 $query .= "FROM `INFORMATION_SCHEMA`.`ROUTINES` ";
197 $query .= "WHERE `ROUTINE_SCHEMA` "
198 . PMA_Util
::getCollateForIS() . "='$db'";
199 $query .= "AND `ROUTINE_TYPE`='PROCEDURE' ";
200 if (! empty($searchClause)) {
201 $query .= "AND " . $this->_getWhereClauseForSearch(
202 $searchClause, $singleItem, 'ROUTINE_NAME'
205 $retval = (int)$GLOBALS['dbi']->fetchValue($query);
207 $db = PMA_Util
::sqlAddSlashes($db);
208 $query = "SHOW PROCEDURE STATUS WHERE `Db`='$db' ";
209 if (! empty($searchClause)) {
210 $query .= "AND " . $this->_getWhereClauseForSearch(
211 $searchClause, $singleItem, 'Name'
214 $retval = $GLOBALS['dbi']->numRows(
215 $GLOBALS['dbi']->tryQuery($query)
222 * Returns the number of functions present inside this database
224 * @param string $searchClause A string used to filter the results of
226 * @param boolean $singleItem Whether to get presence of a single known
227 * item or false in none
231 private function _getFunctionCount($searchClause, $singleItem)
233 $db = $this->real_name
;
234 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
235 $db = PMA_Util
::sqlAddSlashes($db);
236 $query = "SELECT COUNT(*) ";
237 $query .= "FROM `INFORMATION_SCHEMA`.`ROUTINES` ";
238 $query .= "WHERE `ROUTINE_SCHEMA` "
239 . PMA_Util
::getCollateForIS() . "='$db' ";
240 $query .= "AND `ROUTINE_TYPE`='FUNCTION' ";
241 if (! empty($searchClause)) {
242 $query .= "AND " . $this->_getWhereClauseForSearch(
243 $searchClause, $singleItem, 'ROUTINE_NAME'
246 $retval = (int)$GLOBALS['dbi']->fetchValue($query);
248 $db = PMA_Util
::sqlAddSlashes($db);
249 $query = "SHOW FUNCTION STATUS WHERE `Db`='$db' ";
250 if (! empty($searchClause)) {
251 $query .= "AND " . $this->_getWhereClauseForSearch(
252 $searchClause, $singleItem, 'Name'
255 $retval = $GLOBALS['dbi']->numRows(
256 $GLOBALS['dbi']->tryQuery($query)
263 * Returns the number of events present inside this database
265 * @param string $searchClause A string used to filter the results of
267 * @param boolean $singleItem Whether to get presence of a single known
268 * item or false in none
272 private function _getEventCount($searchClause, $singleItem)
274 $db = $this->real_name
;
275 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
276 $db = PMA_Util
::sqlAddSlashes($db);
277 $query = "SELECT COUNT(*) ";
278 $query .= "FROM `INFORMATION_SCHEMA`.`EVENTS` ";
279 $query .= "WHERE `EVENT_SCHEMA` "
280 . PMA_Util
::getCollateForIS() . "='$db' ";
281 if (! empty($searchClause)) {
282 $query .= "AND " . $this->_getWhereClauseForSearch(
283 $searchClause, $singleItem, 'EVENT_NAME'
286 $retval = (int)$GLOBALS['dbi']->fetchValue($query);
288 $db = PMA_Util
::backquote($db);
289 $query = "SHOW EVENTS FROM $db ";
290 if (! empty($searchClause)) {
291 $query .= "WHERE " . $this->_getWhereClauseForSearch(
292 $searchClause, $singleItem, 'Name'
295 $retval = $GLOBALS['dbi']->numRows(
296 $GLOBALS['dbi']->tryQuery($query)
303 * Returns the WHERE clause for searching inside a database
305 * @param string $searchClause A string used to filter the results of the query
306 * @param boolean $singleItem Whether to get presence of a single known item
307 * @param string $columnName Name of the column in the result set to match
309 * @return string WHERE clause for searching
311 private function _getWhereClauseForSearch(
312 $searchClause, $singleItem, $columnName
316 $query .= PMA_Util
::backquote($columnName) . " = ";
317 $query .= "'" . PMA_Util
::sqlAddSlashes($searchClause) . "'";
319 $query .= PMA_Util
::backquote($columnName) . " LIKE ";
320 $query .= "'%" . PMA_Util
::sqlAddSlashes($searchClause, true) . "%'";
326 * Returns the names of children of type $type present inside this container
327 * This method is overridden by the Node_Database and Node_Table classes
329 * @param string $type The type of item we are looking for
330 * ('tables', 'views', etc)
331 * @param int $pos The offset of the list within the results
332 * @param string $searchClause A string used to filter the results of the query
336 public function getData($type, $pos, $searchClause = '')
341 $retval = $this->_getTables($pos, $searchClause);
344 $retval = $this->_getViews($pos, $searchClause);
347 $retval = $this->_getProcedures($pos, $searchClause);
350 $retval = $this->_getFunctions($pos, $searchClause);
353 $retval = $this->_getEvents($pos, $searchClause);
359 // Remove hidden items so that they are not displayed in navigation tree
360 $cfgRelation = PMA_getRelationsParam();
361 if ($cfgRelation['navwork']) {
362 $hiddenItems = $this->getHiddenItems(substr($type, 0, -1));
363 foreach ($retval as $key => $item) {
364 if (in_array($item, $hiddenItems)) {
365 unset($retval[$key]);
374 * Return list of hidden items of given type
376 * @param string $type The type of items we are looking for
377 * ('table', 'function', 'group', etc.)
379 * @return array Array containing hidden items of given type
381 public function getHiddenItems($type)
383 $db = $this->real_name
;
384 $cfgRelation = PMA_getRelationsParam();
385 if (empty($cfgRelation['navigationhiding'])) {
388 $navTable = PMA_Util
::backquote($cfgRelation['db'])
389 . "." . PMA_Util
::backquote($cfgRelation['navigationhiding']);
390 $sqlQuery = "SELECT `item_name` FROM " . $navTable
391 . " WHERE `username`='" . $cfgRelation['user'] . "'"
392 . " AND `item_type`='" . $type
393 . "'" . " AND `db_name`='" . PMA_Util
::sqlAddSlashes($db) . "'";
394 $result = PMA_queryAsControlUser($sqlQuery, false);
395 $hiddenItems = array();
397 while ($row = $GLOBALS['dbi']->fetchArray($result)) {
398 $hiddenItems[] = $row[0];
401 $GLOBALS['dbi']->freeResult($result);
406 * Returns the list of tables or views inside this database
408 * @param string $which tables|views
409 * @param int $pos The offset of the list within the results
410 * @param string $searchClause A string used to filter the results of the query
414 private function _getTablesOrViews($which, $pos, $searchClause)
416 if ($which == 'tables') {
421 $maxItems = $GLOBALS['cfg']['MaxNavigationItems'];
423 $db = $this->real_name
;
424 if (! $GLOBALS['cfg']['Server']['DisableIS'] || PMA_DRIZZLE
) {
425 $escdDb = PMA_Util
::sqlAddSlashes($db);
426 $query = "SELECT `TABLE_NAME` AS `name` ";
427 $query .= "FROM `INFORMATION_SCHEMA`.`TABLES` ";
428 $query .= "WHERE `TABLE_SCHEMA`='$escdDb' ";
430 $query .= "AND `TABLE_TYPE`" . $condition . "'BASE' ";
432 $query .= "AND `TABLE_TYPE`" . $condition . "'BASE TABLE' ";
434 if (! empty($searchClause)) {
435 $query .= "AND `TABLE_NAME` LIKE '%";
436 $query .= PMA_Util
::sqlAddSlashes(
441 $query .= "ORDER BY `TABLE_NAME` ASC ";
442 $query .= "LIMIT " . intval($pos) . ", $maxItems";
443 $retval = $GLOBALS['dbi']->fetchResult($query);
445 $query = " SHOW FULL TABLES FROM ";
446 $query .= PMA_Util
::backquote($db);
447 $query .= " WHERE `Table_type`" . $condition . "'BASE TABLE' ";
448 if (! empty($searchClause)) {
449 $query .= "AND " . PMA_Util
::backquote(
452 $query .= " LIKE '%" . PMA_Util
::sqlAddSlashes(
457 $handle = $GLOBALS['dbi']->tryQuery($query);
458 if ($handle !== false) {
460 if ($GLOBALS['dbi']->dataSeek($handle, $pos)) {
461 while ($arr = $GLOBALS['dbi']->fetchArray($handle)) {
462 if ($count < $maxItems) {
476 * Returns the list of tables inside this database
478 * @param int $pos The offset of the list within the results
479 * @param string $searchClause A string used to filter the results of the query
483 private function _getTables($pos, $searchClause)
485 return $this->_getTablesOrViews('tables', $pos, $searchClause);
489 * Returns the list of views inside this database
491 * @param int $pos The offset of the list within the results
492 * @param string $searchClause A string used to filter the results of the query
496 private function _getViews($pos, $searchClause)
498 return $this->_getTablesOrViews('views', $pos, $searchClause);
502 * Returns the list of procedures or functions inside this database
504 * @param string $routineType PROCEDURE|FUNCTION
505 * @param int $pos The offset of the list within the results
506 * @param string $searchClause A string used to filter the results of the query
510 private function _getRoutines($routineType, $pos, $searchClause)
512 $maxItems = $GLOBALS['cfg']['MaxNavigationItems'];
514 $db = $this->real_name
;
515 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
516 $escdDb = PMA_Util
::sqlAddSlashes($db);
517 $query = "SELECT `ROUTINE_NAME` AS `name` ";
518 $query .= "FROM `INFORMATION_SCHEMA`.`ROUTINES` ";
519 $query .= "WHERE `ROUTINE_SCHEMA` "
520 . PMA_Util
::getCollateForIS() . "='$escdDb'";
521 $query .= "AND `ROUTINE_TYPE`='" . $routineType . "' ";
522 if (! empty($searchClause)) {
523 $query .= "AND `ROUTINE_NAME` LIKE '%";
524 $query .= PMA_Util
::sqlAddSlashes(
529 $query .= "ORDER BY `ROUTINE_NAME` ASC ";
530 $query .= "LIMIT " . intval($pos) . ", $maxItems";
531 $retval = $GLOBALS['dbi']->fetchResult($query);
533 $escdDb = PMA_Util
::sqlAddSlashes($db);
534 $query = "SHOW " . $routineType . " STATUS WHERE `Db`='$escdDb' ";
535 if (! empty($searchClause)) {
536 $query .= "AND `Name` LIKE '%";
537 $query .= PMA_Util
::sqlAddSlashes(
542 $handle = $GLOBALS['dbi']->tryQuery($query);
543 if ($handle !== false) {
545 if ($GLOBALS['dbi']->dataSeek($handle, $pos)) {
546 while ($arr = $GLOBALS['dbi']->fetchArray($handle)) {
547 if ($count < $maxItems) {
548 $retval[] = $arr['Name'];
561 * Returns the list of procedures inside this database
563 * @param int $pos The offset of the list within the results
564 * @param string $searchClause A string used to filter the results of the query
568 private function _getProcedures($pos, $searchClause)
570 return $this->_getRoutines('PROCEDURE', $pos, $searchClause);
574 * Returns the list of functions inside this database
576 * @param int $pos The offset of the list within the results
577 * @param string $searchClause A string used to filter the results of the query
581 private function _getFunctions($pos, $searchClause)
583 return $this->_getRoutines('FUNCTION', $pos, $searchClause);
587 * Returns the list of events inside this database
589 * @param int $pos The offset of the list within the results
590 * @param string $searchClause A string used to filter the results of the query
594 private function _getEvents($pos, $searchClause)
596 $maxItems = $GLOBALS['cfg']['MaxNavigationItems'];
598 $db = $this->real_name
;
599 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
600 $escdDb = PMA_Util
::sqlAddSlashes($db);
601 $query = "SELECT `EVENT_NAME` AS `name` ";
602 $query .= "FROM `INFORMATION_SCHEMA`.`EVENTS` ";
603 $query .= "WHERE `EVENT_SCHEMA` "
604 . PMA_Util
::getCollateForIS() . "='$escdDb' ";
605 if (! empty($searchClause)) {
606 $query .= "AND `EVENT_NAME` LIKE '%";
607 $query .= PMA_Util
::sqlAddSlashes(
612 $query .= "ORDER BY `EVENT_NAME` ASC ";
613 $query .= "LIMIT " . intval($pos) . ", $maxItems";
614 $retval = $GLOBALS['dbi']->fetchResult($query);
616 $escdDb = PMA_Util
::backquote($db);
617 $query = "SHOW EVENTS FROM $escdDb ";
618 if (! empty($searchClause)) {
619 $query .= "WHERE `Name` LIKE '%";
620 $query .= PMA_Util
::sqlAddSlashes(
625 $handle = $GLOBALS['dbi']->tryQuery($query);
626 if ($handle !== false) {
628 if ($GLOBALS['dbi']->dataSeek($handle, $pos)) {
629 while ($arr = $GLOBALS['dbi']->fetchArray($handle)) {
630 if ($count < $maxItems) {
631 $retval[] = $arr['Name'];
644 * Returns HTML for control buttons displayed infront of a node
646 * @return String HTML for control buttons
648 public function getHtmlForControlButtons()
651 $cfgRelation = PMA_getRelationsParam();
652 if ($cfgRelation['navwork']) {
653 if ($this->hiddenCount
> 0) {
654 $ret = '<span class="dbItemControls">'
655 . '<a href="navigation.php'
656 . PMA_URL_getCommon()
657 . '&showUnhideDialog=true'
658 . '&dbName=' . urldecode($this->real_name
) . '"'
659 . ' class="showUnhide ajax">'
660 . PMA_Util
::getImage(
661 'lightbulb.png', __('Show hidden items')
671 * Sets the number of hidden items in this database
673 * @param int $count hidden item count
677 public function setHiddenCount($count)
679 $this->hiddenCount
= $count;
683 * Returns the number of hidden items in this database
685 * @return int hidden item count
687 public function getHiddenCount()
689 return $this->hiddenCount
;