Translated using Weblate (Portuguese)
[phpmyadmin.git] / src / Bookmarks / BookmarkRepository.php
blobf4f5c401513b9f5082aae85f53d36d790b267049
1 <?php
2 /**
3 * Handles bookmarking SQL queries
4 */
6 declare(strict_types=1);
8 namespace PhpMyAdmin\Bookmarks;
10 use PhpMyAdmin\Config;
11 use PhpMyAdmin\ConfigStorage\Features\BookmarkFeature;
12 use PhpMyAdmin\ConfigStorage\Relation;
13 use PhpMyAdmin\DatabaseInterface;
14 use PhpMyAdmin\Dbal\ConnectionType;
15 use PhpMyAdmin\Identifiers\DatabaseName;
16 use PhpMyAdmin\Util;
18 /**
19 * Handles bookmarking SQL queries
21 final class BookmarkRepository
23 private BookmarkFeature|null $bookmarkFeature;
24 private readonly Config $config;
26 public function __construct(private DatabaseInterface $dbi, Relation $relation)
28 $this->bookmarkFeature = $relation->getRelationParameters()->bookmarkFeature;
29 $this->config = Config::getInstance();
32 /**
33 * Creates a Bookmark object from the parameters
35 * @param bool $shared whether to make the bookmark available for all users
37 public function createBookmark(
38 string $sqlQuery,
39 string $label,
40 string $user,
41 string $database,
42 bool $shared = false,
43 ): Bookmark|false {
44 if ($this->bookmarkFeature === null) {
45 return false;
48 if ($sqlQuery === '' || $label === '') {
49 return false;
52 if (! $this->config->settings['AllowSharedBookmarks']) {
53 $shared = false;
56 if (! $shared && $user === '') {
57 return false;
60 return new Bookmark($this->dbi, $this->bookmarkFeature, $database, $shared ? '' : $user, $label, $sqlQuery);
63 /**
64 * Gets the list of bookmarks defined for the current database
66 * @param string $user Current user
67 * @param string|false $db the current database name or false
69 * @return Bookmark[] the bookmarks list
71 * @infection-ignore-all
73 public function getList(
74 string $user,
75 string|false $db = false,
76 ): array {
77 if ($this->bookmarkFeature === null) {
78 return [];
81 $exactUserMatch = ! $this->config->settings['AllowSharedBookmarks'];
83 $query = 'SELECT * FROM ' . Util::backquote($this->bookmarkFeature->database)
84 . '.' . Util::backquote($this->bookmarkFeature->bookmark)
85 . ' WHERE (`user` = ' . $this->dbi->quoteString($user);
86 if (! $exactUserMatch) {
87 $query .= " OR `user` = ''";
90 $query .= ')';
92 if ($db !== false) {
93 $query .= ' AND dbase = ' . $this->dbi->quoteString($db);
96 $query .= ' ORDER BY label ASC';
98 $result = $this->dbi->fetchResult($query, null, null, ConnectionType::ControlUser);
100 $bookmarks = [];
101 foreach ($result as $row) {
102 $bookmarks[] = $this->createFromRow($row);
105 return $bookmarks;
109 * Retrieve a specific bookmark
111 public function get(
112 string|null $user,
113 int $id,
114 ): Bookmark|null {
115 if ($this->bookmarkFeature === null) {
116 return null;
119 $query = 'SELECT * FROM ' . Util::backquote($this->bookmarkFeature->database)
120 . '.' . Util::backquote($this->bookmarkFeature->bookmark)
121 . ' WHERE `id` = ' . $id;
123 if ($user !== null) {
124 $query .= ' AND (user = ' . $this->dbi->quoteString($user);
126 $exactUserMatch = ! $this->config->settings['AllowSharedBookmarks'];
127 if (! $exactUserMatch) {
128 $query .= " OR user = ''";
131 $query .= ')';
134 $query .= ' LIMIT 1';
136 $result = $this->dbi->fetchSingleRow($query, DatabaseInterface::FETCH_ASSOC, ConnectionType::ControlUser);
137 if ($result !== null) {
138 return $this->createFromRow($result);
141 return null;
145 * Retrieve a specific bookmark by its label
147 public function getByLabel(
148 string $user,
149 DatabaseName $db,
150 string $label,
151 ): Bookmark|null {
152 if ($this->bookmarkFeature === null) {
153 return null;
156 $query = 'SELECT * FROM ' . Util::backquote($this->bookmarkFeature->database)
157 . '.' . Util::backquote($this->bookmarkFeature->bookmark)
158 . ' WHERE `label`'
159 . ' = ' . $this->dbi->quoteString($label)
160 . ' AND dbase = ' . $this->dbi->quoteString($db->getName())
161 . ' AND user = ' . $this->dbi->quoteString($user)
162 . ' LIMIT 1';
164 $result = $this->dbi->fetchSingleRow($query, DatabaseInterface::FETCH_ASSOC, ConnectionType::ControlUser);
165 if ($result !== null) {
166 return $this->createFromRow($result);
169 return null;
172 /** @param string[] $row Resource used to build the bookmark */
173 private function createFromRow(array $row): Bookmark
175 return new Bookmark(
176 $this->dbi,
177 $this->bookmarkFeature,
178 $row['dbase'],
179 $row['user'],
180 $row['label'],
181 $row['query'],
182 (int) $row['id'],