Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / includes / jobqueue / jobs / RefreshLinksJob.php
blob651a332d5e820393c6a98d4ec1d8f85bd7b2ded3
1 <?php
2 /**
3 * Job to update link tables for pages
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup JobQueue
23 use MediaWiki\MediaWikiServices;
25 /**
26 * Job to update link tables for pages
28 * This job comes in a few variants:
29 * - a) Recursive jobs to update links for backlink pages for a given title.
30 * These jobs have (recursive:true,table:<table>) set.
31 * - b) Jobs to update links for a set of pages (the job title is ignored).
32 * These jobs have (pages:(<page ID>:(<namespace>,<title>),...) set.
33 * - c) Jobs to update links for a single page (the job title)
34 * These jobs need no extra fields set.
36 * @ingroup JobQueue
38 class RefreshLinksJob extends Job {
39 /** @var float Cache parser output when it takes this long to render */
40 const PARSE_THRESHOLD_SEC = 1.0;
41 /** @var integer Lag safety margin when comparing root job times to last-refresh times */
42 const CLOCK_FUDGE = 10;
43 /** @var integer How many seconds to wait for replica DBs to catch up */
44 const LAG_WAIT_TIMEOUT = 15;
46 function __construct( Title $title, array $params ) {
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 )
57 /**
58 * @param Title $title
59 * @param array $params
60 * @return RefreshLinksJob
62 public static function newPrioritized( Title $title, array $params ) {
63 $job = new self( $title, $params );
64 $job->command = 'refreshLinksPrioritized';
66 return $job;
69 /**
70 * @param Title $title
71 * @param array $params
72 * @return RefreshLinksJob
74 public static function newDynamic( Title $title, array $params ) {
75 $job = new self( $title, $params );
76 $job->command = 'refreshLinksDynamic';
78 return $job;
81 function run() {
82 global $wgUpdateRowsPerJob;
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' );
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
106 $jobs = BacklinkJobUtils::partitionBacklinkJob(
107 $this,
108 $wgUpdateRowsPerJob,
109 1, // job-per-title
110 [ 'params' => $extraParams ]
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 ) );
119 // Job to update link tables for a given title
120 } else {
121 $this->runForTitle( $this->title );
124 return true;
128 * @param Title $title
129 * @return bool
131 protected function runForTitle( Title $title ) {
132 $services = MediaWikiServices::getInstance();
133 $stats = $services->getStatsdDataFactory();
134 $lbFactory = $services->getDBLoadBalancerFactory();
135 $ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
137 $page = WikiPage::factory( $title );
138 $page->loadPageData( WikiPage::READ_LATEST );
140 // Serialize links updates by page ID so they see each others' changes
141 $dbw = $lbFactory->getMainLB()->getConnection( DB_MASTER );
142 /** @noinspection PhpUnusedLocalVariableInspection */
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 );
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
156 } else {
157 // Fetch current revision; READ_LATEST reduces lockAndGetLatest() check failures
158 $revision = Revision::newFromTitle( $title, false, Revision::READ_LATEST );
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;
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();
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'] );
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
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;
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
214 $parserOutput = false; // too stale
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()
235 $ctime = wfTimestamp( TS_MW, (int)$start ); // cache time
236 ParserCache::singleton()->save(
237 $parserOutput, $page, $parserOptions, $ctime, $revision->getId()
240 $stats->increment( 'refreshlinks.parser_uncached' );
243 $updates = $content->getSecondaryDataUpdates(
244 $title,
245 null,
246 !empty( $this->params['useRecursiveLinksUpdate'] ),
247 $parserOutput
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 );
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 );
269 $update->setTriggeringUser( $user );
274 foreach ( $updates as $update ) {
275 $update->setTransactionTicket( $ticket );
276 $update->doUpdate();
279 InfoAction::invalidateCache( $title );
281 return true;
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'] );
295 return $info;
298 public function workItemCount() {
299 return isset( $this->params['pages'] ) ? count( $this->params['pages'] ) : 1;