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 ) ;
56 function fixLinkTails ( &$parser , $key )
59 if ( !isset ( $this->children
[$k2] ) ) return ;
60 if ( !is_string ( $this->children
[$k2]) ) return ;
61 if ( is_string ( $this->children
[$key]) ) return ;
62 if ( $this->children
[$key]->name
!= "LINK" ) return ;
64 $n = $this->children
[$k2] ;
67 ( ( $n[0] >= 'a' AND $n[0] <= 'z' ) OR
68 $n[0] == 'ä' OR $n[0] == 'ö' OR
69 $n[0] == 'ü' OR $n[0] == 'ß' ) )
72 $n = substr ( $n , 1 ) ;
74 $this->children
[$k2] = $n ;
76 if ( count ( $this->children
[$key]->children
) > 1 )
78 $kl = array_keys ( $this->children
[$key]->children
) ;
79 $kl = array_pop ( $kl ) ;
80 $this->children
[$key]->children
[$kl]->children
[] = $s ;
85 $e->name
= "LINKOPTION" ;
86 $t = $this->children
[$key]->sub_makeXHTML ( $parser ) ;
87 $e->children
[] = trim ( $t ) . $s ;
88 $this->children
[$key]->children
[] = $e ;
93 * This function generates the XHTML for the entire subtree
95 function sub_makeXHTML ( &$parser , $tag = "" , $attr = "" )
99 $attr2 = $this->getSourceAttrs () ;
100 if ( $attr != "" AND $attr2 != "" ) $attr .= " " ;
106 if ( $attr != "" ) $ret .= " " . $attr ;
110 # THIS SHOULD BE DONE IN THE WIKI2XML-PARSER INSTEAD
111 # foreach ( array_keys ( $this->children ) AS $x )
112 # $this->fixLinkTails ( $parser , $x ) ;
114 foreach ($this->children
as $key => $child) {
115 if ( is_string($child) ) {
117 } else if ( $child->name
!= "ATTRS" ) {
118 $ret .= $child->makeXHTML ( $parser );
122 $ret .= "</" . $tag . ">\n" ;
129 function createInternalLink ( &$parser , $target , $display_title , $options )
132 $skin = $wgUser->getSkin() ;
133 $tp = explode ( ":" , $target ) ; # tp = target parts
134 $title = "" ; # The plain title
135 $language = "" ; # The language/meta/etc. part
136 $namespace = "" ; # The namespace, if any
137 $subtarget = "" ; # The '#' thingy
140 $nt = Title
::newFromText ( $target ) ;
141 $fl = strtoupper ( $this->attrs
["FORCEDLINK"] ) == "YES" ;
143 if ( $fl ||
count ( $tp ) == 1 ) $title = $target ; # Plain and simple case
144 else # There's stuff missing here...
146 if ( $nt->getNamespace() == NS_IMAGE
)
148 $options[] = $display_title ;
149 return $skin->makeImageLinkObj ( $nt , implode ( "|" , $options ) ) ;
151 else $title = $target ; # Default
154 if ( $language != "" ) # External link within the WikiMedia project
156 return "{language link}" ;
158 else if ( $namespace != "" ) # Link to another namespace, check for image/media stuff
160 return "{namespace link}" ;
164 return $skin->makeLink ( $target , $display_title ) ;
168 function makeInternalLink ( &$parser )
172 foreach ($this->children
as $child) {
173 if ( is_string($child) ) {
174 # This shouldn't be the case!
176 if ( $child->name
== "LINKTARGET" )
177 $target = trim ( $child->makeXHTML ( $parser ) ) ;
179 $option[] = trim ( $child->makeXHTML ( $parser ) ) ;
183 if ( count ( $option ) == 0 ) $option[] = $target ; # Create dummy display title
184 $display_title = array_pop ( $option ) ;
185 return $this->createInternalLink ( $parser , $target , $display_title , $option ) ;
188 function getTemplateXHTML ( $title , $parts , &$parser ) {
189 global $wgLang , $wgUser ;
190 $skin = $wgUser->getSkin() ;
191 $ot = $title ; # Original title
192 if ( count ( explode ( ":" , $title ) ) == 1 )
193 $title = $wgLang->getNsText ( NS_TEMPLATE
) . ":" . $title ;
194 $nt = Title
::newFromText ( $title ) ;
195 $id = $nt->getArticleID() ;
196 if ( $id == 0 ) { # No/non-existing page
197 return $skin->makeBrokenLink ( $title , $ot ) ;
201 $tv = array () ; # Template variables
202 foreach ( $parts AS $part ) {
204 $x = explode ( "=" , $part , 2 ) ;
205 if ( count ( $x ) == 1 ) $key = "{$a}" ;
207 $value = array_pop ( $x ) ;
210 $art = new Article ( $nt ) ;
211 $text = $art->getContent ( false ) ;
212 $parser->plain_parse ( $text , true , $tv ) ;
218 * This function actually converts wikiXML into XHTML tags
220 function makeXHTML ( &$parser )
223 $n = $this->name
; # Shortcut
225 if ( $n == "EXTENSION" ) # Fix allowed HTML
228 $ext = strtoupper ( $this->attrs
["NAME"] ) ;
229 if ( $ext == "B" ||
$ext == "STRONG" ) $n = "BOLD" ;
230 else if ( $ext == "I" ||
$ext == "EM" ) $n = "ITALICS" ;
231 else if ( $ext == "U" ) $n = "UNDERLINED" ; # Hey, virtual wiki tag! ;-)
232 else if ( $ext == "S" ) $n = "STRIKE" ;
233 else if ( $ext == "P" ) $n = "PARAGRAPH" ;
234 else if ( $ext == "TABLE" ) $n = "TABLE" ;
235 else if ( $ext == "TR" ) $n = "TABLEROW" ;
236 else if ( $ext == "TD" ) $n = "TABLECELL" ;
237 else if ( $ext == "TH" ) $n = "TABLEHEAD" ;
238 else if ( $ext == "CAPTION" ) $n = "CAPTION" ;
239 else if ( $ext == "NOWIKI" ) $n = "NOWIKI" ;
240 if ( $n != $old_n ) unset ( $this->attrs
["NAME"] ) ; # Cleanup
241 else if ( $parser->nowiki
> 0 ) $n = "" ; # No "real" wiki tags allowed in nowiki section
244 if ( $n == "ARTICLE" )
245 $ret .= $this->sub_makeXHTML ( $parser ) ;
246 else if ( $n == "HEADING" )
247 $ret .= $this->sub_makeXHTML ( $parser , "h" . $this->attrs
["LEVEL"] ) ;
248 else if ( $n == "PARAGRAPH" )
249 $ret .= $this->sub_makeXHTML ( $parser , "p" ) ;
250 else if ( $n == "BOLD" )
251 $ret .= $this->sub_makeXHTML ( $parser , "strong" ) ;
252 else if ( $n == "ITALICS" )
253 $ret .= $this->sub_makeXHTML ( $parser , "em" ) ;
255 # These don't exist as wiki markup
256 else if ( $n == "UNDERLINED" )
257 $ret .= $this->sub_makeXHTML ( $parser , "u" ) ;
258 else if ( $n == "STRIKE" )
259 $ret .= $this->sub_makeXHTML ( $parser , "strike" ) ;
262 else if ( $n == "COMMENT" )
263 $ret .= "" ; # Comments are parsed out
266 else if ( $n == "LINK" )
267 $ret .= $this->makeInternalLink ( $parser ) ;
268 else if ( $n == "LINKTARGET" )
269 $ret .= $this->sub_makeXHTML ( $parser ) ;
270 else if ( $n == "LINKOPTION" )
271 $ret .= $this->sub_makeXHTML ( $parser ) ;
273 else if ( $n == "TEMPLATE" )
275 $parts = $this->sub_makeXHTML ( $parser ) ;
276 $parts = explode ( "|" , $parts ) ;
277 $title = array_shift ( $parts ) ;
278 $ret .= $this->getTemplateXHTML ( $title , $parts , &$parser ) ;
280 else if ( $n == "TEMPLATEVAR" )
282 $x = $this->sub_makeXHTML ( $parser ) ;
283 if ( isset ( $parser->mCurrentTemplateOptions
["{$x}"] ) )
284 $ret .= $parser->mCurrentTemplateOptions
["{$x}"] ;
287 else if ( $n == "IGNORE" ) # Internal use, not generated by wiki2xml parser
288 $ret .= $this->sub_makeXHTML ( $parser ) ;
290 else if ( $n == "NOWIKI" )
293 $ret .= $this->sub_makeXHTML ( $parser , "" ) ;
297 # Unknown HTML extension
298 else if ( $n == "EXTENSION" ) # This is currently a dummy!!!
300 $ext = $this->attrs
["NAME"] ;
302 $ret .= "<" . $ext . ">" ;
303 $ret .= $this->sub_makeXHTML ( $parser ) ;
304 $ret .= "</" . $ext . "> " ;
308 else if ( $n == "TABLE" )
310 $ret .= $this->sub_makeXHTML ( $parser , "table" ) ;
312 else if ( $n == "TABLEROW" )
314 $ret .= $this->sub_makeXHTML ( $parser , "tr" ) ;
316 else if ( $n == "TABLECELL" )
318 $ret .= $this->sub_makeXHTML ( $parser , "td" ) ;
320 else if ( $n == "TABLEHEAD" )
322 $ret .= $this->sub_makeXHTML ( $parser , "th" ) ;
324 else if ( $n == "CAPTION" )
326 $ret .= $this->sub_makeXHTML ( $parser , "caption" ) ;
329 else if ( $n == "ATTRS" ) # SPECIAL CASE : returning attributes
331 return $this->getTheseAttrs () ;
335 else if ( $n == "LISTITEM" )
337 if ( $parser->mListType
== "dl" ) $ret .= $this->sub_makeXHTML ( $parser , "dd" ) ;
338 else $ret .= $this->sub_makeXHTML ( $parser , "li" ) ;
340 else if ( $n == "LIST" )
342 $type = "ol" ; # Default
343 if ( $this->attrs
["TYPE"] == "bullet" ) $type = "ul" ;
344 else if ( $this->attrs
["TYPE"] == "indent" ) $type = "dl" ;
345 $oldtype = $parser->mListType
;
346 $parser->mListType
= $type ;
347 $ret .= $this->sub_makeXHTML ( $parser , $type ) ;
348 $parser->mListType
= $oldtype ;
351 # Something else entirely
354 $ret .= "<" . $n . ">" ;
355 $ret .= $this->sub_makeXHTML ( $parser ) ;
356 $ret .= "</" . $n . "> " ;
359 $ret = "\n{$ret}\n" ;
360 $ret = str_replace ( "\n\n" , "\n" , $ret ) ;
365 * A function for additional debugging output
369 $ret .= "<li> <b> Name: </b> $this->name </li>\n";
371 $ret .= '<li> <b> Attributes: </b>';
372 foreach ($this->attrs
as $name => $value) {
373 $ret .= "$name => $value; " ;
377 foreach ($this->children
as $child) {
378 if ( is_string($child) ) {
379 $ret .= "<li> $child </li>\n";
381 $ret .= $child->myPrint();
389 $ancStack = array(); // the stack with ancestral elements
391 // Three global functions needed for parsing, sorry guys
392 function wgXMLstartElement($parser, $name, $attrs) {
395 $newElem = new element
;
396 $newElem->name
= $name;
397 $newElem->attrs
= $attrs;
399 array_push($ancStack, $newElem);
402 function wgXMLendElement($parser, $name) {
403 global $ancStack, $rootElem;
404 // pop element off stack
405 $elem = array_pop ($ancStack);
406 if (count ($ancStack) == 0)
409 // add it to its parent
410 array_push ($ancStack[count($ancStack)-1]->children
, $elem);
413 function wgXMLcharacterData($parser, $data) {
415 $data = trim ($data); // Don't add blank lines, they're no use...
416 // add to parent if parent exists
417 if ( $ancStack && $data != "" ) {
418 array_push ($ancStack[count($ancStack)-1]->children
, $data);
424 * Here's the class that generates a nice tree
428 function &scanFile( $filename ) {
429 global $ancStack, $rootElem;
432 $xml_parser = xml_parser_create();
433 xml_set_element_handler ($xml_parser, 'wgXMLstartElement', 'wgXMLendElement');
434 xml_set_character_data_handler ($xml_parser, 'wgXMLcharacterData');
435 if (!($fp = fopen($filename, 'r'))) {
436 die('could not open XML input');
438 while ($data = fread($fp, 4096)) {
439 if (!xml_parse($xml_parser, $data, feof($fp))) {
440 die(sprintf("XML error: %s at line %d",
441 xml_error_string(xml_get_error_code($xml_parser)),
442 xml_get_current_line_number($xml_parser)));
445 xml_parser_free($xml_parser);
447 // return the remaining root element we copied in the beginning
451 function scanString ( $input ) {
452 global $ancStack, $rootElem;
455 $xml_parser = xml_parser_create();
456 xml_set_element_handler ($xml_parser, 'wgXMLstartElement', 'wgXMLendElement');
457 xml_set_character_data_handler ($xml_parser, 'wgXMLcharacterData');
459 if (!xml_parse ($xml_parser, $input, true)) {
460 die (sprintf ("XML error: %s at line %d",
461 xml_error_string(xml_get_error_code($xml_parser)),
462 xml_get_current_line_number($xml_parser)));
464 xml_parser_free ($xml_parser);
466 // return the remaining root element we copied in the beginning
472 class ParserXML
EXTENDS Parser
478 var $mTagHooks, $mListType;
480 # Cleared with clearState():
481 var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
482 var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
485 var $mOptions, $mTitle, $mOutputType,
486 $mTemplates, // cache of already loaded templates, avoids
487 // multiple SQL queries for the same string
488 $mTemplatePath; // stores an unsorted hash of all the templates already loaded
489 // in this path. Used for loop detection.
491 var $nowikicount , $mCurrentTemplateOptions ;
500 function ParserXML() {
501 $this->mTemplates
= array();
502 $this->mTemplatePath
= array();
503 $this->mTagHooks
= array();
512 function clearState() {
513 $this->mOutput
= new ParserOutput
;
514 $this->mAutonumber
= 0;
515 $this->mLastSection
= "";
516 $this->mDTopen
= false;
517 $this->mVariables
= false;
518 $this->mIncludeCount
= array();
519 $this->mStripState
= array();
520 $this->mArgStack
= array();
521 $this->mInPre
= false;
525 * Turns the wikitext into XML by calling the external parser
528 function html2xml ( &$text ) {
531 # generating html2xml command path
533 $a = explode ( "/" , $a ) ;
536 $html2xml = implode ( "/" , $a ) ;
539 $tmpfname = tempnam("/tmp", "FOO");
540 $handle = fopen($tmpfname, "w");
541 fwrite($handle, utf8_encode ( $text ) );
543 exec ( $html2xml . " < " . $tmpfname , $a ) ;
544 $text = utf8_decode ( implode ( "\n" , $a ) ) ;
548 function runXMLparser ( &$text ) {
551 $this->html2xml ( $text ) ;
553 $tmpfname = tempnam("/tmp", "FOO");
554 $handle = fopen($tmpfname, "w");
555 fwrite($handle, $text ) ;
557 exec ( $wgWiki2xml . " < " . $tmpfname , $a ) ;
558 $text = utf8_decode ( implode ( "\n" , $a ) ) ;
562 function plain_parse ( &$text , $inline = false , $templateOptions = array () ) {
563 $this->runXMLparser ( $text ) ;
566 $result = $w->scanString( $text );
568 $oldTemplateOptions = $this->mCurrentTemplateOptions
;
569 $this->mCurrentTemplateOptions
= $templateOptions ;
571 if ( $inline ) { # Inline rendering off for templates
572 if ( count ( $result->children
) == 1 )
573 $result->children
[0]->name
= "IGNORE" ;
576 if ( 1 ) $text = $result->makeXHTML ( $this ) ; # No debugging info
577 else $text = $result->makeXHTML ( $this ) . "<hr>" . $text . "<hr>" . $result->myPrint();
578 $this->mCurrentTemplateOptions
= $oldTemplateOptions ;
581 function parse( $text, &$title, $options, $linestart = true, $clearState = true ) {
582 $this->plain_parse ( $text ) ;
583 $this->mOutput
->setText ( $text ) ;
584 return $this->mOutput
;