Moved view count from WikiPage to Title; avoids an extra DB query when showing the...
authorAlexandre Emsenhuber <ialex@users.mediawiki.org>
Sun, 11 Dec 2011 11:30:11 +0000 (11:30 +0000)
committerAlexandre Emsenhuber <ialex@users.mediawiki.org>
Sun, 11 Dec 2011 11:30:11 +0000 (11:30 +0000)
includes/SkinTemplate.php
includes/Title.php
includes/WikiPage.php

index fe801c5..56413fd 100644 (file)
@@ -330,9 +330,8 @@ class SkinTemplate extends Skin {
                $tpl->set( 'numberofwatchingusers', false );
                if ( $out->isArticle() && $title->exists() ) {
                        if ( $this->isRevisionCurrent() ) {
-                               $page = WikiPage::factory( $title );
                                if ( !$wgDisableCounters ) {
-                                       $viewcount = $page->getCount();
+                                       $viewcount = $title->getCount();
                                        if ( $viewcount ) {
                                                $tpl->set( 'viewcount', $this->msg( 'viewcount' )->numParams( $viewcount )->parse() );
                                        }
@@ -352,7 +351,8 @@ class SkinTemplate extends Skin {
                                }
 
                                if ( $wgMaxCredits != 0 ) {
-                                       $tpl->set( 'credits', Action::factory( 'credits', $page, $this->getContext() )->getCredits( $wgMaxCredits, $wgShowCreditsIfMax ) );
+                                       $tpl->set( 'credits', Action::factory( 'credits', WikiPage::factory( $title ),
+                                               $this->getContext() )->getCredits( $wgMaxCredits, $wgShowCreditsIfMax ) );
                                } else {
                                        $tpl->set( 'lastmod', $this->lastModified() );
                                }
index 28589c7..2caa5ca 100644 (file)
@@ -63,6 +63,7 @@ class Title {
        var $mFragment;                   // /< Title fragment (i.e. the bit after the #)
        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")
        var $mRestrictions = array();     // /< Array of groups allowed to edit this article
        var $mOldRestrictions = false;
        var $mCascadeRestriction;         ///< Cascade restrictions on this page to included templates and images?
@@ -272,11 +273,14 @@ class Title {
                                $this->mRedirect = (bool)$row->page_is_redirect;
                        if ( isset( $row->page_latest ) )
                                $this->mLatestID = (int)$row->page_latest;
+                       if ( isset( $row->page_counter ) )
+                               $this->mCounter = (int)$row->page_counter;
                } else { // page not found
                        $this->mArticleID = 0;
                        $this->mLength = 0;
                        $this->mRedirect = false;
                        $this->mLatestID = 0;
+                       $this->mCounter = 0;
                }
        }
 
@@ -2516,6 +2520,28 @@ class Title {
                return $deleted;
        }
 
+       /**
+        * Get the number of views of this page
+        *
+        * @return int The view count for the page
+        */
+       public function getCount() {
+               if ( $this->mCounter == -1 ) {
+                       if ( $this->exists() ) {
+                               $dbr = wfGetDB( DB_SLAVE );
+                               $this->mCounter = $dbr->selectField( 'page',
+                                       'page_counter',
+                                       array( 'page_id' => $this->getArticleID() ),
+                                       __METHOD__
+                               );
+                       } else {
+                               $this->mCounter = 0;
+                       }
+               }
+
+               return $this->mCounter;
+       }
+
        /**
         * Get the article ID for this Title from the link cache,
         * adding it if necessary
@@ -2628,6 +2654,7 @@ class Title {
                $this->mRedirect = null;
                $this->mLength = -1;
                $this->mLatestID = false;
+               $this->mCounter = -1;
        }
 
        /**
index 6de1e35..e5dbb0a 100644 (file)
@@ -21,7 +21,6 @@ class WikiPage extends Page {
        /**@{{
         * @protected
         */
-       public $mCounter = -1;               // !< Integer (-1 means "not loaded")
        public $mDataLoaded = false;         // !< Boolean
        public $mIsRedirect = false;         // !< Boolean
        public $mLatest = false;             // !< Integer (false means "not loaded")
@@ -240,7 +239,6 @@ class WikiPage extends Page {
        public function clear() {
                $this->mDataLoaded = false;
 
-               $this->mCounter = -1; # Not loaded
                $this->mRedirectTarget = null; # Title object if set
                $this->mLastRevision = null; # Latest revision
                $this->mTimestamp = '';
@@ -380,7 +378,6 @@ class WikiPage extends Page {
                        # Old-fashioned restrictions
                        $this->mTitle->loadRestrictions( $data->page_restrictions );
 
-                       $this->mCounter     = intval( $data->page_counter );
                        $this->mTouched     = wfTimestamp( TS_MW, $data->page_touched );
                        $this->mIsRedirect  = intval( $data->page_is_redirect );
                        $this->mLatest      = intval( $data->page_latest );
@@ -420,25 +417,12 @@ class WikiPage extends Page {
        }
 
        /**
+        * Get the number of views of this page
+        *
         * @return int The view count for the page
         */
        public function getCount() {
-               if ( -1 == $this->mCounter ) {
-                       $id = $this->getId();
-
-                       if ( $id == 0 ) {
-                               $this->mCounter = 0;
-                       } else {
-                               $dbr = wfGetDB( DB_SLAVE );
-                               $this->mCounter = $dbr->selectField( 'page',
-                                       'page_counter',
-                                       array( 'page_id' => $id ),
-                                       __METHOD__
-                               );
-                       }
-               }
-
-               return $this->mCounter;
+               return $this->mTitle->getCount();
        }
 
        /**