* Whitespace fixes
authorNiklas Laxström <nikerabbit@users.mediawiki.org>
Sun, 10 Aug 2008 10:30:52 +0000 (10:30 +0000)
committerNiklas Laxström <nikerabbit@users.mediawiki.org>
Sun, 10 Aug 2008 10:30:52 +0000 (10:30 +0000)
* Avoid running some queries multiple times by caching the result
- Title::isDeleted is called twice when viewing a page that does not exist by SkinTemplate::buildContentActionUrls and Skin::getUndeleteLink
- Title::getTitleProtections is called twice when viewing a page that does not exist by SkinTemplate::buildContentActionUrls and efUniversalEditLink

includes/Title.php

index ed28f8b..0abe718 100644 (file)
@@ -45,28 +45,29 @@ class Title {
         * @private
         */
 
-       var $mTextform = '';              # Text form (spaces not underscores) of the main part
-       var $mUrlform = '';               # URL-encoded form of the main part
-       var $mDbkeyform = '';             # Main part with underscores
+       var $mTextform = '';              # Text form (spaces not underscores) of the main part
+       var $mUrlform = '';               # URL-encoded form of the main part
+       var $mDbkeyform = '';             # Main part with underscores
        var $mUserCaseDBKey;              # DB key with the initial letter in the case specified by the user
        var $mNamespace = NS_MAIN;        # Namespace index, i.e. one of the NS_xxxx constants
-       var $mInterwiki = '';             # Interwiki prefix (or null string)
-       var $mFragment;                       # Title fragment (i.e. the bit after the #)
+       var $mInterwiki = '';             # Interwiki prefix (or null string)
+       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 $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?
-       var $mRestrictionsExpiry;             # When do the restrictions on this page expire?
+       var $mCascadeRestriction;         # Cascade restrictions on this page to included templates and images?
+       var $mRestrictionsExpiry;         # When do the restrictions on this page expire?
        var $mHasCascadingRestrictions;   # Are cascading restrictions in effect on this page?
        var $mCascadeRestrictionSources;  # Where are the cascading restrictions coming from on this page?
        var $mRestrictionsLoaded = false; # Boolean for initialisation on demand
-       var $mPrefixedText;                   # Text form including namespace/interwiki, initialised on demand
+       var $mPrefixedText;               # Text form including namespace/interwiki, initialised on demand
+
        # Don't change the following default, NS_MAIN is hardcoded in several
        # places.  See bug 696.
        var $mDefaultNamespace = NS_MAIN; # Namespace index when there is no namespace
-                                             # Zero except in {{transclusion}} tags
-       var $mWatched = null;                     # Is $wgUser watching this page? null if unfilled, accessed through userIsWatching()
+                                         # Zero except in {{transclusion}} tags
+       var $mWatched = null;             # Is $wgUser watching this page? null if unfilled, accessed through userIsWatching()
        var $mLength = -1;                # The page length, 0 for special pages
        var $mRedirect = null;            # Is the article at this title a redirect?
        /**#@-*/
@@ -1339,15 +1340,28 @@ class Title {
                        return false;
                }
 
+               wfDebug( wfBacktrace() );
+
+               // Cache to avoid multiple database queries for the same title
+               if ( isset($this->mProtections) ) {
+                       return $this->mProtections;
+               }
+
+               $conds = array(
+                       'pt_namespace' => $this->getNamespace(),
+                       'pt_title' => $this->getDBkey()
+               );
+
                $dbr = wfGetDB( DB_SLAVE );
-               $res = $dbr->select( 'protected_titles', '*',
-                       array ('pt_namespace' => $this->getNamespace(), 'pt_title' => $this->getDBkey()) );
+               $res = $dbr->select( 'protected_titles', '*', $conds, __METHOD__ );
 
                if ($row = $dbr->fetchRow( $res )) {
-                       return $row;
+                       $this->mProtections = $row;
                } else {
-                       return false;
+                       $this->mProtections = false;
                }
+
+               return $this->mProtections;
        }
 
        public function updateTitleProtection( $create_perm, $reason, $expiry ) {
@@ -1879,19 +1893,30 @@ class Title {
         * @return int the number of archived revisions
         */
        public function isDeleted() {
-               $fname = 'Title::isDeleted';
                if ( $this->getNamespace() < 0 ) {
-                       $n = 0;
-               } else {
-                       $dbr = wfGetDB( DB_SLAVE );
-                       $n = $dbr->selectField( 'archive', 'COUNT(*)', array( 'ar_namespace' => $this->getNamespace(),
-                               'ar_title' => $this->getDBkey() ), $fname );
-                       if( $this->getNamespace() == NS_IMAGE ) {
-                               $n += $dbr->selectField( 'filearchive', 'COUNT(*)',
-                                       array( 'fa_name' => $this->getDBkey() ), $fname );
-                       }
+                       return 0;
                }
-               return (int)$n;
+
+               // Cache to avoid multiple queries for the same title
+               if ( isset($this->mIsDeleted) ) {
+                       return $this->mIsDeleted;
+               }
+
+               $dbr = wfGetDB( DB_SLAVE );
+               $aConds = array(
+                       'ar_namespace' => $this->getNamespace(),
+                       'ar_title' => $this->getDBkey()
+               );
+               $n = $dbr->selectField( 'archive', 'COUNT(*)', $aConds, __METHOD__ );
+
+               if ( $this->getNamespace() == NS_IMAGE ) {
+                       $faConds = array( 'fa_name' => $this->getDBkey() );
+                       $n += $dbr->selectField( 'filearchive', 'COUNT(*)', $faConds , __METHOD__ );
+               }
+
+               $this->mIsDeleted = (int) $n;
+
+               return $this->mIsDeleted;
        }
 
        /**