Maintain the page ID in WikiPage instead of relying on Title
authorAlexandre Emsenhuber <ialex.wiki@gmail.com>
Thu, 24 Jan 2013 19:43:15 +0000 (20:43 +0100)
committerAlexandre Emsenhuber <ialex.wiki@gmail.com>
Thu, 21 Feb 2013 07:30:15 +0000 (08:30 +0100)
Also updated WikiPage methods to use it instead of calling Title's
methods directly.

This allows to win a database query for most web requests because
Action::factory() calls WikiPage::getActionOverrides() which will
call exists() through getContentHandler() and getContentModel().
This call to exists() trigerred the object's load from the database,
but this only affected the Title object, so WikiPage needed to load
itself from the database too.
Now this call to exists() will load the WikiPage object, which
populates the LinkCache too.

Change-Id: Iea16d6726ddc2356a25011fed9f2a8dbae3b7b42

includes/WikiPage.php

index 6048294..c8e76aa 100644 (file)
@@ -50,6 +50,11 @@ class WikiPage implements Page, IDBAccessObject {
        public $mPreparedEdit = false;       // !< Array
        /**@}}*/
 
+       /**
+        * @var int
+        */
+       protected $mId = null;
+
        /**
         * @var int; one of the READ_* constants
         */
@@ -228,6 +233,7 @@ class WikiPage implements Page, IDBAccessObject {
         * @return void
         */
        protected function clearCacheFields() {
+               $this->mId = null;
                $this->mCounter = null;
                $this->mRedirectTarget = null; // Title object if set
                $this->mLastRevision = null; // Latest revision
@@ -380,6 +386,7 @@ class WikiPage implements Page, IDBAccessObject {
                        // Old-fashioned restrictions
                        $this->mTitle->loadRestrictions( $data->page_restrictions );
 
+                       $this->mId          = intval( $data->page_id );
                        $this->mCounter     = intval( $data->page_counter );
                        $this->mTouched     = wfTimestamp( TS_MW, $data->page_touched );
                        $this->mIsRedirect  = intval( $data->page_is_redirect );
@@ -396,6 +403,8 @@ class WikiPage implements Page, IDBAccessObject {
                        $this->mTitle->loadFromRow( false );
 
                        $this->clearCacheFields();
+
+                       $this->mId = 0;
                }
 
                $this->mDataLoaded = true;
@@ -406,14 +415,20 @@ class WikiPage implements Page, IDBAccessObject {
         * @return int Page ID
         */
        public function getId() {
-               return $this->mTitle->getArticleID();
+               if ( !$this->mDataLoaded ) {
+                       $this->loadPageData();
+               }
+               return $this->mId;
        }
 
        /**
         * @return bool Whether or not the page exists in the database
         */
        public function exists() {
-               return $this->mTitle->exists();
+               if ( !$this->mDataLoaded ) {
+                       $this->loadPageData();
+               }
+               return $this->mId > 0;
        }
 
        /**
@@ -425,7 +440,7 @@ class WikiPage implements Page, IDBAccessObject {
         * @return bool
         */
        public function hasViewableContent() {
-               return $this->mTitle->exists() || $this->mTitle->isAlwaysKnown();
+               return $this->exists() || $this->mTitle->isAlwaysKnown();
        }
 
        /**
@@ -1067,7 +1082,7 @@ class WikiPage implements Page, IDBAccessObject {
 
                return $wgEnableParserCache
                        && $parserOptions->getStubThreshold() == 0
-                       && $this->mTitle->exists()
+                       && $this->exists()
                        && ( $oldid === null || $oldid === 0 || $oldid === $this->getLatest() )
                        && $this->getContentHandler()->isParserCacheSupported();
        }
@@ -1123,7 +1138,7 @@ class WikiPage implements Page, IDBAccessObject {
                }
 
                // Don't update page view counters on views from bot users (bug 14044)
-               if ( !$wgDisableCounters && !$user->isAllowed( 'bot' ) && $this->mTitle->exists() ) {
+               if ( !$wgDisableCounters && !$user->isAllowed( 'bot' ) && $this->exists() ) {
                        DeferredUpdates::addUpdate( new ViewCountUpdate( $this->getId() ) );
                        DeferredUpdates::addUpdate( new SiteStatsUpdate( 1, 0, 0 ) );
                }
@@ -1160,7 +1175,7 @@ class WikiPage implements Page, IDBAccessObject {
                if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
                        // @todo: move this logic to MessageCache
 
-                       if ( $this->mTitle->exists() ) {
+                       if ( $this->exists() ) {
                                // NOTE: use transclusion text for messages.
                                //       This is consistent with  MessageCache::getMsgFromNamespace()
 
@@ -1209,6 +1224,7 @@ class WikiPage implements Page, IDBAccessObject {
 
                if ( $affected ) {
                        $newid = $dbw->insertId();
+                       $this->mId = $newid;
                        $this->mTitle->resetArticleID( $newid );
                }
                wfProfileOut( __METHOD__ );
@@ -1506,7 +1522,7 @@ class WikiPage implements Page, IDBAccessObject {
         */
        function checkFlags( $flags ) {
                if ( !( $flags & EDIT_NEW ) && !( $flags & EDIT_UPDATE ) ) {
-                       if ( $this->mTitle->getArticleID() ) {
+                       if ( $this->exists() ) {
                                $flags |= EDIT_UPDATE;
                        } else {
                                $flags |= EDIT_NEW;
@@ -2068,7 +2084,7 @@ class WikiPage implements Page, IDBAccessObject {
                        }
                }
 
-               if ( !$this->mTitle->exists() ) {
+               if ( !$this->exists() ) {
                        wfProfileOut( __METHOD__ );
                        return;
                }
@@ -2207,7 +2223,7 @@ class WikiPage implements Page, IDBAccessObject {
 
                $restrictionTypes = $this->mTitle->getRestrictionTypes();
 
-               $id = $this->mTitle->getArticleID();
+               $id = $this->getId();
 
                if ( !$cascade ) {
                        $cascade = false;
@@ -2588,7 +2604,7 @@ class WikiPage implements Page, IDBAccessObject {
 
                // Now that it's safely backed up, delete it
                $dbw->delete( 'page', array( 'page_id' => $id ), __METHOD__ );
-               $ok = ( $dbw->affectedRows() > 0 ); // getArticleID() uses slave, could be laggy
+               $ok = ( $dbw->affectedRows() > 0 ); // $id could be laggy
 
                if ( !$ok ) {
                        $dbw->rollback( __METHOD__ );
@@ -2635,11 +2651,8 @@ class WikiPage implements Page, IDBAccessObject {
                // Clear caches
                WikiPage::onArticleDelete( $this->mTitle );
 
-               // Reset this object
-               $this->clear();
-
-               // Clear the cached article id so the interface doesn't act like we exist
-               $this->mTitle->resetArticleID( 0 );
+               // Reset this object and the Title object
+               $this->loadFromRow( false, self::READ_LATEST );
        }
 
        /**
@@ -2943,7 +2956,7 @@ class WikiPage implements Page, IDBAccessObject {
         */
        public function getHiddenCategories() {
                $result = array();
-               $id = $this->mTitle->getArticleID();
+               $id = $this->getId();
 
                if ( $id == 0 ) {
                        return array();
@@ -3078,7 +3091,7 @@ class WikiPage implements Page, IDBAccessObject {
                // are visible.
 
                // Get templates from templatelinks
-               $id = $this->mTitle->getArticleID();
+               $id = $this->getId();
 
                $tlTemplates = array();