r5479 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r5478‎ | r5479 | r5480 >
Date:05:16, 25 September 2004
Author:wmahan
Status:old
Tags:
Comment:
Fix two parser bugs: bug 41 and bug 529. For the former, I replaced
a broken check for '{{{' in braceSubstitution(). For the latter,
I undid part of JeLuF's fix to bug 523 and changed
braceSubstitution() to add a newline '{|', '#', '*', ';', or ':'
come at the beginning of a template and a newline isn't already
present. Also, minor cleanups to the template section code.
Modified paths:
  • /trunk/phase3/includes/Parser.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Parser.php
@@ -1586,7 +1586,7 @@
15871587 $text = preg_replace_callback( "/{{{([$titleChars]*?)}}}/", 'wfArgSubstitution', $text );
15881588 }
15891589 # Template substitution
1590 - $regex = '/{{(['.$titleChars.']*)(\\|.*?|)}}/s';
 1590+ $regex = '/(\\n|{)?{{(['.$titleChars.']*)(\\|.*?|)}}/s';
15911591 $text = preg_replace_callback( $regex, 'wfBraceSubstitution', $text );
15921592
15931593 array_pop( $this->mArgStack );
@@ -1663,21 +1663,25 @@
16641664 $found = false;
16651665 $nowiki = false;
16661666 $noparse = false;
1667 - $itcamefromthedatabase = false;
16681667
16691668 $title = NULL;
16701669
 1670+ # Need to know if the template comes at the start of a line,
 1671+ # to treat the beginning of the template like the beginning
 1672+ # of a line for tables and block-level elements.
 1673+ $linestart = $matches[1];
 1674+
16711675 # $part1 is the bit before the first |, and must contain only title characters
16721676 # $args is a list of arguments, starting from index 0, not including $part1
16731677
1674 - $part1 = $matches[1];
1675 - # If the second subpattern matched anything, it will start with |
 1678+ $part1 = $matches[2];
 1679+ # If the third subpattern matched anything, it will start with |
16761680
1677 - $args = $this->getTemplateArgs($matches[2]);
 1681+ $args = $this->getTemplateArgs($matches[3]);
