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