2 require_once ( "Parser.php" ) ;
5 * This should one day become the XML->(X)HTML parser
6 * Based on work by Jan Hidders and Magnus Manske
8 * $wgUseXMLparser = true ;
9 * $wgEnableParserCache = false ;
10 * $wgWiki2xml to the path and executable of the command line version (cli)
11 * in LocalSettings.php
13 * @subpackage Experimental
17 * the base class for an element
22 var $children = array();
25 * This finds the ATTRS element and returns the ATTR sub-children as a single string
27 function getSourceAttrs ()
30 foreach ($this->children
as $child)
32 if ( !is_string($child) AND $child->name
== "ATTRS" )
34 $ret = $child->makeXHTML ( $parser );
41 * This collects the ATTR thingies for getSourceAttrs()
43 function getTheseAttrs ()
46 foreach ($this->children
as $child)
48 if ( !is_string($child) AND $child->name
== "ATTR" )
50 $ret[] = $child->attrs
["NAME"] . "='" . $child->children
[0] . "'" ;
53 return implode ( " " , $ret ) ;
57 * This function generates the XHTML for the entire subtree
59 function sub_makeXHTML ( &$parser , $tag = "" , $attr = "" )
63 $attr2 = $this->getSourceAttrs () ;
64 if ( $attr != "" AND $attr2 != "" ) $attr .= " " ;
70 if ( $attr != "" ) $ret .= " " . $attr ;
74 foreach ($this->children
as $child) {
75 if ( is_string($child) ) {
77 } else if ( $child->name
!= "ATTRS" ) {
78 $ret .= $child->makeXHTML ( $parser );
82 $ret .= "</" . $tag . ">\n" ;
89 function createInternalLink ( &$parser , $target , $display_title , $options )
92 $skin = $wgUser->getSkin() ;
93 $tp = explode ( ":" , $target ) ; # tp = target parts
94 $title = "" ; # The plain title
95 $language = "" ; # The language/meta/etc. part
96 $namespace = "" ; # The namespace, if any
97 $subtarget = "" ; # The '#' thingy
100 $nt = Title
::newFromText ( $target ) ;
101 $fl = strtoupper ( $this->attrs
["FORCEDLINK"] ) == "YES" ;
103 if ( $fl ||
count ( $tp ) == 1 ) $title = $target ; # Plain and simple case
104 else # There's stuff missing here...
106 if ( $nt->getNamespace() == NS_IMAGE
)
108 $options[] = $display_title ;
109 return $skin->makeImageLinkObj ( $nt , implode ( "|" , $options ) ) ;
111 else $title = $target ; # Default
114 if ( $language != "" ) # External link within the WikiMedia project
116 return "{language link}" ;
118 else if ( $namespace != "" ) # Link to another namespace, check for image/media stuff
120 return "{namespace link}" ;
124 return $skin->makeLink ( $target , $display_title ) ;
128 function makeInternalLink ( &$parser )
132 foreach ($this->children
as $child) {
133 if ( is_string($child) ) {
134 # This shouldn't be the case!
136 if ( $child->name
== "LINKTARGET" )
137 $target = trim ( $child->makeXHTML ( $parser ) ) ;
139 $option[] = trim ( $child->makeXHTML ( $parser ) ) ;
143 if ( count ( $option ) == 0 ) $option[] = $target ; # Create dummy display title
144 $display_title = array_pop ( $option ) ;
145 return $this->createInternalLink ( $parser , $target , $display_title , $option ) ;
148 function getTemplateXHTML ( $title , $parts , &$parser ) {
149 global $wgLang , $wgUser ;
150 $skin = $wgUser->getSkin() ;
151 $ot = $title ; # Original title
152 if ( count ( explode ( ":" , $title ) ) == 1 )
153 $title = $wgLang->getNsText ( NS_TEMPLATE
) . ":" . $title ;
154 $nt = Title
::newFromText ( $title ) ;
155 $id = $nt->getArticleID() ;
156 if ( $id == 0 ) { # No/non-existing page
157 return $skin->makeBrokenLink ( $title , $ot ) ;
161 $tv = array () ; # Template variables
162 foreach ( $parts AS $part ) {
164 $x = explode ( "=" , $part , 2 ) ;
165 if ( count ( $x ) == 1 ) $key = "{$a}" ;
167 $value = array_pop ( $x ) ;
170 $art = new Article ( $nt ) ;
171 $text = $art->getContent ( false ) ;
172 $parser->plain_parse ( $text , true , $tv ) ;
178 * This function actually converts wikiXML into XHTML tags
180 function makeXHTML ( &$parser )
183 $n = $this->name
; # Shortcut
185 if ( $n == "EXTENSION" ) # Fix allowed HTML
188 $ext = strtoupper ( $this->attrs
["NAME"] ) ;
189 if ( $ext == "B" ||
$ext == "STRONG" ) $n = "BOLD" ;
190 else if ( $ext == "I" ||
$ext == "EM" ) $n = "ITALICS" ;
191 else if ( $ext == "U" ) $n = "UNDERLINED" ; # Hey, virtual wiki tag! ;-)
192 else if ( $ext == "S" ) $n = "STRIKE" ;
193 else if ( $ext == "P" ) $n = "PARAGRAPH" ;
194 else if ( $ext == "TABLE" ) $n = "TABLE" ;
195 else if ( $ext == "TR" ) $n = "TABLEROW" ;
196 else if ( $ext == "TD" ) $n = "TABLECELL" ;
197 else if ( $ext == "TH" ) $n = "TABLEHEAD" ;
198 else if ( $ext == "CAPTION" ) $n = "CAPTION" ;
199 else if ( $ext == "NOWIKI" ) $n = "NOWIKI" ;
200 if ( $n != $old_n ) unset ( $this->attrs
["NAME"] ) ; # Cleanup
201 else if ( $parser->nowiki
> 0 ) $n = "" ; # No "real" wiki tags allowed in nowiki section
204 if ( $n == "ARTICLE" )
205 $ret .= $this->sub_makeXHTML ( $parser ) ;
206 else if ( $n == "HEADING" )
207 $ret .= $this->sub_makeXHTML ( $parser , "h" . $this->attrs
["LEVEL"] ) ;
208 else if ( $n == "PARAGRAPH" )
209 $ret .= $this->sub_makeXHTML ( $parser , "p" ) ;
210 else if ( $n == "BOLD" )
211 $ret .= $this->sub_makeXHTML ( $parser , "strong" ) ;
212 else if ( $n == "ITALICS" )
213 $ret .= $this->sub_makeXHTML ( $parser , "em" ) ;
215 # These don't exist as wiki markup
216 else if ( $n == "UNDERLINED" )
217 $ret .= $this->sub_makeXHTML ( $parser , "u" ) ;
218 else if ( $n == "STRIKE" )
219 $ret .= $this->sub_makeXHTML ( $parser , "strike" ) ;
222 else if ( $n == "COMMENT" )
223 $ret .= "" ; # Comments are parsed out
226 else if ( $n == "LINK" )
227 $ret .= $this->makeInternalLink ( $parser ) ;
228 else if ( $n == "LINKTARGET" )
229 $ret .= $this->sub_makeXHTML ( $parser ) ;
230 else if ( $n == "LINKOPTION" )
231 $ret .= $this->sub_makeXHTML ( $parser ) ;
233 else if ( $n == "TEMPLATE" )
235 $parts = $this->sub_makeXHTML ( $parser ) ;
236 $parts = explode ( "|" , $parts ) ;
237 $title = array_shift ( $parts ) ;
238 $ret .= $this->getTemplateXHTML ( $title , $parts , &$parser ) ;
240 else if ( $n == "TEMPLATEVAR" )
242 $x = $this->sub_makeXHTML ( $parser ) ;
243 if ( isset ( $parser->mCurrentTemplateOptions
["{$x}"] ) )
244 $ret .= $parser->mCurrentTemplateOptions
["{$x}"] ;
247 else if ( $n == "IGNORE" ) # Internal use, not generated by wiki2xml parser
248 $ret .= $this->sub_makeXHTML ( $parser ) ;
250 else if ( $n == "NOWIKI" )
253 $ret .= $this->sub_makeXHTML ( $parser , "" ) ;
257 # Unknown HTML extension
258 else if ( $n == "EXTENSION" ) # This is currently a dummy!!!
260 $ext = $this->attrs
["NAME"] ;
262 $ret .= "<" . $ext . ">" ;
263 $ret .= $this->sub_makeXHTML ( $parser ) ;
264 $ret .= "</" . $ext . "> " ;
268 else if ( $n == "TABLE" )
270 $ret .= $this->sub_makeXHTML ( $parser , "table" ) ;
272 else if ( $n == "TABLEROW" )
274 $ret .= $this->sub_makeXHTML ( $parser , "tr" ) ;
276 else if ( $n == "TABLECELL" )
278 $ret .= $this->sub_makeXHTML ( $parser , "td" ) ;
280 else if ( $n == "TABLEHEAD" )
282 $ret .= $this->sub_makeXHTML ( $parser , "th" ) ;
284 else if ( $n == "CAPTION" )
286 $ret .= $this->sub_makeXHTML ( $parser , "caption" ) ;
289 else if ( $n == "ATTRS" ) # SPECIAL CASE : returning attributes
291 return $this->getTheseAttrs () ;
295 else if ( $n == "LISTITEM" )
297 if ( $parser->mListType
== "dl" ) $ret .= $this->sub_makeXHTML ( $parser , "dd" ) ;
298 else $ret .= $this->sub_makeXHTML ( $parser , "li" ) ;
300 else if ( $n == "LIST" )
302 $type = "ol" ; # Default
303 if ( $this->attrs
["TYPE"] == "bullet" ) $type = "ul" ;
304 else if ( $this->attrs
["TYPE"] == "indent" ) $type = "dl" ;
305 $oldtype = $parser->mListType
;
306 $parser->mListType
= $type ;
307 $ret .= $this->sub_makeXHTML ( $parser , $type ) ;
308 $parser->mListType
= $oldtype ;
311 # Something else entirely
314 $ret .= "<" . $n . ">" ;
315 $ret .= $this->sub_makeXHTML ( $parser ) ;
316 $ret .= "</" . $n . "> " ;
319 $ret = "\n{$ret}\n" ;
320 $ret = str_replace ( "\n\n" , "\n" , $ret ) ;
325 * A function for additional debugging output
329 $ret .= "<li> <b> Name: </b> $this->name </li>\n";
331 $ret .= '<li> <b> Attributes: </b>';
332 foreach ($this->attrs
as $name => $value) {
333 $ret .= "$name => $value; " ;
337 foreach ($this->children
as $child) {
338 if ( is_string($child) ) {
339 $ret .= "<li> $child </li>\n";
341 $ret .= $child->myPrint();
349 $ancStack = array(); // the stack with ancestral elements
351 // Three global functions needed for parsing, sorry guys
352 function wgXMLstartElement($parser, $name, $attrs) {
355 $newElem = new element
;
356 $newElem->name
= $name;
357 $newElem->attrs
= $attrs;
359 array_push($ancStack, $newElem);
362 function wgXMLendElement($parser, $name) {
363 global $ancStack, $rootElem;
364 // pop element off stack
365 $elem = array_pop ($ancStack);
366 if (count ($ancStack) == 0)
369 // add it to its parent
370 array_push ($ancStack[count($ancStack)-1]->children
, $elem);
373 function wgXMLcharacterData($parser, $data) {
375 $data = trim ($data); // Don't add blank lines, they're no use...
376 // add to parent if parent exists
377 if ( $ancStack && $data != "" ) {
378 array_push ($ancStack[count($ancStack)-1]->children
, $data);
384 * Here's the class that generates a nice tree
388 function &scanFile( $filename ) {
389 global $ancStack, $rootElem;
392 $xml_parser = xml_parser_create();
393 xml_set_element_handler ($xml_parser, 'wgXMLstartElement', 'wgXMLendElement');
394 xml_set_character_data_handler ($xml_parser, 'wgXMLcharacterData');
395 if (!($fp = fopen($filename, 'r'))) {
396 die('could not open XML input');
398 while ($data = fread($fp, 4096)) {
399 if (!xml_parse($xml_parser, $data, feof($fp))) {
400 die(sprintf("XML error: %s at line %d",
401 xml_error_string(xml_get_error_code($xml_parser)),
402 xml_get_current_line_number($xml_parser)));
405 xml_parser_free($xml_parser);
407 // return the remaining root element we copied in the beginning
411 function scanString ( $input ) {
412 global $ancStack, $rootElem;
415 $xml_parser = xml_parser_create();
416 xml_set_element_handler ($xml_parser, 'wgXMLstartElement', 'wgXMLendElement');
417 xml_set_character_data_handler ($xml_parser, 'wgXMLcharacterData');
419 if (!xml_parse ($xml_parser, $input, true)) {
420 die (sprintf ("XML error: %s at line %d",
421 xml_error_string(xml_get_error_code($xml_parser)),
422 xml_get_current_line_number($xml_parser)));
424 xml_parser_free ($xml_parser);
426 // return the remaining root element we copied in the beginning
432 class ParserXML
EXTENDS Parser
438 var $mTagHooks, $mListType;
440 # Cleared with clearState():
441 var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
442 var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
445 var $mOptions, $mTitle, $mOutputType,
446 $mTemplates, // cache of already loaded templates, avoids
447 // multiple SQL queries for the same string
448 $mTemplatePath; // stores an unsorted hash of all the templates already loaded
449 // in this path. Used for loop detection.
451 var $nowikicount , $mCurrentTemplateOptions ;
460 function ParserXML() {
461 $this->mTemplates
= array();
462 $this->mTemplatePath
= array();
463 $this->mTagHooks
= array();
472 function clearState() {
473 $this->mOutput
= new ParserOutput
;
474 $this->mAutonumber
= 0;
475 $this->mLastSection
= "";
476 $this->mDTopen
= false;
477 $this->mVariables
= false;
478 $this->mIncludeCount
= array();
479 $this->mStripState
= array();
480 $this->mArgStack
= array();
481 $this->mInPre
= false;
485 * Turns the wikitext into XML by calling the external parser
488 function runXMLparser ( &$text ) {
491 $tmpfname = tempnam("/tmp", "FOO");
492 $handle = fopen($tmpfname, "w");
493 fwrite($handle, $text);
495 exec ( $wgWiki2xml . " < " . $tmpfname , $a ) ;
496 $text = implode ( "\n" , $a ) ;
500 function plain_parse ( &$text , $inline = false , $templateOptions = array () ) {
501 $this->runXMLparser ( $text ) ;
504 $result = $w->scanString( $text );
506 $oldTemplateOptions = $this->mCurrentTemplateOptions
;
507 $this->mCurrentTemplateOptions
= $templateOptions ;
509 if ( $inline ) { # Inline rendering off for templates
510 if ( count ( $result->children
) == 1 )
511 $result->children
[0]->name
= "IGNORE" ;
514 if ( 1 ) $text = $result->makeXHTML ( $this ) ; # No debugging info
515 else $text = $result->makeXHTML ( $this ) . "<hr>" . $text . "<hr>" . $result->myPrint();
516 $this->mCurrentTemplateOptions
= $oldTemplateOptions ;
519 function parse( $text, &$title, $options, $linestart = true, $clearState = true ) {
520 $this->plain_parse ( $text ) ;
521 $this->mOutput
->setText ( $text ) ;
522 return $this->mOutput
;