MediaWiki master
ChangedTablesTracker.php
Go to the documentation of this file.
1<?php
2
4
5use RuntimeException;
6
16 private const TRACKED_VERBS = [ 'INSERT', 'UPDATE', 'REPLACE' ];
17 private static bool $trackingEnabled = false;
19 private static array $tableMap = [];
20
24 public static function startTracking(): void {
25 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
26 throw new RuntimeException( "This class is internal and should only be used in tests." );
27 }
28 if ( self::$trackingEnabled ) {
29 throw new RuntimeException( "Tracking is already enabled" );
30 }
31 self::$trackingEnabled = true;
32 self::$tableMap = [];
33 }
34
41 public static function getTables( string $domainId ): array {
42 $tableMap = self::$tableMap[$domainId] ?? [];
43 return array_keys( $tableMap );
44 }
45
49 public static function stopTracking(): void {
50 if ( !self::$trackingEnabled ) {
51 throw new RuntimeException( "Tracking is not enabled" );
52 }
53 self::$tableMap = [];
54 self::$trackingEnabled = false;
55 }
56
61 public static function recordQuery( DatabaseDomain $domain, Query $query ): void {
62 if ( !self::$trackingEnabled ) {
63 return;
64 }
65 if ( !$query->isWriteQuery() ) {
66 return;
67 }
68 $domainId = $domain->getId();
69 $queryVerb = $query->getVerb();
70 $tableName = $query->getWriteTable();
71 if ( $tableName === null ) {
72 return;
73 }
74 if ( $queryVerb === 'DROP' ) {
75 // Special case: if a table is being dropped, forget about it.
76 unset( self::$tableMap[$domainId][$tableName] );
77 return;
78 }
79 if ( !in_array( $queryVerb, self::TRACKED_VERBS, true ) ) {
80 return;
81 }
82
83 self::$tableMap[$domainId][$tableName] = true;
84 }
85}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
Utility class that keeps a list of DB tables that were (presumably) changed by write queries.
static getTables(string $domainId)
Get the tables for a given domain ID.
static stopTracking()
Reset the internal list and disable tracking.
static startTracking()
Enables query tracking and resets the list of changed tables.
static recordQuery(DatabaseDomain $domain, Query $query)
When tracking is enabled and a query alters tables, record the list of tables that are altered.
Class to handle database/schema/prefix specifications for IDatabase.
Holds information on Query to be executed.
Definition Query.php:31