Fix bug 77: semicolon-colon wikitext syntax requires extra space now;
[lhc/web/wiklou.git] / includes / Parser.php
index c7d27a6..0335920 100644 (file)
@@ -184,9 +184,6 @@ class Parser
                # only once and last
                $text = $this->doBlockLevels( $text, $linestart );
                $text = $this->unstripNoWiki( $text, $this->mStripState );
-               if($wgUseTidy) {
-                       $text = $this->tidy($text);
-               }
                $this->mOutput->setText( $text );
                wfProfileOut( $fname );
                return $this->mOutput;
@@ -483,7 +480,8 @@ class Parser
        /**
         * interface with html tidy, used if $wgUseTidy = true
         *
-        * @access private
+        * @access public
+        * @static
         */
        function tidy ( $text ) {
                global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
@@ -492,15 +490,16 @@ class Parser
                wfProfileIn( $fname );
 
                $cleansource = '';
+               $opts = '';
                switch(strtoupper($wgOutputEncoding)) {
                        case 'ISO-8859-1':
-                               $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)? ' -latin1':' -raw';
+                               $opts .= ($wgInputEncoding == $wgOutputEncoding)? ' -latin1':' -raw';
                                break;
                        case 'UTF-8':
-                               $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)? ' -utf8':' -raw';
+                               $opts .= ($wgInputEncoding == $wgOutputEncoding)? ' -utf8':' -raw';
                                break;
                        default:
-                               $wgTidyOpts .= ' -raw';
+                               $opts .= ' -raw';
                        }
 
                $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
@@ -511,7 +510,7 @@ class Parser
                        1 => array('pipe', 'w'),
                        2 => array('file', '/dev/null', 'a')
                );
-               $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts", $descriptorspec, $pipes);
+               $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts$opts", $descriptorspec, $pipes);
                if (is_resource($process)) {
                        fwrite($pipes[0], $wrappedtext);
                        fclose($pipes[0]);
@@ -659,11 +658,11 @@ class Parser
                        $text = $wgDateFormatter->reformat( $this->mOptions->getDateFormat(), $text );
                }
                $text = $this->doAllQuotes( $text );
-               $text = $this->doMagicLinks( $text );
                $text = $this->replaceInternalLinks ( $text );
                # Another call to replace links and images inside captions of images
                $text = $this->replaceInternalLinks ( $text );
                $text = $this->replaceExternalLinks( $text );
+               $text = $this->doMagicLinks( $text );
                $text = $this->doTableStuff( $text );
                $text = $this->formatHeadings( $text, $isMain );
                $sk =& $this->mOptions->getSkin();
@@ -1058,7 +1057,6 @@ class Parser
         */
        function replaceInternalLinks( $s ) {
                global $wgLang, $wgContLang, $wgLinkCache;
-               global $wgNamespacesWithSubpages;
                static $fname = 'Parser::replaceInternalLinks' ;
                wfProfileIn( $fname );
 
@@ -1135,43 +1133,13 @@ class Parser
                                continue;
                        }
 
-                       # Valid link forms:
-                       # Foobar -- normal
-                       # :Foobar -- override special treatment of prefix (images, language links)
-                       # /Foobar -- convert to CurrentPage/Foobar
-                       # /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
-                       
-                       # Look at the first character
-                       $c = substr($m[1],0,1);
-                       $noforce = ($c != ':');
-                       
-                       # subpage
-                       if( $c == '/' ) {
-                               # / at end means we don't want the slash to be shown
-                               if(substr($m[1],-1,1)=='/') {
-                                       $m[1]=substr($m[1],1,strlen($m[1])-2);
-                                       $noslash=$m[1];
-                               } else {
-                                       $noslash=substr($m[1],1);
-                               }
-                               
-                               # Some namespaces don't allow subpages
-                               if(!empty($wgNamespacesWithSubpages[$this->mTitle->getNamespace()])) {
-                                       # subpages allowed here
-                                       $link = $this->mTitle->getPrefixedText(). '/' . trim($noslash);
-                                       if( '' == $text ) {
-                                               $text= $m[1];
-                                       } # this might be changed for ugliness reasons
-                               } else {
-                                       # no subpage allowed, use standard link
-                                       $link = $noslash;
-                               }
-                               
-                       } elseif( $noforce ) { # no subpage
-                               $link = $m[1];
-                       } else {
-                               # We don't want to keep the first character
-                               $link = substr( $m[1], 1 );
+                       # Make subpage if necessary
+                       $link = $this->maybeDoSubpageLink( $m[1], $text );
+
+                       $noforce = (substr($m[1], 0, 1) != ':');
+                       if (!$noforce) {
+                               # Strip off leading ':'
+                               $link = substr($link, 1);
                        }
                        
                        $wasblank = ( '' == $text );
@@ -1251,6 +1219,53 @@ class Parser
                return $s;
        }
 
