Skip to content

Commit 2edf663

Browse files
committed
Remove the Bookmark::getParams method
Uses BookmarkFeature class instead. Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent 8e9dfe7 commit 2edf663

10 files changed

Lines changed: 135 additions & 260 deletions

File tree

libraries/classes/Bookmark.php

Lines changed: 34 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
namespace PhpMyAdmin;
99

10+
use PhpMyAdmin\ConfigStorage\Features\BookmarkFeature;
1011
use PhpMyAdmin\ConfigStorage\Relation;
1112

1213
use function count;
13-
use function is_array;
1414
use function preg_match_all;
1515
use function preg_replace;
1616
use function str_replace;
@@ -57,21 +57,13 @@ class Bookmark
5757
/** @var DatabaseInterface */
5858
private $dbi;
5959

60-
/**
61-
* Current user
62-
*
63-
* @var string
64-
*/
65-
private $user;
60+
/** @var Relation */
61+
private $relation;
6662

67-
/**
68-
* @param DatabaseInterface $dbi DatabaseInterface object
69-
* @param string $user Current user
70-
*/
71-
public function __construct(DatabaseInterface $dbi, string $user)
63+
public function __construct(DatabaseInterface $dbi, Relation $relation)
7264
{
7365
$this->dbi = $dbi;
74-
$this->user = $user;
66+
$this->relation = $relation;
7567
}
7668

