MediaWiki REL1_39
HTMLCacheUpdateJob.php
Go to the documentation of this file.
1<?php
24
37class HTMLCacheUpdateJob extends Job {
39 private const NORMAL_MAX_LAG = 10;
40
41 public function __construct( Title $title, array $params ) {
42 parent::__construct( 'htmlCacheUpdate', $title, $params );
43 // Avoid the overhead of de-duplication when it would be pointless.
44 // Note that these jobs always set page_touched to the current time,
45 // so letting the older existing job "win" is still correct.
46 $this->removeDuplicates = (
47 // Ranges rarely will line up
48 !isset( $params['range'] ) &&
49 // Multiple pages per job make matches unlikely
50 !( isset( $params['pages'] ) && count( $params['pages'] ) != 1 )
51 );
52 $this->params += [ 'causeAction' => 'unknown', 'causeAgent' => 'unknown' ];
53 }
54
62 public static function newForBacklinks( PageReference $page, $table, $params = [] ) {
63 $title = Title::castFromPageReference( $page );
64 return new self(
65 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable castFrom does not return null here
66 $title,
67 [
68 'table' => $table,
69 'recursive' => true
70 ] + Job::newRootJobParams( // "overall" refresh links job info
71 "htmlCacheUpdate:{$table}:{$title->getPrefixedText()}"
72 ) + $params
73 );
74 }
75
76 public function run() {
77 $updateRowsPerJob = MediaWikiServices::getInstance()->getMainConfig()->get(
78 MainConfigNames::UpdateRowsPerJob );
79 $updateRowsPerQuery = MediaWikiServices::getInstance()->getMainConfig()->get(
80 MainConfigNames::UpdateRowsPerQuery );
81 if ( isset( $this->params['table'] ) && !isset( $this->params['pages'] ) ) {
82 $this->params['recursive'] = true; // b/c; base job
83 }
84
85 // Job to purge all (or a range of) backlink pages for a page
86 if ( !empty( $this->params['recursive'] ) ) {
87 // Carry over information for de-duplication
88 $extraParams = $this->getRootJobParams();
89 // Carry over cause information for logging
90 $extraParams['causeAction'] = $this->params['causeAction'];
91 $extraParams['causeAgent'] = $this->params['causeAgent'];
92 // Convert this into no more than $wgUpdateRowsPerJob HTMLCacheUpdateJob per-title
93 // jobs and possibly a recursive HTMLCacheUpdateJob job for the rest of the backlinks
95 $this,
96 $updateRowsPerJob,
97 $updateRowsPerQuery, // jobs-per-title
98 // Carry over information for de-duplication
99 [ 'params' => $extraParams ]
100 );
101 MediaWikiServices::getInstance()->getJobQueueGroup()->push( $jobs );
102 // Job to purge pages for a set of titles
103 } elseif ( isset( $this->params['pages'] ) ) {
104 $this->invalidateTitles( $this->params['pages'] );
105 // Job to update a single title
106 } else {
108 $this->invalidateTitles( [
109 $t->getArticleID() => [ $t->getNamespace(), $t->getDBkey() ]
110 ] );
111 }
112
113 return true;
114 }
115
119 protected function invalidateTitles( array $pages ) {
120 // Get all page IDs in this query into an array
121 $pageIds = array_keys( $pages );
122 if ( !$pageIds ) {
123 return;
124 }
125
126 $rootTsUnix = wfTimestampOrNull( TS_UNIX, $this->params['rootJobTimestamp'] ?? null );
127 // Bump page_touched to the current timestamp. This previously used the root job timestamp
128 // (e.g. template/file edit time), which is a bit more efficient when template edits are
129 // rare and don't effect the same pages much. However, this way better de-duplicates jobs,
130 // which is much more useful for wikis with high edit rates. Note that RefreshLinksJob,
131 // enqueued alongside HTMLCacheUpdateJob, saves the parser output since it has to parse
132 // anyway. We assume that vast majority of the cache jobs finish before the link jobs,
133 // so using the current timestamp instead of the root timestamp is not expected to
134 // invalidate these cache entries too often.
135 $newTouchedUnix = time();
136 // Timestamp used to bypass pages already invalided since the triggering event
137 $casTsUnix = $rootTsUnix ?? $newTouchedUnix;
138
139 $services = MediaWikiServices::getInstance();
140 $config = $services->getMainConfig();
141
142 $lbFactory = $services->getDBLoadBalancerFactory();
143 $dbw = $lbFactory->getMainLB()->getConnectionRef( DB_PRIMARY );
144 $ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
145 // Update page_touched (skipping pages already touched since the root job).
146 // Check $wgUpdateRowsPerQuery; batch jobs are sized by that already.
147 $batches = array_chunk( $pageIds, $config->get( MainConfigNames::UpdateRowsPerQuery ) );
148 foreach ( $batches as $batch ) {
149 $dbw->update( 'page',
150 [ 'page_touched' => $dbw->timestamp( $newTouchedUnix ) ],
151 [
152 'page_id' => $batch,
153 "page_touched < " . $dbw->addQuotes( $dbw->timestamp( $casTsUnix ) )
154 ],
155 __METHOD__
156 );
157 if ( count( $batches ) > 1 ) {
158 $lbFactory->commitAndWaitForReplication( __METHOD__, $ticket );
159 }
160 }
161 // Get the list of affected pages (races only mean something else did the purge)
162 $titleArray = TitleArray::newFromResult( $dbw->select(
163 'page',
164 array_merge(
165 [ 'page_namespace', 'page_title' ],
166 $config->get( MainConfigNames::PageLanguageUseDB ) ? [ 'page_lang' ] : []
167 ),
168 [ 'page_id' => $pageIds, 'page_touched' => $dbw->timestamp( $newTouchedUnix ) ],
169 __METHOD__
170 ) );
171
172 // Update CDN and file caches
173 $htmlCache = MediaWikiServices::getInstance()->getHtmlCacheUpdater();
174 $htmlCache->purgeTitleUrls(
175 $titleArray,
176 $htmlCache::PURGE_NAIVE | $htmlCache::PURGE_URLS_LINKSUPDATE_ONLY,
177 [ $htmlCache::UNLESS_CACHE_MTIME_AFTER => $casTsUnix + self::NORMAL_MAX_LAG ]
178 );
179 }
180
181 public function getDeduplicationInfo() {
182 $info = parent::getDeduplicationInfo();
183 if ( is_array( $info['params'] ) ) {
184 // For per-pages jobs, the job title is that of the template that changed
185 // (or similar), so remove that since it ruins duplicate detection
186 if ( isset( $info['params']['pages'] ) ) {
187 unset( $info['namespace'] );
188 unset( $info['title'] );
189 }
190 }
191
192 return $info;
193 }
194
195 public function workItemCount() {
196 if ( !empty( $this->params['recursive'] ) ) {
197 return 0; // nothing actually purged
198 } elseif ( isset( $this->params['pages'] ) ) {
199 return count( $this->params['pages'] );
200 }
201
202 return 1; // one title
203 }
204}
wfTimestampOrNull( $outputtype=TS_UNIX, $ts=null)
Return a formatted timestamp, or null if input is null.
static partitionBacklinkJob(Job $job, $bSize, $cSize, $opts=[])
Break down $job into approximately ($bSize/$cSize) leaf jobs and a single partition job that covers t...
Job to purge the HTML/file cache for all pages that link to or use another page or file.
getDeduplicationInfo()
Subclasses may need to override this to make duplication detection work.
__construct(Title $title, array $params)
static newForBacklinks(PageReference $page, $table, $params=[])
invalidateTitles(array $pages)
Class to both describe a background job and handle jobs.
Definition Job.php:39
getRootJobParams()
Definition Job.php:362
array $params
Array of job parameters.
Definition Job.php:44
static newRootJobParams( $key)
Get "root job" parameters for a task.
Definition Job.php:348
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
static newFromResult( $res)
Represents a title within MediaWiki.
Definition Title.php:49
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.
const DB_PRIMARY
Definition defines.php:28
return true
Definition router.php:92