Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
PurgeJobUtils
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
30
0.00% covered (danger)
0.00%
0 / 1
 invalidatePages
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2/**
3 * Base code for update jobs that put some secondary data extracted
4 * from article content into the database.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 */
23use MediaWiki\Deferred\AutoCommitUpdate;
24use MediaWiki\Deferred\DeferredUpdates;
25use MediaWiki\MainConfigNames;
26use MediaWiki\MediaWikiServices;
27use Wikimedia\Rdbms\IDatabase;
28
29class PurgeJobUtils {
30    /**
31     * Invalidate the cache of a list of pages from a single namespace.
32     * This is intended for use by subclasses.
33     *
34     * @param IDatabase $dbw
35     * @param int $namespace Namespace number
36     * @param string[] $dbkeys
37     */
38    public static function invalidatePages( IDatabase $dbw, $namespace, array $dbkeys ) {
39        if ( $dbkeys === [] ) {
40            return;
41        }
42        $fname = __METHOD__;
43
44        DeferredUpdates::addUpdate( new AutoCommitUpdate(
45            $dbw,
46            __METHOD__,
47            static function () use ( $dbw, $namespace, $dbkeys, $fname ) {
48                $services = MediaWikiServices::getInstance();
49                $dbProvider = $services->getConnectionProvider();
50                // Determine which pages need to be updated.
51                // This is necessary to prevent the job queue from smashing the DB with
52                // large numbers of concurrent invalidations of the same page.
53                $now = $dbw->timestamp();
54                $ids = $dbw->newSelectQueryBuilder()
55                    ->select( 'page_id' )
56                    ->from( 'page' )
57                    ->where( [ 'page_namespace' => $namespace ] )
58                    ->andWhere( [ 'page_title' => $dbkeys ] )
59                    ->andWhere( $dbw->expr( 'page_touched', '<', $now ) )
60                    ->caller( $fname )->fetchFieldValues();
61
62                if ( !$ids ) {
63                    return;
64                }
65
66                $batchSize =
67                    $services->getMainConfig()->get( MainConfigNames::UpdateRowsPerQuery );
68                $ticket = $dbProvider->getEmptyTransactionTicket( $fname );
69                $idBatches = array_chunk( $ids, $batchSize );
70                foreach ( $idBatches as $idBatch ) {
71                    $dbw->newUpdateQueryBuilder()
72                        ->update( 'page' )
73                        ->set( [ 'page_touched' => $now ] )
74                        ->where( [ 'page_id' => $idBatch ] )
75                        ->andWhere( $dbw->expr( 'page_touched', '<', $now ) ) // handle races
76                        ->caller( $fname )->execute();
77                    if ( count( $idBatches ) > 1 ) {
78                        $dbProvider->commitAndWaitForReplication( $fname, $ticket );
79                    }
80                }
81            }
82        ) );
83    }
84}