Turn Title::isNewPage() into something useful by caching its result and preload it...
authorAlexandre Emsenhuber <ialex@users.mediawiki.org>
Sun, 1 Jan 2012 12:23:17 +0000 (12:23 +0000)
committerAlexandre Emsenhuber <ialex@users.mediawiki.org>
Sun, 1 Jan 2012 12:23:17 +0000 (12:23 +0000)
includes/Title.php

index 87f97b6..5d62ca8 100644 (file)
@@ -64,6 +64,7 @@ class Title {
        var $mArticleID = -1;             // /< Article ID, fetched from the link cache on demand
        var $mLatestID = false;           // /< ID of most recent revision
        var $mCounter = -1;               // /< Number of times this page has been viewed (-1 means "not loaded")
+       private $mIsNew;                  // /< Whether this is a "new page" (i.e. it has only one revision)
        private $mEstimateRevisions;      // /< Estimated number of revisions; null of not loaded
        var $mRestrictions = array();     // /< Array of groups allowed to edit this article
        var $mOldRestrictions = false;
@@ -276,6 +277,8 @@ class Title {
                                $this->mLatestID = (int)$row->page_latest;
                        if ( isset( $row->page_counter ) )
                                $this->mCounter = (int)$row->page_counter;
+                       if ( isset( $row->page_is_new ) )
+                               $this->mIsNew = (bool)$row->page_is_new;
                } else { // page not found
                        $this->mArticleID = 0;
                        $this->mLength = 0;
@@ -2783,6 +2786,27 @@ class Title {
                return $this->mCounter;
        }
 
+       /**
+        * Check if this is a new page (i.e. it has only one revision)
+        *
+        * @return bool
+        */
+       public function isNewPage() {
+               if ( $this->mIsNew === null ) {
+                       if ( $this->exists() ) {
+                               $dbr = wfGetDB( DB_SLAVE );
+                               $this->mIsNew = (bool)$dbr->selectField( 'page',
+                                       'page_is_new',
+                                       array( 'page_id' => $this->getArticleID() ),
+                                       __METHOD__
+                               );
+                       } else {
+                               $this->mIsNew = false;
+                       }
+               }
+               return $this->mIsNew;
+       }
+
        /**
         * Get the article ID for this Title from the link cache,
         * adding it if necessary
@@ -3991,16 +4015,6 @@ class Title {
                return $rev ? $rev->getTimestamp() : null;
        }
 
-       /**
-        * Check if this is a new page
-        *
-        * @return bool
-        */
-       public function isNewPage() {
-               $dbr = wfGetDB( DB_SLAVE );
-               return (bool)$dbr->selectField( 'page', 'page_is_new', $this->pageCond(), __METHOD__ );
-       }
-
        /**
         * Check whether the number of revisions of this page surpasses $wgDeleteRevisionsLimit
         *