Translated using Weblate (Slovenian)
[phpmyadmin.git] / libraries / ListDatabase.php
blob35e03b673ee85c64379ef6fcf47238f647e9237f
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * holds the ListDatabase class
6 * @package PhpMyAdmin
7 */
8 namespace PMA\libraries;
10 require_once './libraries/check_user_privileges.lib.php';
12 /**
13 * handles database lists
15 * <code>
16 * $ListDatabase = new ListDatabase($userlink);
17 * </code>
19 * @todo this object should be attached to the PMA_Server object
21 * @package PhpMyAdmin
22 * @since phpMyAdmin 2.9.10
24 class ListDatabase extends ListAbstract
26 /**
27 * @var mixed database link resource|object to be used
28 * @access protected
30 protected $db_link = null;
32 /**
33 * @var mixed user database link resource|object
34 * @access protected
36 protected $db_link_user = null;
38 /**
39 * Constructor
41 * @param mixed $db_link_user user database link resource|object
43 public function __construct($db_link_user = null)
45 $this->db_link = $db_link_user;
46 $this->db_link_user = $db_link_user;
48 parent::__construct();
49 $this->build();
52 /**
53 * checks if the configuration wants to hide some databases
55 * @return void
57 protected function checkHideDatabase()
59 if (empty($GLOBALS['cfg']['Server']['hide_db'])) {
60 return;
63 foreach ($this->getArrayCopy() as $key => $db) {
64 if (preg_match('/' . $GLOBALS['cfg']['Server']['hide_db'] . '/', $db)) {
65 $this->offsetUnset($key);
70 /**
71 * retrieves database list from server
73 * @param string $like_db_name usually a db_name containing wildcards
75 * @return array
77 protected function retrieve($like_db_name = null)
79 $database_list = array();
80 $command = "";
81 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
82 $command .= "SELECT `SCHEMA_NAME` FROM `INFORMATION_SCHEMA`.`SCHEMATA`";
83 if (null !== $like_db_name) {
84 $command .= " WHERE `SCHEMA_NAME` LIKE '" . $like_db_name . "'";
86 } else {
87 if ($GLOBALS['dbs_to_test'] === false || null !== $like_db_name) {
88 $command .= "SHOW DATABASES";
89 if (null !== $like_db_name) {
90 $command .= " LIKE '" . $like_db_name . "'";
92 } else {
93 foreach ($GLOBALS['dbs_to_test'] as $db) {
94 $database_list = array_merge(
95 $database_list, $this->retrieve($db)
101 if ($command) {
102 $database_list = $GLOBALS['dbi']->fetchResult(
103 $command, null, null, $this->db_link
107 if ($GLOBALS['cfg']['NaturalOrder']) {
108 natsort($database_list);
109 } else {
110 // need to sort anyway, otherwise information_schema
111 // goes at the top
112 sort($database_list);
115 return $database_list;
119 * builds up the list
121 * @return void
123 public function build()
125 if (! $this->checkOnlyDatabase()) {
126 $items = $this->retrieve();
127 $this->exchangeArray($items);
130 $this->checkHideDatabase();
134 * checks the only_db configuration
136 * @return boolean false if there is no only_db, otherwise true
138 protected function checkOnlyDatabase()
140 if (is_string($GLOBALS['cfg']['Server']['only_db'])
141 && strlen($GLOBALS['cfg']['Server']['only_db']) > 0
143 $GLOBALS['cfg']['Server']['only_db'] = array(
144 $GLOBALS['cfg']['Server']['only_db']
148 if (! is_array($GLOBALS['cfg']['Server']['only_db'])) {
149 return false;
152 $items = array();
154 foreach ($GLOBALS['cfg']['Server']['only_db'] as $each_only_db) {
156 // check if the db name contains wildcard,
157 // thus containing not escaped _ or %
158 if (! preg_match('/(^|[^\\\\])(_|%)/', $each_only_db)) {
159 // ... not contains wildcard
160 $items[] = Util::unescapeMysqlWildcards($each_only_db);
161 continue;
164 $items = array_merge($items, $this->retrieve($each_only_db));
167 $this->exchangeArray($items);
169 return true;
173 * returns default item
175 * @return string default item
177 public function getDefault()
179 if (strlen($GLOBALS['db']) > 0) {
180 return $GLOBALS['db'];
183 return $this->getEmpty();