7769
/**
@@ -119,13 +111,13 @@ public function getQuery(): string
119111
*/
120112
public function save(): bool
121113
{
122-
$cfgBookmark = self::getParams($this->user);
123-
if (! is_array($cfgBookmark)) {
114+
$bookmarkFeature = $this->relation->getRelationParameters()->bookmarkFeature;
115+
if ($bookmarkFeature === null) {
124116
return false;
125117
}
126118

127-
$query = 'INSERT INTO ' . Util::backquote($cfgBookmark['db'])
128-
. '.' . Util::backquote($cfgBookmark['table'])
119+
$query = 'INSERT INTO ' . Util::backquote($bookmarkFeature->database)
120+
. '.' . Util::backquote($bookmarkFeature->bookmark)
129121
. ' (id, dbase, user, query, label) VALUES (NULL, '
130122
. "'" . $this->dbi->escapeString($this->database) . "', "
131123
. "'" . $this->dbi->escapeString($this->currentUser) . "', "
@@ -140,13 +132,13 @@ public function save(): bool
140132
*/
141133
public function delete(): bool
142134
{
143-
$cfgBookmark = self::getParams($this->user);
144-
if (! is_array($cfgBookmark)) {
135+
$bookmarkFeature = $this->relation->getRelationParameters()->bookmarkFeature;
136+
if ($bookmarkFeature === null) {
145137
return false;
146138
}
147139

148-
$query = 'DELETE FROM ' . Util::backquote($cfgBookmark['db'])
149-
. '.' . Util::backquote($cfgBookmark['table'])
140+
$query = 'DELETE FROM ' . Util::backquote($bookmarkFeature->database)
141+
. '.' . Util::backquote($bookmarkFeature->bookmark)
150142
. ' WHERE id = ' . $this->id;
151143

152144
return (bool) $this->dbi->tryQuery($query, DatabaseInterface::CONNECT_CONTROL);
@@ -196,59 +188,16 @@ public function applyVariables(array $variables): string
196188
return $query;
197189
}
198190

199-
/**
200-
* Defines the bookmark parameters for the current user
201-
*
202-
* @param string $user Current user
203-
*
204-
* @return array|false the bookmark parameters for the current user
205-
*/
206-
public static function getParams(string $user)
207-
{
208-
global $dbi;
209-
$cacheKey = 'Bookmark.params';
210-
211-
if (Cache::has($cacheKey)) {
212-
return Cache::get($cacheKey);
213-
}
214-
215-
$relation = new Relation($dbi);
216-
$bookmarkFeature = $relation->getRelationParameters()->bookmarkFeature;
217-
if ($bookmarkFeature !== null) {
218-
$cfgBookmark = [
219-
'user' => $user,
220-
'db' => $bookmarkFeature->database->getName(),
221-
'table' => $bookmarkFeature->bookmark->getName(),
222-
];
223-
Cache::set($cacheKey, $cfgBookmark);
224-
225-
return $cfgBookmark;
226-
}
227-
228-
$cfgBookmark = false;
229-
Cache::set($cacheKey, $cfgBookmark);
230-
231-
return $cfgBookmark;
232-
}
233-
234191
/**
235192
* Creates a Bookmark object from the parameters
236193
*
237-
* @param DatabaseInterface $dbi DatabaseInterface object
238-
* @param string $user Current user
239-
* @param array $bkm_fields the properties of the bookmark to add; here,
240-
* $bkm_fields['bkm_sql_query'] is urlencoded
241-
* @param bool $all_users whether to make the bookmark
242-
* available for all users
194+
* @param array $bkm_fields the properties of the bookmark to add; here, $bkm_fields['bkm_sql_query'] is urlencoded
195+
* @param bool $all_users whether to make the bookmark available for all users
243196
*
244197
* @return Bookmark|false
245198
*/
246-
public static function createBookmark(
247-
DatabaseInterface $dbi,
248-
string $user,
249-
array $bkm_fields,
250-
bool $all_users = false
251-
) {
199+
public static function createBookmark(DatabaseInterface $dbi, array $bkm_fields, bool $all_users = false)
200+
{
252201
if (
253202
! (isset($bkm_fields['bkm_sql_query'], $bkm_fields['bkm_label'])
254203
&& strlen($bkm_fields['bkm_sql_query']) > 0
@@ -257,7 +206,7 @@ public static function createBookmark(
257206
return false;
258207
}
259208

260-
$bookmark = new Bookmark($dbi, $user);
209+
$bookmark = new Bookmark($dbi, new Relation($dbi));
261210
$bookmark->database = $bkm_fields['bkm_database'];
262211
$bookmark->label = $bkm_fields['bkm_label'];
263212
$bookmark->query = $bkm_fields['bkm_sql_query'];
@@ -267,16 +216,11 @@ public static function createBookmark(
267216
}
268217

269218
/**
270-
* @param DatabaseInterface $dbi DatabaseInterface object
271-
* @param string $user Current user
272-
* @param array $row Resource used to build the bookmark
219+
* @param array $row Resource used to build the bookmark
273220
*/
274-
protected static function createFromRow(
275-
DatabaseInterface $dbi,
276-
string $user,
277-
$row
278-
): Bookmark {
279-
$bookmark = new Bookmark($dbi, $user);
221+
protected static function createFromRow(DatabaseInterface $dbi, $row): Bookmark
222+
{
223+
$bookmark = new Bookmark($dbi, new Relation($dbi));
280224
$bookmark->id = $row['id'];
281225
$bookmark->database = $row['dbase'];
282226
$bookmark->currentUser = $row['user'];
@@ -296,19 +240,15 @@ protected static function createFromRow(
296240
* @return Bookmark[] the bookmarks list
297241
*/
298242
public static function getList(
243+
BookmarkFeature $bookmarkFeature,
299244
DatabaseInterface $dbi,
300245
string $user,
301246
$db = false
302247
): array {
303-
$cfgBookmark = self::getParams($user);
304-
if (! is_array($cfgBookmark)) {
305-
return [];
306-
}
307-
308-
$query = 'SELECT * FROM ' . Util::backquote($cfgBookmark['db'])
309-
. '.' . Util::backquote($cfgBookmark['table'])
248+
$query = 'SELECT * FROM ' . Util::backquote($bookmarkFeature->database)
249+
. '.' . Util::backquote($bookmarkFeature->bookmark)
310250
. " WHERE ( `user` = ''"
311-
. " OR `user` = '" . $dbi->escapeString($cfgBookmark['user']) . "' )";
251+
. " OR `user` = '" . $dbi->escapeString($user) . "' )";
312252
if ($db !== false) {
313253
$query .= " AND dbase = '" . $dbi->escapeString($db) . "'";
314254
}
@@ -326,7 +266,7 @@ public static function getList(
326266
if (! empty($result)) {
327267
$bookmarks = [];
328268
foreach ($result as $row) {
329-
$bookmarks[] = self::createFromRow($dbi, $user, $row);
269+
$bookmarks[] = self::createFromRow($dbi, $row);
330270
}
331271

332272
return $bookmarks;
@@ -358,17 +298,18 @@ public static function get(
358298
bool $action_bookmark_all = false,
359299
bool $exact_user_match = false
360300
): ?self {
361-
$cfgBookmark = self::getParams($user);
362-
if (! is_array($cfgBookmark)) {
301+
$relation = new Relation($dbi);
302+
$bookmarkFeature = $relation->getRelationParameters()->bookmarkFeature;
303+
if ($bookmarkFeature === null) {
363304
return null;
364305
}
365306

366-
$query = 'SELECT * FROM ' . Util::backquote($cfgBookmark['db'])
367-
. '.' . Util::backquote($cfgBookmark['table'])
307+
$query = 'SELECT * FROM ' . Util::backquote($bookmarkFeature->database)
308+
. '.' . Util::backquote($bookmarkFeature->bookmark)
368309
. " WHERE dbase = '" . $dbi->escapeString($db) . "'";
369310
if (! $action_bookmark_all) {
370311
$query .= " AND (user = '"
371-
. $dbi->escapeString($cfgBookmark['user']) . "'";
312+
. $dbi->escapeString($user) . "'";
372313
if (! $exact_user_match) {
373314
$query .= " OR user = ''";
374315
}
@@ -381,7 +322,7 @@ public static function get(
381322

382323
$result = $dbi->fetchSingleRow($query, DatabaseInterface::FETCH_ASSOC, DatabaseInterface::CONNECT_CONTROL);
383324
if (! empty($result)) {
384-
return self::createFromRow($dbi, $user, $result);
325+
return self::createFromRow($dbi, $result);
385326
}
386327

387328
return null;

libraries/classes/Console.php

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -78,30 +78,31 @@ public static function getBookmarkContent(): string
7878
global $dbi;
7979

8080
$template = new Template();
81-
$cfgBookmark = Bookmark::getParams($GLOBALS['cfg']['Server']['user']);
82-
if ($cfgBookmark) {
83-
$bookmarks = Bookmark::getList($dbi, $GLOBALS['cfg']['Server']['user']);
84-
$count_bookmarks = count($bookmarks);
85-
if ($count_bookmarks > 0) {
86-
$welcomeMessage = sprintf(
87-
_ngettext(
88-
'Showing %1$d bookmark (both private and shared)',
89-
'Showing %1$d bookmarks (both private and shared)',
90-
$count_bookmarks
91-
),
81+
$relation = new Relation($dbi);
82+
$bookmarkFeature = $relation->getRelationParameters()->bookmarkFeature;
83+
if ($bookmarkFeature === null) {
84+
return '';
85+
}
86+
87+
$bookmarks = Bookmark::getList($bookmarkFeature, $dbi, $GLOBALS['cfg']['Server']['user']);
88+
$count_bookmarks = count($bookmarks);
89+
if ($count_bookmarks > 0) {
90+
$welcomeMessage = sprintf(
91+
_ngettext(
92+
'Showing %1$d bookmark (both private and shared)',
93+
'Showing %1$d bookmarks (both private and shared)',
9294
$count_bookmarks
93-
);
94-
} else {
95-
$welcomeMessage = __('No bookmarks');
96-
}
97-
98-
return $template->render('console/bookmark_content', [
99-
'welcome_message' => $welcomeMessage,
100-
'bookmarks' => $bookmarks,
101-
]);
95+
),
96+
$count_bookmarks
97+
);
98+
} else {
99+
$welcomeMessage = __('No bookmarks');
102100
}
103101

104-
return '';
102+
return $template->render('console/bookmark_content', [
103+
'welcome_message' => $welcomeMessage,
104+
'bookmarks' => $bookmarks,
105+
]);
105106
}
106107

107108
/**
@@ -119,21 +120,20 @@ public function getScripts(): array
119120
*/
120121
public function getDisplay(): string
121122
{
122-
if (! $this->isAjax && $this->isEnabled) {
123-
$cfgBookmark = Bookmark::getParams($GLOBALS['cfg']['Server']['user']);
124-
125-
$image = Html\Generator::getImage('console', __('SQL Query Console'));
126-
$_sql_history = $this->relation->getHistory($GLOBALS['cfg']['Server']['user']);
127-
$bookmarkContent = static::getBookmarkContent();
128-
129-
return $this->template->render('console/display', [
130-
'cfg_bookmark' => $cfgBookmark,
131-
'image' => $image,
132-
'sql_history' => $_sql_history,
133-
'bookmark_content' => $bookmarkContent,
134-
]);
123+
if ($this->isAjax || ! $this->isEnabled) {
124+
return '';
135125
}
136126

137-
return '';
127+
$bookmarkFeature = $this->relation->getRelationParameters()->bookmarkFeature;
128+
$image = Html\Generator::getImage('console', __('SQL Query Console'));
129+
$_sql_history = $this->relation->getHistory($GLOBALS['cfg']['Server']['user']);
130+
$bookmarkContent = static::getBookmarkContent();
131+
132+
return $this->template->render('console/display', [
133+
'has_bookmark_feature' => $bookmarkFeature !== null,
134+
'image' => $image,
135+
'sql_history' => $_sql_history,
136+
'bookmark_content' => $bookmarkContent,
137+
]);
138138
}
139139
}

libraries/classes/Controllers/ImportController.php

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpMyAdmin\Controllers;
66

77
use PhpMyAdmin\Bookmark;
8+
use PhpMyAdmin\ConfigStorage\Relation;
89
use PhpMyAdmin\Console;
910
use PhpMyAdmin\Core;
1011
use PhpMyAdmin\DatabaseInterface;
@@ -115,20 +116,14 @@ public function __invoke(): void
115116
return;
116117
}
117118

118-
$cfgBookmark = Bookmark::getParams($cfg['Server']['user']);
119-
120-
if (! is_array($cfgBookmark)) {
121-
$cfgBookmark = [];
122-
}
123-
124119
$bookmarkFields = [
125120
'bkm_database' => $_POST['db'],
126-
'bkm_user' => $cfgBookmark['user'],
121+
'bkm_user' => $cfg['Server']['user'],
127122
'bkm_sql_query' => $_POST['bookmark_query'],
128123
'bkm_label' => $_POST['label'],
129124
];
130125
$isShared = ($_POST['shared'] === 'true');
131-
$bookmark = Bookmark::createBookmark($this->dbi, $cfg['Server']['user'], $bookmarkFields, $isShared);
126+
$bookmark = Bookmark::createBookmark($this->dbi, $bookmarkFields, $isShared);
132127
if ($bookmark !== false && $bookmark->save()) {
133128
$this->response->addJSON('message', __('Succeeded'));
134129
$this->response->addJSON('data', $bookmarkFields);
@@ -768,15 +763,12 @@ public function __invoke(): void
768763
// since only one bookmark has to be added for all the queries submitted through
769764
// the SQL tab
770765
if (! empty($_POST['bkm_label']) && ! empty($import_text)) {
771-
$cfgBookmark = Bookmark::getParams($cfg['Server']['user']);
772-
773-
if (! is_array($cfgBookmark)) {
774-
$cfgBookmark = [];
775-
}
766+
$relation = new Relation($this->dbi);
776767

777768
$this->sql->storeTheQueryAsBookmark(
769+
$relation->getRelationParameters()->bookmarkFeature,
778770
$db,
779-
$cfgBookmark['user'],
771+
$cfg['Server']['user'],
780772
$_POST['sql_query'],
781773
$_POST['bkm_label'],
782774
isset($_POST['bkm_replace'])
@@ -792,15 +784,12 @@ public function __invoke(): void
792784
if ($result) {
793785
// Save a Bookmark with more than one queries (if Bookmark label given).
794786
if (! empty($_POST['bkm_label']) && ! empty($import_text)) {
795-
$cfgBookmark = Bookmark::getParams($cfg['Server']['user']);
796-
797-
if (! is_array($cfgBookmark)) {
798-
$cfgBookmark = [];
799-
}
787+
$relation = new Relation($this->dbi);
800788

801789
$this->sql->storeTheQueryAsBookmark(
790+
$relation->getRelationParameters()->bookmarkFeature,
802791
$db,
803-
$cfgBookmark['user'],
792+
$cfg['Server']['user'],
804793
$_POST['sql_query'],
805794
$_POST['bkm_label'],
806795
isset($_POST['bkm_replace'])

0 commit comments

Comments
 (0)