Skip to content

Commit 3f12593

Browse files
Merge pull request #18259 from kamil-tekiela/Tracking
Refactor and optimize Tracking
2 parents d6dd0fd + 67cc2be commit 3f12593

23 files changed

Lines changed: 403 additions & 495 deletions

libraries/classes/Common.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use PhpMyAdmin\Plugins\AuthenticationPluginFactory;
2020
use PhpMyAdmin\SqlParser\Lexer;
2121
use PhpMyAdmin\Theme\ThemeManager;
22+
use PhpMyAdmin\Tracking\Tracker;
2223
use RuntimeException;
2324
use Symfony\Component\DependencyInjection\ContainerInterface;
2425

libraries/classes/Controllers/Database/StructureController.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
use PhpMyAdmin\Sanitize;
2121
use PhpMyAdmin\StorageEngine;
2222
use PhpMyAdmin\Template;
23-
use PhpMyAdmin\Tracker;
23+
use PhpMyAdmin\Tracking\TrackedTable;
24+
use PhpMyAdmin\Tracking\Tracker;
25+
use PhpMyAdmin\Tracking\TrackingChecker;
2426
use PhpMyAdmin\Url;
2527
use PhpMyAdmin\Util;
2628

@@ -74,6 +76,7 @@ public function __construct(
7476
private Relation $relation,
7577
private Replication $replication,
7678
private DatabaseInterface $dbi,
79+
private TrackingChecker $trackingChecker,
7780
) {
7881
parent::__construct($response, $template);
7982

@@ -221,6 +224,7 @@ protected function displayTableList(array $replicaInfo): string
221224
$hiddenFields = [];
222225
$overallApproxRows = false;
223226
$structureTableRows = [];
227+
$trackedTables = $this->trackingChecker->getTrackedTables($GLOBALS['db']);
224228
foreach ($this->tables as $currentTable) {
225229
// Get valid statistics whatever is the table type
226230

@@ -396,7 +400,7 @@ protected function displayTableList(array $replicaInfo): string
396400
),
397401
),
398402
),
399-
'tracking_icon' => $this->getTrackingIcon($truename),
403+
'tracking_icon' => $this->getTrackingIcon($truename, $trackedTables[$truename] ?? null),
400404
'server_replica_status' => $replicaInfo['status'],
401405
'table_url_params' => $tableUrlParams,
402406
'db_is_system_schema' => $this->dbIsSystemSchema,
@@ -500,20 +504,17 @@ protected function displayTableList(array $replicaInfo): string
500504
/**
501505
* Returns the tracking icon if the table is tracked
502506
*
503-
* @param string $table table name
504-
*
505507
* @return string HTML for tracking icon
506508
*/
507-
protected function getTrackingIcon(string $table): string
509+
protected function getTrackingIcon(string $table, TrackedTable|null $trackedTable): string
508510
{
509511
$trackingIcon = '';
510512
if (Tracker::isActive()) {
511-
$isTracked = Tracker::isTracked($GLOBALS['db'], $table);
512-
if ($isTracked || Tracker::getVersion($GLOBALS['db'], $table) > 0) {
513+
if ($trackedTable !== null) {
513514
$trackingIcon = $this->template->render('database/structure/tracking_icon', [
514515
'db' => $GLOBALS['db'],
515516
'table' => $table,
516-
'is_tracked' => $isTracked,
517+
'is_tracked' => $trackedTable->active,
517518
]);
518519
}
519520
}

libraries/classes/Controllers/Database/TrackingController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
use PhpMyAdmin\Query\Utilities;
1414
use PhpMyAdmin\ResponseRenderer;
1515
use PhpMyAdmin\Template;
16-
use PhpMyAdmin\Tracker;
17-
use PhpMyAdmin\Tracking;
16+
use PhpMyAdmin\Tracking\Tracker;
17+
use PhpMyAdmin\Tracking\Tracking;
1818
use PhpMyAdmin\Url;
1919
use PhpMyAdmin\Util;
2020

libraries/classes/Controllers/Table/StructureController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
use PhpMyAdmin\StorageEngine;
2424
use PhpMyAdmin\Table;
2525
use PhpMyAdmin\Template;
26-
use PhpMyAdmin\Tracker;
26+
use PhpMyAdmin\Tracking\Tracker;
2727
use PhpMyAdmin\Transformations;
2828
use PhpMyAdmin\Url;
2929
use PhpMyAdmin\Util;

libraries/classes/Controllers/Table/TrackingController.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
use PhpMyAdmin\Message;
1313
use PhpMyAdmin\ResponseRenderer;
1414
use PhpMyAdmin\Template;
15-
use PhpMyAdmin\Tracker;
16-
use PhpMyAdmin\Tracking;
15+
use PhpMyAdmin\Tracking\Tracker;
16+
use PhpMyAdmin\Tracking\Tracking;
17+
use PhpMyAdmin\Tracking\TrackingChecker;
1718
use PhpMyAdmin\Url;
1819
use PhpMyAdmin\Util;
1920
use Throwable;
@@ -35,6 +36,7 @@ public function __construct(
3536
ResponseRenderer $response,
3637
Template $template,
3738
private Tracking $tracking,
39+
private TrackingChecker $trackingChecker,
3840
) {
3941
parent::__construct($response, $template);
4042
}
@@ -64,9 +66,11 @@ public function __invoke(ServerRequest $request): void
6466
$toggleActivation = $request->getParsedBodyParam('toggle_activation');
6567
$reportExport = $request->getParsedBodyParam('report_export');
6668

69+
$trackedTables = $this->trackingChecker->getTrackedTables($GLOBALS['db']);
6770
if (
6871
Tracker::isActive()
69-
&& Tracker::isTracked($GLOBALS['db'], $GLOBALS['table'])
72+
&& isset($trackedTables[$GLOBALS['table']])
73+
&& $trackedTables[$GLOBALS['table']]->active
7074
&& $toggleActivation !== 'deactivate_now'
7175
&& $reportExport !== 'sqldumpfile'
7276
) {
@@ -209,7 +213,7 @@ public function __invoke(ServerRequest $request): void
209213
$message = $GLOBALS['msg']->getDisplay();
210214
} elseif ($reportExport === 'sqldump') {
211215
$this->addScriptFiles(['sql.js']);
212-
$sqlDump = $this->tracking->exportAsSqlDump($GLOBALS['db'], $GLOBALS['table'], $GLOBALS['entries']);
216+
$sqlDump = $this->tracking->exportAsSqlDump($GLOBALS['entries']);
213217
}
214218

215219
$schemaSnapshot = '';

libraries/classes/DatabaseInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use PhpMyAdmin\Query\Generator as QueryGenerator;
2121
use PhpMyAdmin\Query\Utilities;
2222
use PhpMyAdmin\SqlParser\Context;
23+
use PhpMyAdmin\Tracking\Tracker;
2324
use PhpMyAdmin\Utils\SessionCache;
2425
use stdClass;
2526

libraries/classes/Menu.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PhpMyAdmin\ConfigStorage\Relation;
1111
use PhpMyAdmin\Dbal\Connection;
1212
use PhpMyAdmin\Query\Utilities;
13+
use PhpMyAdmin\Tracking\Tracker;
1314
use PhpMyAdmin\Utils\SessionCache;
1415

1516
use function __;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMyAdmin\Tracking;
6+
7+
use PhpMyAdmin\Dbal\TableName;
8+
9+
final class TrackedTable
10+
{
11+
public function __construct(public readonly TableName $name, public readonly bool $active)
12+
{
13+
}
14+
}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55

66
declare(strict_types=1);
77

8-
namespace PhpMyAdmin;
8+
namespace PhpMyAdmin\Tracking;
99

10+
use PhpMyAdmin\Cache;
1011
use PhpMyAdmin\ConfigStorage\Relation;
1112
use PhpMyAdmin\Dbal\Connection;
13+
use PhpMyAdmin\Plugins;
1214
use PhpMyAdmin\Plugins\Export\ExportSql;
1315
use PhpMyAdmin\SqlParser\Parser;
1416
use PhpMyAdmin\SqlParser\Statements\AlterStatement;
@@ -19,6 +21,7 @@
1921
use PhpMyAdmin\SqlParser\Statements\RenameStatement;
2022
use PhpMyAdmin\SqlParser\Statements\TruncateStatement;
2123
use PhpMyAdmin\SqlParser\Statements\UpdateStatement;
24+
use PhpMyAdmin\Util;
2225

2326
use function array_values;
2427
use function count;
@@ -478,7 +481,7 @@ public static function deactivateTracking(string $dbname, string $tablename, str
478481
*
479482
* @return int (-1 if no version exists | > 0 if a version exists)
480483
*/
481-
public static function getVersion(string $dbname, string $tablename, string|null $statement = null): int
484+
private static function getVersion(string $dbname, string $tablename, string|null $statement = null): int
482485
{
483486
$relation = new Relation($GLOBALS['dbi']);
484487
$trackingFeature = $relation->getRelationParameters()->trackingFeature;
@@ -800,8 +803,6 @@ public static function parseQuery(string $query): array
800803
*/
801804
public static function handleQuery(string $query): void
802805
{
803-
$relation = new Relation($GLOBALS['dbi']);
804-
805806
// If query is marked as untouchable, leave
806807
if (mb_strstr($query, '/*NOTRACK*/')) {
807808
return;
@@ -881,6 +882,7 @@ public static function handleQuery(string $query): void
881882
// Add log information
882883
$query = self::getLogComment() . $query;
883884

885+
$relation = new Relation($GLOBALS['dbi']);
884886
$trackingFeature = $relation->getRelationParameters()->trackingFeature;
885887
if ($trackingFeature === null) {
886888
return;
Lines changed: 13 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,28 @@
55

66
declare(strict_types=1);
77

8-
namespace PhpMyAdmin;
8+
namespace PhpMyAdmin\Tracking;
99

1010
use DateTimeImmutable;
1111
use PhpMyAdmin\ConfigStorage\Relation;
12+
use PhpMyAdmin\Core;
13+
use PhpMyAdmin\DatabaseInterface;
1214
use PhpMyAdmin\Dbal\ResultInterface;
1315
use PhpMyAdmin\Html\Generator;
16+
use PhpMyAdmin\Message;
17+
use PhpMyAdmin\SqlQueryForm;
18+
use PhpMyAdmin\Template;
19+
use PhpMyAdmin\Url;
20+
use PhpMyAdmin\Util;
1421

1522
use function __;
16-
use function array_key_exists;
1723
use function array_merge;
1824
use function array_multisort;
1925
use function count;
2026
use function date;
2127
use function htmlspecialchars;
2228
use function in_array;
2329
use function ini_set;
24-
use function is_array;
2530
use function json_encode;
2631
use function mb_strstr;
2732
use function preg_replace;
@@ -32,7 +37,7 @@
3237
use const SORT_ASC;
3338

3439
/**
35-
* PhpMyAdmin\Tracking class
40+
* PhpMyAdmin\Tracking\Tracking class
3641
*/
3742
class Tracking
3843
{
@@ -41,6 +46,7 @@ public function __construct(
4146
public Template $template,
4247
protected Relation $relation,
4348
private DatabaseInterface $dbi,
49+
private TrackingChecker $trackingChecker,
4450
) {
4551
}
4652

@@ -117,18 +123,6 @@ public function getHtmlForMainPage(
117123
string $textDir,
118124
int|null $lastVersion = null,
119125
): string {
120-
$selectableTablesSqlResult = $this->getSqlResultForSelectableTables($db);
121-
$selectableTablesEntries = [];
122-
$selectableTablesNumRows = 0;
123-
if ($selectableTablesSqlResult !== false) {
124-
foreach ($selectableTablesSqlResult as $entry) {
125-
$entry['is_tracked'] = Tracker::isTracked($entry['db_name'], $entry['table_name']);
126-
$selectableTablesEntries[] = $entry;
127-
}
128-
129-
$selectableTablesNumRows = $selectableTablesSqlResult->numRows();
130-
}
131-
132126
$versionSqlResult = $this->getListOfVersionsOfTable($db, $table);
133127
if ($lastVersion === null && $versionSqlResult !== false) {
134128
$lastVersion = $this->getTableLastVersionNumber($versionSqlResult);
@@ -145,8 +139,7 @@ public function getHtmlForMainPage(
145139
'url_params' => $urlParams,
146140
'db' => $db,
147141
'table' => $table,
148-
'selectable_tables_num_rows' => $selectableTablesNumRows,
149-
'selectable_tables_entries' => $selectableTablesEntries,
142+
'selectable_tables_entries' => $this->trackingChecker->getTrackedTables($db),
150143
'selected_table' => $_POST['table'] ?? null,
151144
'last_version' => $lastVersion,
152145
'versions' => $versions,
@@ -164,24 +157,6 @@ public function getTableLastVersionNumber(ResultInterface $result): int
164157
return (int) $result->fetchValue('version');
165158
}
166159

167-
/**
168-
* Function to get sql results for selectable tables
169-
*/
170-
public function getSqlResultForSelectableTables(string $db): ResultInterface|false
171-
{
172-
$trackingFeature = $this->relation->getRelationParameters()->trackingFeature;
173-
if ($trackingFeature === null) {
174-
return false;
175-
}
176-
177-
$sql_query = ' SELECT DISTINCT db_name, table_name FROM '
178-
. Util::backquote($trackingFeature->database) . '.' . Util::backquote($trackingFeature->tracking)
179-
. " WHERE db_name = '" . $this->dbi->escapeString($db) . "' "
180-
. ' ORDER BY db_name, table_name';
181-
182-
return $this->dbi->queryAsControlUser($sql_query);
183-
}
184-
185160
/**
186161
* Function to get html for tracking report and tracking report export
187162
*
@@ -774,7 +749,7 @@ public function deleteFromTrackingReportLog(
774749
*
775750
* @return string HTML SQL query form
776751
*/
777-
public function exportAsSqlDump(string $db, string $table, array $entries): string
752+
public function exportAsSqlDump(array $entries): string
778753
{
779754
$html = '';
780755
$new_query = '# '
@@ -1082,7 +1057,7 @@ public function getHtmlForDbTrackingTables(
10821057
. '\' GROUP BY table_name ORDER BY table_name ASC';
10831058

10841059
$allTablesResult = $this->dbi->queryAsControlUser($allTablesQuery);
1085-
$untrackedTables = $this->getUntrackedTables($db);
1060+
$untrackedTables = $this->trackingChecker->getUntrackedTableNames($db);
10861061

10871062
// If a HEAD version exists
10881063
$versions = [];
@@ -1107,44 +1082,4 @@ public function getHtmlForDbTrackingTables(
11071082
'untracked_tables' => $untrackedTables,
11081083
]);
11091084
}
1110-
1111-
/**
1112-
* Helper function: Recursive function for getting table names from $table_list
1113-
*
1114-
* @param array $table_list Table list
1115-
* @param string $db Current database
1116-
*
1117-
* @return array
1118-
*/
1119-
public function extractTableNames(array $table_list, string $db): array
1120-
{
1121-
$untracked_tables = [];
1122-
$sep = $GLOBALS['cfg']['NavigationTreeTableSeparator'];
1123-
1124-
foreach ($table_list as $value) {
1125-
if (is_array($value) && array_key_exists('is' . $sep . 'group', $value) && $value['is' . $sep . 'group']) {
1126-
// Recursion step
1127-
$untracked_tables = array_merge($this->extractTableNames($value, $db), $untracked_tables);
1128-
} elseif (is_array($value) && (Tracker::getVersion($db, $value['Name']) == -1)) {
1129-
$untracked_tables[] = $value['Name'];
1130-
}
1131-
}
1132-
1133-
return $untracked_tables;
1134-
}
1135-
1136-
/**
1137-
* Get untracked tables
1138-
*
1139-
* @param string $db current database
1140-
*
1141-
* @return array
1142-
*/
1143-
public function getUntrackedTables(string $db): array
1144-
{
1145-
$table_list = Util::getTableList($db);
1146-
1147-
//Use helper function to get table list recursively.
1148-
return $this->extractTableNames($table_list, $db);
1149-
}
11501085
}

0 commit comments

Comments
 (0)