diff options
author | Magnus Manske <magnusmanske@users.mediawiki.org> | 2004-12-21 13:51:15 +0000 |
---|---|---|
committer | Magnus Manske <magnusmanske@users.mediawiki.org> | 2004-12-21 13:51:15 +0000 |
commit | 9079e54474b40e5c60d58b23e9b2da84f1a5636d (patch) | |
tree | 760371e68b9b1d3087f5e3e892e77ecb0a92caac /includes/ParserXML.php | |
parent | d1fd1a58ee80bfa368de451592b3aaf359df428d (diff) | |
download | mediawikicore-9079e54474b40e5c60d58b23e9b2da84f1a5636d.tar.gz mediawikicore-9079e54474b40e5c60d58b23e9b2da84f1a5636d.zip |
can now handle templates (no recursive check, yet)
Notes
Notes:
http://mediawiki.org/wiki/Special:Code/MediaWiki/6730
Diffstat (limited to 'includes/ParserXML.php')
-rw-r--r-- | includes/ParserXML.php | 74 |
1 files changed, 61 insertions, 13 deletions
diff --git a/includes/ParserXML.php b/includes/ParserXML.php index 2c5661a8470a..83f4c0990406 100644 --- a/includes/ParserXML.php +++ b/includes/ParserXML.php @@ -145,6 +145,35 @@ class element { return $this->createInternalLink ( $parser , $target , $display_title , $option ) ; } + function getTemplateXHTML ( $title , $parts , &$parser ) { + global $wgLang , $wgUser ; + $skin = $wgUser->getSkin() ; + $ot = $title ; # Original title + if ( count ( explode ( ":" , $title ) ) == 1 ) + $title = $wgLang->getNsText ( NS_TEMPLATE ) . ":" . $title ; + $nt = Title::newFromText ( $title ) ; + $id = $nt->getArticleID() ; + if ( $id == 0 ) { # No/non-existing page + return $skin->makeBrokenLink ( $title , $ot ) ; + } + + $a = 0 ; + $tv = array () ; # Template variables + foreach ( $parts AS $part ) { + $a++ ; + $x = explode ( "=" , $part , 2 ) ; + if ( count ( $x ) == 1 ) $key = "{$a}" ; + else $key = $x[0] ; + $value = array_pop ( $x ) ; + $tv[$key] = $value ; + } + $art = new Article ( $nt ) ; + $text = $art->getContent ( false ) ; + $parser->plain_parse ( $text , true , $tv ) ; + + return $text ; + } + /** * This function actually converts wikiXML into XHTML tags */ @@ -197,6 +226,23 @@ class element { else if ( $n == "LINKOPTION" ) $ret .= $this->sub_makeXHTML ( $parser ) ; + else if ( $n == "TEMPLATE" ) + { + $parts = $this->sub_makeXHTML ( $parser ) ; + $parts = explode ( "|" , $parts ) ; + $title = array_shift ( $parts ) ; + $ret .= $this->getTemplateXHTML ( $title , $parts , &$parser ) ; + } + else if ( $n == "TEMPLATEVAR" ) + { + $x = $this->sub_makeXHTML ( $parser ) ; + if ( isset ( $parser->mCurrentTemplateOptions["{$x}"] ) ) + $ret .= $parser->mCurrentTemplateOptions["{$x}"] ; + } + + else if ( $n == "IGNORE" ) # Internal use, not generated by wiki2xml parser + $ret .= $this->sub_makeXHTML ( $parser ) ; + else if ( $n == "NOWIKI" ) { $parser->nowiki++ ; @@ -372,16 +418,6 @@ class xml2php { } -/* Example code: - - $w = new xml2php; - $filename = 'sample.xml'; - $result = $w->scanFile( $filename ); - print $result->myPrint(); -*/ - -$dummytext = "<article><heading level='2'> R-type </heading><paragraph><link><linktarget>image:a.jpg</linktarget><linkoption>1</linkoption><linkoption>2</linkoption><linkoption>3</linkoption><linkoption>text</linkoption></link></paragraph><paragraph>The <link><linktarget>video game</linktarget><linkoption>computer game</linkoption></link> <bold>R-type</bold> is <extension name='nowiki'>cool & stuff</extension> because:</paragraph><list type='bullet'><listitem>it's nice</listitem><listitem>it's fast</listitem><listitem>it has:<list type='bullet'><listitem>graphics</listitem><listitem>sound</listitem></list></listitem></list><table><tablerow><tablecell>Version 1 </tablecell><tablecell>not bad</tablecell></tablerow><tablerow><tablecell>Version 2 </tablecell><tablecell>much better </tablecell></tablerow></table><paragraph>This is a || token in the middle of text.</paragraph></article>" ; - class ParserXML EXTENDS Parser { /**#@+ @@ -401,7 +437,7 @@ class ParserXML EXTENDS Parser $mTemplatePath; // stores an unsorted hash of all the templates already loaded // in this path. Used for loop detection. - var $nowikicount ; + var $nowikicount , $mCurrentTemplateOptions ; /**#@-*/ @@ -450,15 +486,27 @@ class ParserXML EXTENDS Parser unlink($tmpfname); } - function parse( $text, &$title, $options, $linestart = true, $clearState = true ) { + function plain_parse ( &$text , $inline = false , $templateOptions = array () ) { $this->runXMLparser ( $text ) ; $nowikicount = 0 ; $w = new xml2php; $result = $w->scanString( $text ); + $oldTemplateOptions = $this->mCurrentTemplateOptions ; + $this->mCurrentTemplateOptions = $templateOptions ; + + if ( $inline ) { # Inline rendering off for templates + if ( count ( $result->children ) == 1 ) + $result->children[0]->name = "IGNORE" ; + } + if ( 1 ) $text = $result->makeXHTML ( $this ) ; # No debugging info else $text = $result->makeXHTML ( $this ) . "<hr>" . $text . "<hr>" . $result->myPrint(); - + $this->mCurrentTemplateOptions = $oldTemplateOptions ; + } + + function parse( $text, &$title, $options, $linestart = true, $clearState = true ) { + $this->plain_parse ( $text ) ; $this->mOutput->setText ( $text ) ; return $this->mOutput; } |