add tests for {{#special:}}
[lhc/web/wiklou.git] / includes / Title.php
index 6d06e37..b81b631 100644 (file)
@@ -299,7 +299,7 @@ class Title {
 
                                $rt = Title::newFromText( $m[1] );
                                # Disallow redirects to Special:Userlogout
-                               if ( !is_null($rt) && $rt->getNamespace() == NS_SPECIAL && preg_match( '/^Userlogout/i', $rt->getText() ) ) {
+                               if ( !is_null($rt) && $rt->isSpecial( 'Userlogout' ) ) {
                                        $rt = NULL;
                                }
                        }
@@ -356,7 +356,7 @@ class Title {
                $lc = SearchEngine::legalSearchChars() . '&#;';
                $t = $wgContLang->stripForSearch( $title );
                $t = preg_replace( "/[^{$lc}]+/", ' ', $t );
-               $t = strtolower( $t );
+               $t = $wgContLang->lc( $t );
 
                # Handle 's, s'
                $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
@@ -393,10 +393,10 @@ class Title {
         */
        function getInterwikiLink( $key )  {
                global $wgMemc, $wgInterwikiExpiry;
-               global $wgInterwikiCache;
+               global $wgInterwikiCache, $wgContLang;
                $fname = 'Title::getInterwikiLink';
 
-               $key = strtolower( $key );
+               $key = $wgContLang->lc( $key );
 
                $k = wfMemcKey( 'interwiki', $key );
                if( array_key_exists( $k, Title::$interwikiCache ) ) {
@@ -850,8 +850,9 @@ class Title {
                                        else 
                                                $variantArticlePath = $wgVariantArticlePath;
                                        
-                                       $url = str_replace( '$1', $dbkey, $variantArticlePath );
-                                       $url = str_replace( '$2', urlencode( $variant ), $url );                                        
+                                       $url = str_replace( '$2', urlencode( $variant ), $variantArticlePath );
+                                       $url = str_replace( '$1', $dbkey, $url  );
+                                       
                                }
                                else 
                                        $url = str_replace( '$1', $dbkey, $wgArticlePath );
@@ -1159,10 +1160,11 @@ class Title {
                } else {
                        global $wgWhitelistRead;
 
-                       /** If anon users can create an account,
-                           they need to reach the login page first! */
-                       if( $wgUser->isAllowed( 'createaccount' )
-                           && $this->getNamespace() == NS_SPECIAL
+                       /** 
+                        * Always grant access to the login page.
+                        * Even anons need to be able to log in.
+                       */
+                       if( $this->getNamespace() == NS_SPECIAL
                            && $this->getText() == 'Userlogin' ) {
                                return true;
                        }
@@ -1468,7 +1470,7 @@ class Title {
                do {
                        if ( preg_match( "/^(.+?)_*:_*(.*)$/S", $t, $m ) ) {
                                $p = $m[1];
-                               $lowerNs = strtolower( $p );
+                               $lowerNs = $wgContLang->lc( $p );
                                if ( $ns = Namespace::getCanonicalIndex( $lowerNs ) ) {
                                        # Canonical namespace
                                        $t = $m[2];
@@ -1486,7 +1488,7 @@ class Title {
 
                                        # Interwiki link
                                        $t = $m[2];
-                                       $this->mInterwiki = strtolower( $p );
+                                       $this->mInterwiki = $wgContLang->lc( $p );
 
                                        # Redundant interwiki prefix to the local wiki
                                        if ( 0 == strcasecmp( $this->mInterwiki, $wgLocalInterwiki ) ) {
@@ -2292,26 +2294,12 @@ class Title {
         * Get a cached value from a global cache that is invalidated when this page changes
         * @param string $key the key
         * @param callback $callback A callback function which generates the value on cache miss
+        *
+        * @deprecated use DependencyWrapper
         */
        function getRelatedCache( $memc, $key, $expiry, $callback, $params = array() ) {
-               $touched = $this->getTouched();
-               $cacheEntry = $memc->get( $key );
-               if ( $cacheEntry ) {
-                       if ( $cacheEntry['touched'] >= $touched ) {
-                               return $cacheEntry['value'];
-                       } else {
-                               wfDebug( __METHOD__.": $key expired\n" );
-                       }
-               } else {
-                       wfDebug( __METHOD__.": $key not found\n" );
-               }
-               $value = call_user_func_array( $callback, $params );
-               $cacheEntry = array(
-                       'value' => $value,
-                       'touched' => $touched
-               );
-               $memc->set( $key, $cacheEntry, $expiry );
-               return $value;
+               return DependencyWrapper::getValueFromCache( $memc, $key, $expiry, $callback, 
+                       $params, new TitleDependency( $this ) );
        }
 
        function trackbackURL() {
@@ -2343,6 +2331,7 @@ class Title {
         * @return string
         */
        function getNamespaceKey() {
+               global $wgContLang;
                switch ($this->getNamespace()) {
                        case NS_MAIN:
                        case NS_TALK:
@@ -2373,8 +2362,40 @@ class Title {
                        case NS_CATEGORY_TALK:
                                return 'nstab-category';
                        default:
-                               return 'nstab-' . strtolower( $this->getSubjectNsText() );
+                               return 'nstab-' . $wgContLang->lc( $this->getSubjectNsText() );
+               }
+       }
+
+       /**
+        * Returns true if this title resolves to the named special page
+        * @param string $name The special page name
+        * @access public
+        */
+       function isSpecial( $name ) {
+               if ( $this->getNamespace() == NS_SPECIAL ) {
+                       list( $thisName, $subpage ) = SpecialPage::resolveAliasWithSubpage( $this->getDBkey() );
+                       if ( $name == $thisName ) {
+                               return true;
+                       }
+               }
+               return false;
+       }
+
+       /**
+        * If the Title refers to a special page alias which is not the local default, 
+        * returns a new Title which points to the local default. Otherwise, returns $this.
+        */
+       function fixSpecialName() {
+               if ( $this->getNamespace() == NS_SPECIAL ) {
+                       $canonicalName = SpecialPage::resolveAlias( $this->mDbkeyform );
+                       if ( $canonicalName ) {
+                               $localName = SpecialPage::getLocalNameFor( $canonicalName );
+                               if ( $localName != $this->mDbkeyform ) {
+                                       return Title::makeTitle( NS_SPECIAL, $localName );
+                               }
+                       }
                }
+               return $this;
        }
 }
 ?>