aboutsummaryrefslogtreecommitdiffstats
path: root/maintenance/pruneUnusedLinkTargetRows.php
blob: 59b28008e7c3f6bfe9201b45ea159ee0601485f8 (plain) (blame)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php

use MediaWiki\Maintenance\Maintenance;

// @codeCoverageIgnoreStart
require_once __DIR__ . '/Maintenance.php';
// @codeCoverageIgnoreEnd

/**
 * Maintenance script that cleans unused rows in linktarget table
 *
 * @ingroup Maintenance
 * @since 1.39
 */
class PruneUnusedLinkTargetRows extends Maintenance {
	public function __construct() {
		parent::__construct();
		$this->addDescription(
			'Clean unused rows in linktarget table'
		);
		$this->addOption(
			'sleep',
			'Sleep time (in seconds) between every batch. Default: 0',
			false,
			true
		);
		$this->addOption( 'dry', 'Dry run', false );
		$this->addOption( 'start', 'Start after this lt_id', false, true );
		$this->setBatchSize( 50 );
	}

	public function execute() {
		$dbw = $this->getPrimaryDB();
		$dbr = $this->getReplicaDB();
		$maxLtId = (int)$dbr->newSelectQueryBuilder()
			->select( 'MAX(lt_id)' )
			->from( 'linktarget' )
			->caller( __METHOD__ )
			->fetchField();
		// To avoid race condition of newly added linktarget rows
		// being deleted before getting a chance to be used, let's ignore the newest ones.
		$maxLtId = min( [ $maxLtId - 1, (int)( $maxLtId * 0.99 ) ] );

		$ltCounter = (int)$this->getOption( 'start', 0 );

		$this->output( "Deleting unused linktarget rows...\n" );
		$deleted = 0;
		$linksMigration = $this->getServiceContainer()->getLinksMigration();
		while ( $ltCounter < $maxLtId ) {
			$batchMaxLtId = min( $ltCounter + $this->getBatchSize(), $maxLtId ) + 1;
			$this->output( "Checking lt_id between $ltCounter and $batchMaxLtId...\n" );
			$queryBuilder = $dbr->newSelectQueryBuilder()
				->select( [ 'lt_id' ] )
				->from( 'linktarget' );
			$queryBuilder->where( [
				$dbr->expr( 'lt_id', '<', $batchMaxLtId ),
				$dbr->expr( 'lt_id', '>', $ltCounter )
			] );
			foreach ( $linksMigration::$mapping as $table => $tableData ) {
				$queryBuilder->leftJoin( $table, null, $tableData['target_id'] . '=lt_id' );
				$queryBuilder->andWhere( [
					$tableData['target_id'] => null
				] );
			}
			$ltIdsToDelete = $queryBuilder->caller( __METHOD__ )->fetchFieldValues();
			if ( !$ltIdsToDelete ) {
				$ltCounter += $this->getBatchSize();
				continue;
			}

			// Run against primary as well with a faster query plan, just to be safe.
			// Also having a bit of time in between helps in cases of immediate removal and insertion of use.
			$queryBuilder = $dbr->newSelectQueryBuilder()
				->select( [ 'lt_id' ] )
				->from( 'linktarget' )
				->where( [
					'lt_id' => $ltIdsToDelete,
				] );
			foreach ( $linksMigration::$mapping as $table => $tableData ) {
				$queryBuilder->leftJoin( $table, null, $tableData['target_id'] . '=lt_id' );
				$queryBuilder->andWhere( [
					$tableData['target_id'] => null
				] );
			}
			$ltIdsToDelete = $queryBuilder->caller( __METHOD__ )->fetchFieldValues();
			if ( !$ltIdsToDelete ) {
				$ltCounter += $this->getBatchSize();
				continue;
			}

			if ( !$this->getOption( 'dry' ) ) {
				$dbw->newDeleteQueryBuilder()
					->deleteFrom( 'linktarget' )
					->where( [ 'lt_id' => $ltIdsToDelete ] )
					->caller( __METHOD__ )->execute();
			}
			$deleted += count( $ltIdsToDelete );
			$ltCounter += $this->getBatchSize();

			// Sleep between batches for replication to catch up
			$this->waitForReplication();
			$sleep = (int)$this->getOption( 'sleep', 0 );
			if ( $sleep > 0 ) {
				sleep( $sleep );
			}
		}

		$this->output(
			"Completed clean up linktarget table, "
			. "$deleted rows deleted.\n"
		);

		return true;
	}

}

// @codeCoverageIgnoreStart
$maintClass = PruneUnusedLinkTargetRows::class;
require_once RUN_MAINTENANCE_IF_MAIN;
// @codeCoverageIgnoreEnd