Merge "Don't fallback from uk to ru"
[lhc/web/wiklou.git] / includes / jobqueue / utils / PurgeJobUtils.php
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 */
23 use MediaWiki\MediaWikiServices;
24
25 class PurgeJobUtils {
26 /**
27 * Invalidate the cache of a list of pages from a single namespace.
28 * This is intended for use by subclasses.
29 *
30 * @param IDatabase $dbw
31 * @param int $namespace Namespace number
32 * @param array $dbkeys
33 */
34 public static function invalidatePages( IDatabase $dbw, $namespace, array $dbkeys ) {
35 if ( $dbkeys === [] ) {
36 return;
37 }
38
39 $dbw->onTransactionIdle(
40 function () use ( $dbw, $namespace, $dbkeys ) {
41 $services = MediaWikiServices::getInstance();
42 $lbFactory = $services->getDBLoadBalancerFactory();
43 // Determine which pages need to be updated.
44 // This is necessary to prevent the job queue from smashing the DB with
45 // large numbers of concurrent invalidations of the same page.
46 $now = $dbw->timestamp();
47 $ids = $dbw->selectFieldValues(
48 'page',
49 'page_id',
50 [
51 'page_namespace' => $namespace,
52 'page_title' => $dbkeys,
53 'page_touched < ' . $dbw->addQuotes( $now )
54 ],
55 __METHOD__
56 );
57
58 if ( !$ids ) {
59 return;
60 }
61
62 $batchSize = $services->getMainConfig()->get( 'UpdateRowsPerQuery' );
63 $ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
64 foreach ( array_chunk( $ids, $batchSize ) as $idBatch ) {
65 $dbw->update(
66 'page',
67 [ 'page_touched' => $now ],
68 [
69 'page_id' => $idBatch,
70 'page_touched < ' . $dbw->addQuotes( $now ) // handle races
71 ],
72 __METHOD__
73 );
74 $lbFactory->commitAndWaitForReplication( __METHOD__, $ticket );
75 }
76 },
77 __METHOD__
78 );
79 }
80 }