Translated using Weblate (Portuguese)
[phpmyadmin.git] / src / ListDatabase.php
blob5aff0cd589036c1c0eb3ef6f81a55c1df09c5a10
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin;
7 use ArrayObject;
8 use PhpMyAdmin\Query\Utilities;
10 use function array_merge;
11 use function in_array;
12 use function is_array;
13 use function is_string;
14 use function preg_match;
15 use function sort;
16 use function strnatcasecmp;
17 use function strtr;
18 use function usort;
20 /**
21 * Handles database lists
23 * @extends ArrayObject<int, string>
25 class ListDatabase extends ArrayObject
27 public function __construct(
28 private readonly DatabaseInterface $dbi,
29 private readonly Config $config,
30 private readonly UserPrivilegesFactory $userPrivilegesFactory,
31 ) {
32 parent::__construct();
34 $userPrivileges = $this->userPrivilegesFactory->getPrivileges();
36 $this->build($userPrivileges);
39 /** @return array<int, array<string, bool|string>> */
40 public function getList(): array
42 $list = [];
43 foreach ($this as $eachItem) {
44 if (Utilities::isSystemSchema($eachItem)) {
45 continue;
48 $list[] = ['name' => $eachItem, 'is_selected' => $eachItem === Current::$database];
51 return $list;
54 /**
55 * checks if the configuration wants to hide some databases
57 protected function checkHideDatabase(): void
59 if (empty($this->config->selectedServer['hide_db'])) {
60 return;
63 foreach ($this->getArrayCopy() as $key => $db) {
64 if (! preg_match('/' . $this->config->selectedServer['hide_db'] . '/', $db)) {
65 continue;
68 $this->offsetUnset($key);
72 /**
73 * retrieves database list from server
75 * @param string|null $likeDbName usually a db_name containing wildcards
77 * @return mixed[]
79 protected function retrieve(UserPrivileges $userPrivileges, string|null $likeDbName = null): array
81 $databaseList = [];
82 $command = '';
83 if (! $this->config->selectedServer['DisableIS']) {
84 $command .= 'SELECT `SCHEMA_NAME` FROM `INFORMATION_SCHEMA`.`SCHEMATA`';
85 if ($likeDbName !== null) {
86 $command .= " WHERE `SCHEMA_NAME` LIKE '" . $likeDbName . "'";
88 } elseif ($userPrivileges->databasesToTest === false || $likeDbName !== null) {
89 $command .= 'SHOW DATABASES';
90 if ($likeDbName !== null) {
91 $command .= " LIKE '" . $likeDbName . "'";
93 } else {
94 foreach ($userPrivileges->databasesToTest as $db) {
95 $databaseList = array_merge(
96 $databaseList,
97 $this->retrieve($userPrivileges, $db),
102 if ($command !== '') {
103 $databaseList = $this->dbi->fetchResult($command);
106 if ($this->config->settings['NaturalOrder']) {
107 usort($databaseList, strnatcasecmp(...));
108 } else {
109 // need to sort anyway, otherwise information_schema
110 // goes at the top
111 sort($databaseList);
114 return $databaseList;
118 * builds up the list
120 public function build(UserPrivileges $userPrivileges): void
122 if (! $this->checkOnlyDatabase($userPrivileges)) {
123 $items = $this->retrieve($userPrivileges);
124 $this->exchangeArray($items);
127 $this->checkHideDatabase();
131 * checks the only_db configuration
133 protected function checkOnlyDatabase(UserPrivileges $userPrivileges): bool
135 if (
136 is_string($this->config->selectedServer['only_db']) && $this->config->selectedServer['only_db'] !== ''
138 $this->config->selectedServer['only_db'] = [$this->config->selectedServer['only_db']];
141 if (! is_array($this->config->selectedServer['only_db'])) {
142 return false;
145 $items = [];
147 foreach ($this->config->selectedServer['only_db'] as $eachOnlyDb) {
148 // check if the db name contains wildcard,
149 // thus containing not escaped _ or %
150 if (! preg_match('/(^|[^\\\\])(_|%)/', $eachOnlyDb)) {
151 // ... not contains wildcard
152 $items[] = strtr($eachOnlyDb, ['\\\\' => '\\', '\\_' => '_', '\\%' => '%']);
153 continue;
156 $items = array_merge($items, $this->retrieve($userPrivileges, $eachOnlyDb));
159 $this->exchangeArray($items);
161 return true;
165 * Checks if the given strings exists in the current list, if there is
166 * missing at least one item it returns false otherwise true
168 public function exists(string ...$params): bool
170 $elements = $this->getArrayCopy();
171 foreach ($params as $param) {
172 if (! in_array($param, $elements, true)) {
173 return false;
177 return true;