Completed
Branch master (d7c4e6)
by
unknown
29:20
created

PurgeJobUtils::invalidatePages()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 39
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 20
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 39
rs 8.8571
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
class PurgeJobUtils {
24
	/**
25
	 * Invalidate the cache of a list of pages from a single namespace.
26
	 * This is intended for use by subclasses.
27
	 *
28
	 * @param IDatabase $dbw
29
	 * @param int $namespace Namespace number
30
	 * @param array $dbkeys
31
	 */
32
	public static function invalidatePages( IDatabase $dbw, $namespace, array $dbkeys ) {
33
		if ( $dbkeys === [] ) {
34
			return;
35
		}
36
37
		$dbw->onTransactionPreCommitOrIdle( function() use ( $dbw, $namespace, $dbkeys ) {
38
			// Determine which pages need to be updated.
39
			// This is necessary to prevent the job queue from smashing the DB with
40
			// large numbers of concurrent invalidations of the same page.
41
			$now = $dbw->timestamp();
42
			$ids = $dbw->selectFieldValues(
43
				'page',
44
				'page_id',
45
				[
46
					'page_namespace' => $namespace,
47
					'page_title' => $dbkeys,
48
					'page_touched < ' . $dbw->addQuotes( $now )
49
				],
50
				__METHOD__
51
			);
52
53
			if ( $ids === [] ) {
54
				return;
55
			}
56
57
			// Do the update.
58
			// We still need the page_touched condition, in case the row has changed since
59
			// the non-locking select above.
60
			$dbw->update(
61
				'page',
62
				[ 'page_touched' => $now ],
63
				[
64
					'page_id' => $ids,
65
					'page_touched < ' . $dbw->addQuotes( $now )
66
				],
67
				__METHOD__
68
			);
69
		} );
70
	}
71
}
72