MediaWiki.php 39 KB

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