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