can now handle templates (no recursive check, yet)
authorMagnus Manske <magnusmanske@users.mediawiki.org>
Tue, 21 Dec 2004 13:51:15 +0000 (13:51 +0000)
committerMagnus Manske <magnusmanske@users.mediawiki.org>
Tue, 21 Dec 2004 13:51:15 +0000 (13:51 +0000)
includes/ParserXML.php

index 2c5661a..83f4c09 100644 (file)
@@ -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 &amp; 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;
        }