Don't always count CASCADINGSOURCES as expensive
[lhc/web/wiklou.git] / includes / Title.php
index 1d13f4b..f2de4b8 100644 (file)
  * @internal documentation reviewed 15 Mar 2010
  */
 class Title {
-       /** @name Static cache variables */
-       // @{
-       static private $titleCache = array();
-       // @}
+       /** @var MapCacheLRU */
+       static private $titleCache = null;
 
        /**
         * Title::newFromText maintains a cache to avoid expensive re-normalization of
@@ -61,7 +59,7 @@ class Title {
        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 $mInterwiki = '';             // /< Interwiki prefix
        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
@@ -75,7 +73,7 @@ class Title {
        var $mHasCascadingRestrictions;   ///< Are cascading restrictions in effect on this page?
        var $mCascadeSources;             ///< 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 = null;        ///< Text form including namespace/interwiki, initialised on demand
        var $mTitleProtection;            ///< Cached value for getTitleProtection (create protection)
        # Don't change the following default, NS_MAIN is hardcoded in several
        # places.  See bug 696.
@@ -130,6 +128,8 @@ class Title {
                        throw new MWException( 'Title::newFromText given an object' );
                }
 
+               $cache = self::getTitleCache();
+
                /**
                 * Wiki pages often contain multiple links to the same page.
                 * Title normalization and parsing can become expensive on
@@ -138,8 +138,8 @@ class Title {
                 *
                 * In theory these are value objects and won't get changed...
                 */
-               if ( $defaultNamespace == NS_MAIN && isset( Title::$titleCache[$text] ) ) {
-                       return Title::$titleCache[$text];
+               if ( $defaultNamespace == NS_MAIN && $cache->has( $text ) ) {
+                       return $cache->get( $text );
                }
 
                # Convert things like &eacute; &#257; or &#x3017; into normalized (bug 14952) text
@@ -149,16 +149,9 @@ class Title {
                $t->mDbkeyform = str_replace( ' ', '_', $filteredText );
                $t->mDefaultNamespace = $defaultNamespace;
 
-               static $cachedcount = 0;
                if ( $t->secureAndSplit() ) {
                        if ( $defaultNamespace == NS_MAIN ) {
-                               if ( $cachedcount >= self::CACHE_MAX ) {
-                                       # Avoid memory leaks on mass operations...
-                                       Title::$titleCache = array();
-                                       $cachedcount = 0;
-                               }
-                               $cachedcount++;
-                               Title::$titleCache[$text] =& $t;
+                               $cache->set( $text, $t );
                        }
                        return $t;
                } else {
@@ -200,6 +193,16 @@ class Title {
                }
        }
 
+       /**
+        * @return MapCacheLRU
+        */
+       private static function getTitleCache() {
+               if ( self::$titleCache == null ) {
+                       self::$titleCache = new MapCacheLRU( self::CACHE_MAX );
+               }
+               return self::$titleCache;
+       }
+
        /**
         * Returns a list of fields that are to be selected for initializing Title objects or LinkCache entries.
         * Uses $wgContentHandlerUseDB to determine whether to include page_content_model.
@@ -682,7 +685,7 @@ class Title {
         * @return Bool TRUE if this is an in-project interwiki link or a wikilink, FALSE otherwise
         */
        public function isLocal() {
-               if ( $this->mInterwiki != '' ) {
+               if ( $this->isExternal() ) {
                        $iw = Interwiki::fetch( $this->mInterwiki );
                        if ( $iw ) {
                                return $iw->isLocal();
@@ -697,11 +700,11 @@ class Title {
         * @return Bool
         */
        public function isExternal() {
-               return ( $this->mInterwiki != '' );
+               return $this->mInterwiki !== '';
        }
 
        /**
-        * Get the interwiki prefix (or null string)
+        * Get the interwiki prefix
         *
         * @return String Interwiki prefix
         */
@@ -716,7 +719,7 @@ class Title {
         * @return Bool TRUE if this is transcludable
         */
        public function isTrans() {
-               if ( $this->mInterwiki == '' ) {
+               if ( !$this->isExternal() ) {
                        return false;
                }
 
@@ -729,7 +732,7 @@ class Title {
         * @return String the DB name
         */
        public function getTransWikiID() {
-               if ( $this->mInterwiki == '' ) {
+               if ( !$this->isExternal() ) {
                        return false;
                }
 
@@ -827,7 +830,7 @@ class Title {
        public function getNsText() {
                global $wgContLang;
 
-               if ( $this->mInterwiki != '' ) {
+               if ( $this->isExternal() ) {
                        // This probably shouldn't even happen. ohh man, oh yuck.
                        // But for interwiki transclusion it sometimes does.
                        // Shit. Shit shit shit.
@@ -1011,7 +1014,7 @@ class Title {
         * @return Bool TRUE or FALSE
         */
        public function isMovable() {
-               if ( !MWNamespace::isMovable( $this->getNamespace() ) || $this->getInterwiki() != '' ) {
+               if ( !MWNamespace::isMovable( $this->getNamespace() ) || $this->isExternal() ) {
                        // Interwiki title or immovable namespace. Hooks don't get to override here
                        return false;
                }
@@ -1233,7 +1236,7 @@ class Title {
         */
        private function prefix( $name ) {
                $p = '';
-               if ( $this->mInterwiki != '' ) {
+               if ( $this->isExternal() ) {
                        $p = $this->mInterwiki . ':';
                }
 
@@ -1262,8 +1265,7 @@ class Title {
         * @return String the prefixed title, with spaces
         */
        public function getPrefixedText() {
-               // @todo FIXME: Bad usage of empty() ?
-               if ( empty( $this->mPrefixedText ) ) {
+               if ( $this->mPrefixedText === null ) {
                        $s = $this->prefix( $this->mTextform );
                        $s = str_replace( '_', ' ', $s );
                        $this->mPrefixedText = $s;
@@ -1732,7 +1734,7 @@ class Title {
         *  interwiki link
         */
        public function getEditURL() {
-               if ( $this->mInterwiki != '' ) {
+               if ( $this->isExternal() ) {
                        return '';
                }
                $s = $this->getLocalURL( 'action=edit' );
@@ -2553,6 +2555,19 @@ class Title {
                return ( $sources > 0 );
        }
 
+       /**
+        * Determines whether cascading protection sources have already been loaded from
+        * the database.
+        *
+        * @param bool $getPages True to check if the pages are loaded, or false to check
+        * if the status is loaded.
+        * @return bool Whether or not the specified information has been loaded
+        * @since 1.23
+        */
+       public function areCascadeProtectionSourcesLoaded( $getPages = true ) {
+               return $getPages ? isset( $this->mCascadeSources ) : isset( $this->mHasCascadingRestrictions );
+       }
+
        /**
         * Cascading protection: Get the source of any cascading restrictions on this page.
         *
@@ -2653,6 +2668,17 @@ class Title {
                return array( $sources, $pagerestrictions );
        }
 
+       /**
+        * Accessor for mRestrictionsLoaded
+        *
+        * @return bool Whether or not the page's restrictions have already been
+        * loaded from the database
+        * @since 1.23
+        */
+       public function areRestrictionsLoaded() {
+               return $this->mRestrictionsLoaded;
+       }
+
        /**
         * Accessor/initialisation for mRestrictions
         *
@@ -2668,6 +2694,21 @@ class Title {
                                : array();
        }
 
+       /**
+        * Accessor/initialisation for mRestrictions
+        *
+        * @return Array of Arrays of Strings the first level indexed by
+        * action, the second level containing the names of the groups
+        * allowed to perform each action
+        * @since 1.23
+        */
+       public function getAllRestrictions() {
+               if ( !$this->mRestrictionsLoaded ) {
+                       $this->loadRestrictions();
+               }
+               return $this->mRestrictions;
+       }
+
        /**
         * Get the expiry time for the restriction against a given action
         *
@@ -2889,15 +2930,15 @@ class Title {
                # alone to cache the result.  There's no point in having it hanging
                # around uninitialized in every Title object; therefore we only add it
                # if needed and don't declare it statically.
-               if ( isset( $this->mHasSubpages ) ) {
-                       return $this->mHasSubpages;
+               if ( !isset( $this->mHasSubpages ) ) {
+                       $this->mHasSubpages = false;
+                       $subpages = $this->getSubpages( 1 );
+                       if ( $subpages instanceof TitleArray ) {
+                               $this->mHasSubpages = (bool)$subpages->count();
+                       }
                }
 
-               $subpages = $this->getSubpages( 1 );
-               if ( $subpages instanceof TitleArray ) {
-                       return $this->mHasSubpages = (bool)$subpages->count();
-               }
-               return $this->mHasSubpages = false;
+               return $this->mHasSubpages;
        }
 
        /**
@@ -2919,7 +2960,7 @@ class Title {
                if ( $limit > -1 ) {
                        $options['LIMIT'] = $limit;
                }
-               return $this->mSubpages = TitleArray::newFromResult(
+               $this->mSubpages = TitleArray::newFromResult(
                        $dbr->select( 'page',
                                array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' ),
                                $conds,
@@ -2927,6 +2968,7 @@ class Title {
                                $options
                        )
                );
+               return $this->mSubpages;
        }
 
        /**
@@ -2987,7 +3029,8 @@ class Title {
         */
        public function getArticleID( $flags = 0 ) {
                if ( $this->getNamespace() < 0 ) {
-                       return $this->mArticleID = 0;
+                       $this->mArticleID = 0;
+                       return $this->mArticleID;
                }
                $linkCache = LinkCache::singleton();
                if ( $flags & self::GAID_FOR_UPDATE ) {
@@ -3016,7 +3059,8 @@ class Title {
                }
                # Calling getArticleID() loads the field from cache as needed
                if ( !$this->getArticleID( $flags ) ) {
-                       return $this->mRedirect = false;
+                       $this->mRedirect = false;
+                       return $this->mRedirect;
                }
 
                $linkCache = LinkCache::singleton();
@@ -3028,7 +3072,8 @@ class Title {
                        # LinkCache as appropriate, or use $flags = Title::GAID_FOR_UPDATE. If that flag is
                        # set, then LinkCache will definitely be up to date here, since getArticleID() forces
                        # LinkCache to refresh its data from the master.
-                       return $this->mRedirect = false;
+                       $this->mRedirect = false;
+                       return $this->mRedirect;
                }
 
                $this->mRedirect = (bool)$cached;
@@ -3049,13 +3094,15 @@ class Title {
                }
                # Calling getArticleID() loads the field from cache as needed
                if ( !$this->getArticleID( $flags ) ) {
-                       return $this->mLength = 0;
+                       $this->mLength = 0;
+                       return $this->mLength;
                }
                $linkCache = LinkCache::singleton();
                $cached = $linkCache->getGoodLinkFieldObj( $this, 'length' );
                if ( $cached === null ) {
                        # Trust LinkCache's state over our own, as for isRedirect()
-                       return $this->mLength = 0;
+                       $this->mLength = 0;
+                       return $this->mLength;
                }
 
                $this->mLength = intval( $cached );
@@ -3075,14 +3122,16 @@ class Title {
                }
                # Calling getArticleID() loads the field from cache as needed
                if ( !$this->getArticleID( $flags ) ) {
-                       return $this->mLatestID = 0;
+                       $this->mLatestID = 0;
+                       return $this->mLatestID;
                }
                $linkCache = LinkCache::singleton();
                $linkCache->addLinkObj( $this );
                $cached = $linkCache->getGoodLinkFieldObj( $this, 'revision' );
                if ( $cached === null ) {
                        # Trust LinkCache's state over our own, as for isRedirect()
-                       return $this->mLatestID = 0;
+                       $this->mLatestID = 0;
+                       return $this->mLatestID;
                }
 
                $this->mLatestID = intval( $cached );
@@ -3247,7 +3296,7 @@ class Title {
                } while ( true );
 
                # We already know that some pages won't be in the database!
-               if ( $this->mInterwiki != '' || NS_SPECIAL == $this->mNamespace ) {
+               if ( $this->isExternal() || NS_SPECIAL == $this->mNamespace ) {
                        $this->mArticleID = 0;
                }
                $fragment = strstr( $dbkey, '#' );
@@ -3303,14 +3352,13 @@ class Title {
                # and [[Foo]] point to the same place.  Don't force it for interwikis, since the
                # other site might be case-sensitive.
                $this->mUserCaseDBKey = $dbkey;
-               if ( $this->mInterwiki == '' ) {
+               if ( !$this->isExternal() ) {
                        $dbkey = self::capitalize( $dbkey, $this->mNamespace );
                }
 
                # Can't make a link to a namespace alone... "empty" local links can only be
                # self-links with a fragment identifier.
-               # TODO: Why do we exclude NS_MAIN (bug 54044)
-               if ( $dbkey == '' && $this->mInterwiki == '' && $this->mNamespace != NS_MAIN ) {
+               if ( $dbkey == '' && !$this->isExternal() && $this->mNamespace != NS_MAIN ) {
                        return false;
                }
 
@@ -3529,6 +3577,13 @@ class Title {
                        }
                }
 
+               // If we are looking at a css/js user subpage, purge the action=raw.
+               if ( $this->isJsSubpage() ) {
+                       $urls[] = $this->getInternalUrl( 'action=raw&ctype=text/javascript' );
+               } elseif ( $this->isCssSubpage() ) {
+                       $urls[] = $this->getInternalUrl( 'action=raw&ctype=text/css' );
+               }
+
                wfRunHooks( 'TitleSquidURLs', array( $this, &$urls ) );
                return $urls;
        }
@@ -3580,7 +3635,7 @@ class Title {
                if ( !$this->isMovable() ) {
                        $errors[] = array( 'immobile-source-namespace', $this->getNsText() );
                }
-               if ( $nt->getInterwiki() != '' ) {
+               if ( $nt->isExternal() ) {
                        $errors[] = array( 'immobile-target-namespace-iw' );
                }
                if ( !$nt->isMovable() ) {
@@ -3882,6 +3937,7 @@ class Title {
 
                if ( $moveOverRedirect ) {
                        $newid = $nt->getArticleID();
+                       $newcontent = $newpage->getContent();
 
                        # Delete the old redirect. We don't save it to history since
                        # by definition if we've got here it's rather uninteresting.
@@ -3889,7 +3945,7 @@ class Title {
                        # a conflict on the unique namespace+title index...
                        $dbw->delete( 'page', array( 'page_id' => $newid ), __METHOD__ );
 
-                       $newpage->doDeleteUpdates( $newid );
+                       $newpage->doDeleteUpdates( $newid, $newcontent );
                }
 
                # Save a null revision in the page's history notifying of the move
@@ -4492,7 +4548,7 @@ class Title {
                        return $isKnown;
                }
 
-               if ( $this->mInterwiki != '' ) {
+               if ( $this->isExternal() ) {
                        return true;  // any interwiki link might be viewable, for all we know
                }
 
@@ -4647,7 +4703,8 @@ class Title {
                        return $this->mNotificationTimestamp[$uid];
                }
                if ( !$uid || !$wgShowUpdatedMarker || !$user->isAllowed( 'viewmywatchlist' ) ) {
-                       return $this->mNotificationTimestamp[$uid] = false;
+                       $this->mNotificationTimestamp[$uid] = false;
+                       return $this->mNotificationTimestamp[$uid];
                }
                // Don't cache too much!
                if ( count( $this->mNotificationTimestamp ) >= self::CACHE_MAX ) {