* (bug 6243) Fix email for usernames containing dots when using PEAR::Mail
[lhc/web/wiklou.git] / includes / Title.php
index 4cfce2b..75acdec 100644 (file)
@@ -8,9 +8,6 @@
 /** */
 require_once( 'normal/UtfNormal.php' );
 
-$wgTitleInterwikiCache = array();
-$wgTitleCache = array();
-
 define ( 'GAID_FOR_UPDATE', 1 );
 
 # Title::newFromTitle maintains a cache to avoid
@@ -28,6 +25,13 @@ 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
@@ -46,12 +50,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()
        /**#@-*/
 
 
@@ -105,11 +109,10 @@ class Title {
         * @access public
         */
        function newFromText( $text, $defaultNamespace = NS_MAIN ) {
-               global $wgTitleCache;
                $fname = 'Title::newFromText';
 
                if( is_object( $text ) ) {
-                       wfDebugDieBacktrace( 'Title::newFromText given an object' );
+                       throw new MWException( 'Title::newFromText given an object' );
                }
 
                /**
@@ -120,8 +123,8 @@ class Title {
                 *
                 * In theory these are value objects and won't get changed...
                 */
-               if( $defaultNamespace == NS_MAIN && isset( $wgTitleCache[$text] ) ) {
-                       return $wgTitleCache[$text];
+               if( $defaultNamespace == NS_MAIN && isset( Title::$titleCache[$text] ) ) {
+                       return Title::$titleCache[$text];
                }
 
                /**
@@ -138,11 +141,11 @@ class Title {
                        if( $defaultNamespace == NS_MAIN ) {
                                if( $cachedcount >= MW_TITLECACHE_MAX ) {
                                        # Avoid memory leaks on mass operations...
-                                       $wgTitleCache = array();
+                                       Title::$titleCache = array();
                                        $cachedcount=0;
                                }
                                $cachedcount++;
-                               $wgTitleCache[$text] =& $t;
+                               Title::$titleCache[$text] =& $t;
                        }
                        return $t;
                } else {
@@ -334,7 +337,6 @@ class Title {
         */
        /* static */ function indexTitle( $ns, $title ) {
                global $wgContLang;
-               require_once( 'SearchEngine.php' );
 
                $lc = SearchEngine::legalSearchChars() . '&#;';
                $t = $wgContLang->stripForSearch( $title );
@@ -375,15 +377,15 @@ class Title {
         * @access public
         */
        function getInterwikiLink( $key )  {
-               global $wgMemc, $wgDBname, $wgInterwikiExpiry, $wgTitleInterwikiCache;
+               global $wgMemc, $wgDBname, $wgInterwikiExpiry;
                global $wgInterwikiCache;
                $fname = 'Title::getInterwikiLink';
 
                $key = strtolower( $key );
 
                $k = $wgDBname.':interwiki:'.$key;
-               if( array_key_exists( $k, $wgTitleInterwikiCache ) ) {
-                       return $wgTitleInterwikiCache[$k]->iw_url;
+               if( array_key_exists( $k, Title::$interwikiCache ) ) {
+                       return Title::$interwikiCache[$k]->iw_url;
                }
 
                if ($wgInterwikiCache) {
@@ -393,7 +395,7 @@ class Title {
                $s = $wgMemc->get( $k );
                # Ignore old keys with no iw_local
                if( $s && isset( $s->iw_local ) && isset($s->iw_trans)) {
-                       $wgTitleInterwikiCache[$k] = $s;
+                       Title::$interwikiCache[$k] = $s;
                        return $s->iw_url;
                }
 
@@ -414,7 +416,7 @@ class Title {
                        $s->iw_trans = 0;
                }
                $wgMemc->set( $k, $s, $wgInterwikiExpiry );
-               $wgTitleInterwikiCache[$k] = $s;
+               Title::$interwikiCache[$k] = $s;
 
                return $s->iw_url;
        }
@@ -429,7 +431,6 @@ class Title {
         */
        function getInterwikiCached( $key ) {
                global $wgDBname, $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
-               global $wgTitleInterwikiCache;
                static $db, $site;
 
                if (!$db)
@@ -441,11 +442,11 @@ class Title {
                                $site = $wgInterwikiFallbackSite;
                }
                $value = dba_fetch("{$wgDBname}:{$key}", $db);
-               if ($value=='' and $wgInterwikiScopes>=3) { 
+               if ($value=='' and $wgInterwikiScopes>=3) {
                        /* try site-level */
                        $value = dba_fetch("_{$site}:{$key}", $db);
                }
-               if ($value=='' and $wgInterwikiScopes>=2) { 
+               if ($value=='' and $wgInterwikiScopes>=2) {
                        /* try globals */
                        $value = dba_fetch("__global:{$key}", $db);
                }
@@ -460,7 +461,7 @@ class Title {
                        $s->iw_url=$url;
                        $s->iw_local=(int)$local;
                }
-               $wgTitleInterwikiCache[$wgDBname.':interwiki:'.$key] = $s;
+               Title::$interwikiCache[$wgDBname.':interwiki:'.$key] = $s;
                return $s->iw_url;
        }
        /**
@@ -472,13 +473,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;
                }
@@ -492,14 +493,14 @@ class Title {
         * @access public
         */
        function isTrans() {
-               global $wgTitleInterwikiCache, $wgDBname;
+               global $wgDBname;
 
                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);
        }
 
        /**
@@ -604,6 +605,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
@@ -651,7 +669,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 );
@@ -668,7 +685,6 @@ class Title {
         * @access public
         */
        function getFullText() {
-               global $wgContLang;
                $text = $this->getPrefixedText();
                if( '' != $this->mFragment ) {
                        $text .= '#' . $this->mFragment;
@@ -676,6 +692,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
@@ -731,10 +789,13 @@ class Title {
                                }
                                $url .= $query;
                        }
-                       if ( '' != $this->mFragment ) {
-                               $url .= '#' . $this->mFragment;
-                       }
                }
+
+               # Finally, add the fragment.
+               if ( '' != $this->mFragment ) {
+                       $url .= '#' . $this->mFragment;
+               }
+
                wfRunHooks( 'GetFullURL', array( &$this, &$url, $query ) );
                return $url;
        }
@@ -753,8 +814,8 @@ 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 
+                               // 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";
@@ -841,8 +902,6 @@ class Title {
         * @access public
         */
        function getEditURL() {
-               global $wgServer, $wgScript;
-
                if ( '' != $this->mInterwiki ) { return ''; }
                $s = $this->getLocalURL( 'action=edit' );
 
@@ -866,6 +925,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,
@@ -928,8 +1004,9 @@ class Title {
 
                global $wgUser;
 
-               $result = true;
-               if ( !wfRunHooks( 'userCan', array( &$this, &$wgUser, $action, &$result ) ) ) {
+               $result = null;
+               wfRunHooks( 'userCan', array( &$this, &$wgUser, $action, &$result ) );
+               if ( $result !== null ) {
                        wfProfileOut( $fname );
                        return $result;
                }
@@ -952,14 +1029,6 @@ class Title {
                        return false;
                }
 
-               # protect global styles and js
-               if ( NS_MEDIAWIKI == $this->mNamespace
-                && preg_match("/\\.(css|js)$/", $this->mTextform )
-                    && !$wgUser->isAllowed('editinterface') ) {
-                       wfProfileOut( $fname );
-                       return false;
-               }
-
                # protect css/js subpages of user pages
                # XXX: this might be better using restrictions
                # XXX: Find a way to work around the php bug that prevents using $this->userCanEditCssJsSubpage() from working
@@ -1037,8 +1106,9 @@ class Title {
        function userCanRead() {
                global $wgUser;
 
-               $result = true;
-                       if ( !wfRunHooks( 'userCan', array( &$this, &$wgUser, "read", &$result ) ) ) {
+               $result = null;
+               wfRunHooks( 'userCan', array( &$this, &$wgUser, 'read', &$result ) );
+               if ( $result !== null ) {
                        return $result;
                }
 
@@ -1088,6 +1158,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
@@ -1364,7 +1450,7 @@ class Title {
                                                continue;
                                        }
 
-                                       # If there's an initial colon after the interwiki, that also 
+                                       # If there's an initial colon after the interwiki, that also
                                        # resets the default namespace
                                        if ( $t !== '' && $t[0] == ':' ) {
                                                $this->mNamespace = NS_MAIN;
@@ -1444,6 +1530,11 @@ class Title {
                        return false;
                }
 
+               // Any remaining initial :s are illegal.
+               if ( $t !== '' && ':' == $t{0} ) {
+                       return false;
+               }
+               
                # Fill fields
                $this->mDbkeyform = $t;
                $this->mUrlform = wfUrlencode( $t );
@@ -1547,7 +1638,7 @@ class Title {
                              AND pl_title=page_title
                            WHERE pl_from=?
                              AND page_namespace IS NULL
-                                 !",
+                                 !",
                        $db->tableName( 'pagelinks' ),
                        $db->tableName( 'page' ),
                        $this->getArticleId(),
@@ -1598,7 +1689,6 @@ class Title {
         * @access public
         */
        function isValidMoveOperation( &$nt, $auth = true ) {
-               global $wgUser;
                if( !$this or !$nt ) {
                        return 'badtitletext';
                }
@@ -1721,7 +1811,7 @@ class Title {
         * @access private
         */
        function moveOverExistingRedirect( &$nt, $reason = '' ) {
-               global $wgUser, $wgUseSquid, $wgMwRedir;
+               global $wgUseSquid, $wgMwRedir;
                $fname = 'Title::moveOverExistingRedirect';
                $comment = wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
 
@@ -1799,7 +1889,7 @@ class Title {
         * @access private
         */
        function moveToNewTitle( &$nt, $reason = '' ) {
-               global $wgUser, $wgUseSquid;
+               global $wgUseSquid;
                global $wgMwRedir;
                $fname = 'MovePageForm::moveToNewTitle';
                $comment = wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
@@ -1936,7 +2026,6 @@ class Title {
         * @access public
         */
        function createRedirect( $dest, $comment ) {
-               global $wgUser;
                if ( $this->getArticleID() ) {
                        return false;
                }
@@ -1976,7 +2065,7 @@ class Title {
         * @access public
         */
        function getParentCategories() {
-               global $wgContLang,$wgUser;
+               global $wgContLang;
 
                $titlekey = $this->getArticleId();
                $dbr =& wfGetDB( DB_SLAVE );
@@ -2071,9 +2160,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();
        }
 
        /**
@@ -2159,5 +2249,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() );
+               }
+       }
 }
 ?>