16781682 $argc = count( $args );
16791683
1680 - # {{{}}}
1681 - if ( strpos( $matches[0], '{{{' ) !== false ) {
 1684+ # Don't parse {{{}}} because that's only for template arguments
 1685+ if ( $linestart === '{' ) {
16821686 $text = $matches[0];
16831687 $found = true;
16841688 $noparse = true;
@@ -1713,7 +1717,7 @@
17141718 $mwInt =& MagicWord::get( MAG_INT );
17151719 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
17161720 if ( $this->incrementIncludeCount( 'int:'.$part1 ) ) {
1717 - $text = wfMsgReal( $part1, $args, true );
 1721+ $text = $linestart . wfMsgReal( $part1, $args, true );
17181722 $found = true;
17191723 }
17201724 }
@@ -1725,12 +1729,12 @@
17261730 $mwNs = MagicWord::get( MAG_NS );
17271731 if ( $mwNs->matchStartAndRemove( $part1 ) ) {
17281732 if ( intval( $part1 ) ) {
1729 - $text = $wgContLang->getNsText( intval( $part1 ) );
 1733+ $text = $linestart . $wgContLang->getNsText( intval( $part1 ) );
17301734 $found = true;
17311735 } else {
17321736 $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
17331737 if ( !is_null( $index ) ) {
1734 - $text = $wgContLang->getNsText( $index );
 1738+ $text = $linestart . $wgContLang->getNsText( $index );
17351739 $found = true;
17361740 }
17371741 }
@@ -1754,9 +1758,9 @@
17551759 $title = Title::newFromText( $part1 );
17561760 if ( !is_null( $title ) ) {
17571761 if ( $argc > 0 ) {
1758 - $text = $title->$func( $args[0] );
 1762+ $text = $linestart . $title->$func( $args[0] );
17591763 } else {
1760 - $text = $title->$func();
 1764+ $text = $linestart . $title->$func();
17611765 }
17621766 $found = true;
17631767 }
@@ -1767,7 +1771,7 @@
17681772 if ( !$found && $argc == 1 ) {
17691773 $mwGrammar =& MagicWord::get( MAG_GRAMMAR );
17701774 if ( $mwGrammar->matchStartAndRemove( $part1 ) ) {
1771 - $text = $wgContLang->convertGrammar( $args[0], $part1 );
 1775+ $text = $linestart . $wgContLang->convertGrammar( $args[0], $part1 );
17721776 $found = true;
17731777 }
17741778 }
@@ -1778,7 +1782,7 @@
17791783 # and we need to check for loops.
17801784 if ( !$found && isset( $this->mTemplates[$part1] ) ) {
17811785 # set $text to cached message.
1782 - $text = $this->mTemplates[$part1];
 1786+ $text = $linestart . $this->mTemplates[$part1];
17831787 $found = true;
17841788
17851789 # Infinite loop test
@@ -1801,14 +1805,13 @@
18021806 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
18031807 if ( $articleContent !== false ) {
18041808 $found = true;
1805 - $text = $articleContent;
1806 - $itcamefromthedatabase = true;
 1809+ $text = $linestart . $articleContent;
18071810 }
18081811 }
18091812
18101813 # If the title is valid but undisplayable, make a link to it
18111814 if ( $this->mOutputType == OT_HTML && !$found ) {
1812 - $text = '[['.$title->getPrefixedText().']]';
 1815+ $text = $linestart . '[['.$title->getPrefixedText().']]';
18131816 $found = true;
18141817 }
18151818
@@ -1852,28 +1855,22 @@
18531856 if ( $this->mOutputType == OT_HTML && !is_null( $title ) ) {
18541857 $wgLinkCache->addLinkObj( $title );
18551858 }
1856 - }
18571859
1858 - # Empties the template path
1859 - $this->mTemplatePath = array();
1860 - if ( !$found ) {
1861 - return $matches[0];
1862 - } else {
18631860 # replace ==section headers==
18641861 # XXX this needs to go away once we have a better parser.
1865 - if ( $this->mOutputType != OT_WIKI && $itcamefromthedatabase ) {
 1862+ if ( $this->mOutputType == OT_HTML ) {
18661863 if( !is_null( $title ) )
18671864 $encodedname = base64_encode($title->getPrefixedDBkey());
18681865 else
18691866 $encodedname = base64_encode("");
1870 - $matches = preg_split('/(^={1,6}.*?={1,6}\s*?$)/m', $text, -1,
 1867+ $m = preg_split('/(^={1,6}.*?={1,6}\s*?$)/m', $text, -1,
18711868 PREG_SPLIT_DELIM_CAPTURE);
18721869 $text = '';
18731870 $nsec = 0;
1874 - for( $i = 0; $i < count($matches); $i += 2 ) {
1875 - $text .= $matches[$i];
1876 - if (!isset($matches[$i + 1]) || $matches[$i + 1] == "") continue;
1877 - $hl = $matches[$i + 1];
 1871+ for( $i = 0; $i < count($m); $i += 2 ) {
 1872+ $text .= $m[$i];
 1873+ if (!isset($m[$i + 1]) || $m[$i + 1] == "") continue;
 1874+ $hl = $m[$i + 1];
18781875 if( strstr($hl, "<!--MWTEMPLATESECTION") ) {
18791876 $text .= $hl;
18801877 continue;
@@ -1885,6 +1882,19 @@
18861883 $nsec++;
18871884 }
18881885 }
 1886+
 1887+ # If the template begins with a table or block-level
 1888+ # element, it should be treated as beginning a new line.
 1889+ if ($linestart !== '\n' && preg_match('/^({\\||:|;|#|\*)/', $text)) {
 1890+ $text = "\n" . $text;
 1891+ }
 1892+ }
 1893+
 1894+ # Empties the template path
 1895+ $this->mTemplatePath = array();
 1896+ if ( !$found ) {
 1897+ return $matches[0];
 1898+ } else {
18891899 return $text;
18901900 }
18911901 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r29205* Removed noargs/noparse in braceSubstitution(), they have been largely obsol...tstarling05:37, 3 January 2008

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r5293BUG#523 Don't remove newlines in front of templates or template variablesjeluf21:02, 18 September 2004

Status & tagging log