--help ftw !
[lhc/web/wiklou.git] / includes / Title.php
index e005c63..b2ec314 100644 (file)
@@ -9,6 +9,8 @@
 require_once( 'normal/UtfNormal.php' );
 
 $wgTitleInterwikiCache = array();
+$wgTitleCache = array();
+
 define ( 'GAID_FOR_UPDATE', 1 );
 
 # Title::newFromTitle maintains a cache to avoid
@@ -103,6 +105,7 @@ class Title {
         * @access public
         */
        function newFromText( $text, $defaultNamespace = NS_MAIN ) {
+               global $wgTitleCache;
                $fname = 'Title::newFromText';
                wfProfileIn( $fname );
 
@@ -118,10 +121,9 @@ class Title {
                 *
                 * In theory these are value objects and won't get changed...
                 */
-               static $titleCache = array();
-               if( $defaultNamespace == NS_MAIN && isset( $titleCache[$text] ) ) {
+               if( $defaultNamespace == NS_MAIN && isset( $wgTitleCache[$text] ) ) {
                        wfProfileOut( $fname );
-                       return $titleCache[$text];
+                       return $wgTitleCache[$text];
                }
 
                /**
@@ -135,11 +137,11 @@ class Title {
 
                if( $t->secureAndSplit() ) {
                        if( $defaultNamespace == NS_MAIN ) {
-                               if( count( $titleCache ) >= MW_TITLECACHE_MAX ) {
+                               if( count( $wgTitleCache ) >= MW_TITLECACHE_MAX ) {
                                        # Avoid memory leaks on mass operations...
-                                       $titleCache = array();
+                                       $wgTitleCache = array();
                                }
-                               $titleCache[$text] =& $t;
+                               $wgTitleCache[$text] =& $t;
                        }
                        wfProfileOut( $fname );
                        return $t;
@@ -216,7 +218,7 @@ class Title {
                $t =& new Title();
                $t->mInterwiki = '';
                $t->mFragment = '';
-               $t->mNamespace = IntVal( $ns );
+               $t->mNamespace = intval( $ns );
                $t->mDbkeyform = str_replace( ' ', '_', $title );
                $t->mArticleID = ( $ns >= 0 ) ? -1 : 0;
                $t->mUrlform = wfUrlencode( $t->mDbkeyform );
@@ -316,24 +318,8 @@ class Title {
         * @access public
         */
        function legalChars() {
-               # Missing characters:
-               #  * []|# Needed for link syntax
-               #  * % and + are corrupted by Apache when they appear in the path
-               #
-               # % seems to work though
-               #
-               # The problem with % is that URLs are double-unescaped: once by Apache's
-               # path conversion code, and again by PHP. So %253F, for example, becomes "?".
-               # Our code does not double-escape to compensate for this, indeed double escaping
-               # would break if the double-escaped title was passed in the query string
-               # rather than the path. This is a minor security issue because articles can be
-               # created such that they are hard to view or edit. -- TS
-               #
-               # Theoretically 0x80-0x9F of ISO 8859-1 should be disallowed, but
-               # this breaks interlanguage links
-
-               $set = " %!\"$&'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF";
-               return $set;
+               global $wgLegalTitleChars;
+               return $wgLegalTitleChars;
        }
 
        /**
@@ -393,6 +379,8 @@ class Title {
 
                wfProfileIn( $fname );
 
+               $key = strtolower( $key );
+
                $k = $wgDBname.':interwiki:'.$key;
                if( array_key_exists( $k, $wgTitleInterwikiCache ) ) {
                        wfProfileOut( $fname );
@@ -554,6 +542,25 @@ class Title {
         * @access public
         */
        function getNamespace() { return $this->mNamespace; }
+       /**
+        * Get the namespace text
+        * @return string
+        * @access public
+        */
+       function getNsText() { 
+               global $wgContLang;
+               return $wgContLang->getNsText( $this->mNamespace );
+       }
+       /**
+        * Get the namespace text of the subject (rather than talk) page
+        * @return string
+        * @access public
+        */
+       function getSubjectNsText() {
+               global $wgContLang;
+               return $wgContLang->getNsText( Namespace::getSubject( $this->mNamespace ) );
+       }
+
        /**
         * Get the interwiki prefix (or null string)
         * @return string
@@ -602,7 +609,7 @@ class Title {
         */
        function getPrefixedText() {
                global $wgContLang;
-               if ( empty( $this->mPrefixedText ) ) {
+               if ( empty( $this->mPrefixedText ) ) { // FIXME: bad usage of empty() ?
                        $s = $this->prefix( $this->mTextform );
                        $s = str_replace( '_', ' ', $s );
                        $this->mPrefixedText = $s;
@@ -654,93 +661,33 @@ class Title {
         * @access public
         */
        function getFullURL( $query = '' ) {
-               global $wgContLang, $wgServer, $wgScript, $wgMakeDumpLinks, $wgArticlePath;
+               global $wgContLang, $wgServer;
 
                if ( '' == $this->mInterwiki ) {
-                       return $wgServer . $this->getLocalUrl( $query );
-               } elseif ( $wgMakeDumpLinks && $wgContLang->getLanguageName( $this->mInterwiki ) ) {
-                       $baseUrl = str_replace( '$1', "../../{$this->mInterwiki}/$1", $wgArticlePath );
-                       $baseUrl = str_replace( '$1', $this->getHashedDirectory() . '/$1', $baseUrl );
+                       $url = $wgServer . $this->getLocalUrl( $query );
                } else {
                        $baseUrl = $this->getInterwikiLink( $this->mInterwiki );
-               }
 
-               $namespace = $wgContLang->getNsText( $this->mNamespace );
-               if ( '' != $namespace ) {
-                       # Can this actually happen? Interwikis shouldn't be parsed.
-                       $namepace .= ':';
-               }
-               $url = str_replace( '$1', $namespace . $this->mUrlform, $baseUrl );
-               if( $query != '' ) {
-                       if( false === strpos( $url, '?' ) ) {
-                               $url .= '?';
-                       } else {
-                               $url .= '&';
+                       $namespace = $wgContLang->getNsText( $this->mNamespace );
+                       if ( '' != $namespace ) {
+                               # Can this actually happen? Interwikis shouldn't be parsed.
+                               $namespace .= ':';
                        }
-                       $url .= $query;
-               }
-               if ( '' != $this->mFragment ) {
-                       $url .= '#' . $this->mFragment;
-               }
-               return $url;
-       }
-
-       /**
-        * Get a relative directory for putting an HTML version of this article into
-        */
-       function getHashedDirectory() {
-               global $wgMakeDumpLinks, $wgInputEncoding;
-               $dbkey = $this->getDBkey();
-
-               # Split into characters
-               if ( $wgInputEncoding == 'UTF-8' ) {
-                       preg_match_all( '/./us', $dbkey, $m );
-               } else {
-                       preg_match_all( '/./s', $dbkey, $m );
-               }
-               $chars = $m[0];
-               $length = count( $chars );
-               $dir = '';
-
-               for ( $i = 0; $i < $wgMakeDumpLinks; $i++ ) {
-                       if ( $i ) {
-                               $dir .= '/';
+                       $url = str_replace( '$1', $namespace . $this->mUrlform, $baseUrl );
+                       if( $query != '' ) {
+                               if( false === strpos( $url, '?' ) ) {
+                                       $url .= '?';
+                               } else {
+                                       $url .= '&';
+                               }
+                               $url .= $query;
                        }
-                       if ( $i >= $length ) {
-                               $dir .= '_';
-                       } elseif ( ord( $chars[$i] ) > 32 ) {
-                               $dir .= strtolower( $chars[$i] );
-                       } else {
-                               $dir .= sprintf( "%02X", ord( $chars[$i] ) );
+                       if ( '' != $this->mFragment ) {
+                               $url .= '#' . $this->mFragment;
                        }
                }
-               return $dir;
-       }
-
-       function getHashedFilename() {
-               $dbkey = $this->getPrefixedDBkey();
-               $mainPage = Title::newMainPage();
-               if ( $mainPage->getPrefixedDBkey() == $dbkey ) {
-                       return 'index.html';
-               }
-
-               $dir = $this->getHashedDirectory();
-
-               # Replace illegal charcters for Windows paths with underscores
-               $friendlyName = strtr( $dbkey, '/\\*?"<>|~', '_________' );
-
-               # Work out lower case form. We assume we're on a system with case-insensitive
-               # filenames, so unless the case is of a special form, we have to disambiguate
-               $lowerCase = $this->prefix( ucfirst( strtolower( $this->getDBkey() ) ) );
-
-               # Make it mostly unique
-               if ( $lowerCase != $friendlyName  ) {
-                       $friendlyName .= '_' . substr(md5( $dbkey ), 0, 4);
-               }
-               # Handle colon specially by replacing it with tilde
-               # Thus we reduce the number of paths with hashes appended
-               $friendlyName = str_replace( ':', '~', $friendlyName );
-               return "$dir/$friendlyName.html";
+               wfRunHooks( 'GetFullURL', array( &$this, &$url, $query ) );
+               return $url;
        }
 
        /**
@@ -752,40 +699,42 @@ class Title {
         * @access public
         */
        function getLocalURL( $query = '' ) {
-               global $wgLang, $wgArticlePath, $wgScript, $wgMakeDumpLinks, $wgServer, $action;
+               global $wgArticlePath, $wgScript, $wgServer, $wgRequest;
 
                if ( $this->isExternal() ) {
-                       return $this->getFullURL();
-               }
-
-               $dbkey = wfUrlencode( $this->getPrefixedDBkey() );
-               if ( $wgMakeDumpLinks ) {
-                       $url = str_replace( '$1', wfUrlencode( $this->getHashedFilename() ), $wgArticlePath );
-               } elseif ( $query == '' ) {
-                       $url = str_replace( '$1', $dbkey, $wgArticlePath );
+                       $url = $this->getFullURL();
                } else {
-                       global $wgActionPaths;
-                       if( !empty( $wgActionPaths ) &&
-                               preg_match( '/^(.*&|)action=([^&]*)(&(.*)|)$/', $query, $matches ) ) {
-                               $action = urldecode( $matches[2] );
-                               if( isset( $wgActionPaths[$action] ) ) {
-                                       $query = $matches[1];
-                                       if( isset( $matches[4] ) ) $query .= $matches[4];
-                                       $url = str_replace( '$1', $dbkey, $wgActionPaths[$action] );
-                                       if( $query != '' ) $url .= '?' . $query;
-                                       return $url;
+                       $dbkey = wfUrlencode( $this->getPrefixedDBkey() );
+                       if ( $query == '' ) {
+                               $url = str_replace( '$1', $dbkey, $wgArticlePath );
+                       } else {
+                               global $wgActionPaths;
+                               $url = false;
+                               if( !empty( $wgActionPaths ) &&
+                                       preg_match( '/^(.*&|)action=([^&]*)(&(.*)|)$/', $query, $matches ) ) 
+                               {
+                                       $action = urldecode( $matches[2] );
+                                       if( isset( $wgActionPaths[$action] ) ) {
+                                               $query = $matches[1];
+                                               if( isset( $matches[4] ) ) $query .= $matches[4];
+                                               $url = str_replace( '$1', $dbkey, $wgActionPaths[$action] );
+                                               if( $query != '' ) $url .= '?' . $query;
+                                       }
+                               }
+                               if ( $url === false ) {
+                                       if ( $query == '-' ) {
+                                               $query = '';
+                                       }
+                                       $url = "{$wgScript}?title={$dbkey}&{$query}";
                                }
                        }
-                       if ( $query == '-' ) {
-                               $query = '';
+
+                       if ($wgRequest->getText('action') == 'render') {
+                               $url = $wgServer . $url;
                        }
-                       $url = "{$wgScript}?title={$dbkey}&{$query}";
                }
-
-               if ($action == 'render')
-                       return $wgServer . $url;
-               else
-                       return $url;
+               wfRunHooks( 'GetLocalURL', array( &$this, &$url, $query ) );
+               return $url;
        }
 
        /**
@@ -896,13 +845,13 @@ class Title {
        }
 
        /**
-        * Is $wgUser perform $action this page?
+        * Can $wgUser perform $action this page?
         * @param string $action action that permission needs to be checked for
         * @return boolean
         * @access private
         */
        function userCan($action) {
-               $fname = 'Title::userCanEdit';
+               $fname = 'Title::userCan';
                wfProfileIn( $fname );
 
                global $wgUser;
@@ -910,11 +859,14 @@ class Title {
                        wfProfileOut( $fname );
                        return false;
                }
+               // XXX: This is the code that prevents unprotecting a page in NS_MEDIAWIKI
+               // from taking effect -ævar
                if( NS_MEDIAWIKI == $this->mNamespace &&
                    !$wgUser->isAllowed('editinterface') ) {
                        wfProfileOut( $fname );
                        return false;
                }
+               
                if( $this->mDbkeyform == '_' ) {
                        # FIXME: Is this necessary? Shouldn't be allowed anyway...
                        wfProfileOut( $fname );
@@ -951,7 +903,8 @@ class Title {
                        }
                }
 
-               if( $action == 'move' && !$this->isMovable() ) {
+               if( $action == 'move' &&
+                       !( $this->isMovable() && $wgUser->isAllowed( 'move' ) ) ) {
                        wfProfileOut( $fname );
                        return false;
                }
@@ -1149,7 +1102,6 @@ class Title {
                                $this->mArticleID = $wgLinkCache->addLinkObj( $this );
                        }
                }
-wfdebug("title: articleid = ".$this->mArticleID."\n");
                return $this->mArticleID;
        }
 
@@ -1281,57 +1233,58 @@ wfdebug("title: articleid = ".$this->mArticleID."\n");
 
                $this->mDbkeyform = $t;
 
-               # Initial colon indicating main namespace
+               # Initial colon indicates main namespace rather than specified default
+               # but should not create invalid {ns,title} pairs such as {0,Project:Foo}
                if ( ':' == $t{0} ) {
-                       $r = substr( $t, 1 );
                        $this->mNamespace = NS_MAIN;
-               } else {
-                       # Namespace or interwiki prefix
-                       $firstPass = true;
-                       do {
-                               if ( preg_match( "/^(.+?)_*:_*(.*)$/S", $t, $m ) ) {
-                                       $p = $m[1];
-                                       $lowerNs = strtolower( $p );
-                                       if ( $ns = Namespace::getCanonicalIndex( $lowerNs ) ) {
-                                               # Canonical namespace
-                                               $t = $m[2];
-                                               $this->mNamespace = $ns;
-                                       } elseif ( $ns = $wgContLang->getNsIndex( $lowerNs )) {
-                                               # Ordinary namespace
-                                               $t = $m[2];
-                                               $this->mNamespace = $ns;
-                                       } elseif( $this->getInterwikiLink( $p ) ) {
-                                               if( !$firstPass ) {
-                                                       # Can't make a local interwiki link to an interwiki link.
-                                                       # That's just crazy!
+                       $t = substr( $t, 1 ); # remove the colon but continue processing
+               }
+
+               # Namespace or interwiki prefix
+               $firstPass = true;
+               do {
+                       if ( preg_match( "/^(.+?)_*:_*(.*)$/S", $t, $m ) ) {
+                               $p = $m[1];
+                               $lowerNs = strtolower( $p );
+                               if ( $ns = Namespace::getCanonicalIndex( $lowerNs ) ) {
+                                       # Canonical namespace
+                                       $t = $m[2];
+                                       $this->mNamespace = $ns;
+                               } elseif ( $ns = $wgContLang->getNsIndex( $lowerNs )) {
+                                       # Ordinary namespace
+                                       $t = $m[2];
+                                       $this->mNamespace = $ns;
+                               } elseif( $this->getInterwikiLink( $p ) ) {
+                                       if( !$firstPass ) {
+                                               # Can't make a local interwiki link to an interwiki link.
+                                               # That's just crazy!
+                                               wfProfileOut( $fname );
+                                               return false;
+                                       }
+
+                                       # Interwiki link
+                                       $t = $m[2];
+                                       $this->mInterwiki = strtolower( $p );
+
+                                       # Redundant interwiki prefix to the local wiki
+                                       if ( 0 == strcasecmp( $this->mInterwiki, $wgLocalInterwiki ) ) {
+                                               if( $t == '' ) {
+                                                       # Can't have an empty self-link
                                                        wfProfileOut( $fname );
                                                        return false;
                                                }
-
-                                               # Interwiki link
-                                               $t = $m[2];
-                                               $this->mInterwiki = $p;
-
-                                               # Redundant interwiki prefix to the local wiki
-                                               if ( 0 == strcasecmp( $this->mInterwiki, $wgLocalInterwiki ) ) {
-                                                       if( $t == '' ) {
-                                                               # Can't have an empty self-link
-                                                               wfProfileOut( $fname );
-                                                               return false;
-                                                       }
-                                                       $this->mInterwiki = '';
-                                                       $firstPass = false;
-                                                       # Do another namespace split...
-                                                       continue;
-                                               }
+                                               $this->mInterwiki = '';
+                                               $firstPass = false;
+                                               # Do another namespace split...
+                                               continue;
                                        }
-                                       # If there's no recognized interwiki or namespace,
-                                       # then let the colon expression be part of the title.
                                }
-                               break;
-                       } while( true );
-                       $r = $t;
-               }
+                               # If there's no recognized interwiki or namespace,
+                               # then let the colon expression be part of the title.
+                       }
+                       break;
+               } while( true );
+               $r = $t;
 
                # We already know that some pages won't be in the database!
                #
@@ -1658,6 +1611,7 @@ wfdebug("title: articleid = ".$this->mArticleID."\n");
                        $u->doUpdate();
                }
 
+               global $wgUser;
                wfRunHooks( 'TitleMoveComplete', array( &$this, &$nt, &$wgUser, $pageid, $redirid ) );
                return true;
        }
@@ -1846,7 +1800,7 @@ wfdebug("title: articleid = ".$this->mArticleID."\n");
                # Is it a redirect?
                $id  = $nt->getArticleID();
                $obj = $dbw->selectRow( array( 'page', 'revision', 'text'),
-                       array( 'page_is_redirect','old_text' ),
+                       array( 'page_is_redirect','old_text','old_flags' ),
                        array( 'page_id' => $id, 'page_latest=rev_id', 'rev_text_id=old_id' ),
                        $fname, 'FOR UPDATE' );
 
@@ -1854,14 +1808,18 @@ wfdebug("title: articleid = ".$this->mArticleID."\n");
                        # Not a redirect
                        return false;
                }
+               $text = Revision::getRevisionText( $obj );
 
                # Does the redirect point to the source?
-               if ( preg_match( "/\\[\\[\\s*([^\\]\\|]*)]]/", $obj->old_text, $m ) ) {
+               if ( preg_match( "/\\[\\[\\s*([^\\]\\|]*)]]/", $text, $m ) ) {
                        $redirTitle = Title::newFromText( $m[1] );
                        if( !is_object( $redirTitle ) ||
                                $redirTitle->getPrefixedDBkey() != $this->getPrefixedDBkey() ) {
                                return false;
                        }
+               } else {
+                       # Fail safe
+                       return false;
                }
 
                # Does the article have a history?
@@ -2014,8 +1972,8 @@ wfdebug("title: articleid = ".$this->mArticleID."\n");
        function getPreviousRevisionID( $revision ) {
                $dbr =& wfGetDB( DB_SLAVE );
                return $dbr->selectField( 'revision', 'rev_id',
-                       'rev_page=' . IntVal( $this->getArticleId() ) .
-                       ' AND rev_id<' . IntVal( $revision ) . ' ORDER BY rev_id DESC' );
+                       'rev_page=' . intval( $this->getArticleId() ) .
+                       ' AND rev_id<' . intval( $revision ) . ' ORDER BY rev_id DESC' );
        }
 
        /**
@@ -2027,8 +1985,8 @@ wfdebug("title: articleid = ".$this->mArticleID."\n");
        function getNextRevisionID( $revision ) {
                $dbr =& wfGetDB( DB_SLAVE );
                return $dbr->selectField( 'revision', 'rev_id',
-                       'rev_page=' . IntVal( $this->getArticleId() ) .
-                       ' AND rev_id>' . IntVal( $revision ) . ' ORDER BY rev_id' );
+                       'rev_page=' . intval( $this->getArticleId() ) .
+                       ' AND rev_id>' . intval( $revision ) . ' ORDER BY rev_id' );
        }
 
        /**
@@ -2037,7 +1995,7 @@ wfdebug("title: articleid = ".$this->mArticleID."\n");
         * @param Title $title
         * @return bool
         */
-       function equals( &$title ) {
+       function equals( $title ) {
                return $this->getInterwiki() == $title->getInterwiki()
                        && $this->getNamespace() == $title->getNamespace()
                        && $this->getDbkey() == $title->getDbkey();