Translated using Weblate (Estonian)
[phpmyadmin.git] / libraries / classes / ListDatabase.php
blobe65120e55ff689726e7430b3f3eb73c32251c78c
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * holds the ListDatabase class
6 * @package PhpMyAdmin
7 */
8 declare(strict_types=1);
10 namespace PhpMyAdmin;
12 use PhpMyAdmin\ListAbstract;
13 use PhpMyAdmin\Util;
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();
36 $checkUserPrivileges = new CheckUserPrivileges($GLOBALS['dbi']);
37 $checkUserPrivileges->getPrivileges();
39 $this->build();
42 /**
43 * checks if the configuration wants to hide some databases
45 * @return void
47 protected function checkHideDatabase()
49 if (empty($GLOBALS['cfg']['Server']['hide_db'])) {
50 return;
53 foreach ($this->getArrayCopy() as $key => $db) {
54 if (preg_match('/' . $GLOBALS['cfg']['Server']['hide_db'] . '/', $db)) {
55 $this->offsetUnset($key);
60 /**
61 * retrieves database list from server
63 * @param string $like_db_name usually a db_name containing wildcards
65 * @return array
67 protected function retrieve($like_db_name = null)
69 $database_list = [];
70 $command = "";
71 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
72 $command .= "SELECT `SCHEMA_NAME` FROM `INFORMATION_SCHEMA`.`SCHEMATA`";
73 if (null !== $like_db_name) {
74 $command .= " WHERE `SCHEMA_NAME` LIKE '" . $like_db_name . "'";
76 } else {
77 if ($GLOBALS['dbs_to_test'] === false || null !== $like_db_name) {
78 $command .= "SHOW DATABASES";
79 if (null !== $like_db_name) {
80 $command .= " LIKE '" . $like_db_name . "'";
82 } else {
83 foreach ($GLOBALS['dbs_to_test'] as $db) {
84 $database_list = array_merge(
85 $database_list,
86 $this->retrieve($db)
92 if ($command) {
93 $database_list = $GLOBALS['dbi']->fetchResult(
94 $command,
95 null,
96 null
100 if ($GLOBALS['cfg']['NaturalOrder']) {
101 usort($database_list, 'strnatcasecmp');
102 } else {
103 // need to sort anyway, otherwise information_schema
104 // goes at the top
105 sort($database_list);
108 return $database_list;
112 * builds up the list
114 * @return void
116 public function build()
118 if (! $this->checkOnlyDatabase()) {
119 $items = $this->retrieve();
120 $this->exchangeArray($items);
123 $this->checkHideDatabase();
127 * checks the only_db configuration
129 * @return boolean false if there is no only_db, otherwise true
131 protected function checkOnlyDatabase()
133 if (is_string($GLOBALS['cfg']['Server']['only_db'])
134 && strlen($GLOBALS['cfg']['Server']['only_db']) > 0
136 $GLOBALS['cfg']['Server']['only_db'] = [
137 $GLOBALS['cfg']['Server']['only_db'],
141 if (! is_array($GLOBALS['cfg']['Server']['only_db'])) {
142 return false;
145 $items = [];
147 foreach ($GLOBALS['cfg']['Server']['only_db'] as $each_only_db) {
148 // check if the db name contains wildcard,
149 // thus containing not escaped _ or %
150 if (! preg_match('/(^|[^\\\\])(_|%)/', $each_only_db)) {
151 // ... not contains wildcard
152 $items[] = Util::unescapeMysqlWildcards($each_only_db);
153 continue;
156 $items = array_merge($items, $this->retrieve($each_only_db));
159 $this->exchangeArray($items);
161 return true;
165 * returns default item
167 * @return string default item
169 public function getDefault()
171 if (strlen($GLOBALS['db']) > 0) {
172 return $GLOBALS['db'];
175 return $this->getEmpty();