3 * Handles bookmarking SQL queries
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
;
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();
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(
44 if ($this->bookmarkFeature
=== null) {
48 if ($sqlQuery === '' ||
$label === '') {
52 if (! $this->config
->settings
['AllowSharedBookmarks']) {
56 if (! $shared && $user === '') {
60 return new Bookmark($this->dbi
, $this->bookmarkFeature
, $database, $shared ?
'' : $user, $label, $sqlQuery);
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(
75 string|
false $db = false,
77 if ($this->bookmarkFeature
=== null) {
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` = ''";
93 $query .= ' AND dbase = ' . $this->dbi
->quoteString($db);
96 $query .= ' ORDER BY label ASC';
98 $result = $this->dbi
->fetchResult($query, null, null, ConnectionType
::ControlUser
);
101 foreach ($result as $row) {
102 $bookmarks[] = $this->createFromRow($row);
109 * Retrieve a specific bookmark
115 if ($this->bookmarkFeature
=== 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 = ''";
134 $query .= ' LIMIT 1';
136 $result = $this->dbi
->fetchSingleRow($query, DatabaseInterface
::FETCH_ASSOC
, ConnectionType
::ControlUser
);
137 if ($result !== null) {
138 return $this->createFromRow($result);
145 * Retrieve a specific bookmark by its label
147 public function getByLabel(
152 if ($this->bookmarkFeature
=== null) {
156 $query = 'SELECT * FROM ' . Util
::backquote($this->bookmarkFeature
->database
)
157 . '.' . Util
::backquote($this->bookmarkFeature
->bookmark
)
159 . ' = ' . $this->dbi
->quoteString($label)
160 . ' AND dbase = ' . $this->dbi
->quoteString($db->getName())
161 . ' AND user = ' . $this->dbi
->quoteString($user)
164 $result = $this->dbi
->fetchSingleRow($query, DatabaseInterface
::FETCH_ASSOC
, ConnectionType
::ControlUser
);
165 if ($result !== null) {
166 return $this->createFromRow($result);
172 /** @param string[] $row Resource used to build the bookmark */
173 private function createFromRow(array $row): Bookmark
177 $this->bookmarkFeature
,