MediaWiki REL1_30
HTMLCacheUpdateJob.php
Go to the documentation of this file.
1<?php
26
38class HTMLCacheUpdateJob extends Job {
39 function __construct( Title $title, array $params ) {
40 parent::__construct( 'htmlCacheUpdate', $title, $params );
41 // Avoid the overhead of de-duplication when it would be pointless.
42 // Note that these jobs always set page_touched to the current time,
43 // so letting the older existing job "win" is still correct.
44 $this->removeDuplicates = (
45 // Ranges rarely will line up
46 !isset( $params['range'] ) &&
47 // Multiple pages per job make matches unlikely
48 !( isset( $params['pages'] ) && count( $params['pages'] ) != 1 )
49 );
50 }
51
57 public static function newForBacklinks( Title $title, $table ) {
58 return new self(
59 $title,
60 [
61 'table' => $table,
62 'recursive' => true
63 ] + Job::newRootJobParams( // "overall" refresh links job info
64 "htmlCacheUpdate:{$table}:{$title->getPrefixedText()}"
65 )
66 );
67 }
68
69 function run() {
71
72 if ( isset( $this->params['table'] ) && !isset( $this->params['pages'] ) ) {
73 $this->params['recursive'] = true; // b/c; base job
74 }
75
76 // Job to purge all (or a range of) backlink pages for a page
77 if ( !empty( $this->params['recursive'] ) ) {
78 // Convert this into no more than $wgUpdateRowsPerJob HTMLCacheUpdateJob per-title
79 // jobs and possibly a recursive HTMLCacheUpdateJob job for the rest of the backlinks
81 $this,
83 $wgUpdateRowsPerQuery, // jobs-per-title
84 // Carry over information for de-duplication
85 [ 'params' => $this->getRootJobParams() ]
86 );
87 JobQueueGroup::singleton()->push( $jobs );
88 // Job to purge pages for a set of titles
89 } elseif ( isset( $this->params['pages'] ) ) {
90 $this->invalidateTitles( $this->params['pages'] );
91 // Job to update a single title
92 } else {
94 $this->invalidateTitles( [
95 $t->getArticleID() => [ $t->getNamespace(), $t->getDBkey() ]
96 ] );
97 }
98
99 return true;
100 }
101
105 protected function invalidateTitles( array $pages ) {
107
108 // Get all page IDs in this query into an array
109 $pageIds = array_keys( $pages );
110 if ( !$pageIds ) {
111 return;
112 }
113
114 // Bump page_touched to the current timestamp. This used to use the root job timestamp
115 // (e.g. template/file edit time), which was a bit more efficient when template edits are
116 // rare and don't effect the same pages much. However, this way allows for better
117 // de-duplication, which is much more useful for wikis with high edit rates. Note that
118 // RefreshLinksJob, which is enqueued alongside HTMLCacheUpdateJob, saves the parser output
119 // since it has to parse anyway. We assume that vast majority of the cache jobs finish
120 // before the link jobs, so using the current timestamp instead of the root timestamp is
121 // not expected to invalidate these cache entries too often.
122 $touchTimestamp = wfTimestampNow();
123 // If page_touched is higher than this, then something else already bumped it after enqueue
124 $condTimestamp = isset( $this->params['rootJobTimestamp'] )
125 ? $this->params['rootJobTimestamp']
126 : $touchTimestamp;
127
128 $dbw = wfGetDB( DB_MASTER );
129 $factory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
130 $ticket = $factory->getEmptyTransactionTicket( __METHOD__ );
131 // Update page_touched (skipping pages already touched since the root job).
132 // Check $wgUpdateRowsPerQuery for sanity; batch jobs are sized by that already.
133 foreach ( array_chunk( $pageIds, $wgUpdateRowsPerQuery ) as $batch ) {
134 $factory->commitAndWaitForReplication( __METHOD__, $ticket );
135
136 $dbw->update( 'page',
137 [ 'page_touched' => $dbw->timestamp( $touchTimestamp ) ],
138 [ 'page_id' => $batch,
139 // don't invalidated pages that were already invalidated
140 "page_touched < " . $dbw->addQuotes( $dbw->timestamp( $condTimestamp ) )
141 ],
142 __METHOD__
143 );
144 }
145 // Get the list of affected pages (races only mean something else did the purge)
146 $titleArray = TitleArray::newFromResult( $dbw->select(
147 'page',
148 [ 'page_namespace', 'page_title' ],
149 [ 'page_id' => $pageIds, 'page_touched' => $dbw->timestamp( $touchTimestamp ) ],
150 __METHOD__
151 ) );
152
153 // Update CDN; call purge() directly so as to not bother with secondary purges
154 $urls = [];
155 foreach ( $titleArray as $title ) {
157 $urls = array_merge( $urls, $title->getCdnUrls() );
158 }
160
161 // Update file cache
162 if ( $wgUseFileCache ) {
163 foreach ( $titleArray as $title ) {
165 }
166 }
167 }
168
169 public function workItemCount() {
170 if ( !empty( $this->params['recursive'] ) ) {
171 return 0; // nothing actually purged
172 } elseif ( isset( $this->params['pages'] ) ) {
173 return count( $this->params['pages'] );
174 }
175
176 return 1; // one title
177 }
178}
$wgUpdateRowsPerJob
Number of rows to update per job.
$wgUseFileCache
This will cache static pages for non-logged-in users to reduce database traffic on public sites.
$wgUpdateRowsPerQuery
Number of rows to update per query.
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
static partitionBacklinkJob(Job $job, $bSize, $cSize, $opts=[])
Break down $job into approximately ($bSize/$cSize) leaf jobs and a single partition job that covers t...
static purge(array $urlArr)
Purges a list of CDN nodes defined in $wgSquidServers.
Job to purge the cache for all pages that link to or use another page or file.
__construct(Title $title, array $params)
static newForBacklinks(Title $title, $table)
invalidateTitles(array $pages)
static clearFileCache(Title $title)
Clear the file caches for a page for all actions.
static singleton( $wiki=false)
Class to both describe a background job and handle jobs.
Definition Job.php:31
Title $title
Definition Job.php:42
getRootJobParams()
Definition Job.php:284
array $params
Array of job parameters.
Definition Job.php:36
static newRootJobParams( $key)
Get "root job" parameters for a task.
Definition Job.php:271
MediaWikiServices is the service locator for the application scope of MediaWiki.
static newFromResult( $res)
Represents a title within MediaWiki.
Definition Title.php:39
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition hooks.txt:1976
$batch
Definition linkcache.txt:23
const DB_MASTER
Definition defines.php:26