Visitar URL original
Merge pull request #19521 from kamil-tekiela/Fix-favoriteTables · phpmyadmin/phpmyadmin@ce440bf · GitHub
Skip to content

Commit ce440bf

Browse files
Merge pull request #19521 from kamil-tekiela/Fix-favoriteTables
Create a new controller for synchronizing favourites
2 parents 4858e79 + f08d2d4 commit ce440bf

10 files changed

Lines changed: 237 additions & 175 deletions

File tree

‎app/services_controllers.php‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use PhpMyAdmin\Controllers\Server;
3232
use PhpMyAdmin\Controllers\Setup;
3333
use PhpMyAdmin\Controllers\Sql;
34+
use PhpMyAdmin\Controllers\SyncFavoriteTablesController;
3435
use PhpMyAdmin\Controllers\Table;
3536
use PhpMyAdmin\Controllers\TableController;
3637
use PhpMyAdmin\Controllers\ThemesController;
@@ -308,7 +309,6 @@
308309
'arguments' => [
309310
'$response' => '@response',
310311
'$template' => '@template',
311-
'$relation' => '@relation',
312312
'$dbTableExists' => '@' . DbTableExists::class,
313313
],
314314
],
@@ -1361,5 +1361,12 @@
13611361
'$dbTableExists' => '@' . DbTableExists::class,
13621362
],
13631363
],
1364+
SyncFavoriteTablesController::class => [
1365+
'class' => SyncFavoriteTablesController::class,
1366+
'arguments' => [
1367+
'$response' => '@response',
1368+
'$relation' => '@relation',
1369+
],
1370+
],
13641371
],
13651372
];

‎phpstan-baseline.neon‎

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2337,12 +2337,6 @@ parameters:
23372337
count: 1
23382338
path: src/Controllers/Database/Structure/EmptyTableController.php
23392339

2340-
-
2341-
message: '#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\.$#'
2342-
identifier: foreach.nonIterable
2343-
count: 1
2344-
path: src/Controllers/Database/Structure/FavoriteTableController.php
2345-
23462340
-
23472341
message: '''
23482342
#^Call to deprecated method getInstance\(\) of class PhpMyAdmin\\Config\:
@@ -2352,42 +2346,12 @@ parameters:
23522346
count: 1
23532347
path: src/Controllers/Database/Structure/FavoriteTableController.php
23542348

2355-
-
2356-
message: '#^Cannot access offset ''db'' on mixed\.$#'
2357-
identifier: offsetAccess.nonOffsetAccessible
2358-
count: 1
2359-
path: src/Controllers/Database/Structure/FavoriteTableController.php
2360-
2361-
-
2362-
message: '#^Cannot access offset ''favorites_synced'' on mixed\.$#'
2363-
identifier: offsetAccess.nonOffsetAccessible
2364-
count: 1
2365-
path: src/Controllers/Database/Structure/FavoriteTableController.php
2366-
2367-
-
2368-
message: '#^Cannot access offset ''table'' on mixed\.$#'
2369-
identifier: offsetAccess.nonOffsetAccessible
2370-
count: 1
2371-
path: src/Controllers/Database/Structure/FavoriteTableController.php
2372-
2373-
-
2374-
message: '#^Cannot access offset int\<0, max\> on mixed\.$#'
2375-
identifier: offsetAccess.nonOffsetAccessible
2376-
count: 1
2377-
path: src/Controllers/Database/Structure/FavoriteTableController.php
2378-
23792349
-
23802350
message: '#^Loose comparison via "\=\=" is not allowed\.$#'
23812351
identifier: equal.notAllowed
23822352
count: 1
23832353
path: src/Controllers/Database/Structure/FavoriteTableController.php
23842354

2385-
-
2386-
message: '#^Parameter \#1 \$json of function json_decode expects string, mixed given\.$#'
2387-
identifier: argument.type
2388-
count: 1
2389-
path: src/Controllers/Database/Structure/FavoriteTableController.php
2390-
23912355
-
23922356
message: '#^Cannot cast mixed to string\.$#'
23932357
identifier: cast.string
@@ -3633,6 +3597,39 @@ parameters:
36333597
count: 1
36343598
path: src/Controllers/Sql/SqlController.php
36353599

3600+
-
3601+
message: '''
3602+
#^Call to deprecated method getInstance\(\) of class PhpMyAdmin\\Config\:
3603+
Use dependency injection instead\.$#
3604+
'''
3605+
identifier: staticMethod.deprecated
3606+
count: 1
3607+
path: src/Controllers/SyncFavoriteTablesController.php
3608+
3609+
-
3610+
message: '#^Cannot access offset ''db'' on mixed\.$#'
3611+
identifier: offsetAccess.nonOffsetAccessible
3612+
count: 1
3613+
path: src/Controllers/SyncFavoriteTablesController.php
3614+
3615+
-
3616+
message: '#^Cannot access offset ''favorites_synced'' on mixed\.$#'
3617+
identifier: offsetAccess.nonOffsetAccessible
3618+
count: 1
3619+
path: src/Controllers/SyncFavoriteTablesController.php
3620+
3621+
-
3622+
message: '#^Cannot access offset ''table'' on mixed\.$#'
3623+
identifier: offsetAccess.nonOffsetAccessible
3624+
count: 1
3625+
path: src/Controllers/SyncFavoriteTablesController.php
3626+
3627+
-
3628+
message: '#^Cannot access offset int\<0, max\> on mixed\.$#'
3629+
identifier: offsetAccess.nonOffsetAccessible
3630+
count: 1
3631+
path: src/Controllers/SyncFavoriteTablesController.php
3632+
36363633
-
36373634
message: '#^Cannot access offset \(int\|string\) on mixed\.$#'
36383635
identifier: offsetAccess.nonOffsetAccessible
@@ -18579,6 +18576,12 @@ parameters:
1857918576
count: 2
1858018577
path: tests/unit/Controllers/Sql/SetValuesControllerTest.php
1858118578

18579+
-
18580+
message: '#^Parameter \#1 \$str of method PhpMyAdmin\\Dbal\\DatabaseInterface\:\:quoteString\(\) expects string, string\|false given\.$#'
18581+
identifier: argument.type
18582+
count: 1
18583+
path: tests/unit/Controllers/SyncFavoriteTablesControllerTest.php
18584+
1858218585
-
1858318586
message: '''
1858418587
#^Call to deprecated method getInstance\(\) of class PhpMyAdmin\\Config\:

