-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathTrackingChecker.php
More file actions
83 lines (68 loc) · 2.43 KB
/
TrackingChecker.php
File metadata and controls
83 lines (68 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Tracking;
use PhpMyAdmin\ConfigStorage\Features\TrackingFeature;
use PhpMyAdmin\ConfigStorage\Relation;
use PhpMyAdmin\Dbal\ConnectionType;
use PhpMyAdmin\Dbal\DatabaseInterface;
use PhpMyAdmin\Identifiers\TableName;
use PhpMyAdmin\Util;
use function array_column;
use function array_diff;
use function array_values;
use function sprintf;
class TrackingChecker
{
private TrackingFeature|null $trackingFeature;
public function __construct(
private DatabaseInterface $dbi,
Relation $relation,
) {
$this->trackingFeature = $relation->getRelationParameters()->trackingFeature;
}
/**
* Get a list of untracked tables.
* Deactivated tracked tables are not included in the list.
*
* @return array<int, string|TableName>
*/
public function getUntrackedTableNames(string $dbName): array
{
$tableList = $this->dbi->getTables($dbName);
if ($this->trackingFeature === null) {
return $tableList;
}
$trackedTables = array_column($this->getTrackedTables($dbName), 'name');
return array_values(array_diff($tableList, $trackedTables));
}
/** @return TrackedTable[] */
public function getTrackedTables(string $dbName): array
{
if (! Tracker::isEnabled()) {
return [];
}
if ($this->trackingFeature === null) {
return [];
}
$sqlQuery = sprintf(
"SELECT table_name, tracking_active
FROM (
SELECT table_name, MAX(version) version
FROM %s.%s WHERE db_name = %s AND table_name <> ''
GROUP BY table_name
) filtered_tables
JOIN %s.%s USING(table_name, version)",
Util::backquote($this->trackingFeature->database),
Util::backquote($this->trackingFeature->tracking),
$this->dbi->quoteString($dbName, ConnectionType::ControlUser),
Util::backquote($this->trackingFeature->database),
Util::backquote($this->trackingFeature->tracking),
);
$trackedTables = [];
foreach ($this->dbi->queryAsControlUser($sqlQuery) as $row) {
$trackedTable = new TrackedTable(TableName::from($row['table_name']), (bool) $row['tracking_active']);
$trackedTables[$trackedTable->name->getName()] = $trackedTable;
}
return $trackedTables;
}
}