Move ChronologyProtector/TransactionProfiler to Rdbms namespace
[lhc/web/wiklou.git] / includes / MediaWiki.php
1 <?php
2 /**
3 * Helper class for the index.php entry point.
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 */
22
23 use MediaWiki\Logger\LoggerFactory;
24 use MediaWiki\MediaWikiServices;
25 use Wikimedia\Rdbms\ChronologyProtector;
26
27 /**
28 * The MediaWiki class is the helper class for the index.php entry point.
29 */
30 class MediaWiki {
31 /**
32 * @var IContextSource
33 */
34 private $context;
35
36 /**
37 * @var Config
38 */
39 private $config;
40
41 /**
42 * @var String Cache what action this request is
43 */
44 private $action;
45
46 /**
47 * @param IContextSource|null $context
48 */
49 public function __construct( IContextSource $context = null ) {
50 if ( !$context ) {
51 $context = RequestContext::getMain();
52 }
53
54 $this->context = $context;
55 $this->config = $context->getConfig();
56 }
57
58 /**
59 * Parse the request to get the Title object
60 *
61 * @throws MalformedTitleException If a title has been provided by the user, but is invalid.
62 * @return Title Title object to be $wgTitle
63 */
64 private function parseTitle() {
65 global $wgContLang;
66
67 $request = $this->context->getRequest();
68 $curid = $request->getInt( 'curid' );
69 $title = $request->getVal( 'title' );
70 $action = $request->getVal( 'action' );
71
72 if ( $request->getCheck( 'search' ) ) {
73 // Compatibility with old search URLs which didn't use Special:Search
74 // Just check for presence here, so blank requests still
75 // show the search page when using ugly URLs (bug 8054).
76 $ret = SpecialPage::getTitleFor( 'Search' );
77 } elseif ( $curid ) {
78 // URLs like this are generated by RC, because rc_title isn't always accurate
79 $ret = Title::newFromID( $curid );
80 } else {
81 $ret = Title::newFromURL( $title );
82 // Alias NS_MEDIA page URLs to NS_FILE...we only use NS_MEDIA
83 // in wikitext links to tell Parser to make a direct file link
84 if ( !is_null( $ret ) && $ret->getNamespace() == NS_MEDIA ) {
85 $ret = Title::makeTitle( NS_FILE, $ret->getDBkey() );
86 }
87 // Check variant links so that interwiki links don't have to worry
88 // about the possible different language variants
89 if ( count( $wgContLang->getVariants() ) > 1
90 && !is_null( $ret ) && $ret->getArticleID() == 0
91 ) {
92 $wgContLang->findVariantLink( $title, $ret );
93 }
94 }
95
96 // If title is not provided, always allow oldid and diff to set the title.
97 // If title is provided, allow oldid and diff to override the title, unless
98 // we are talking about a special page which might use these parameters for
99 // other purposes.
100 if ( $ret === null || !$ret->isSpecialPage() ) {
101 // We can have urls with just ?diff=,?oldid= or even just ?diff=
102 $oldid = $request->getInt( 'oldid' );
103 $oldid = $oldid ? $oldid : $request->getInt( 'diff' );
104 // Allow oldid to override a changed or missing title
105 if ( $oldid ) {
106 $rev = Revision::newFromId( $oldid );
107 $ret = $rev ? $rev->getTitle() : $ret;
108 }
109 }
110
111 // Use the main page as default title if nothing else has been provided
112 if ( $ret === null
113 && strval( $title ) === ''
114 && !$request->getCheck( 'curid' )
115 && $action !== 'delete'
116 ) {
117 $ret = Title::newMainPage();
118 }
119
120 if ( $ret === null || ( $ret->getDBkey() == '' && !$ret->isExternal() ) ) {
121 // If we get here, we definitely don't have a valid title; throw an exception.
122 // Try to get detailed invalid title exception first, fall back to MalformedTitleException.
123 Title::newFromTextThrow( $title );
124 throw new MalformedTitleException( 'badtitletext', $title );
125 }
126
127 return $ret;
128 }
129
130 /**
131 * Get the Title object that we'll be acting on, as specified in the WebRequest
132 * @return Title
133 */
134 public function getTitle() {
135 if ( !$this->context->hasTitle() ) {
136 try {
137 $this->context->setTitle( $this->parseTitle() );
138 } catch ( MalformedTitleException $ex ) {
139 $this->context->setTitle( SpecialPage::getTitleFor( 'Badtitle' ) );
140 }
141 }
142 return $this->context->getTitle();
143 }
144
145 /**
146 * Returns the name of the action that will be executed.
147 *
148 * @return string Action
149 */
150 public function getAction() {
151 if ( $this->action === null ) {
152 $this->action = Action::getActionName( $this->context );
153 }
154
155 return $this->action;
156 }
157
158 /**
159 * Performs the request.
160 * - bad titles
161 * - read restriction
162 * - local interwiki redirects
163 * - redirect loop
164 * - special pages
165 * - normal pages
166 *
167 * @throws MWException|PermissionsError|BadTitleError|HttpError
168 * @return void
169 */
170 private function performRequest() {
171 global $wgTitle;
172
173 $request = $this->context->getRequest();
174 $requestTitle = $title = $this->context->getTitle();
175 $output = $this->context->getOutput();
176 $user = $this->context->getUser();
177
178 if ( $request->getVal( 'printable' ) === 'yes' ) {
179 $output->setPrintable();
180 }
181
182 $unused = null; // To pass it by reference
183 Hooks::run( 'BeforeInitialize', [ &$title, &$unused, &$output, &$user, $request, $this ] );
184
185 // Invalid titles. Bug 21776: The interwikis must redirect even if the page name is empty.
186 if ( is_null( $title ) || ( $title->getDBkey() == '' && !$title->isExternal() )
187 || $title->isSpecial( 'Badtitle' )
188 ) {
189 $this->context->setTitle( SpecialPage::getTitleFor( 'Badtitle' ) );
190 try {
191 $this->parseTitle();
192 } catch ( MalformedTitleException $ex ) {
193 throw new BadTitleError( $ex );
194 }
195 throw new BadTitleError();
196 }
197
198 // Check user's permissions to read this page.
199 // We have to check here to catch special pages etc.
200 // We will check again in Article::view().
201 $permErrors = $title->isSpecial( 'RunJobs' )
202 ? [] // relies on HMAC key signature alone
203 : $title->getUserPermissionsErrors( 'read', $user );
204 if ( count( $permErrors ) ) {
205 // Bug 32276: allowing the skin to generate output with $wgTitle or
206 // $this->context->title set to the input title would allow anonymous users to
207 // determine whether a page exists, potentially leaking private data. In fact, the
208 // curid and oldid request parameters would allow page titles to be enumerated even
209 // when they are not guessable. So we reset the title to Special:Badtitle before the
210 // permissions error is displayed.
211
212 // The skin mostly uses $this->context->getTitle() these days, but some extensions
213 // still use $wgTitle.
214 $badTitle = SpecialPage::getTitleFor( 'Badtitle' );
215 $this->context->setTitle( $badTitle );
216 $wgTitle = $badTitle;
217
218 throw new PermissionsError( 'read', $permErrors );
219 }
220
221 // Interwiki redirects
222 if ( $title->isExternal() ) {
223 $rdfrom = $request->getVal( 'rdfrom' );
224 if ( $rdfrom ) {
225 $url = $title->getFullURL( [ 'rdfrom' => $rdfrom ] );
226 } else {
227 $query = $request->getValues();
228 unset( $query['title'] );
229 $url = $title->getFullURL( $query );
230 }
231 // Check for a redirect loop
232 if ( !preg_match( '/^' . preg_quote( $this->config->get( 'Server' ), '/' ) . '/', $url )
233 && $title->isLocal()
234 ) {
235 // 301 so google et al report the target as the actual url.
236 $output->redirect( $url, 301 );
237 } else {
238 $this->context->setTitle( SpecialPage::getTitleFor( 'Badtitle' ) );
239 try {
240 $this->parseTitle();
241 } catch ( MalformedTitleException $ex ) {
242 throw new BadTitleError( $ex );
243 }
244 throw new BadTitleError();
245 }
246 // Handle any other redirects.
247 // Redirect loops, titleless URL, $wgUsePathInfo URLs, and URLs with a variant
248 } elseif ( !$this->tryNormaliseRedirect( $title ) ) {
249 // Prevent information leak via Special:MyPage et al (T109724)
250 if ( $title->isSpecialPage() ) {
251 $specialPage = SpecialPageFactory::getPage( $title->getDBkey() );
252 if ( $specialPage instanceof RedirectSpecialPage ) {
253 $specialPage->setContext( $this->context );
254 if ( $this->config->get( 'HideIdentifiableRedirects' )
255 && $specialPage->personallyIdentifiableTarget()
256 ) {
257 list( , $subpage ) = SpecialPageFactory::resolveAlias( $title->getDBkey() );
258 $target = $specialPage->getRedirect( $subpage );
259 // target can also be true. We let that case fall through to normal processing.
260 if ( $target instanceof Title ) {
261 $query = $specialPage->getRedirectQuery() ?: [];
262 $request = new DerivativeRequest( $this->context->getRequest(), $query );
263 $request->setRequestURL( $this->context->getRequest()->getRequestURL() );
264 $this->context->setRequest( $request );
265 // Do not varnish cache these. May vary even for anons
266 $this->context->getOutput()->lowerCdnMaxage( 0 );
267 $this->context->setTitle( $target );
268 $wgTitle = $target;
269 // Reset action type cache. (Special pages have only view)
270 $this->action = null;
271 $title = $target;
272 $output->addJsConfigVars( [
273 'wgInternalRedirectTargetUrl' => $target->getFullURL( $query ),
274 ] );
275 $output->addModules( 'mediawiki.action.view.redirect' );
276 }
277 }
278 }
279 }
280
281 // Special pages ($title may have changed since if statement above)
282 if ( NS_SPECIAL == $title->getNamespace() ) {
283 // Actions that need to be made when we have a special pages
284 SpecialPageFactory::executePath( $title, $this->context );
285 } else {
286 // ...otherwise treat it as an article view. The article
287 // may still be a wikipage redirect to another article or URL.
288 $article = $this->initializeArticle();
289 if ( is_object( $article ) ) {
290 $this->performAction( $article, $requestTitle );
291 } elseif ( is_string( $article ) ) {
292 $output->redirect( $article );
293 } else {
294 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle()"
295 . " returned neither an object nor a URL" );
296 }
297 }
298 }
299 }
300
301 /**
302 * Handle redirects for uncanonical title requests.
303 *
304 * Handles:
305 * - Redirect loops.
306 * - No title in URL.
307 * - $wgUsePathInfo URLs.
308 * - URLs with a variant.
309 * - Other non-standard URLs (as long as they have no extra query parameters).
310 *
311 * Behaviour:
312 * - Normalise title values:
313 * /wiki/Foo%20Bar -> /wiki/Foo_Bar
314 * - Normalise empty title:
315 * /wiki/ -> /wiki/Main
316 * /w/index.php?title= -> /wiki/Main
317 * - Don't redirect anything with query parameters other than 'title' or 'action=view'.
318 *
319 * @param Title $title
320 * @return bool True if a redirect was set.
321 * @throws HttpError
322 */
323 private function tryNormaliseRedirect( Title $title ) {
324 $request = $this->context->getRequest();
325 $output = $this->context->getOutput();
326
327 if ( $request->getVal( 'action', 'view' ) != 'view'
328 || $request->wasPosted()
329 || ( $request->getVal( 'title' ) !== null
330 && $title->getPrefixedDBkey() == $request->getVal( 'title' ) )
331 || count( $request->getValueNames( [ 'action', 'title' ] ) )
332 || !Hooks::run( 'TestCanonicalRedirect', [ $request, $title, $output ] )
333 ) {
334 return false;
335 }
336
337 if ( $title->isSpecialPage() ) {
338 list( $name, $subpage ) = SpecialPageFactory::resolveAlias( $title->getDBkey() );
339 if ( $name ) {
340 $title = SpecialPage::getTitleFor( $name, $subpage );
341 }
342 }
343 // Redirect to canonical url, make it a 301 to allow caching
344 $targetUrl = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
345 if ( $targetUrl == $request->getFullRequestURL() ) {
346 $message = "Redirect loop detected!\n\n" .
347 "This means the wiki got confused about what page was " .
348 "requested; this sometimes happens when moving a wiki " .
349 "to a new server or changing the server configuration.\n\n";
350
351 if ( $this->config->get( 'UsePathInfo' ) ) {
352 $message .= "The wiki is trying to interpret the page " .
353 "title from the URL path portion (PATH_INFO), which " .
354 "sometimes fails depending on the web server. Try " .
355 "setting \"\$wgUsePathInfo = false;\" in your " .
356 "LocalSettings.php, or check that \$wgArticlePath " .
357 "is correct.";
358 } else {
359 $message .= "Your web server was detected as possibly not " .
360 "supporting URL path components (PATH_INFO) correctly; " .
361 "check your LocalSettings.php for a customized " .
362 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
363 "to true.";
364 }
365 throw new HttpError( 500, $message );
366 }
367 $output->setSquidMaxage( 1200 );
368 $output->redirect( $targetUrl, '301' );
369 return true;
370 }
371
372 /**
373 * Initialize the main Article object for "standard" actions (view, etc)
374 * Create an Article object for the page, following redirects if needed.
375 *
376 * @return Article|string An Article, or a string to redirect to another URL
377 */
378 private function initializeArticle() {
379 $title = $this->context->getTitle();
380 if ( $this->context->canUseWikiPage() ) {
381 // Try to use request context wiki page, as there
382 // is already data from db saved in per process
383 // cache there from this->getAction() call.
384 $page = $this->context->getWikiPage();
385 } else {
386 // This case should not happen, but just in case.
387 // @TODO: remove this or use an exception
388 $page = WikiPage::factory( $title );
389 $this->context->setWikiPage( $page );
390 wfWarn( "RequestContext::canUseWikiPage() returned false" );
391 }
392
393 // Make GUI wrapper for the WikiPage
394 $article = Article::newFromWikiPage( $page, $this->context );
395
396 // Skip some unnecessary code if the content model doesn't support redirects
397 if ( !ContentHandler::getForTitle( $title )->supportsRedirects() ) {
398 return $article;
399 }
400
401 $request = $this->context->getRequest();
402
403 // Namespace might change when using redirects
404 // Check for redirects ...
405 $action = $request->getVal( 'action', 'view' );
406 $file = ( $page instanceof WikiFilePage ) ? $page->getFile() : null;
407 if ( ( $action == 'view' || $action == 'render' ) // ... for actions that show content
408 && !$request->getVal( 'oldid' ) // ... and are not old revisions
409 && !$request->getVal( 'diff' ) // ... and not when showing diff
410 && $request->getVal( 'redirect' ) != 'no' // ... unless explicitly told not to
411 // ... and the article is not a non-redirect image page with associated file
412 && !( is_object( $file ) && $file->exists() && !$file->getRedirected() )
413 ) {
414 // Give extensions a change to ignore/handle redirects as needed
415 $ignoreRedirect = $target = false;
416
417 Hooks::run( 'InitializeArticleMaybeRedirect',
418 [ &$title, &$request, &$ignoreRedirect, &$target, &$article ] );
419 $page = $article->getPage(); // reflect any hook changes
420
421 // Follow redirects only for... redirects.
422 // If $target is set, then a hook wanted to redirect.
423 if ( !$ignoreRedirect && ( $target || $page->isRedirect() ) ) {
424 // Is the target already set by an extension?
425 $target = $target ? $target : $page->followRedirect();
426 if ( is_string( $target ) ) {
427 if ( !$this->config->get( 'DisableHardRedirects' ) ) {
428 // we'll need to redirect
429 return $target;
430 }
431 }
432 if ( is_object( $target ) ) {
433 // Rewrite environment to redirected article
434 $rpage = WikiPage::factory( $target );
435 $rpage->loadPageData();
436 if ( $rpage->exists() || ( is_object( $file ) && !$file->isLocal() ) ) {
437 $rarticle = Article::newFromWikiPage( $rpage, $this->context );
438 $rarticle->setRedirectedFrom( $title );
439
440 $article = $rarticle;
441 $this->context->setTitle( $target );
442 $this->context->setWikiPage( $article->getPage() );
443 }
444 }
445 } else {
446 // Article may have been changed by hook
447 $this->context->setTitle( $article->getTitle() );
448 $this->context->setWikiPage( $article->getPage() );
449 }
450 }
451
452 return $article;
453 }
454
455 /**
456 * Perform one of the "standard" actions
457 *
458 * @param Page $page
459 * @param Title $requestTitle The original title, before any redirects were applied
460 */
461 private function performAction( Page $page, Title $requestTitle ) {
462 $request = $this->context->getRequest();
463 $output = $this->context->getOutput();
464 $title = $this->context->getTitle();
465 $user = $this->context->getUser();
466
467 if ( !Hooks::run( 'MediaWikiPerformAction',
468 [ $output, $page, $title, $user, $request, $this ] )
469 ) {
470 return;
471 }
472
473 $act = $this->getAction();
474 $action = Action::factory( $act, $page, $this->context );
475
476 if ( $action instanceof Action ) {
477 // Narrow DB query expectations for this HTTP request
478 $trxLimits = $this->config->get( 'TrxProfilerLimits' );
479 $trxProfiler = Profiler::instance()->getTransactionProfiler();
480 if ( $request->wasPosted() && !$action->doesWrites() ) {
481 $trxProfiler->setExpectations( $trxLimits['POST-nonwrite'], __METHOD__ );
482 $request->markAsSafeRequest();
483 }
484
485 # Let CDN cache things if we can purge them.
486 if ( $this->config->get( 'UseSquid' ) &&
487 in_array(
488 // Use PROTO_INTERNAL because that's what getCdnUrls() uses
489 wfExpandUrl( $request->getRequestURL(), PROTO_INTERNAL ),
490 $requestTitle->getCdnUrls()
491 )
492 ) {
493 $output->setCdnMaxage( $this->config->get( 'SquidMaxage' ) );
494 }
495
496 $action->show();
497 return;
498 }
499 // NOTE: deprecated hook. Add to $wgActions instead
500 if ( Hooks::run(
501 'UnknownAction',
502 [
503 $request->getVal( 'action', 'view' ),
504 $page
505 ],
506 '1.19'
507 ) ) {
508 $output->setStatusCode( 404 );
509 $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
510 }
511 }
512
513 /**
514 * Run the current MediaWiki instance; index.php just calls this
515 */
516 public function run() {
517 try {
518 $this->setDBProfilingAgent();
519 try {
520 $this->main();
521 } catch ( ErrorPageError $e ) {
522 // Bug 62091: while exceptions are convenient to bubble up GUI errors,
523 // they are not internal application faults. As with normal requests, this
524 // should commit, print the output, do deferred updates, jobs, and profiling.
525 $this->doPreOutputCommit();
526 $e->report(); // display the GUI error
527 }
528 } catch ( Exception $e ) {
529 $context = $this->context;
530 $action = $context->getRequest()->getVal( 'action', 'view' );
531 if (
532 $e instanceof DBConnectionError &&
533 $context->hasTitle() &&
534 $context->getTitle()->canExist() &&
535 in_array( $action, [ 'view', 'history' ], true ) &&
536 HTMLFileCache::useFileCache( $this->context, HTMLFileCache::MODE_OUTAGE )
537 ) {
538 // Try to use any (even stale) file during outages...
539 $cache = new HTMLFileCache( $context->getTitle(), 'view' );
540 if ( $cache->isCached() ) {
541 $cache->loadFromFileCache( $context, HTMLFileCache::MODE_OUTAGE );
542 print MWExceptionRenderer::getHTML( $e );
543 exit;
544 }
545
546 }
547
548 MWExceptionHandler::handleException( $e );
549 }
550
551 $this->doPostOutputShutdown( 'normal' );
552 }
553
554 private function setDBProfilingAgent() {
555 $services = MediaWikiServices::getInstance();
556 // Add a comment for easy SHOW PROCESSLIST interpretation
557 $name = $this->context->getUser()->getName();
558 $services->getDBLoadBalancerFactory()->setAgentName(
559 mb_strlen( $name ) > 15 ? mb_substr( $name, 0, 15 ) . '...' : $name
560 );
561 }
562
563 /**
564 * @see MediaWiki::preOutputCommit()
565 * @param callable $postCommitWork [default: null]
566 * @since 1.26
567 */
568 public function doPreOutputCommit( callable $postCommitWork = null ) {
569 self::preOutputCommit( $this->context, $postCommitWork );
570 }
571
572 /**
573 * This function commits all DB changes as needed before
574 * the user can receive a response (in case commit fails)
575 *
576 * @param IContextSource $context
577 * @param callable $postCommitWork [default: null]
578 * @since 1.27
579 */
580 public static function preOutputCommit(
581 IContextSource $context, callable $postCommitWork = null
582 ) {
583 // Either all DBs should commit or none
584 ignore_user_abort( true );
585
586 $config = $context->getConfig();
587 $request = $context->getRequest();
588 $output = $context->getOutput();
589 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
590
591 // Commit all changes
592 $lbFactory->commitMasterChanges(
593 __METHOD__,
594 // Abort if any transaction was too big
595 [ 'maxWriteDuration' => $config->get( 'MaxUserDBWriteDuration' ) ]
596 );
597 wfDebug( __METHOD__ . ': primary transaction round committed' );
598
599 // Run updates that need to block the user or affect output (this is the last chance)
600 DeferredUpdates::doUpdates( 'enqueue', DeferredUpdates::PRESEND );
601 wfDebug( __METHOD__ . ': pre-send deferred updates completed' );
602
603 // Decide when clients block on ChronologyProtector DB position writes
604 $urlDomainDistance = (
605 $request->wasPosted() &&
606 $output->getRedirect() &&
607 $lbFactory->hasOrMadeRecentMasterChanges( INF )
608 ) ? self::getUrlDomainDistance( $output->getRedirect(), $context ) : false;
609
610 if ( $urlDomainDistance === 'local' || $urlDomainDistance === 'remote' ) {
611 // OutputPage::output() will be fast; $postCommitWork will not be useful for
612 // masking the latency of syncing DB positions accross all datacenters synchronously.
613 // Instead, make use of the RTT time of the client follow redirects.
614 $flags = $lbFactory::SHUTDOWN_CHRONPROT_ASYNC;
615 $cpPosTime = microtime( true );
616 // Client's next request should see 1+ positions with this DBMasterPos::asOf() time
617 if ( $urlDomainDistance === 'local' ) {
618 // Client will stay on this domain, so set an unobtrusive cookie
619 $expires = time() + ChronologyProtector::POSITION_TTL;
620 $options = [ 'prefix' => '' ];
621 $request->response()->setCookie( 'cpPosTime', $cpPosTime, $expires, $options );
622 } else {
623 // Cookies may not work across wiki domains, so use a URL parameter
624 $safeUrl = $lbFactory->appendPreShutdownTimeAsQuery(
625 $output->getRedirect(),
626 $cpPosTime
627 );
628 $output->redirect( $safeUrl );
629 }
630 } else {
631 // OutputPage::output() is fairly slow; run it in $postCommitWork to mask
632 // the latency of syncing DB positions accross all datacenters synchronously
633 $flags = $lbFactory::SHUTDOWN_CHRONPROT_SYNC;
634 if ( $lbFactory->hasOrMadeRecentMasterChanges( INF ) ) {
635 $cpPosTime = microtime( true );
636 // Set a cookie in case the DB position store cannot sync accross datacenters.
637 // This will at least cover the common case of the user staying on the domain.
638 $expires = time() + ChronologyProtector::POSITION_TTL;
639 $options = [ 'prefix' => '' ];
640 $request->response()->setCookie( 'cpPosTime', $cpPosTime, $expires, $options );
641 }
642 }
643 // Record ChronologyProtector positions for DBs affected in this request at this point
644 $lbFactory->shutdown( $flags, $postCommitWork );
645 wfDebug( __METHOD__ . ': LBFactory shutdown completed' );
646
647 // Set a cookie to tell all CDN edge nodes to "stick" the user to the DC that handles this
648 // POST request (e.g. the "master" data center). Also have the user briefly bypass CDN so
649 // ChronologyProtector works for cacheable URLs.
650 if ( $request->wasPosted() && $lbFactory->hasOrMadeRecentMasterChanges() ) {
651 $expires = time() + $config->get( 'DataCenterUpdateStickTTL' );
652 $options = [ 'prefix' => '' ];
653 $request->response()->setCookie( 'UseDC', 'master', $expires, $options );
654 $request->response()->setCookie( 'UseCDNCache', 'false', $expires, $options );
655 }
656
657 // Avoid letting a few seconds of replica DB lag cause a month of stale data. This logic is
658 // also intimately related to the value of $wgCdnReboundPurgeDelay.
659 if ( $lbFactory->laggedReplicaUsed() ) {
660 $maxAge = $config->get( 'CdnMaxageLagged' );
661 $output->lowerCdnMaxage( $maxAge );
662 $request->response()->header( "X-Database-Lagged: true" );
663 wfDebugLog( 'replication', "Lagged DB used; CDN cache TTL limited to $maxAge seconds" );
664 }
665
666 // Avoid long-term cache pollution due to message cache rebuild timeouts (T133069)
667 if ( MessageCache::singleton()->isDisabled() ) {
668 $maxAge = $config->get( 'CdnMaxageSubstitute' );
669 $output->lowerCdnMaxage( $maxAge );
670 $request->response()->header( "X-Response-Substitute: true" );
671 }
672 }
673
674 /**
675 * @param string $url
676 * @param IContextSource $context
677 * @return string Either "local", "remote" if in the farm, "external" otherwise
678 */
679 private static function getUrlDomainDistance( $url, IContextSource $context ) {
680 static $relevantKeys = [ 'host' => true, 'port' => true ];
681
682 $infoCandidate = wfParseUrl( $url );
683 if ( $infoCandidate === false ) {
684 return 'external';
685 }
686
687 $infoCandidate = array_intersect_key( $infoCandidate, $relevantKeys );
688 $clusterHosts = array_merge(
689 // Local wiki host (the most common case)
690 [ $context->getConfig()->get( 'CanonicalServer' ) ],
691 // Any local/remote wiki virtual hosts for this wiki farm
692 $context->getConfig()->get( 'LocalVirtualHosts' )
693 );
694
695 foreach ( $clusterHosts as $i => $clusterHost ) {
696 $parseUrl = wfParseUrl( $clusterHost );
697 if ( !$parseUrl ) {
698 continue;
699 }
700 $infoHost = array_intersect_key( $parseUrl, $relevantKeys );
701 if ( $infoCandidate === $infoHost ) {
702 return ( $i === 0 ) ? 'local' : 'remote';
703 }
704 }
705
706 return 'external';
707 }
708
709 /**
710 * This function does work that can be done *after* the
711 * user gets the HTTP response so they don't block on it
712 *
713 * This manages deferred updates, job insertion,
714 * final commit, and the logging of profiling data
715 *
716 * @param string $mode Use 'fast' to always skip job running
717 * @since 1.26
718 */
719 public function doPostOutputShutdown( $mode = 'normal' ) {
720 $timing = $this->context->getTiming();
721 $timing->mark( 'requestShutdown' );
722
723 // Show visible profiling data if enabled (which cannot be post-send)
724 Profiler::instance()->logDataPageOutputOnly();
725
726 $callback = function () use ( $mode ) {
727 try {
728 $this->restInPeace( $mode );
729 } catch ( Exception $e ) {
730 MWExceptionHandler::handleException( $e );
731 }
732 };
733
734 // Defer everything else...
735 if ( function_exists( 'register_postsend_function' ) ) {
736 // https://github.com/facebook/hhvm/issues/1230
737 register_postsend_function( $callback );
738 } else {
739 if ( function_exists( 'fastcgi_finish_request' ) ) {
740 fastcgi_finish_request();
741 } else {
742 // Either all DB and deferred updates should happen or none.
743 // The latter should not be cancelled due to client disconnect.
744 ignore_user_abort( true );
745 }
746
747 $callback();
748 }
749 }
750
751 private function main() {
752 global $wgTitle;
753
754 $output = $this->context->getOutput();
755 $request = $this->context->getRequest();
756
757 // Send Ajax requests to the Ajax dispatcher.
758 if ( $this->config->get( 'UseAjax' ) && $request->getVal( 'action' ) === 'ajax' ) {
759 // Set a dummy title, because $wgTitle == null might break things
760 $title = Title::makeTitle( NS_SPECIAL, 'Badtitle/performing an AJAX call in '
761 . __METHOD__
762 );
763 $this->context->setTitle( $title );
764 $wgTitle = $title;
765
766 $dispatcher = new AjaxDispatcher( $this->config );
767 $dispatcher->performAction( $this->context->getUser() );
768
769 return;
770 }
771
772 // Get title from request parameters,
773 // is set on the fly by parseTitle the first time.
774 $title = $this->getTitle();
775 $action = $this->getAction();
776 $wgTitle = $title;
777
778 // Set DB query expectations for this HTTP request
779 $trxLimits = $this->config->get( 'TrxProfilerLimits' );
780 $trxProfiler = Profiler::instance()->getTransactionProfiler();
781 $trxProfiler->setLogger( LoggerFactory::getInstance( 'DBPerformance' ) );
782 if ( $request->hasSafeMethod() ) {
783 $trxProfiler->setExpectations( $trxLimits['GET'], __METHOD__ );
784 } else {
785 $trxProfiler->setExpectations( $trxLimits['POST'], __METHOD__ );
786 }
787
788 // If the user has forceHTTPS set to true, or if the user
789 // is in a group requiring HTTPS, or if they have the HTTPS
790 // preference set, redirect them to HTTPS.
791 // Note: Do this after $wgTitle is setup, otherwise the hooks run from
792 // isLoggedIn() will do all sorts of weird stuff.
793 if (
794 $request->getProtocol() == 'http' &&
795 // switch to HTTPS only when supported by the server
796 preg_match( '#^https://#', wfExpandUrl( $request->getRequestURL(), PROTO_HTTPS ) ) &&
797 (
798 $request->getSession()->shouldForceHTTPS() ||
799 // Check the cookie manually, for paranoia
800 $request->getCookie( 'forceHTTPS', '' ) ||
801 // check for prefixed version that was used for a time in older MW versions
802 $request->getCookie( 'forceHTTPS' ) ||
803 // Avoid checking the user and groups unless it's enabled.
804 (
805 $this->context->getUser()->isLoggedIn()
806 && $this->context->getUser()->requiresHTTPS()
807 )
808 )
809 ) {
810 $oldUrl = $request->getFullRequestURL();
811 $redirUrl = preg_replace( '#^http://#', 'https://', $oldUrl );
812
813 // ATTENTION: This hook is likely to be removed soon due to overall design of the system.
814 if ( Hooks::run( 'BeforeHttpsRedirect', [ $this->context, &$redirUrl ] ) ) {
815
816 if ( $request->wasPosted() ) {
817 // This is weird and we'd hope it almost never happens. This
818 // means that a POST came in via HTTP and policy requires us
819 // redirecting to HTTPS. It's likely such a request is going
820 // to fail due to post data being lost, but let's try anyway
821 // and just log the instance.
822
823 // @todo FIXME: See if we could issue a 307 or 308 here, need
824 // to see how clients (automated & browser) behave when we do
825 wfDebugLog( 'RedirectedPosts', "Redirected from HTTP to HTTPS: $oldUrl" );
826 }
827 // Setup dummy Title, otherwise OutputPage::redirect will fail
828 $title = Title::newFromText( 'REDIR', NS_MAIN );
829 $this->context->setTitle( $title );
830 // Since we only do this redir to change proto, always send a vary header
831 $output->addVaryHeader( 'X-Forwarded-Proto' );
832 $output->redirect( $redirUrl );
833 $output->output();
834
835 return;
836 }
837 }
838
839 if ( $title->canExist() && HTMLFileCache::useFileCache( $this->context ) ) {
840 // Try low-level file cache hit
841 $cache = new HTMLFileCache( $title, $action );
842 if ( $cache->isCacheGood( /* Assume up to date */ ) ) {
843 // Check incoming headers to see if client has this cached
844 $timestamp = $cache->cacheTimestamp();
845 if ( !$output->checkLastModified( $timestamp ) ) {
846 $cache->loadFromFileCache( $this->context );
847 }
848 // Do any stats increment/watchlist stuff, assuming user is viewing the
849 // latest revision (which should always be the case for file cache)
850 $this->context->getWikiPage()->doViewUpdates( $this->context->getUser() );
851 // Tell OutputPage that output is taken care of
852 $output->disable();
853
854 return;
855 }
856 }
857
858 // Actually do the work of the request and build up any output
859 $this->performRequest();
860
861 // GUI-ify and stash the page output in MediaWiki::doPreOutputCommit() while
862 // ChronologyProtector synchronizes DB positions or slaves accross all datacenters.
863 $buffer = null;
864 $outputWork = function () use ( $output, &$buffer ) {
865 if ( $buffer === null ) {
866 $buffer = $output->output( true );
867 }
868
869 return $buffer;
870 };
871
872 // Now commit any transactions, so that unreported errors after
873 // output() don't roll back the whole DB transaction and so that
874 // we avoid having both success and error text in the response
875 $this->doPreOutputCommit( $outputWork );
876
877 // Now send the actual output
878 print $outputWork();
879 }
880
881 /**
882 * Ends this task peacefully
883 * @param string $mode Use 'fast' to always skip job running
884 */
885 public function restInPeace( $mode = 'fast' ) {
886 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
887 // Assure deferred updates are not in the main transaction
888 $lbFactory->commitMasterChanges( __METHOD__ );
889
890 // Loosen DB query expectations since the HTTP client is unblocked
891 $trxProfiler = Profiler::instance()->getTransactionProfiler();
892 $trxProfiler->resetExpectations();
893 $trxProfiler->setExpectations(
894 $this->config->get( 'TrxProfilerLimits' )['PostSend'],
895 __METHOD__
896 );
897
898 // Do any deferred jobs
899 DeferredUpdates::doUpdates( 'enqueue' );
900 DeferredUpdates::setImmediateMode( true );
901
902 // Make sure any lazy jobs are pushed
903 JobQueueGroup::pushLazyJobs();
904
905 // Now that everything specific to this request is done,
906 // try to occasionally run jobs (if enabled) from the queues
907 if ( $mode === 'normal' ) {
908 $this->triggerJobs();
909 }
910
911 // Log profiling data, e.g. in the database or UDP
912 wfLogProfilingData();
913
914 // Commit and close up!
915 $lbFactory->commitMasterChanges( __METHOD__ );
916 $lbFactory->shutdown( LBFactory::SHUTDOWN_NO_CHRONPROT );
917
918 wfDebug( "Request ended normally\n" );
919 }
920
921 /**
922 * Potentially open a socket and sent an HTTP request back to the server
923 * to run a specified number of jobs. This registers a callback to cleanup
924 * the socket once it's done.
925 */
926 public function triggerJobs() {
927 $jobRunRate = $this->config->get( 'JobRunRate' );
928 if ( $this->getTitle()->isSpecial( 'RunJobs' ) ) {
929 return; // recursion guard
930 } elseif ( $jobRunRate <= 0 || wfReadOnly() ) {
931 return;
932 }
933
934 if ( $jobRunRate < 1 ) {
935 $max = mt_getrandmax();
936 if ( mt_rand( 0, $max ) > $max * $jobRunRate ) {
937 return; // the higher the job run rate, the less likely we return here
938 }
939 $n = 1;
940 } else {
941 $n = intval( $jobRunRate );
942 }
943
944 $runJobsLogger = LoggerFactory::getInstance( 'runJobs' );
945
946 // Fall back to running the job(s) while the user waits if needed
947 if ( !$this->config->get( 'RunJobsAsync' ) ) {
948 $runner = new JobRunner( $runJobsLogger );
949 $runner->run( [ 'maxJobs' => $n ] );
950 return;
951 }
952
953 // Do not send request if there are probably no jobs
954 try {
955 $group = JobQueueGroup::singleton();
956 if ( !$group->queuesHaveJobs( JobQueueGroup::TYPE_DEFAULT ) ) {
957 return;
958 }
959 } catch ( JobQueueError $e ) {
960 MWExceptionHandler::logException( $e );
961 return; // do not make the site unavailable
962 }
963
964 $query = [ 'title' => 'Special:RunJobs',
965 'tasks' => 'jobs', 'maxjobs' => $n, 'sigexpiry' => time() + 5 ];
966 $query['signature'] = SpecialRunJobs::getQuerySignature(
967 $query, $this->config->get( 'SecretKey' ) );
968
969 $errno = $errstr = null;
970 $info = wfParseUrl( $this->config->get( 'CanonicalServer' ) );
971 $host = $info ? $info['host'] : null;
972 $port = 80;
973 if ( isset( $info['scheme'] ) && $info['scheme'] == 'https' ) {
974 $host = "tls://" . $host;
975 $port = 443;
976 }
977 if ( isset( $info['port'] ) ) {
978 $port = $info['port'];
979 }
980
981 MediaWiki\suppressWarnings();
982 $sock = $host ? fsockopen(
983 $host,
984 $port,
985 $errno,
986 $errstr,
987 // If it takes more than 100ms to connect to ourselves there is a problem...
988 0.100
989 ) : false;
990 MediaWiki\restoreWarnings();
991
992 $invokedWithSuccess = true;
993 if ( $sock ) {
994 $special = SpecialPageFactory::getPage( 'RunJobs' );
995 $url = $special->getPageTitle()->getCanonicalURL( $query );
996 $req = (
997 "POST $url HTTP/1.1\r\n" .
998 "Host: {$info['host']}\r\n" .
999 "Connection: Close\r\n" .
1000 "Content-Length: 0\r\n\r\n"
1001 );
1002
1003 $runJobsLogger->info( "Running $n job(s) via '$url'" );
1004 // Send a cron API request to be performed in the background.
1005 // Give up if this takes too long to send (which should be rare).
1006 stream_set_timeout( $sock, 2 );
1007 $bytes = fwrite( $sock, $req );
1008 if ( $bytes !== strlen( $req ) ) {
1009 $invokedWithSuccess = false;
1010 $runJobsLogger->error( "Failed to start cron API (socket write error)" );
1011 } else {
1012 // Do not wait for the response (the script should handle client aborts).
1013 // Make sure that we don't close before that script reaches ignore_user_abort().
1014 $start = microtime( true );
1015 $status = fgets( $sock );
1016 $sec = microtime( true ) - $start;
1017 if ( !preg_match( '#^HTTP/\d\.\d 202 #', $status ) ) {
1018 $invokedWithSuccess = false;
1019 $runJobsLogger->error( "Failed to start cron API: received '$status' ($sec)" );
1020 }
1021 }
1022 fclose( $sock );
1023 } else {
1024 $invokedWithSuccess = false;
1025 $runJobsLogger->error( "Failed to start cron API (socket error $errno): $errstr" );
1026 }
1027
1028 // Fall back to running the job(s) while the user waits if needed
1029 if ( !$invokedWithSuccess ) {
1030 $runJobsLogger->warning( "Jobs switched to blocking; Special:RunJobs disabled" );
1031
1032 $runner = new JobRunner( $runJobsLogger );
1033 $runner->run( [ 'maxJobs' => $n ] );
1034 }
1035 }
1036 }