Translated using Weblate (Czech)
[phpmyadmin.git] / libraries / classes / ListDatabase.php
blobab44de963a49f3dd9fbbd27aaabbb019ff2fa3cc
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * holds the ListDatabase class
6 * @package PhpMyAdmin
7 */
8 namespace PhpMyAdmin;
10 use PhpMyAdmin\ListAbstract;
11 use PhpMyAdmin\Util;
13 require_once './libraries/check_user_privileges.inc.php';
15 /**
16 * handles database lists
18 * <code>
19 * $ListDatabase = new ListDatabase();
20 * </code>
22 * @todo this object should be attached to the PMA_Server object
24 * @package PhpMyAdmin
25 * @since phpMyAdmin 2.9.10
27 class ListDatabase extends ListAbstract
29 /**
30 * Constructor
32 public function __construct()
34 parent::__construct();
35 $this->build();
38 /**
39 * checks if the configuration wants to hide some databases
41 * @return void
43 protected function checkHideDatabase()
45 if (empty($GLOBALS['cfg']['Server']['hide_db'])) {
46 return;
49 foreach ($this->getArrayCopy() as $key => $db) {
50 if (preg_match('/' . $GLOBALS['cfg']['Server']['hide_db'] . '/', $db)) {
51 $this->offsetUnset($key);
56 /**
57 * retrieves database list from server
59 * @param string $like_db_name usually a db_name containing wildcards
61 * @return array
63 protected function retrieve($like_db_name = null)
65 $database_list = array();
66 $command = "";
67 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
68 $command .= "SELECT `SCHEMA_NAME` FROM `INFORMATION_SCHEMA`.`SCHEMATA`";
69 if (null !== $like_db_name) {
70 $command .= " WHERE `SCHEMA_NAME` LIKE '" . $like_db_name . "'";
72 } else {
73 if ($GLOBALS['dbs_to_test'] === false || null !== $like_db_name) {
74 $command .= "SHOW DATABASES";
75 if (null !== $like_db_name) {
76 $command .= " LIKE '" . $like_db_name . "'";
78 } else {
79 foreach ($GLOBALS['dbs_to_test'] as $db) {
80 $database_list = array_merge(
81 $database_list, $this->retrieve($db)
87 if ($command) {
88 $database_list = $GLOBALS['dbi']->fetchResult(
89 $command, null, null
93 if ($GLOBALS['cfg']['NaturalOrder']) {
94 usort($database_list, 'strnatcasecmp');
95 } else {
96 // need to sort anyway, otherwise information_schema
97 // goes at the top
98 sort($database_list);
101 return $database_list;
105 * builds up the list
107 * @return void
109 public function build()
111 if (! $this->checkOnlyDatabase()) {
112 $items = $this->retrieve();
113 $this->exchangeArray($items);
116 $this->checkHideDatabase();
120 * checks the only_db configuration
122 * @return boolean false if there is no only_db, otherwise true
124 protected function checkOnlyDatabase()
126 if (is_string($GLOBALS['cfg']['Server']['only_db'])
127 && strlen($GLOBALS['cfg']['Server']['only_db']) > 0
129 $GLOBALS['cfg']['Server']['only_db'] = array(
130 $GLOBALS['cfg']['Server']['only_db']
134 if (! is_array($GLOBALS['cfg']['Server']['only_db'])) {
135 return false;
138 $items = array();
140 foreach ($GLOBALS['cfg']['Server']['only_db'] as $each_only_db) {
142 // check if the db name contains wildcard,
143 // thus containing not escaped _ or %
144 if (! preg_match('/(^|[^\\\\])(_|%)/', $each_only_db)) {
145 // ... not contains wildcard
146 $items[] = Util::unescapeMysqlWildcards($each_only_db);
147 continue;
150 $items = array_merge($items, $this->retrieve($each_only_db));
153 $this->exchangeArray($items);
155 return true;
159 * returns default item
161 * @return string default item
163 public function getDefault()
165 if (strlen($GLOBALS['db']) > 0) {
166 return $GLOBALS['db'];
169 return $this->getEmpty();