Tell regexp parser to use extra analysis on external link regexp;
[lhc/web/wiklou.git] / includes / Title.php
index d79aa5c..404c7c0 100644 (file)
@@ -93,30 +93,48 @@ class Title {
        /* static */ function &newFromText( $text, $defaultNamespace = 0 ) {    
                $fname = 'Title::newFromText';
                wfProfileIn( $fname );
+               
+               /**
+                * Wiki pages often contain multiple links to the same page.
+                * Title normalization and parsing can become expensive on
+                * pages with many links, so we can save a little time by
+                * caching them.
+                *
+                * In theory these are value objects and won't get changed...
+                */
+               static $titleCache = array();
+               if( $defaultNamespace == 0 && isset( $titleCache[$text] ) ) {
+                       wfProfileOut( $fname );
+                       return $titleCache[$text];
+               }
 
                /**
                 * Convert things like é into real text...
                 */
                global $wgInputEncoding;
-               $text = do_html_entity_decode( $text, ENT_COMPAT, $wgInputEncoding );
+               $filteredText = do_html_entity_decode( $text, ENT_COMPAT, $wgInputEncoding );
 
                /**
                 * Convert things like ā or 〗 into real text...
                 * WARNING: Not friendly to internal links on a latin-1 wiki.
                 */
-               $text = wfMungeToUtf8( $text );
+               $filteredText = wfMungeToUtf8( $filteredText );
                
                # What was this for? TS 2004-03-03
                # $text = urldecode( $text );
 
                $t =& new Title();
-               $t->mDbkeyform = str_replace( ' ', '_', $text );
+               $t->mDbkeyform = str_replace( ' ', '_', $filteredText );
                $t->mDefaultNamespace = $defaultNamespace;
 
                if( $t->secureAndSplit() ) {
+                       if( $defaultNamespace == 0 ) {
+                               $titleCache[$text] =& $t;
+                       }
                        wfProfileOut( $fname );
                        return $t;
                } else {
+                       $titleCache[$text] = null;
                        wfProfileOut( $fname );
                        return NULL;
                }
@@ -172,6 +190,9 @@ class Title {
         * Create a new Title from a namespace index and a DB key.
         * It's assumed that $ns and $title are *valid*, for instance when
         * they came directly from the database or a special page name.
+        * For convenience, spaces are converted to underscores so that
+        * eg user_text fields can be used directly.
+        *
         * @param int $ns the namespace of the article
         * @param string $title the unprefixed database key form
         * @return Title the new object
@@ -183,9 +204,9 @@ class Title {
                $t->mInterwiki = '';
                $t->mFragment = '';
                $t->mNamespace = IntVal( $ns );
-               $t->mDbkeyform = $title;
+               $t->mDbkeyform = str_replace( ' ', '_', $title );
                $t->mArticleID = ( $ns >= 0 ) ? -1 : 0;
-               $t->mUrlform = wfUrlencode( $title );
+               $t->mUrlform = wfUrlencode( $t->mDbkeyform );
                $t->mTextform = str_replace( '_', ' ', $title );
                return $t;
        }
@@ -564,8 +585,6 @@ class Title {
                $s = wfUrlencode ( $s ) ;
                
                # Cleaning up URL to make it look nice -- is this safe?
-               $s = preg_replace( '/%3[Aa]/', ':', $s );
-               $s = preg_replace( '/%2[Ff]/', '/', $s );
                $s = str_replace( '%28', '(', $s );
                $s = str_replace( '%29', ')', $s );
 
@@ -699,13 +718,21 @@ class Title {
 
        /**
         * Does the title correspond to a protected article?
+        * @param string $what the action the page is protected from,
+        *      by default checks move and edit
         * @return boolean
         * @access public
         */
-       function isProtected() {
+       function isProtected($action = '') {
                if ( -1 == $this->mNamespace ) { return true; }
-               $a = $this->getRestrictions();
-               if ( in_array( 'sysop', $a ) ) { return true; }
+               if($action == 'edit' || $action == '') {
+                       $a = $this->getRestrictions("edit");
+                       if ( in_array( 'sysop', $a ) ) { return true; }
+               }
+               if($action == 'move' || $action == '') {
+                       $a = $this->getRestrictions("move");
+                       if ( in_array( 'sysop', $a ) ) { return true; } 
+               }       
                return false;
        }
 
@@ -723,12 +750,13 @@ class Title {
                return $wgUser->isWatched( $this );
        }
 
-       /**
-        * Can $wgUser edit this page?
+       /**
+        * Is $wgUser perform $action this page?
+        * @param string $action action that permission needs to be checked for
         * @return boolean
-        * @access public
-        */
-       function userCanEdit() {
+        * @access private
+        */
+       function userCan($action) {
                $fname = 'Title::userCanEdit';
                wfProfileIn( $fname );
                
@@ -767,7 +795,7 @@ class Title {
                        return false;
                }
 
-               foreach( $this->getRestrictions() as $right ) {
+               foreach( $this->getRestrictions($action) as $right ) {
                        if( '' != $right && !$wgUser->isAllowed( $right ) ) {
                                wfProfileOut( $fname );
                                return false;
@@ -777,6 +805,24 @@ class Title {
                return true;
        }
 
+       /**
+        * Can $wgUser edit this page?
+        * @return boolean
+        * @access public
+        */
+       function userCanEdit() {
+               return $this->userCan('edit');
+       }
+       
+       /**
+        * Can $wgUser move this page?
+        * @return boolean
+        * @access public
+        */     
+       function userCanMove() {
+               return $this->userCan('move');
+       }
+
        /**
         * Can $wgUser read this page?
         * @return boolean
@@ -851,22 +897,44 @@ class Title {
                return ( $wgUser->isAllowed('editinterface') or preg_match('/^'.preg_quote($wgUser->getName(), '/').'\//', $this->mTextform) );
        }
 
+       /**
+        * Loads a string into mRestrictions array
+        * @param string $res restrictions in string format      
+        * @access public
+        */
+       function loadRestrictions( $res ) {
+               foreach( explode( ':', trim( $res ) ) as $restrict ) {
+                       $temp = explode( '=', trim( $restrict ) );
+                       if(count($temp) == 1) {
+                               // old format should be treated as edit/move restriction
+                               $this->mRestrictions["edit"] = explode( ',', trim( $temp[0] ) );
+                               $this->mRestrictions["move"] = explode( ',', trim( $temp[0] ) );
+                       } else {
+                               $this->mRestrictions[$temp[0]] = explode( ',', trim( $temp[1] ) );
+                       }
+               }
+               $this->mRestrictionsLoaded = true;
+       }
+
        /**
         * Accessor/initialisation for mRestrictions
+        * @param string $action action that permission needs to be checked for  
         * @return array the array of groups allowed to edit this article
         * @access public
         */
-       function getRestrictions() {
+       function getRestrictions($action) {
                $id = $this->getArticleID();
                if ( 0 == $id ) { return array(); }
 
                if ( ! $this->mRestrictionsLoaded ) {
                        $dbr =& wfGetDB( DB_SLAVE );
                        $res = $dbr->selectField( 'cur', 'cur_restrictions', 'cur_id='.$id );
-                       $this->mRestrictions = explode( ',', trim( $res ) );
-                       $this->mRestrictionsLoaded = true;
+                       $this->loadRestrictions( $res );
+               }
+               if( isset( $this->mRestrictions[$action] ) ) {
+                       return $this->mRestrictions[$action];
                }
-               return $this->mRestrictions;
+               return array();
        }
        
        /**