2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * holds the ListDatabase class
8 declare(strict_types
=1);
12 use PhpMyAdmin\ListAbstract
;
16 * handles database lists
19 * $ListDatabase = new ListDatabase();
22 * @todo this object should be attached to the PMA_Server object
25 * @since phpMyAdmin 2.9.10
27 class ListDatabase
extends ListAbstract
32 public function __construct()
34 parent
::__construct();
36 $checkUserPrivileges = new CheckUserPrivileges($GLOBALS['dbi']);
37 $checkUserPrivileges->getPrivileges();
43 * checks if the configuration wants to hide some databases
47 protected function checkHideDatabase()
49 if (empty($GLOBALS['cfg']['Server']['hide_db'])) {
53 foreach ($this->getArrayCopy() as $key => $db) {
54 if (preg_match('/' . $GLOBALS['cfg']['Server']['hide_db'] . '/', $db)) {
55 $this->offsetUnset($key);
61 * retrieves database list from server
63 * @param string $like_db_name usually a db_name containing wildcards
67 protected function retrieve($like_db_name = null)
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 . "'";
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 . "'";
83 foreach ($GLOBALS['dbs_to_test'] as $db) {
84 $database_list = array_merge(
93 $database_list = $GLOBALS['dbi']->fetchResult(
100 if ($GLOBALS['cfg']['NaturalOrder']) {
101 usort($database_list, 'strnatcasecmp');
103 // need to sort anyway, otherwise information_schema
105 sort($database_list);
108 return $database_list;
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'])) {
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);
156 $items = array_merge($items, $this->retrieve($each_only_db));
159 $this->exchangeArray($items);
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();