Merge "Add MessagesBi.php"
[lhc/web/wiklou.git] / includes / jobqueue / jobs / RefreshLinksJob.php
1 <?php
2 /**
3 * Job to update link tables for pages
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup JobQueue
22 */
23 use MediaWiki\MediaWikiServices;
24
25 /**
26 * Job to update link tables for pages
27 *
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.
35 *
36 * @ingroup JobQueue
37 */
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 int Lag safety margin when comparing root job times to last-refresh times */
42 const CLOCK_FUDGE = 10;
43 /** @var int How many seconds to wait for replica DBs to catch up */
44 const LAG_WAIT_TIMEOUT = 15;
45
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 )
54 );
55 $this->params += [ 'causeAction' => 'unknown', 'causeAgent' => 'unknown' ];
56 }
57
58 /**
59 * @param Title $title
60 * @param array $params
61 * @return RefreshLinksJob
62 */
63 public static function newPrioritized( Title $title, array $params ) {
64 $job = new self( $title, $params );
65 $job->command = 'refreshLinksPrioritized';
66
67 return $job;
68 }
69
70 /**
71 * @param Title $title
72 * @param array $params
73 * @return RefreshLinksJob
74 */
75 public static function newDynamic( Title $title, array $params ) {
76 $job = new self( $title, $params );
77 $job->command = 'refreshLinksDynamic';
78
79 return $job;
80 }
81
82 function run() {
83 global $wgUpdateRowsPerJob;
84
85 // Job to update all (or a range of) backlink pages for a page
86 if ( !empty( $this->params['recursive'] ) ) {
87 // When the base job branches, wait for the replica DBs to catch up to the master.
88 // From then on, we know that any template changes at the time the base job was
89 // enqueued will be reflected in backlink page parses when the leaf jobs run.
90 if ( !isset( $this->params['range'] ) ) {
91 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
92 if ( !$lbFactory->waitForReplication( [
93 'wiki' => wfWikiID(),
94 'timeout' => self::LAG_WAIT_TIMEOUT
95 ] ) ) { // only try so hard
96 $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
97 $stats->increment( 'refreshlinks.lag_wait_failed' );
98 }
99 }
100 // Carry over information for de-duplication
101 $extraParams = $this->getRootJobParams();
102 $extraParams['triggeredRecursive'] = true;
103 // Carry over cause information for logging
104 $extraParams['causeAction'] = $this->params['causeAction'];
105 $extraParams['causeAgent'] = $this->params['causeAgent'];
106 // Convert this into no more than $wgUpdateRowsPerJob RefreshLinks per-title
107 // jobs and possibly a recursive RefreshLinks job for the rest of the backlinks
108 $jobs = BacklinkJobUtils::partitionBacklinkJob(
109 $this,
110 $wgUpdateRowsPerJob,
111 1, // job-per-title
112 [ 'params' => $extraParams ]
113 );
114 JobQueueGroup::singleton()->push( $jobs );
115 // Job to update link tables for a set of titles
116 } elseif ( isset( $this->params['pages'] ) ) {
117 foreach ( $this->params['pages'] as $nsAndKey ) {
118 list( $ns, $dbKey ) = $nsAndKey;
119 $this->runForTitle( Title::makeTitleSafe( $ns, $dbKey ) );
120 }
121 // Job to update link tables for a given title
122 } else {
123 $this->runForTitle( $this->title );
124 }
125
126 return true;
127 }
128
129 /**
130 * @param Title $title
131 * @return bool
132 */
133 protected function runForTitle( Title $title ) {
134 $services = MediaWikiServices::getInstance();
135 $stats = $services->getStatsdDataFactory();
136 $lbFactory = $services->getDBLoadBalancerFactory();
137 $ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
138
139 $page = WikiPage::factory( $title );
140 $page->loadPageData( WikiPage::READ_LATEST );
141
142 // Serialize links updates by page ID so they see each others' changes
143 $dbw = $lbFactory->getMainLB()->getConnection( DB_MASTER );
144 /** @noinspection PhpUnusedLocalVariableInspection */
145 $scopedLock = LinksUpdate::acquirePageLock( $dbw, $page->getId(), 'job' );
146 if ( $scopedLock === null ) {
147 // Another job is already updating the page, likely for an older revision (T170596).
148 $this->setLastError( 'LinksUpdate already running for this page, try again later.' );
149 return false;
150 }
151 // Get the latest ID *after* acquirePageLock() flushed the transaction.
152 // This is used to detect edits/moves after loadPageData() but before the scope lock.
153 // The works around the chicken/egg problem of determining the scope lock key.
154 $latest = $title->getLatestRevID( Title::GAID_FOR_UPDATE );
155
156 if ( !empty( $this->params['triggeringRevisionId'] ) ) {
157 // Fetch the specified revision; lockAndGetLatest() below detects if the page
158 // was edited since and aborts in order to avoid corrupting the link tables
159 $revision = Revision::newFromId(
160 $this->params['triggeringRevisionId'],
161 Revision::READ_LATEST
162 );
163 } else {
164 // Fetch current revision; READ_LATEST reduces lockAndGetLatest() check failures
165 $revision = Revision::newFromTitle( $title, false, Revision::READ_LATEST );
166 }
167
168 if ( !$revision ) {
169 $stats->increment( 'refreshlinks.rev_not_found' );
170 $this->setLastError( "Revision not found for {$title->getPrefixedDBkey()}" );
171 return false; // just deleted?
172 } elseif ( $revision->getId() != $latest || $revision->getPage() !== $page->getId() ) {
173 // Do not clobber over newer updates with older ones. If all jobs where FIFO and
174 // serialized, it would be OK to update links based on older revisions since it
175 // would eventually get to the latest. Since that is not the case (by design),
176 // only update the link tables to a state matching the current revision's output.
177 $stats->increment( 'refreshlinks.rev_not_current' );
178 $this->setLastError( "Revision {$revision->getId()} is not current" );
179 return false;
180 }
181
182 $content = $revision->getContent( Revision::RAW );
183 if ( !$content ) {
184 // If there is no content, pretend the content is empty
185 $content = $revision->getContentHandler()->makeEmptyContent();
186 }
187
188 $parserOutput = false;
189 $parserOptions = $page->makeParserOptions( 'canonical' );
190 // If page_touched changed after this root job, then it is likely that
191 // any views of the pages already resulted in re-parses which are now in
192 // cache. The cache can be reused to avoid expensive parsing in some cases.
193 if ( isset( $this->params['rootJobTimestamp'] ) ) {
194 $opportunistic = !empty( $this->params['isOpportunistic'] );
195
196 $skewedTimestamp = $this->params['rootJobTimestamp'];
197 if ( $opportunistic ) {
198 // Neither clock skew nor DB snapshot/replica DB lag matter much for such
199 // updates; focus on reusing the (often recently updated) cache
200 } else {
201 // For transclusion updates, the template changes must be reflected
202 $skewedTimestamp = wfTimestamp( TS_MW,
203 wfTimestamp( TS_UNIX, $skewedTimestamp ) + self::CLOCK_FUDGE
204 );
205 }
206
207 if ( $page->getLinksTimestamp() > $skewedTimestamp ) {
208 // Something already updated the backlinks since this job was made
209 $stats->increment( 'refreshlinks.update_skipped' );
210 return true;
211 }
212
213 if ( $page->getTouched() >= $this->params['rootJobTimestamp'] || $opportunistic ) {
214 // Cache is suspected to be up-to-date. As long as the cache rev ID matches
215 // and it reflects the job's triggering change, then it is usable.
216 $parserOutput = $services->getParserCache()->getDirty( $page, $parserOptions );
217 if ( !$parserOutput
218 || $parserOutput->getCacheRevisionId() != $revision->getId()
219 || $parserOutput->getCacheTime() < $skewedTimestamp
220 ) {
221 $parserOutput = false; // too stale
222 }
223 }
224 }
225
226 // Fetch the current revision and parse it if necessary...
227 if ( $parserOutput ) {
228 $stats->increment( 'refreshlinks.parser_cached' );
229 } else {
230 $start = microtime( true );
231 // Revision ID must be passed to the parser output to get revision variables correct
232 $parserOutput = $content->getParserOutput(
233 $title, $revision->getId(), $parserOptions, false );
234 $elapsed = microtime( true ) - $start;
235 // If it took a long time to render, then save this back to the cache to avoid
236 // wasted CPU by other apaches or job runners. We don't want to always save to
237 // cache as this can cause high cache I/O and LRU churn when a template changes.
238 if ( $elapsed >= self::PARSE_THRESHOLD_SEC
239 && $page->shouldCheckParserCache( $parserOptions, $revision->getId() )
240 && $parserOutput->isCacheable()
241 ) {
242 $ctime = wfTimestamp( TS_MW, (int)$start ); // cache time
243 $services->getParserCache()->save(
244 $parserOutput, $page, $parserOptions, $ctime, $revision->getId()
245 );
246 }
247 $stats->increment( 'refreshlinks.parser_uncached' );
248 }
249
250 $options = [
251 'recursive' => !empty( $this->params['useRecursiveLinksUpdate'] ),
252 // Carry over cause so the update can do extra logging
253 'causeAction' => $this->params['causeAction'],
254 'causeAgent' => $this->params['causeAgent'],
255 'defer' => false,
256 'transactionTicket' => $ticket,
257 ];
258 if ( !empty( $this->params['triggeringUser'] ) ) {
259 $userInfo = $this->params['triggeringUser'];
260 if ( $userInfo['userId'] ) {
261 $options['triggeringUser'] = User::newFromId( $userInfo['userId'] );
262 } else {
263 // Anonymous, use the username
264 $options['triggeringUser'] = User::newFromName( $userInfo['userName'], false );
265 }
266 }
267 $page->doSecondaryDataUpdates( $options );
268
269 InfoAction::invalidateCache( $title );
270
271 // Commit any writes here in case this method is called in a loop.
272 // In that case, the scoped lock will fail to be acquired.
273 $lbFactory->commitAndWaitForReplication( __METHOD__, $ticket );
274
275 return true;
276 }
277
278 public function getDeduplicationInfo() {
279 $info = parent::getDeduplicationInfo();
280 unset( $info['causeAction'] );
281 unset( $info['causeAgent'] );
282 if ( is_array( $info['params'] ) ) {
283 // For per-pages jobs, the job title is that of the template that changed
284 // (or similar), so remove that since it ruins duplicate detection
285 if ( isset( $info['params']['pages'] ) ) {
286 unset( $info['namespace'] );
287 unset( $info['title'] );
288 }
289 }
290
291 return $info;
292 }
293
294 public function workItemCount() {
295 if ( !empty( $this->params['recursive'] ) ) {
296 return 0; // nothing actually refreshed
297 } elseif ( isset( $this->params['pages'] ) ) {
298 return count( $this->params['pages'] );
299 }
300
301 return 1; // one title
302 }
303 }