‎psalm-baseline.xml‎

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,25 +1206,15 @@
12061206
<DeprecatedMethod>
12071207
<code><![CDATA[Config::getInstance()]]></code>
12081208
</DeprecatedMethod>
1209-
<MixedArgument>
1210-
<code><![CDATA[$favoriteTables]]></code>
1211-
</MixedArgument>
1212-
<MixedArrayAccess>
1213-
<code><![CDATA[$value['db']]]></code>
1214-
<code><![CDATA[$value['table']]]></code>
1215-
</MixedArrayAccess>
1216-
<MixedArrayAssignment>
1217-
<code><![CDATA[$_SESSION['tmpval']['favorites_synced']]]></code>
1218-
<code><![CDATA[$_SESSION['tmpval']['favorites_synced'][Current::$server]]]></code>
1219-
</MixedArrayAssignment>
12201209
<MixedAssignment>
12211210
<code><![CDATA[$favoriteTables]]></code>
1222-
<code><![CDATA[$favoriteTables]]></code>
1223-
<code><![CDATA[$value]]></code>
12241211
</MixedAssignment>
12251212
<MixedOperand>
12261213
<code><![CDATA[Current::$database]]></code>
12271214
</MixedOperand>
1215+
<PossiblyUnusedMethod>
1216+
<code><![CDATA[__construct]]></code>
1217+
</PossiblyUnusedMethod>
12281218
</file>
12291219
<file src="src/Controllers/Database/Structure/RealRowCountController.php">
12301220
<MixedArgument>
@@ -2261,6 +2251,23 @@
22612251
<code><![CDATA[$sqlQuery]]></code>
22622252
</MixedAssignment>
22632253
</file>
2254+
<file src="src/Controllers/SyncFavoriteTablesController.php">
2255+
<DeprecatedMethod>
2256+
<code><![CDATA[Config::getInstance()]]></code>
2257+
</DeprecatedMethod>
2258+
<MixedArrayAccess>
2259+
<code><![CDATA[$value['db']]]></code>
2260+
<code><![CDATA[$value['table']]]></code>
2261+
</MixedArrayAccess>
2262+
<MixedArrayAssignment>
2263+
<code><![CDATA[$_SESSION['tmpval']['favorites_synced']]]></code>
2264+
<code><![CDATA[$_SESSION['tmpval']['favorites_synced'][Current::$server]]]></code>
2265+
</MixedArrayAssignment>
2266+
<MixedAssignment>
2267+
<code><![CDATA[$favoriteTables]]></code>
2268+
<code><![CDATA[$value]]></code>
2269+
</MixedAssignment>
2270+
</file>
22642271
<file src="src/Controllers/Table/AddFieldController.php">
22652272
<MixedArgument>
22662273
<code><![CDATA[$_POST['field_input_transformation'][$fieldindex]]]></code>

