* Remove deprecated $wgOnlySysopsCanPatrol references; use User::isAllowed( 'patrol...
[lhc/web/wiklou.git] / includes / Title.php
index 92d8787..de575cb 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];
                }
 
@@ -146,10 +144,8 @@ class Title {
                                $cachedcount++;
                                $wgTitleCache[$text] =& $t;
                        }
-                       wfProfileOut( $fname );
                        return $t;
                } else {
-                       wfProfileOut( $fname );
                        $ret = NULL;
                        return $ret;
                }
@@ -383,18 +379,14 @@ class Title {
                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) {
-                       wfProfileOut( $fname );
                        return Title::getInterwikiCached( $key );
                }
 
@@ -402,7 +394,6 @@ class Title {
                # 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;
                }
 
@@ -411,7 +402,6 @@ class Title {
                        array( 'iw_url', 'iw_local', 'iw_trans' ),
                        array( 'iw_prefix' => $key ), $fname );
                if( !$res ) {
-                       wfProfileOut( $fname );
                        return '';
                }
 
@@ -426,7 +416,6 @@ class Title {
                $wgMemc->set( $k, $s, $wgInterwikiExpiry );
                $wgTitleInterwikiCache[$k] = $s;
 
-               wfProfileOut( $fname );
                return $s->iw_url;
        }
        
@@ -452,11 +441,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);
                }
@@ -505,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 );
@@ -662,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 );
@@ -679,7 +667,6 @@ class Title {
         * @access public
         */
        function getFullText() {
-               global $wgContLang;
                $text = $this->getPrefixedText();
                if( '' != $this->mFragment ) {
                        $text .= '#' . $this->mFragment;
@@ -687,6 +674,20 @@ 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 title (not an actual URL) including interwiki
         * @return string the URL-encoded form
@@ -763,6 +764,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 == '' ) {
@@ -845,8 +853,6 @@ class Title {
         * @access public
         */
        function getEditURL() {
-               global $wgServer, $wgScript;
-
                if ( '' != $this->mInterwiki ) { return ''; }
                $s = $this->getLocalURL( 'action=edit' );
 
@@ -870,6 +876,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,
@@ -931,6 +954,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;
@@ -951,7 +981,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;
@@ -1034,6 +1064,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 {
@@ -1080,6 +1115,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
@@ -1285,7 +1336,6 @@ class Title {
        /* private */ function secureAndSplit() {
                global $wgContLang, $wgLocalInterwiki, $wgCapitalLinks;
                $fname = 'Title::secureAndSplit';
-               wfProfileIn( $fname );
 
                # Initialisation
                static $rxTc = false;
@@ -1303,13 +1353,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;
                }
 
@@ -1340,7 +1388,6 @@ class Title {
                                        if( !$firstPass ) {
                                                # Can't make a local interwiki link to an interwiki link.
                                                # That's just crazy!
-                                               wfProfileOut( $fname );
                                                return false;
                                        }
 
@@ -1352,7 +1399,6 @@ class Title {
                                        if ( 0 == strcasecmp( $this->mInterwiki, $wgLocalInterwiki ) ) {
                                                if( $t == '' ) {
                                                        # Can't have an empty self-link
-                                                       wfProfileOut( $fname );
                                                        return false;
                                                }
                                                $this->mInterwiki = '';
@@ -1360,6 +1406,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.
@@ -1385,7 +1438,6 @@ class Title {
                # Reject illegal characters.
                #
                if( preg_match( $rxTc, $r ) ) {
-                       wfProfileOut( $fname );
                        return false;
                }
 
@@ -1401,14 +1453,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;
                }
 
@@ -1434,7 +1484,6 @@ class Title {
                if( $t == '' &&
                        $this->mInterwiki == '' &&
                        $this->mNamespace != NS_MAIN ) {
-                       wfProfileOut( $fname );
                        return false;
                }
 
@@ -1444,7 +1493,6 @@ class Title {
 
                $this->mTextform = str_replace( '_', ' ', $t );
 
-               wfProfileOut( $fname );
                return true;
        }
 
@@ -1542,7 +1590,7 @@ class Title {
                              AND pl_title=page_title
                            WHERE pl_from=?
                              AND page_namespace IS NULL
-                                 !",
+                                 !",
                        $db->tableName( 'pagelinks' ),
                        $db->tableName( 'page' ),
                        $this->getArticleId(),
@@ -1593,7 +1641,6 @@ class Title {
         * @access public
         */
        function isValidMoveOperation( &$nt, $auth = true ) {
-               global $wgUser;
                if( !$this or !$nt ) {
                        return 'badtitletext';
                }
@@ -1716,7 +1763,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() );
 
@@ -1794,7 +1841,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() );
@@ -1931,7 +1978,6 @@ class Title {
         * @access public
         */
        function createRedirect( $dest, $comment ) {
-               global $wgUser;
                if ( $this->getArticleID() ) {
                        return false;
                }
@@ -1971,7 +2017,7 @@ class Title {
         * @access public
         */
        function getParentCategories() {
-               global $wgContLang,$wgUser;
+               global $wgContLang;
 
                $titlekey = $this->getArticleId();
                $dbr =& wfGetDB( DB_SLAVE );
@@ -2066,9 +2112,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();
        }
 
        /**
@@ -2154,5 +2201,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() );
+               }
+       }
 }
 ?>