Prevent registration/login with the username "MediaWiki default"
[lhc/web/wiklou.git] / includes / Title.php
index 0d5d1c7..9001c35 100644 (file)
@@ -46,12 +46,12 @@ 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()
        /**#@-*/
 
 
@@ -107,7 +107,6 @@ class Title {
        function newFromText( $text, $defaultNamespace = NS_MAIN ) {
                global $wgTitleCache;
                $fname = 'Title::newFromText';
-               wfProfileIn( $fname );
 
                if( is_object( $text ) ) {
                        wfDebugDieBacktrace( 'Title::newFromText given an object' );
@@ -122,7 +121,6 @@ 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];
                }
 
@@ -135,18 +133,19 @@ class Title {
                $t->mDbkeyform = str_replace( ' ', '_', $filteredText );
                $t->mDefaultNamespace = $defaultNamespace;
 
+               static $cachedcount = 0 ;
                if( $t->secureAndSplit() ) {
                        if( $defaultNamespace == NS_MAIN ) {
-                               if( count( $wgTitleCache ) >= MW_TITLECACHE_MAX ) {
+                               if( $cachedcount >= MW_TITLECACHE_MAX ) {
                                        # Avoid memory leaks on mass operations...
                                        $wgTitleCache = array();
+                                       $cachedcount=0;
                                }
+                               $cachedcount++;
                                $wgTitleCache[$text] =& $t;
                        }
-                       wfProfileOut( $fname );
                        return $t;
                } else {
-                       wfProfileOut( $fname );
                        $ret = NULL;
                        return $ret;
                }
@@ -161,14 +160,17 @@ class Title {
         * @access public
         */
        function newFromURL( $url ) {
+               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 {
@@ -374,23 +376,24 @@ class Title {
         */
        function getInterwikiLink( $key )  {
                global $wgMemc, $wgDBname, $wgInterwikiExpiry, $wgTitleInterwikiCache;
+               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 ($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 );
                        return $s->iw_url;
                }
 
@@ -399,7 +402,6 @@ class Title {
                        array( 'iw_url', 'iw_local', 'iw_trans' ),
                        array( 'iw_prefix' => $key ), $fname );
                if( !$res ) {
-                       wfProfileOut( $fname );
                        return '';
                }
 
@@ -414,10 +416,53 @@ class Title {
                $wgMemc->set( $k, $s, $wgInterwikiExpiry );
                $wgTitleInterwikiCache[$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;
+               global $wgTitleInterwikiCache;
+               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;
+               }
+               $wgTitleInterwikiCache[$wgDBname.':interwiki:'.$key] = $s;
+               return $s->iw_url;
+       }
        /**
         * Determine whether the object refers to a page within
         * this project.
@@ -449,7 +494,7 @@ class Title {
        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 );
@@ -545,7 +590,7 @@ class Title {
         * @return string
         * @access public
         */
-       function getNsText() { 
+       function getNsText() {
                global $wgContLang;
                return $wgContLang->getNsText( $this->mNamespace );
        }
@@ -606,7 +651,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 );
@@ -623,7 +667,6 @@ class Title {
         * @access public
         */
        function getFullText() {
-               global $wgContLang;
                $text = $this->getPrefixedText();
                if( '' != $this->mFragment ) {
                        $text .= '#' . $this->mFragment;
@@ -631,6 +674,31 @@ class Title {
                return $text;
        }
 
+       /**
+        * Get the lowest-level subpage name, i.e. the rightmost part after /
+        * @return string Subpage name
+        */
+       function getSubpageText() {
+               global $wgNamespacesWithSubpages;
+               if( $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,10 +727,16 @@ class Title {
         * @access public
         */
        function getFullURL( $query = '' ) {
-               global $wgContLang, $wgServer;
+               global $wgContLang, $wgServer, $wgRequest;
 
                if ( '' == $this->mInterwiki ) {
-                       $url = $wgServer . $this->getLocalUrl( $query );
+                       $url = $this->getLocalUrl( $query );
+
+                       // 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;
+                       }
                } else {
                        $baseUrl = $this->getInterwikiLink( $this->mInterwiki );
 
@@ -701,6 +775,13 @@ class Title {
 
                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 == '' ) {
@@ -709,7 +790,7 @@ class Title {
                                global $wgActionPaths;
                                $url = false;
                                if( !empty( $wgActionPaths ) &&
-                                       preg_match( '/^(.*&|)action=([^&]*)(&(.*)|)$/', $query, $matches ) ) 
+                                       preg_match( '/^(.*&|)action=([^&]*)(&(.*)|)$/', $query, $matches ) )
                                {
                                        $action = urldecode( $matches[2] );
                                        if( isset( $wgActionPaths[$action] ) ) {
@@ -726,8 +807,10 @@ class Title {
                                        $url = "{$wgScript}?title={$dbkey}&{$query}";
                                }
                        }
-
-                       if ($wgRequest->getText('action') == 'render') {
+                       
+                       // 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;
                        }
                }
@@ -769,7 +852,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;
        }
 
        /**
@@ -779,8 +864,6 @@ class Title {
         * @access public
         */
        function getEditURL() {
-               global $wgServer, $wgScript;
-
                if ( '' != $this->mInterwiki ) { return ''; }
                $s = $this->getLocalURL( 'action=edit' );
 
@@ -804,6 +887,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,
@@ -811,16 +911,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;
        }
 
@@ -853,6 +965,13 @@ class Title {
                wfProfileIn( $fname );
 
                global $wgUser;
+
+               $result = true;
+               if ( !wfRunHooks( 'userCan', array( &$this, &$wgUser, $action, &$result ) ) ) {
+                       wfProfileOut( $fname );
+                       return $result;
+               }
+
                if( NS_SPECIAL == $this->mNamespace ) {
                        wfProfileOut( $fname );
                        return false;
@@ -864,7 +983,7 @@ class Title {
                        wfProfileOut( $fname );
                        return false;
                }
-               
+
                if( $this->mDbkeyform == '_' ) {
                        # FIXME: Is this necessary? Shouldn't be allowed anyway...
                        wfProfileOut( $fname );
@@ -873,7 +992,7 @@ class Title {
 
                # protect global styles and js
                if ( NS_MEDIAWIKI == $this->mNamespace
-                && preg_match("/\\.(css|js)$/", $this->mTextform )
+                && preg_match("/\\.(css|js)$/", $this->mTextform )
                     && !$wgUser->isAllowed('editinterface') ) {
                        wfProfileOut( $fname );
                        return false;
@@ -906,7 +1025,7 @@ class Title {
                        wfProfileOut( $fname );
                        return false;
                }
-               
+
                if( $action == 'create' ) {
                        if( (  $this->isTalkPage() && !$wgUser->isAllowed( 'createtalk' ) ) ||
                                ( !$this->isTalkPage() && !$wgUser->isAllowed( 'createpage' ) ) ) {
@@ -956,6 +1075,11 @@ class Title {
        function userCanRead() {
                global $wgUser;
 
+               $result = true;
+                       if ( !wfRunHooks( 'userCan', array( &$this, &$wgUser, "read", &$result ) ) ) {
+                       return $result;
+               }
+
                if( $wgUser->isAllowed('read') ) {
                        return true;
                } else {
@@ -1002,6 +1126,22 @@ 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() {
+               global $wgValidSkinNames;
+               return( $this->isCssJsSubpage() && array_key_exists( $this->getSkinFromCssJsSubpage(), $wgValidSkinNames ) );
+       }
+       /**
+        * 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
@@ -1097,14 +1237,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;
@@ -1123,7 +1263,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
@@ -1133,8 +1273,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; }
@@ -1207,7 +1347,6 @@ class Title {
        /* private */ function secureAndSplit() {
                global $wgContLang, $wgLocalInterwiki, $wgCapitalLinks;
                $fname = 'Title::secureAndSplit';
-               wfProfileIn( $fname );
 
                # Initialisation
                static $rxTc = false;
@@ -1225,13 +1364,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;
                }
 
@@ -1262,7 +1399,6 @@ class Title {
                                        if( !$firstPass ) {
                                                # Can't make a local interwiki link to an interwiki link.
                                                # That's just crazy!
-                                               wfProfileOut( $fname );
                                                return false;
                                        }
 
@@ -1274,7 +1410,6 @@ class Title {
                                        if ( 0 == strcasecmp( $this->mInterwiki, $wgLocalInterwiki ) ) {
                                                if( $t == '' ) {
                                                        # Can't have an empty self-link
-                                                       wfProfileOut( $fname );
                                                        return false;
                                                }
                                                $this->mInterwiki = '';
@@ -1282,6 +1417,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.
@@ -1307,7 +1449,6 @@ class Title {
                # Reject illegal characters.
                #
                if( preg_match( $rxTc, $r ) ) {
-                       wfProfileOut( $fname );
                        return false;
                }
 
@@ -1323,14 +1464,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;
                }
 
@@ -1356,7 +1495,6 @@ class Title {
                if( $t == '' &&
                        $this->mInterwiki == '' &&
                        $this->mNamespace != NS_MAIN ) {
-                       wfProfileOut( $fname );
                        return false;
                }
 
@@ -1366,7 +1504,6 @@ class Title {
 
                $this->mTextform = str_replace( '_', ' ', $t );
 
-               wfProfileOut( $fname );
                return true;
        }
 
@@ -1398,8 +1535,8 @@ class Title {
         * @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 ) {
@@ -1408,12 +1545,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 );
 
@@ -1421,7 +1558,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;
                                }
                        }
@@ -1430,6 +1567,18 @@ class Title {
                return $retVal;
        }
 
+       /**
+        * Get an array of Title objects using this Title as a template
+        * Also stores the IDs in the link cache.
+        *
+        * @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
         *
@@ -1438,8 +1587,6 @@ class Title {
         * @access public
         */
        function getBrokenLinksFrom( $options = '' ) {
-               global $wgLinkCache;
-
                if ( $options ) {
                        $db =& wfGetDB( DB_MASTER );
                } else {
@@ -1454,7 +1601,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,7 +1652,6 @@ class Title {
         * @access public
         */
        function isValidMoveOperation( &$nt, $auth = true ) {
-               global $wgUser;
                if( !$this or !$nt ) {
                        return 'badtitletext';
                }
@@ -1565,7 +1711,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();
@@ -1628,7 +1774,7 @@ class Title {
         * @access private
         */
        function moveOverExistingRedirect( &$nt, $reason = '' ) {
-               global $wgUser, $wgLinkCache, $wgUseSquid, $wgMwRedir;
+               global $wgUseSquid, $wgMwRedir;
                $fname = 'Title::moveOverExistingRedirect';
                $comment = wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
 
@@ -1641,6 +1787,7 @@ class Title {
                $newid = $nt->getArticleID();
                $oldid = $this->getArticleID();
                $dbw =& wfGetDB( DB_MASTER );
+               $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.
@@ -1649,9 +1796,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:
@@ -1665,7 +1810,7 @@ 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";
@@ -1677,7 +1822,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' );
@@ -1704,11 +1849,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
         */
-       function moveToNewTitle( &$nt, &$newid, $reason = '' ) {
-               global $wgUser, $wgLinkCache, $wgUseSquid;
+       function moveToNewTitle( &$nt, $reason = '' ) {
+               global $wgUseSquid;
                global $wgMwRedir;
                $fname = 'MovePageForm::moveToNewTitle';
                $comment = wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
@@ -1720,13 +1864,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
@@ -1741,7 +1883,7 @@ class Title {
                        $fname
                );
 
-               $wgLinkCache->clearLink( $nt->getPrefixedDBkey() );
+               $linkCache->clearLink( $nt->getPrefixedDBkey() );
 
                # Insert redirect
                $redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
@@ -1753,7 +1895,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' );
@@ -1847,7 +1989,6 @@ class Title {
         * @access public
         */
        function createRedirect( $dest, $comment ) {
-               global $wgUser;
                if ( $this->getArticleID() ) {
                        return false;
                }
@@ -1887,7 +2028,7 @@ class Title {
         * @access public
         */
        function getParentCategories() {
-               global $wgContLang,$wgUser;
+               global $wgContLang;
 
                $titlekey = $this->getArticleId();
                $dbr =& wfGetDB( DB_SLAVE );
@@ -1940,26 +2081,13 @@ class Title {
 
        /**
         * Get an associative array for selecting this title from
-        * the "cur" 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
+        * the "page" 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 );
        }
 
        /**
@@ -1995,9 +2123,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();
        }
 
        /**
@@ -2035,14 +2164,25 @@ class Title {
                                'pl_namespace' => $this->getNamespace(),
                                'pl_title'     => $this->getDbKey() ),
                        $fname );
-               if ( 0 == $dbw->numRows( $res ) ) {
-                       return;
-               }
 
                $toucharr = array();
                while( $row = $dbw->fetchObject( $res ) ) {
                        $toucharr[] = $row->pl_from;
                }
+               $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 );
+               }
+
                if (!count($toucharr))
                        return;
                $dbw->update( 'page', /* SET */ array( 'page_touched' => $dbw->timestamp() ),
@@ -2072,5 +2212,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-wp';
+                       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() );
+               }
+       }
 }
 ?>