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