Merge branch 'QA_4_4' into QA_4_5
[phpmyadmin.git] / libraries / List_Database.class.php
blob57da2c385475b45076306158dfd9ff862f0c1bc1
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * holds the PMA_List_Database class
6 * @package PhpMyAdmin
7 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
14 * the list base class
16 require_once './libraries/List.class.php';
17 require_once './libraries/check_user_privileges.lib.php';
19 /**
20 * handles database lists
22 * <code>
23 * $PMA_List_Database = new PMA_List_Database($userlink);
24 * </code>
26 * @todo this object should be attached to the PMA_Server object
28 * @package PhpMyAdmin
29 * @since phpMyAdmin 2.9.10
31 class PMA_List_Database extends PMA_List
33 /**
34 * @var mixed database link resource|object to be used
35 * @access protected
37 protected $db_link = null;
39 /**
40 * @var mixed user database link resource|object
41 * @access protected
43 protected $db_link_user = null;
45 /**
46 * Constructor
48 * @param mixed $db_link_user user database link resource|object
50 public function __construct($db_link_user = null)
52 $this->db_link = $db_link_user;
53 $this->db_link_user = $db_link_user;
55 parent::__construct();
56 $this->build();
59 /**
60 * checks if the configuration wants to hide some databases
62 * @return void
64 protected function checkHideDatabase()
66 if (empty($GLOBALS['cfg']['Server']['hide_db'])) {
67 return;
70 foreach ($this->getArrayCopy() as $key => $db) {
71 if (preg_match('/' . $GLOBALS['cfg']['Server']['hide_db'] . '/', $db)) {
72 $this->offsetUnset($key);
77 /**
78 * retrieves database list from server
80 * @param string $like_db_name usually a db_name containing wildcards
82 * @return array
84 protected function retrieve($like_db_name = null)
86 $database_list = array();
87 $command = "";
88 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
89 $command .= "SELECT `SCHEMA_NAME` FROM `INFORMATION_SCHEMA`.`SCHEMATA`";
90 if (null !== $like_db_name) {
91 $command .= " WHERE `SCHEMA_NAME` LIKE '" . $like_db_name . "'";
93 } else {
94 if ($GLOBALS['dbs_to_test'] === false || null !== $like_db_name) {
95 $command .= "SHOW DATABASES";
96 if (null !== $like_db_name) {
97 $command .= " LIKE '" . $like_db_name . "'";
99 } else {
100 foreach ($GLOBALS['dbs_to_test'] as $db) {
101 $database_list = array_merge(
102 $database_list, $this->retrieve($db)
108 if ($command) {
109 $database_list = $GLOBALS['dbi']->fetchResult(
110 $command, null, null, $this->db_link
114 if ($GLOBALS['cfg']['NaturalOrder']) {
115 natsort($database_list);
116 } else {
117 // need to sort anyway, otherwise information_schema
118 // goes at the top
119 sort($database_list);
122 return $database_list;
126 * builds up the list
128 * @return void
130 public function build()
132 if (! $this->checkOnlyDatabase()) {
133 $items = $this->retrieve();
134 $this->exchangeArray($items);
137 $this->checkHideDatabase();
141 * checks the only_db configuration
143 * @return boolean false if there is no only_db, otherwise true
145 protected function checkOnlyDatabase()
147 if (is_string($GLOBALS['cfg']['Server']['only_db'])
148 && /*overload*/mb_strlen($GLOBALS['cfg']['Server']['only_db'])
150 $GLOBALS['cfg']['Server']['only_db'] = array(
151 $GLOBALS['cfg']['Server']['only_db']
155 if (! is_array($GLOBALS['cfg']['Server']['only_db'])) {
156 return false;
159 $items = array();
161 foreach ($GLOBALS['cfg']['Server']['only_db'] as $each_only_db) {
163 // check if the db name contains wildcard,
164 // thus containing not escaped _ or %
165 if (! preg_match('/(^|[^\\\\])(_|%)/', $each_only_db)) {
166 // ... not contains wildcard
167 $items[] = PMA_Util::unescapeMysqlWildcards($each_only_db);
168 continue;
171 $items = array_merge($items, $this->retrieve($each_only_db));
174 $this->exchangeArray($items);
176 return true;
180 * returns default item
182 * @return string default item
184 public function getDefault()
186 if (/*overload*/mb_strlen($GLOBALS['db'])) {
187 return $GLOBALS['db'];
190 return $this->getEmpty();