MediaWiki.php 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  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\ChronologyProtector;
  26. use Wikimedia\Rdbms\LBFactory;
  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. global $wgContLang;
  63. $request = $this->context->getRequest();
  64. $curid = $request->getInt( 'curid' );
  65. $title = $request->getVal( 'title' );
  66. $action = $request->getVal( 'action' );
  67. if ( $request->getCheck( 'search' ) ) {
  68. // Compatibility with old search URLs which didn't use Special:Search
  69. // Just check for presence here, so blank requests still
  70. // show the search page when using ugly URLs (T10054).
  71. $ret = SpecialPage::getTitleFor( 'Search' );
  72. } elseif ( $curid ) {
  73. // URLs like this are generated by RC, because rc_title isn't always accurate
  74. $ret = Title::newFromID( $curid );
  75. } else {
  76. $ret = Title::newFromURL( $title );
  77. // Alias NS_MEDIA page URLs to NS_FILE...we only use NS_MEDIA
  78. // in wikitext links to tell Parser to make a direct file link
  79. if ( !is_null( $ret ) && $ret->getNamespace() == NS_MEDIA ) {
  80. $ret = Title::makeTitle( NS_FILE, $ret->getDBkey() );
  81. }
  82. // Check variant links so that interwiki links don't have to worry
  83. // about the possible different language variants
  84. if ( count( $wgContLang->getVariants() ) > 1
  85. && !is_null( $ret ) && $ret->getArticleID() == 0
  86. ) {
  87. $wgContLang->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 ? $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. if ( $title->isSpecialPage() ) {
  230. $specialPage = SpecialPageFactory::getPage( $title->getDBkey() );
  231. if ( $specialPage instanceof RedirectSpecialPage ) {
  232. $specialPage->setContext( $this->context );
  233. if ( $this->config->get( 'HideIdentifiableRedirects' )
  234. && $specialPage->personallyIdentifiableTarget()
  235. ) {
  236. list( , $subpage ) = SpecialPageFactory::resolveAlias( $title->getDBkey() );
  237. $target = $specialPage->getRedirect( $subpage );
  238. // target can also be true. We let that case fall through to normal processing.
  239. if ( $target instanceof Title ) {
  240. $query = $specialPage->getRedirectQuery() ?: [];
  241. $request = new DerivativeRequest( $this->context->getRequest(), $query );
  242. $request->setRequestURL( $this->context->getRequest()->getRequestURL() );
  243. $this->context->setRequest( $request );
  244. // Do not varnish cache these. May vary even for anons
  245. $this->context->getOutput()->lowerCdnMaxage( 0 );
  246. $this->context->setTitle( $target );
  247. $wgTitle = $target;
  248. // Reset action type cache. (Special pages have only view)
  249. $this->action = null;
  250. $title = $target;
  251. $output->addJsConfigVars( [
  252. 'wgInternalRedirectTargetUrl' => $target->getFullURL( $query ),
  253. ] );
  254. $output->addModules( 'mediawiki.action.view.redirect' );
  255. }
  256. }
  257. }
  258. }
  259. // Special pages ($title may have changed since if statement above)
  260. if ( $title->isSpecialPage() ) {
  261. // Actions that need to be made when we have a special pages
  262. SpecialPageFactory::executePath( $title, $this->context );
  263. } else {
  264. // ...otherwise treat it as an article view. The article
  265. // may still be a wikipage redirect to another article or URL.
  266. $article = $this->initializeArticle();
  267. if ( is_object( $article ) ) {
  268. $this->performAction( $article, $requestTitle );
  269. } elseif ( is_string( $article ) ) {
  270. $output->redirect( $article );
  271. } else {
  272. throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle()"
  273. . " returned neither an object nor a URL" );
  274. }
  275. }
  276. }
  277. }
  278. /**
  279. * Handle redirects for uncanonical title requests.
  280. *
  281. * Handles:
  282. * - Redirect loops.
  283. * - No title in URL.
  284. * - $wgUsePathInfo URLs.
  285. * - URLs with a variant.
  286. * - Other non-standard URLs (as long as they have no extra query parameters).
  287. *
  288. * Behaviour:
  289. * - Normalise title values:
  290. * /wiki/Foo%20Bar -> /wiki/Foo_Bar
  291. * - Normalise empty title:
  292. * /wiki/ -> /wiki/Main
  293. * /w/index.php?title= -> /wiki/Main
  294. * - Don't redirect anything with query parameters other than 'title' or 'action=view'.
  295. *
  296. * @param Title $title
  297. * @return bool True if a redirect was set.
  298. * @throws HttpError
  299. */
  300. private function tryNormaliseRedirect( Title $title ) {
  301. $request = $this->context->getRequest();
  302. $output = $this->context->getOutput();
  303. if ( $request->getVal( 'action', 'view' ) != 'view'
  304. || $request->wasPosted()
  305. || ( $request->getVal( 'title' ) !== null
  306. && $title->getPrefixedDBkey() == $request->getVal( 'title' ) )
  307. || count( $request->getValueNames( [ 'action', 'title' ] ) )
  308. || !Hooks::run( 'TestCanonicalRedirect', [ $request, $title, $output ] )
  309. ) {
  310. return false;
  311. }
  312. if ( $title->isSpecialPage() ) {
  313. list( $name, $subpage ) = SpecialPageFactory::resolveAlias( $title->getDBkey() );
  314. if ( $name ) {
  315. $title = SpecialPage::getTitleFor( $name, $subpage );
  316. }
  317. }
  318. // Redirect to canonical url, make it a 301 to allow caching
  319. $targetUrl = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
  320. if ( $targetUrl == $request->getFullRequestURL() ) {
  321. $message = "Redirect loop detected!\n\n" .
  322. "This means the wiki got confused about what page was " .
  323. "requested; this sometimes happens when moving a wiki " .
  324. "to a new server or changing the server configuration.\n\n";
  325. if ( $this->config->get( 'UsePathInfo' ) ) {
  326. $message .= "The wiki is trying to interpret the page " .
  327. "title from the URL path portion (PATH_INFO), which " .
  328. "sometimes fails depending on the web server. Try " .
  329. "setting \"\$wgUsePathInfo = false;\" in your " .
  330. "LocalSettings.php, or check that \$wgArticlePath " .
  331. "is correct.";
  332. } else {
  333. $message .= "Your web server was detected as possibly not " .
  334. "supporting URL path components (PATH_INFO) correctly; " .
  335. "check your LocalSettings.php for a customized " .
  336. "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
  337. "to true.";
  338. }
  339. throw new HttpError( 500, $message );
  340. }
  341. $output->setCdnMaxage( 1200 );
  342. $output->redirect( $targetUrl, '301' );
  343. return true;
  344. }
  345. /**
  346. * Initialize the main Article object for "standard" actions (view, etc)
  347. * Create an Article object for the page, following redirects if needed.
  348. *
  349. * @return Article|string An Article, or a string to redirect to another URL
  350. */
  351. private function initializeArticle() {
  352. $title = $this->context->getTitle();
  353. if ( $this->context->canUseWikiPage() ) {
  354. // Try to use request context wiki page, as there
  355. // is already data from db saved in per process
  356. // cache there from this->getAction() call.
  357. $page = $this->context->getWikiPage();
  358. } else {
  359. // This case should not happen, but just in case.
  360. // @TODO: remove this or use an exception
  361. $page = WikiPage::factory( $title );
  362. $this->context->setWikiPage( $page );
  363. wfWarn( "RequestContext::canUseWikiPage() returned false" );
  364. }
  365. // Make GUI wrapper for the WikiPage
  366. $article = Article::newFromWikiPage( $page, $this->context );
  367. // Skip some unnecessary code if the content model doesn't support redirects
  368. if ( !ContentHandler::getForTitle( $title )->supportsRedirects() ) {
  369. return $article;
  370. }
  371. $request = $this->context->getRequest();
  372. // Namespace might change when using redirects
  373. // Check for redirects ...
  374. $action = $request->getVal( 'action', 'view' );
  375. $file = ( $page instanceof WikiFilePage ) ? $page->getFile() : null;
  376. if ( ( $action == 'view' || $action == 'render' ) // ... for actions that show content
  377. && !$request->getVal( 'oldid' ) // ... and are not old revisions
  378. && !$request->getVal( 'diff' ) // ... and not when showing diff
  379. && $request->getVal( 'redirect' ) != 'no' // ... unless explicitly told not to
  380. // ... and the article is not a non-redirect image page with associated file
  381. && !( is_object( $file ) && $file->exists() && !$file->getRedirected() )
  382. ) {
  383. // Give extensions a change to ignore/handle redirects as needed
  384. $ignoreRedirect = $target = false;
  385. Hooks::run( 'InitializeArticleMaybeRedirect',
  386. [ &$title, &$request, &$ignoreRedirect, &$target, &$article ] );
  387. $page = $article->getPage(); // reflect any hook changes
  388. // Follow redirects only for... redirects.
  389. // If $target is set, then a hook wanted to redirect.
  390. if ( !$ignoreRedirect && ( $target || $page->isRedirect() ) ) {
  391. // Is the target already set by an extension?
  392. $target = $target ? $target : $page->followRedirect();
  393. if ( is_string( $target ) ) {
  394. if ( !$this->config->get( 'DisableHardRedirects' ) ) {
  395. // we'll need to redirect
  396. return $target;
  397. }
  398. }
  399. if ( is_object( $target ) ) {
  400. // Rewrite environment to redirected article
  401. $rpage = WikiPage::factory( $target );
  402. $rpage->loadPageData();
  403. if ( $rpage->exists() || ( is_object( $file ) && !$file->isLocal() ) ) {
  404. $rarticle = Article::newFromWikiPage( $rpage, $this->context );
  405. $rarticle->setRedirectedFrom( $title );
  406. $article = $rarticle;
  407. $this->context->setTitle( $target );
  408. $this->context->setWikiPage( $article->getPage() );
  409. }
  410. }
  411. } else {
  412. // Article may have been changed by hook
  413. $this->context->setTitle( $article->getTitle() );
  414. $this->context->setWikiPage( $article->getPage() );
  415. }
  416. }
  417. return $article;
  418. }
  419. /**
  420. * Perform one of the "standard" actions
  421. *
  422. * @param Page $page
  423. * @param Title $requestTitle The original title, before any redirects were applied
  424. */
  425. private function performAction( Page $page, Title $requestTitle ) {
  426. $request = $this->context->getRequest();
  427. $output = $this->context->getOutput();
  428. $title = $this->context->getTitle();
  429. $user = $this->context->getUser();
  430. if ( !Hooks::run( 'MediaWikiPerformAction',
  431. [ $output, $page, $title, $user, $request, $this ] )
  432. ) {
  433. return;
  434. }
  435. $act = $this->getAction();
  436. $action = Action::factory( $act, $page, $this->context );
  437. if ( $action instanceof Action ) {
  438. // Narrow DB query expectations for this HTTP request
  439. $trxLimits = $this->config->get( 'TrxProfilerLimits' );
  440. $trxProfiler = Profiler::instance()->getTransactionProfiler();
  441. if ( $request->wasPosted() && !$action->doesWrites() ) {
  442. $trxProfiler->setExpectations( $trxLimits['POST-nonwrite'], __METHOD__ );
  443. $request->markAsSafeRequest();
  444. }
  445. # Let CDN cache things if we can purge them.
  446. if ( $this->config->get( 'UseSquid' ) &&
  447. in_array(
  448. // Use PROTO_INTERNAL because that's what getCdnUrls() uses
  449. wfExpandUrl( $request->getRequestURL(), PROTO_INTERNAL ),
  450. $requestTitle->getCdnUrls()
  451. )
  452. ) {
  453. $output->setCdnMaxage( $this->config->get( 'SquidMaxage' ) );
  454. }
  455. $action->show();
  456. return;
  457. }
  458. // NOTE: deprecated hook. Add to $wgActions instead
  459. if ( Hooks::run(
  460. 'UnknownAction',
  461. [
  462. $request->getVal( 'action', 'view' ),
  463. $page
  464. ],
  465. '1.19'
  466. ) ) {
  467. $output->setStatusCode( 404 );
  468. $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
  469. }
  470. }
  471. /**
  472. * Run the current MediaWiki instance; index.php just calls this
  473. */
  474. public function run() {
  475. try {
  476. $this->setDBProfilingAgent();
  477. try {
  478. $this->main();
  479. } catch ( ErrorPageError $e ) {
  480. // T64091: while exceptions are convenient to bubble up GUI errors,
  481. // they are not internal application faults. As with normal requests, this
  482. // should commit, print the output, do deferred updates, jobs, and profiling.
  483. $this->doPreOutputCommit();
  484. $e->report(); // display the GUI error
  485. }
  486. } catch ( Exception $e ) {
  487. $context = $this->context;
  488. $action = $context->getRequest()->getVal( 'action', 'view' );
  489. if (
  490. $e instanceof DBConnectionError &&
  491. $context->hasTitle() &&
  492. $context->getTitle()->canExist() &&
  493. in_array( $action, [ 'view', 'history' ], true ) &&
  494. HTMLFileCache::useFileCache( $this->context, HTMLFileCache::MODE_OUTAGE )
  495. ) {
  496. // Try to use any (even stale) file during outages...
  497. $cache = new HTMLFileCache( $context->getTitle(), $action );
  498. if ( $cache->isCached() ) {
  499. $cache->loadFromFileCache( $context, HTMLFileCache::MODE_OUTAGE );
  500. print MWExceptionRenderer::getHTML( $e );
  501. exit;
  502. }
  503. }
  504. MWExceptionHandler::handleException( $e );
  505. } catch ( Error $e ) {
  506. // Type errors and such: at least handle it now and clean up the LBFactory state
  507. MWExceptionHandler::handleException( $e );
  508. }
  509. $this->doPostOutputShutdown( 'normal' );
  510. }
  511. private function setDBProfilingAgent() {
  512. $services = MediaWikiServices::getInstance();
  513. // Add a comment for easy SHOW PROCESSLIST interpretation
  514. $name = $this->context->getUser()->getName();
  515. $services->getDBLoadBalancerFactory()->setAgentName(
  516. mb_strlen( $name ) > 15 ? mb_substr( $name, 0, 15 ) . '...' : $name
  517. );
  518. }
  519. /**
  520. * @see MediaWiki::preOutputCommit()
  521. * @param callable $postCommitWork [default: null]
  522. * @since 1.26
  523. */
  524. public function doPreOutputCommit( callable $postCommitWork = null ) {
  525. self::preOutputCommit( $this->context, $postCommitWork );
  526. }
  527. /**
  528. * This function commits all DB changes as needed before
  529. * the user can receive a response (in case commit fails)
  530. *
  531. * @param IContextSource $context
  532. * @param callable $postCommitWork [default: null]
  533. * @since 1.27
  534. */
  535. public static function preOutputCommit(
  536. IContextSource $context, callable $postCommitWork = null
  537. ) {
  538. // Either all DBs should commit or none
  539. ignore_user_abort( true );
  540. $config = $context->getConfig();
  541. $request = $context->getRequest();
  542. $output = $context->getOutput();
  543. $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
  544. // Commit all changes
  545. $lbFactory->commitMasterChanges(
  546. __METHOD__,
  547. // Abort if any transaction was too big
  548. [ 'maxWriteDuration' => $config->get( 'MaxUserDBWriteDuration' ) ]
  549. );
  550. wfDebug( __METHOD__ . ': primary transaction round committed' );
  551. // Run updates that need to block the user or affect output (this is the last chance)
  552. DeferredUpdates::doUpdates( 'enqueue', DeferredUpdates::PRESEND );
  553. wfDebug( __METHOD__ . ': pre-send deferred updates completed' );
  554. // Should the client return, their request should observe the new ChronologyProtector
  555. // DB positions. This request might be on a foreign wiki domain, so synchronously update
  556. // the DB positions in all datacenters to be safe. If this output is not a redirect,
  557. // then OutputPage::output() will be relatively slow, meaning that running it in
  558. // $postCommitWork should help mask the latency of those updates.
  559. $flags = $lbFactory::SHUTDOWN_CHRONPROT_SYNC;
  560. $strategy = 'cookie+sync';
  561. $allowHeaders = !( $output->isDisabled() || headers_sent() );
  562. if ( $output->getRedirect() && $lbFactory->hasOrMadeRecentMasterChanges( INF ) ) {
  563. // OutputPage::output() will be fast, so $postCommitWork is useless for masking
  564. // the latency of synchronously updating the DB positions in all datacenters.
  565. // Try to make use of the time the client spends following redirects instead.
  566. $domainDistance = self::getUrlDomainDistance( $output->getRedirect() );
  567. if ( $domainDistance === 'local' && $allowHeaders ) {
  568. $flags = $lbFactory::SHUTDOWN_CHRONPROT_ASYNC;
  569. $strategy = 'cookie'; // use same-domain cookie and keep the URL uncluttered
  570. } elseif ( $domainDistance === 'remote' ) {
  571. $flags = $lbFactory::SHUTDOWN_CHRONPROT_ASYNC;
  572. $strategy = 'cookie+url'; // cross-domain cookie might not work
  573. }
  574. }
  575. // Record ChronologyProtector positions for DBs affected in this request at this point
  576. $cpIndex = null;
  577. $lbFactory->shutdown( $flags, $postCommitWork, $cpIndex );
  578. wfDebug( __METHOD__ . ': LBFactory shutdown completed' );
  579. if ( $cpIndex > 0 ) {
  580. if ( $allowHeaders ) {
  581. $expires = time() + ChronologyProtector::POSITION_TTL;
  582. $options = [ 'prefix' => '' ];
  583. $request->response()->setCookie( 'cpPosIndex', $cpIndex, $expires, $options );
  584. }
  585. if ( $strategy === 'cookie+url' ) {
  586. if ( $output->getRedirect() ) { // sanity
  587. $safeUrl = $lbFactory->appendShutdownCPIndexAsQuery(
  588. $output->getRedirect(),
  589. $cpIndex
  590. );
  591. $output->redirect( $safeUrl );
  592. } else {
  593. $e = new LogicException( "No redirect; cannot append cpPosIndex parameter." );
  594. MWExceptionHandler::logException( $e );
  595. }
  596. }
  597. }
  598. // Set a cookie to tell all CDN edge nodes to "stick" the user to the DC that handles this
  599. // POST request (e.g. the "master" data center). Also have the user briefly bypass CDN so
  600. // ChronologyProtector works for cacheable URLs.
  601. if ( $request->wasPosted() && $lbFactory->hasOrMadeRecentMasterChanges() ) {
  602. $expires = time() + $config->get( 'DataCenterUpdateStickTTL' );
  603. $options = [ 'prefix' => '' ];
  604. $request->response()->setCookie( 'UseDC', 'master', $expires, $options );
  605. $request->response()->setCookie( 'UseCDNCache', 'false', $expires, $options );
  606. }
  607. // Avoid letting a few seconds of replica DB lag cause a month of stale data. This logic is
  608. // also intimately related to the value of $wgCdnReboundPurgeDelay.
  609. if ( $lbFactory->laggedReplicaUsed() ) {
  610. $maxAge = $config->get( 'CdnMaxageLagged' );
  611. $output->lowerCdnMaxage( $maxAge );
  612. $request->response()->header( "X-Database-Lagged: true" );
  613. wfDebugLog( 'replication', "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. * @param string $url
  624. * @return string Either "local", "remote" if in the farm, "external" otherwise
  625. */
  626. private static function getUrlDomainDistance( $url ) {
  627. $clusterWiki = WikiMap::getWikiFromUrl( $url );
  628. if ( $clusterWiki === wfWikiID() ) {
  629. return 'local'; // the current wiki
  630. } elseif ( $clusterWiki !== false ) {
  631. return 'remote'; // another wiki in this cluster/farm
  632. }
  633. return 'external';
  634. }
  635. /**
  636. * This function does work that can be done *after* the
  637. * user gets the HTTP response so they don't block on it
  638. *
  639. * This manages deferred updates, job insertion,
  640. * final commit, and the logging of profiling data
  641. *
  642. * @param string $mode Use 'fast' to always skip job running
  643. * @since 1.26
  644. */
  645. public function doPostOutputShutdown( $mode = 'normal' ) {
  646. // Perform the last synchronous operations...
  647. try {
  648. // Record backend request timing
  649. $timing = $this->context->getTiming();
  650. $timing->mark( 'requestShutdown' );
  651. // Show visible profiling data if enabled (which cannot be post-send)
  652. Profiler::instance()->logDataPageOutputOnly();
  653. } catch ( Exception $e ) {
  654. // An error may already have been shown in run(), so just log it to be safe
  655. MWExceptionHandler::rollbackMasterChangesAndLog( $e );
  656. }
  657. $blocksHttpClient = true;
  658. // Defer everything else if possible...
  659. $callback = function () use ( $mode, &$blocksHttpClient ) {
  660. try {
  661. $this->restInPeace( $mode, $blocksHttpClient );
  662. } catch ( Exception $e ) {
  663. // If this is post-send, then displaying errors can cause broken HTML
  664. MWExceptionHandler::rollbackMasterChangesAndLog( $e );
  665. }
  666. };
  667. if ( function_exists( 'register_postsend_function' ) ) {
  668. // https://github.com/facebook/hhvm/issues/1230
  669. register_postsend_function( $callback );
  670. /** @noinspection PhpUnusedLocalVariableInspection */
  671. $blocksHttpClient = false;
  672. } else {
  673. if ( function_exists( 'fastcgi_finish_request' ) ) {
  674. fastcgi_finish_request();
  675. /** @noinspection PhpUnusedLocalVariableInspection */
  676. $blocksHttpClient = false;
  677. } else {
  678. // Either all DB and deferred updates should happen or none.
  679. // The latter should not be cancelled due to client disconnect.
  680. ignore_user_abort( true );
  681. }
  682. $callback();
  683. }
  684. }
  685. private function main() {
  686. global $wgTitle;
  687. $output = $this->context->getOutput();
  688. $request = $this->context->getRequest();
  689. // Send Ajax requests to the Ajax dispatcher.
  690. if ( $this->config->get( 'UseAjax' ) && $request->getVal( 'action' ) === 'ajax' ) {
  691. // Set a dummy title, because $wgTitle == null might break things
  692. $title = Title::makeTitle( NS_SPECIAL, 'Badtitle/performing an AJAX call in '
  693. . __METHOD__
  694. );
  695. $this->context->setTitle( $title );
  696. $wgTitle = $title;
  697. $dispatcher = new AjaxDispatcher( $this->config );
  698. $dispatcher->performAction( $this->context->getUser() );
  699. return;
  700. }
  701. // Get title from request parameters,
  702. // is set on the fly by parseTitle the first time.
  703. $title = $this->getTitle();
  704. $action = $this->getAction();
  705. $wgTitle = $title;
  706. // Set DB query expectations for this HTTP request
  707. $trxLimits = $this->config->get( 'TrxProfilerLimits' );
  708. $trxProfiler = Profiler::instance()->getTransactionProfiler();
  709. $trxProfiler->setLogger( LoggerFactory::getInstance( 'DBPerformance' ) );
  710. if ( $request->hasSafeMethod() ) {
  711. $trxProfiler->setExpectations( $trxLimits['GET'], __METHOD__ );
  712. } else {
  713. $trxProfiler->setExpectations( $trxLimits['POST'], __METHOD__ );
  714. }
  715. // If the user has forceHTTPS set to true, or if the user
  716. // is in a group requiring HTTPS, or if they have the HTTPS
  717. // preference set, redirect them to HTTPS.
  718. // Note: Do this after $wgTitle is setup, otherwise the hooks run from
  719. // isLoggedIn() will do all sorts of weird stuff.
  720. if (
  721. $request->getProtocol() == 'http' &&
  722. // switch to HTTPS only when supported by the server
  723. preg_match( '#^https://#', wfExpandUrl( $request->getRequestURL(), PROTO_HTTPS ) ) &&
  724. (
  725. $request->getSession()->shouldForceHTTPS() ||
  726. // Check the cookie manually, for paranoia
  727. $request->getCookie( 'forceHTTPS', '' ) ||
  728. // check for prefixed version that was used for a time in older MW versions
  729. $request->getCookie( 'forceHTTPS' ) ||
  730. // Avoid checking the user and groups unless it's enabled.
  731. (
  732. $this->context->getUser()->isLoggedIn()
  733. && $this->context->getUser()->requiresHTTPS()
  734. )
  735. )
  736. ) {
  737. $oldUrl = $request->getFullRequestURL();
  738. $redirUrl = preg_replace( '#^http://#', 'https://', $oldUrl );
  739. // ATTENTION: This hook is likely to be removed soon due to overall design of the system.
  740. if ( Hooks::run( 'BeforeHttpsRedirect', [ $this->context, &$redirUrl ] ) ) {
  741. if ( $request->wasPosted() ) {
  742. // This is weird and we'd hope it almost never happens. This
  743. // means that a POST came in via HTTP and policy requires us
  744. // redirecting to HTTPS. It's likely such a request is going
  745. // to fail due to post data being lost, but let's try anyway
  746. // and just log the instance.
  747. // @todo FIXME: See if we could issue a 307 or 308 here, need
  748. // to see how clients (automated & browser) behave when we do
  749. wfDebugLog( 'RedirectedPosts', "Redirected from HTTP to HTTPS: $oldUrl" );
  750. }
  751. // Setup dummy Title, otherwise OutputPage::redirect will fail
  752. $title = Title::newFromText( 'REDIR', NS_MAIN );
  753. $this->context->setTitle( $title );
  754. // Since we only do this redir to change proto, always send a vary header
  755. $output->addVaryHeader( 'X-Forwarded-Proto' );
  756. $output->redirect( $redirUrl );
  757. $output->output();
  758. return;
  759. }
  760. }
  761. if ( $title->canExist() && HTMLFileCache::useFileCache( $this->context ) ) {
  762. // Try low-level file cache hit
  763. $cache = new HTMLFileCache( $title, $action );
  764. if ( $cache->isCacheGood( /* Assume up to date */ ) ) {
  765. // Check incoming headers to see if client has this cached
  766. $timestamp = $cache->cacheTimestamp();
  767. if ( !$output->checkLastModified( $timestamp ) ) {
  768. $cache->loadFromFileCache( $this->context );
  769. }
  770. // Do any stats increment/watchlist stuff, assuming user is viewing the
  771. // latest revision (which should always be the case for file cache)
  772. $this->context->getWikiPage()->doViewUpdates( $this->context->getUser() );
  773. // Tell OutputPage that output is taken care of
  774. $output->disable();
  775. return;
  776. }
  777. }
  778. // Actually do the work of the request and build up any output
  779. $this->performRequest();
  780. // GUI-ify and stash the page output in MediaWiki::doPreOutputCommit() while
  781. // ChronologyProtector synchronizes DB positions or replicas accross all datacenters.
  782. $buffer = null;
  783. $outputWork = function () use ( $output, &$buffer ) {
  784. if ( $buffer === null ) {
  785. $buffer = $output->output( true );
  786. }
  787. return $buffer;
  788. };
  789. // Now commit any transactions, so that unreported errors after
  790. // output() don't roll back the whole DB transaction and so that
  791. // we avoid having both success and error text in the response
  792. $this->doPreOutputCommit( $outputWork );
  793. // Now send the actual output
  794. print $outputWork();
  795. }
  796. /**
  797. * Ends this task peacefully
  798. * @param string $mode Use 'fast' to always skip job running
  799. * @param bool $blocksHttpClient Whether this blocks an HTTP response to a client
  800. */
  801. public function restInPeace( $mode = 'fast', $blocksHttpClient = true ) {
  802. $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
  803. // Assure deferred updates are not in the main transaction
  804. $lbFactory->commitMasterChanges( __METHOD__ );
  805. // Loosen DB query expectations since the HTTP client is unblocked
  806. $trxProfiler = Profiler::instance()->getTransactionProfiler();
  807. $trxProfiler->resetExpectations();
  808. $trxProfiler->setExpectations(
  809. $this->context->getRequest()->hasSafeMethod()
  810. ? $this->config->get( 'TrxProfilerLimits' )['PostSend-GET']
  811. : $this->config->get( 'TrxProfilerLimits' )['PostSend-POST'],
  812. __METHOD__
  813. );
  814. // Important: this must be the last deferred update added (T100085, T154425)
  815. DeferredUpdates::addCallableUpdate( [ JobQueueGroup::class, 'pushLazyJobs' ] );
  816. // Do any deferred jobs; preferring to run them now if a client will not wait on them
  817. DeferredUpdates::doUpdates( $blocksHttpClient ? 'enqueue' : 'run' );
  818. // Now that everything specific to this request is done,
  819. // try to occasionally run jobs (if enabled) from the queues
  820. if ( $mode === 'normal' ) {
  821. $this->triggerJobs();
  822. }
  823. // Log profiling data, e.g. in the database or UDP
  824. wfLogProfilingData();
  825. // Commit and close up!
  826. $lbFactory->commitMasterChanges( __METHOD__ );
  827. $lbFactory->shutdown( LBFactory::SHUTDOWN_NO_CHRONPROT );
  828. wfDebug( "Request ended normally\n" );
  829. }
  830. /**
  831. * Send out any buffered statsd data according to sampling rules
  832. *
  833. * @param IBufferingStatsdDataFactory $stats
  834. * @param Config $config
  835. * @throws ConfigException
  836. * @since 1.31
  837. */
  838. public static function emitBufferedStatsdData(
  839. IBufferingStatsdDataFactory $stats, Config $config
  840. ) {
  841. if ( $config->get( 'StatsdServer' ) && $stats->hasData() ) {
  842. try {
  843. $statsdServer = explode( ':', $config->get( 'StatsdServer' ) );
  844. $statsdHost = $statsdServer[0];
  845. $statsdPort = isset( $statsdServer[1] ) ? $statsdServer[1] : 8125;
  846. $statsdSender = new SocketSender( $statsdHost, $statsdPort );
  847. $statsdClient = new SamplingStatsdClient( $statsdSender, true, false );
  848. $statsdClient->setSamplingRates( $config->get( 'StatsdSamplingRates' ) );
  849. $statsdClient->send( $stats->getData() );
  850. $stats->clearData(); // empty buffer for the next round
  851. } catch ( Exception $ex ) {
  852. MWExceptionHandler::logException( $ex );
  853. }
  854. }
  855. }
  856. /**
  857. * Potentially open a socket and sent an HTTP request back to the server
  858. * to run a specified number of jobs. This registers a callback to cleanup
  859. * the socket once it's done.
  860. */
  861. public function triggerJobs() {
  862. $jobRunRate = $this->config->get( 'JobRunRate' );
  863. if ( $this->getTitle()->isSpecial( 'RunJobs' ) ) {
  864. return; // recursion guard
  865. } elseif ( $jobRunRate <= 0 || wfReadOnly() ) {
  866. return;
  867. }
  868. if ( $jobRunRate < 1 ) {
  869. $max = mt_getrandmax();
  870. if ( mt_rand( 0, $max ) > $max * $jobRunRate ) {
  871. return; // the higher the job run rate, the less likely we return here
  872. }
  873. $n = 1;
  874. } else {
  875. $n = intval( $jobRunRate );
  876. }
  877. $logger = LoggerFactory::getInstance( 'runJobs' );
  878. try {
  879. if ( $this->config->get( 'RunJobsAsync' ) ) {
  880. // Send an HTTP request to the job RPC entry point if possible
  881. $invokedWithSuccess = $this->triggerAsyncJobs( $n, $logger );
  882. if ( !$invokedWithSuccess ) {
  883. // Fall back to blocking on running the job(s)
  884. $logger->warning( "Jobs switched to blocking; Special:RunJobs disabled" );
  885. $this->triggerSyncJobs( $n, $logger );
  886. }
  887. } else {
  888. $this->triggerSyncJobs( $n, $logger );
  889. }
  890. } catch ( JobQueueError $e ) {
  891. // Do not make the site unavailable (T88312)
  892. MWExceptionHandler::logException( $e );
  893. }
  894. }
  895. /**
  896. * @param int $n Number of jobs to try to run
  897. * @param LoggerInterface $runJobsLogger
  898. */
  899. private function triggerSyncJobs( $n, LoggerInterface $runJobsLogger ) {
  900. $runner = new JobRunner( $runJobsLogger );
  901. $runner->run( [ 'maxJobs' => $n ] );
  902. }
  903. /**
  904. * @param int $n Number of jobs to try to run
  905. * @param LoggerInterface $runJobsLogger
  906. * @return bool Success
  907. */
  908. private function triggerAsyncJobs( $n, LoggerInterface $runJobsLogger ) {
  909. // Do not send request if there are probably no jobs
  910. $group = JobQueueGroup::singleton();
  911. if ( !$group->queuesHaveJobs( JobQueueGroup::TYPE_DEFAULT ) ) {
  912. return true;
  913. }
  914. $query = [ 'title' => 'Special:RunJobs',
  915. 'tasks' => 'jobs', 'maxjobs' => $n, 'sigexpiry' => time() + 5 ];
  916. $query['signature'] = SpecialRunJobs::getQuerySignature(
  917. $query, $this->config->get( 'SecretKey' ) );
  918. $errno = $errstr = null;
  919. $info = wfParseUrl( $this->config->get( 'CanonicalServer' ) );
  920. $host = $info ? $info['host'] : null;
  921. $port = 80;
  922. if ( isset( $info['scheme'] ) && $info['scheme'] == 'https' ) {
  923. $host = "tls://" . $host;
  924. $port = 443;
  925. }
  926. if ( isset( $info['port'] ) ) {
  927. $port = $info['port'];
  928. }
  929. Wikimedia\suppressWarnings();
  930. $sock = $host ? fsockopen(
  931. $host,
  932. $port,
  933. $errno,
  934. $errstr,
  935. // If it takes more than 100ms to connect to ourselves there is a problem...
  936. 0.100
  937. ) : false;
  938. Wikimedia\restoreWarnings();
  939. $invokedWithSuccess = true;
  940. if ( $sock ) {
  941. $special = SpecialPageFactory::getPage( 'RunJobs' );
  942. $url = $special->getPageTitle()->getCanonicalURL( $query );
  943. $req = (
  944. "POST $url HTTP/1.1\r\n" .
  945. "Host: {$info['host']}\r\n" .
  946. "Connection: Close\r\n" .
  947. "Content-Length: 0\r\n\r\n"
  948. );
  949. $runJobsLogger->info( "Running $n job(s) via '$url'" );
  950. // Send a cron API request to be performed in the background.
  951. // Give up if this takes too long to send (which should be rare).
  952. stream_set_timeout( $sock, 2 );
  953. $bytes = fwrite( $sock, $req );
  954. if ( $bytes !== strlen( $req ) ) {
  955. $invokedWithSuccess = false;
  956. $runJobsLogger->error( "Failed to start cron API (socket write error)" );
  957. } else {
  958. // Do not wait for the response (the script should handle client aborts).
  959. // Make sure that we don't close before that script reaches ignore_user_abort().
  960. $start = microtime( true );
  961. $status = fgets( $sock );
  962. $sec = microtime( true ) - $start;
  963. if ( !preg_match( '#^HTTP/\d\.\d 202 #', $status ) ) {
  964. $invokedWithSuccess = false;
  965. $runJobsLogger->error( "Failed to start cron API: received '$status' ($sec)" );
  966. }
  967. }
  968. fclose( $sock );
  969. } else {
  970. $invokedWithSuccess = false;
  971. $runJobsLogger->error( "Failed to start cron API (socket error $errno): $errstr" );
  972. }
  973. return $invokedWithSuccess;
  974. }
  975. }