MediaWiki  master
MediaWiki.php
Go to the documentation of this file.
1 <?php
23 use Liuggio\StatsdClient\Sender\SocketSender;
24 use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
27 use Psr\Log\LoggerInterface;
28 use Wikimedia\AtEase;
32 
36 class MediaWiki {
37  use ProtectedHookAccessorTrait;
38 
40  private $context;
42  private $config;
43 
45  private $action;
48 
50  private const DEFER_FASTCGI_FINISH_REQUEST = 1;
52  private const DEFER_SET_LENGTH_AND_FLUSH = 2;
53 
57  public function __construct( IContextSource $context = null ) {
58  $this->context = $context ?: RequestContext::getMain();
59  $this->config = $this->context->getConfig();
60  if ( function_exists( 'fastcgi_finish_request' ) ) {
61  $this->postSendStrategy = self::DEFER_FASTCGI_FINISH_REQUEST;
62  } else {
63  $this->postSendStrategy = self::DEFER_SET_LENGTH_AND_FLUSH;
64  }
65  }
66 
73  private function parseTitle() {
74  $request = $this->context->getRequest();
75  $curid = $request->getInt( 'curid' );
76  $title = $request->getVal( 'title' );
77  $action = $request->getVal( 'action' );
78 
79  if ( $request->getCheck( 'search' ) ) {
80  // Compatibility with old search URLs which didn't use Special:Search
81  // Just check for presence here, so blank requests still
82  // show the search page when using ugly URLs (T10054).
83  $ret = SpecialPage::getTitleFor( 'Search' );
84  } elseif ( $curid ) {
85  // URLs like this are generated by RC, because rc_title isn't always accurate
86  $ret = Title::newFromID( $curid );
87  } else {
88  $ret = Title::newFromURL( $title );
89  // Alias NS_MEDIA page URLs to NS_FILE...we only use NS_MEDIA
90  // in wikitext links to tell Parser to make a direct file link
91  if ( $ret !== null && $ret->getNamespace() === NS_MEDIA ) {
92  $ret = Title::makeTitle( NS_FILE, $ret->getDBkey() );
93  }
94  $contLang = MediaWikiServices::getInstance()->getContentLanguage();
95  // Check variant links so that interwiki links don't have to worry
96  // about the possible different language variants
97  if (
98  $contLang->hasVariants() && $ret !== null && $ret->getArticleID() == 0
99  ) {
100  $contLang->findVariantLink( $title, $ret );
101  }
102  }
103 
104  // If title is not provided, always allow oldid and diff to set the title.
105  // If title is provided, allow oldid and diff to override the title, unless
106  // we are talking about a special page which might use these parameters for
107  // other purposes.
108  if ( $ret === null || !$ret->isSpecialPage() ) {
109  // We can have urls with just ?diff=,?oldid= or even just ?diff=
110  $oldid = $request->getInt( 'oldid' );
111  $oldid = $oldid ?: $request->getInt( 'diff' );
112  // Allow oldid to override a changed or missing title
113  if ( $oldid ) {
114  $revRecord = MediaWikiServices::getInstance()
115  ->getRevisionLookup()
116  ->getRevisionById( $oldid );
117  if ( $revRecord ) {
119  $revRecord->getPageAsLinkTarget()
120  );
121  }
122  }
123  }
124 
125  // Use the main page as default title if nothing else has been provided
126  if ( $ret === null
127  && strval( $title ) === ''
128  && !$request->getCheck( 'curid' )
129  && $action !== 'delete'
130  ) {
131  $ret = Title::newMainPage();
132  }
133 
134  if ( $ret === null || ( $ret->getDBkey() == '' && !$ret->isExternal() ) ) {
135  // If we get here, we definitely don't have a valid title; throw an exception.
136  // Try to get detailed invalid title exception first, fall back to MalformedTitleException.
138  throw new MalformedTitleException( 'badtitletext', $title );
139  }
140 
141  return $ret;
142  }
143 
148  public function getTitle() {
149  if ( !$this->context->hasTitle() ) {
150  try {
151  $this->context->setTitle( $this->parseTitle() );
152  } catch ( MalformedTitleException $ex ) {
153  $this->context->setTitle( SpecialPage::getTitleFor( 'Badtitle' ) );
154  }
155  }
156  return $this->context->getTitle();
157  }
158 
164  public function getAction() : string {
165  if ( $this->action === null ) {
166  $this->action = Action::getActionName( $this->context );
167  }
168 
169  return $this->action;
170  }
171 
184  private function performRequest() {
185  global $wgTitle;
186 
187  $request = $this->context->getRequest();
188  $requestTitle = $title = $this->context->getTitle();
189  $output = $this->context->getOutput();
190  $user = $this->context->getUser();
191 
192  if ( $request->getVal( 'printable' ) === 'yes' ) {
193  $output->setPrintable();
194  }
195 
196  $this->getHookRunner()->onBeforeInitialize( $title, null, $output, $user, $request, $this );
197 
198  // Invalid titles. T23776: The interwikis must redirect even if the page name is empty.
199  if ( $title === null || ( $title->getDBkey() == '' && !$title->isExternal() )
200  || $title->isSpecial( 'Badtitle' )
201  ) {
202  $this->context->setTitle( SpecialPage::getTitleFor( 'Badtitle' ) );
203  try {
204  $this->parseTitle();
205  } catch ( MalformedTitleException $ex ) {
206  throw new BadTitleError( $ex );
207  }
208  throw new BadTitleError();
209  }
210 
211  // Check user's permissions to read this page.
212  // We have to check here to catch special pages etc.
213  // We will check again in Article::view().
214  $permErrors = $title->isSpecial( 'RunJobs' )
215  ? [] // relies on HMAC key signature alone
216  : MediaWikiServices::getInstance()->getPermissionManager()
217  ->getPermissionErrors( 'read', $user, $title );
218  if ( count( $permErrors ) ) {
219  // T34276: allowing the skin to generate output with $wgTitle or
220  // $this->context->title set to the input title would allow anonymous users to
221  // determine whether a page exists, potentially leaking private data. In fact, the
222  // curid and oldid request parameters would allow page titles to be enumerated even
223  // when they are not guessable. So we reset the title to Special:Badtitle before the
224  // permissions error is displayed.
225 
226  // The skin mostly uses $this->context->getTitle() these days, but some extensions
227  // still use $wgTitle.
228  $badTitle = SpecialPage::getTitleFor( 'Badtitle' );
229  $this->context->setTitle( $badTitle );
230  $wgTitle = $badTitle;
231 
232  throw new PermissionsError( 'read', $permErrors );
233  }
234 
235  // Interwiki redirects
236  if ( $title->isExternal() ) {
237  $rdfrom = $request->getVal( 'rdfrom' );
238  if ( $rdfrom ) {
239  $url = $title->getFullURL( [ 'rdfrom' => $rdfrom ] );
240  } else {
241  $query = $request->getValues();
242  unset( $query['title'] );
243  $url = $title->getFullURL( $query );
244  }
245  // Check for a redirect loop
246  if ( !preg_match( '/^' . preg_quote( $this->config->get( 'Server' ), '/' ) . '/', $url )
247  && $title->isLocal()
248  ) {
249  // 301 so google et al report the target as the actual url.
250  $output->redirect( $url, 301 );
251  } else {
252  $this->context->setTitle( SpecialPage::getTitleFor( 'Badtitle' ) );
253  try {
254  $this->parseTitle();
255  } catch ( MalformedTitleException $ex ) {
256  throw new BadTitleError( $ex );
257  }
258  throw new BadTitleError();
259  }
260  // Handle any other redirects.
261  // Redirect loops, titleless URL, $wgUsePathInfo URLs, and URLs with a variant
262  } elseif ( !$this->tryNormaliseRedirect( $title ) ) {
263  // Prevent information leak via Special:MyPage et al (T109724)
264  $spFactory = MediaWikiServices::getInstance()->getSpecialPageFactory();
265  if ( $title->isSpecialPage() ) {
266  $specialPage = $spFactory->getPage( $title->getDBkey() );
267  if ( $specialPage instanceof RedirectSpecialPage ) {
268  $specialPage->setContext( $this->context );
269  if ( $this->config->get( 'HideIdentifiableRedirects' )
270  && $specialPage->personallyIdentifiableTarget()
271  ) {
272  list( , $subpage ) = $spFactory->resolveAlias( $title->getDBkey() );
273  $target = $specialPage->getRedirect( $subpage );
274  // Target can also be true. We let that case fall through to normal processing.
275  if ( $target instanceof Title ) {
276  if ( $target->isExternal() ) {
277  // Handle interwiki redirects
278  $target = SpecialPage::getTitleFor(
279  'GoToInterwiki',
280  'force/' . $target->getPrefixedDBkey()
281  );
282  }
283 
284  $query = $specialPage->getRedirectQuery( $subpage ) ?: [];
285  $request = new DerivativeRequest( $this->context->getRequest(), $query );
286  $request->setRequestURL( $this->context->getRequest()->getRequestURL() );
287  $this->context->setRequest( $request );
288  // Do not varnish cache these. May vary even for anons
289  $this->context->getOutput()->lowerCdnMaxage( 0 );
290  $this->context->setTitle( $target );
291  $wgTitle = $target;
292  // Reset action type cache. (Special pages have only view)
293  $this->action = null;
294  $title = $target;
295  $output->addJsConfigVars( [
296  'wgInternalRedirectTargetUrl' => $target->getLinkURL( $query ),
297  ] );
298  $output->addModules( 'mediawiki.action.view.redirect' );
299  }
300  }
301  }
302  }
303 
304  // Special pages ($title may have changed since if statement above)
305  if ( $title->isSpecialPage() ) {
306  // Actions that need to be made when we have a special pages
307  $spFactory->executePath( $title, $this->context );
308  } else {
309  // ...otherwise treat it as an article view. The article
310  // may still be a wikipage redirect to another article or URL.
311  $article = $this->initializeArticle();
312  if ( is_object( $article ) ) {
313  $this->performAction( $article, $requestTitle );
314  } elseif ( is_string( $article ) ) {
315  $output->redirect( $article );
316  } else {
317  throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle()"
318  . " returned neither an object nor a URL" );
319  }
320  }
321  $output->considerCacheSettingsFinal();
322  }
323  }
324 
347  private function tryNormaliseRedirect( Title $title ) {
348  $request = $this->context->getRequest();
349  $output = $this->context->getOutput();
350 
351  if ( $request->getVal( 'action', 'view' ) != 'view'
352  || $request->wasPosted()
353  || ( $request->getCheck( 'title' )
354  && $title->getPrefixedDBkey() == $request->getVal( 'title' ) )
355  || count( $request->getValueNames( [ 'action', 'title' ] ) )
356  || !$this->getHookRunner()->onTestCanonicalRedirect( $request, $title, $output )
357  ) {
358  return false;
359  }
360 
361  if ( $this->config->get( 'MainPageIsDomainRoot' ) && $request->getRequestURL() === '/' ) {
362  return false;
363  }
364 
365  if ( $title->isSpecialPage() ) {
366  list( $name, $subpage ) = MediaWikiServices::getInstance()->getSpecialPageFactory()->
367  resolveAlias( $title->getDBkey() );
368  if ( $name ) {
369  $title = SpecialPage::getTitleFor( $name, $subpage );
370  }
371  }
372  // Redirect to canonical url, make it a 301 to allow caching
373  $targetUrl = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
374  if ( $targetUrl == $request->getFullRequestURL() ) {
375  $message = "Redirect loop detected!\n\n" .
376  "This means the wiki got confused about what page was " .
377  "requested; this sometimes happens when moving a wiki " .
378  "to a new server or changing the server configuration.\n\n";
379 
380  if ( $this->config->get( 'UsePathInfo' ) ) {
381  $message .= "The wiki is trying to interpret the page " .
382  "title from the URL path portion (PATH_INFO), which " .
383  "sometimes fails depending on the web server. Try " .
384  "setting \"\$wgUsePathInfo = false;\" in your " .
385  "LocalSettings.php, or check that \$wgArticlePath " .
386  "is correct.";
387  } else {
388  $message .= "Your web server was detected as possibly not " .
389  "supporting URL path components (PATH_INFO) correctly; " .
390  "check your LocalSettings.php for a customized " .
391  "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
392  "to true.";
393  }
394  throw new HttpError( 500, $message );
395  }
396  $output->setCdnMaxage( 1200 );
397  $output->redirect( $targetUrl, '301' );
398  return true;
399  }
400 
407  private function initializeArticle() {
408  $title = $this->context->getTitle();
409  if ( $this->context->canUseWikiPage() ) {
410  // Try to use request context wiki page, as there
411  // is already data from db saved in per process
412  // cache there from this->getAction() call.
413  $page = $this->context->getWikiPage();
414  } else {
415  // This case should not happen, but just in case.
416  // @TODO: remove this or use an exception
417  $page = WikiPage::factory( $title );
418  $this->context->setWikiPage( $page );
419  wfWarn( "RequestContext::canUseWikiPage() returned false" );
420  }
421 
422  // Make GUI wrapper for the WikiPage
423  $article = Article::newFromWikiPage( $page, $this->context );
424 
425  // Skip some unnecessary code if the content model doesn't support redirects
427  ->getContentHandlerFactory()
428  ->getContentHandler( $title->getContentModel() )
429  ->supportsRedirects()
430  ) {
431  return $article;
432  }
433 
434  $request = $this->context->getRequest();
435 
436  // Namespace might change when using redirects
437  // Check for redirects ...
438  $action = $request->getVal( 'action', 'view' );
439  $file = ( $page instanceof WikiFilePage ) ? $page->getFile() : null;
440  if ( ( $action == 'view' || $action == 'render' ) // ... for actions that show content
441  && !$request->getVal( 'oldid' ) // ... and are not old revisions
442  && !$request->getVal( 'diff' ) // ... and not when showing diff
443  && $request->getVal( 'redirect' ) != 'no' // ... unless explicitly told not to
444  // ... and the article is not a non-redirect image page with associated file
445  && !( is_object( $file ) && $file->exists() && !$file->getRedirected() )
446  ) {
447  // Give extensions a change to ignore/handle redirects as needed
448  $ignoreRedirect = $target = false;
449 
450  $this->getHookRunner()->onInitializeArticleMaybeRedirect( $title, $request,
451  $ignoreRedirect, $target, $article );
452  $page = $article->getPage(); // reflect any hook changes
453 
454  // Follow redirects only for... redirects.
455  // If $target is set, then a hook wanted to redirect.
456  if ( !$ignoreRedirect && ( $target || $page->isRedirect() ) ) {
457  // Is the target already set by an extension?
458  $target = $target ?: $page->followRedirect();
459  if ( is_string( $target ) && !$this->config->get( 'DisableHardRedirects' ) ) {
460  // we'll need to redirect
461  return $target;
462  }
463  if ( is_object( $target ) ) {
464  // Rewrite environment to redirected article
465  $rpage = WikiPage::factory( $target );
466  $rpage->loadPageData();
467  if ( $rpage->exists() || ( is_object( $file ) && !$file->isLocal() ) ) {
468  $rarticle = Article::newFromWikiPage( $rpage, $this->context );
469  $rarticle->setRedirectedFrom( $title );
470 
471  $article = $rarticle;
472  $this->context->setTitle( $target );
473  $this->context->setWikiPage( $article->getPage() );
474  }
475  }
476  } else {
477  // Article may have been changed by hook
478  $this->context->setTitle( $article->getTitle() );
479  $this->context->setWikiPage( $article->getPage() );
480  }
481  }
482 
483  return $article;
484  }
485 
492  private function performAction( Article $article, Title $requestTitle ) {
493  $request = $this->context->getRequest();
494  $output = $this->context->getOutput();
495  $title = $this->context->getTitle();
496  $user = $this->context->getUser();
497 
498  if ( !$this->getHookRunner()->onMediaWikiPerformAction(
499  $output, $article, $title, $user, $request, $this )
500  ) {
501  return;
502  }
503 
504  $act = $this->getAction();
505  $action = Action::factory( $act, $article, $this->context );
506 
507  if ( $action instanceof Action ) {
508  // Narrow DB query expectations for this HTTP request
509  $trxLimits = $this->config->get( 'TrxProfilerLimits' );
510  $trxProfiler = Profiler::instance()->getTransactionProfiler();
511  if ( $request->wasPosted() && !$action->doesWrites() ) {
512  $trxProfiler->setExpectations( $trxLimits['POST-nonwrite'], __METHOD__ );
513  $request->markAsSafeRequest();
514  }
515 
516  # Let CDN cache things if we can purge them.
517  if ( $this->config->get( 'UseCdn' ) &&
518  in_array(
519  // Use PROTO_INTERNAL because that's what getCdnUrls() uses
520  wfExpandUrl( $request->getRequestURL(), PROTO_INTERNAL ),
521  $requestTitle->getCdnUrls()
522  )
523  ) {
524  $output->setCdnMaxage( $this->config->get( 'CdnMaxAge' ) );
525  }
526 
527  $action->show();
528  return;
529  }
530 
531  // If we've not found out which action it is by now, it's unknown
532  $output->setStatusCode( 404 );
533  $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
534  }
535 
539  public function run() {
540  try {
541  $this->setDBProfilingAgent();
542  try {
543  $this->main();
544  } catch ( ErrorPageError $e ) {
545  $out = $this->context->getOutput();
546  // TODO: Should ErrorPageError::report accept a OutputPage parameter?
548  $out->considerCacheSettingsFinal();
549 
550  // T64091: while exceptions are convenient to bubble up GUI errors,
551  // they are not internal application faults. As with normal requests, this
552  // should commit, print the output, do deferred updates, jobs, and profiling.
553  $this->doPreOutputCommit();
554  $out->output(); // display the GUI error
555  }
556  } catch ( Exception $e ) {
558  $action = $context->getRequest()->getVal( 'action', 'view' );
559  if (
560  $e instanceof DBConnectionError &&
561  $context->hasTitle() &&
562  $context->getTitle()->canExist() &&
563  in_array( $action, [ 'view', 'history' ], true ) &&
565  ) {
566  // Try to use any (even stale) file during outages...
568  if ( $cache->isCached() ) {
569  $cache->loadFromFileCache( $context, HTMLFileCache::MODE_OUTAGE );
570  print MWExceptionRenderer::getHTML( $e );
571  exit;
572  }
573  }
574  // GUI-ify and stash the page output in MediaWiki::doPreOutputCommit() while
575  // ChronologyProtector synchronizes DB positions or replicas across all datacenters.
576  MWExceptionHandler::handleException( $e, MWExceptionHandler::CAUGHT_BY_ENTRYPOINT );
577  } catch ( Throwable $e ) {
578  // Type errors and such: at least handle it now and clean up the LBFactory state
579  MWExceptionHandler::handleException( $e, MWExceptionHandler::CAUGHT_BY_ENTRYPOINT );
580  }
581 
582  $this->doPostOutputShutdown();
583  }
584 
588  private function setDBProfilingAgent() {
589  $services = MediaWikiServices::getInstance();
590  $name = $this->context->getUser()->getName();
591  $services->getDBLoadBalancerFactory()->setAgentName(
592  mb_strlen( $name ) > 15 ? mb_substr( $name, 0, 15 ) . '...' : $name
593  );
594  }
595 
599  private function schedulePostSendJobs() {
600  $jobRunRate = $this->config->get( 'JobRunRate' );
601  if (
602  // Recursion guard
603  $this->getTitle()->isSpecial( 'RunJobs' ) ||
604  // Short circuit if there is nothing to do
605  ( $jobRunRate <= 0 || wfReadOnly() ) ||
606  // Avoid blocking the client on stock apache; see doPostOutputShutdown()
607  (
608  $this->context->getRequest()->getMethod() === 'HEAD' ||
609  $this->context->getRequest()->getHeader( 'If-Modified-Since' )
610  )
611  ) {
612  return;
613  }
614 
615  if ( $jobRunRate < 1 ) {
616  $max = mt_getrandmax();
617  if ( mt_rand( 0, $max ) > $max * $jobRunRate ) {
618  return; // the higher the job run rate, the less likely we return here
619  }
620  $n = 1;
621  } else {
622  $n = intval( $jobRunRate );
623  }
624 
625  // Note that DeferredUpdates will catch and log an errors (T88312)
626  DeferredUpdates::addUpdate( new TransactionRoundDefiningUpdate( function () use ( $n ) {
627  $logger = LoggerFactory::getInstance( 'runJobs' );
628  if ( $this->config->get( 'RunJobsAsync' ) ) {
629  // Send an HTTP request to the job RPC entry point if possible
630  $invokedWithSuccess = $this->triggerAsyncJobs( $n, $logger );
631  if ( !$invokedWithSuccess ) {
632  // Fall back to blocking on running the job(s)
633  $logger->warning( "Jobs switched to blocking; Special:RunJobs disabled" );
634  $this->triggerSyncJobs( $n );
635  }
636  } else {
637  $this->triggerSyncJobs( $n );
638  }
639  }, __METHOD__ ) );
640  }
641 
647  public function doPreOutputCommit( callable $postCommitWork = null ) {
648  self::preOutputCommit( $this->context, $postCommitWork );
649  }
650 
662  public static function preOutputCommit(
663  IContextSource $context, callable $postCommitWork = null
664  ) {
666  $request = $context->getRequest();
667  $output = $context->getOutput();
668  $services = MediaWikiServices::getInstance();
669  $lbFactory = $services->getDBLoadBalancerFactory();
670 
671  // Try to make sure that all RDBMs, session, and other storage updates complete
672  ignore_user_abort( true );
673 
674  // Commit all RDBMs changes from the main transaction round
675  $lbFactory->commitMasterChanges(
676  __METHOD__,
677  // Abort if any transaction was too big
678  [ 'maxWriteDuration' => $config->get( 'MaxUserDBWriteDuration' ) ]
679  );
680  wfDebug( __METHOD__ . ': primary transaction round committed' );
681 
682  // Run updates that need to block the client or affect output (this is the last chance)
684  wfDebug( __METHOD__ . ': pre-send deferred updates completed' );
685  // Persist the session to avoid race conditions on subsequent requests by the client
686  $request->getSession()->save(); // T214471
687  wfDebug( __METHOD__ . ': session changes committed' );
688 
689  // Figure out whether to wait for DB replication now or to use some method that assures
690  // that subsequent requests by the client will use the DB replication positions written
691  // during the shutdown() call below; the later requires working around replication lag
692  // of the store containing DB replication positions (e.g. dynomite, mcrouter).
693  list( $flags, $strategy ) = self::getChronProtStrategy( $lbFactory, $output );
694  // Record ChronologyProtector positions for DBs affected in this request at this point
695  $cpIndex = null;
696  $cpClientId = null;
697  $lbFactory->shutdown( $flags, $postCommitWork, $cpIndex, $cpClientId );
698  wfDebug( __METHOD__ . ': LBFactory shutdown completed' );
699 
700  $allowHeaders = !( $output->isDisabled() || headers_sent() );
701  if ( $cpIndex > 0 ) {
702  if ( $allowHeaders ) {
703  $now = time();
704  $expires = $now + ChronologyProtector::POSITION_COOKIE_TTL;
705  $options = [ 'prefix' => '' ];
706  $value = $lbFactory::makeCookieValueFromCPIndex( $cpIndex, $now, $cpClientId );
707  $request->response()->setCookie( 'cpPosIndex', $value, $expires, $options );
708  }
709 
710  if ( $strategy === 'cookie+url' ) {
711  if ( $output->getRedirect() ) { // sanity
712  $safeUrl = $lbFactory->appendShutdownCPIndexAsQuery(
713  $output->getRedirect(),
714  $cpIndex
715  );
716  $output->redirect( $safeUrl );
717  } else {
719  new LogicException( "No redirect; cannot append cpPosIndex parameter." ),
720  MWExceptionHandler::CAUGHT_BY_ENTRYPOINT
721  );
722  }
723  }
724  }
725 
726  if ( $allowHeaders ) {
727  // Set a cookie to tell all CDN edge nodes to "stick" the user to the DC that
728  // handles this POST request (e.g. the "master" data center). Also have the user
729  // briefly bypass CDN so ChronologyProtector works for cacheable URLs.
730  if ( $request->wasPosted() && $lbFactory->hasOrMadeRecentMasterChanges() ) {
731  $expires = time() + $config->get( 'DataCenterUpdateStickTTL' );
732  $options = [ 'prefix' => '' ];
733  $request->response()->setCookie( 'UseDC', 'master', $expires, $options );
734  $request->response()->setCookie( 'UseCDNCache', 'false', $expires, $options );
735  }
736 
737  // Avoid letting a few seconds of replica DB lag cause a month of stale data.
738  // This logic is also intimately related to the value of $wgCdnReboundPurgeDelay.
739  if ( $lbFactory->laggedReplicaUsed() ) {
740  $maxAge = $config->get( 'CdnMaxageLagged' );
741  $output->lowerCdnMaxage( $maxAge );
742  $request->response()->header( "X-Database-Lagged: true" );
743  wfDebugLog( 'replication',
744  "Lagged DB used; CDN cache TTL limited to $maxAge seconds" );
745  }
746 
747  // Avoid long-term cache pollution due to message cache rebuild timeouts (T133069)
748  if ( $services->getMessageCache()->isDisabled() ) {
749  $maxAge = $config->get( 'CdnMaxageSubstitute' );
750  $output->lowerCdnMaxage( $maxAge );
751  $request->response()->header( "X-Response-Substitute: true" );
752  }
753 
754  if ( !$output->couldBePublicCached() || $output->haveCacheVaryCookies() ) {
755  // Autoblocks: If this user is autoblocked (and the cookie block feature is enabled
756  // for autoblocks), then set a cookie to track this block.
757  // This has to be done on all logged-in page loads (not just upon saving edits),
758  // because an autoblocked editor might not edit again from the same IP address.
759  //
760  // IP blocks: For anons, if their IP is blocked (and cookie block feature is enabled
761  // for IP blocks), we also want to set the cookie whenever it is safe to do.
762  // Basically from any url that are definitely not publicly cacheable (like viewing
763  // EditPage), or when the HTTP response is personalised for other reasons (e.g. viewing
764  // articles within the same browsing session after making an edit).
765  $user = $context->getUser();
766  $services->getBlockManager()
767  ->trackBlockWithCookie( $user, $request->response() );
768  }
769  }
770  }
771 
777  private static function getChronProtStrategy( ILBFactory $lbFactory, OutputPage $output ) {
778  // Should the client return, their request should observe the new ChronologyProtector
779  // DB positions. This request might be on a foreign wiki domain, so synchronously update
780  // the DB positions in all datacenters to be safe. If this output is not a redirect,
781  // then OutputPage::output() will be relatively slow, meaning that running it in
782  // $postCommitWork should help mask the latency of those updates.
783  $flags = $lbFactory::SHUTDOWN_CHRONPROT_SYNC;
784  $strategy = 'cookie+sync';
785 
786  $allowHeaders = !( $output->isDisabled() || headers_sent() );
787  if ( $output->getRedirect() && $lbFactory->hasOrMadeRecentMasterChanges( INF ) ) {
788  // OutputPage::output() will be fast, so $postCommitWork is useless for masking
789  // the latency of synchronously updating the DB positions in all datacenters.
790  // Try to make use of the time the client spends following redirects instead.
791  $domainDistance = self::getUrlDomainDistance( $output->getRedirect() );
792  if ( $domainDistance === 'local' && $allowHeaders ) {
793  $flags = $lbFactory::SHUTDOWN_CHRONPROT_ASYNC;
794  $strategy = 'cookie'; // use same-domain cookie and keep the URL uncluttered
795  } elseif ( $domainDistance === 'remote' ) {
796  $flags = $lbFactory::SHUTDOWN_CHRONPROT_ASYNC;
797  $strategy = 'cookie+url'; // cross-domain cookie might not work
798  }
799  }
800 
801  return [ $flags, $strategy ];
802  }
803 
808  private static function getUrlDomainDistance( $url ) {
809  $clusterWiki = WikiMap::getWikiFromUrl( $url );
810  if ( WikiMap::isCurrentWikiId( $clusterWiki ) ) {
811  return 'local'; // the current wiki
812  }
813  if ( $clusterWiki !== false ) {
814  return 'remote'; // another wiki in this cluster/farm
815  }
816 
817  return 'external';
818  }
819 
829  public function doPostOutputShutdown() {
830  // Record backend request timing
831  $timing = $this->context->getTiming();
832  $timing->mark( 'requestShutdown' );
833 
834  // Perform the last synchronous operations...
835  try {
836  // Show visible profiling data if enabled (which cannot be post-send)
837  Profiler::instance()->logDataPageOutputOnly();
838  } catch ( Throwable $e ) {
839  // An error may already have been shown in run(), so just log it to be safe
840  MWExceptionHandler::logException( $e, MWExceptionHandler::CAUGHT_BY_ENTRYPOINT );
841  }
842 
843  // Disable WebResponse setters for post-send processing (T191537).
845 
846  // Defer everything else if possible...
847  $callback = function () {
848  try {
849  $this->restInPeace();
850  } catch ( Throwable $e ) {
851  // If this is post-send, then displaying errors can cause broken HTML
853  $e,
854  MWExceptionHandler::CAUGHT_BY_ENTRYPOINT
855  );
856  }
857  };
858 
859  if ( $this->postSendStrategy === self::DEFER_FASTCGI_FINISH_REQUEST ) {
860  fastcgi_finish_request();
861  $callback();
862  } else {
863  // Flush PHP and web server output buffers
864  if ( !$this->config->get( 'CommandLineMode' ) ) {
865  AtEase\AtEase::suppressWarnings();
866  if ( ob_get_status() ) {
867  ob_end_flush();
868  }
869  flush();
870  AtEase\AtEase::restoreWarnings();
871  }
872  $callback();
873  }
874  }
875 
879  private function main() {
880  global $wgTitle;
881 
882  $output = $this->context->getOutput();
883  $request = $this->context->getRequest();
884 
885  // Send Ajax requests to the Ajax dispatcher.
886  if ( $request->getVal( 'action' ) === 'ajax' ) {
887  // Set a dummy title, because $wgTitle == null might break things
888  $title = Title::makeTitle( NS_SPECIAL, 'Badtitle/performing an AJAX call in '
889  . __METHOD__
890  );
891  $this->context->setTitle( $title );
892  $wgTitle = $title;
893 
894  $dispatcher = new AjaxDispatcher( $this->config );
895  $dispatcher->performAction( $this->context->getUser() );
896 
897  return;
898  }
899 
900  // Get title from request parameters,
901  // is set on the fly by parseTitle the first time.
902  $title = $this->getTitle();
903  $action = $this->getAction();
904  $wgTitle = $title;
905 
906  // Set DB query expectations for this HTTP request
907  $trxLimits = $this->config->get( 'TrxProfilerLimits' );
908  $trxProfiler = Profiler::instance()->getTransactionProfiler();
909  $trxProfiler->setLogger( LoggerFactory::getInstance( 'DBPerformance' ) );
910  if ( $request->hasSafeMethod() ) {
911  $trxProfiler->setExpectations( $trxLimits['GET'], __METHOD__ );
912  } else {
913  $trxProfiler->setExpectations( $trxLimits['POST'], __METHOD__ );
914  }
915 
916  if ( $this->maybeDoHttpsRedirect() ) {
917  return;
918  }
919 
920  if ( $title->canExist() && HTMLFileCache::useFileCache( $this->context ) ) {
921  // Try low-level file cache hit
923  if ( $cache->isCacheGood( /* Assume up to date */ ) ) {
924  // Check incoming headers to see if client has this cached
925  $timestamp = $cache->cacheTimestamp();
926  if ( !$output->checkLastModified( $timestamp ) ) {
927  $cache->loadFromFileCache( $this->context );
928  }
929  // Do any stats increment/watchlist stuff, assuming user is viewing the
930  // latest revision (which should always be the case for file cache)
931  $this->context->getWikiPage()->doViewUpdates( $this->context->getUser() );
932  // Tell OutputPage that output is taken care of
933  $output->disable();
934 
935  return;
936  }
937  }
938 
939  // Actually do the work of the request and build up any output
940  $this->performRequest();
941 
942  // GUI-ify and stash the page output in MediaWiki::doPreOutputCommit() while
943  // ChronologyProtector synchronizes DB positions or replicas across all datacenters.
944  $buffer = null;
945  $outputWork = function () use ( $output, &$buffer ) {
946  if ( $buffer === null ) {
947  $buffer = $output->output( true );
948  }
949 
950  return $buffer;
951  };
952 
953  // Commit any changes in the current transaction round so that:
954  // a) the transaction is not rolled back after success output was already sent
955  // b) error output is not jumbled together with success output in the response
956  $this->doPreOutputCommit( $outputWork );
957  // If needed, push a deferred update to run jobs after the output is send
958  $this->schedulePostSendJobs();
959  // If no exceptions occurred then send the output since it is safe now
960  $this->outputResponsePayload( $outputWork() );
961  }
962 
969  private function shouldDoHttpRedirect() {
970  $request = $this->context->getRequest();
971 
972  // Don't redirect if we're already on HTTPS
973  if ( $request->getProtocol() !== 'http' ) {
974  return false;
975  }
976 
977  $force = $this->config->get( 'ForceHTTPS' );
978 
979  // Don't redirect if $wgServer is explicitly HTTP. We test for this here
980  // by checking whether wfExpandUrl() is able to force HTTPS.
981  if ( !preg_match( '#^https://#', wfExpandUrl( $request->getRequestURL(), PROTO_HTTPS ) ) ) {
982  if ( $force ) {
983  throw new RuntimeException( '$wgForceHTTPS is true but the server is not HTTPS' );
984  }
985  return false;
986  }
987 
988  // Configured $wgForceHTTPS overrides the remaining conditions
989  if ( $force ) {
990  return true;
991  }
992 
993  // Check if HTTPS is required by the session or user preferences
994  return $request->getSession()->shouldForceHTTPS() ||
995  // Check the cookie manually, for paranoia
996  $request->getCookie( 'forceHTTPS', '' ) ||
997  // Avoid checking the user and groups unless it's enabled.
998  (
999  $this->context->getUser()->isLoggedIn()
1000  && $this->context->getUser()->requiresHTTPS()
1001  );
1002  }
1003 
1013  private function maybeDoHttpsRedirect() {
1014  if ( !$this->shouldDoHttpRedirect() ) {
1015  return false;
1016  }
1017 
1018  $request = $this->context->getRequest();
1019  $oldUrl = $request->getFullRequestURL();
1020  $redirUrl = preg_replace( '#^http://#', 'https://', $oldUrl );
1021 
1022  // ATTENTION: This hook is likely to be removed soon due to overall design of the system.
1023  if ( !$this->getHookRunner()->onBeforeHttpsRedirect( $this->context, $redirUrl ) ) {
1024  return false;
1025  }
1026 
1027  if ( $request->wasPosted() ) {
1028  // This is weird and we'd hope it almost never happens. This
1029  // means that a POST came in via HTTP and policy requires us
1030  // redirecting to HTTPS. It's likely such a request is going
1031  // to fail due to post data being lost, but let's try anyway
1032  // and just log the instance.
1033 
1034  // @todo FIXME: See if we could issue a 307 or 308 here, need
1035  // to see how clients (automated & browser) behave when we do
1036  wfDebugLog( 'RedirectedPosts', "Redirected from HTTP to HTTPS: $oldUrl" );
1037  }
1038  // Setup dummy Title, otherwise OutputPage::redirect will fail
1039  $title = Title::newFromText( 'REDIR', NS_MAIN );
1040  $this->context->setTitle( $title );
1041  // Since we only do this redir to change proto, always send a vary header
1042  $output = $this->context->getOutput();
1043  $output->addVaryHeader( 'X-Forwarded-Proto' );
1044  $output->redirect( $redirUrl );
1045  $output->output();
1046 
1047  return true;
1048  }
1049 
1057  private function outputResponsePayload( $content ) {
1058  if (
1059  $this->postSendStrategy === self::DEFER_SET_LENGTH_AND_FLUSH &&
1061  !headers_sent()
1062  ) {
1063  $response = $this->context->getRequest()->response();
1064  // Make the browser indicate the page as "loaded" as soon as it gets all the content
1065  $response->header( 'Connection: close' );
1066  // The client should not be blocked on "post-send" updates. If apache or ob_* decide
1067  // that a response should be gzipped, the entire script will have to finish before
1068  // any data can be sent. Disable compression if there are any post-send updates.
1069  $response->header( 'Content-Encoding: none' );
1070  AtEase\AtEase::suppressWarnings();
1071  ini_set( 'zlib.output_compression', 0 );
1072  if ( function_exists( 'apache_setenv' ) ) {
1073  apache_setenv( 'no-gzip', '1' );
1074  }
1075  AtEase\AtEase::restoreWarnings();
1076  // Also set the Content-Length so that apache does not block waiting on PHP to finish.
1077  // If OutputPage is disabled, then either there is no body (e.g. HTTP 304) and thus no
1078  // Content-Length, or it was taken care of already.
1079  if ( !$this->context->getOutput()->isDisabled() ) {
1080  ob_start();
1081  print $content;
1082  $response->header( 'Content-Length: ' . ob_get_length() );
1083  ob_end_flush();
1084  }
1085  // @TODO: this still blocks on HEAD responses and 304 responses to GETs
1086  } else {
1087  print $content;
1088  }
1089  }
1090 
1094  public function restInPeace() {
1095  // Either all DB and deferred updates should happen or none.
1096  // The latter should not be cancelled due to client disconnect.
1097  ignore_user_abort( true );
1098 
1099  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
1100  // Assure deferred updates are not in the main transaction
1101  $lbFactory->commitMasterChanges( __METHOD__ );
1102 
1103  // Loosen DB query expectations since the HTTP client is unblocked
1104  $trxProfiler = Profiler::instance()->getTransactionProfiler();
1105  $trxProfiler->redefineExpectations(
1106  $this->context->getRequest()->hasSafeMethod()
1107  ? $this->config->get( 'TrxProfilerLimits' )['PostSend-GET']
1108  : $this->config->get( 'TrxProfilerLimits' )['PostSend-POST'],
1109  __METHOD__
1110  );
1111 
1112  // Do any deferred jobs; preferring to run them now if a client will not wait on them
1113  DeferredUpdates::doUpdates( 'run' );
1114 
1115  // Log profiling data, e.g. in the database or UDP
1117 
1118  // Commit and close up!
1119  $lbFactory->commitMasterChanges( __METHOD__ );
1120  $lbFactory->shutdown( $lbFactory::SHUTDOWN_NO_CHRONPROT );
1121 
1122  wfDebug( "Request ended normally" );
1123  }
1124 
1133  public static function emitBufferedStatsdData(
1135  ) {
1136  if ( $config->get( 'StatsdServer' ) && $stats->hasData() ) {
1137  try {
1138  $statsdServer = explode( ':', $config->get( 'StatsdServer' ), 2 );
1139  $statsdHost = $statsdServer[0];
1140  $statsdPort = $statsdServer[1] ?? 8125;
1141  $statsdSender = new SocketSender( $statsdHost, $statsdPort );
1142  $statsdClient = new SamplingStatsdClient( $statsdSender, true, false );
1143  $statsdClient->setSamplingRates( $config->get( 'StatsdSamplingRates' ) );
1144  $statsdClient->send( $stats->getData() );
1145 
1146  $stats->clearData(); // empty buffer for the next round
1147  } catch ( Exception $e ) {
1148  MWExceptionHandler::logException( $e, MWExceptionHandler::CAUGHT_BY_ENTRYPOINT );
1149  }
1150  }
1151  }
1152 
1159  public function triggerJobs() {
1160  $jobRunRate = $this->config->get( 'JobRunRate' );
1161  if ( $this->getTitle()->isSpecial( 'RunJobs' ) ) {
1162  return; // recursion guard
1163  } elseif ( $jobRunRate <= 0 || wfReadOnly() ) {
1164  return;
1165  }
1166 
1167  if ( $jobRunRate < 1 ) {
1168  $max = mt_getrandmax();
1169  if ( mt_rand( 0, $max ) > $max * $jobRunRate ) {
1170  return; // the higher the job run rate, the less likely we return here
1171  }
1172  $n = 1;
1173  } else {
1174  $n = intval( $jobRunRate );
1175  }
1176 
1177  $logger = LoggerFactory::getInstance( 'runJobs' );
1178 
1179  try {
1180  if ( $this->config->get( 'RunJobsAsync' ) ) {
1181  // Send an HTTP request to the job RPC entry point if possible
1182  $invokedWithSuccess = $this->triggerAsyncJobs( $n, $logger );
1183  if ( !$invokedWithSuccess ) {
1184  // Fall back to blocking on running the job(s)
1185  $logger->warning( "Jobs switched to blocking; Special:RunJobs disabled" );
1186  $this->triggerSyncJobs( $n );
1187  }
1188  } else {
1189  $this->triggerSyncJobs( $n );
1190  }
1191  } catch ( JobQueueError $e ) {
1192  // Do not make the site unavailable (T88312)
1193  MWExceptionHandler::logException( $e, MWExceptionHandler::CAUGHT_BY_ENTRYPOINT );
1194  }
1195  }
1196 
1200  private function triggerSyncJobs( $n ) {
1201  $trxProfiler = Profiler::instance()->getTransactionProfiler();
1202  $old = $trxProfiler->setSilenced( true );
1203  try {
1204  $runner = MediaWikiServices::getInstance()->getJobRunner();
1205  $runner->run( [ 'maxJobs' => $n ] );
1206  } finally {
1207  $trxProfiler->setSilenced( $old );
1208  }
1209  }
1210 
1216  private function triggerAsyncJobs( $n, LoggerInterface $runJobsLogger ) {
1217  // Do not send request if there are probably no jobs
1218  $group = JobQueueGroup::singleton();
1219  if ( !$group->queuesHaveJobs( JobQueueGroup::TYPE_DEFAULT ) ) {
1220  return true;
1221  }
1222 
1223  $query = [ 'title' => 'Special:RunJobs',
1224  'tasks' => 'jobs', 'maxjobs' => $n, 'sigexpiry' => time() + 5 ];
1225  $query['signature'] = SpecialRunJobs::getQuerySignature(
1226  $query, $this->config->get( 'SecretKey' ) );
1227 
1228  $errno = $errstr = null;
1229  $info = wfParseUrl( $this->config->get( 'CanonicalServer' ) );
1230  $host = $info ? $info['host'] : null;
1231  $port = 80;
1232  if ( isset( $info['scheme'] ) && $info['scheme'] == 'https' ) {
1233  $host = "tls://" . $host;
1234  $port = 443;
1235  }
1236  if ( isset( $info['port'] ) ) {
1237  $port = $info['port'];
1238  }
1239 
1240  Wikimedia\suppressWarnings();
1241  $sock = $host ? fsockopen(
1242  $host,
1243  $port,
1244  $errno,
1245  $errstr,
1246  // If it takes more than 100ms to connect to ourselves there is a problem...
1247  0.100
1248  ) : false;
1249  Wikimedia\restoreWarnings();
1250 
1251  $invokedWithSuccess = true;
1252  if ( $sock ) {
1253  $special = MediaWikiServices::getInstance()->getSpecialPageFactory()->
1254  getPage( 'RunJobs' );
1255  $url = $special->getPageTitle()->getCanonicalURL( $query );
1256  $req = (
1257  "POST $url HTTP/1.1\r\n" .
1258  "Host: {$info['host']}\r\n" .
1259  "Connection: Close\r\n" .
1260  "Content-Length: 0\r\n\r\n"
1261  );
1262 
1263  $runJobsLogger->info( "Running $n job(s) via '$url'" );
1264  // Send a cron API request to be performed in the background.
1265  // Give up if this takes too long to send (which should be rare).
1266  stream_set_timeout( $sock, 2 );
1267  $bytes = fwrite( $sock, $req );
1268  if ( $bytes !== strlen( $req ) ) {
1269  $invokedWithSuccess = false;
1270  $runJobsLogger->error( "Failed to start cron API (socket write error)" );
1271  } else {
1272  // Do not wait for the response (the script should handle client aborts).
1273  // Make sure that we don't close before that script reaches ignore_user_abort().
1274  $start = microtime( true );
1275  $status = fgets( $sock );
1276  $sec = microtime( true ) - $start;
1277  if ( !preg_match( '#^HTTP/\d\.\d 202 #', $status ) ) {
1278  $invokedWithSuccess = false;
1279  $runJobsLogger->error( "Failed to start cron API: received '$status' ($sec)" );
1280  }
1281  }
1282  fclose( $sock );
1283  } else {
1284  $invokedWithSuccess = false;
1285  $runJobsLogger->error( "Failed to start cron API (socket error $errno): $errstr" );
1286  }
1287 
1288  return $invokedWithSuccess;
1289  }
1290 }
Action\getActionName
static getActionName(IContextSource $context)
Get the action that will be executed, not necessarily the one passed passed through the "action" requ...
Definition: Action.php:154
DerivativeRequest
Similar to FauxRequest, but only fakes URL parameters and method (POST or GET) and use the base reque...
Definition: DerivativeRequest.php:36
IBufferingStatsdDataFactory\clearData
clearData()
Clear all buffered data from the factory.
HTMLFileCache\useFileCache
static useFileCache(IContextSource $context, $mode=self::MODE_NORMAL)
Check if pages can be cached for this request/user.
Definition: HTMLFileCache.php:93
MediaWiki\setDBProfilingAgent
setDBProfilingAgent()
Add a comment to future SQL queries for easy SHOW PROCESSLIST interpretation.
Definition: MediaWiki.php:588
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:329
MediaWiki\performAction
performAction(Article $article, Title $requestTitle)
Perform one of the "standard" actions.
Definition: MediaWiki.php:492
MediaWiki\emitBufferedStatsdData
static emitBufferedStatsdData(IBufferingStatsdDataFactory $stats, Config $config)
Send out any buffered statsd data according to sampling rules.
Definition: MediaWiki.php:1133
PROTO_INTERNAL
const PROTO_INTERNAL
Definition: Defines.php:213
MWExceptionRenderer\getHTML
static getHTML(Throwable $e)
If $wgShowExceptionDetails is true, return a HTML message with a backtrace to the error,...
Definition: MWExceptionRenderer.php:180
Profiler\instance
static instance()
Singleton.
Definition: Profiler.php:63
WikiMap\isCurrentWikiId
static isCurrentWikiId( $wikiId)
Definition: WikiMap.php:321
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:154
HTMLFileCache
Page view caching in the file system.
Definition: HTMLFileCache.php:33
MediaWiki\run
run()
Run the current MediaWiki instance; index.php just calls this.
Definition: MediaWiki.php:539
JobQueueGroup\TYPE_DEFAULT
const TYPE_DEFAULT
Definition: JobQueueGroup.php:47
MediaWiki\restInPeace
restInPeace()
Ends this task peacefully.
Definition: MediaWiki.php:1094
HTMLFileCache\MODE_OUTAGE
const MODE_OUTAGE
Definition: HTMLFileCache.php:35
MediaWiki\getTitle
getTitle()
Get the Title object that we'll be acting on, as specified in the WebRequest.
Definition: MediaWiki.php:148
WikiMap\getWikiFromUrl
static getWikiFromUrl( $url)
Definition: WikiMap.php:224
MediaWiki\main
main()
Determine and send the response headers and body for this web request.
Definition: MediaWiki.php:879
DeferredUpdates\addUpdate
static addUpdate(DeferrableUpdate $update, $stage=self::POSTSEND)
Add an update to the deferred update queue for execution at the appropriate time.
Definition: DeferredUpdates.php:106
NS_FILE
const NS_FILE
Definition: Defines.php:75
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
SpecialRunJobs\getQuerySignature
static getQuerySignature(array $query, $secretKey)
Definition: SpecialRunJobs.php:124
wfReadOnly
wfReadOnly()
Check whether the wiki is in read-only mode.
Definition: GlobalFunctions.php:1125
SpecialPage\getTitleFor
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,...
Definition: SpecialPage.php:92
PermissionsError
Show an error when a user tries to do something they do not have the necessary permissions for.
Definition: PermissionsError.php:31
Title\newMainPage
static newMainPage(MessageLocalizer $localizer=null)
Create a new Title for the Main Page.
Definition: Title.php:654
OutputPage\getRedirect
getRedirect()
Get the URL to redirect to, or an empty string if not redirect URL set.
Definition: OutputPage.php:373
Action\factory
static factory(?string $action, Page $article, IContextSource $context=null)
Get an appropriate Action subclass for the given action.
Definition: Action.php:115
RedirectSpecialPage
Shortcut to construct a special page alias.
Definition: RedirectSpecialPage.php:31
$wgTitle
$wgTitle
Definition: Setup.php:794
IBufferingStatsdDataFactory\hasData
hasData()
Check whether this data factory has any buffered data.
wfDebugLog
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
Definition: GlobalFunctions.php:988
HttpError
Show an error that looks like an HTTP server error.
Definition: HttpError.php:32
ErrorPageError\report
report( $action=self::SEND_OUTPUT)
Stable to override.
Definition: ErrorPageError.php:77
Action
Actions are things which can be done to pages (edit, delete, rollback, etc).
Definition: Action.php:43
MediaWiki\getChronProtStrategy
static getChronProtStrategy(ILBFactory $lbFactory, OutputPage $output)
Definition: MediaWiki.php:777
NS_MAIN
const NS_MAIN
Definition: Defines.php:69
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:185
MWExceptionHandler\logException
static logException(Throwable $e, $catcher=self::CAUGHT_BY_OTHER, $extraData=[])
Log a throwable to the exception log (if enabled).
Definition: MWExceptionHandler.php:668
MediaWiki\maybeDoHttpsRedirect
maybeDoHttpsRedirect()
If the stars are suitably aligned, do an HTTP->HTTPS redirect.
Definition: MediaWiki.php:1013
ErrorPageError\STAGE_OUTPUT
const STAGE_OUTPUT
Definition: ErrorPageError.php:32
Config
Interface for configuration instances.
Definition: Config.php:30
Title\getCdnUrls
getCdnUrls()
Get a list of URLs to purge from the CDN cache when this page changes.
Definition: Title.php:3607
NS_SPECIAL
const NS_SPECIAL
Definition: Defines.php:58
MediaWiki\$postSendStrategy
int $postSendStrategy
Class DEFER_* constant; how non-blocking post-response tasks should run.
Definition: MediaWiki.php:47
wfParseUrl
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
Definition: GlobalFunctions.php:791
Article\newFromWikiPage
static newFromWikiPage(WikiPage $page, IContextSource $context)
Create an Article object of the appropriate class for the given page.
Definition: Article.php:226
IBufferingStatsdDataFactory\getData
getData()
Return the buffered data from the factory.
MWException
MediaWiki exception.
Definition: MWException.php:29
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:156
MWExceptionHandler\handleException
static handleException(Throwable $e, $catcher=self::CAUGHT_BY_OTHER)
Exception handler which simulates the appropriate catch() handling:
Definition: MWExceptionHandler.php:183
MediaWiki\Logger\LoggerFactory
PSR-3 logger instance factory.
Definition: LoggerFactory.php:45
Config\get
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".
MediaWiki\triggerSyncJobs
triggerSyncJobs( $n)
Definition: MediaWiki.php:1200
MediaWiki\parseTitle
parseTitle()
Parse the request to get the Title object.
Definition: MediaWiki.php:73
MediaWiki\schedulePostSendJobs
schedulePostSendJobs()
If enabled, after everything specific to this request is done, occasionally run jobs.
Definition: MediaWiki.php:599
OutputPage\isDisabled
isDisabled()
Return whether the output will be completely disabled.
Definition: OutputPage.php:1154
MediaWiki
A helper class for throttling authentication attempts.
PROTO_CURRENT
const PROTO_CURRENT
Definition: Defines.php:211
SamplingStatsdClient
A statsd client that applies the sampling rate to the data items before sending them.
Definition: SamplingStatsdClient.php:32
$title
$title
Definition: testCompression.php:38
MediaWiki\doPreOutputCommit
doPreOutputCommit(callable $postCommitWork=null)
Definition: MediaWiki.php:647
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:592
JobQueueError
@newable
Definition: JobQueueError.php:29
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:909
OutputPage
This is one of the Core classes and should be read at least once by any new developers.
Definition: OutputPage.php:47
MediaWiki\triggerAsyncJobs
triggerAsyncJobs( $n, LoggerInterface $runJobsLogger)
Definition: MediaWiki.php:1216
PROTO_HTTPS
const PROTO_HTTPS
Definition: Defines.php:209
Title\newFromTextThrow
static newFromTextThrow( $text, $defaultNamespace=NS_MAIN)
Like Title::newFromText(), but throws MalformedTitleException when the title is invalid,...
Definition: Title.php:364
MediaWiki\triggerJobs
triggerJobs()
Potentially open a socket and sent an HTTP request back to the server to run a specified number of jo...
Definition: MediaWiki.php:1159
WebResponse\disableForPostSend
static disableForPostSend()
Disable setters for post-send processing.
Definition: WebResponse.php:49
AjaxDispatcher
Object-Oriented Ajax functions.
Definition: AjaxDispatcher.php:37
$content
$content
Definition: router.php:76
MediaWiki\preOutputCommit
static preOutputCommit(IContextSource $context, callable $postCommitWork=null)
This function commits all DB and session changes as needed before the client can receive a response (...
Definition: MediaWiki.php:662
NS_MEDIA
const NS_MEDIA
Definition: Defines.php:57
MediaWiki\tryNormaliseRedirect
tryNormaliseRedirect(Title $title)
Handle redirects for uncanonical title requests.
Definition: MediaWiki.php:347
MediaWiki\__construct
__construct(IContextSource $context=null)
Definition: MediaWiki.php:57
Title\newFromURL
static newFromURL( $url)
THIS IS NOT THE FUNCTION YOU WANT.
Definition: Title.php:414
IContextSource\getUser
getUser()
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:453
IContextSource\getTitle
getTitle()
Title\newFromLinkTarget
static newFromLinkTarget(LinkTarget $linkTarget, $forceClone='')
Returns a Title given a LinkTarget.
Definition: Title.php:281
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:55
DeferredUpdates\doUpdates
static doUpdates( $mode='run', $stage=self::ALL)
Consume the list of deferred updates and execute them.
Definition: DeferredUpdates.php:171
DeferredUpdates\pendingUpdatesCount
static pendingUpdatesCount()
Get the number of currently enqueued updates in the top-queues.
Definition: DeferredUpdates.php:535
IBufferingStatsdDataFactory
MediaWiki adaptation of StatsdDataFactory that provides buffering functionality.
Definition: IBufferingStatsdDataFactory.php:13
MediaWiki\performRequest
performRequest()
Performs the request.
Definition: MediaWiki.php:184
MediaWiki\$action
string $action
Cache what action this request is.
Definition: MediaWiki.php:45
MediaWiki\getAction
getAction()
Returns the name of the action that will be executed.
Definition: MediaWiki.php:164
Title
Represents a title within MediaWiki.
Definition: Title.php:42
MediaWiki\initializeArticle
initializeArticle()
Initialize the main Article object for "standard" actions (view, etc) Create an Article object for th...
Definition: MediaWiki.php:407
MWExceptionHandler\rollbackMasterChangesAndLog
static rollbackMasterChangesAndLog(Throwable $e, $catcher=self::CAUGHT_BY_OTHER)
Roll back any open database transactions and log the stack trace of the throwable.
Definition: MWExceptionHandler.php:125
JobQueueGroup\singleton
static singleton( $domain=false)
Definition: JobQueueGroup.php:70
$cache
$cache
Definition: mcc.php:33
MalformedTitleException
MalformedTitleException is thrown when a TitleParser is unable to parse a title string.
Definition: MalformedTitleException.php:26
DeferredUpdates\PRESEND
const PRESEND
Definition: DeferredUpdates.php:84
Wikimedia\Rdbms\ChronologyProtector
Helper class for mitigating DB replication lag in order to provide "session consistency".
Definition: ChronologyProtector.php:41
IContextSource\getConfig
getConfig()
Get the site configuration.
MediaWiki\$config
Config $config
Definition: MediaWiki.php:42
TransactionRoundDefiningUpdate
Deferrable update that must run outside of any explicit LBFactory transaction round.
Definition: TransactionRoundDefiningUpdate.php:10
Wikimedia\Rdbms\DBConnectionError
@newable
Definition: DBConnectionError.php:27
IContextSource\getRequest
getRequest()
WikiFilePage
Special handling for file pages.
Definition: WikiFilePage.php:31
wfWarn
wfWarn( $msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
Definition: GlobalFunctions.php:1073
Article
Class for viewing MediaWiki article and history.
Definition: Article.php:46
MediaWiki\shouldDoHttpRedirect
shouldDoHttpRedirect()
Check if an HTTP->HTTPS redirect should be done.
Definition: MediaWiki.php:969
MediaWiki\$context
IContextSource $context
Definition: MediaWiki.php:40
MediaWiki\doPostOutputShutdown
doPostOutputShutdown()
This function does work that can be done after the user gets the HTTP response so they don't block on...
Definition: MediaWiki.php:829
Wikimedia\Rdbms\ILBFactory\hasOrMadeRecentMasterChanges
hasOrMadeRecentMasterChanges( $age=null)
Determine if any master connection has pending/written changes from this request.
ErrorPageError
An error page which can definitely be safely rendered using the OutputPage.
Definition: ErrorPageError.php:30
MediaWiki\outputResponsePayload
outputResponsePayload( $content)
Set the actual output and attempt to flush it to the client if necessary.
Definition: MediaWiki.php:1057
MediaWiki\getUrlDomainDistance
static getUrlDomainDistance( $url)
Definition: MediaWiki.php:808
Title\newFromID
static newFromID( $id, $flags=0)
Create a new Title from an article ID.
Definition: Title.php:473
IContextSource\getOutput
getOutput()
BadTitleError
Show an error page on a badtitle.
Definition: BadTitleError.php:31
wfExpandUrl
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
Definition: GlobalFunctions.php:490
Wikimedia\Rdbms\ILBFactory
An interface for generating database load balancers.
Definition: ILBFactory.php:33
wfLogProfilingData
wfLogProfilingData()
Definition: GlobalFunctions.php:1094