replace TYPE= with ENGINE=, (supported since 4.0, TYPE deprecated since 4.1)
[lhc/web/wiklou.git] / includes / Parser.php
index 2b89d92..3b5d119 100644 (file)
@@ -73,7 +73,7 @@ define( 'EXT_IMAGE_REGEX',
  *   performs brace substitution on MediaWiki messages
  *
  * Globals used:
- *    objects:   $wgLang
+ *    objects:   $wgLang, $wgContLang
  *
  * NOT $wgArticle, $wgUser or $wgTitle. Keep them away!
  *
@@ -236,11 +236,11 @@ class Parser
 
                $this->replaceLinkHolders( $text );
 
-               # the position of the convert() call should not be changed. it
-               # assumes that the links are all replaces and the only thing left
+               # the position of the parserConvert() call should not be changed. it
+               # assumes that the links are all replaced and the only thing left
                # is the <nowiki> mark.
-               $text = $wgContLang->convert($text);
-               $this->mOutput->setTitleText($wgContLang->getParsedTitle());
+               # Side-effects: this calls $this->mOutput->setTitleText()
+               $text = $wgContLang->parserConvert( $text, $this );
 
                $text = $this->unstripNoWiki( $text, $this->mStripState );
 
@@ -270,6 +270,9 @@ class Parser
                return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
        }
 
