Merge pull request #19212 from kamil-tekiela/privatize-const
[phpmyadmin.git] / src / Bookmarks / Bookmark.php
blob478af296ddc1465af6ef29ba8920024663fdbb03
1 <?php
2 /**
3 * Handles bookmarking SQL queries
4 */
6 declare(strict_types=1);
8 namespace PhpMyAdmin\Bookmarks;
10 use PhpMyAdmin\ConfigStorage\Features\BookmarkFeature;
11 use PhpMyAdmin\DatabaseInterface;
12 use PhpMyAdmin\Dbal\ConnectionType;
13 use PhpMyAdmin\Util;
15 use function count;
16 use function preg_match_all;
17 use function preg_replace;
18 use function str_replace;
20 use const PREG_SET_ORDER;
22 /**
23 * Handles bookmarking SQL queries
25 class Bookmark
27 /**
28 * @param string $database Database the bookmark belongs to
29 * @param string $currentUser The user to whom the bookmark belongs, empty for public bookmarks
30 * @param string $label Label of the bookmark
31 * @param string $query SQL query that is bookmarked
32 * @param int $id ID of the bookmark
34 public function __construct(
35 private DatabaseInterface $dbi,
36 private BookmarkFeature $bookmarkFeature,
37 private string $database,
38 private string $currentUser,
39 private string $label,
40 private string $query,
41 private int $id = 0,
42 ) {
45 /**
46 * Returns the ID of the bookmark
48 public function getId(): int
50 return $this->id;
53 /**
54 * Returns the database of the bookmark
56 public function getDatabase(): string
58 return $this->database;
61 /**
62 * Returns the user whom the bookmark belongs to
64 public function getUser(): string
66 return $this->currentUser;
69 /**
70 * Returns the label of the bookmark
72 public function getLabel(): string
74 return $this->label;
77 /**
78 * Returns the query
80 public function getQuery(): string
82 return $this->query;
85 /**
86 * Adds a bookmark
88 public function save(): bool
90 $query = 'INSERT INTO ' . Util::backquote($this->bookmarkFeature->database)
91 . '.' . Util::backquote($this->bookmarkFeature->bookmark)
92 . ' (id, dbase, user, query, label) VALUES (NULL, '
93 . $this->dbi->quoteString($this->database) . ', '
94 . $this->dbi->quoteString($this->currentUser) . ', '
95 . $this->dbi->quoteString($this->query) . ', '
96 . $this->dbi->quoteString($this->label) . ')';
98 return (bool) $this->dbi->query($query, ConnectionType::ControlUser);
102 * Deletes a bookmark
104 public function delete(): bool
106 $query = 'DELETE FROM ' . Util::backquote($this->bookmarkFeature->database)
107 . '.' . Util::backquote($this->bookmarkFeature->bookmark)
108 . ' WHERE id = ' . $this->id;
110 return (bool) $this->dbi->tryQuery($query, ConnectionType::ControlUser);
114 * Returns the number of variables in a bookmark
116 * @return int number of variables
118 public function getVariableCount(): int
120 $matches = [];
121 preg_match_all('/\[VARIABLE[0-9]*\]/', $this->query, $matches, PREG_SET_ORDER);
123 return count($matches);
127 * Replace the placeholders in the bookmark query with variables
129 * @param mixed[] $variables
131 * @return string query with variables applied
133 public function applyVariables(array $variables): string
135 // remove comments that encloses a variable placeholder
136 $query = (string) preg_replace('|/\*(.*\[VARIABLE[0-9]*\].*)\*/|imsU', '${1}', $this->query);
137 // replace variable placeholders with values
138 $numberOfVariables = $this->getVariableCount();
139 for ($i = 1; $i <= $numberOfVariables; $i++) {
140 $var = $variables[$i] ?? '';
142 $query = str_replace('[VARIABLE' . $i . ']', $var, $query);
143 // backward compatibility
144 if ($i !== 1) {
145 continue;
148 $query = str_replace('[VARIABLE]', $var, $query);
151 return $query;