Removing old documentation
[openemr.git] / phpmyadmin / libraries / navigation / Nodes / Node_Database.class.php
blobc564dc6679cf94a190cb15e01ce67ab002a66a72
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Functionality for the navigation tree
6 * @package PhpMyAdmin-Navigation
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
13 * Represents a database node in the navigation tree
15 * @package PhpMyAdmin-Navigation
17 class Node_Database extends Node
19 /**
20 * The number of hidden items in this database
22 * @var int
24 protected $hiddenCount = 0;
26 /**
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(
38 's_db.png',
39 __('Database operations')
42 $script_name = PMA_Util::getScriptNameForOption(
43 $GLOBALS['cfg']['DefaultTabDatabase'], 'database'
45 $this->links = array(
46 'text' => $script_name
47 . '?server=' . $GLOBALS['server']
48 . '&amp;db=%1$s&amp;token=' . $_SESSION[' PMA_token '],
49 'icon' => 'db_operations.php?server=' . $GLOBALS['server']
50 . '&amp;db=%1$s&amp;token=' . $_SESSION[' PMA_token '],
51 'title' => __('Structure')
53 $this->classes = 'database';
56 /**
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
63 * the query
64 * @param boolean $singleItem Whether to get presence of a single known
65 * item or false in none
67 * @return int
69 public function getPresence($type = '', $searchClause = '', $singleItem = false)
71 $retval = 0;
72 switch ($type) {
73 case 'tables':
74 $retval = $this->_getTableCount($searchClause, $singleItem);
75 break;
76 case 'views':
77 $retval = $this->_getViewCount($searchClause, $singleItem);
78 break;
79 case 'procedures':
80 $retval = $this->_getProcedureCount($searchClause, $singleItem);
81 break;
82 case 'functions':
83 $retval = $this->_getFunctionCount($searchClause, $singleItem);
84 break;
85 case 'events':
86 $retval = $this->_getEventCount($searchClause, $singleItem);
87 break;
88 default:
89 break;
91 return $retval;
94 /**
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
99 * the query
100 * @param boolean $singleItem Whether to get presence of a single known
101 * item or false in none
103 * @return int
105 private function _getTableOrViewCount($which, $searchClause, $singleItem)
107 $db = $this->real_name;
108 if ($which == 'tables') {
109 $condition = '=';
110 } else {
111 $condition = '!=';
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' ";
119 if (PMA_DRIZZLE) {
120 $query .= "AND `TABLE_TYPE`" . $condition . "'BASE' ";
121 } else {
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);
130 } else {
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)
143 return $retval;
147 * Returns the number of tables present inside this database
149 * @param string $searchClause A string used to filter the results of
150 * the query
151 * @param boolean $singleItem Whether to get presence of a single known
152 * item or false in none
154 * @return int
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
167 * the query
168 * @param boolean $singleItem Whether to get presence of a single known
169 * item or false in none
171 * @return int
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
184 * the query
185 * @param boolean $singleItem Whether to get presence of a single known
186 * item or false in none
188 * @return int
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);
206 } else {
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)
218 return $retval;
222 * Returns the number of functions present inside this database
224 * @param string $searchClause A string used to filter the results of
225 * the query
226 * @param boolean $singleItem Whether to get presence of a single known
227 * item or false in none
229 * @return int
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);
247 } else {
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)
259 return $retval;
263 * Returns the number of events present inside this database
265 * @param string $searchClause A string used to filter the results of
266 * the query
267 * @param boolean $singleItem Whether to get presence of a single known
268 * item or false in none
270 * @return int
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);
287 } else {
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)
299 return $retval;
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
314 $query = '';
315 if ($singleItem) {
316 $query .= PMA_Util::backquote($columnName) . " = ";
317 $query .= "'" . PMA_Util::sqlAddSlashes($searchClause) . "'";
318 } else {
319 $query .= PMA_Util::backquote($columnName) . " LIKE ";
320 $query .= "'%" . PMA_Util::sqlAddSlashes($searchClause, true) . "%'";
322 return $query;
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
334 * @return array
336 public function getData($type, $pos, $searchClause = '')
338 $retval = array();
339 switch ($type) {
340 case 'tables':
341 $retval = $this->_getTables($pos, $searchClause);
342 break;
343 case 'views':
344 $retval = $this->_getViews($pos, $searchClause);
345 break;
346 case 'procedures':
347 $retval = $this->_getProcedures($pos, $searchClause);
348 break;
349 case 'functions':
350 $retval = $this->_getFunctions($pos, $searchClause);
351 break;
352 case 'events':
353 $retval = $this->_getEvents($pos, $searchClause);
354 break;
355 default:
356 break;
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]);
370 return $retval;
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'])) {
386 return array();
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();
396 if ($result) {
397 while ($row = $GLOBALS['dbi']->fetchArray($result)) {
398 $hiddenItems[] = $row[0];
401 $GLOBALS['dbi']->freeResult($result);
402 return $hiddenItems;
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
412 * @return array
414 private function _getTablesOrViews($which, $pos, $searchClause)
416 if ($which == 'tables') {
417 $condition = '=';
418 } else {
419 $condition = '!=';
421 $maxItems = $GLOBALS['cfg']['MaxNavigationItems'];
422 $retval = array();
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' ";
429 if (PMA_DRIZZLE) {
430 $query .= "AND `TABLE_TYPE`" . $condition . "'BASE' ";
431 } else {
432 $query .= "AND `TABLE_TYPE`" . $condition . "'BASE TABLE' ";
434 if (! empty($searchClause)) {
435 $query .= "AND `TABLE_NAME` LIKE '%";
436 $query .= PMA_Util::sqlAddSlashes(
437 $searchClause, true
439 $query .= "%'";
441 $query .= "ORDER BY `TABLE_NAME` ASC ";
442 $query .= "LIMIT " . intval($pos) . ", $maxItems";
443 $retval = $GLOBALS['dbi']->fetchResult($query);
444 } else {
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(
450 "Tables_in_" . $db
452 $query .= " LIKE '%" . PMA_Util::sqlAddSlashes(
453 $searchClause, true
455 $query .= "%'";
457 $handle = $GLOBALS['dbi']->tryQuery($query);
458 if ($handle !== false) {
459 $count = 0;
460 if ($GLOBALS['dbi']->dataSeek($handle, $pos)) {
461 while ($arr = $GLOBALS['dbi']->fetchArray($handle)) {
462 if ($count < $maxItems) {
463 $retval[] = $arr[0];
464 $count++;
465 } else {
466 break;
472 return $retval;
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
481 * @return array
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
494 * @return array
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
508 * @return array
510 private function _getRoutines($routineType, $pos, $searchClause)
512 $maxItems = $GLOBALS['cfg']['MaxNavigationItems'];
513 $retval = array();
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(
525 $searchClause, true
527 $query .= "%'";
529 $query .= "ORDER BY `ROUTINE_NAME` ASC ";
530 $query .= "LIMIT " . intval($pos) . ", $maxItems";
531 $retval = $GLOBALS['dbi']->fetchResult($query);
532 } else {
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(
538 $searchClause, true
540 $query .= "%'";
542 $handle = $GLOBALS['dbi']->tryQuery($query);
543 if ($handle !== false) {
544 $count = 0;
545 if ($GLOBALS['dbi']->dataSeek($handle, $pos)) {
546 while ($arr = $GLOBALS['dbi']->fetchArray($handle)) {
547 if ($count < $maxItems) {
548 $retval[] = $arr['Name'];
549 $count++;
550 } else {
551 break;
557 return $retval;
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
566 * @return array
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
579 * @return array
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
592 * @return array
594 private function _getEvents($pos, $searchClause)
596 $maxItems = $GLOBALS['cfg']['MaxNavigationItems'];
597 $retval = array();
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(
608 $searchClause, true
610 $query .= "%'";
612 $query .= "ORDER BY `EVENT_NAME` ASC ";
613 $query .= "LIMIT " . intval($pos) . ", $maxItems";
614 $retval = $GLOBALS['dbi']->fetchResult($query);
615 } else {
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(
621 $searchClause, true
623 $query .= "%'";
625 $handle = $GLOBALS['dbi']->tryQuery($query);
626 if ($handle !== false) {
627 $count = 0;
628 if ($GLOBALS['dbi']->dataSeek($handle, $pos)) {
629 while ($arr = $GLOBALS['dbi']->fetchArray($handle)) {
630 if ($count < $maxItems) {
631 $retval[] = $arr['Name'];
632 $count++;
633 } else {
634 break;
640 return $retval;
644 * Returns HTML for control buttons displayed infront of a node
646 * @return String HTML for control buttons
648 public function getHtmlForControlButtons()
650 $ret = '';
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')
663 . '</a></span>';
667 return $ret;
671 * Sets the number of hidden items in this database
673 * @param int $count hidden item count
675 * @return void
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;