+       function &getTitle() { return $this->mTitle; }
+       function getOptions() { return $this->mOptions; }
+
        /**
         * Replaces all occurrences of <$tag>content</$tag> in the text
         * with a random marker and returns the new text. the output parameter
@@ -478,8 +481,8 @@ class Parser
                        $state['nowiki'] = $state['nowiki'] + $nowiki_content;
                        $state['math'] = $state['math'] + $math_content;
                        $state['pre'] = $state['pre'] + $pre_content;
-                       $state['comment'] = $state['comment'] + $comment_content;
                        $state['gallery'] = $state['gallery'] + $gallery_content;
+                       $state['comment'] = $state['comment'] + $comment_content;
 
                        foreach( $ext_content as $tag => $array ) {
                                if ( array_key_exists( $tag, $state ) ) {
@@ -492,8 +495,8 @@ class Parser
                          'nowiki' => $nowiki_content,
                          'math' => $math_content,
                          'pre' => $pre_content,
-                         'comment' => $comment_content,
                          'gallery' => $gallery_content,
+                         'comment' => $comment_content,
                        ) + $ext_content;
                }
                return $text;
@@ -623,6 +626,11 @@ class Parser
                $pipes = array();
                $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts$opts", $descriptorspec, $pipes);
                if (is_resource($process)) {
+                       // Theoretically, this style of communication could cause a deadlock
+                       // here. If the stdout buffer fills up, then writes to stdin could
+                       // block. This doesn't appear to happen with tidy, because tidy only
+                       // writes to stdout after it's finished reading from stdin. Search
+                       // for tidyParseStdin and tidySaveStdout in console/tidy.c
                        fwrite($pipes[0], $text);
                        fclose($pipes[0]);
                        while (!feof($pipes[1])) {
@@ -687,6 +695,7 @@ class Parser
                $ltd = array () ; # Was it TD or TH?
                $tr = array () ; # Is currently a tr tag open?
                $ltr = array () ; # tr attributes
+               $has_opened_tr = array(); # Did this table open a <tr> element?
                $indent_level = 0; # indent level of the table
                foreach ( $t AS $k => $x )
                {
@@ -703,11 +712,13 @@ class Parser
                                array_push ( $ltd , '' ) ;
                                array_push ( $tr , false ) ;
                                array_push ( $ltr , '' ) ;
+                               array_push ( $has_opened_tr, false );
                        }
                        else if ( count ( $td ) == 0 ) { } # Don't do any of the following
                        else if ( '|}' == substr ( $x , 0 , 2 ) ) {
                                $z = "</table>" . substr ( $x , 2);
                                $l = array_pop ( $ltd ) ;
+                               if ( !array_pop ( $has_opened_tr ) ) $z = "<tr><td></td></tr>" . $z ;
                                if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
                                if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
                                array_pop ( $ltr ) ;
@@ -718,6 +729,8 @@ class Parser
                                while ( $x != '' && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
                                $z = '' ;
                                $l = array_pop ( $ltd ) ;
+                               array_pop ( $has_opened_tr );
+                               array_push ( $has_opened_tr , true ) ;
                                if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
                                if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
                                array_pop ( $ltr ) ;
@@ -749,6 +762,8 @@ class Parser
                                                if ( !array_pop ( $tr ) ) $z = '<tr'.$tra.">\n" ;
                                                array_push ( $tr , true ) ;
                                                array_push ( $ltr , '' ) ;
+                                               array_pop ( $has_opened_tr );
+                                               array_push ( $has_opened_tr , true ) ;
                                        }
 
                                        $l = array_pop ( $ltd ) ;
@@ -781,8 +796,10 @@ class Parser
                # Closing open td, tr && table
                while ( count ( $td ) > 0 )
                {
+                       $l = array_pop ( $ltd ) ;
                        if ( array_pop ( $td ) ) $t[] = '</td>' ;
                        if ( array_pop ( $tr ) ) $t[] = '</tr>' ;
+                       if ( !array_pop ( $has_opened_tr ) ) $t[] = "<tr><td></td></tr>" ;
                        $t[] = '</table>' ;
                }
 
@@ -798,7 +815,6 @@ class Parser
         * @access private
         */
        function internalParse( $text ) {
-               global $wgContLang;
                $args = array();
                $isMain = true;
                $fname = 'Parser::internalParse';
@@ -1209,12 +1225,12 @@ class Parser
 
        /**
         * Replace unusual URL escape codes with their equivalent characters
-        * @param string 
+        * @param string
         * @return string
         * @static
         */
        function replaceUnusualEscapes( $url ) {
-               return preg_replace_callback( '/%[0-9A-Fa-f]{2}/', 
+               return preg_replace_callback( '/%[0-9A-Fa-f]{2}/',
                        array( 'Parser', 'replaceUnusualEscapesCallback' ), $url );
        }
 
@@ -2005,6 +2021,8 @@ class Parser
                                return $this->mTitle->getPrefixedText();
                        case MAG_FULLPAGENAMEE:
                                return $this->mTitle->getPrefixedURL();
+                       case MAG_SUBPAGENAME:
+                               return $this->mTitle->getSubpageText();
                        case MAG_REVISIONID:
                                return $this->mRevisionId;
                        case MAG_NAMESPACE:
@@ -2353,7 +2371,7 @@ class Parser
                $fname = 'Parser::braceSubstitution';
                wfProfileIn( $fname );
 
-               # Flags 
+               # Flags
                $found = false;             # $text has been filled
                $nowiki = false;            # wiki markup in $text should be escaped
                $noparse = false;           # Unsafe HTML tags should not be stripped, etc.
@@ -2720,7 +2738,7 @@ class Parser
        }
 
        /**
-        * Fetch the unparsed text of a template and register a reference to it. 
+        * Fetch the unparsed text of a template and register a reference to it.
         */
        function fetchTemplate( $title ) {
                $text = false;
@@ -3003,6 +3021,8 @@ class Parser
                        # strip out HTML
                        $canonized_headline = preg_replace( '/<.*?' . '>/','',$canonized_headline );
                        $tocline = trim( $canonized_headline );
+                       # Save headline for section edit hint before it's escaped
+                       $headline_hint = trim( $canonized_headline );
                        $canonized_headline = Sanitizer::escapeId( $tocline );
                        $refers[$headlineCount] = $canonized_headline;
 
@@ -3031,7 +3051,7 @@ class Parser
                                if( $istemplate )
                                        $head[$headlineCount] .= $sk->editSectionLinkForOther($templatetitle, $templatesection);
                                else
-                                       $head[$headlineCount] .= $sk->editSectionLink($this->mTitle, $sectionCount+1);
+                                       $head[$headlineCount] .= $sk->editSectionLink($this->mTitle, $sectionCount+1, $headline_hint);
                        }
 
                        # give headline the correct <h#> tag
@@ -3258,7 +3278,7 @@ class Parser
                
                # Signatures
                $sigText = $this->getUserSig( $user );
-               $text = strtr( $text, array( 
+               $text = strtr( $text, array(
                        '~~~~~' => $d,
                        '~~~~' => "$sigText $d",
                        '~~~' => $sigText
@@ -3311,8 +3331,6 @@ class Parser
         * @access private
         */
        function getUserSig( &$user ) {
-               global $wgContLang;
-
                $username = $user->getName();
                $nickname = $user->getOption( 'nickname' );
                $nickname = $nickname === '' ? $username : $nickname;
@@ -3414,8 +3432,11 @@ class Parser
 
        /**
         * Create an HTML-style tag, e.g. <yourtag>special text</yourtag>
-        * Callback will be called with the text within
-        * Transform and return the text within
+        * The callback should have the following form:
+        *    function myParserHook( $text, $params, &$parser ) { ... }
+        *
+        * Transform and return $text. Use $parser for any required context, e.g. use
+        * $parser->getTitle() and $parser->getOptions() not $wgTitle or $wgOut->mParserOptions
         *
         * @access public
         *
@@ -3599,9 +3620,6 @@ class Parser
         * @return string
         */
        function replaceLinkHoldersText( $text ) {
-               global $wgUser;
-               global $wgOutputReplace;
-
                $fname = 'Parser::replaceLinkHoldersText';
                wfProfileIn( $fname );
 
@@ -3685,7 +3703,7 @@ class Parser
         * Parse image options text and use it to make an image
         */
        function makeImage( &$nt, $options ) {
-               global $wgContLang, $wgUseImageResize, $wgUser;
+               global $wgUseImageResize;
 
                $align = '';
 
@@ -3765,7 +3783,7 @@ class Parser
                $this->mOutput->mCacheTime = -1;
        }
 
-       /**#@+ 
+       /**#@+
         * Callback from the Sanitizer for expanding items found in HTML attribute
         * values, so they can be safely tested and escaped.
         * @param string $text
@@ -3967,8 +3985,8 @@ class ParserOptions
 
        /** Get user options */
        function initialiseFromUser( &$userInput ) {
-               global $wgUseTeX, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages,
-                      $wgAllowExternalImagesFrom, $wgAllowSpecialInclusion;
+               global $wgUseTeX, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
+               global $wgAllowExternalImagesFrom, $wgAllowSpecialInclusion;
                $fname = 'ParserOptions::initialiseFromUser';
                wfProfileIn( $fname );
                if ( !$userInput ) {