3 * Handles bookmarking SQL queries
6 declare(strict_types
=1);
8 namespace PhpMyAdmin\Bookmarks
;
10 use PhpMyAdmin\ConfigStorage\Features\BookmarkFeature
;
11 use PhpMyAdmin\DatabaseInterface
;
12 use PhpMyAdmin\Dbal\ConnectionType
;
16 use function preg_match_all
;
17 use function preg_replace
;
18 use function str_replace
;
20 use const PREG_SET_ORDER
;
23 * Handles bookmarking SQL queries
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,
46 * Returns the ID of the bookmark
48 public function getId(): int
54 * Returns the database of the bookmark
56 public function getDatabase(): string
58 return $this->database
;
62 * Returns the user whom the bookmark belongs to
64 public function getUser(): string
66 return $this->currentUser
;
70 * Returns the label of the bookmark
72 public function getLabel(): string
80 public function getQuery(): string
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
);
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
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
148 $query = str_replace('[VARIABLE]', $var, $query);