Skip to content

Commit a6c1a62

Browse files
Merge pull request #19111 from MauricioFauth/recent-tables-controller-removal
Extract recent table handling from Header into a middleware
2 parents 6c62f2e + 9b25d12 commit a6c1a62

13 files changed

Lines changed: 112 additions & 108 deletions

File tree

app/services_controllers.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
use PhpMyAdmin\Controllers\Operations;
2828
use PhpMyAdmin\Controllers\PhpInfoController;
2929
use PhpMyAdmin\Controllers\Preferences;
30-
use PhpMyAdmin\Controllers\RecentTablesListController;
3130
use PhpMyAdmin\Controllers\SchemaExportController;
3231
use PhpMyAdmin\Controllers\Server;
3332
use PhpMyAdmin\Controllers\Sql;
@@ -544,10 +543,6 @@
544543
'class' => PhpInfoController::class,
545544
'arguments' => ['$response' => '@response'],
546545
],
547-
RecentTablesListController::class => [
548-
'class' => RecentTablesListController::class,
549-
'arguments' => ['$response' => '@response'],
550-
],
551546
Preferences\ExportController::class => [
552547
'class' => Preferences\ExportController::class,
553548
'arguments' => [

phpstan-baseline.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8217,7 +8217,7 @@ parameters:
82178217

82188218
-
82198219
message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#"
8220-
count: 8
8220+
count: 7
82218221
path: src/Header.php
82228222

82238223
-

psalm-baseline.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2554,11 +2554,6 @@
25542554
<code><![CDATA[__construct]]></code>
25552555
</PossiblyUnusedMethod>
25562556
</file>
2557-
<file src="src/Controllers/RecentTablesListController.php">
2558-
<PossiblyUnusedMethod>
2559-
<code><![CDATA[__construct]]></code>
2560-
</PossiblyUnusedMethod>
2561-
</file>
25622557
<file src="src/Controllers/SchemaExportController.php">
25632558
<PossiblyUnusedReturnValue>
25642559
<code><![CDATA[Response|null]]></code>
@@ -6533,7 +6528,6 @@
65336528
<RiskyTruthyFalsyComparison>
65346529
<code><![CDATA[! $this->config->get('DisableShortcutKeys')]]></code>
65356530
<code><![CDATA[empty($_REQUEST['message'])]]></code>
6536-
<code><![CDATA[empty($_REQUEST['recent_table'])]]></code>
65376531
</RiskyTruthyFalsyComparison>
65386532
</file>
65396533
<file src="src/Html/Generator.php">

resources/js/src/modules/functions.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3121,24 +3121,10 @@ function getPageSelectorEventHandler () {
31213121
}
31223122

31233123
function teardownRecentFavoriteTables (): void {
3124-
$('#update_recent_tables').off('ready');
31253124
$('#sync_favorite_tables').off('ready');
31263125
}
31273126

31283127
function onloadRecentFavoriteTables (): void {
3129-
var $updateRecentTables = $('#update_recent_tables');
3130-
if ($updateRecentTables.length) {
3131-
$.get(
3132-
$updateRecentTables.attr('href'),
3133-
{ 'no_debug': true },
3134-
function (data) {
3135-
if (typeof data !== 'undefined' && data.success === true) {
3136-
$('#recentTableList').html(data.list);
3137-
}
3138-
}
3139-
);
3140-
}
3141-
31423128
// Sync favorite tables from localStorage to pmadb.
31433129
if (! $('#sync_favorite_tables').length) {
31443130
return;

resources/templates/header.twig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@
117117
<div id="page_content">
118118
{{ messages|raw }}
119119

120-
{{ recent_table|raw }}
121120
{%- if is_logged_in -%}
122121
{{ include('modals/preview_sql_modal.twig') }}
123122
{{ include('modals/enum_set_editor.twig') }}

src/Application.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use PhpMyAdmin\Http\Middleware\PhpExtensionsChecking;
3030
use PhpMyAdmin\Http\Middleware\PhpSettingsConfiguration;
3131
use PhpMyAdmin\Http\Middleware\ProfilingChecking;
32+
use PhpMyAdmin\Http\Middleware\RecentTableHandling;
3233
use PhpMyAdmin\Http\Middleware\RequestProblemChecking;
3334
use PhpMyAdmin\Http\Middleware\ResponseRendererLoading;
3435
use PhpMyAdmin\Http\Middleware\RouteParsing;
@@ -114,6 +115,7 @@ public function run(bool $isSetupPage = false): void
114115
$requestHandler->add(new TokenMismatchChecking());
115116
$requestHandler->add(new ProfilingChecking());
116117
$requestHandler->add(new UserPreferencesLoading($this->config));
118+
$requestHandler->add(new RecentTableHandling($this->config));
117119

118120
$runner = new RequestHandlerRunner(
119121
$requestHandler,

src/Controllers/RecentTablesListController.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/Favorites/RecentFavoriteTables.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -329,16 +329,6 @@ public function getHtmlSyncFavoriteTables(): string
329329
return $retval;
330330
}
331331

332-
/**
333-
* Generate Html to update recent tables.
334-
*/
335-
public static function getHtmlUpdateRecentTables(): string
336-
{
337-
return '<a class="hide" id="update_recent_tables" href="'
338-
. Url::getFromRoute('/recent-table', ['ajax_request' => true, 'recent_table' => true])
339-
. '"></a>';
340-
}
341-
342332
/**
343333
* Return the name of the configuration storage table
344334
*

src/Header.php

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@
99

1010
use PhpMyAdmin\ConfigStorage\Relation;
1111
use PhpMyAdmin\Container\ContainerBuilder;
12-
use PhpMyAdmin\Favorites\RecentFavoriteTable;
13-
use PhpMyAdmin\Favorites\RecentFavoriteTables;
14-
use PhpMyAdmin\Favorites\TableType;
1512
use PhpMyAdmin\Html\Generator;
16-
use PhpMyAdmin\Identifiers\DatabaseName;
17-
use PhpMyAdmin\Identifiers\TableName;
1813
use PhpMyAdmin\Navigation\Navigation;
1914
use PhpMyAdmin\Theme\ThemeManager;
2015

@@ -252,22 +247,10 @@ public function disableWarnings(): void
252247
*/
253248
public function getDisplay(): string
254249
{
255-
if ($this->headerIsSent || ! $this->isEnabled) {
250+
if ($this->headerIsSent || ! $this->isEnabled || $this->isAjax) {
256251
return '';
257252
}
258253

259-
$recentTable = '';
260-
if (empty($_REQUEST['recent_table']) && Current::$table !== '') {
261-
$recentTable = $this->addRecentTable(
262-
DatabaseName::from(Current::$database),
263-
TableName::from(Current::$table),
264-
);
265-
}
266-
267-
if ($this->isAjax) {
268-
return $recentTable;
269-
}
270-
271254
$this->sendHttpHeaders();
272255

273256
$baseDir = defined('PMA_PATH_TO_BASEDIR') ? PMA_PATH_TO_BASEDIR : '';
@@ -359,7 +342,6 @@ public function getDisplay(): string
359342
'menu' => $menu ?? '',
360343
'console' => $console,
361344
'messages' => $messages,
362-
'recent_table' => $recentTable,
363345
'theme_color_mode' => $theme->getColorMode(),
364346
'theme_color_modes' => $theme->getColorModes(),
365347
'theme_id' => $theme->getId(),
@@ -580,24 +562,6 @@ private function getCspHeaders(): array
580562
return $headers;
581563
}
582564

583-
/**
584-
* Add recently used table and reload the navigation.
585-
*/
586-
private function addRecentTable(DatabaseName $db, TableName $table): string
587-
{
588-
if ($this->menuEnabled && $this->config->settings['NumRecentTables'] > 0) {
589-
$favoriteTable = new RecentFavoriteTable($db, $table);
590-
$error = RecentFavoriteTables::getInstance(TableType::Recent)->add($favoriteTable);
591-
if ($error === true) {
592-
return RecentFavoriteTables::getHtmlUpdateRecentTables();
593-
}
594-
595-
return $error->getDisplay();
596-
}
597-
598-
return '';
599-
}
600-
601565
/**
602566
* Returns the phpMyAdmin version to be appended to the url to avoid caching
603567
* between versions
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMyAdmin\Http\Middleware;
6+
7+
use PhpMyAdmin\Config;
8+
use PhpMyAdmin\Favorites\RecentFavoriteTable;
9+
use PhpMyAdmin\Favorites\RecentFavoriteTables;
10+
use PhpMyAdmin\Favorites\TableType;
11+
use PhpMyAdmin\Http\ServerRequest;
12+
use PhpMyAdmin\Identifiers\DatabaseName;
13+
use PhpMyAdmin\Identifiers\TableName;
14+
use PhpMyAdmin\Message;
15+
use Psr\Http\Message\ResponseInterface;
16+
use Psr\Http\Message\ServerRequestInterface;
17+
use Psr\Http\Server\MiddlewareInterface;
18+
use Psr\Http\Server\RequestHandlerInterface;
19+
20+
use function assert;
21+
22+
final class RecentTableHandling implements MiddlewareInterface
23+
{
24+
public function __construct(private readonly Config $config)
25+
{
26+
}
27+
28+
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
29+
{
30+
assert($request instanceof ServerRequest);
31+
if ($this->config->settings['NumRecentTables'] === 0) {
32+
return $handler->handle($request);
33+
}
34+
35+
$response = $handler->handle($request);
36+
37+
$db = DatabaseName::tryFrom($request->getParam('db'));
38+
$table = TableName::tryFrom($request->getParam('table'));
39+
if ($db !== null && $table !== null) {
40+
$recentTable = new RecentFavoriteTable($db, $table);
41+
$isAddedOrError = RecentFavoriteTables::getInstance(TableType::Recent)->add($recentTable);
42+
if ($isAddedOrError instanceof Message) {
43+
$response->getBody()->write($isAddedOrError->getMessage());
44+
}
45+
}
46+
47+
return $response;
48+
}
49+
}

0 commit comments

Comments
 (0)