accidentially removed the test for
[lhc/web/wiklou.git] / includes / Title.php
index 2e36078..255cc15 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 # See title.doc
 
-/* private static */ $title_interwiki_cache = array();
+$wgTitleInterwikiCache = array();
 
 # Title class
 # 
@@ -55,24 +55,14 @@ class Title {
        # From text, such as what you would find in a link
        /* static */ function newFromText( $text, $defaultNamespace = 0 )
        {       
-               static $trans;
                $fname = "Title::newFromText";
                wfProfileIn( $fname );
 
-               # Note - mixing latin1 named entities and unicode numbered
-               # ones will result in a bad link.
-               if( !isset( $trans ) ) {
-                       global $wgInputEncoding;
-                       $trans = array_flip( get_html_translation_table( HTML_ENTITIES ) );
-                       if( strcasecmp( "utf-8", $wgInputEncoding ) == 0 ) {
-                               $trans = array_map( "utf8_encode", $trans );
-                       }
-               }
-
                if( is_object( $text ) ) {
                        wfDebugDieBacktrace( "Called with object instead of string." );
                }
-               $text = strtr( $text, $trans );
+               global $wgInputEncoding;
+               $text = do_html_entity_decode( $text, ENT_COMPAT, $wgInputEncoding );
 
                $text = wfMungeToUtf8( $text );
                
@@ -238,21 +228,22 @@ class Title {
        # The URL contains $1, which is replaced by the title
        function getInterwikiLink( $key )
        {       
-               global $wgMemc, $wgDBname;
-               static $title_interwiki_cache = array();
+               global $wgMemc, $wgDBname, $wgInterwikiExpiry;
+               static $wgTitleInterwikiCache = array();
 
                $k = "$wgDBname:interwiki:$key";
 
-               if( array_key_exists( $k, $title_interwiki_cache ) )
-                       return $title_interwiki_cache[$k]->iw_url;
+               if( array_key_exists( $k, $wgTitleInterwikiCache ) )
+                       return $wgTitleInterwikiCache[$k]->iw_url;
 
                $s = $wgMemc->get( $k ); 
-               if( $s ) { 
-                       $title_interwiki_cache[$k] = $s;
+               # Ignore old keys with no iw_local
+               if( $s && isset( $s->iw_local ) ) { 
+                       $wgTitleInterwikiCache[$k] = $s;
                        return $s->iw_url;
                }
                $dkey = wfStrencode( $key );
-               $query = "SELECT iw_url FROM interwiki WHERE iw_prefix='$dkey'";
+               $query = "SELECT iw_url,iw_local FROM interwiki WHERE iw_prefix='$dkey'";
                $res = wfQuery( $query, DB_READ, "Title::getInterwikiLink" );
                if(!$res) return "";
                
@@ -261,11 +252,24 @@ class Title {
                        $s = (object)false;
                        $s->iw_url = "";
                }
-               $wgMemc->set( $k, $s );
-               $title_interwiki_cache[$k] = $s;
+               $wgMemc->set( $k, $s, $wgInterwikiExpiry );
+               $wgTitleInterwikiCache[$k] = $s;
                return $s->iw_url;
        }
-       
+
+       function isLocal() {
+               global $wgTitleInterwikiCache, $wgDBname;
+
+               if ( $this->mInterwiki != "" ) {
+                       # Make sure key is loaded into cache
+                       $this->getInterwikiLink( $this->mInterwiki );
+                       $k = "$wgDBname:interwiki:" . $this->mInterwiki;
+                       return (bool)($wgTitleInterwikiCache[$k]->iw_local);
+               } else {
+                       return true;
+               }
+       }
+
        # Update the cur_touched field for an array of title objects
        # Inefficient unless the IDs are already loaded into the link cache
        /* static */ function touchArray( $titles, $timestamp = "" ) {
@@ -482,19 +486,20 @@ class Title {
        # Can $wgUser edit this page?
        function userCanEdit()
        {
-               global $wgUser;
 
                if ( -1 == $this->mNamespace ) { return false; }
                # if ( 0 == $this->getArticleID() ) { return false; }
                if ( $this->mDbkeyform == "_" ) { return false; }
+               //if ( $this->isCssJsSubpage() and !$this->userCanEditCssJsSubpage() ) { 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
+               global $wgUser;
                if( Namespace::getUser() == $this->mNamespace
                        and preg_match("/\\.(css|js)$/", $this->mTextform )
-                       and !$wgUser->isDeveloper()
-                       and !preg_match("/^".$wgUser->getName()."/", $this->mTextform) )
+                       and !$wgUser->isSysop()
+                       and !preg_match("/^".preg_quote($wgUser->getName(), '/')."/", $this->mTextform) )
                { return false; }
-
                $ur = $wgUser->getRights();
                foreach ( $this->getRestrictions() as $r ) {
                        if ( "" != $r && ( ! in_array( $r, $ur ) ) ) {
@@ -503,6 +508,39 @@ class Title {
                }
                return true;
        }
+       
+       function userCanRead() {
+               global $wgUser;
+               global $wgWhitelistRead;
+               
+               if( 0 != $wgUser->getID() ) return true;
+               if( !is_array( $wgWhitelistRead ) ) return true;
+               
+               $name = $this->getPrefixedText();
+               if( in_array( $name, $wgWhitelistRead ) ) return true;
+               
+               # Compatibility with old settings
+               if( $this->getNamespace() == NS_ARTICLE ) {
+                       if( in_array( ":" . $name, $wgWhitelistRead ) ) return true;
+               }
+               return false;
+       }
+       
+       function isCssJsSubpage() {
+               return ( Namespace::getUser() == $this->mNamespace and preg_match("/\\.(css|js)$/", $this->mTextform ) );
+       }
+       function isCssSubpage() {
+               return ( Namespace::getUser() == $this->mNamespace and preg_match("/\\.css$/", $this->mTextform ) );
+       }
+       function isJsSubpage() {
+               return ( Namespace::getUser() == $this->mNamespace and preg_match("/\\.js$/", $this->mTextform ) );
+       }
+       function userCanEditCssJsSubpage() {
+               # protect css/js subpages of user pages
+               # XXX: this might be better using restrictions
+               global $wgUser;
+               return ( $wgUser->isSysop() or preg_match("/^".preg_quote($wgUser->getName())."/", $this->mTextform) );
+       }
 
        # Accessor/initialisation for mRestrictions
        function getRestrictions()
@@ -1101,5 +1139,6 @@ class Title {
                Article::onArticleCreate( $this );
                return true;
        }
+       
 }
 ?>