5 * @subpackage Experimental
9 require_once ('Parser.php');
12 * This should one day become the XML->(X)HTML parser
13 * Based on work by Jan Hidders and Magnus Manske
15 * $wgUseXMLparser = true ;
16 * $wgEnableParserCache = false ;
17 * $wgWiki2xml to the path and executable of the command line version (cli)
18 * in LocalSettings.php
20 * @subpackage Experimental
24 * the base class for an element
26 * @subpackage Experimental
30 var $attrs = array ();
31 var $children = array ();
34 * This finds the ATTRS element and returns the ATTR sub-children as a single string
35 * @todo FIXME $parser always empty when calling makeXHTML()
37 function getSourceAttrs() {
39 foreach ($this->children
as $child) {
40 if (!is_string($child) AND $child->name
== 'ATTRS') {
41 $ret = $child->makeXHTML($parser);
48 * This collects the ATTR thingies for getSourceAttrs()
50 function getTheseAttrs() {
52 foreach ($this->children
as $child) {
53 if (!is_string($child) AND $child->name
== 'ATTR') {
54 $ret[] = $child->attrs
["NAME"]."='".$child->children
[0]."'";
57 return implode(' ', $ret);
60 function fixLinkTails(& $parser, $key) {
62 if (!isset ($this->children
[$k2]))
64 if (!is_string($this->children
[$k2]))
66 if (is_string($this->children
[$key]))
68 if ($this->children
[$key]->name
!= "LINK")
71 $n = $this->children
[$k2];
73 while ($n != '' AND (($n[0] >= 'a' AND $n[0] <= 'z') OR $n[0] == 'ä' OR $n[0] == 'ö' OR $n[0] == 'ü' OR $n[0] == 'ß')) {
77 $this->children
[$k2] = $n;
79 if (count($this->children
[$key]->children
) > 1) {
80 $kl = array_keys($this->children
[$key]->children
);
82 $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 = '') {
98 $attr2 = $this->getSourceAttrs();
99 if ($attr != '' AND $attr2 != '')
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 $child) {
115 if (is_string($child)) {
117 } elseif ($child->name
!= 'ATTRS') {
118 $ret .= $child->makeXHTML($parser);
122 $ret .= '</'.$tag.">\n";
129 function createInternalLink(& $parser, $target, $display_title, $options) {
131 $skin = $wgUser->getSkin();
132 $tp = explode(':', $target); # tp = target parts
133 $title = ''; # The plain title
134 $language = ''; # The language/meta/etc. part
135 $namespace = ''; # The namespace, if any
136 $subtarget = ''; # The '#' thingy
138 $nt = Title
:: newFromText($target);
139 $fl = strtoupper($this->attrs
['FORCEDLINK']) == 'YES';
141 if ($fl ||
count($tp) == 1) {
142 # Plain and simple case
145 # There's stuff missing here...
146 if ($nt->getNamespace() == NS_IMAGE
) {
147 $options[] = $display_title;
148 return $parser->makeImage($nt, implode('|', $options));
155 if ($language != '') {
156 # External link within the WikiMedia project
157 return "{language link}";
159 if ($namespace != '') {
160 # Link to another namespace, check for image/media stuff
161 return "{namespace link}";
163 return $skin->makeLink($target, $display_title);
168 /** @todo document */
169 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));
184 if (count($option) == 0)
185 $option[] = $target; # Create dummy display title
186 $display_title = array_pop($option);
187 return $this->createInternalLink($parser, $target, $display_title, $option);
190 /** @todo document */
191 function getTemplateXHTML($title, $parts, & $parser) {
192 global $wgLang, $wgUser;
193 $skin = $wgUser->getSkin();
194 $ot = $title; # Original title
195 if (count(explode(':', $title)) == 1)
196 $title = $wgLang->getNsText(NS_TEMPLATE
).":".$title;
197 $nt = Title
:: newFromText($title);
198 $id = $nt->getArticleID();
200 # No/non-existing page
201 return $skin->makeBrokenLink($title, $ot);
205 $tv = array (); # Template variables
206 foreach ($parts AS $part) {
208 $x = explode('=', $part, 2);
213 $value = array_pop($x);
216 $art = new Article($nt);
217 $text = $art->getContent(false);
218 $parser->plain_parse($text, true, $tv);
224 * This function actually converts wikiXML into XHTML tags
225 * @todo use switch() !
227 function makeXHTML(& $parser) {
229 $n = $this->name
; # Shortcut
231 if ($n == 'EXTENSION') {
234 $ext = strtoupper($this->attrs
['NAME']);
246 $n = 'UNDERLINED'; # Hey, virtual wiki tag! ;-)
274 unset ($this->attrs
['NAME']); # Cleanup
275 } elseif ($parser->nowiki
> 0) {
276 # No 'real' wiki tags allowed in nowiki section
279 } // $n = 'EXTENSION'
283 $ret .= $this->sub_makeXHTML($parser);
286 $ret .= $this->sub_makeXHTML($parser, 'h'.$this->attrs
['LEVEL']);
289 $ret .= $this->sub_makeXHTML($parser, 'p');
292 $ret .= $this->sub_makeXHTML($parser, 'strong');
295 $ret .= $this->sub_makeXHTML($parser, 'em');
298 # These don't exist as wiki markup
300 $ret .= $this->sub_makeXHTML($parser, 'u');
303 $ret .= $this->sub_makeXHTML($parser, 'strike');
308 # Comments are parsed out
315 $ret .= $this->makeInternalLink($parser);
319 $ret .= $this->sub_makeXHTML($parser);
323 $parts = $this->sub_makeXHTML($parser);
324 $parts = explode('|', $parts);
325 $title = array_shift($parts);
326 $ret .= $this->getTemplateXHTML($title, $parts, & $parser);
330 $x = $this->sub_makeXHTML($parser);
331 if (isset ($parser->mCurrentTemplateOptions
["{$x}"]))
332 $ret .= $parser->mCurrentTemplateOptions
["{$x}"];
335 # Internal use, not generated by wiki2xml parser
337 $ret .= $this->sub_makeXHTML($parser);
341 $ret .= $this->sub_makeXHTML($parser, '');
345 # Unknown HTML extension
346 case 'EXTENSION': # This is currently a dummy!!!
347 $ext = $this->attrs
['NAME'];
349 $ret .= '<'.$ext.'>';
350 $ret .= $this->sub_makeXHTML($parser);
351 $ret .= '</'.$ext.'> ';
358 $ret .= $this->sub_makeXHTML($parser, 'table');
361 $ret .= $this->sub_makeXHTML($parser, 'tr');
364 $ret .= $this->sub_makeXHTML($parser, 'td');
367 $ret .= $this->sub_makeXHTML($parser, 'th');
370 $ret .= $this->sub_makeXHTML($parser, 'caption');
372 case 'ATTRS': # SPECIAL CASE : returning attributes
373 return $this->getTheseAttrs();
378 if ($parser->mListType
== 'dl')
379 $ret .= $this->sub_makeXHTML($parser, 'dd');
381 $ret .= $this->sub_makeXHTML($parser, 'li');
384 $type = 'ol'; # Default
385 if ($this->attrs
['TYPE'] == 'bullet')
388 if ($this->attrs
['TYPE'] == 'indent')
390 $oldtype = $parser->mListType
;
391 $parser->mListType
= $type;
392 $ret .= $this->sub_makeXHTML($parser, $type);
393 $parser->mListType
= $oldtype;
396 # Something else entirely
398 $ret .= '<'.$n.'>';
399 $ret .= $this->sub_makeXHTML($parser);
400 $ret .= '</'.$n.'> ';
404 $ret = str_replace("\n\n", "\n", $ret);
409 * A function for additional debugging output
413 $ret .= "<li> <b> Name: </b> $this->name </li>\n";
415 $ret .= '<li> <b> Attributes: </b>';
416 foreach ($this->attrs
as $name => $value) {
417 $ret .= "$name => $value; ";
421 foreach ($this->children
as $child) {
422 if (is_string($child)) {
423 $ret .= "<li> $child </li>\n";
425 $ret .= $child->myPrint();
433 $ancStack = array (); // the stack with ancestral elements
435 // START Three global functions needed for parsing, sorry guys
436 /** @todo document */
437 function wgXMLstartElement($parser, $name, $attrs) {
440 $newElem = new element
;
441 $newElem->name
= $name;
442 $newElem->attrs
= $attrs;
444 array_push($ancStack, $newElem);
447 /** @todo document */
448 function wgXMLendElement($parser, $name) {
449 global $ancStack, $rootElem;
450 // pop element off stack
451 $elem = array_pop($ancStack);
452 if (count($ancStack) == 0)
455 // add it to its parent
456 array_push($ancStack[count($ancStack) - 1]->children
, $elem);
459 /** @todo document */
460 function wgXMLcharacterData($parser, $data) {
462 $data = trim($data); // Don't add blank lines, they're no use...
463 // add to parent if parent exists
464 if ($ancStack && $data != "") {
465 array_push($ancStack[count($ancStack) - 1]->children
, $data);
468 // END Three global functions needed for parsing, sorry guys
471 * Here's the class that generates a nice tree
473 * @subpackage Experimental
477 /** @todo document */
478 function & scanFile($filename) {
479 global $ancStack, $rootElem;
480 $ancStack = array ();
482 $xml_parser = xml_parser_create();
483 xml_set_element_handler($xml_parser, 'wgXMLstartElement', 'wgXMLendElement');
484 xml_set_character_data_handler($xml_parser, 'wgXMLcharacterData');
485 if (!($fp = fopen($filename, 'r'))) {
486 die('could not open XML input');
488 while ($data = fread($fp, 4096)) {
489 if (!xml_parse($xml_parser, $data, feof($fp))) {
490 die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
493 xml_parser_free($xml_parser);
495 // return the remaining root element we copied in the beginning
499 /** @todo document */
500 function scanString($input) {
501 global $ancStack, $rootElem;
502 $ancStack = array ();
504 $xml_parser = xml_parser_create();
505 xml_set_element_handler($xml_parser, 'wgXMLstartElement', 'wgXMLendElement');
506 xml_set_character_data_handler($xml_parser, 'wgXMLcharacterData');
508 if (!xml_parse($xml_parser, $input, true)) {
509 die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
511 xml_parser_free($xml_parser);
513 // return the remaining root element we copied in the beginning
522 * @subpackage Experimental
524 class ParserXML
extends Parser
{
529 var $mTagHooks, $mListType;
531 # Cleared with clearState():
532 var $mOutput, $mAutonumber, $mDTopen, $mStripState = array ();
533 var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
536 var $mOptions, $mTitle, $mOutputType, $mTemplates, // cache of already loaded templates, avoids
537 // multiple SQL queries for the same string
538 $mTemplatePath; // stores an unsorted hash of all the templates already loaded
539 // in this path. Used for loop detection.
541 var $nowikicount, $mCurrentTemplateOptions;
550 function ParserXML() {
551 $this->mTemplates
= array ();
552 $this->mTemplatePath
= array ();
553 $this->mTagHooks
= array ();
562 function clearState() {
563 $this->mOutput
= new ParserOutput
;
564 $this->mAutonumber
= 0;
565 $this->mLastSection
= "";
566 $this->mDTopen
= false;
567 $this->mVariables
= false;
568 $this->mIncludeCount
= array ();
569 $this->mStripState
= array ();
570 $this->mArgStack
= array ();
571 $this->mInPre
= false;
575 * Turns the wikitext into XML by calling the external parser
578 function html2xml(& $text) {
581 # generating html2xml command path
583 $a = explode('/', $a);
586 $html2xml = implode('/', $a);
589 $tmpfname = tempnam( wfTempDir(), 'FOO' );
590 $handle = fopen($tmpfname, 'w');
591 fwrite($handle, utf8_encode($text));
593 exec($html2xml.' < '.$tmpfname, $a);
594 $text = utf8_decode(implode("\n", $a));
598 /** @todo document */
599 function runXMLparser(& $text) {
602 $this->html2xml($text);
604 $tmpfname = tempnam( wfTempDir(), 'FOO');
605 $handle = fopen($tmpfname, 'w');
606 fwrite($handle, $text);
608 exec($wgWiki2xml.' < '.$tmpfname, $a);
609 $text = utf8_decode(implode("\n", $a));
613 /** @todo document */
614 function plain_parse(& $text, $inline = false, $templateOptions = array ()) {
615 $this->runXMLparser($text);
618 $result = $w->scanString($text);
620 $oldTemplateOptions = $this->mCurrentTemplateOptions
;
621 $this->mCurrentTemplateOptions
= $templateOptions;
623 if ($inline) { # Inline rendering off for templates
624 if (count($result->children
) == 1)
625 $result->children
[0]->name
= 'IGNORE';
629 $text = $result->makeXHTML($this); # No debugging info
631 $text = $result->makeXHTML($this).'<hr>'.$text.'<hr>'.$result->myPrint();
632 $this->mCurrentTemplateOptions
= $oldTemplateOptions;
635 /** @todo document */
636 function parse($text, & $title, $options, $linestart = true, $clearState = true) {
637 $this->plain_parse($text);
638 $this->mOutput
->setText($text);
639 return $this->mOutput
;