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