MediaWiki.php 36 KB

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