MediaWiki  1.28.1
RefreshLinksJob.php
Go to the documentation of this file.
1 <?php
24 
38 class RefreshLinksJob extends Job {
40  const PARSE_THRESHOLD_SEC = 1.0;
42  const CLOCK_FUDGE = 10;
44  const LAG_WAIT_TIMEOUT = 15;
45 
47  parent::__construct( 'refreshLinks', $title, $params );
48  // Avoid the overhead of de-duplication when it would be pointless
49  $this->removeDuplicates = (
50  // Ranges rarely will line up
51  !isset( $params['range'] ) &&
52  // Multiple pages per job make matches unlikely
53  !( isset( $params['pages'] ) && count( $params['pages'] ) != 1 )
54  );
55  }
56 
62  public static function newPrioritized( Title $title, array $params ) {
63  $job = new self( $title, $params );
64  $job->command = 'refreshLinksPrioritized';
65 
66  return $job;
67  }
68 
74  public static function newDynamic( Title $title, array $params ) {
75  $job = new self( $title, $params );
76  $job->command = 'refreshLinksDynamic';
77 
78  return $job;
79  }
80 
81  function run() {
82  global $wgUpdateRowsPerJob;
83 
84  // Job to update all (or a range of) backlink pages for a page
85  if ( !empty( $this->params['recursive'] ) ) {
86  // When the base job branches, wait for the replica DBs to catch up to the master.
87  // From then on, we know that any template changes at the time the base job was
88  // enqueued will be reflected in backlink page parses when the leaf jobs run.
89  if ( !isset( $params['range'] ) ) {
90  try {
91  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
92  $lbFactory->waitForReplication( [
93  'wiki' => wfWikiID(),
94  'timeout' => self::LAG_WAIT_TIMEOUT
95  ] );
96  } catch ( DBReplicationWaitError $e ) { // only try so hard
97  $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
98  $stats->increment( 'refreshlinks.lag_wait_failed' );
99  }
100  }
101  // Carry over information for de-duplication
102  $extraParams = $this->getRootJobParams();
103  $extraParams['triggeredRecursive'] = true;
104  // Convert this into no more than $wgUpdateRowsPerJob RefreshLinks per-title
105  // jobs and possibly a recursive RefreshLinks job for the rest of the backlinks
107  $this,
108  $wgUpdateRowsPerJob,
109  1, // job-per-title
110  [ 'params' => $extraParams ]
111  );
112  JobQueueGroup::singleton()->push( $jobs );
113  // Job to update link tables for a set of titles
114  } elseif ( isset( $this->params['pages'] ) ) {
115  foreach ( $this->params['pages'] as $pageId => $nsAndKey ) {
116  list( $ns, $dbKey ) = $nsAndKey;
117  $this->runForTitle( Title::makeTitleSafe( $ns, $dbKey ) );
118  }
119  // Job to update link tables for a given title
120  } else {
121  $this->runForTitle( $this->title );
122  }
123 
124  return true;
125  }
126 
131  protected function runForTitle( Title $title ) {
132  $services = MediaWikiServices::getInstance();
133  $stats = $services->getStatsdDataFactory();
134  $lbFactory = $services->getDBLoadBalancerFactory();
135  $ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
136 
137  $page = WikiPage::factory( $title );
138  $page->loadPageData( WikiPage::READ_LATEST );
139 
140  // Serialize links updates by page ID so they see each others' changes
141  $dbw = $lbFactory->getMainLB()->getConnection( DB_MASTER );
143  $scopedLock = LinksUpdate::acquirePageLock( $dbw, $page->getId(), 'job' );
144  // Get the latest ID *after* acquirePageLock() flushed the transaction.
145  // This is used to detect edits/moves after loadPageData() but before the scope lock.
146  // The works around the chicken/egg problem of determining the scope lock key.
147  $latest = $title->getLatestRevID( Title::GAID_FOR_UPDATE );
148 
149  if ( !empty( $this->params['triggeringRevisionId'] ) ) {
150  // Fetch the specified revision; lockAndGetLatest() below detects if the page
151  // was edited since and aborts in order to avoid corrupting the link tables
152  $revision = Revision::newFromId(
153  $this->params['triggeringRevisionId'],
154  Revision::READ_LATEST
155  );
156  } else {
157  // Fetch current revision; READ_LATEST reduces lockAndGetLatest() check failures
158  $revision = Revision::newFromTitle( $title, false, Revision::READ_LATEST );
159  }
160 
161  if ( !$revision ) {
162  $stats->increment( 'refreshlinks.rev_not_found' );
163  $this->setLastError( "Revision not found for {$title->getPrefixedDBkey()}" );
164  return false; // just deleted?
165  } elseif ( $revision->getId() != $latest || $revision->getPage() !== $page->getId() ) {
166  // Do not clobber over newer updates with older ones. If all jobs where FIFO and
167  // serialized, it would be OK to update links based on older revisions since it
168  // would eventually get to the latest. Since that is not the case (by design),
169  // only update the link tables to a state matching the current revision's output.
170  $stats->increment( 'refreshlinks.rev_not_current' );
171  $this->setLastError( "Revision {$revision->getId()} is not current" );
172  return false;
173  }
174 
175  $content = $revision->getContent( Revision::RAW );
176  if ( !$content ) {
177  // If there is no content, pretend the content is empty
178  $content = $revision->getContentHandler()->makeEmptyContent();
179  }
180 
181  $parserOutput = false;
182  $parserOptions = $page->makeParserOptions( 'canonical' );
183  // If page_touched changed after this root job, then it is likely that
184  // any views of the pages already resulted in re-parses which are now in
185  // cache. The cache can be reused to avoid expensive parsing in some cases.
186  if ( isset( $this->params['rootJobTimestamp'] ) ) {
187  $opportunistic = !empty( $this->params['isOpportunistic'] );
188 
189  $skewedTimestamp = $this->params['rootJobTimestamp'];
190  if ( $opportunistic ) {
191  // Neither clock skew nor DB snapshot/replica DB lag matter much for such
192  // updates; focus on reusing the (often recently updated) cache
193  } else {
194  // For transclusion updates, the template changes must be reflected
195  $skewedTimestamp = wfTimestamp( TS_MW,
196  wfTimestamp( TS_UNIX, $skewedTimestamp ) + self::CLOCK_FUDGE
197  );
198  }
199 
200  if ( $page->getLinksTimestamp() > $skewedTimestamp ) {
201  // Something already updated the backlinks since this job was made
202  $stats->increment( 'refreshlinks.update_skipped' );
203  return true;
204  }
205 
206  if ( $page->getTouched() >= $this->params['rootJobTimestamp'] || $opportunistic ) {
207  // Cache is suspected to be up-to-date. As long as the cache rev ID matches
208  // and it reflects the job's triggering change, then it is usable.
209  $parserOutput = ParserCache::singleton()->getDirty( $page, $parserOptions );
210  if ( !$parserOutput
211  || $parserOutput->getCacheRevisionId() != $revision->getId()
212  || $parserOutput->getCacheTime() < $skewedTimestamp
213  ) {
214  $parserOutput = false; // too stale
215  }
216  }
217  }
218 
219  // Fetch the current revision and parse it if necessary...
220  if ( $parserOutput ) {
221  $stats->increment( 'refreshlinks.parser_cached' );
222  } else {
223  $start = microtime( true );
224  // Revision ID must be passed to the parser output to get revision variables correct
225  $parserOutput = $content->getParserOutput(
226  $title, $revision->getId(), $parserOptions, false );
227  $elapsed = microtime( true ) - $start;
228  // If it took a long time to render, then save this back to the cache to avoid
229  // wasted CPU by other apaches or job runners. We don't want to always save to
230  // cache as this can cause high cache I/O and LRU churn when a template changes.
231  if ( $elapsed >= self::PARSE_THRESHOLD_SEC
232  && $page->shouldCheckParserCache( $parserOptions, $revision->getId() )
233  && $parserOutput->isCacheable()
234  ) {
235  $ctime = wfTimestamp( TS_MW, (int)$start ); // cache time
236  ParserCache::singleton()->save(
237  $parserOutput, $page, $parserOptions, $ctime, $revision->getId()
238  );
239  }
240  $stats->increment( 'refreshlinks.parser_uncached' );
241  }
242 
243  $updates = $content->getSecondaryDataUpdates(
244  $title,
245  null,
246  !empty( $this->params['useRecursiveLinksUpdate'] ),
248  );
249 
250  // For legacy hook handlers doing updates via LinksUpdateConstructed, make sure
251  // any pending writes they made get flushed before the doUpdate() calls below.
252  // This avoids snapshot-clearing errors in LinksUpdate::acquirePageLock().
253  $lbFactory->commitAndWaitForReplication( __METHOD__, $ticket );
254 
255  foreach ( $updates as $key => $update ) {
256  // FIXME: This code probably shouldn't be here?
257  // Needed by things like Echo notifications which need
258  // to know which user caused the links update
259  if ( $update instanceof LinksUpdate ) {
260  $update->setRevision( $revision );
261  if ( !empty( $this->params['triggeringUser'] ) ) {
262  $userInfo = $this->params['triggeringUser'];
263  if ( $userInfo['userId'] ) {
264  $user = User::newFromId( $userInfo['userId'] );
265  } else {
266  // Anonymous, use the username
267  $user = User::newFromName( $userInfo['userName'], false );
268  }
269  $update->setTriggeringUser( $user );
270  }
271  }
272  }
273 
274  foreach ( $updates as $update ) {
275  $update->setTransactionTicket( $ticket );
276  $update->doUpdate();
277  }
278 
279  InfoAction::invalidateCache( $title );
280 
281  return true;
282  }
283 
284  public function getDeduplicationInfo() {
285  $info = parent::getDeduplicationInfo();
286  if ( is_array( $info['params'] ) ) {
287  // For per-pages jobs, the job title is that of the template that changed
288  // (or similar), so remove that since it ruins duplicate detection
289  if ( isset( $info['pages'] ) ) {
290  unset( $info['namespace'] );
291  unset( $info['title'] );
292  }
293  }
294 
295  return $info;
296  }
297 
298  public function workItemCount() {
299  return isset( $this->params['pages'] ) ? count( $this->params['pages'] ) : 1;
300  }
301 }
static newFromName($name, $validate= 'valid')
Static factory method for creation from username.
Definition: User.php:525
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:115
getLatestRevID($flags=0)
What is the page_latest field for this page?
Definition: Title.php:3298
__construct(Title $title, array $params)
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
the array() calling protocol came about after MediaWiki 1.4rc1.
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Class the manages updates of *_link tables as well as similar extension-managed tables.
Definition: LinksUpdate.php:33
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException'returning false will NOT prevent logging $e
Definition: hooks.txt:2102
Class to both describe a background job and handle jobs.
Definition: Job.php:31
static newFromId($id)
Static factory method for creation from a given user ID.
Definition: User.php:548
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:23
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
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:128
title
const DB_MASTER
Definition: defines.php:23
const TS_UNIX
Unix time - the number of seconds since 1970-01-01 00:00:00 UTC.
Definition: defines.php:6
wfTimestamp($outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
runForTitle(Title $title)
static invalidateCache(Title $title, $revid=null)
Clear the info cache for a given Title.
Definition: InfoAction.php:69
Exception class for replica DB wait timeouts.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context $parserOutput
Definition: hooks.txt:1046
getRootJobParams()
Definition: Job.php:274
const GAID_FOR_UPDATE
Used to be GAID_FOR_UPDATE define.
Definition: Title.php:51
const TS_MW
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
Definition: defines.php:11
static makeTitleSafe($ns, $title, $fragment= '', $interwiki= '')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:535
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
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:2159
const RAW
Definition: Revision.php:94
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:9
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 local account $user
Definition: hooks.txt:242
static singleton($wiki=false)
static singleton()
Get an instance of this object.
Definition: ParserCache.php:36
Job to update link tables for pages.
static newFromId($id, $flags=0)
Load a page revision from a given revision ID number.
Definition: Revision.php:110
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:35
static newDynamic(Title $title, array $params)
$lbFactory
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content $content
Definition: hooks.txt:1046
if(count($args)< 1) $job
setLastError($error)
Definition: Job.php:393
getDeduplicationInfo()
Subclasses may need to override this to make duplication detection work.
static newPrioritized(Title $title, array $params)
array $params
Array of job parameters.
Definition: Job.php:36
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 acquirePageLock(IDatabase $dbw, $pageId, $why= 'atomicity')
Acquire a lock for performing link table updates for a page on a DB.
Title $title
Definition: Job.php:42
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached $page
Definition: hooks.txt:2491