Return nothing on empty math tags instead of char encoding
[lhc/web/wiklou.git] / includes / Title.php
index c88e062..a8abb6f 100644 (file)
@@ -8,9 +8,6 @@
 /** */
 require_once( 'normal/UtfNormal.php' );
 
-$wgTitleInterwikiCache = array();
-$wgTitleCache = array();
-
 define ( 'GAID_FOR_UPDATE', 1 );
 
 # Title::newFromTitle maintains a cache to avoid
@@ -28,13 +25,20 @@ define( 'MW_TITLECACHE_MAX', 1000 );
  * @package MediaWiki
  */
 class Title {
+       /**
+        * Static cache variables
+        */
+       static private $titleCache=array();
+       static private $interwikiCache=array();
+       
+       
        /**
         * All member variables should be considered private
         * Please use the accessor functions
         */
 
         /**#@+
-        * @access private
+        * @private
         */
 
        var $mTextform;           # Text form (spaces not underscores) of the main part
@@ -46,18 +50,18 @@ class Title {
        var $mArticleID;          # Article ID, fetched from the link cache on demand
        var $mLatestID;         # ID of most recent revision
        var $mRestrictions;       # Array of groups allowed to edit this article
-                              # Only null or "sysop" are supported
+                               # Only null or "sysop" are supported
        var $mRestrictionsLoaded; # Boolean for initialisation on demand
        var $mPrefixedText;       # Text form including namespace/interwiki, initialised on demand
        var $mDefaultNamespace;   # Namespace index when there is no namespace
-                              # Zero except in {{transclusion}} tags
-       var $mWatched;            # Is $wgUser watching this page? NULL if unfilled, accessed through userIsWatching()
+                           # Zero except in {{transclusion}} tags
+       var $mWatched;      # Is $wgUser watching this page? NULL if unfilled, accessed through userIsWatching()
        /**#@-*/
 
 
        /**
         * Constructor
-        * @access private
+        * @private
         */
        /* private */ function Title() {
                $this->mInterwiki = $this->mUrlform =
@@ -104,13 +108,9 @@ class Title {
         * @static
         * @access public
         */
-       function newFromText( $text, $defaultNamespace = NS_MAIN ) {
-               global $wgTitleCache;
-               $fname = 'Title::newFromText';
-               wfProfileIn( $fname );
-
+       public static function newFromText( $text, $defaultNamespace = NS_MAIN ) {
                if( is_object( $text ) ) {
-                       wfDebugDieBacktrace( 'Title::newFromText given an object' );
+                       throw new MWException( 'Title::newFromText given an object' );
                }
 
                /**
@@ -121,9 +121,8 @@ class Title {
                 *
                 * In theory these are value objects and won't get changed...
                 */
-               if( $defaultNamespace == NS_MAIN && isset( $wgTitleCache[$text] ) ) {
-                       wfProfileOut( $fname );
-                       return $wgTitleCache[$text];
+               if( $defaultNamespace == NS_MAIN && isset( Title::$titleCache[$text] ) ) {
+                       return Title::$titleCache[$text];
                }
 
                /**
@@ -131,7 +130,7 @@ class Title {
                 */
                $filteredText = Sanitizer::decodeCharReferences( $text );
 
-               $t =& new Title();
+               $t = new Title();
                $t->mDbkeyform = str_replace( ' ', '_', $filteredText );
                $t->mDefaultNamespace = $defaultNamespace;
 
@@ -140,16 +139,14 @@ class Title {
                        if( $defaultNamespace == NS_MAIN ) {
                                if( $cachedcount >= MW_TITLECACHE_MAX ) {
                                        # Avoid memory leaks on mass operations...
-                                       $wgTitleCache = array();
+                                       Title::$titleCache = array();
                                        $cachedcount=0;
                                }
                                $cachedcount++;
-                               $wgTitleCache[$text] =& $t;
+                               Title::$titleCache[$text] =& $t;
                        }
-                       wfProfileOut( $fname );
                        return $t;
                } else {
-                       wfProfileOut( $fname );
                        $ret = NULL;
                        return $ret;
                }
@@ -163,7 +160,7 @@ class Title {
         * @static
         * @access public
         */
-       function newFromURL( $url ) {
+       public static function newFromURL( $url ) {
                global $wgLegalTitleChars;
                $t = new Title();
 
@@ -206,6 +203,21 @@ class Title {
                return $title;
        }
 
+       /**
+        * Make an array of titles from an array of IDs 
+        */
+       function newFromIDs( $ids ) {
+               $dbr =& wfGetDB( DB_SLAVE );
+               $res = $dbr->select( 'page', array( 'page_namespace', 'page_title' ),
+                       'page_id IN (' . $dbr->makeList( $ids ) . ')', __METHOD__ );
+
+               $titles = array();
+               while ( $row = $dbr->fetchObject( $res ) ) {
+                       $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
+               }
+               return $titles;
+       }
+
        /**
         * Create a new Title from a namespace index and a DB key.
         * It's assumed that $ns and $title are *valid*, for instance when
@@ -219,8 +231,8 @@ class Title {
         * @static
         * @access public
         */
-       function &makeTitle( $ns, $title ) {
-               $t =& new Title();
+       public static function &makeTitle( $ns, $title ) {
+               $t = new Title();
                $t->mInterwiki = '';
                $t->mFragment = '';
                $t->mNamespace = intval( $ns );
@@ -232,7 +244,7 @@ class Title {
        }
 
        /**
-        * Create a new Title frrom a namespace index and a DB key.
+        * Create a new Title from a namespace index and a DB key.
         * The parameters will be checked for validity, which is a bit slower
         * than makeTitle() but safer for user-provided data.
         *
@@ -242,7 +254,7 @@ class Title {
         * @static
         * @access public
         */
-       function makeTitleSafe( $ns, $title ) {
+       public static function makeTitleSafe( $ns, $title ) {
                $t = new Title();
                $t->mDbkeyform = Title::makeName( $ns, $title );
                if( $t->secureAndSplit() ) {
@@ -259,7 +271,7 @@ class Title {
         * @return Title the new object
         * @access public
         */
-       function newMainPage() {
+       public static function newMainPage() {
                return Title::newFromText( wfMsgForContent( 'mainpage' ) );
        }
 
@@ -271,10 +283,11 @@ class Title {
         * @static
         * @access public
         */
-       function newFromRedirect( $text ) {
-               global $wgMwRedir;
+       public static function newFromRedirect( $text ) {
+               $mwRedir = MagicWord::get( 'redirect' );
                $rt = NULL;
-               if ( $wgMwRedir->matchStart( $text ) ) {
+               if ( $mwRedir->matchStart( $text ) ) {
+                       $m = array();
                        if ( preg_match( '/\[{2}(.*?)(?:\||\]{2})/', $text, $m ) ) {
                                # categories are escaped using : for example one can enter:
                                # #REDIRECT [[:Category:Music]]. Need to remove it.
@@ -285,7 +298,7 @@ class Title {
 
                                $rt = Title::newFromText( $m[1] );
                                # Disallow redirects to Special:Userlogout
-                               if ( !is_null($rt) && $rt->getNamespace() == NS_SPECIAL && preg_match( '/^Userlogout/i', $rt->getText() ) ) {
+                               if ( !is_null($rt) && $rt->isSpecial( 'Userlogout' ) ) {
                                        $rt = NULL;
                                }
                        }
@@ -322,7 +335,7 @@ class Title {
         * @static
         * @access public
         */
-       function legalChars() {
+       public static function legalChars() {
                global $wgLegalTitleChars;
                return $wgLegalTitleChars;
        }
@@ -338,12 +351,11 @@ class Title {
         */
        /* static */ function indexTitle( $ns, $title ) {
                global $wgContLang;
-               require_once( 'SearchEngine.php' );
 
                $lc = SearchEngine::legalSearchChars() . '&#;';
                $t = $wgContLang->stripForSearch( $title );
                $t = preg_replace( "/[^{$lc}]+/", ' ', $t );
-               $t = strtolower( $t );
+               $t = $wgContLang->lc( $t );
 
                # Handle 's, s'
                $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
@@ -363,7 +375,7 @@ class Title {
         * @param string $title the DB key form the title
         * @return string the prefixed form of the title
         */
-       /* static */ function makeName( $ns, $title ) {
+       public static function makeName( $ns, $title ) {
                global $wgContLang;
 
                $n = $wgContLang->getNsText( $ns );
@@ -379,24 +391,25 @@ class Title {
         * @access public
         */
        function getInterwikiLink( $key )  {
-               global $wgMemc, $wgDBname, $wgInterwikiExpiry, $wgTitleInterwikiCache;
+               global $wgMemc, $wgInterwikiExpiry;
+               global $wgInterwikiCache, $wgContLang;
                $fname = 'Title::getInterwikiLink';
 
-               wfProfileIn( $fname );
+               $key = $wgContLang->lc( $key );
 
-               $key = strtolower( $key );
+               $k = wfMemcKey( 'interwiki', $key );
+               if( array_key_exists( $k, Title::$interwikiCache ) ) {
+                       return Title::$interwikiCache[$k]->iw_url;
+               }
 
-               $k = $wgDBname.':interwiki:'.$key;
-               if( array_key_exists( $k, $wgTitleInterwikiCache ) ) {
-                       wfProfileOut( $fname );
-                       return $wgTitleInterwikiCache[$k]->iw_url;
+               if ($wgInterwikiCache) {
+                       return Title::getInterwikiCached( $key );
                }
 
                $s = $wgMemc->get( $k );
                # Ignore old keys with no iw_local
                if( $s && isset( $s->iw_local ) && isset($s->iw_trans)) {
-                       $wgTitleInterwikiCache[$k] = $s;
-                       wfProfileOut( $fname );
+                       Title::$interwikiCache[$k] = $s;
                        return $s->iw_url;
                }
 
@@ -405,7 +418,6 @@ class Title {
                        array( 'iw_url', 'iw_local', 'iw_trans' ),
                        array( 'iw_prefix' => $key ), $fname );
                if( !$res ) {
-                       wfProfileOut( $fname );
                        return '';
                }
 
@@ -418,12 +430,54 @@ class Title {
                        $s->iw_trans = 0;
                }
                $wgMemc->set( $k, $s, $wgInterwikiExpiry );
-               $wgTitleInterwikiCache[$k] = $s;
+               Title::$interwikiCache[$k] = $s;
 
-               wfProfileOut( $fname );
                return $s->iw_url;
        }
-
+       
+       /**
+        * Fetch interwiki prefix data from local cache in constant database
+        *
+        * More logic is explained in DefaultSettings
+        *
+        * @return string URL of interwiki site
+        * @access public
+        */
+       function getInterwikiCached( $key ) {
+               global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
+               static $db, $site;
+
+               if (!$db)
+                       $db=dba_open($wgInterwikiCache,'r','cdb');
+               /* Resolve site name */
+               if ($wgInterwikiScopes>=3 and !$site) {
+                       $site = dba_fetch('__sites:' . wfWikiID(), $db);
+                       if ($site=="")
+                               $site = $wgInterwikiFallbackSite;
+               }
+               $value = dba_fetch( wfMemcKey( $key ), $db);
+               if ($value=='' and $wgInterwikiScopes>=3) {
+                       /* try site-level */
+                       $value = dba_fetch("_{$site}:{$key}", $db);
+               }
+               if ($value=='' and $wgInterwikiScopes>=2) {
+                       /* try globals */
+                       $value = dba_fetch("__global:{$key}", $db);
+               }
+               if ($value=='undef')
+                       $value='';
+               $s = (object)false;
+               $s->iw_url = '';
+               $s->iw_local = 0;
+               $s->iw_trans = 0;
+               if ($value!='') {
+                       list($local,$url)=explode(' ',$value,2);
+                       $s->iw_url=$url;
+                       $s->iw_local=(int)$local;
+               }
+               Title::$interwikiCache[wfMemcKey( 'interwiki', $key )] = $s;
+               return $s->iw_url;
+       }
        /**
         * Determine whether the object refers to a page within
         * this project.
@@ -433,13 +487,11 @@ class Title {
         * @access public
         */
        function isLocal() {
-               global $wgTitleInterwikiCache, $wgDBname;
-
                if ( $this->mInterwiki != '' ) {
                        # Make sure key is loaded into cache
                        $this->getInterwikiLink( $this->mInterwiki );
-                       $k = $wgDBname.':interwiki:' . $this->mInterwiki;
-                       return (bool)($wgTitleInterwikiCache[$k]->iw_local);
+                       $k = wfMemcKey( 'interwiki', $this->mInterwiki );
+                       return (bool)(Title::$interwikiCache[$k]->iw_local);
                } else {
                        return true;
                }
@@ -453,14 +505,12 @@ class Title {
         * @access public
         */
        function isTrans() {
-               global $wgTitleInterwikiCache, $wgDBname;
-
-               if ($this->mInterwiki == '' || !$this->isLocal())
+               if ($this->mInterwiki == '')
                        return false;
                # Make sure key is loaded into cache
                $this->getInterwikiLink( $this->mInterwiki );
-               $k = $wgDBname.':interwiki:' . $this->mInterwiki;
-               return (bool)($wgTitleInterwikiCache[$k]->iw_trans);
+               $k = wfMemcKey( 'interwiki', $this->mInterwiki );
+               return (bool)(Title::$interwikiCache[$k]->iw_trans);
        }
 
        /**
@@ -489,7 +539,7 @@ class Title {
 
                foreach ( $titles as $title ) {
                        if ( $wgUseFileCache ) {
-                               $cm = new CacheManager($title);
+                               $cm = new HTMLFileCache($title);
                                @unlink($cm->fileCacheName());
                        }
 
@@ -517,6 +567,19 @@ class Title {
                }
        }
 
+       /**
+        * Escape a text fragment, say from a link, for a URL
+        */
+       static function escapeFragmentForURL( $fragment ) {
+               $fragment = str_replace( ' ', '_', $fragment );
+               $fragment = urlencode( Sanitizer::decodeCharReferences( $fragment ) );
+               $replaceArray = array(
+                       '%3A' => ':',
+                       '%' => '.'
+               );
+               return strtr( $fragment, $replaceArray );
+       }
+
 #----------------------------------------------------------------------------
 #      Other stuff
 #----------------------------------------------------------------------------
@@ -552,7 +615,19 @@ class Title {
         * @access public
         */
        function getNsText() {
-               global $wgContLang;
+               global $wgContLang, $wgCanonicalNamespaceNames;
+
+               if ( '' != $this->mInterwiki ) {
+                       // This probably shouldn't even happen. ohh man, oh yuck.
+                       // But for interwiki transclusion it sometimes does.
+                       // Shit. Shit shit shit.
+                       //
+                       // Use the canonical namespaces if possible to try to
+                       // resolve a foreign namespace.
+                       if( isset( $wgCanonicalNamespaceNames[$this->mNamespace] ) ) {
+                               return $wgCanonicalNamespaceNames[$this->mNamespace];
+                       }
+               }
                return $wgContLang->getNsText( $this->mNamespace );
        }
        /**
@@ -565,6 +640,23 @@ class Title {
                return $wgContLang->getNsText( Namespace::getSubject( $this->mNamespace ) );
        }
 
+       /**
+        * Get the namespace text of the talk page
+        * @return string
+        */
+       function getTalkNsText() {
+               global $wgContLang;
+               return( $wgContLang->getNsText( Namespace::getTalk( $this->mNamespace ) ) );
+       }
+       
+       /**
+        * Could this title have a corresponding talk page?
+        * @return bool
+        */
+       function canTalk() {
+               return( Namespace::canTalk( $this->mNamespace ) );
+       }
+       
        /**
         * Get the interwiki prefix (or null string)
         * @return string
@@ -572,11 +664,24 @@ class Title {
         */
        function getInterwiki() { return $this->mInterwiki; }
        /**
-        * Get the Title fragment (i.e. the bit after the #)
+        * Get the Title fragment (i.e. the bit after the #) in text form
         * @return string
         * @access public
         */
        function getFragment() { return $this->mFragment; }
+       /**
+        * Get the fragment in URL form, including the "#" character if there is one
+        *
+        * @return string
+        * @access public
+        */
+       function getFragmentForURL() {
+               if ( $this->mFragment == '' ) {
+                       return '';
+               } else {
+                       return '#' . Title::escapeFragmentForURL( $this->mFragment );
+               }
+       }
        /**
         * Get the default namespace index, for when there is no namespace
         * @return int
@@ -612,7 +717,6 @@ class Title {
         * @access public
         */
        function getPrefixedText() {
-               global $wgContLang;
                if ( empty( $this->mPrefixedText ) ) { // FIXME: bad usage of empty() ?
                        $s = $this->prefix( $this->mTextform );
                        $s = str_replace( '_', ' ', $s );
@@ -629,7 +733,6 @@ class Title {
         * @access public
         */
        function getFullText() {
-               global $wgContLang;
                $text = $this->getPrefixedText();
                if( '' != $this->mFragment ) {
                        $text .= '#' . $this->mFragment;
@@ -637,6 +740,48 @@ class Title {
                return $text;
        }
 
+       /**
+        * Get the base name, i.e. the leftmost parts before the /
+        * @return string Base name
+        */
+       function getBaseText() {
+               global $wgNamespacesWithSubpages;
+               if( isset( $wgNamespacesWithSubpages[ $this->mNamespace ] ) && $wgNamespacesWithSubpages[ $this->mNamespace ] ) {
+                       $parts = explode( '/', $this->getText() );
+                       # Don't discard the real title if there's no subpage involved
+                       if( count( $parts ) > 1 )
+                               unset( $parts[ count( $parts ) - 1 ] );
+                       return implode( '/', $parts );
+               } else {
+                       return $this->getText();
+               }
+       }
+
+       /**
+        * Get the lowest-level subpage name, i.e. the rightmost part after /
+        * @return string Subpage name
+        */
+       function getSubpageText() {
+               global $wgNamespacesWithSubpages;
+               if( isset( $wgNamespacesWithSubpages[ $this->mNamespace ] ) && $wgNamespacesWithSubpages[ $this->mNamespace ] ) {
+                       $parts = explode( '/', $this->mTextform );
+                       return( $parts[ count( $parts ) - 1 ] );
+               } else {
+                       return( $this->mTextform );
+               }
+       }
+       
+       /**
+        * Get a URL-encoded form of the subpage text
+        * @return string URL-encoded subpage name
+        */
+       function getSubpageUrlForm() {
+               $text = $this->getSubpageText();
+               $text = wfUrlencode( str_replace( ' ', '_', $text ) );
+               $text = str_replace( '%28', '(', str_replace( '%29', ')', $text ) ); # Clean up the URL; per below, this might not be safe
+               return( $text );
+       }
+
        /**
         * Get a URL-encoded title (not an actual URL) including interwiki
         * @return string the URL-encoded form
@@ -661,14 +806,15 @@ class Title {
         *
         * @param string $query an optional query string, not used
         *      for interwiki links
+        * @param string $variant language variant of url (for sr, zh..)
         * @return string the URL
         * @access public
         */
-       function getFullURL( $query = '' ) {
+       function getFullURL( $query = '', $variant = false ) {
                global $wgContLang, $wgServer, $wgRequest;
 
                if ( '' == $this->mInterwiki ) {
-                       $url = $this->getLocalUrl( $query );
+                       $url = $this->getLocalUrl( $query, $variant );
 
                        // Ugly quick hack to avoid duplicate prefixes (bug 4571 etc)
                        // Correct fix would be to move the prepending elsewhere.
@@ -678,9 +824,10 @@ class Title {
                } else {
                        $baseUrl = $this->getInterwikiLink( $this->mInterwiki );
 
-                       $namespace = $wgContLang->getNsText( $this->mNamespace );
+                       $namespace = wfUrlencode( $this->getNsText() );
                        if ( '' != $namespace ) {
                                # Can this actually happen? Interwikis shouldn't be parsed.
+                               # Yes! It can in interwiki transclusion. But... it probably shouldn't.
                                $namespace .= ':';
                        }
                        $url = str_replace( '$1', $namespace . $this->mUrlform, $baseUrl );
@@ -692,10 +839,11 @@ class Title {
                                }
                                $url .= $query;
                        }
-                       if ( '' != $this->mFragment ) {
-                               $url .= '#' . $this->mFragment;
-                       }
                }
+
+               # Finally, add the fragment.
+               $url .= $this->getFragmentForURL();
+
                wfRunHooks( 'GetFullURL', array( &$this, &$url, $query ) );
                return $url;
        }
@@ -705,21 +853,49 @@ class Title {
         * with action=render, $wgServer is prepended.
         * @param string $query an optional query string; if not specified,
         *      $wgArticlePath will be used.
+        * @param string $variant language variant of url (for sr, zh..)
         * @return string the URL
         * @access public
         */
-       function getLocalURL( $query = '' ) {
+       function getLocalURL( $query = '', $variant = false ) {
                global $wgArticlePath, $wgScript, $wgServer, $wgRequest;
+               global $wgVariantArticlePath, $wgContLang, $wgUser;
+
+               // internal links should point to same variant as current page (only anonymous users)
+               if($variant == false && $wgContLang->hasVariants() && !$wgUser->isLoggedIn()){
+                       $pref = $wgContLang->getPreferredVariant(false);
+                       if($pref != $wgContLang->getCode())
+                               $variant = $pref;
+               }
 
                if ( $this->isExternal() ) {
                        $url = $this->getFullURL();
+                       if ( $query ) {
+                               // This is currently only used for edit section links in the
+                               // context of interwiki transclusion. In theory we should
+                               // append the query to the end of any existing query string,
+                               // but interwiki transclusion is already broken in that case.
+                               $url .= "?$query";
+                       }
                } else {
                        $dbkey = wfUrlencode( $this->getPrefixedDBkey() );
                        if ( $query == '' ) {
-                               $url = str_replace( '$1', $dbkey, $wgArticlePath );
+                               if($variant!=false && $wgContLang->hasVariants()){
+                                       if($wgVariantArticlePath==false)
+                                               $variantArticlePath =  "$wgScript?title=$1&variant=$2"; // default
+                                       else 
+                                               $variantArticlePath = $wgVariantArticlePath;
+                                       
+                                       $url = str_replace( '$2', urlencode( $variant ), $variantArticlePath );
+                                       $url = str_replace( '$1', $dbkey, $url  );
+                                       
+                               }
+                               else 
+                                       $url = str_replace( '$1', $dbkey, $wgArticlePath );
                        } else {
                                global $wgActionPaths;
                                $url = false;
+                               $matches = array();
                                if( !empty( $wgActionPaths ) &&
                                        preg_match( '/^(.*&|)action=([^&]*)(&(.*)|)$/', $query, $matches ) )
                                {
@@ -778,12 +954,13 @@ class Title {
         * internal hostname for the server from the exposed one.
         *
         * @param string $query an optional query string
+        * @param string $variant language variant of url (for sr, zh..)
         * @return string the URL
         * @access public
         */
-       function getInternalURL( $query = '' ) {
+       function getInternalURL( $query = '', $variant = false ) {
                global $wgInternalServer;
-               $url = $wgInternalServer . $this->getLocalURL( $query );
+               $url = $wgInternalServer . $this->getLocalURL( $query, $variant );
                wfRunHooks( 'GetInternalURL', array( &$this, &$url, $query ) );
                return $url;
        }
@@ -795,8 +972,6 @@ class Title {
         * @access public
         */
        function getEditURL() {
-               global $wgServer, $wgScript;
-
                if ( '' != $this->mInterwiki ) { return ''; }
                $s = $this->getLocalURL( 'action=edit' );
 
@@ -820,6 +995,31 @@ class Title {
         */
        function isExternal() { return ( '' != $this->mInterwiki ); }
 
+       /**
+        * Is this page "semi-protected" - the *only* protection is autoconfirm?
+        *
+        * @param string Action to check (default: edit)
+        * @return bool
+        */
+       function isSemiProtected( $action = 'edit' ) {
+               if( $this->exists() ) {
+                       $restrictions = $this->getRestrictions( $action );
+                       if( count( $restrictions ) > 0 ) {
+                               foreach( $restrictions as $restriction ) {
+                                       if( strtolower( $restriction ) != 'autoconfirmed' )
+                                               return false;
+                               }
+                       } else {
+                               # Not protected
+                               return false;
+                       }
+                       return true;
+               } else {
+                       # If it doesn't exist, it can't be protected
+                       return false;
+               }
+       }
+
        /**
         * Does the title correspond to a protected article?
         * @param string $what the action the page is protected from,
@@ -827,16 +1027,28 @@ class Title {
         * @return boolean
         * @access public
         */
-       function isProtected($action = '') {
-               if ( -1 == $this->mNamespace ) { return true; }
-               if($action == 'edit' || $action == '') {
-                       $a = $this->getRestrictions("edit");
-                       if ( in_array( 'sysop', $a ) ) { return true; }
+       function isProtected( $action = '' ) {
+               global $wgRestrictionLevels;
+               if ( NS_SPECIAL == $this->mNamespace ) { return true; }
+                               
+               if( $action == 'edit' || $action == '' ) {
+                       $r = $this->getRestrictions( 'edit' );
+                       foreach( $wgRestrictionLevels as $level ) {
+                               if( in_array( $level, $r ) && $level != '' ) {
+                                       return( true );
+                               }
+                       }
                }
-               if($action == 'move' || $action == '') {
-                       $a = $this->getRestrictions("move");
-                       if ( in_array( 'sysop', $a ) ) { return true; }
+               
+               if( $action == 'move' || $action == '' ) {
+                       $r = $this->getRestrictions( 'move' );
+                       foreach( $wgRestrictionLevels as $level ) {
+                               if( in_array( $level, $r ) && $level != '' ) {
+                                       return( true );
+                               }
+                       }
                }
+
                return false;
        }
 
@@ -849,7 +1061,7 @@ class Title {
                global $wgUser;
 
                if ( is_null( $this->mWatched ) ) {
-                       if ( -1 == $this->mNamespace || 0 == $wgUser->getID()) {
+                       if ( NS_SPECIAL == $this->mNamespace || !$wgUser->isLoggedIn()) {
                                $this->mWatched = false;
                        } else {
                                $this->mWatched = $wgUser->isWatched( $this );
@@ -862,13 +1074,21 @@ class Title {
         * Can $wgUser perform $action this page?
         * @param string $action action that permission needs to be checked for
         * @return boolean
-        * @access private
+        * @private
         */
        function userCan($action) {
                $fname = 'Title::userCan';
                wfProfileIn( $fname );
 
                global $wgUser;
+
+               $result = null;
+               wfRunHooks( 'userCan', array( &$this, &$wgUser, $action, &$result ) );
+               if ( $result !== null ) {
+                       wfProfileOut( $fname );
+                       return $result;
+               }
+
                if( NS_SPECIAL == $this->mNamespace ) {
                        wfProfileOut( $fname );
                        return false;
@@ -887,19 +1107,10 @@ class Title {
                        return false;
                }
 
-               # protect global styles and js
-               if ( NS_MEDIAWIKI == $this->mNamespace
-                && preg_match("/\\.(css|js)$/", $this->mTextform )
-                    && !$wgUser->isAllowed('editinterface') ) {
-                       wfProfileOut( $fname );
-                       return false;
-               }
-
                # protect css/js subpages of user pages
                # XXX: this might be better using restrictions
                # XXX: Find a way to work around the php bug that prevents using $this->userCanEditCssJsSubpage() from working
-               if( NS_USER == $this->mNamespace
-                       && preg_match("/\\.(css|js)$/", $this->mTextform )
+               if( $this->isCssJsSubpage()
                        && !$wgUser->isAllowed('editinterface')
                        && !preg_match('/^'.preg_quote($wgUser->getName(), '/').'\//', $this->mTextform) ) {
                        wfProfileOut( $fname );
@@ -926,6 +1137,7 @@ class Title {
                if( $action == 'create' ) {
                        if( (  $this->isTalkPage() && !$wgUser->isAllowed( 'createtalk' ) ) ||
                                ( !$this->isTalkPage() && !$wgUser->isAllowed( 'createpage' ) ) ) {
+                               wfProfileOut( $fname );
                                return false;
                        }
                }
@@ -943,6 +1155,15 @@ class Title {
                return $this->userCan('edit');
        }
 
+       /**
+        * Can $wgUser create this page?
+        * @return boolean
+        * @access public
+        */
+       function userCanCreate() {
+               return $this->userCan('create');
+       }
+
        /**
         * Can $wgUser move this page?
         * @return boolean
@@ -972,16 +1193,22 @@ class Title {
        function userCanRead() {
                global $wgUser;
 
+               $result = null;
+               wfRunHooks( 'userCan', array( &$this, &$wgUser, 'read', &$result ) );
+               if ( $result !== null ) {
+                       return $result;
+               }
+
                if( $wgUser->isAllowed('read') ) {
                        return true;
                } else {
                        global $wgWhitelistRead;
 
-                       /** If anon users can create an account,
-                           they need to reach the login page first! */
-                       if( $wgUser->isAllowed( 'createaccount' )
-                           && $this->getNamespace() == NS_SPECIAL
-                           && $this->getText() == 'Userlogin' ) {
+                       /** 
+                        * Always grant access to the login page.
+                        * Even anons need to be able to log in.
+                       */
+                       if( $this->isSpecial( 'Userlogin' ) || $this->isSpecial( 'Resetpass' ) ) {
                                return true;
                        }
 
@@ -1010,13 +1237,48 @@ class Title {
                return Namespace::isTalk( $this->getNamespace() );
        }
 
+       /**
+        * Is this a subpage?
+        * @return bool
+        * @access public
+        */
+       function isSubpage() {
+               global $wgNamespacesWithSubpages;
+               
+               if( isset( $wgNamespacesWithSubpages[ $this->mNamespace ] ) ) {
+                       return ( strpos( $this->getText(), '/' ) !== false && $wgNamespacesWithSubpages[ $this->mNamespace ] == true );
+               } else {
+                       return false;
+               }
+       }
+
        /**
         * Is this a .css or .js subpage of a user page?
         * @return bool
         * @access public
         */
        function isCssJsSubpage() {
-               return ( NS_USER == $this->mNamespace and preg_match("/\\.(css|js)$/", $this->mTextform ) );
+               return ( NS_USER == $this->mNamespace and preg_match("/\\/.*\\.(css|js)$/", $this->mTextform ) );
+       }
+       /**
+        * Is this a *valid* .css or .js subpage of a user page?
+        * Check that the corresponding skin exists
+        */
+       function isValidCssJsSubpage() {
+               if ( $this->isCssJsSubpage() ) {
+                       $skinNames = Skin::getSkinNames();
+                       return array_key_exists( $this->getSkinFromCssJsSubpage(), $skinNames );
+               } else {
+                       return false;
+               }
+       }
+       /**
+        * Trim down a .css or .js subpage title to get the corresponding skin name
+        */
+       function getSkinFromCssJsSubpage() {
+               $subpage = explode( '/', $this->mTextform );
+               $subpage = $subpage[ count( $subpage ) - 1 ];
+               return( str_replace( array( '.css', '.js' ), array( '', '' ), $subpage ) );
        }
        /**
         * Is this a .css subpage of a user page?
@@ -1024,7 +1286,7 @@ class Title {
         * @access public
         */
        function isCssSubpage() {
-               return ( NS_USER == $this->mNamespace and preg_match("/\\.css$/", $this->mTextform ) );
+               return ( NS_USER == $this->mNamespace and preg_match("/\\/.*\\.css$/", $this->mTextform ) );
        }
        /**
         * Is this a .js subpage of a user page?
@@ -1032,7 +1294,7 @@ class Title {
         * @access public
         */
        function isJsSubpage() {
-               return ( NS_USER == $this->mNamespace and preg_match("/\\.js$/", $this->mTextform ) );
+               return ( NS_USER == $this->mNamespace and preg_match("/\\/.*\\.js$/", $this->mTextform ) );
        }
        /**
         * Protect css/js subpages of user pages: can $wgUser edit
@@ -1053,6 +1315,15 @@ class Title {
         * @access public
         */
        function loadRestrictions( $res ) {
+               $this->mRestrictions['edit'] = array();
+               $this->mRestrictions['move'] = array();
+               
+               if( !$res ) {
+                       # No restrictions (page_restrictions blank)
+                       $this->mRestrictionsLoaded = true;
+                       return;
+               }
+       
                foreach( explode( ':', trim( $res ) ) as $restrict ) {
                        $temp = explode( '=', trim( $restrict ) );
                        if(count($temp) == 1) {
@@ -1068,23 +1339,24 @@ class Title {
 
        /**
         * Accessor/initialisation for mRestrictions
+        *
+        * @access public
         * @param string $action action that permission needs to be checked for
         * @return array the array of groups allowed to edit this article
-        * @access public
         */
-       function getRestrictions($action) {
-               $id = $this->getArticleID();
-               if ( 0 == $id ) { return array(); }
-
-               if ( ! $this->mRestrictionsLoaded ) {
-                       $dbr =& wfGetDB( DB_SLAVE );
-                       $res = $dbr->selectField( 'page', 'page_restrictions', 'page_id='.$id );
-                       $this->loadRestrictions( $res );
-               }
-               if( isset( $this->mRestrictions[$action] ) ) {
-                       return $this->mRestrictions[$action];
+       function getRestrictions( $action ) {
+               if( $this->exists() ) {
+                       if( !$this->mRestrictionsLoaded ) {
+                               $dbr =& wfGetDB( DB_SLAVE );
+                               $res = $dbr->selectField( 'page', 'page_restrictions', array( 'page_id' => $this->getArticleId() ) );
+                               $this->loadRestrictions( $res );
+                       }
+                       return isset( $this->mRestrictions[$action] )
+                                       ? $this->mRestrictions[$action]
+                                       : array();
+               } else {
+                       return array();
                }
-               return array();
        }
 
        /**
@@ -1100,6 +1372,10 @@ class Title {
                        $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 (int)$n;
        }
@@ -1181,7 +1457,7 @@ class Title {
                );
 
                if ($wgUseFileCache) {
-                       $cache = new CacheManager($this);
+                       $cache = new HTMLFileCache($this);
                        @unlink($cache->fileCacheName());
                }
 
@@ -1194,17 +1470,15 @@ class Title {
         *
         * @param string $name the text
         * @return string the prefixed text
-        * @access private
+        * @private
         */
        /* private */ function prefix( $name ) {
-               global $wgContLang;
-
                $p = '';
                if ( '' != $this->mInterwiki ) {
                        $p = $this->mInterwiki . ':';
                }
                if ( 0 != $this->mNamespace ) {
-                       $p .= $wgContLang->getNsText( $this->mNamespace ) . ':';
+                       $p .= $this->getNsText() . ':';
                }
                return $p . $name;
        }
@@ -1218,12 +1492,10 @@ class Title {
         * namespace prefixes, sets the other forms, and canonicalizes
         * everything.
         * @return bool true on success
-        * @access private
+        * @private
         */
        /* private */ function secureAndSplit() {
                global $wgContLang, $wgLocalInterwiki, $wgCapitalLinks;
-               $fname = 'Title::secureAndSplit';
-               wfProfileIn( $fname );
 
                # Initialisation
                static $rxTc = false;
@@ -1234,63 +1506,68 @@ class Title {
 
                $this->mInterwiki = $this->mFragment = '';
                $this->mNamespace = $this->mDefaultNamespace; # Usually NS_MAIN
-
+               
+               $dbkey = $this->mDbkeyform;
+
+               # Strip Unicode bidi override characters.
+               # Sometimes they slip into cut-n-pasted page titles, where the
+               # override chars get included in list displays.
+               $dbkey = str_replace( "\xE2\x80\x8E", '', $dbkey ); // 200E LEFT-TO-RIGHT MARK
+               $dbkey = str_replace( "\xE2\x80\x8F", '', $dbkey ); // 200F RIGHT-TO-LEFT MARK
+               
                # Clean up whitespace
                #
-               $t = preg_replace( '/[ _]+/', '_', $this->mDbkeyform );
-               $t = trim( $t, '_' );
+               $dbkey = preg_replace( '/[ _]+/', '_', $dbkey );
+               $dbkey = trim( $dbkey, '_' );
 
-               if ( '' == $t ) {
-                       wfProfileOut( $fname );
+               if ( '' == $dbkey ) {
                        return false;
                }
 
-               if( false !== strpos( $t, UTF8_REPLACEMENT ) ) {
+               if( false !== strpos( $dbkey, UTF8_REPLACEMENT ) ) {
                        # Contained illegal UTF-8 sequences or forbidden Unicode chars.
-                       wfProfileOut( $fname );
                        return false;
                }
 
-               $this->mDbkeyform = $t;
+               $this->mDbkeyform = $dbkey;
 
                # Initial colon indicates main namespace rather than specified default
                # but should not create invalid {ns,title} pairs such as {0,Project:Foo}
-               if ( ':' == $t{0} ) {
+               if ( ':' == $dbkey{0} ) {
                        $this->mNamespace = NS_MAIN;
-                       $t = substr( $t, 1 ); # remove the colon but continue processing
+                       $dbkey = substr( $dbkey, 1 ); # remove the colon but continue processing
                }
 
                # Namespace or interwiki prefix
                $firstPass = true;
                do {
-                       if ( preg_match( "/^(.+?)_*:_*(.*)$/S", $t, $m ) ) {
+                       $m = array();
+                       if ( preg_match( "/^(.+?)_*:_*(.*)$/S", $dbkey, $m ) ) {
                                $p = $m[1];
-                               $lowerNs = strtolower( $p );
+                               $lowerNs = $wgContLang->lc( $p );
                                if ( $ns = Namespace::getCanonicalIndex( $lowerNs ) ) {
                                        # Canonical namespace
-                                       $t = $m[2];
+                                       $dbkey = $m[2];
                                        $this->mNamespace = $ns;
                                } elseif ( $ns = $wgContLang->getNsIndex( $lowerNs )) {
                                        # Ordinary namespace
-                                       $t = $m[2];
+                                       $dbkey = $m[2];
                                        $this->mNamespace = $ns;
                                } elseif( $this->getInterwikiLink( $p ) ) {
                                        if( !$firstPass ) {
                                                # Can't make a local interwiki link to an interwiki link.
                                                # That's just crazy!
-                                               wfProfileOut( $fname );
                                                return false;
                                        }
 
                                        # Interwiki link
-                                       $t = $m[2];
-                                       $this->mInterwiki = strtolower( $p );
+                                       $dbkey = $m[2];
+                                       $this->mInterwiki = $wgContLang->lc( $p );
 
                                        # Redundant interwiki prefix to the local wiki
                                        if ( 0 == strcasecmp( $this->mInterwiki, $wgLocalInterwiki ) ) {
-                                               if( $t == '' ) {
+                                               if( $dbkey == '' ) {
                                                        # Can't have an empty self-link
-                                                       wfProfileOut( $fname );
                                                        return false;
                                                }
                                                $this->mInterwiki = '';
@@ -1298,32 +1575,37 @@ class Title {
                                                # Do another namespace split...
                                                continue;
                                        }
+
+                                       # If there's an initial colon after the interwiki, that also
+                                       # resets the default namespace
+                                       if ( $dbkey !== '' && $dbkey[0] == ':' ) {
+                                               $this->mNamespace = NS_MAIN;
+                                               $dbkey = substr( $dbkey, 1 );
+                                       }
                                }
                                # If there's no recognized interwiki or namespace,
                                # then let the colon expression be part of the title.
                        }
                        break;
                } while( true );
-               $r = $t;
 
                # We already know that some pages won't be in the database!
                #
-               if ( '' != $this->mInterwiki || -1 == $this->mNamespace ) {
+               if ( '' != $this->mInterwiki || NS_SPECIAL == $this->mNamespace ) {
                        $this->mArticleID = 0;
                }
-               $f = strstr( $r, '#' );
-               if ( false !== $f ) {
-                       $this->mFragment = substr( $f, 1 );
-                       $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
+               $fragment = strstr( $dbkey, '#' );
+               if ( false !== $fragment ) {
+                       $this->setFragment( $fragment );
+                       $dbkey = substr( $dbkey, 0, strlen( $dbkey ) - strlen( $fragment ) );
                        # remove whitespace again: prevents "Foo_bar_#"
                        # becoming "Foo_bar_"
-                       $r = preg_replace( '/_*$/', '', $r );
+                       $dbkey = preg_replace( '/_*$/', '', $dbkey );
                }
 
                # Reject illegal characters.
                #
-               if( preg_match( $rxTc, $r ) ) {
-                       wfProfileOut( $fname );
+               if( preg_match( $rxTc, $dbkey ) ) {
                        return false;
                }
 
@@ -1332,21 +1614,26 @@ class Title {
                 * often be unreachable due to the way web browsers deal
                 * with 'relative' URLs. Forbid them explicitly.
                 */
-               if ( strpos( $r, '.' ) !== false &&
-                    ( $r === '.' || $r === '..' ||
-                      strpos( $r, './' ) === 0  ||
-                      strpos( $r, '../' ) === 0 ||
-                      strpos( $r, '/./' ) !== false ||
-                      strpos( $r, '/../' ) !== false ) )
+               if ( strpos( $dbkey, '.' ) !== false &&
+                    ( $dbkey === '.' || $dbkey === '..' ||
+                      strpos( $dbkey, './' ) === 0  ||
+                      strpos( $dbkey, '../' ) === 0 ||
+                      strpos( $dbkey, '/./' ) !== false ||
+                      strpos( $dbkey, '/../' ) !== false ) )
                {
-                       wfProfileOut( $fname );
                        return false;
                }
 
-               # We shouldn't need to query the DB for the size.
-               #$maxSize = $dbr->textFieldSize( 'page', 'page_title' );
-               if ( strlen( $r ) > 255 ) {
-                       wfProfileOut( $fname );
+               /**
+                * Limit the size of titles to 255 bytes.
+                * This is typically the size of the underlying database field.
+                * We make an exception for special pages, which don't need to be stored
+                * in the database, and may edge over 255 bytes due to subpage syntax 
+                * for long titles, e.g. [[Special:Block/Long name]]
+                */
+               if ( ( $this->mNamespace != NS_SPECIAL && strlen( $dbkey ) > 255 ) ||
+                 strlen( $dbkey ) > 512 ) 
+               {
                        return false;
                }
 
@@ -1359,9 +1646,7 @@ class Title {
                 * site might be case-sensitive.
                 */
                if( $wgCapitalLinks && $this->mInterwiki == '') {
-                       $t = $wgContLang->ucfirst( $r );
-               } else {
-                       $t = $r;
+                       $dbkey = $wgContLang->ucfirst( $dbkey );
                }
 
                /**
@@ -1369,23 +1654,39 @@ class Title {
                 * "empty" local links can only be self-links
                 * with a fragment identifier.
                 */
-               if( $t == '' &&
+               if( $dbkey == '' &&
                        $this->mInterwiki == '' &&
                        $this->mNamespace != NS_MAIN ) {
-                       wfProfileOut( $fname );
                        return false;
                }
 
+               // Any remaining initial :s are illegal.
+               if ( $dbkey !== '' && ':' == $dbkey{0} ) {
+                       return false;
+               }
+               
                # Fill fields
-               $this->mDbkeyform = $t;
-               $this->mUrlform = wfUrlencode( $t );
+               $this->mDbkeyform = $dbkey;
+               $this->mUrlform = wfUrlencode( $dbkey );
 
-               $this->mTextform = str_replace( '_', ' ', $t );
+               $this->mTextform = str_replace( '_', ' ', $dbkey );
 
-               wfProfileOut( $fname );
                return true;
        }
 
+       /**
+        * Set the fragment for this title
+        * This is kind of bad, since except for this rarely-used function, Title objects
+        * are immutable. The reason this is here is because it's better than setting the 
+        * members directly, which is what Linker::formatComment was doing previously.
+        *
+        * @param string $fragment text
+        * @access kind of public
+        */
+       function setFragment( $fragment ) {
+               $this->mFragment = str_replace( '_', ' ', substr( $fragment, 1 ) );
+       }
+
        /**
         * Get a Title object associated with the talk page of this article
         * @return Title the object for the talk page
@@ -1410,13 +1711,15 @@ class Title {
         * Get an array of Title objects linking to this Title
         * Also stores the IDs in the link cache.
         *
+        * WARNING: do not use this function on arbitrary user-supplied titles!
+        * On heavily-used templates it will max out the memory.
+        *
         * @param string $options may be FOR UPDATE
         * @return array the Title objects linking here
         * @access public
         */
        function getLinksTo( $options = '', $table = 'pagelinks', $prefix = 'pl' ) {
                $linkCache =& LinkCache::singleton();
-               $id = $this->getArticleID();
 
                if ( $options ) {
                        $db =& wfGetDB( DB_MASTER );
@@ -1450,6 +1753,9 @@ class Title {
         * Get an array of Title objects using this Title as a template
         * Also stores the IDs in the link cache.
         *
+        * WARNING: do not use this function on arbitrary user-supplied titles!
+        * On heavily-used templates it will max out the memory.
+        *
         * @param string $options may be FOR UPDATE
         * @return array the Title objects linking here
         * @access public
@@ -1480,7 +1786,7 @@ class Title {
                              AND pl_title=page_title
                            WHERE pl_from=?
                              AND page_namespace IS NULL
-                                 !",
+                                 !",
                        $db->tableName( 'pagelinks' ),
                        $db->tableName( 'page' ),
                        $this->getArticleId(),
@@ -1505,10 +1811,32 @@ class Title {
         * @access public
         */
        function getSquidURLs() {
-               return array(
+               global $wgContLang;
+
+               $urls = array(
                        $this->getInternalURL(),
                        $this->getInternalURL( 'action=history' )
                );
+
+               // purge variant urls as well
+               if($wgContLang->hasVariants()){
+                       $variants = $wgContLang->getVariants();
+                       foreach($variants as $vCode){
+                               if($vCode==$wgContLang->getCode()) continue; // we don't want default variant
+                               $urls[] = $this->getInternalURL('',$vCode);
+                       }
+               }
+
+               return $urls;
+       }
+
+       function purgeSquid() {
+               global $wgUseSquid;
+               if ( $wgUseSquid ) {
+                       $urls = $this->getSquidURLs();
+                       $u = new SquidUpdate( $urls );
+                       $u->doUpdate();
+               }
        }
 
        /**
@@ -1531,7 +1859,6 @@ class Title {
         * @access public
         */
        function isValidMoveOperation( &$nt, $auth = true ) {
-               global $wgUser;
                if( !$this or !$nt ) {
                        return 'badtitletext';
                }
@@ -1651,10 +1978,10 @@ class Title {
         *
         * @param Title &$nt the page to move to, which should currently
         *      be a redirect
-        * @access private
+        * @private
         */
        function moveOverExistingRedirect( &$nt, $reason = '' ) {
-               global $wgUser, $wgUseSquid, $wgMwRedir;
+               global $wgUseSquid;
                $fname = 'Title::moveOverExistingRedirect';
                $comment = wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
 
@@ -1663,7 +1990,6 @@ class Title {
                }
 
                $now = wfTimestampNow();
-               $rand = wfRandom();
                $newid = $nt->getArticleID();
                $oldid = $this->getArticleID();
                $dbw =& wfGetDB( DB_MASTER );
@@ -1693,14 +2019,15 @@ class Title {
                $linkCache->clearLink( $nt->getPrefixedDBkey() );
 
                # Recreate the redirect, this time in the other direction.
-               $redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
+               $mwRedir = MagicWord::get( 'redirect' );
+               $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
                $redirectArticle = new Article( $this );
                $newid = $redirectArticle->insertOn( $dbw );
                $redirectRevision = new Revision( array(
                        'page'    => $newid,
                        'comment' => $comment,
                        'text'    => $redirectText ) );
-               $revid = $redirectRevision->insertOn( $dbw );
+               $redirectRevision->insertOn( $dbw );
                $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
                $linkCache->clearLink( $this->getPrefixedDBkey() );
 
@@ -1729,11 +2056,10 @@ class Title {
        /**
         * Move page to non-existing title.
         * @param Title &$nt the new Title
-        * @access private
+        * @private
         */
        function moveToNewTitle( &$nt, $reason = '' ) {
-               global $wgUser, $wgUseSquid;
-               global $wgMwRedir;
+               global $wgUseSquid;
                $fname = 'MovePageForm::moveToNewTitle';
                $comment = wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
                if ( $reason ) {
@@ -1744,7 +2070,6 @@ class Title {
                $oldid = $this->getArticleID();
                $dbw =& wfGetDB( DB_MASTER );
                $now = $dbw->timestamp();
-               $rand = wfRandom();
                $linkCache =& LinkCache::singleton();
 
                # Save a null revision in the page's history notifying of the move
@@ -1766,14 +2091,15 @@ class Title {
                $linkCache->clearLink( $nt->getPrefixedDBkey() );
 
                # Insert redirect
-               $redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
+               $mwRedir = MagicWord::get( 'redirect' );
+               $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
                $redirectArticle = new Article( $this );
                $newid = $redirectArticle->insertOn( $dbw );
                $redirectRevision = new Revision( array(
                        'page'    => $newid,
                        'comment' => $comment,
                        'text'    => $redirectText ) );
-               $revid = $redirectRevision->insertOn( $dbw );
+               $redirectRevision->insertOn( $dbw );
                $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
                $linkCache->clearLink( $this->getPrefixedDBkey() );
 
@@ -1792,21 +2118,9 @@ class Title {
                                'pl_title'     => $nt->getDBkey() ),
                        $fname );
 
-               # Non-existent target may have had broken links to it; these must
-               # now be touched to update link coloring.
-               $nt->touchLinks();
-
                # Purge old title from squid
                # The new title, and links to the new title, are purged in Article::onArticleCreate()
-               $titles = $nt->getLinksTo();
-               if ( $wgUseSquid ) {
-                       $urls = $this->getSquidURLs();
-                       foreach ( $titles as $linkTitle ) {
-                               $urls[] = $linkTitle->getInternalURL();
-                       }
-                       $u = new SquidUpdate( $urls );
-                       $u->doUpdate();
-               }
+               $this->purgeSquid();
        }
 
        /**
@@ -1830,19 +2144,25 @@ class Title {
 
                if ( !$obj || 0 == $obj->page_is_redirect ) {
                        # Not a redirect
+                       wfDebug( __METHOD__ . ": not a redirect\n" );
                        return false;
                }
                $text = Revision::getRevisionText( $obj );
 
                # Does the redirect point to the source?
+               # Or is it a broken self-redirect, usually caused by namespace collisions?
+               $m = array();
                if ( preg_match( "/\\[\\[\\s*([^\\]\\|]*)]]/", $text, $m ) ) {
                        $redirTitle = Title::newFromText( $m[1] );
                        if( !is_object( $redirTitle ) ||
-                               $redirTitle->getPrefixedDBkey() != $this->getPrefixedDBkey() ) {
+                               ( $redirTitle->getPrefixedDBkey() != $this->getPrefixedDBkey() &&
+                               $redirTitle->getPrefixedDBkey() != $nt->getPrefixedDBkey() ) ) {
+                               wfDebug( __METHOD__ . ": redirect points to other page\n" );
                                return false;
                        }
                } else {
                        # Fail safe
+                       wfDebug( __METHOD__ . ": failsafe\n" );
                        return false;
                }
 
@@ -1869,7 +2189,6 @@ class Title {
         * @access public
         */
        function createRedirect( $dest, $comment ) {
-               global $wgUser;
                if ( $this->getArticleID() ) {
                        return false;
                }
@@ -1884,7 +2203,7 @@ class Title {
                        'comment'   => $comment,
                        'text'      => "#REDIRECT [[" . $dest->getPrefixedText() . "]]\n",
                        ) );
-               $revisionId = $revision->insertOn( $dbw );
+               $revision->insertOn( $dbw );
                $article->updateRevisionOn( $dbw, $revision, 0 );
 
                # Link table
@@ -1909,7 +2228,7 @@ class Title {
         * @access public
         */
        function getParentCategories() {
-               global $wgContLang,$wgUser;
+               global $wgContLang;
 
                $titlekey = $this->getArticleId();
                $dbr =& wfGetDB( DB_SLAVE );
@@ -1950,7 +2269,9 @@ class Title {
                                        $stack[$parent] = array();
                                } else {
                                        $nt = Title::newFromText($parent);
-                                       $stack[$parent] = $nt->getParentCategoryTree( $children + array($parent => 1) );
+                                       if ( $nt ) {
+                                               $stack[$parent] = $nt->getParentCategoryTree( $children + array($parent => 1) );
+                                       }
                                }
                        }
                        return $stack;
@@ -1975,7 +2296,7 @@ class Title {
         * Get the revision ID of the previous revision
         *
         * @param integer $revision  Revision ID. Get the revision that was before this one.
-        * @return interger $oldrevision|false
+        * @return integer $oldrevision|false
         */
        function getPreviousRevisionID( $revision ) {
                $dbr =& wfGetDB( DB_SLAVE );
@@ -1988,7 +2309,7 @@ class Title {
         * Get the revision ID of the next revision
         *
         * @param integer $revision  Revision ID. Get the revision that was after this one.
-        * @return interger $oldrevision|false
+        * @return integer $oldrevision|false
         */
        function getNextRevisionID( $revision ) {
                $dbr =& wfGetDB( DB_SLAVE );
@@ -1997,6 +2318,21 @@ class Title {
                        ' AND rev_id>' . intval( $revision ) . ' ORDER BY rev_id' );
        }
 
+       /**
+        * Get the number of revisions between the given revision IDs.
+        *
+        * @param integer $old  Revision ID.
+        * @param integer $new  Revision ID.
+        * @return integer  Number of revisions between these IDs.
+        */
+       function countRevisionsBetween( $old, $new ) {
+               $dbr =& wfGetDB( DB_SLAVE );
+               return $dbr->selectField( 'revision', 'count(*)',
+                       'rev_page = ' . intval( $this->getArticleId() ) .
+                       ' AND rev_id > ' . intval( $old ) .
+                       ' AND rev_id < ' . intval( $new ) );
+       }
+
        /**
         * Compare with another title.
         *
@@ -2004,9 +2340,10 @@ class Title {
         * @return bool
         */
        function equals( $title ) {
-               return $this->getInterwiki() == $title->getInterwiki()
+               // Note: === is necessary for proper matching of number-like titles.
+               return $this->getInterwiki() === $title->getInterwiki()
                        && $this->getNamespace() == $title->getNamespace()
-                       && $this->getDbkey() == $title->getDbkey();
+                       && $this->getDbkey() === $title->getDbkey();
        }
 
        /**
@@ -2029,44 +2366,44 @@ class Title {
        }
 
        /**
-        * Update page_touched timestamps on pages linking to this title.
-        * In principal, this could be backgrounded and could also do squid
-        * purging.
+        * Update page_touched timestamps and send squid purge messages for
+        * pages linking to this title. May be sent to the job queue depending 
+        * on the number of links. Typically called on create and delete.
         */
        function touchLinks() {
-               $fname = 'Title::touchLinks';
-
-               $dbw =& wfGetDB( DB_MASTER );
-
-               $res = $dbw->select( 'pagelinks',
-                       array( 'pl_from' ),
-                       array(
-                               'pl_namespace' => $this->getNamespace(),
-                               'pl_title'     => $this->getDbKey() ),
-                       $fname );
+               $u = new HTMLCacheUpdate( $this, 'pagelinks' );
+               $u->doUpdate();
 
-               $toucharr = array();
-               while( $row = $dbw->fetchObject( $res ) ) {
-                       $toucharr[] = $row->pl_from;
+               if ( $this->getNamespace() == NS_CATEGORY ) {
+                       $u = new HTMLCacheUpdate( $this, 'categorylinks' );
+                       $u->doUpdate();
                }
-               $dbw->freeResult( $res );
+       }
 
-               if( $this->getNamespace() == NS_CATEGORY ) {
-                       // Categories show up in a separate set of links as well
-                       $res = $dbw->select( 'categorylinks',
-                               array( 'cl_from' ),
-                               array( 'cl_to' => $this->getDbKey() ),
-                               $fname );
-                       while( $row = $dbw->fetchObject( $res ) ) {
-                               $toucharr[] = $row->cl_from;
-                       }
-                       $dbw->freeResult( $res );
-               }
+       /**
+        * Get the last touched timestamp
+        */
+       function getTouched() {
+               $dbr =& wfGetDB( DB_SLAVE );
+               $touched = $dbr->selectField( 'page', 'page_touched',
+                       array( 
+                               'page_namespace' => $this->getNamespace(),
+                               'page_title' => $this->getDBkey()
+                       ), __METHOD__
+               );
+               return $touched;
+       }
 
-               if (!count($toucharr))
-                       return;
-               $dbw->update( 'page', /* SET */ array( 'page_touched' => $dbw->timestamp() ),
-                                                       /* WHERE */ array( 'page_id' => $toucharr ),$fname);
+       /**
+        * Get a cached value from a global cache that is invalidated when this page changes
+        * @param string $key the key
+        * @param callback $callback A callback function which generates the value on cache miss
+        *
+        * @deprecated use DependencyWrapper
+        */
+       function getRelatedCache( $memc, $key, $expiry, $callback, $params = array() ) {
+               return DependencyWrapper::getValueFromCache( $memc, $key, $expiry, $callback, 
+                       $params, new TitleDependency( $this ) );
        }
 
        function trackbackURL() {
@@ -2092,5 +2429,77 @@ class Title {
    trackback:ping=\"$tburl\" />
 </rdf:RDF>";
        }
+
+       /**
+        * Generate strings used for xml 'id' names in monobook tabs
+        * @return string
+        */
+       function getNamespaceKey() {
+               global $wgContLang;
+               switch ($this->getNamespace()) {
+                       case NS_MAIN:
+                       case NS_TALK:
+                               return 'nstab-main';
+                       case NS_USER:
+                       case NS_USER_TALK:
+                               return 'nstab-user';
+                       case NS_MEDIA:
+                               return 'nstab-media';
+                       case NS_SPECIAL:
+                               return 'nstab-special';
+                       case NS_PROJECT:
+                       case NS_PROJECT_TALK:
+                               return 'nstab-project';
+                       case NS_IMAGE:
+                       case NS_IMAGE_TALK:
+                               return 'nstab-image';
+                       case NS_MEDIAWIKI:
+                       case NS_MEDIAWIKI_TALK:
+                               return 'nstab-mediawiki';
+                       case NS_TEMPLATE:
+                       case NS_TEMPLATE_TALK:
+                               return 'nstab-template';
+                       case NS_HELP:
+                       case NS_HELP_TALK:
+                               return 'nstab-help';
+                       case NS_CATEGORY:
+                       case NS_CATEGORY_TALK:
+                               return 'nstab-category';
+                       default:
+                               return 'nstab-' . $wgContLang->lc( $this->getSubjectNsText() );
+               }
+       }
+
+       /**
+        * Returns true if this title resolves to the named special page
+        * @param string $name The special page name
+        * @access public
+        */
+       function isSpecial( $name ) {
+               if ( $this->getNamespace() == NS_SPECIAL ) {
+                       list( $thisName, /* $subpage */ ) = SpecialPage::resolveAliasWithSubpage( $this->getDBkey() );
+                       if ( $name == $thisName ) {
+                               return true;
+                       }
+               }
+               return false;
+       }
+
+       /**
+        * If the Title refers to a special page alias which is not the local default, 
+        * returns a new Title which points to the local default. Otherwise, returns $this.
+        */
+       function fixSpecialName() {
+               if ( $this->getNamespace() == NS_SPECIAL ) {
+                       $canonicalName = SpecialPage::resolveAlias( $this->mDbkeyform );
+                       if ( $canonicalName ) {
+                               $localName = SpecialPage::getLocalNameFor( $canonicalName );
+                               if ( $localName != $this->mDbkeyform ) {
+                                       return Title::makeTitle( NS_SPECIAL, $localName );
+                               }
+                       }
+               }
+               return $this;
+       }
 }
 ?>