(bug 7098) Add an option to disable/enable sending of HTTP ETag headers,
[lhc/web/wiklou.git] / includes / Title.php
index a4dc83f..6f740dc 100644 (file)
@@ -8,7 +8,6 @@
 /** */
 require_once( 'normal/UtfNormal.php' );
 
-$wgTitleInterwikiCache = array();
 define ( 'GAID_FOR_UPDATE', 1 );
 
 # Title::newFromTitle maintains a cache to avoid
@@ -26,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
@@ -44,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 =
@@ -102,12 +108,11 @@ class Title {
         * @static
         * @access public
         */
-       function newFromText( $text, $defaultNamespace = NS_MAIN ) {
+       public static function newFromText( $text, $defaultNamespace = NS_MAIN ) {
                $fname = 'Title::newFromText';
-               wfProfileIn( $fname );
 
                if( is_object( $text ) ) {
-                       wfDebugDieBacktrace( 'Title::newFromText given an object' );
+                       throw new MWException( 'Title::newFromText given an object' );
                }
 
                /**
@@ -118,10 +123,8 @@ class Title {
                 *
                 * In theory these are value objects and won't get changed...
                 */
-               static $titleCache = array();
-               if( $defaultNamespace == NS_MAIN && isset( $titleCache[$text] ) ) {
-                       wfProfileOut( $fname );
-                       return $titleCache[$text];
+               if( $defaultNamespace == NS_MAIN && isset( Title::$titleCache[$text] ) ) {
+                       return Title::$titleCache[$text];
                }
 
                /**
@@ -129,22 +132,23 @@ class Title {
                 */
                $filteredText = Sanitizer::decodeCharReferences( $text );
 
-               $t =& new Title();
+               $t = new Title();
                $t->mDbkeyform = str_replace( ' ', '_', $filteredText );
                $t->mDefaultNamespace = $defaultNamespace;
 
+               static $cachedcount = 0 ;
                if( $t->secureAndSplit() ) {
                        if( $defaultNamespace == NS_MAIN ) {
-                               if( count( $titleCache ) >= MW_TITLECACHE_MAX ) {
+                               if( $cachedcount >= MW_TITLECACHE_MAX ) {
                                        # Avoid memory leaks on mass operations...
-                                       $titleCache = array();
+                                       Title::$titleCache = array();
+                                       $cachedcount=0;
                                }
-                               $titleCache[$text] =& $t;
+                               $cachedcount++;
+                               Title::$titleCache[$text] =& $t;
                        }
-                       wfProfileOut( $fname );
                        return $t;
                } else {
-                       wfProfileOut( $fname );
                        $ret = NULL;
                        return $ret;
                }
@@ -159,15 +163,17 @@ class Title {
         * @access public
         */
        function newFromURL( $url ) {
-               global $wgLang, $wgServer;
+               global $wgLegalTitleChars;
                $t = new Title();
 
-               # For compatibility with old buggy URLs. "+" is not valid in titles,
+               # For compatibility with old buggy URLs. "+" is usually not valid in titles,
                # but some URLs used it as a space replacement and they still come
                # from some external search tools.
-               $s = str_replace( '+', ' ', $url );
+               if ( strpos( $wgLegalTitleChars, '+' ) === false ) {
+                       $url = str_replace( '+', ' ', $url );
+               }
 
-               $t->mDbkeyform = str_replace( ' ', '_', $s );
+               $t->mDbkeyform = str_replace( ' ', '_', $url );
                if( $t->secureAndSplit() ) {
                        return $t;
                } else {
@@ -199,6 +205,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
@@ -212,8 +233,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 );
@@ -225,7 +246,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.
         *
@@ -235,7 +256,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() ) {
@@ -252,7 +273,7 @@ class Title {
         * @return Title the new object
         * @access public
         */
-       function newMainPage() {
+       public static function newMainPage() {
                return Title::newFromText( wfMsgForContent( 'mainpage' ) );
        }
 
@@ -264,10 +285,10 @@ 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 ) ) {
                        if ( preg_match( '/\[{2}(.*?)(?:\||\]{2})/', $text, $m ) ) {
                                # categories are escaped using : for example one can enter:
                                # #REDIRECT [[:Category:Music]]. Need to remove it.
@@ -315,7 +336,7 @@ class Title {
         * @static
         * @access public
         */
-       function legalChars() {
+       public static function legalChars() {
                global $wgLegalTitleChars;
                return $wgLegalTitleChars;
        }
@@ -330,8 +351,7 @@ class Title {
         *      search index
         */
        /* static */ function indexTitle( $ns, $title ) {
-               global $wgDBminWordLen, $wgContLang;
-               require_once( 'SearchEngine.php' );
+               global $wgContLang;
 
                $lc = SearchEngine::legalSearchChars() . '&#;';
                $t = $wgContLang->stripForSearch( $title );
@@ -356,7 +376,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 );
@@ -371,25 +391,26 @@ class Title {
         * @static (arguably)
         * @access public
         */
-       function getInterwikiLink( $key, $transludeonly = false ) {
-               global $wgMemc, $wgDBname, $wgInterwikiExpiry, $wgTitleInterwikiCache;
+       function getInterwikiLink( $key )  {
+               global $wgMemc, $wgDBname, $wgInterwikiExpiry;
+               global $wgInterwikiCache;
                $fname = 'Title::getInterwikiLink';
 
-               wfProfileIn( $fname );
-
                $key = strtolower( $key );
 
                $k = $wgDBname.':interwiki:'.$key;
-               if( array_key_exists( $k, $wgTitleInterwikiCache ) ) {
-                       wfProfileOut( $fname );
-                       return $wgTitleInterwikiCache[$k]->iw_url;
+               if( array_key_exists( $k, Title::$interwikiCache ) ) {
+                       return Title::$interwikiCache[$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;
                }
 
@@ -398,7 +419,6 @@ class Title {
                        array( 'iw_url', 'iw_local', 'iw_trans' ),
                        array( 'iw_prefix' => $key ), $fname );
                if( !$res ) {
-                       wfProfileOut( $fname );
                        return '';
                }
 
@@ -411,12 +431,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 $wgDBname, $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:{$wgDBname}", $db);
+                       if ($site=="")
+                               $site = $wgInterwikiFallbackSite;
+               }
+               $value = dba_fetch("{$wgDBname}:{$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[$wgDBname.':interwiki:'.$key] = $s;
+               return $s->iw_url;
+       }
        /**
         * Determine whether the object refers to a page within
         * this project.
@@ -426,13 +488,13 @@ class Title {
         * @access public
         */
        function isLocal() {
-               global $wgTitleInterwikiCache, $wgDBname;
+               global $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);
+                       return (bool)(Title::$interwikiCache[$k]->iw_local);
                } else {
                        return true;
                }
@@ -446,14 +508,14 @@ class Title {
         * @access public
         */
        function isTrans() {
-               global $wgTitleInterwikiCache, $wgDBname;
+               global $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);
+               return (bool)(Title::$interwikiCache[$k]->iw_trans);
        }
 
        /**
@@ -467,7 +529,6 @@ class Title {
         * @access public
         */
        function touchArray( $titles, $timestamp = '' ) {
-               global $wgUseFileCache;
 
                if ( count( $titles ) == 0 ) {
                        return;
@@ -476,8 +537,8 @@ class Title {
                if ( $timestamp == '' ) {
                        $timestamp = $dbw->timestamp();
                }
-               $page = $dbw->tableName( 'page' );
                /*
+               $page = $dbw->tableName( 'page' );
                $sql = "UPDATE $page SET page_touched='{$timestamp}' WHERE page_id IN (";
                $first = true;
 
@@ -545,7 +606,7 @@ class Title {
         * @return string
         * @access public
         */
-       function getNsText() { 
+       function getNsText() {
                global $wgContLang;
                return $wgContLang->getNsText( $this->mNamespace );
        }
@@ -559,6 +620,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
@@ -606,8 +684,7 @@ class Title {
         * @access public
         */
        function getPrefixedText() {
-               global $wgContLang;
-               if ( empty( $this->mPrefixedText ) ) {
+               if ( empty( $this->mPrefixedText ) ) { // FIXME: bad usage of empty() ?
                        $s = $this->prefix( $this->mTextform );
                        $s = str_replace( '_', ' ', $s );
                        $this->mPrefixedText = $s;
@@ -623,7 +700,6 @@ class Title {
         * @access public
         */
        function getFullText() {
-               global $wgContLang;
                $text = $this->getPrefixedText();
                if( '' != $this->mFragment ) {
                        $text .= '#' . $this->mFragment;
@@ -631,6 +707,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
@@ -659,93 +777,42 @@ class Title {
         * @access public
         */
        function getFullURL( $query = '' ) {
-               global $wgContLang, $wgServer, $wgScript, $wgMakeDumpLinks, $wgArticlePath;
+               global $wgContLang, $wgServer, $wgRequest;
 
                if ( '' == $this->mInterwiki ) {
-                       return $wgServer . $this->getLocalUrl( $query );
-               } elseif ( $wgMakeDumpLinks && $wgContLang->getLanguageName( $this->mInterwiki ) ) {
-                       $baseUrl = str_replace( '$1', "../../{$this->mInterwiki}/$1", $wgArticlePath );
-                       $baseUrl = str_replace( '$1', $this->getHashedDirectory() . '/$1', $baseUrl );
-               } else {
-                       $baseUrl = $this->getInterwikiLink( $this->mInterwiki );
-               }
+                       $url = $this->getLocalUrl( $query );
 
-               $namespace = $wgContLang->getNsText( $this->mNamespace );
-               if ( '' != $namespace ) {
-                       # Can this actually happen? Interwikis shouldn't be parsed.
-                       $namespace .= ':';
-               }
-               $url = str_replace( '$1', $namespace . $this->mUrlform, $baseUrl );
-               if( $query != '' ) {
-                       if( false === strpos( $url, '?' ) ) {
-                               $url .= '?';
-                       } else {
-                               $url .= '&';
+                       // Ugly quick hack to avoid duplicate prefixes (bug 4571 etc)
+                       // Correct fix would be to move the prepending elsewhere.
+                       if ($wgRequest->getVal('action') != 'render') {
+                               $url = $wgServer . $url;
                        }
-                       $url .= $query;
-               }
-               if ( '' != $this->mFragment ) {
-                       $url .= '#' . $this->mFragment;
-               }
-               return $url;
-       }
-
-       /**
-        * Get a relative directory for putting an HTML version of this article into
-        */
-       function getHashedDirectory() {
-               global $wgMakeDumpLinks, $wgInputEncoding;
-               $dbkey = $this->getDBkey();
-
-               # Split into characters
-               if ( $wgInputEncoding == 'UTF-8' ) {
-                       preg_match_all( '/./us', $dbkey, $m );
                } else {
-                       preg_match_all( '/./s', $dbkey, $m );
-               }
-               $chars = $m[0];
-               $length = count( $chars );
-               $dir = '';
+                       $baseUrl = $this->getInterwikiLink( $this->mInterwiki );
 
-               for ( $i = 0; $i < $wgMakeDumpLinks; $i++ ) {
-                       if ( $i ) {
-                               $dir .= '/';
+                       $namespace = $wgContLang->getNsText( $this->mNamespace );
+                       if ( '' != $namespace ) {
+                               # Can this actually happen? Interwikis shouldn't be parsed.
+                               $namespace .= ':';
                        }
-                       if ( $i >= $length ) {
-                               $dir .= '_';
-                       } elseif ( ord( $chars[$i] ) > 32 ) {
-                               $dir .= strtolower( $chars[$i] );
-                       } else {
-                               $dir .= sprintf( "%02X", ord( $chars[$i] ) );
+                       $url = str_replace( '$1', $namespace . $this->mUrlform, $baseUrl );
+                       if( $query != '' ) {
+                               if( false === strpos( $url, '?' ) ) {
+                                       $url .= '?';
+                               } else {
+                                       $url .= '&';
+                               }
+                               $url .= $query;
                        }
                }
-               return $dir;
-       }
 
-       function getHashedFilename() {
-               $dbkey = $this->getPrefixedDBkey();
-               $mainPage = Title::newMainPage();
-               if ( $mainPage->getPrefixedDBkey() == $dbkey ) {
-                       return 'index.html';
+               # Finally, add the fragment.
+               if ( '' != $this->mFragment ) {
+                       $url .= '#' . $this->mFragment;
                }
 
-               $dir = $this->getHashedDirectory();
-
-               # Replace illegal charcters for Windows paths with underscores
-               $friendlyName = strtr( $dbkey, '/\\*?"<>|~', '_________' );
-
-               # Work out lower case form. We assume we're on a system with case-insensitive
-               # filenames, so unless the case is of a special form, we have to disambiguate
-               $lowerCase = $this->prefix( ucfirst( strtolower( $this->getDBkey() ) ) );
-
-               # Make it mostly unique
-               if ( $lowerCase != $friendlyName  ) {
-                       $friendlyName .= '_' . substr(md5( $dbkey ), 0, 4);
-               }
-               # Handle colon specially by replacing it with tilde
-               # Thus we reduce the number of paths with hashes appended
-               $friendlyName = str_replace( ':', '~', $friendlyName );
-               return "$dir/$friendlyName.html";
+               wfRunHooks( 'GetFullURL', array( &$this, &$url, $query ) );
+               return $url;
        }
 
        /**
@@ -757,40 +824,51 @@ class Title {
         * @access public
         */
        function getLocalURL( $query = '' ) {
-               global $wgLang, $wgArticlePath, $wgScript, $wgMakeDumpLinks, $wgServer, $action;
+               global $wgArticlePath, $wgScript, $wgServer, $wgRequest;
 
                if ( $this->isExternal() ) {
-                       return $this->getFullURL();
-               }
-
-               $dbkey = wfUrlencode( $this->getPrefixedDBkey() );
-               if ( $wgMakeDumpLinks ) {
-                       $url = str_replace( '$1', wfUrlencode( $this->getHashedFilename() ), $wgArticlePath );
-               } elseif ( $query == '' ) {
-                       $url = str_replace( '$1', $dbkey, $wgArticlePath );
+                       $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 {
-                       global $wgActionPaths;
-                       if( !empty( $wgActionPaths ) &&
-                               preg_match( '/^(.*&|)action=([^&]*)(&(.*)|)$/', $query, $matches ) ) {
-                               $action = urldecode( $matches[2] );
-                               if( isset( $wgActionPaths[$action] ) ) {
-                                       $query = $matches[1];
-                                       if( isset( $matches[4] ) ) $query .= $matches[4];
-                                       $url = str_replace( '$1', $dbkey, $wgActionPaths[$action] );
-                                       if( $query != '' ) $url .= '?' . $query;
-                                       return $url;
+                       $dbkey = wfUrlencode( $this->getPrefixedDBkey() );
+                       if ( $query == '' ) {
+                               $url = str_replace( '$1', $dbkey, $wgArticlePath );
+                       } else {
+                               global $wgActionPaths;
+                               $url = false;
+                               if( !empty( $wgActionPaths ) &&
+                                       preg_match( '/^(.*&|)action=([^&]*)(&(.*)|)$/', $query, $matches ) )
+                               {
+                                       $action = urldecode( $matches[2] );
+                                       if( isset( $wgActionPaths[$action] ) ) {
+                                               $query = $matches[1];
+                                               if( isset( $matches[4] ) ) $query .= $matches[4];
+                                               $url = str_replace( '$1', $dbkey, $wgActionPaths[$action] );
+                                               if( $query != '' ) $url .= '?' . $query;
+                                       }
+                               }
+                               if ( $url === false ) {
+                                       if ( $query == '-' ) {
+                                               $query = '';
+                                       }
+                                       $url = "{$wgScript}?title={$dbkey}&{$query}";
                                }
                        }
-                       if ( $query == '-' ) {
-                               $query = '';
+                       
+                       // FIXME: this causes breakage in various places when we
+                       // actually expected a local URL and end up with dupe prefixes.
+                       if ($wgRequest->getVal('action') == 'render') {
+                               $url = $wgServer . $url;
                        }
-                       $url = "{$wgScript}?title={$dbkey}&{$query}";
                }
-
-               if ($action == 'render')
-                       return $wgServer . $url;
-               else
-                       return $url;
+               wfRunHooks( 'GetLocalURL', array( &$this, &$url, $query ) );
+               return $url;
        }
 
        /**
@@ -827,7 +905,9 @@ class Title {
         */
        function getInternalURL( $query = '' ) {
                global $wgInternalServer;
-               return $wgInternalServer . $this->getLocalURL( $query );
+               $url = $wgInternalServer . $this->getLocalURL( $query );
+               wfRunHooks( 'GetInternalURL', array( &$this, &$url, $query ) );
+               return $url;
        }
 
        /**
@@ -837,8 +917,6 @@ class Title {
         * @access public
         */
        function getEditURL() {
-               global $wgServer, $wgScript;
-
                if ( '' != $this->mInterwiki ) { return ''; }
                $s = $this->getLocalURL( 'action=edit' );
 
@@ -862,6 +940,23 @@ 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' ) {
+               $restrictions = $this->getRestrictions( $action );
+               # We do a full compare because this could be an array
+               foreach( $restrictions as $restriction ) {
+                       if( strtolower( $restriction ) != 'autoconfirmed' ) {
+                               return( false );
+                       }
+               }
+               return( true );
+       }
+
        /**
         * Does the title correspond to a protected article?
         * @param string $what the action the page is protected from,
@@ -869,16 +964,28 @@ class Title {
         * @return boolean
         * @access public
         */
-       function isProtected($action = '') {
+       function isProtected( $action = '' ) {
+               global $wgRestrictionLevels;
                if ( -1 == $this->mNamespace ) { return true; }
-               if($action == 'edit' || $action == '') {
-                       $a = $this->getRestrictions("edit");
-                       if ( in_array( 'sysop', $a ) ) { 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;
        }
 
@@ -901,39 +1008,42 @@ class Title {
        }
 
        /**
-        * Is $wgUser perform $action this page?
+        * 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::userCanEdit';
+               $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;
                }
+               // XXX: This is the code that prevents unprotecting a page in NS_MEDIAWIKI
+               // from taking effect -ævar
                if( NS_MEDIAWIKI == $this->mNamespace &&
                    !$wgUser->isAllowed('editinterface') ) {
                        wfProfileOut( $fname );
                        return false;
                }
+
                if( $this->mDbkeyform == '_' ) {
                        # FIXME: Is this necessary? Shouldn't be allowed anyway...
                        wfProfileOut( $fname );
                        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
@@ -962,6 +1072,14 @@ class Title {
                        return false;
                }
 
+               if( $action == 'create' ) {
+                       if( (  $this->isTalkPage() && !$wgUser->isAllowed( 'createtalk' ) ) ||
+                               ( !$this->isTalkPage() && !$wgUser->isAllowed( 'createpage' ) ) ) {
+                               wfProfileOut( $fname );
+                               return false;
+                       }
+               }
+
                wfProfileOut( $fname );
                return true;
        }
@@ -975,6 +1093,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
@@ -1004,6 +1131,12 @@ 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 {
@@ -1050,6 +1183,26 @@ class Title {
        function isCssJsSubpage() {
                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?
         * @return bool
@@ -1132,6 +1285,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;
        }
@@ -1145,14 +1302,14 @@ class Title {
         * @access public
         */
        function getArticleID( $flags = 0 ) {
-               global $wgLinkCache;
+               $linkCache =& LinkCache::singleton();
                if ( $flags & GAID_FOR_UPDATE ) {
-                       $oldUpdate = $wgLinkCache->forUpdate( true );
-                       $this->mArticleID = $wgLinkCache->addLinkObj( $this );
-                       $wgLinkCache->forUpdate( $oldUpdate );
+                       $oldUpdate = $linkCache->forUpdate( true );
+                       $this->mArticleID = $linkCache->addLinkObj( $this );
+                       $linkCache->forUpdate( $oldUpdate );
                } else {
                        if ( -1 == $this->mArticleID ) {
-                               $this->mArticleID = $wgLinkCache->addLinkObj( $this );
+                               $this->mArticleID = $linkCache->addLinkObj( $this );
                        }
                }
                return $this->mArticleID;
@@ -1171,7 +1328,7 @@ class Title {
 
        /**
         * This clears some fields in this object, and clears any associated
-        * keys in the "bad links" section of $wgLinkCache.
+        * keys in the "bad links" section of the link cache.
         *
         * - This is called from Article::insertNewArticle() to allow
         * loading of the new page_id. It's also called from
@@ -1181,8 +1338,8 @@ class Title {
         * @access public
         */
        function resetArticleID( $newid ) {
-               global $wgLinkCache;
-               $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
+               $linkCache =& LinkCache::singleton();
+               $linkCache->clearBadLink( $this->getPrefixedDBkey() );
 
                if ( 0 == $newid ) { $this->mArticleID = -1; }
                else { $this->mArticleID = $newid; }
@@ -1202,7 +1359,6 @@ class Title {
                        return;
                }
 
-               $now = wfTimestampNow();
                $dbw =& wfGetDB( DB_MASTER );
                $success = $dbw->update( 'page',
                        array( /* SET */
@@ -1227,7 +1383,7 @@ class Title {
         *
         * @param string $name the text
         * @return string the prefixed text
-        * @access private
+        * @private
         */
        /* private */ function prefix( $name ) {
                global $wgContLang;
@@ -1251,12 +1407,11 @@ 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;
@@ -1274,13 +1429,11 @@ class Title {
                $t = trim( $t, '_' );
 
                if ( '' == $t ) {
-                       wfProfileOut( $fname );
                        return false;
                }
 
                if( false !== strpos( $t, UTF8_REPLACEMENT ) ) {
                        # Contained illegal UTF-8 sequences or forbidden Unicode chars.
-                       wfProfileOut( $fname );
                        return false;
                }
 
@@ -1311,7 +1464,6 @@ class Title {
                                        if( !$firstPass ) {
                                                # Can't make a local interwiki link to an interwiki link.
                                                # That's just crazy!
-                                               wfProfileOut( $fname );
                                                return false;
                                        }
 
@@ -1323,7 +1475,6 @@ class Title {
                                        if ( 0 == strcasecmp( $this->mInterwiki, $wgLocalInterwiki ) ) {
                                                if( $t == '' ) {
                                                        # Can't have an empty self-link
-                                                       wfProfileOut( $fname );
                                                        return false;
                                                }
                                                $this->mInterwiki = '';
@@ -1331,6 +1482,13 @@ class Title {
                                                # Do another namespace split...
                                                continue;
                                        }
+
+                                       # If there's an initial colon after the interwiki, that also
+                                       # resets the default namespace
+                                       if ( $t !== '' && $t[0] == ':' ) {
+                                               $this->mNamespace = NS_MAIN;
+                                               $t = substr( $t, 1 );
+                                       }
                                }
                                # If there's no recognized interwiki or namespace,
                                # then let the colon expression be part of the title.
@@ -1356,7 +1514,6 @@ class Title {
                # Reject illegal characters.
                #
                if( preg_match( $rxTc, $r ) ) {
-                       wfProfileOut( $fname );
                        return false;
                }
 
@@ -1372,14 +1529,12 @@ class Title {
                       strpos( $r, '/./' ) !== false ||
                       strpos( $r, '/../' ) !== 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 );
                        return false;
                }
 
@@ -1405,17 +1560,20 @@ class Title {
                if( $t == '' &&
                        $this->mInterwiki == '' &&
                        $this->mNamespace != NS_MAIN ) {
-                       wfProfileOut( $fname );
                        return false;
                }
 
+               // Any remaining initial :s are illegal.
+               if ( $t !== '' && ':' == $t{0} ) {
+                       return false;
+               }
+               
                # Fill fields
                $this->mDbkeyform = $t;
                $this->mUrlform = wfUrlencode( $t );
 
                $this->mTextform = str_replace( '_', ' ', $t );
 
-               wfProfileOut( $fname );
                return true;
        }
 
@@ -1443,12 +1601,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 = '' ) {
-               global $wgLinkCache;
+       function getLinksTo( $options = '', $table = 'pagelinks', $prefix = 'pl' ) {
+               $linkCache =& LinkCache::singleton();
                $id = $this->getArticleID();
 
                if ( $options ) {
@@ -1457,12 +1618,12 @@ class Title {
                        $db =& wfGetDB( DB_SLAVE );
                }
 
-               $res = $db->select( array( 'page', 'pagelinks' ),
+               $res = $db->select( array( 'page', $table ),
                        array( 'page_namespace', 'page_title', 'page_id' ),
                        array(
-                               'pl_from=page_id',
-                               'pl_namespace' => $this->getNamespace(),
-                               'pl_title'     => $this->getDbKey() ),
+                               "{$prefix}_from=page_id",
+                               "{$prefix}_namespace" => $this->getNamespace(),
+                               "{$prefix}_title"     => $this->getDbKey() ),
                        'Title::getLinksTo',
                        $options );
 
@@ -1470,7 +1631,7 @@ class Title {
                if ( $db->numRows( $res ) ) {
                        while ( $row = $db->fetchObject( $res ) ) {
                                if ( $titleObj = Title::makeTitle( $row->page_namespace, $row->page_title ) ) {
-                                       $wgLinkCache->addGoodLinkObj( $row->page_id, $titleObj );
+                                       $linkCache->addGoodLinkObj( $row->page_id, $titleObj );
                                        $retVal[] = $titleObj;
                                }
                        }
@@ -1479,6 +1640,21 @@ class Title {
                return $retVal;
        }
 
+       /**
+        * 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
+        */
+       function getTemplateLinksTo( $options = '' ) {
+               return $this->getLinksTo( $options, 'templatelinks', 'tl' );
+       }
+
        /**
         * Get an array of Title objects referring to non-existent articles linked from this page
         *
@@ -1487,8 +1663,6 @@ class Title {
         * @access public
         */
        function getBrokenLinksFrom( $options = '' ) {
-               global $wgLinkCache;
-
                if ( $options ) {
                        $db =& wfGetDB( DB_MASTER );
                } else {
@@ -1503,7 +1677,7 @@ class Title {
                              AND pl_title=page_title
                            WHERE pl_from=?
                              AND page_namespace IS NULL
-                                 !",
+                                 !",
                        $db->tableName( 'pagelinks' ),
                        $db->tableName( 'page' ),
                        $this->getArticleId(),
@@ -1534,6 +1708,15 @@ class Title {
                );
        }
 
+       function purgeSquid() {
+               global $wgUseSquid;
+               if ( $wgUseSquid ) {
+                       $urls = $this->getSquidURLs();
+                       $u = new SquidUpdate( $urls );
+                       $u->doUpdate();
+               }
+       }
+
        /**
         * Move this page without authentication
         * @param Title &$nt the new page Title
@@ -1553,8 +1736,7 @@ class Title {
         * @return mixed true on success, message name on failure
         * @access public
         */
-       function isValidMoveOperation( &$nt, $auth = true, $reason = '' ) {
-               global $wgUser;
+       function isValidMoveOperation( &$nt, $auth = true ) {
                if( !$this or !$nt ) {
                        return 'badtitletext';
                }
@@ -1565,7 +1747,6 @@ class Title {
                        return 'immobile_namespace';
                }
 
-               $fname = 'Title::move';
                $oldid = $this->getArticleID();
                $newid = $nt->getArticleID();
 
@@ -1605,7 +1786,7 @@ class Title {
         * @access public
         */
        function moveTo( &$nt, $auth = true, $reason = '' ) {
-               $err = $this->isValidMoveOperation( $nt, $auth, $reason );
+               $err = $this->isValidMoveOperation( $nt, $auth );
                if( is_string( $err ) ) {
                        return $err;
                }
@@ -1615,7 +1796,7 @@ class Title {
                        $this->moveOverExistingRedirect( $nt, $reason );
                        $pageCountChange = 0;
                } else { # Target didn't exist, do normal move.
-                       $this->moveToNewTitle( $nt, $newid, $reason );
+                       $this->moveToNewTitle( $nt, $reason );
                        $pageCountChange = 1;
                }
                $redirid = $this->getArticleID();
@@ -1664,6 +1845,7 @@ class Title {
                        $u->doUpdate();
                }
 
+               global $wgUser;
                wfRunHooks( 'TitleMoveComplete', array( &$this, &$nt, &$wgUser, $pageid, $redirid ) );
                return true;
        }
@@ -1674,10 +1856,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, $wgLinkCache, $wgUseSquid, $wgMwRedir;
+               global $wgUseSquid;
                $fname = 'Title::moveOverExistingRedirect';
                $comment = wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
 
@@ -1690,7 +1872,7 @@ class Title {
                $newid = $nt->getArticleID();
                $oldid = $this->getArticleID();
                $dbw =& wfGetDB( DB_MASTER );
-               $links = $dbw->tableName( 'links' );
+               $linkCache =& LinkCache::singleton();
 
                # Delete the old redirect. We don't save it to history since
                # by definition if we've got here it's rather uninteresting.
@@ -1699,9 +1881,7 @@ class Title {
                $dbw->delete( 'page', array( 'page_id' => $newid ), $fname );
 
                # Save a null revision in the page's history notifying of the move
-               $nullRevision = Revision::newNullRevision( $dbw, $oldid,
-                       wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() ),
-                       true );
+               $nullRevision = Revision::newNullRevision( $dbw, $oldid, $comment, true );
                $nullRevId = $nullRevision->insertOn( $dbw );
 
                # Change the name of the target page:
@@ -1715,10 +1895,11 @@ class Title {
                        /* WHERE */ array( 'page_id' => $oldid ),
                        $fname
                );
-               $wgLinkCache->clearLink( $nt->getPrefixedDBkey() );
+               $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(
@@ -1727,7 +1908,7 @@ class Title {
                        'text'    => $redirectText ) );
                $revid = $redirectRevision->insertOn( $dbw );
                $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
-               $wgLinkCache->clearLink( $this->getPrefixedDBkey() );
+               $linkCache->clearLink( $this->getPrefixedDBkey() );
 
                # Log the move
                $log = new LogPage( 'move' );
@@ -1754,12 +1935,10 @@ class Title {
        /**
         * Move page to non-existing title.
         * @param Title &$nt the new Title
-        * @param int &$newid set to be the new article ID
-        * @access private
+        * @private
         */
-       function moveToNewTitle( &$nt, &$newid, $reason = '' ) {
-               global $wgUser, $wgLinkCache, $wgUseSquid;
-               global $wgMwRedir;
+       function moveToNewTitle( &$nt, $reason = '' ) {
+               global $wgUseSquid;
                $fname = 'MovePageForm::moveToNewTitle';
                $comment = wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
                if ( $reason ) {
@@ -1770,13 +1949,11 @@ class Title {
                $oldid = $this->getArticleID();
                $dbw =& wfGetDB( DB_MASTER );
                $now = $dbw->timestamp();
-               wfSeedRandom();
                $rand = wfRandom();
+               $linkCache =& LinkCache::singleton();
 
                # Save a null revision in the page's history notifying of the move
-               $nullRevision = Revision::newNullRevision( $dbw, $oldid,
-                       wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() ),
-                       true );
+               $nullRevision = Revision::newNullRevision( $dbw, $oldid, $comment, true );
                $nullRevId = $nullRevision->insertOn( $dbw );
 
                # Rename cur entry
@@ -1791,10 +1968,11 @@ class Title {
                        $fname
                );
 
-               $wgLinkCache->clearLink( $nt->getPrefixedDBkey() );
+               $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(
@@ -1803,7 +1981,7 @@ class Title {
                        'text'    => $redirectText ) );
                $revid = $redirectRevision->insertOn( $dbw );
                $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
-               $wgLinkCache->clearLink( $this->getPrefixedDBkey() );
+               $linkCache->clearLink( $this->getPrefixedDBkey() );
 
                # Log the move
                $log = new LogPage( 'move' );
@@ -1820,21 +1998,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();
        }
 
        /**
@@ -1858,19 +2024,24 @@ 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?
                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;
                }
 
@@ -1897,7 +2068,6 @@ class Title {
         * @access public
         */
        function createRedirect( $dest, $comment ) {
-               global $wgUser;
                if ( $this->getArticleID() ) {
                        return false;
                }
@@ -1937,11 +2107,9 @@ class Title {
         * @access public
         */
        function getParentCategories() {
-               global $wgContLang,$wgUser;
+               global $wgContLang;
 
                $titlekey = $this->getArticleId();
-               $sk =& $wgUser->getSkin();
-               $parents = array();
                $dbr =& wfGetDB( DB_SLAVE );
                $categorylinks = $dbr->tableName( 'categorylinks' );
 
@@ -1974,8 +2142,7 @@ class Title {
                $parents = $this->getParentCategories();
 
                if($parents != '') {
-                       foreach($parents as $parent => $current)
-                       {
+                       foreach($parents as $parent => $current) {
                                if ( array_key_exists( $parent, $children ) ) {
                                        # Circular reference
                                        $stack[$parent] = array();
@@ -1993,26 +2160,13 @@ class Title {
 
        /**
         * Get an associative array for selecting this title from
-        * the "cur" table
+        * the "page" table
         *
         * @return array
         * @access public
         */
-       function curCond() {
-               wfDebugDieBacktrace( 'curCond called' );
-               return array( 'cur_namespace' => $this->mNamespace, 'cur_title' => $this->mDbkeyform );
-       }
-
-       /**
-        * Get an associative array for selecting this title from the
-        * "old" table
-        *
-        * @return array
-        * @access public
-        */
-       function oldCond() {
-               wfDebugDieBacktrace( 'oldCond called' );
-               return array( 'old_namespace' => $this->mNamespace, 'old_title' => $this->mDbkeyform );
+       function pageCond() {
+               return array( 'page_namespace' => $this->mNamespace, 'page_title' => $this->mDbkeyform );
        }
 
        /**
@@ -2047,10 +2201,11 @@ class Title {
         * @param Title $title
         * @return bool
         */
-       function equals( &$title ) {
-               return $this->getInterwiki() == $title->getInterwiki()
+       function equals( $title ) {
+               // 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();
        }
 
        /**
@@ -2064,45 +2219,27 @@ class Title {
        /**
         * Should a link should be displayed as a known link, just based on its title?
         *
-        * Currently, a self-link with a fragment, special pages and image pages are in
-        * this category. Special pages never exist in the database. Some images do not
-        * have description pages in the database, but the description page contains
-        * useful history information that the user may want to link to.
+        * Currently, a self-link with a fragment and special pages are in
+        * this category. Special pages never exist in the database.
         */
        function isAlwaysKnown() {
                return  $this->isExternal() || ( 0 == $this->mNamespace && "" == $this->mDbkeyform )
-                 || NS_SPECIAL == $this->mNamespace || NS_IMAGE == $this->mNamespace;
+                 || NS_SPECIAL == $this->mNamespace;
        }
 
        /**
-        * 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 );
-               if ( 0 == $dbw->numRows( $res ) ) {
-                       return;
-               }
+               $u = new HTMLCacheUpdate( $this, 'pagelinks' );
+               $u->doUpdate();
 
-               $arr = array();
-               $toucharr = array();
-               while( $row = $dbw->fetchObject( $res ) ) {
-                       $toucharr[] = $row->pl_from;
+               if ( $this->getNamespace() == NS_CATEGORY ) {
+                       $u = new HTMLCacheUpdate( $this, 'categorylinks' );
+                       $u->doUpdate();
                }
-               if (!count($toucharr))
-                       return;
-               $dbw->update( 'page', /* SET */ array( 'page_touched' => $dbw->timestamp() ),
-                                                       /* WHERE */ array( 'page_id' => $toucharr ),$fname);
        }
 
        function trackbackURL() {
@@ -2128,5 +2265,44 @@ class Title {
    trackback:ping=\"$tburl\" />
 </rdf:RDF>";
        }
+
+       /**
+        * Generate strings used for xml 'id' names in monobook tabs
+        * @return string
+        */
+       function getNamespaceKey() {
+               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-' . strtolower( $this->getSubjectNsText() );
+               }
+       }
 }
 ?>