Reuse the WikiPage object to save a db query
authorBrian Wolff <bawolff+wn@gmail.com>
Fri, 4 Jan 2013 09:11:13 +0000 (05:11 -0400)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 4 Jan 2013 13:05:09 +0000 (13:05 +0000)
Early in the request we call Action::getActionName to get the action
name for HTMLFileCache to see if the current action is cached.
This causes data to be loaded from the WikiPage object
stored in the main request context. However, later when
we're initializing the Article object, we overwrite that
WikiPage object with the one used by the Article object.
Later on the WikiPage object has to be re-loaded, and
hence the exact same db request is run twice in one request,
which seems wasteful.

Instead, initialize the Article object using the WikiPage
object from the context, in order to save the already
loaded data.

Change-Id: I292f0d70feb505fae5fa955fd735d85ad3b22fea

includes/Wiki.php

index d4840cc..2b12dd7 100644 (file)
@@ -331,8 +331,18 @@ class MediaWiki {
                wfProfileIn( __METHOD__ );
 
                $title = $this->context->getTitle();
-               $article = Article::newFromTitle( $title, $this->context );
-               $this->context->setWikiPage( $article->getPage() );
+               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() );
+               }
+
                // NS_MEDIAWIKI has no redirects.
                // It is also used for CSS/JS, so performance matters here...
                if ( $title->getNamespace() == NS_MEDIAWIKI ) {