2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * holds the ListDatabase class
8 namespace PMA\libraries
;
10 require_once './libraries/check_user_privileges.lib.php';
13 * handles database lists
16 * $ListDatabase = new ListDatabase($userlink);
19 * @todo this object should be attached to the PMA_Server object
22 * @since phpMyAdmin 2.9.10
24 class ListDatabase
extends ListAbstract
27 * @var mixed database link resource|object to be used
30 protected $db_link = null;
33 * @var mixed user database link resource|object
36 protected $db_link_user = null;
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();
53 * checks if the configuration wants to hide some databases
57 protected function checkHideDatabase()
59 if (empty($GLOBALS['cfg']['Server']['hide_db'])) {
63 foreach ($this->getArrayCopy() as $key => $db) {
64 if (preg_match('/' . $GLOBALS['cfg']['Server']['hide_db'] . '/', $db)) {
65 $this->offsetUnset($key);
71 * retrieves database list from server
73 * @param string $like_db_name usually a db_name containing wildcards
77 protected function retrieve($like_db_name = null)
79 $database_list = array();
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 . "'";
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 . "'";
93 foreach ($GLOBALS['dbs_to_test'] as $db) {
94 $database_list = array_merge(
95 $database_list, $this->retrieve($db)
102 $database_list = $GLOBALS['dbi']->fetchResult(
103 $command, null, null, $this->db_link
107 if ($GLOBALS['cfg']['NaturalOrder']) {
108 usort($database_list, 'strnatcasecmp');
110 // need to sort anyway, otherwise information_schema
112 sort($database_list);
115 return $database_list;
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'])) {
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);
164 $items = array_merge($items, $this->retrieve($each_only_db));
167 $this->exchangeArray($items);
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();