Use global cache keys login/create account rate limitting
[lhc/web/wiklou.git] / includes / MediaWiki.php
index 0e3a734..7f2f737 100644 (file)
@@ -247,29 +247,31 @@ class MediaWiki {
                        // Prevent information leak via Special:MyPage et al (T109724)
                        if ( $title->isSpecialPage() ) {
                                $specialPage = SpecialPageFactory::getPage( $title->getDBKey() );
-                               if ( $specialPage instanceof RedirectSpecialPage
-                                       && $this->config->get( 'HideIdentifiableRedirects' )
-                                       && $specialPage->personallyIdentifiableTarget()
-                               ) {
-                                       list( , $subpage ) = SpecialPageFactory::resolveAlias( $title->getDBKey() );
-                                       $target = $specialPage->getRedirect( $subpage );
-                                       // target can also be true. We let that case fall through to normal processing.
-                                       if ( $target instanceof Title ) {
-                                               $query = $specialPage->getRedirectQuery() ?: array();
-                                               $request = new DerivativeRequest( $this->context->getRequest(), $query );
-                                               $request->setRequestURL( $this->context->getRequest()->getRequestURL() );
-                                               $this->context->setRequest( $request );
-                                               // Do not varnish cache these. May vary even for anons
-                                               $this->context->getOutput()->lowerCdnMaxage( 0 );
-                                               $this->context->setTitle( $target );
-                                               $wgTitle = $target;
-                                               // Reset action type cache. (Special pages have only view)
-                                               $this->action = null;
-                                               $title = $target;
-                                               $output->addJsConfigVars( array(
-                                                       'wgInternalRedirectTargetUrl' => $target->getFullURL( $query ),
-                                               ) );
-                                               $output->addModules( 'mediawiki.action.view.redirect' );
+                               if ( $specialPage instanceof RedirectSpecialPage ) {
+                                       $specialPage->setContext( $this->context );
+                                       if ( $this->config->get( 'HideIdentifiableRedirects' )
+                                               && $specialPage->personallyIdentifiableTarget()
+                                       ) {
+                                               list( , $subpage ) = SpecialPageFactory::resolveAlias( $title->getDBKey() );
+                                               $target = $specialPage->getRedirect( $subpage );
+                                               // target can also be true. We let that case fall through to normal processing.
+                                               if ( $target instanceof Title ) {
+                                                       $query = $specialPage->getRedirectQuery() ?: array();
+                                                       $request = new DerivativeRequest( $this->context->getRequest(), $query );
+                                                       $request->setRequestURL( $this->context->getRequest()->getRequestURL() );
+                                                       $this->context->setRequest( $request );
+                                                       // Do not varnish cache these. May vary even for anons
+                                                       $this->context->getOutput()->lowerCdnMaxage( 0 );
+                                                       $this->context->setTitle( $target );
+                                                       $wgTitle = $target;
+                                                       // Reset action type cache. (Special pages have only view)
+                                                       $this->action = null;
+                                                       $title = $target;
+                                                       $output->addJsConfigVars( array(
+                                                               'wgInternalRedirectTargetUrl' => $target->getFullURL( $query ),
+                                                       ) );
+                                                       $output->addModules( 'mediawiki.action.view.redirect' );
+                                               }
                                        }
                                }
                        }
@@ -379,23 +381,26 @@ class MediaWiki {
         * Initialize the main Article object for "standard" actions (view, etc)
         * Create an Article object for the page, following redirects if needed.
         *
-        * @return mixed An Article, or a string to redirect to another URL
+        * @return Article|string An Article, or a string to redirect to another URL
         */
        private function initializeArticle() {
-
                $title = $this->context->getTitle();
                if ( $this->context->canUseWikiPage() ) {
                        // Try to use request context wiki page, as there
                        // is already data from db saved in per process
                        // cache there from this->getAction() call.
                        $page = $this->context->getWikiPage();
-                       $article = Article::newFromWikiPage( $page, $this->context );
                } else {
                        // This case should not happen, but just in case.
-                       $article = Article::newFromTitle( $title, $this->context );
-                       $this->context->setWikiPage( $article->getPage() );
+                       // @TODO: remove this or use an exception
+                       $page = WikiPage::factory( $title );
+                       $this->context->setWikiPage( $page );
+                       wfWarn( "RequestContext::canUseWikiPage() returned false" );
                }
 
+               // Make GUI wrapper for the WikiPage
+               $article = Article::newFromWikiPage( $page, $this->context );
+
                // Skip some unnecessary code if the content model doesn't support redirects
                if ( !ContentHandler::getForTitle( $title )->supportsRedirects() ) {
                        return $article;
@@ -406,7 +411,7 @@ class MediaWiki {
                // Namespace might change when using redirects
                // Check for redirects ...
                $action = $request->getVal( 'action', 'view' );
-               $file = ( $title->getNamespace() == NS_FILE ) ? $article->getFile() : null;
+               $file = ( $page instanceof WikiFilePage ) ? $page->getFile() : null;
                if ( ( $action == 'view' || $action == 'render' ) // ... for actions that show content
                        && !$request->getVal( 'oldid' ) // ... and are not old revisions
                        && !$request->getVal( 'diff' ) // ... and not when showing diff
@@ -419,12 +424,13 @@ class MediaWiki {
 
                        Hooks::run( 'InitializeArticleMaybeRedirect',
                                array( &$title, &$request, &$ignoreRedirect, &$target, &$article ) );
+                       $page = $article->getPage(); // reflect any hook changes
 
                        // Follow redirects only for... redirects.
                        // If $target is set, then a hook wanted to redirect.
-                       if ( !$ignoreRedirect && ( $target || $article->isRedirect() ) ) {
+                       if ( !$ignoreRedirect && ( $target || $page->isRedirect() ) ) {
                                // Is the target already set by an extension?
-                               $target = $target ? $target : $article->followRedirect();
+                               $target = $target ? $target : $page->followRedirect();
                                if ( is_string( $target ) ) {
                                        if ( !$this->config->get( 'DisableHardRedirects' ) ) {
                                                // we'll need to redirect
@@ -433,16 +439,19 @@ class MediaWiki {
                                }
                                if ( is_object( $target ) ) {
                                        // Rewrite environment to redirected article
-                                       $rarticle = Article::newFromTitle( $target, $this->context );
-                                       $rarticle->loadPageData();
-                                       if ( $rarticle->exists() || ( is_object( $file ) && !$file->isLocal() ) ) {
+                                       $rpage = WikiPage::factory( $target );
+                                       $rpage->loadPageData();
+                                       if ( $rpage->exists() || ( is_object( $file ) && !$file->isLocal() ) ) {
+                                               $rarticle = Article::newFromWikiPage( $rpage, $this->context );
                                                $rarticle->setRedirectedFrom( $title );
+
                                                $article = $rarticle;
                                                $this->context->setTitle( $target );
                                                $this->context->setWikiPage( $article->getPage() );
                                        }
                                }
                        } else {
+                               // Article may have been changed by hook
                                $this->context->setTitle( $article->getTitle() );
                                $this->context->setWikiPage( $article->getPage() );
                        }
@@ -544,21 +553,12 @@ class MediaWiki {
                $config = $context->getConfig();
 
                $factory = wfGetLBFactory();
-               // Check if any transaction was too big
-               $limit = $config->get( 'MaxUserDBWriteDuration' );
-               $factory->forEachLB( function ( LoadBalancer $lb ) use ( $limit ) {
-                       $lb->forEachOpenConnection( function ( IDatabase $db ) use ( $limit ) {
-                               $time = $db->pendingWriteQueryDuration();
-                               if ( $limit > 0 && $time > $limit ) {
-                                       throw new DBTransactionError(
-                                               $db,
-                                               wfMessage( 'transaction-duration-limit-exceeded', $time, $limit )->text()
-                                       );
-                               }
-                       } );
-               } );
                // Commit all changes
-               $factory->commitMasterChanges( __METHOD__ );
+               $factory->commitMasterChanges(
+                       __METHOD__,
+                       // Abort if any transaction was too big
+                       array( 'maxWriteDuration' => $config->get( 'MaxUserDBWriteDuration' ) )
+               );
                // Record ChronologyProtector positions
                $factory->shutdown();
                wfDebug( __METHOD__ . ': all transactions committed' );