Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
PurgeExpiredWatchlistItems
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * Purge expired watchlist items, looping through 500 at a time.
4 */
5
6use MediaWiki\MainConfigNames;
7
8require_once __DIR__ . '/Maintenance.php';
9
10class PurgeExpiredWatchlistItems extends Maintenance {
11
12    public function __construct() {
13        parent::__construct();
14        $this->addDescription( 'Removes expired items from the watchlist and watchlist_expiry tables.' );
15        $this->setBatchSize( 500 );
16    }
17
18    /**
19     * @inheritDoc
20     */
21    public function execute() {
22        // Make sure watchlist expiring is enabled.
23        if ( !$this->getServiceContainer()->getMainConfig()->get( MainConfigNames::WatchlistExpiry ) ) {
24            $this->error( "Watchlist expiry is not enabled. Set `\$wgWatchlistExpiry = true;` to enable." );
25            return false;
26        }
27
28        // Loop through 500 entries at a time and delete them.
29        $watchedItemStore = $this->getServiceContainer()->getWatchedItemStore();
30        $count = $watchedItemStore->countExpired();
31        $this->output( $count . " expired watchlist entries found.\n" );
32        if ( $count === 0 ) {
33            // None found to delete.
34            return true;
35        }
36        while ( $watchedItemStore->countExpired() > 0 ) {
37            $watchedItemStore->removeExpired( $this->getBatchSize(), true );
38        }
39
40        // Report success.
41        $this->output( "All expired entries purged.\n" );
42        return true;
43    }
44}
45
46$maintClass = PurgeExpiredWatchlistItems::class;
47require_once RUN_MAINTENANCE_IF_MAIN;