‎src/Controllers/Database/Structure/FavoriteTableController.php‎

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

77
use PhpMyAdmin\Config;
8-
use PhpMyAdmin\ConfigStorage\Relation;
98
use PhpMyAdmin\Controllers\InvocableController;
109
use PhpMyAdmin\Current;
1110
use PhpMyAdmin\DbTableExists;
@@ -22,18 +21,17 @@
2221

2322
use function __;
2423
use function count;
24+
use function hash;
2525
use function is_array;
2626
use function json_decode;
2727
use function json_encode;
2828
use function md5;
29-
use function sha1;
3029

3130
final class FavoriteTableController implements InvocableController
3231
{
3332
public function __construct(
3433
private readonly ResponseRenderer $response,
3534
private readonly Template $template,
36-
private readonly Relation $relation,
3735
private readonly DbTableExists $dbTableExists,
3836
) {
3937
}
@@ -52,29 +50,13 @@ public function __invoke(ServerRequest $request): Response
5250

5351
$favoriteInstance = RecentFavoriteTables::getInstance(TableType::Favorite);
5452

55-
$favoriteTables = $request->getParam('favoriteTables');
56-
$favoriteTables = $favoriteTables !== null ? json_decode($favoriteTables, true) : [];
57-
53+
$favoriteTables = json_decode($request->getParsedBodyParamAsString('favoriteTables'), true);
5854
if (! is_array($favoriteTables)) {
5955
$favoriteTables = [];
6056
}
6157

6258
// Required to keep each user's preferences separate.
63-
$user = sha1($config->selectedServer['user']);
64-
65-
// Request for Synchronization of favorite tables.
66-
if ($request->getParam('sync_favorite_tables') !== null) {
67-
$relationParameters = $this->relation->getRelationParameters();
68-
if ($relationParameters->favoriteTablesFeature !== null) {
69-
$this->response->addJSON($this->synchronizeFavoriteTables(
70-
$favoriteInstance,
71-
$user,
72-
$favoriteTables,
73-
));
74-
}
75-
76-
return $this->response->response();
77-
}
59+
$user = hash('sha1', $config->selectedServer['user']);
7860

7961
$databaseName = DatabaseName::tryFrom($request->getParam('db'));
8062
if ($databaseName === null || ! $this->dbTableExists->selectDatabase($databaseName)) {
@@ -144,35 +126,4 @@ public function __invoke(ServerRequest $request): Response
144126

145127
return $this->response->response();
146128
}
147-
148-
/**
149-
* Synchronize favorite tables
150-
*
151-
* @param RecentFavoriteTables $favoriteInstance Instance of this class
152-
* @param string $user The user hash
153-
* @param mixed[] $favoriteTables Existing favorites
154-
*
155-
* @return mixed[]
156-
*/
157-
private function synchronizeFavoriteTables(
158-
RecentFavoriteTables $favoriteInstance,
159-
string $user,
160-
array $favoriteTables,
161-
): array {
162-
if ($favoriteInstance->getTables() === [] && isset($favoriteTables[$user])) {
163-
foreach ($favoriteTables[$user] as $value) {
164-
$favoriteInstance->add(new RecentFavoriteTable(
165-
DatabaseName::from($value['db']),
166-
TableName::from($value['table']),
167-
));
168-
}
169-
}
170-
171-
$favoriteTables[$user] = $favoriteInstance->getTables();
172-
173-
// Set flag when localStorage and pmadb(if present) are in sync.
174-
$_SESSION['tmpval']['favorites_synced'][Current::$server] = true;
175-
176-
return ['favoriteTables' => json_encode($favoriteTables), 'list' => $favoriteInstance->getHtmlList()];
177-
}
178129
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMyAdmin\Controllers;
6+
7+
use PhpMyAdmin\Config;
8+
use PhpMyAdmin\ConfigStorage\Relation;
9+
use PhpMyAdmin\Current;
10+
use PhpMyAdmin\Favorites\RecentFavoriteTable;
11+
use PhpMyAdmin\Favorites\RecentFavoriteTables;
12+
use PhpMyAdmin\Favorites\TableType;
13+
use PhpMyAdmin\Http\Response;
14+
use PhpMyAdmin\Http\ServerRequest;
15+
use PhpMyAdmin\Identifiers\DatabaseName;
16+
use PhpMyAdmin\Identifiers\TableName;
17+
use PhpMyAdmin\ResponseRenderer;
18+
19+
use function hash;
20+
use function is_array;
21+
use function json_decode;
22+
use function json_encode;
23+
24+
final class SyncFavoriteTablesController implements InvocableController
25+
{
26+
public function __construct(
27+
private readonly ResponseRenderer $response,
28+
private readonly Relation $relation,
29+
) {
30+
}
31+
32+
public function __invoke(ServerRequest $request): Response
33+
{
34+
if (! $request->isAjax()) {
35+
return $this->response->response();
36+
}
37+
38+
$favoriteInstance = RecentFavoriteTables::getInstance(TableType::Favorite);
39+
40+
$favoriteTables = json_decode($request->getParsedBodyParamAsString('favoriteTables'), true);
41+
if (! is_array($favoriteTables)) {
42+
$favoriteTables = [];
43+
}
44+
45+
// Required to keep each user's preferences separate.
46+
$user = hash('sha1', Config::getInstance()->selectedServer['user']);
47+
48+
$relationParameters = $this->relation->getRelationParameters();
49+
if ($relationParameters->favoriteTablesFeature !== null) {
50+
if (
51+
$favoriteInstance->getTables() === []
52+
&& isset($favoriteTables[$user])
53+
&& is_array($favoriteTables[$user])
54+
) {
55+
foreach ($favoriteTables[$user] as $value) {
56+
$favoriteInstance->add(new RecentFavoriteTable(
57+
DatabaseName::from($value['db']),
58+
TableName::from($value['table']),
59+
));
60+
}
61+
}
62+
63+
$favoriteTables[$user] = $favoriteInstance->getTables();
64+
65+
// Set flag when localStorage and pmadb(if present) are in sync.
66+
$_SESSION['tmpval']['favorites_synced'][Current::$server] = true;
67+
68+
$this->response->addJSON([
69+
'favoriteTables' => json_encode($favoriteTables),
70+
'list' => $favoriteInstance->getHtmlList(),
71+
]);
72+
}
73+
74+
return $this->response->response();
75+
}
76+
}

‎src/Favorites/RecentFavoriteTables.php‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,6 @@ public function contains(RecentFavoriteTable $currentTable): bool
294294
*/
295295
public function getHtmlSyncFavoriteTables(): string
296296
{
297-
$retval = '';
298297
if (Current::$server === 0) {
299298
return '';
300299
}
@@ -305,16 +304,12 @@ public function getHtmlSyncFavoriteTables(): string
305304
$relationParameters->favoriteTablesFeature !== null
306305
&& ! isset($_SESSION['tmpval']['favorites_synced'][Current::$server])
307306
) {
308-
$url = Url::getFromRoute('/database/structure/favorite-table', [
309-
'ajax_request' => true,
310-
'favorite_table' => true,
311-
'sync_favorite_tables' => true,
312-
]);
313-
$retval = '<a class="hide" id="sync_favorite_tables"';
314-
$retval .= ' href="' . $url . '"></a>';
307+
$url = Url::getFromRoute('/sync-favorite-tables', ['ajax_request' => true]);
308+
309+
return '<a class="hide" id="sync_favorite_tables" href="' . $url . '"></a>';
315310
}
316311

317-
return $retval;
312+
return '';
318313
}
319314

320315
/**

0 commit comments

Comments
 (0)