77
88namespace PhpMyAdmin ;
99
10+ use PhpMyAdmin \ConfigStorage \Features \BookmarkFeature ;
1011use PhpMyAdmin \ConfigStorage \Relation ;
1112
1213use function count ;
13- use function is_array ;
1414use function preg_match_all ;
1515use function preg_replace ;
1616use 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 ;
0 commit comments