CentralIdLookup: Add @since to factoryNonLocal()
[mediawiki.git] / includes / MediaWiki.php
blob732b235e0ff404670a6718efc4f6f36c9b79f831
1 <?php
2 /**
3 * Helper class for the index.php entry point.
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
23 use Liuggio\StatsdClient\Sender\SocketSender;
24 use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
25 use MediaWiki\Logger\LoggerFactory;
26 use MediaWiki\MediaWikiServices;
27 use Psr\Log\LoggerInterface;
28 use Wikimedia\AtEase;
29 use Wikimedia\Rdbms\ChronologyProtector;
30 use Wikimedia\Rdbms\DBConnectionError;
31 use Wikimedia\Rdbms\ILBFactory;
33 /**
34 * The MediaWiki class is the helper class for the index.php entry point.
36 class MediaWiki {
37 use ProtectedHookAccessorTrait;
39 /** @var IContextSource */
40 private $context;
41 /** @var Config */
42 private $config;
44 /** @var string Cache what action this request is */
45 private $action;
46 /** @var int Class DEFER_* constant; how non-blocking post-response tasks should run */
47 private $postSendStrategy;
49 /** @var int Use fastcgi_finish_request() */
50 private const DEFER_FASTCGI_FINISH_REQUEST = 1;
51 /** @var int Use ob_end_flush() after explicitly setting the Content-Length */
52 private const DEFER_SET_LENGTH_AND_FLUSH = 2;
54 /**
55 * @param IContextSource|null $context
57 public function __construct( IContextSource $context = null ) {
58 $this->context = $context ?: RequestContext::getMain();
59 $this->config = $this->context->getConfig();
60 if ( function_exists( 'fastcgi_finish_request' ) ) {
61 $this->postSendStrategy = self::DEFER_FASTCGI_FINISH_REQUEST;
62 } else {
63 $this->postSendStrategy = self::DEFER_SET_LENGTH_AND_FLUSH;
67 /**
68 * Parse the request to get the Title object
70 * @throws MalformedTitleException If a title has been provided by the user, but is invalid.
71 * @return Title Title object to be $wgTitle
73 private function parseTitle() {
74 $request = $this->context->getRequest();
75 $curid = $request->getInt( 'curid' );
76 $title = $request->getVal( 'title' );
77 $action = $request->getVal( 'action' );
79 if ( $request->getCheck( 'search' ) ) {
80 // Compatibility with old search URLs which didn't use Special:Search
81 // Just check for presence here, so blank requests still
82 // show the search page when using ugly URLs (T10054).
83 $ret = SpecialPage::getTitleFor( 'Search' );
84 } elseif ( $curid ) {
85 // URLs like this are generated by RC, because rc_title isn't always accurate
86 $ret = Title::newFromID( $curid );
87 } else {
88 $ret = Title::newFromURL( $title );
89 // Alias NS_MEDIA page URLs to NS_FILE...we only use NS_MEDIA
90 // in wikitext links to tell Parser to make a direct file link
91 if ( $ret !== null && $ret->getNamespace() === NS_MEDIA ) {
92 $ret = Title::makeTitle( NS_FILE, $ret->getDBkey() );
94 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
95 // Check variant links so that interwiki links don't have to worry
96 // about the possible different language variants
97 if (
98 $contLang->hasVariants() && $ret !== null && $ret->getArticleID() == 0
99 ) {
100 $contLang->findVariantLink( $title, $ret );
104 // If title is not provided, always allow oldid and diff to set the title.
105 // If title is provided, allow oldid and diff to override the title, unless
106 // we are talking about a special page which might use these parameters for
107 // other purposes.
108 if ( $ret === null || !$ret->isSpecialPage() ) {
109 // We can have urls with just ?diff=,?oldid= or even just ?diff=
110 $oldid = $request->getInt( 'oldid' );
111 $oldid = $oldid ?: $request->getInt( 'diff' );
112 // Allow oldid to override a changed or missing title
113 if ( $oldid ) {
114 $revRecord = MediaWikiServices::getInstance()
115 ->getRevisionLookup()
116 ->getRevisionById( $oldid );
117 if ( $revRecord ) {
118 $ret = Title::newFromLinkTarget(
119 $revRecord->getPageAsLinkTarget()
125 // Use the main page as default title if nothing else has been provided
126 if ( $ret === null
127 && strval( $title ) === ''
128 && !$request->getCheck( 'curid' )
129 && $action !== 'delete'
131 $ret = Title::newMainPage();
134 if ( $ret === null || ( $ret->getDBkey() == '' && !$ret->isExternal() ) ) {
135 // If we get here, we definitely don't have a valid title; throw an exception.
136 // Try to get detailed invalid title exception first, fall back to MalformedTitleException.
137 Title::newFromTextThrow( $title );
138 throw new MalformedTitleException( 'badtitletext', $title );
141 return $ret;
145 * Get the Title object that we'll be acting on, as specified in the WebRequest
146 * @return Title
148 public function getTitle() {
149 if ( !$this->context->hasTitle() ) {
150 try {
151 $this->context->setTitle( $this->parseTitle() );
152 } catch ( MalformedTitleException $ex ) {
153 $this->context->setTitle( SpecialPage::getTitleFor( 'Badtitle' ) );
156 return $this->context->getTitle();
160 * Returns the name of the action that will be executed.
162 * @return string Action
164 public function getAction() : string {
165 if ( $this->action === null ) {
166 $this->action = Action::getActionName( $this->context );
169 return $this->action;
173 * Performs the request.
174 * - bad titles
175 * - read restriction
176 * - local interwiki redirects
177 * - redirect loop
178 * - special pages
179 * - normal pages
181 * @throws MWException|PermissionsError|BadTitleError|HttpError
182 * @return void
184 private function performRequest() {
185 global $wgTitle;
187 $request = $this->context->getRequest();
188 $requestTitle = $title = $this->context->getTitle();
189 $output = $this->context->getOutput();
190 $user = $this->context->getUser();
192 if ( $request->getVal( 'printable' ) === 'yes' ) {
193 $output->setPrintable();
196 $this->getHookRunner()->onBeforeInitialize( $title, null, $output, $user, $request, $this );
198 // Invalid titles. T23776: The interwikis must redirect even if the page name is empty.
199 if ( $title === null || ( $title->getDBkey() == '' && !$title->isExternal() )
200 || $title->isSpecial( 'Badtitle' )
202 $this->context->setTitle( SpecialPage::getTitleFor( 'Badtitle' ) );
203 try {
204 $this->parseTitle();
205 } catch ( MalformedTitleException $ex ) {
206 throw new BadTitleError( $ex );
208 throw new BadTitleError();
211 // Check user's permissions to read this page.
212 // We have to check here to catch special pages etc.
213 // We will check again in Article::view().
214 $permErrors = $title->isSpecial( 'RunJobs' )
215 ? [] // relies on HMAC key signature alone
216 : MediaWikiServices::getInstance()->getPermissionManager()
217 ->getPermissionErrors( 'read', $user, $title );
218 if ( count( $permErrors ) ) {
219 // T34276: allowing the skin to generate output with $wgTitle or
220 // $this->context->title set to the input title would allow anonymous users to
221 // determine whether a page exists, potentially leaking private data. In fact, the
222 // curid and oldid request parameters would allow page titles to be enumerated even
223 // when they are not guessable. So we reset the title to Special:Badtitle before the
224 // permissions error is displayed.
226 // The skin mostly uses $this->context->getTitle() these days, but some extensions
227 // still use $wgTitle.
228 $badTitle = SpecialPage::getTitleFor( 'Badtitle' );
229 $this->context->setTitle( $badTitle );
230 $wgTitle = $badTitle;
232 throw new PermissionsError( 'read', $permErrors );
235 // Interwiki redirects
236 if ( $title->isExternal() ) {
237 $rdfrom = $request->getVal( 'rdfrom' );
238 if ( $rdfrom ) {
239 $url = $title->getFullURL( [ 'rdfrom' => $rdfrom ] );
240 } else {
241 $query = $request->getValues();
242 unset( $query['title'] );
243 $url = $title->getFullURL( $query );
245 // Check for a redirect loop
246 if ( !preg_match( '/^' . preg_quote( $this->config->get( 'Server' ), '/' ) . '/', $url )
247 && $title->isLocal()
249 // 301 so google et al report the target as the actual url.
250 $output->redirect( $url, 301 );
251 } else {
252 $this->context->setTitle( SpecialPage::getTitleFor( 'Badtitle' ) );
253 try {
254 $this->parseTitle();
255 } catch ( MalformedTitleException $ex ) {
256 throw new BadTitleError( $ex );
258 throw new BadTitleError();
260 // Handle any other redirects.
261 // Redirect loops, titleless URL, $wgUsePathInfo URLs, and URLs with a variant
262 } elseif ( !$this->tryNormaliseRedirect( $title ) ) {
263 // Prevent information leak via Special:MyPage et al (T109724)
264 $spFactory = MediaWikiServices::getInstance()->getSpecialPageFactory();
265 if ( $title->isSpecialPage() ) {
266 $specialPage = $spFactory->getPage( $title->getDBkey() );
267 if ( $specialPage instanceof RedirectSpecialPage ) {
268 $specialPage->setContext( $this->context );
269 if ( $this->config->get( 'HideIdentifiableRedirects' )
270 && $specialPage->personallyIdentifiableTarget()
272 list( , $subpage ) = $spFactory->resolveAlias( $title->getDBkey() );
273 $target = $specialPage->getRedirect( $subpage );
274 // Target can also be true. We let that case fall through to normal processing.
275 if ( $target instanceof Title ) {
276 if ( $target->isExternal() ) {
277 // Handle interwiki redirects
278 $target = SpecialPage::getTitleFor(
279 'GoToInterwiki',
280 'force/' . $target->getPrefixedDBkey()
284 $query = $specialPage->getRedirectQuery( $subpage ) ?: [];
285 $request = new DerivativeRequest( $this->context->getRequest(), $query );
286 $request->setRequestURL( $this->context->getRequest()->getRequestURL() );
287 $this->context->setRequest( $request );
288 // Do not varnish cache these. May vary even for anons
289 $this->context->getOutput()->lowerCdnMaxage( 0 );
290 $this->context->setTitle( $target );
291 $wgTitle = $target;
292 // Reset action type cache. (Special pages have only view)
293 $this->action = null;
294 $title = $target;
295 $output->addJsConfigVars( [
296 'wgInternalRedirectTargetUrl' => $target->getLinkURL( $query ),
297 ] );
298 $output->addModules( 'mediawiki.action.view.redirect' );
304 // Special pages ($title may have changed since if statement above)
305 if ( $title->isSpecialPage() ) {
306 // Actions that need to be made when we have a special pages
307 $spFactory->executePath( $title, $this->context );
308 } else {
309 // ...otherwise treat it as an article view. The article
310 // may still be a wikipage redirect to another article or URL.
311 $article = $this->initializeArticle();
312 if ( is_object( $article ) ) {
313 $this->performAction( $article, $requestTitle );
314 } elseif ( is_string( $article ) ) {
315 $output->redirect( $article );
316 } else {
317 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle()"
318 . " returned neither an object nor a URL" );
321 $output->considerCacheSettingsFinal();
326 * Handle redirects for uncanonical title requests.
328 * Handles:
329 * - Redirect loops.
330 * - No title in URL.
331 * - $wgUsePathInfo URLs.
332 * - URLs with a variant.
333 * - Other non-standard URLs (as long as they have no extra query parameters).
335 * Behaviour:
336 * - Normalise title values:
337 * /wiki/Foo%20Bar -> /wiki/Foo_Bar
338 * - Normalise empty title:
339 * /wiki/ -> /wiki/Main
340 * /w/index.php?title= -> /wiki/Main
341 * - Don't redirect anything with query parameters other than 'title' or 'action=view'.
343 * @param Title $title
344 * @return bool True if a redirect was set.
345 * @throws HttpError
347 private function tryNormaliseRedirect( Title $title ) {
348 $request = $this->context->getRequest();
349 $output = $this->context->getOutput();
351 if ( $request->getVal( 'action', 'view' ) != 'view'
352 || $request->wasPosted()
353 || ( $request->getCheck( 'title' )
354 && $title->getPrefixedDBkey() == $request->getVal( 'title' ) )
355 || count( $request->getValueNames( [ 'action', 'title' ] ) )
356 || !$this->getHookRunner()->onTestCanonicalRedirect( $request, $title, $output )
358 return false;
361 if ( $this->config->get( 'MainPageIsDomainRoot' ) && $request->getRequestURL() === '/' ) {
362 return false;
365 if ( $title->isSpecialPage() ) {
366 list( $name, $subpage ) = MediaWikiServices::getInstance()->getSpecialPageFactory()->
367 resolveAlias( $title->getDBkey() );
368 if ( $name ) {
369 $title = SpecialPage::getTitleFor( $name, $subpage );
372 // Redirect to canonical url, make it a 301 to allow caching
373 $targetUrl = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
374 if ( $targetUrl == $request->getFullRequestURL() ) {
375 $message = "Redirect loop detected!\n\n" .
376 "This means the wiki got confused about what page was " .
377 "requested; this sometimes happens when moving a wiki " .
378 "to a new server or changing the server configuration.\n\n";
380 if ( $this->config->get( 'UsePathInfo' ) ) {
381 $message .= "The wiki is trying to interpret the page " .
382 "title from the URL path portion (PATH_INFO), which " .
383 "sometimes fails depending on the web server. Try " .
384 "setting \"\$wgUsePathInfo = false;\" in your " .
385 "LocalSettings.php, or check that \$wgArticlePath " .
386 "is correct.";
387 } else {
388 $message .= "Your web server was detected as possibly not " .
389 "supporting URL path components (PATH_INFO) correctly; " .
390 "check your LocalSettings.php for a customized " .
391 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
392 "to true.";
394 throw new HttpError( 500, $message );
396 $output->setCdnMaxage( 1200 );
397 $output->redirect( $targetUrl, '301' );
398 return true;
402 * Initialize the main Article object for "standard" actions (view, etc)
403 * Create an Article object for the page, following redirects if needed.
405 * @return Article|string An Article, or a string to redirect to another URL
407 private function initializeArticle() {
408 $title = $this->context->getTitle();
409 if ( $this->context->canUseWikiPage() ) {
410 // Try to use request context wiki page, as there
411 // is already data from db saved in per process
412 // cache there from this->getAction() call.
413 $page = $this->context->getWikiPage();
414 } else {
415 // This case should not happen, but just in case.
416 // @TODO: remove this or use an exception
417 $page = WikiPage::factory( $title );
418 $this->context->setWikiPage( $page );
419 wfWarn( "RequestContext::canUseWikiPage() returned false" );
422 // Make GUI wrapper for the WikiPage
423 $article = Article::newFromWikiPage( $page, $this->context );
425 // Skip some unnecessary code if the content model doesn't support redirects
426 if ( !MediaWikiServices::getInstance()
427 ->getContentHandlerFactory()
428 ->getContentHandler( $title->getContentModel() )
429 ->supportsRedirects()
431 return $article;
434 $request = $this->context->getRequest();
436 // Namespace might change when using redirects
437 // Check for redirects ...
438 $action = $request->getVal( 'action', 'view' );
439 $file = ( $page instanceof WikiFilePage ) ? $page->getFile() : null;
440 if ( ( $action == 'view' || $action == 'render' ) // ... for actions that show content
441 && !$request->getVal( 'oldid' ) // ... and are not old revisions
442 && !$request->getVal( 'diff' ) // ... and not when showing diff
443 && $request->getVal( 'redirect' ) != 'no' // ... unless explicitly told not to
444 // ... and the article is not a non-redirect image page with associated file
445 && !( is_object( $file ) && $file->exists() && !$file->getRedirected() )
447 // Give extensions a change to ignore/handle redirects as needed
448 $ignoreRedirect = $target = false;
450 $this->getHookRunner()->onInitializeArticleMaybeRedirect( $title, $request,
451 $ignoreRedirect, $target, $article );
452 $page = $article->getPage(); // reflect any hook changes
454 // Follow redirects only for... redirects.
455 // If $target is set, then a hook wanted to redirect.
456 if ( !$ignoreRedirect && ( $target || $page->isRedirect() ) ) {
457 // Is the target already set by an extension?
458 $target = $target ?: $page->followRedirect();
459 if ( is_string( $target ) && !$this->config->get( 'DisableHardRedirects' ) ) {
460 // we'll need to redirect
461 return $target;
463 if ( is_object( $target ) ) {
464 // Rewrite environment to redirected article
465 $rpage = WikiPage::factory( $target );
466 $rpage->loadPageData();
467 if ( $rpage->exists() || ( is_object( $file ) && !$file->isLocal() ) ) {
468 $rarticle = Article::newFromWikiPage( $rpage, $this->context );
469 $rarticle->setRedirectedFrom( $title );
471 $article = $rarticle;
472 $this->context->setTitle( $target );
473 $this->context->setWikiPage( $article->getPage() );
476 } else {
477 // Article may have been changed by hook
478 $this->context->setTitle( $article->getTitle() );
479 $this->context->setWikiPage( $article->getPage() );
483 return $article;
487 * Perform one of the "standard" actions
489 * @param Article $article
490 * @param Title $requestTitle The original title, before any redirects were applied
492 private function performAction( Article $article, Title $requestTitle ) {
493 $request = $this->context->getRequest();
494 $output = $this->context->getOutput();
495 $title = $this->context->getTitle();
496 $user = $this->context->getUser();
498 if ( !$this->getHookRunner()->onMediaWikiPerformAction(
499 $output, $article, $title, $user, $request, $this )
501 return;
504 $act = $this->getAction();
505 $action = Action::factory( $act, $article, $this->context );
507 if ( $action instanceof Action ) {
508 // Narrow DB query expectations for this HTTP request
509 $trxLimits = $this->config->get( 'TrxProfilerLimits' );
510 $trxProfiler = Profiler::instance()->getTransactionProfiler();
511 if ( $request->wasPosted() && !$action->doesWrites() ) {
512 $trxProfiler->setExpectations( $trxLimits['POST-nonwrite'], __METHOD__ );
513 $request->markAsSafeRequest();
516 # Let CDN cache things if we can purge them.
517 if ( $this->config->get( 'UseCdn' ) &&
518 in_array(
519 // Use PROTO_INTERNAL because that's what getCdnUrls() uses
520 wfExpandUrl( $request->getRequestURL(), PROTO_INTERNAL ),
521 $requestTitle->getCdnUrls()
524 $output->setCdnMaxage( $this->config->get( 'CdnMaxAge' ) );
527 $action->show();
528 return;
531 // If we've not found out which action it is by now, it's unknown
532 $output->setStatusCode( 404 );
533 $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
537 * Run the current MediaWiki instance; index.php just calls this
539 public function run() {
540 try {
541 $this->setDBProfilingAgent();
542 try {
543 $this->main();
544 } catch ( ErrorPageError $e ) {
545 $out = $this->context->getOutput();
546 // TODO: Should ErrorPageError::report accept a OutputPage parameter?
547 $e->report( ErrorPageError::STAGE_OUTPUT );
548 $out->considerCacheSettingsFinal();
550 // T64091: while exceptions are convenient to bubble up GUI errors,
551 // they are not internal application faults. As with normal requests, this
552 // should commit, print the output, do deferred updates, jobs, and profiling.
553 $this->doPreOutputCommit();
554 $out->output(); // display the GUI error
556 } catch ( Exception $e ) {
557 $context = $this->context;
558 $action = $context->getRequest()->getVal( 'action', 'view' );
559 if (
560 $e instanceof DBConnectionError &&
561 $context->hasTitle() &&
562 $context->getTitle()->canExist() &&
563 in_array( $action, [ 'view', 'history' ], true ) &&
564 HTMLFileCache::useFileCache( $context, HTMLFileCache::MODE_OUTAGE )
566 // Try to use any (even stale) file during outages...
567 $cache = new HTMLFileCache( $context->getTitle(), $action );
568 if ( $cache->isCached() ) {
569 $cache->loadFromFileCache( $context, HTMLFileCache::MODE_OUTAGE );
570 print MWExceptionRenderer::getHTML( $e );
571 exit;
574 // GUI-ify and stash the page output in MediaWiki::doPreOutputCommit() while
575 // ChronologyProtector synchronizes DB positions or replicas across all datacenters.
576 MWExceptionHandler::handleException( $e, MWExceptionHandler::CAUGHT_BY_ENTRYPOINT );
577 } catch ( Throwable $e ) {
578 // Type errors and such: at least handle it now and clean up the LBFactory state
579 MWExceptionHandler::handleException( $e, MWExceptionHandler::CAUGHT_BY_ENTRYPOINT );
582 $this->doPostOutputShutdown();
586 * Add a comment to future SQL queries for easy SHOW PROCESSLIST interpretation
588 private function setDBProfilingAgent() {
589 $services = MediaWikiServices::getInstance();
590 $name = $this->context->getUser()->getName();
591 $services->getDBLoadBalancerFactory()->setAgentName(
592 mb_strlen( $name ) > 15 ? mb_substr( $name, 0, 15 ) . '...' : $name
597 * If enabled, after everything specific to this request is done, occasionally run jobs
599 private function schedulePostSendJobs() {
600 $jobRunRate = $this->config->get( 'JobRunRate' );
601 if (
602 // Recursion guard
603 $this->getTitle()->isSpecial( 'RunJobs' ) ||
604 // Short circuit if there is nothing to do
605 ( $jobRunRate <= 0 || wfReadOnly() ) ||
606 // Avoid blocking the client on stock apache; see doPostOutputShutdown()
608 $this->context->getRequest()->getMethod() === 'HEAD' ||
609 $this->context->getRequest()->getHeader( 'If-Modified-Since' )
612 return;
615 if ( $jobRunRate < 1 ) {
616 $max = mt_getrandmax();
617 if ( mt_rand( 0, $max ) > $max * $jobRunRate ) {
618 return; // the higher the job run rate, the less likely we return here
620 $n = 1;
621 } else {
622 $n = intval( $jobRunRate );
625 // Note that DeferredUpdates will catch and log an errors (T88312)
626 DeferredUpdates::addUpdate( new TransactionRoundDefiningUpdate( function () use ( $n ) {
627 $logger = LoggerFactory::getInstance( 'runJobs' );
628 if ( $this->config->get( 'RunJobsAsync' ) ) {
629 // Send an HTTP request to the job RPC entry point if possible
630 $invokedWithSuccess = $this->triggerAsyncJobs( $n, $logger );
631 if ( !$invokedWithSuccess ) {
632 // Fall back to blocking on running the job(s)
633 $logger->warning( "Jobs switched to blocking; Special:RunJobs disabled" );
634 $this->triggerSyncJobs( $n );
636 } else {
637 $this->triggerSyncJobs( $n );
639 }, __METHOD__ ) );
643 * @see MediaWiki::preOutputCommit()
644 * @param callable|null $postCommitWork [default: null]
645 * @since 1.26
647 public function doPreOutputCommit( callable $postCommitWork = null ) {
648 self::preOutputCommit( $this->context, $postCommitWork );
652 * This function commits all DB and session changes as needed *before* the
653 * client can receive a response (in case DB commit fails) and thus also before
654 * the response can trigger a subsequent related request by the client
656 * If there is a significant amount of content to flush, it can be done in $postCommitWork
658 * @param IContextSource $context
659 * @param callable|null $postCommitWork [default: null]
660 * @since 1.27
662 public static function preOutputCommit(
663 IContextSource $context, callable $postCommitWork = null
665 $config = $context->getConfig();
666 $request = $context->getRequest();
667 $output = $context->getOutput();
668 $services = MediaWikiServices::getInstance();
669 $lbFactory = $services->getDBLoadBalancerFactory();
671 // Try to make sure that all RDBMs, session, and other storage updates complete
672 ignore_user_abort( true );
674 // Commit all RDBMs changes from the main transaction round
675 $lbFactory->commitMasterChanges(
676 __METHOD__,
677 // Abort if any transaction was too big
678 [ 'maxWriteDuration' => $config->get( 'MaxUserDBWriteDuration' ) ]
680 wfDebug( __METHOD__ . ': primary transaction round committed' );
682 // Run updates that need to block the client or affect output (this is the last chance)
683 DeferredUpdates::doUpdates( 'run', DeferredUpdates::PRESEND );
684 wfDebug( __METHOD__ . ': pre-send deferred updates completed' );
685 // Persist the session to avoid race conditions on subsequent requests by the client
686 $request->getSession()->save(); // T214471
687 wfDebug( __METHOD__ . ': session changes committed' );
689 // Figure out whether to wait for DB replication now or to use some method that assures
690 // that subsequent requests by the client will use the DB replication positions written
691 // during the shutdown() call below; the later requires working around replication lag
692 // of the store containing DB replication positions (e.g. dynomite, mcrouter).
693 list( $flags, $strategy ) = self::getChronProtStrategy( $lbFactory, $output );
694 // Record ChronologyProtector positions for DBs affected in this request at this point
695 $cpIndex = null;
696 $cpClientId = null;
697 $lbFactory->shutdown( $flags, $postCommitWork, $cpIndex, $cpClientId );
698 wfDebug( __METHOD__ . ': LBFactory shutdown completed' );
700 $allowHeaders = !( $output->isDisabled() || headers_sent() );
701 if ( $cpIndex > 0 ) {
702 if ( $allowHeaders ) {
703 $now = time();
704 $expires = $now + ChronologyProtector::POSITION_COOKIE_TTL;
705 $options = [ 'prefix' => '' ];
706 $value = $lbFactory::makeCookieValueFromCPIndex( $cpIndex, $now, $cpClientId );
707 $request->response()->setCookie( 'cpPosIndex', $value, $expires, $options );
710 if ( $strategy === 'cookie+url' ) {
711 if ( $output->getRedirect() ) { // sanity
712 $safeUrl = $lbFactory->appendShutdownCPIndexAsQuery(
713 $output->getRedirect(),
714 $cpIndex
716 $output->redirect( $safeUrl );
717 } else {
718 MWExceptionHandler::logException(
719 new LogicException( "No redirect; cannot append cpPosIndex parameter." ),
720 MWExceptionHandler::CAUGHT_BY_ENTRYPOINT
726 if ( $allowHeaders ) {
727 // Set a cookie to tell all CDN edge nodes to "stick" the user to the DC that
728 // handles this POST request (e.g. the "master" data center). Also have the user
729 // briefly bypass CDN so ChronologyProtector works for cacheable URLs.
730 if ( $request->wasPosted() && $lbFactory->hasOrMadeRecentMasterChanges() ) {
731 $expires = time() + $config->get( 'DataCenterUpdateStickTTL' );
732 $options = [ 'prefix' => '' ];
733 $request->response()->setCookie( 'UseDC', 'master', $expires, $options );
734 $request->response()->setCookie( 'UseCDNCache', 'false', $expires, $options );
737 // Avoid letting a few seconds of replica DB lag cause a month of stale data.
738 // This logic is also intimately related to the value of $wgCdnReboundPurgeDelay.
739 if ( $lbFactory->laggedReplicaUsed() ) {
740 $maxAge = $config->get( 'CdnMaxageLagged' );
741 $output->lowerCdnMaxage( $maxAge );
742 $request->response()->header( "X-Database-Lagged: true" );
743 wfDebugLog( 'replication',
744 "Lagged DB used; CDN cache TTL limited to $maxAge seconds" );
747 // Avoid long-term cache pollution due to message cache rebuild timeouts (T133069)
748 if ( $services->getMessageCache()->isDisabled() ) {
749 $maxAge = $config->get( 'CdnMaxageSubstitute' );
750 $output->lowerCdnMaxage( $maxAge );
751 $request->response()->header( "X-Response-Substitute: true" );
754 if ( !$output->couldBePublicCached() || $output->haveCacheVaryCookies() ) {
755 // Autoblocks: If this user is autoblocked (and the cookie block feature is enabled
756 // for autoblocks), then set a cookie to track this block.
757 // This has to be done on all logged-in page loads (not just upon saving edits),
758 // because an autoblocked editor might not edit again from the same IP address.
760 // IP blocks: For anons, if their IP is blocked (and cookie block feature is enabled
761 // for IP blocks), we also want to set the cookie whenever it is safe to do.
762 // Basically from any url that are definitely not publicly cacheable (like viewing
763 // EditPage), or when the HTTP response is personalised for other reasons (e.g. viewing
764 // articles within the same browsing session after making an edit).
765 $user = $context->getUser();
766 $services->getBlockManager()
767 ->trackBlockWithCookie( $user, $request->response() );
773 * @param ILBFactory $lbFactory
774 * @param OutputPage $output
775 * @return array
777 private static function getChronProtStrategy( ILBFactory $lbFactory, OutputPage $output ) {
778 // Should the client return, their request should observe the new ChronologyProtector
779 // DB positions. This request might be on a foreign wiki domain, so synchronously update
780 // the DB positions in all datacenters to be safe. If this output is not a redirect,
781 // then OutputPage::output() will be relatively slow, meaning that running it in
782 // $postCommitWork should help mask the latency of those updates.
783 $flags = $lbFactory::SHUTDOWN_CHRONPROT_SYNC;
784 $strategy = 'cookie+sync';
786 $allowHeaders = !( $output->isDisabled() || headers_sent() );
787 if ( $output->getRedirect() && $lbFactory->hasOrMadeRecentMasterChanges( INF ) ) {
788 // OutputPage::output() will be fast, so $postCommitWork is useless for masking
789 // the latency of synchronously updating the DB positions in all datacenters.
790 // Try to make use of the time the client spends following redirects instead.
791 $domainDistance = self::getUrlDomainDistance( $output->getRedirect() );
792 if ( $domainDistance === 'local' && $allowHeaders ) {
793 $flags = $lbFactory::SHUTDOWN_CHRONPROT_ASYNC;
794 $strategy = 'cookie'; // use same-domain cookie and keep the URL uncluttered
795 } elseif ( $domainDistance === 'remote' ) {
796 $flags = $lbFactory::SHUTDOWN_CHRONPROT_ASYNC;
797 $strategy = 'cookie+url'; // cross-domain cookie might not work
801 return [ $flags, $strategy ];
805 * @param string $url
806 * @return string Either "local", "remote" if in the farm, "external" otherwise
808 private static function getUrlDomainDistance( $url ) {
809 $clusterWiki = WikiMap::getWikiFromUrl( $url );
810 if ( WikiMap::isCurrentWikiId( $clusterWiki ) ) {
811 return 'local'; // the current wiki
813 if ( $clusterWiki !== false ) {
814 return 'remote'; // another wiki in this cluster/farm
817 return 'external';
821 * This function does work that can be done *after* the
822 * user gets the HTTP response so they don't block on it
824 * This manages deferred updates, job insertion,
825 * final commit, and the logging of profiling data
827 * @since 1.26
829 public function doPostOutputShutdown() {
830 // Record backend request timing
831 $timing = $this->context->getTiming();
832 $timing->mark( 'requestShutdown' );
834 // Perform the last synchronous operations...
835 try {
836 // Show visible profiling data if enabled (which cannot be post-send)
837 Profiler::instance()->logDataPageOutputOnly();
838 } catch ( Throwable $e ) {
839 // An error may already have been shown in run(), so just log it to be safe
840 MWExceptionHandler::logException( $e, MWExceptionHandler::CAUGHT_BY_ENTRYPOINT );
843 // Disable WebResponse setters for post-send processing (T191537).
844 WebResponse::disableForPostSend();
846 // Defer everything else if possible...
847 $callback = function () {
848 try {
849 $this->restInPeace();
850 } catch ( Throwable $e ) {
851 // If this is post-send, then displaying errors can cause broken HTML
852 MWExceptionHandler::rollbackMasterChangesAndLog(
854 MWExceptionHandler::CAUGHT_BY_ENTRYPOINT
859 if ( $this->postSendStrategy === self::DEFER_FASTCGI_FINISH_REQUEST ) {
860 fastcgi_finish_request();
861 $callback();
862 } else {
863 // Flush PHP and web server output buffers
864 if ( !$this->config->get( 'CommandLineMode' ) ) {
865 AtEase\AtEase::suppressWarnings();
866 if ( ob_get_status() ) {
867 ob_end_flush();
869 flush();
870 AtEase\AtEase::restoreWarnings();
872 $callback();
877 * Determine and send the response headers and body for this web request
879 private function main() {
880 global $wgTitle;
882 $output = $this->context->getOutput();
883 $request = $this->context->getRequest();
885 // Send Ajax requests to the Ajax dispatcher.
886 if ( $request->getVal( 'action' ) === 'ajax' ) {
887 // Set a dummy title, because $wgTitle == null might break things
888 $title = Title::makeTitle( NS_SPECIAL, 'Badtitle/performing an AJAX call in '
889 . __METHOD__
891 $this->context->setTitle( $title );
892 $wgTitle = $title;
894 $dispatcher = new AjaxDispatcher( $this->config );
895 $dispatcher->performAction( $this->context->getUser() );
897 return;
900 // Get title from request parameters,
901 // is set on the fly by parseTitle the first time.
902 $title = $this->getTitle();
903 $action = $this->getAction();
904 $wgTitle = $title;
906 // Set DB query expectations for this HTTP request
907 $trxLimits = $this->config->get( 'TrxProfilerLimits' );
908 $trxProfiler = Profiler::instance()->getTransactionProfiler();
909 $trxProfiler->setLogger( LoggerFactory::getInstance( 'DBPerformance' ) );
910 if ( $request->hasSafeMethod() ) {
911 $trxProfiler->setExpectations( $trxLimits['GET'], __METHOD__ );
912 } else {
913 $trxProfiler->setExpectations( $trxLimits['POST'], __METHOD__ );
916 if ( $this->maybeDoHttpsRedirect() ) {
917 return;
920 if ( $title->canExist() && HTMLFileCache::useFileCache( $this->context ) ) {
921 // Try low-level file cache hit
922 $cache = new HTMLFileCache( $title, $action );
923 if ( $cache->isCacheGood( /* Assume up to date */ ) ) {
924 // Check incoming headers to see if client has this cached
925 $timestamp = $cache->cacheTimestamp();
926 if ( !$output->checkLastModified( $timestamp ) ) {
927 $cache->loadFromFileCache( $this->context );
929 // Do any stats increment/watchlist stuff, assuming user is viewing the
930 // latest revision (which should always be the case for file cache)
931 $this->context->getWikiPage()->doViewUpdates( $this->context->getUser() );
932 // Tell OutputPage that output is taken care of
933 $output->disable();
935 return;
939 // Actually do the work of the request and build up any output
940 $this->performRequest();
942 // GUI-ify and stash the page output in MediaWiki::doPreOutputCommit() while
943 // ChronologyProtector synchronizes DB positions or replicas across all datacenters.
944 $buffer = null;
945 $outputWork = function () use ( $output, &$buffer ) {
946 if ( $buffer === null ) {
947 $buffer = $output->output( true );
950 return $buffer;
953 // Commit any changes in the current transaction round so that:
954 // a) the transaction is not rolled back after success output was already sent
955 // b) error output is not jumbled together with success output in the response
956 $this->doPreOutputCommit( $outputWork );
957 // If needed, push a deferred update to run jobs after the output is send
958 $this->schedulePostSendJobs();
959 // If no exceptions occurred then send the output since it is safe now
960 $this->outputResponsePayload( $outputWork() );
964 * Check if an HTTP->HTTPS redirect should be done. It may still be aborted
965 * by a hook, so this is not the final word.
967 * @return bool
969 private function shouldDoHttpRedirect() {
970 $request = $this->context->getRequest();
972 // Don't redirect if we're already on HTTPS
973 if ( $request->getProtocol() !== 'http' ) {
974 return false;
977 $force = $this->config->get( 'ForceHTTPS' );
979 // Don't redirect if $wgServer is explicitly HTTP. We test for this here
980 // by checking whether wfExpandUrl() is able to force HTTPS.
981 if ( !preg_match( '#^https://#', wfExpandUrl( $request->getRequestURL(), PROTO_HTTPS ) ) ) {
982 if ( $force ) {
983 throw new RuntimeException( '$wgForceHTTPS is true but the server is not HTTPS' );
985 return false;
988 // Configured $wgForceHTTPS overrides the remaining conditions
989 if ( $force ) {
990 return true;
993 // Check if HTTPS is required by the session or user preferences
994 return $request->getSession()->shouldForceHTTPS() ||
995 // Check the cookie manually, for paranoia
996 $request->getCookie( 'forceHTTPS', '' ) ||
997 // Avoid checking the user and groups unless it's enabled.
999 $this->context->getUser()->isLoggedIn()
1000 && $this->context->getUser()->requiresHTTPS()
1005 * If the stars are suitably aligned, do an HTTP->HTTPS redirect
1007 * Note: Do this after $wgTitle is setup, otherwise the hooks run from
1008 * isLoggedIn() will do all sorts of weird stuff.
1010 * @return bool True if the redirect was done. Handling of the request
1011 * should be aborted. False if no redirect was done.
1013 private function maybeDoHttpsRedirect() {
1014 if ( !$this->shouldDoHttpRedirect() ) {
1015 return false;
1018 $request = $this->context->getRequest();
1019 $oldUrl = $request->getFullRequestURL();
1020 $redirUrl = preg_replace( '#^http://#', 'https://', $oldUrl );
1022 // ATTENTION: This hook is likely to be removed soon due to overall design of the system.
1023 if ( !$this->getHookRunner()->onBeforeHttpsRedirect( $this->context, $redirUrl ) ) {
1024 return false;
1027 if ( $request->wasPosted() ) {
1028 // This is weird and we'd hope it almost never happens. This
1029 // means that a POST came in via HTTP and policy requires us
1030 // redirecting to HTTPS. It's likely such a request is going
1031 // to fail due to post data being lost, but let's try anyway
1032 // and just log the instance.
1034 // @todo FIXME: See if we could issue a 307 or 308 here, need
1035 // to see how clients (automated & browser) behave when we do
1036 wfDebugLog( 'RedirectedPosts', "Redirected from HTTP to HTTPS: $oldUrl" );
1038 // Setup dummy Title, otherwise OutputPage::redirect will fail
1039 $title = Title::newFromText( 'REDIR', NS_MAIN );
1040 $this->context->setTitle( $title );
1041 // Since we only do this redir to change proto, always send a vary header
1042 $output = $this->context->getOutput();
1043 $output->addVaryHeader( 'X-Forwarded-Proto' );
1044 $output->redirect( $redirUrl );
1045 $output->output();
1047 return true;
1051 * Set the actual output and attempt to flush it to the client if necessary
1053 * No PHP buffers should be active at this point
1055 * @param string $content
1057 private function outputResponsePayload( $content ) {
1058 if (
1059 $this->postSendStrategy === self::DEFER_SET_LENGTH_AND_FLUSH &&
1060 DeferredUpdates::pendingUpdatesCount() &&
1061 !headers_sent()
1063 $response = $this->context->getRequest()->response();
1064 // Make the browser indicate the page as "loaded" as soon as it gets all the content
1065 $response->header( 'Connection: close' );
1066 // The client should not be blocked on "post-send" updates. If apache or ob_* decide
1067 // that a response should be gzipped, the entire script will have to finish before
1068 // any data can be sent. Disable compression if there are any post-send updates.
1069 $response->header( 'Content-Encoding: none' );
1070 AtEase\AtEase::suppressWarnings();
1071 ini_set( 'zlib.output_compression', 0 );
1072 if ( function_exists( 'apache_setenv' ) ) {
1073 apache_setenv( 'no-gzip', '1' );
1075 AtEase\AtEase::restoreWarnings();
1076 // Also set the Content-Length so that apache does not block waiting on PHP to finish.
1077 // If OutputPage is disabled, then either there is no body (e.g. HTTP 304) and thus no
1078 // Content-Length, or it was taken care of already.
1079 if ( !$this->context->getOutput()->isDisabled() ) {
1080 ob_start();
1081 print $content;
1082 $response->header( 'Content-Length: ' . ob_get_length() );
1083 ob_end_flush();
1085 // @TODO: this still blocks on HEAD responses and 304 responses to GETs
1086 } else {
1087 print $content;
1092 * Ends this task peacefully
1094 public function restInPeace() {
1095 // Either all DB and deferred updates should happen or none.
1096 // The latter should not be cancelled due to client disconnect.
1097 ignore_user_abort( true );
1099 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
1100 // Assure deferred updates are not in the main transaction
1101 $lbFactory->commitMasterChanges( __METHOD__ );
1103 // Loosen DB query expectations since the HTTP client is unblocked
1104 $trxProfiler = Profiler::instance()->getTransactionProfiler();
1105 $trxProfiler->redefineExpectations(
1106 $this->context->getRequest()->hasSafeMethod()
1107 ? $this->config->get( 'TrxProfilerLimits' )['PostSend-GET']
1108 : $this->config->get( 'TrxProfilerLimits' )['PostSend-POST'],
1109 __METHOD__
1112 // Do any deferred jobs; preferring to run them now if a client will not wait on them
1113 DeferredUpdates::doUpdates( 'run' );
1115 // Log profiling data, e.g. in the database or UDP
1116 wfLogProfilingData();
1118 // Commit and close up!
1119 $lbFactory->commitMasterChanges( __METHOD__ );
1120 $lbFactory->shutdown( $lbFactory::SHUTDOWN_NO_CHRONPROT );
1122 wfDebug( "Request ended normally" );
1126 * Send out any buffered statsd data according to sampling rules
1128 * @param IBufferingStatsdDataFactory $stats
1129 * @param Config $config
1130 * @throws ConfigException
1131 * @since 1.31
1133 public static function emitBufferedStatsdData(
1134 IBufferingStatsdDataFactory $stats, Config $config
1136 if ( $config->get( 'StatsdServer' ) && $stats->hasData() ) {
1137 try {
1138 $statsdServer = explode( ':', $config->get( 'StatsdServer' ), 2 );
1139 $statsdHost = $statsdServer[0];
1140 $statsdPort = $statsdServer[1] ?? 8125;
1141 $statsdSender = new SocketSender( $statsdHost, $statsdPort );
1142 $statsdClient = new SamplingStatsdClient( $statsdSender, true, false );
1143 $statsdClient->setSamplingRates( $config->get( 'StatsdSamplingRates' ) );
1144 $statsdClient->send( $stats->getData() );
1146 $stats->clearData(); // empty buffer for the next round
1147 } catch ( Exception $e ) {
1148 MWExceptionHandler::logException( $e, MWExceptionHandler::CAUGHT_BY_ENTRYPOINT );
1154 * Potentially open a socket and sent an HTTP request back to the server
1155 * to run a specified number of jobs. This registers a callback to cleanup
1156 * the socket once it's done.
1157 * @deprecated Since 1.34
1159 public function triggerJobs() {
1160 $jobRunRate = $this->config->get( 'JobRunRate' );
1161 if ( $this->getTitle()->isSpecial( 'RunJobs' ) ) {
1162 return; // recursion guard
1163 } elseif ( $jobRunRate <= 0 || wfReadOnly() ) {
1164 return;
1167 if ( $jobRunRate < 1 ) {
1168 $max = mt_getrandmax();
1169 if ( mt_rand( 0, $max ) > $max * $jobRunRate ) {
1170 return; // the higher the job run rate, the less likely we return here
1172 $n = 1;
1173 } else {
1174 $n = intval( $jobRunRate );
1177 $logger = LoggerFactory::getInstance( 'runJobs' );
1179 try {
1180 if ( $this->config->get( 'RunJobsAsync' ) ) {
1181 // Send an HTTP request to the job RPC entry point if possible
1182 $invokedWithSuccess = $this->triggerAsyncJobs( $n, $logger );
1183 if ( !$invokedWithSuccess ) {
1184 // Fall back to blocking on running the job(s)
1185 $logger->warning( "Jobs switched to blocking; Special:RunJobs disabled" );
1186 $this->triggerSyncJobs( $n );
1188 } else {
1189 $this->triggerSyncJobs( $n );
1191 } catch ( JobQueueError $e ) {
1192 // Do not make the site unavailable (T88312)
1193 MWExceptionHandler::logException( $e, MWExceptionHandler::CAUGHT_BY_ENTRYPOINT );
1198 * @param int $n Number of jobs to try to run
1200 private function triggerSyncJobs( $n ) {
1201 $trxProfiler = Profiler::instance()->getTransactionProfiler();
1202 $old = $trxProfiler->setSilenced( true );
1203 try {
1204 $runner = MediaWikiServices::getInstance()->getJobRunner();
1205 $runner->run( [ 'maxJobs' => $n ] );
1206 } finally {
1207 $trxProfiler->setSilenced( $old );
1212 * @param int $n Number of jobs to try to run
1213 * @param LoggerInterface $runJobsLogger
1214 * @return bool Success
1216 private function triggerAsyncJobs( $n, LoggerInterface $runJobsLogger ) {
1217 // Do not send request if there are probably no jobs
1218 $group = JobQueueGroup::singleton();
1219 if ( !$group->queuesHaveJobs( JobQueueGroup::TYPE_DEFAULT ) ) {
1220 return true;
1223 $query = [ 'title' => 'Special:RunJobs',
1224 'tasks' => 'jobs', 'maxjobs' => $n, 'sigexpiry' => time() + 5 ];
1225 $query['signature'] = SpecialRunJobs::getQuerySignature(
1226 $query, $this->config->get( 'SecretKey' ) );
1228 $errno = $errstr = null;
1229 $info = wfParseUrl( $this->config->get( 'CanonicalServer' ) );
1230 $host = $info ? $info['host'] : null;
1231 $port = 80;
1232 if ( isset( $info['scheme'] ) && $info['scheme'] == 'https' ) {
1233 $host = "tls://" . $host;
1234 $port = 443;
1236 if ( isset( $info['port'] ) ) {
1237 $port = $info['port'];
1240 Wikimedia\suppressWarnings();
1241 $sock = $host ? fsockopen(
1242 $host,
1243 $port,
1244 $errno,
1245 $errstr,
1246 // If it takes more than 100ms to connect to ourselves there is a problem...
1247 0.100
1248 ) : false;
1249 Wikimedia\restoreWarnings();
1251 $invokedWithSuccess = true;
1252 if ( $sock ) {
1253 $special = MediaWikiServices::getInstance()->getSpecialPageFactory()->
1254 getPage( 'RunJobs' );
1255 $url = $special->getPageTitle()->getCanonicalURL( $query );
1256 $req = (
1257 "POST $url HTTP/1.1\r\n" .
1258 "Host: {$info['host']}\r\n" .
1259 "Connection: Close\r\n" .
1260 "Content-Length: 0\r\n\r\n"
1263 $runJobsLogger->info( "Running $n job(s) via '$url'" );
1264 // Send a cron API request to be performed in the background.
1265 // Give up if this takes too long to send (which should be rare).
1266 stream_set_timeout( $sock, 2 );
1267 $bytes = fwrite( $sock, $req );
1268 if ( $bytes !== strlen( $req ) ) {
1269 $invokedWithSuccess = false;
1270 $runJobsLogger->error( "Failed to start cron API (socket write error)" );
1271 } else {
1272 // Do not wait for the response (the script should handle client aborts).
1273 // Make sure that we don't close before that script reaches ignore_user_abort().
1274 $start = microtime( true );
1275 $status = fgets( $sock );
1276 $sec = microtime( true ) - $start;
1277 if ( !preg_match( '#^HTTP/\d\.\d 202 #', $status ) ) {
1278 $invokedWithSuccess = false;
1279 $runJobsLogger->error( "Failed to start cron API: received '$status' ($sec)" );
1282 fclose( $sock );
1283 } else {
1284 $invokedWithSuccess = false;
1285 $runJobsLogger->error( "Failed to start cron API (socket error $errno): $errstr" );
1288 return $invokedWithSuccess;