+       /**
+        * Handle link to subpage if necessary
+        * @param $target string the source of the link
+        * @param &$text the link text, modified as necessary
+        * @return string the full name of the link
+        * @access private
+        */
+       function maybeDoSubpageLink($target, &$text) {
+               # Valid link forms:
+               # Foobar -- normal
+               # :Foobar -- override special treatment of prefix (images, language links)
+               # /Foobar -- convert to CurrentPage/Foobar
+               # /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
+               global $wgNamespacesWithSubpages;
+
+               $fname = 'Parser::maybeDoSubpageLink';
+               wfProfileIn( $fname );
+               # Look at the first character
+               if( $target{0} == '/' ) {
+                       # / at end means we don't want the slash to be shown
+                       if(substr($target,-1,1)=='/') {
+                               $target=substr($target,1,-1);
+                               $noslash=$target;
+                       } else {
+                               $noslash=substr($target,1);
+                       }
+                               
+                       # Some namespaces don't allow subpages
+                       if(!empty($wgNamespacesWithSubpages[$this->mTitle->getNamespace()])) {
+                               # subpages allowed here
+                               $ret = $this->mTitle->getPrefixedText(). '/' . trim($noslash);
+                               if( '' === $text ) {
+                                       $text = $target;
+                               } # this might be changed for ugliness reasons
+                       } else {
+                               # no subpage allowed, use standard link
+                               $ret = $target;
+                       }
+               } else {
+                       # no subpage
+                       $ret = $target;
+               }
+
+               wfProfileOut( $fname );
+               return $ret;
+       }
+
        /**#@+
         * Used by doBlockLevels()
         * @access private
@@ -1383,7 +1398,7 @@ class Parser
                                        # So we check for : in the remainder text to split up the
                                        # title and definition, without b0rking links.
                                        # FIXME: This is not foolproof. Something better in Tokenizer might help.
-                                       if( preg_match( '/^(.*?(?:\s|&nbsp;)):(.*)$/', $t, $match ) ) {
+                                       if( preg_match( '/^(.*?):(.*)$/', $t, $match ) ) {
                                                $term = $match[1];
                                                $output .= $term . $this->nextItem( ':' );
                                                $t = $match[2];
@@ -1407,7 +1422,7 @@ class Parser
 
                                        if ( ';' == $char ) {
                                                # FIXME: This is dupe of code above
-                                               if( preg_match( '/^(.*?(?:\s|&nbsp;)):(.*)$/', $t, $match ) ) {
+                                               if( preg_match( '/^(.*?):(.*)$/', $t, $match ) ) {
                                                        $term = $match[1];
                                                        $output .= $term . $this->nextItem( ':' );
                                                        $t = $match[2];
@@ -1538,12 +1553,15 @@ class Parser
         * @access private
         */
        function initialiseVariables() {
+               $fname = 'Parser::initialiseVariables';
+               wfProfileIn( $fname );
                global $wgVariableIDs;
                $this->mVariables = array();
                foreach ( $wgVariableIDs as $id ) {
                        $mw =& MagicWord::get( $id );
                        $mw->addToArray( $this->mVariables, $this->getVariableValue( $id ) );
                }
+               wfProfileOut( $fname );
        }
 
        /**
@@ -1796,7 +1814,12 @@ class Parser
                # Load from database
                $itcamefromthedatabase = false;
                if ( !$found ) {
-                       $title = Title::newFromText( $part1, NS_TEMPLATE );
+                       $ns = NS_TEMPLATE;
+                       $part1 = $this->maybeDoSubpageLink( $part1, $subpage='' );
+                       if ($subpage !== '') {
+                               $ns = $this->mTitle->getNamespace();
+                       }
+                       $title = Title::newFromText( $part1, $ns );
                        if ( !is_null( $title ) && !$title->isExternal() ) {
                                # Check for excessive inclusion
                                $dbk = $title->getPrefixedDBkey();