MediaWiki  REL1_31
RefreshLinksJob.php
Go to the documentation of this file.
1 <?php
25 
39 class RefreshLinksJob extends Job {
41  const PARSE_THRESHOLD_SEC = 1.0;
43  const CLOCK_FUDGE = 10;
45  const LAG_WAIT_TIMEOUT = 15;
46 
48  parent::__construct( 'refreshLinks', $title, $params );
49  // Avoid the overhead of de-duplication when it would be pointless
50  $this->removeDuplicates = (
51  // Ranges rarely will line up
52  !isset( $params['range'] ) &&
53  // Multiple pages per job make matches unlikely
54  !( isset( $params['pages'] ) && count( $params['pages'] ) != 1 )
55  );
56  $this->params += [ 'causeAction' => 'unknown', 'causeAgent' => 'unknown' ];
57  }
58 
64  public static function newPrioritized( Title $title, array $params ) {
65  $job = new self( $title, $params );
66  $job->command = 'refreshLinksPrioritized';
67 
68  return $job;
69  }
70 
76  public static function newDynamic( Title $title, array $params ) {
77  $job = new self( $title, $params );
78  $job->command = 'refreshLinksDynamic';
79 
80  return $job;
81  }
82 
83  function run() {
85 
86  // Job to update all (or a range of) backlink pages for a page
87  if ( !empty( $this->params['recursive'] ) ) {
88  // When the base job branches, wait for the replica DBs to catch up to the master.
89  // From then on, we know that any template changes at the time the base job was
90  // enqueued will be reflected in backlink page parses when the leaf jobs run.
91  if ( !isset( $this->params['range'] ) ) {
92  try {
93  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
94  $lbFactory->waitForReplication( [
95  'wiki' => wfWikiID(),
96  'timeout' => self::LAG_WAIT_TIMEOUT
97  ] );
98  } catch ( DBReplicationWaitError $e ) { // only try so hard
99  $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
100  $stats->increment( 'refreshlinks.lag_wait_failed' );
101  }
102  }
103  // Carry over information for de-duplication
104  $extraParams = $this->getRootJobParams();
105  $extraParams['triggeredRecursive'] = true;
106  // Carry over cause information for logging
107  $extraParams['causeAction'] = $this->params['causeAction'];
108  $extraParams['causeAgent'] = $this->params['causeAgent'];
109  // Convert this into no more than $wgUpdateRowsPerJob RefreshLinks per-title
110  // jobs and possibly a recursive RefreshLinks job for the rest of the backlinks
112  $this,
114  1, // job-per-title
115  [ 'params' => $extraParams ]
116  );
117  JobQueueGroup::singleton()->push( $jobs );
118  // Job to update link tables for a set of titles
119  } elseif ( isset( $this->params['pages'] ) ) {
120  foreach ( $this->params['pages'] as $nsAndKey ) {
121  list( $ns, $dbKey ) = $nsAndKey;
122  $this->runForTitle( Title::makeTitleSafe( $ns, $dbKey ) );
123  }
124  // Job to update link tables for a given title
125  } else {
126  $this->runForTitle( $this->title );
127  }
128 
129  return true;
130  }
131 
136  protected function runForTitle( Title $title ) {
137  $services = MediaWikiServices::getInstance();
138  $stats = $services->getStatsdDataFactory();
139  $lbFactory = $services->getDBLoadBalancerFactory();
140  $ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
141 
142  $page = WikiPage::factory( $title );
143  $page->loadPageData( WikiPage::READ_LATEST );
144 
145  // Serialize links updates by page ID so they see each others' changes
146  $dbw = $lbFactory->getMainLB()->getConnection( DB_MASTER );
148  $scopedLock = LinksUpdate::acquirePageLock( $dbw, $page->getId(), 'job' );
149  // Get the latest ID *after* acquirePageLock() flushed the transaction.
150  // This is used to detect edits/moves after loadPageData() but before the scope lock.
151  // The works around the chicken/egg problem of determining the scope lock key.
153 
154  if ( !empty( $this->params['triggeringRevisionId'] ) ) {
155  // Fetch the specified revision; lockAndGetLatest() below detects if the page
156  // was edited since and aborts in order to avoid corrupting the link tables
157  $revision = Revision::newFromId(
158  $this->params['triggeringRevisionId'],
159  Revision::READ_LATEST
160  );
161  } else {
162  // Fetch current revision; READ_LATEST reduces lockAndGetLatest() check failures
163  $revision = Revision::newFromTitle( $title, false, Revision::READ_LATEST );
164  }
165 
166  if ( !$revision ) {
167  $stats->increment( 'refreshlinks.rev_not_found' );
168  $this->setLastError( "Revision not found for {$title->getPrefixedDBkey()}" );
169  return false; // just deleted?
170  } elseif ( $revision->getId() != $latest || $revision->getPage() !== $page->getId() ) {
171  // Do not clobber over newer updates with older ones. If all jobs where FIFO and
172  // serialized, it would be OK to update links based on older revisions since it
173  // would eventually get to the latest. Since that is not the case (by design),
174  // only update the link tables to a state matching the current revision's output.
175  $stats->increment( 'refreshlinks.rev_not_current' );
176  $this->setLastError( "Revision {$revision->getId()} is not current" );
177  return false;
178  }
179 
180  $content = $revision->getContent( Revision::RAW );
181  if ( !$content ) {
182  // If there is no content, pretend the content is empty
183  $content = $revision->getContentHandler()->makeEmptyContent();
184  }
185 
186  $parserOutput = false;
187  $parserOptions = $page->makeParserOptions( 'canonical' );
188  // If page_touched changed after this root job, then it is likely that
189  // any views of the pages already resulted in re-parses which are now in
190  // cache. The cache can be reused to avoid expensive parsing in some cases.
191  if ( isset( $this->params['rootJobTimestamp'] ) ) {
192  $opportunistic = !empty( $this->params['isOpportunistic'] );
193 
194  $skewedTimestamp = $this->params['rootJobTimestamp'];
195  if ( $opportunistic ) {
196  // Neither clock skew nor DB snapshot/replica DB lag matter much for such
197  // updates; focus on reusing the (often recently updated) cache
198  } else {
199  // For transclusion updates, the template changes must be reflected
200  $skewedTimestamp = wfTimestamp( TS_MW,
201  wfTimestamp( TS_UNIX, $skewedTimestamp ) + self::CLOCK_FUDGE
202  );
203  }
204 
205  if ( $page->getLinksTimestamp() > $skewedTimestamp ) {
206  // Something already updated the backlinks since this job was made
207  $stats->increment( 'refreshlinks.update_skipped' );
208  return true;
209  }
210 
211  if ( $page->getTouched() >= $this->params['rootJobTimestamp'] || $opportunistic ) {
212  // Cache is suspected to be up-to-date. As long as the cache rev ID matches
213  // and it reflects the job's triggering change, then it is usable.
214  $parserOutput = $services->getParserCache()->getDirty( $page, $parserOptions );
215  if ( !$parserOutput
216  || $parserOutput->getCacheRevisionId() != $revision->getId()
217  || $parserOutput->getCacheTime() < $skewedTimestamp
218  ) {
219  $parserOutput = false; // too stale
220  }
221  }
222  }
223 
224  // Fetch the current revision and parse it if necessary...
225  if ( $parserOutput ) {
226  $stats->increment( 'refreshlinks.parser_cached' );
227  } else {
228  $start = microtime( true );
229  // Revision ID must be passed to the parser output to get revision variables correct
230  $parserOutput = $content->getParserOutput(
231  $title, $revision->getId(), $parserOptions, false );
232  $elapsed = microtime( true ) - $start;
233  // If it took a long time to render, then save this back to the cache to avoid
234  // wasted CPU by other apaches or job runners. We don't want to always save to
235  // cache as this can cause high cache I/O and LRU churn when a template changes.
236  if ( $elapsed >= self::PARSE_THRESHOLD_SEC
237  && $page->shouldCheckParserCache( $parserOptions, $revision->getId() )
238  && $parserOutput->isCacheable()
239  ) {
240  $ctime = wfTimestamp( TS_MW, (int)$start ); // cache time
241  $services->getParserCache()->save(
242  $parserOutput, $page, $parserOptions, $ctime, $revision->getId()
243  );
244  }
245  $stats->increment( 'refreshlinks.parser_uncached' );
246  }
247 
248  $updates = $content->getSecondaryDataUpdates(
249  $title,
250  null,
251  !empty( $this->params['useRecursiveLinksUpdate'] ),
252  $parserOutput
253  );
254 
255  // For legacy hook handlers doing updates via LinksUpdateConstructed, make sure
256  // any pending writes they made get flushed before the doUpdate() calls below.
257  // This avoids snapshot-clearing errors in LinksUpdate::acquirePageLock().
258  $lbFactory->commitAndWaitForReplication( __METHOD__, $ticket );
259 
260  foreach ( $updates as $update ) {
261  // Carry over cause in case so the update can do extra logging
262  $update->setCause( $this->params['causeAction'], $this->params['causeAgent'] );
263  // FIXME: This code probably shouldn't be here?
264  // Needed by things like Echo notifications which need
265  // to know which user caused the links update
266  if ( $update instanceof LinksUpdate ) {
267  $update->setRevision( $revision );
268  if ( !empty( $this->params['triggeringUser'] ) ) {
269  $userInfo = $this->params['triggeringUser'];
270  if ( $userInfo['userId'] ) {
271  $user = User::newFromId( $userInfo['userId'] );
272  } else {
273  // Anonymous, use the username
274  $user = User::newFromName( $userInfo['userName'], false );
275  }
276  $update->setTriggeringUser( $user );
277  }
278  }
279  }
280 
281  foreach ( $updates as $update ) {
282  $update->setTransactionTicket( $ticket );
283  $update->doUpdate();
284  }
285 
287 
288  // Commit any writes here in case this method is called in a loop.
289  // In that case, the scoped lock will fail to be acquired.
290  $lbFactory->commitAndWaitForReplication( __METHOD__, $ticket );
291 
292  return true;
293  }
294 
295  public function getDeduplicationInfo() {
296  $info = parent::getDeduplicationInfo();
297  unset( $info['causeAction'] );
298  unset( $info['causeAgent'] );
299  if ( is_array( $info['params'] ) ) {
300  // For per-pages jobs, the job title is that of the template that changed
301  // (or similar), so remove that since it ruins duplicate detection
302  if ( isset( $info['params']['pages'] ) ) {
303  unset( $info['namespace'] );
304  unset( $info['title'] );
305  }
306  }
307 
308  return $info;
309  }
310 
311  public function workItemCount() {
312  if ( !empty( $this->params['recursive'] ) ) {
313  return 0; // nothing actually refreshed
314  } elseif ( isset( $this->params['pages'] ) ) {
315  return count( $this->params['pages'] );
316  }
317 
318  return 1; // one title
319  }
320 }
Job\getRootJobParams
getRootJobParams()
Definition: Job.php:299
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:247
User\newFromId
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:614
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
Revision\newFromId
static newFromId( $id, $flags=0)
Load a page revision from a given revision ID number.
Definition: Revision.php:114
LinksUpdate\acquirePageLock
static acquirePageLock(IDatabase $dbw, $pageId, $why='atomicity')
Acquire a lock for performing link table updates for a page on a DB.
Definition: LinksUpdate.php:201
array
the array() calling protocol came about after MediaWiki 1.4rc1.
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1980
Wikimedia\Rdbms\DBReplicationWaitError
Exception class for replica DB wait timeouts.
Definition: DBReplicationWaitError.php:28
Job\$title
Title $title
Definition: Job.php:42
RefreshLinksJob\__construct
__construct(Title $title, array $params)
Definition: RefreshLinksJob.php:47
RefreshLinksJob\getDeduplicationInfo
getDeduplicationInfo()
Subclasses may need to override this to make duplication detection work.
Definition: RefreshLinksJob.php:295
LinksUpdate
Class the manages updates of *_link tables as well as similar extension-managed tables.
Definition: LinksUpdate.php:34
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:591
RefreshLinksJob\newDynamic
static newDynamic(Title $title, array $params)
Definition: RefreshLinksJob.php:76
RefreshLinksJob\newPrioritized
static newPrioritized(Title $title, array $params)
Definition: RefreshLinksJob.php:64
Job\$params
array $params
Array of job parameters.
Definition: Job.php:36
Job\setLastError
setLastError( $error)
Definition: Job.php:419
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
BacklinkJobUtils\partitionBacklinkJob
static partitionBacklinkJob(Job $job, $bSize, $cSize, $opts=[])
Break down $job into approximately ($bSize/$cSize) leaf jobs and a single partition job that covers t...
Definition: BacklinkJobUtils.php:87
Job
Class to both describe a background job and handle jobs.
Definition: Job.php:31
Revision\newFromTitle
static newFromTitle(LinkTarget $linkTarget, $id=0, $flags=0)
Load either the current, or a specified, revision that's attached to a given link target.
Definition: Revision.php:133
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:115
RefreshLinksJob\run
run()
Run the job.
Definition: RefreshLinksJob.php:83
title
title
Definition: parserTests.txt:219
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:95
$wgUpdateRowsPerJob
$wgUpdateRowsPerJob
Number of rows to update per job.
Definition: DefaultSettings.php:8432
DB_MASTER
const DB_MASTER
Definition: defines.php:26
$services
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title after the basic globals have been set but before ordinary actions take place or wrap services the preferred way to define a new service is the $wgServiceWiringFiles array $services
Definition: hooks.txt:2273
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:562
wfWikiID
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
Definition: GlobalFunctions.php:2763
RefreshLinksJob
Job to update link tables for pages.
Definition: RefreshLinksJob.php:39
RefreshLinksJob\workItemCount
workItemCount()
Definition: RefreshLinksJob.php:311
Title\getLatestRevID
getLatestRevID( $flags=0)
What is the page_latest field for this page?
Definition: Title.php:3525
Title\GAID_FOR_UPDATE
const GAID_FOR_UPDATE
Used to be GAID_FOR_UPDATE define.
Definition: Title.php:54
Revision\RAW
const RAW
Definition: Revision.php:57
Title
Represents a title within MediaWiki.
Definition: Title.php:39
JobQueueGroup\singleton
static singleton( $domain=false)
Definition: JobQueueGroup.php:72
$job
if(count( $args)< 1) $job
Definition: recompressTracked.php:47
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:22
InfoAction\invalidateCache
static invalidateCache(Title $title, $revid=null)
Clear the info cache for a given Title.
Definition: InfoAction.php:70
RefreshLinksJob\runForTitle
runForTitle(Title $title)
Definition: RefreshLinksJob.php:136
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:25
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2171