MediaWiki REL1_28
MediaWiki.php
Go to the documentation of this file.
1<?php
25
29class MediaWiki {
33 private $context;
34
38 private $config;
39
43 private $action;
44
48 public function __construct( IContextSource $context = null ) {
49 if ( !$context ) {
51 }
52
53 $this->context = $context;
54 $this->config = $context->getConfig();
55 }
56
63 private function parseTitle() {
65
66 $request = $this->context->getRequest();
67 $curid = $request->getInt( 'curid' );
68 $title = $request->getVal( 'title' );
69 $action = $request->getVal( 'action' );
70
71 if ( $request->getCheck( 'search' ) ) {
72 // Compatibility with old search URLs which didn't use Special:Search
73 // Just check for presence here, so blank requests still
74 // show the search page when using ugly URLs (bug 8054).
75 $ret = SpecialPage::getTitleFor( 'Search' );
76 } elseif ( $curid ) {
77 // URLs like this are generated by RC, because rc_title isn't always accurate
78 $ret = Title::newFromID( $curid );
79 } else {
80 $ret = Title::newFromURL( $title );
81 // Alias NS_MEDIA page URLs to NS_FILE...we only use NS_MEDIA
82 // in wikitext links to tell Parser to make a direct file link
83 if ( !is_null( $ret ) && $ret->getNamespace() == NS_MEDIA ) {
84 $ret = Title::makeTitle( NS_FILE, $ret->getDBkey() );
85 }
86 // Check variant links so that interwiki links don't have to worry
87 // about the possible different language variants
88 if ( count( $wgContLang->getVariants() ) > 1
89 && !is_null( $ret ) && $ret->getArticleID() == 0
90 ) {
91 $wgContLang->findVariantLink( $title, $ret );
92 }
93 }
94
95 // If title is not provided, always allow oldid and diff to set the title.
96 // If title is provided, allow oldid and diff to override the title, unless
97 // we are talking about a special page which might use these parameters for
98 // other purposes.
99 if ( $ret === null || !$ret->isSpecialPage() ) {
100 // We can have urls with just ?diff=,?oldid= or even just ?diff=
101 $oldid = $request->getInt( 'oldid' );
102 $oldid = $oldid ? $oldid : $request->getInt( 'diff' );
103 // Allow oldid to override a changed or missing title
104 if ( $oldid ) {
105 $rev = Revision::newFromId( $oldid );
106 $ret = $rev ? $rev->getTitle() : $ret;
107 }
108 }
109
110 // Use the main page as default title if nothing else has been provided
111 if ( $ret === null
112 && strval( $title ) === ''
113 && !$request->getCheck( 'curid' )
114 && $action !== 'delete'
115 ) {
116 $ret = Title::newMainPage();
117 }
118
119 if ( $ret === null || ( $ret->getDBkey() == '' && !$ret->isExternal() ) ) {
120 // If we get here, we definitely don't have a valid title; throw an exception.
121 // Try to get detailed invalid title exception first, fall back to MalformedTitleException.
122 Title::newFromTextThrow( $title );
123 throw new MalformedTitleException( 'badtitletext', $title );
124 }
125
126 return $ret;
127 }
128
133 public function getTitle() {
134 if ( !$this->context->hasTitle() ) {
135 try {
136 $this->context->setTitle( $this->parseTitle() );
137 } catch ( MalformedTitleException $ex ) {
138 $this->context->setTitle( SpecialPage::getTitleFor( 'Badtitle' ) );
139 }
140 }
141 return $this->context->getTitle();
142 }
143
149 public function getAction() {
150 if ( $this->action === null ) {
151 $this->action = Action::getActionName( $this->context );
152 }
153
154 return $this->action;
155 }
156
169 private function performRequest() {
171
172 $request = $this->context->getRequest();
173 $requestTitle = $title = $this->context->getTitle();
174 $output = $this->context->getOutput();
175 $user = $this->context->getUser();
176
177 if ( $request->getVal( 'printable' ) === 'yes' ) {
178 $output->setPrintable();
179 }
180
181 $unused = null; // To pass it by reference
182 Hooks::run( 'BeforeInitialize', [ &$title, &$unused, &$output, &$user, $request, $this ] );
183
184 // Invalid titles. Bug 21776: The interwikis must redirect even if the page name is empty.
185 if ( is_null( $title ) || ( $title->getDBkey() == '' && !$title->isExternal() )
186 || $title->isSpecial( 'Badtitle' )
187 ) {
188 $this->context->setTitle( SpecialPage::getTitleFor( 'Badtitle' ) );
189 try {
190 $this->parseTitle();
191 } catch ( MalformedTitleException $ex ) {
192 throw new BadTitleError( $ex );
193 }
194 throw new BadTitleError();
195 }
196
197 // Check user's permissions to read this page.
198 // We have to check here to catch special pages etc.
199 // We will check again in Article::view().
200 $permErrors = $title->isSpecial( 'RunJobs' )
201 ? [] // relies on HMAC key signature alone
202 : $title->getUserPermissionsErrors( 'read', $user );
203 if ( count( $permErrors ) ) {
204 // Bug 32276: allowing the skin to generate output with $wgTitle or
205 // $this->context->title set to the input title would allow anonymous users to
206 // determine whether a page exists, potentially leaking private data. In fact, the
207 // curid and oldid request parameters would allow page titles to be enumerated even
208 // when they are not guessable. So we reset the title to Special:Badtitle before the
209 // permissions error is displayed.
210
211 // The skin mostly uses $this->context->getTitle() these days, but some extensions
212 // still use $wgTitle.
213 $badTitle = SpecialPage::getTitleFor( 'Badtitle' );
214 $this->context->setTitle( $badTitle );
215 $wgTitle = $badTitle;
216
217 throw new PermissionsError( 'read', $permErrors );
218 }
219
220 // Interwiki redirects
221 if ( $title->isExternal() ) {
222 $rdfrom = $request->getVal( 'rdfrom' );
223 if ( $rdfrom ) {
224 $url = $title->getFullURL( [ 'rdfrom' => $rdfrom ] );
225 } else {
226 $query = $request->getValues();
227 unset( $query['title'] );
228 $url = $title->getFullURL( $query );
229 }
230 // Check for a redirect loop
231 if ( !preg_match( '/^' . preg_quote( $this->config->get( 'Server' ), '/' ) . '/', $url )
232 && $title->isLocal()
233 ) {
234 // 301 so google et al report the target as the actual url.
235 $output->redirect( $url, 301 );
236 } else {
237 $this->context->setTitle( SpecialPage::getTitleFor( 'Badtitle' ) );
238 try {
239 $this->parseTitle();
240 } catch ( MalformedTitleException $ex ) {
241 throw new BadTitleError( $ex );
242 }
243 throw new BadTitleError();
244 }
245 // Handle any other redirects.
246 // Redirect loops, titleless URL, $wgUsePathInfo URLs, and URLs with a variant
247 } elseif ( !$this->tryNormaliseRedirect( $title ) ) {
248 // Prevent information leak via Special:MyPage et al (T109724)
249 if ( $title->isSpecialPage() ) {
250 $specialPage = SpecialPageFactory::getPage( $title->getDBkey() );
251 if ( $specialPage instanceof RedirectSpecialPage ) {
252 $specialPage->setContext( $this->context );
253 if ( $this->config->get( 'HideIdentifiableRedirects' )
254 && $specialPage->personallyIdentifiableTarget()
255 ) {
256 list( , $subpage ) = SpecialPageFactory::resolveAlias( $title->getDBkey() );
257 $target = $specialPage->getRedirect( $subpage );
258 // target can also be true. We let that case fall through to normal processing.
259 if ( $target instanceof Title ) {
260 $query = $specialPage->getRedirectQuery() ?: [];
261 $request = new DerivativeRequest( $this->context->getRequest(), $query );
262 $request->setRequestURL( $this->context->getRequest()->getRequestURL() );
263 $this->context->setRequest( $request );
264 // Do not varnish cache these. May vary even for anons
265 $this->context->getOutput()->lowerCdnMaxage( 0 );
266 $this->context->setTitle( $target );
267 $wgTitle = $target;
268 // Reset action type cache. (Special pages have only view)
269 $this->action = null;
270 $title = $target;
272 'wgInternalRedirectTargetUrl' => $target->getFullURL( $query ),
273 ] );
274 $output->addModules( 'mediawiki.action.view.redirect' );
275 }
276 }
277 }
278 }
279
280 // Special pages ($title may have changed since if statement above)
281 if ( NS_SPECIAL == $title->getNamespace() ) {
282 // Actions that need to be made when we have a special pages
283 SpecialPageFactory::executePath( $title, $this->context );
284 } else {
285 // ...otherwise treat it as an article view. The article
286 // may still be a wikipage redirect to another article or URL.
287 $article = $this->initializeArticle();
288 if ( is_object( $article ) ) {
289 $this->performAction( $article, $requestTitle );
290 } elseif ( is_string( $article ) ) {
291 $output->redirect( $article );
292 } else {
293 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle()"
294 . " returned neither an object nor a URL" );
295 }
296 }
297 }
298 }
299
322 private function tryNormaliseRedirect( Title $title ) {
323 $request = $this->context->getRequest();
324 $output = $this->context->getOutput();
325
326 if ( $request->getVal( 'action', 'view' ) != 'view'
327 || $request->wasPosted()
328 || ( $request->getVal( 'title' ) !== null
329 && $title->getPrefixedDBkey() == $request->getVal( 'title' ) )
330 || count( $request->getValueNames( [ 'action', 'title' ] ) )
331 || !Hooks::run( 'TestCanonicalRedirect', [ $request, $title, $output ] )
332 ) {
333 return false;
334 }
335
336 if ( $title->isSpecialPage() ) {
337 list( $name, $subpage ) = SpecialPageFactory::resolveAlias( $title->getDBkey() );
338 if ( $name ) {
340 }
341 }
342 // Redirect to canonical url, make it a 301 to allow caching
343 $targetUrl = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
344 if ( $targetUrl == $request->getFullRequestURL() ) {
345 $message = "Redirect loop detected!\n\n" .
346 "This means the wiki got confused about what page was " .
347 "requested; this sometimes happens when moving a wiki " .
348 "to a new server or changing the server configuration.\n\n";
349
350 if ( $this->config->get( 'UsePathInfo' ) ) {
351 $message .= "The wiki is trying to interpret the page " .
352 "title from the URL path portion (PATH_INFO), which " .
353 "sometimes fails depending on the web server. Try " .
354 "setting \"\$wgUsePathInfo = false;\" in your " .
355 "LocalSettings.php, or check that \$wgArticlePath " .
356 "is correct.";
357 } else {
358 $message .= "Your web server was detected as possibly not " .
359 "supporting URL path components (PATH_INFO) correctly; " .
360 "check your LocalSettings.php for a customized " .
361 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
362 "to true.";
363 }
364 throw new HttpError( 500, $message );
365 }
366 $output->setSquidMaxage( 1200 );
367 $output->redirect( $targetUrl, '301' );
368 return true;
369 }
370
377 private function initializeArticle() {
378 $title = $this->context->getTitle();
379 if ( $this->context->canUseWikiPage() ) {
380 // Try to use request context wiki page, as there
381 // is already data from db saved in per process
382 // cache there from this->getAction() call.
383 $page = $this->context->getWikiPage();
384 } else {
385 // This case should not happen, but just in case.
386 // @TODO: remove this or use an exception
388 $this->context->setWikiPage( $page );
389 wfWarn( "RequestContext::canUseWikiPage() returned false" );
390 }
391
392 // Make GUI wrapper for the WikiPage
393 $article = Article::newFromWikiPage( $page, $this->context );
394
395 // Skip some unnecessary code if the content model doesn't support redirects
396 if ( !ContentHandler::getForTitle( $title )->supportsRedirects() ) {
397 return $article;
398 }
399
400 $request = $this->context->getRequest();
401
402 // Namespace might change when using redirects
403 // Check for redirects ...
404 $action = $request->getVal( 'action', 'view' );
405 $file = ( $page instanceof WikiFilePage ) ? $page->getFile() : null;
406 if ( ( $action == 'view' || $action == 'render' ) // ... for actions that show content
407 && !$request->getVal( 'oldid' ) // ... and are not old revisions
408 && !$request->getVal( 'diff' ) // ... and not when showing diff
409 && $request->getVal( 'redirect' ) != 'no' // ... unless explicitly told not to
410 // ... and the article is not a non-redirect image page with associated file
411 && !( is_object( $file ) && $file->exists() && !$file->getRedirected() )
412 ) {
413 // Give extensions a change to ignore/handle redirects as needed
414 $ignoreRedirect = $target = false;
415
416 Hooks::run( 'InitializeArticleMaybeRedirect',
417 [ &$title, &$request, &$ignoreRedirect, &$target, &$article ] );
418 $page = $article->getPage(); // reflect any hook changes
419
420 // Follow redirects only for... redirects.
421 // If $target is set, then a hook wanted to redirect.
422 if ( !$ignoreRedirect && ( $target || $page->isRedirect() ) ) {
423 // Is the target already set by an extension?
424 $target = $target ? $target : $page->followRedirect();
425 if ( is_string( $target ) ) {
426 if ( !$this->config->get( 'DisableHardRedirects' ) ) {
427 // we'll need to redirect
428 return $target;
429 }
430 }
431 if ( is_object( $target ) ) {
432 // Rewrite environment to redirected article
433 $rpage = WikiPage::factory( $target );
434 $rpage->loadPageData();
435 if ( $rpage->exists() || ( is_object( $file ) && !$file->isLocal() ) ) {
436 $rarticle = Article::newFromWikiPage( $rpage, $this->context );
437 $rarticle->setRedirectedFrom( $title );
438
439 $article = $rarticle;
440 $this->context->setTitle( $target );
441 $this->context->setWikiPage( $article->getPage() );
442 }
443 }
444 } else {
445 // Article may have been changed by hook
446 $this->context->setTitle( $article->getTitle() );
447 $this->context->setWikiPage( $article->getPage() );
448 }
449 }
450
451 return $article;
452 }
453
460 private function performAction( Page $page, Title $requestTitle ) {
461 $request = $this->context->getRequest();
462 $output = $this->context->getOutput();
463 $title = $this->context->getTitle();
464 $user = $this->context->getUser();
465
466 if ( !Hooks::run( 'MediaWikiPerformAction',
467 [ $output, $page, $title, $user, $request, $this ] )
468 ) {
469 return;
470 }
471
472 $act = $this->getAction();
473 $action = Action::factory( $act, $page, $this->context );
474
475 if ( $action instanceof Action ) {
476 // Narrow DB query expectations for this HTTP request
477 $trxLimits = $this->config->get( 'TrxProfilerLimits' );
478 $trxProfiler = Profiler::instance()->getTransactionProfiler();
479 if ( $request->wasPosted() && !$action->doesWrites() ) {
480 $trxProfiler->setExpectations( $trxLimits['POST-nonwrite'], __METHOD__ );
481 $request->markAsSafeRequest();
482 }
483
484 # Let CDN cache things if we can purge them.
485 if ( $this->config->get( 'UseSquid' ) &&
486 in_array(
487 // Use PROTO_INTERNAL because that's what getCdnUrls() uses
488 wfExpandUrl( $request->getRequestURL(), PROTO_INTERNAL ),
489 $requestTitle->getCdnUrls()
490 )
491 ) {
492 $output->setCdnMaxage( $this->config->get( 'SquidMaxage' ) );
493 }
494
495 $action->show();
496 return;
497 }
498
499 if ( Hooks::run( 'UnknownAction', [ $request->getVal( 'action', 'view' ), $page ] ) ) {
500 $output->setStatusCode( 404 );
501 $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
502 }
503 }
504
508 public function run() {
509 try {
510 $this->setDBProfilingAgent();
511 try {
512 $this->main();
513 } catch ( ErrorPageError $e ) {
514 // Bug 62091: while exceptions are convenient to bubble up GUI errors,
515 // they are not internal application faults. As with normal requests, this
516 // should commit, print the output, do deferred updates, jobs, and profiling.
517 $this->doPreOutputCommit();
518 $e->report(); // display the GUI error
519 }
520 } catch ( Exception $e ) {
522 $action = $context->getRequest()->getVal( 'action', 'view' );
523 if (
524 $e instanceof DBConnectionError &&
525 $context->hasTitle() &&
526 $context->getTitle()->canExist() &&
527 in_array( $action, [ 'view', 'history' ], true ) &&
529 ) {
530 // Try to use any (even stale) file during outages...
531 $cache = new HTMLFileCache( $context->getTitle(), 'view' );
532 if ( $cache->isCached() ) {
533 $cache->loadFromFileCache( $context, HTMLFileCache::MODE_OUTAGE );
535 exit;
536 }
537
538 }
539
540 MWExceptionHandler::handleException( $e );
541 }
542
543 $this->doPostOutputShutdown( 'normal' );
544 }
545
546 private function setDBProfilingAgent() {
548 // Add a comment for easy SHOW PROCESSLIST interpretation
549 $name = $this->context->getUser()->getName();
550 $services->getDBLoadBalancerFactory()->setAgentName(
551 mb_strlen( $name ) > 15 ? mb_substr( $name, 0, 15 ) . '...' : $name
552 );
553 }
554
560 public function doPreOutputCommit( callable $postCommitWork = null ) {
561 self::preOutputCommit( $this->context, $postCommitWork );
562 }
563
572 public static function preOutputCommit(
573 IContextSource $context, callable $postCommitWork = null
574 ) {
575 // Either all DBs should commit or none
576 ignore_user_abort( true );
577
578 $config = $context->getConfig();
580 $output = $context->getOutput();
581 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
582
583 // Commit all changes
584 $lbFactory->commitMasterChanges(
585 __METHOD__,
586 // Abort if any transaction was too big
587 [ 'maxWriteDuration' => $config->get( 'MaxUserDBWriteDuration' ) ]
588 );
589 wfDebug( __METHOD__ . ': primary transaction round committed' );
590
591 // Run updates that need to block the user or affect output (this is the last chance)
592 DeferredUpdates::doUpdates( 'enqueue', DeferredUpdates::PRESEND );
593 wfDebug( __METHOD__ . ': pre-send deferred updates completed' );
594
595 // Decide when clients block on ChronologyProtector DB position writes
596 $urlDomainDistance = (
597 $request->wasPosted() &&
598 $output->getRedirect() &&
599 $lbFactory->hasOrMadeRecentMasterChanges( INF )
600 ) ? self::getUrlDomainDistance( $output->getRedirect(), $context ) : false;
601
602 if ( $urlDomainDistance === 'local' || $urlDomainDistance === 'remote' ) {
603 // OutputPage::output() will be fast; $postCommitWork will not be useful for
604 // masking the latency of syncing DB positions accross all datacenters synchronously.
605 // Instead, make use of the RTT time of the client follow redirects.
606 $flags = $lbFactory::SHUTDOWN_CHRONPROT_ASYNC;
607 $cpPosTime = microtime( true );
608 // Client's next request should see 1+ positions with this DBMasterPos::asOf() time
609 if ( $urlDomainDistance === 'local' ) {
610 // Client will stay on this domain, so set an unobtrusive cookie
611 $expires = time() + ChronologyProtector::POSITION_TTL;
612 $options = [ 'prefix' => '' ];
613 $request->response()->setCookie( 'cpPosTime', $cpPosTime, $expires, $options );
614 } else {
615 // Cookies may not work across wiki domains, so use a URL parameter
616 $safeUrl = $lbFactory->appendPreShutdownTimeAsQuery(
617 $output->getRedirect(),
618 $cpPosTime
619 );
620 $output->redirect( $safeUrl );
621 }
622 } else {
623 // OutputPage::output() is fairly slow; run it in $postCommitWork to mask
624 // the latency of syncing DB positions accross all datacenters synchronously
625 $flags = $lbFactory::SHUTDOWN_CHRONPROT_SYNC;
626 if ( $lbFactory->hasOrMadeRecentMasterChanges( INF ) ) {
627 $cpPosTime = microtime( true );
628 // Set a cookie in case the DB position store cannot sync accross datacenters.
629 // This will at least cover the common case of the user staying on the domain.
630 $expires = time() + ChronologyProtector::POSITION_TTL;
631 $options = [ 'prefix' => '' ];
632 $request->response()->setCookie( 'cpPosTime', $cpPosTime, $expires, $options );
633 }
634 }
635 // Record ChronologyProtector positions for DBs affected in this request at this point
636 $lbFactory->shutdown( $flags, $postCommitWork );
637 wfDebug( __METHOD__ . ': LBFactory shutdown completed' );
638
639 // Set a cookie to tell all CDN edge nodes to "stick" the user to the DC that handles this
640 // POST request (e.g. the "master" data center). Also have the user briefly bypass CDN so
641 // ChronologyProtector works for cacheable URLs.
642 if ( $request->wasPosted() && $lbFactory->hasOrMadeRecentMasterChanges() ) {
643 $expires = time() + $config->get( 'DataCenterUpdateStickTTL' );
644 $options = [ 'prefix' => '' ];
645 $request->response()->setCookie( 'UseDC', 'master', $expires, $options );
646 $request->response()->setCookie( 'UseCDNCache', 'false', $expires, $options );
647 }
648
649 // Avoid letting a few seconds of replica DB lag cause a month of stale data. This logic is
650 // also intimately related to the value of $wgCdnReboundPurgeDelay.
651 if ( $lbFactory->laggedReplicaUsed() ) {
652 $maxAge = $config->get( 'CdnMaxageLagged' );
653 $output->lowerCdnMaxage( $maxAge );
654 $request->response()->header( "X-Database-Lagged: true" );
655 wfDebugLog( 'replication', "Lagged DB used; CDN cache TTL limited to $maxAge seconds" );
656 }
657
658 // Avoid long-term cache pollution due to message cache rebuild timeouts (T133069)
659 if ( MessageCache::singleton()->isDisabled() ) {
660 $maxAge = $config->get( 'CdnMaxageSubstitute' );
661 $output->lowerCdnMaxage( $maxAge );
662 $request->response()->header( "X-Response-Substitute: true" );
663 }
664 }
665
671 private static function getUrlDomainDistance( $url, IContextSource $context ) {
672 static $relevantKeys = [ 'host' => true, 'port' => true ];
673
674 $infoCandidate = wfParseUrl( $url );
675 if ( $infoCandidate === false ) {
676 return false;
677 }
678
679 $infoCandidate = array_intersect_key( $infoCandidate, $relevantKeys );
680 $clusterHosts = array_merge(
681 // Local wiki host (the most common case)
682 [ $context->getConfig()->get( 'CanonicalServer' ) ],
683 // Any local/remote wiki virtual hosts for this wiki farm
684 $context->getConfig()->get( 'LocalVirtualHosts' )
685 );
686
687 foreach ( $clusterHosts as $i => $clusterHost ) {
688 $parseUrl = wfParseUrl( $clusterHost );
689 if ( !$parseUrl ) {
690 continue;
691 }
692 $infoHost = array_intersect_key( $parseUrl, $relevantKeys );
693 if ( $infoCandidate === $infoHost ) {
694 return ( $i === 0 ) ? 'local' : 'remote';
695 }
696 }
697
698 return false;
699 }
700
711 public function doPostOutputShutdown( $mode = 'normal' ) {
712 $timing = $this->context->getTiming();
713 $timing->mark( 'requestShutdown' );
714
715 // Show visible profiling data if enabled (which cannot be post-send)
716 Profiler::instance()->logDataPageOutputOnly();
717
718 $callback = function () use ( $mode ) {
719 try {
720 $this->restInPeace( $mode );
721 } catch ( Exception $e ) {
722 MWExceptionHandler::handleException( $e );
723 }
724 };
725
726 // Defer everything else...
727 if ( function_exists( 'register_postsend_function' ) ) {
728 // https://github.com/facebook/hhvm/issues/1230
729 register_postsend_function( $callback );
730 } else {
731 if ( function_exists( 'fastcgi_finish_request' ) ) {
732 fastcgi_finish_request();
733 } else {
734 // Either all DB and deferred updates should happen or none.
735 // The latter should not be cancelled due to client disconnect.
736 ignore_user_abort( true );
737 }
738
739 $callback();
740 }
741 }
742
743 private function main() {
745
746 $output = $this->context->getOutput();
747 $request = $this->context->getRequest();
748
749 // Send Ajax requests to the Ajax dispatcher.
750 if ( $this->config->get( 'UseAjax' ) && $request->getVal( 'action' ) === 'ajax' ) {
751 // Set a dummy title, because $wgTitle == null might break things
752 $title = Title::makeTitle( NS_SPECIAL, 'Badtitle/performing an AJAX call in '
753 . __METHOD__
754 );
755 $this->context->setTitle( $title );
757
758 $dispatcher = new AjaxDispatcher( $this->config );
759 $dispatcher->performAction( $this->context->getUser() );
760
761 return;
762 }
763
764 // Get title from request parameters,
765 // is set on the fly by parseTitle the first time.
766 $title = $this->getTitle();
767 $action = $this->getAction();
769
770 // Set DB query expectations for this HTTP request
771 $trxLimits = $this->config->get( 'TrxProfilerLimits' );
772 $trxProfiler = Profiler::instance()->getTransactionProfiler();
773 $trxProfiler->setLogger( LoggerFactory::getInstance( 'DBPerformance' ) );
774 if ( $request->hasSafeMethod() ) {
775 $trxProfiler->setExpectations( $trxLimits['GET'], __METHOD__ );
776 } else {
777 $trxProfiler->setExpectations( $trxLimits['POST'], __METHOD__ );
778 }
779
780 // If the user has forceHTTPS set to true, or if the user
781 // is in a group requiring HTTPS, or if they have the HTTPS
782 // preference set, redirect them to HTTPS.
783 // Note: Do this after $wgTitle is setup, otherwise the hooks run from
784 // isLoggedIn() will do all sorts of weird stuff.
785 if (
786 $request->getProtocol() == 'http' &&
787 // switch to HTTPS only when supported by the server
788 preg_match( '#^https://#', wfExpandUrl( $request->getRequestURL(), PROTO_HTTPS ) ) &&
789 (
790 $request->getSession()->shouldForceHTTPS() ||
791 // Check the cookie manually, for paranoia
792 $request->getCookie( 'forceHTTPS', '' ) ||
793 // check for prefixed version that was used for a time in older MW versions
794 $request->getCookie( 'forceHTTPS' ) ||
795 // Avoid checking the user and groups unless it's enabled.
796 (
797 $this->context->getUser()->isLoggedIn()
798 && $this->context->getUser()->requiresHTTPS()
799 )
800 )
801 ) {
802 $oldUrl = $request->getFullRequestURL();
803 $redirUrl = preg_replace( '#^http://#', 'https://', $oldUrl );
804
805 // ATTENTION: This hook is likely to be removed soon due to overall design of the system.
806 if ( Hooks::run( 'BeforeHttpsRedirect', [ $this->context, &$redirUrl ] ) ) {
807
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
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
877 public function restInPeace( $mode = 'fast' ) {
878 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
879 // Assure deferred updates are not in the main transaction
880 $lbFactory->commitMasterChanges( __METHOD__ );
881
882 // Loosen DB query expectations since the HTTP client is unblocked
883 $trxProfiler = Profiler::instance()->getTransactionProfiler();
884 $trxProfiler->resetExpectations();
885 $trxProfiler->setExpectations(
886 $this->config->get( 'TrxProfilerLimits' )['PostSend'],
887 __METHOD__
888 );
889
890 // Important: this must be the last deferred update added (T100085, T154425)
891 DeferredUpdates::addCallableUpdate( [ JobQueueGroup::class, 'pushLazyJobs' ] );
892
893 // Do any deferred jobs
894 DeferredUpdates::doUpdates( 'enqueue' );
895
896 // Now that everything specific to this request is done,
897 // try to occasionally run jobs (if enabled) from the queues
898 if ( $mode === 'normal' ) {
899 $this->triggerJobs();
900 }
901
902 // Log profiling data, e.g. in the database or UDP
904
905 // Commit and close up!
906 $lbFactory->commitMasterChanges( __METHOD__ );
908
909 wfDebug( "Request ended normally\n" );
910 }
911
917 public function triggerJobs() {
918 $jobRunRate = $this->config->get( 'JobRunRate' );
919 if ( $this->getTitle()->isSpecial( 'RunJobs' ) ) {
920 return; // recursion guard
921 } elseif ( $jobRunRate <= 0 || wfReadOnly() ) {
922 return;
923 }
924
925 if ( $jobRunRate < 1 ) {
926 $max = mt_getrandmax();
927 if ( mt_rand( 0, $max ) > $max * $jobRunRate ) {
928 return; // the higher the job run rate, the less likely we return here
929 }
930 $n = 1;
931 } else {
932 $n = intval( $jobRunRate );
933 }
934
935 $runJobsLogger = LoggerFactory::getInstance( 'runJobs' );
936
937 // Fall back to running the job(s) while the user waits if needed
938 if ( !$this->config->get( 'RunJobsAsync' ) ) {
939 $runner = new JobRunner( $runJobsLogger );
940 $runner->run( [ 'maxJobs' => $n ] );
941 return;
942 }
943
944 // Do not send request if there are probably no jobs
945 try {
946 $group = JobQueueGroup::singleton();
947 if ( !$group->queuesHaveJobs( JobQueueGroup::TYPE_DEFAULT ) ) {
948 return;
949 }
950 } catch ( JobQueueError $e ) {
951 MWExceptionHandler::logException( $e );
952 return; // do not make the site unavailable
953 }
954
955 $query = [ 'title' => 'Special:RunJobs',
956 'tasks' => 'jobs', 'maxjobs' => $n, 'sigexpiry' => time() + 5 ];
958 $query, $this->config->get( 'SecretKey' ) );
959
960 $errno = $errstr = null;
961 $info = wfParseUrl( $this->config->get( 'CanonicalServer' ) );
962 $host = $info ? $info['host'] : null;
963 $port = 80;
964 if ( isset( $info['scheme'] ) && $info['scheme'] == 'https' ) {
965 $host = "tls://" . $host;
966 $port = 443;
967 }
968 if ( isset( $info['port'] ) ) {
969 $port = $info['port'];
970 }
971
972 MediaWiki\suppressWarnings();
973 $sock = $host ? fsockopen(
974 $host,
975 $port,
976 $errno,
977 $errstr,
978 // If it takes more than 100ms to connect to ourselves there is a problem...
979 0.100
980 ) : false;
981 MediaWiki\restoreWarnings();
982
983 $invokedWithSuccess = true;
984 if ( $sock ) {
986 $url = $special->getPageTitle()->getCanonicalURL( $query );
987 $req = (
988 "POST $url HTTP/1.1\r\n" .
989 "Host: {$info['host']}\r\n" .
990 "Connection: Close\r\n" .
991 "Content-Length: 0\r\n\r\n"
992 );
993
994 $runJobsLogger->info( "Running $n job(s) via '$url'" );
995 // Send a cron API request to be performed in the background.
996 // Give up if this takes too long to send (which should be rare).
997 stream_set_timeout( $sock, 2 );
998 $bytes = fwrite( $sock, $req );
999 if ( $bytes !== strlen( $req ) ) {
1000 $invokedWithSuccess = false;
1001 $runJobsLogger->error( "Failed to start cron API (socket write error)" );
1002 } else {
1003 // Do not wait for the response (the script should handle client aborts).
1004 // Make sure that we don't close before that script reaches ignore_user_abort().
1005 $start = microtime( true );
1006 $status = fgets( $sock );
1007 $sec = microtime( true ) - $start;
1008 if ( !preg_match( '#^HTTP/\d\.\d 202 #', $status ) ) {
1009 $invokedWithSuccess = false;
1010 $runJobsLogger->error( "Failed to start cron API: received '$status' ($sec)" );
1011 }
1012 }
1013 fclose( $sock );
1014 } else {
1015 $invokedWithSuccess = false;
1016 $runJobsLogger->error( "Failed to start cron API (socket error $errno): $errstr" );
1017 }
1018
1019 // Fall back to running the job(s) while the user waits if needed
1020 if ( !$invokedWithSuccess ) {
1021 $runJobsLogger->warning( "Jobs switched to blocking; Special:RunJobs disabled" );
1022
1023 $runner = new JobRunner( $runJobsLogger );
1024 $runner->run( [ 'maxJobs' => $n ] );
1025 }
1026 }
1027}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
wfWarn( $msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
wfReadOnly()
Check whether the wiki is in read-only mode.
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
wfLogProfilingData()
if(! $wgRequest->checkUrlExtension()) if(isset($_SERVER[ 'PATH_INFO']) &&$_SERVER[ 'PATH_INFO'] !='') if(! $wgEnableAPI) $wgTitle
Definition api.php:68
Actions are things which can be done to pages (edit, delete, rollback, etc).
Definition Action.php:37
static factory( $action, Page $page, IContextSource $context=null)
Get an appropriate Action subclass for the given action.
Definition Action.php:95
static getActionName(IContextSource $context)
Get the action that will be executed, not necessarily the one passed passed through the "action" requ...
Definition Action.php:122
Object-Oriented Ajax functions.
static newFromWikiPage(WikiPage $page, IContextSource $context)
Create an Article object of the appropriate class for the given page.
Definition Article.php:144
Show an error page on a badtitle.
static getForTitle(Title $title)
Returns the appropriate ContentHandler singleton for the given title.
getRequest()
Get the WebRequest object.
getOutput()
Get the OutputPage object.
Similar to FauxRequest, but only fakes URL parameters and method (POST or GET) and use the base reque...
An error page which can definitely be safely rendered using the OutputPage.
Page view caching in the file system.
static useFileCache(IContextSource $context, $mode=self::MODE_NORMAL)
Check if pages can be cached for this request/user.
Show an error that looks like an HTTP server error.
Definition HttpError.php:30
static singleton( $wiki=false)
Job queue runner utility methods.
Definition JobRunner.php:37
static getHTML( $e)
If $wgShowExceptionDetails is true, return a HTML message with a backtrace to the error,...
MediaWiki exception.
MalformedTitleException is thrown when a TitleParser is unable to parse a title string.
PSR-3 logger instance factory.
MediaWikiServices is the service locator for the application scope of MediaWiki.
static getInstance()
Returns the global default instance of the top level service locator.
parseTitle()
Parse the request to get the Title object.
Definition MediaWiki.php:63
static getUrlDomainDistance( $url, IContextSource $context)
doPostOutputShutdown( $mode='normal')
This function does work that can be done after the user gets the HTTP response so they don't block on...
initializeArticle()
Initialize the main Article object for "standard" actions (view, etc) Create an Article object for th...
Config $config
Definition MediaWiki.php:38
run()
Run the current MediaWiki instance; index.php just calls this.
getTitle()
Get the Title object that we'll be acting on, as specified in the WebRequest.
__construct(IContextSource $context=null)
Definition MediaWiki.php:48
getAction()
Returns the name of the action that will be executed.
tryNormaliseRedirect(Title $title)
Handle redirects for uncanonical title requests.
String $action
Cache what action this request is.
Definition MediaWiki.php:43
static preOutputCommit(IContextSource $context, callable $postCommitWork=null)
This function commits all DB changes as needed before the user can receive a response (in case commit...
IContextSource $context
Definition MediaWiki.php:33
triggerJobs()
Potentially open a socket and sent an HTTP request back to the server to run a specified number of jo...
performRequest()
Performs the request.
setDBProfilingAgent()
doPreOutputCommit(callable $postCommitWork=null)
performAction(Page $page, Title $requestTitle)
Perform one of the "standard" actions.
restInPeace( $mode='fast')
Ends this task peacefully.
static singleton()
Get the signleton instance of this class.
addJsConfigVars( $keys, $value=null)
Add one or more variables to be set in mw.config in JavaScript.
addModules( $modules)
Show an error when a user tries to do something they do not have the necessary permissions for.
static instance()
Singleton.
Definition Profiler.php:61
Shortcut to construct a special page alias.
static getMain()
Static methods.
static newFromId( $id, $flags=0)
Load a page revision from a given revision ID number.
Definition Revision.php:110
static executePath(Title &$title, IContextSource &$context, $including=false, LinkRenderer $linkRenderer=null)
Execute a special page path.
static getPage( $name)
Find the object with a given name and return it (or NULL)
static resolveAlias( $alias)
Given a special page name with a possible subpage, return an array where the first element is the spe...
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
static getQuerySignature(array $query, $secretKey)
Represents a title within MediaWiki.
Definition Title.php:36
getCdnUrls()
Get a list of URLs to purge from the CDN cache when this page changes.
Definition Title.php:3596
Special handling for file pages.
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition WikiPage.php:115
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the local content language as $wgContLang
Definition design.txt:57
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
$lbFactory
while(( $__line=Maintenance::readconsole()) !==false) print
Definition eval.php:64
const PROTO_HTTPS
Definition Defines.php:224
const NS_FILE
Definition Defines.php:62
const PROTO_CURRENT
Definition Defines.php:226
const NS_MAIN
Definition Defines.php:56
const PROTO_INTERNAL
Definition Defines.php:228
const NS_SPECIAL
Definition Defines.php:45
const NS_MEDIA
Definition Defines.php:44
this hook is for auditing only $req
Definition hooks.txt:1010
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition hooks.txt:1049
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context the output can only depend on parameters provided to this hook not on global state indicating whether full HTML should be generated If generation of HTML may be but other information should still be present in the ParserOutput object & $output
Definition hooks.txt:1102
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition hooks.txt:249
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:986
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
Definition hooks.txt:1096
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition hooks.txt:2710
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition hooks.txt:1950
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition hooks.txt:2685
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title after the basic globals have been set but before ordinary actions take place or wrap services the preferred way to define a new service is the $wgServiceWiringFiles array $services
Definition hooks.txt:2207
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition hooks.txt:1949
this hook is for auditing only RecentChangesLinked and Watchlist $special
Definition hooks.txt:1018
Using a hook running we can avoid having all this option specific stuff in our mainline code Using the function array $article
Definition hooks.txt:78
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:304
namespace are movable Hooks may change this value to override the return value of MWNamespace::isMovable(). 'NewDifferenceEngine' do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached $page
Definition hooks.txt:2534
null for the local wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition hooks.txt:1595
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition hooks.txt:1734
returning false will NOT prevent logging $e
Definition hooks.txt:2110
if( $limit) $timestamp
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
Interface for configuration instances.
Definition Config.php:28
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".
Interface for objects which can provide a MediaWiki context on request.
const SHUTDOWN_NO_CHRONPROT
Interface for type hinting (accepts WikiPage, Article, ImagePage, CategoryPage)
Definition Page.php:24
$context
Definition load.php:50
$cache
Definition mcc.php:33
$buffer
A helper class for throttling authentication attempts.