83f4c0990406a58a70ff0d6625366401b0dc32ba
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
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 == "LINK" )
223 $ret .= $this->makeInternalLink ( $parser ) ;
224 else if ( $n == "LINKTARGET" )
225 $ret .= $this->sub_makeXHTML ( $parser ) ;
226 else if ( $n == "LINKOPTION" )
227 $ret .= $this->sub_makeXHTML ( $parser ) ;
229 else if ( $n == "TEMPLATE" )
231 $parts = $this->sub_makeXHTML ( $parser ) ;
232 $parts = explode ( "|" , $parts ) ;
233 $title = array_shift ( $parts ) ;
234 $ret .= $this->getTemplateXHTML ( $title , $parts , &$parser ) ;
236 else if ( $n == "TEMPLATEVAR" )
238 $x = $this->sub_makeXHTML ( $parser ) ;
239 if ( isset ( $parser->mCurrentTemplateOptions
["{$x}"] ) )
240 $ret .= $parser->mCurrentTemplateOptions
["{$x}"] ;
243 else if ( $n == "IGNORE" ) # Internal use, not generated by wiki2xml parser
244 $ret .= $this->sub_makeXHTML ( $parser ) ;
246 else if ( $n == "NOWIKI" )
249 $ret .= $this->sub_makeXHTML ( $parser , "" ) ;
253 # Unknown HTML extension
254 else if ( $n == "EXTENSION" ) # This is currently a dummy!!!
256 $ext = $this->attrs
["NAME"] ;
258 $ret .= "<" . $ext . ">" ;
259 $ret .= $this->sub_makeXHTML ( $parser ) ;
260 $ret .= "</" . $ext . "> " ;
264 else if ( $n == "TABLE" )
266 $ret .= $this->sub_makeXHTML ( $parser , "table" ) ;
268 else if ( $n == "TABLEROW" )
270 $ret .= $this->sub_makeXHTML ( $parser , "tr" ) ;
272 else if ( $n == "TABLECELL" )
274 $ret .= $this->sub_makeXHTML ( $parser , "td" ) ;
276 else if ( $n == "TABLEHEAD" )
278 $ret .= $this->sub_makeXHTML ( $parser , "th" ) ;
280 else if ( $n == "CAPTION" )
282 $ret .= $this->sub_makeXHTML ( $parser , "caption" ) ;
285 else if ( $n == "ATTRS" ) # SPECIAL CASE : returning attributes
287 return $this->getTheseAttrs () ;
291 else if ( $n == "LISTITEM" )
292 $ret .= $this->sub_makeXHTML ( $parser , "li" ) ;
293 else if ( $n == "LIST" )
295 $type = "ol" ; # Default
296 if ( $this->attrs
["TYPE"] == "bullet" ) $type = "ul" ;
297 $ret .= $this->sub_makeXHTML ( $parser , $type ) ;
300 # Something else entirely
303 $ret .= "<" . $n . ">" ;
304 $ret .= $this->sub_makeXHTML ( $parser ) ;
305 $ret .= "</" . $n . "> " ;
308 $ret = "\n{$ret}\n" ;
309 $ret = str_replace ( "\n\n" , "\n" , $ret ) ;
314 * A function for additional debugging output
318 $ret .= "<li> <b> Name: </b> $this->name </li>\n";
320 $ret .= '<li> <b> Attributes: </b>';
321 foreach ($this->attrs
as $name => $value) {
322 $ret .= "$name => $value; " ;
326 foreach ($this->children
as $child) {
327 if ( is_string($child) ) {
328 $ret .= "<li> $child </li>\n";
330 $ret .= $child->myPrint();
338 $ancStack = array(); // the stack with ancestral elements
340 // Three global functions needed for parsing, sorry guys
341 function wgXMLstartElement($parser, $name, $attrs) {
344 $newElem = new element
;
345 $newElem->name
= $name;
346 $newElem->attrs
= $attrs;
348 array_push($ancStack, $newElem);
351 function wgXMLendElement($parser, $name) {
352 global $ancStack, $rootElem;
353 // pop element off stack
354 $elem = array_pop ($ancStack);
355 if (count ($ancStack) == 0)
358 // add it to its parent
359 array_push ($ancStack[count($ancStack)-1]->children
, $elem);
362 function wgXMLcharacterData($parser, $data) {
364 $data = trim ($data); // Don't add blank lines, they're no use...
365 // add to parent if parent exists
366 if ( $ancStack && $data != "" ) {
367 array_push ($ancStack[count($ancStack)-1]->children
, $data);
373 * Here's the class that generates a nice tree
377 function &scanFile( $filename ) {
378 global $ancStack, $rootElem;
381 $xml_parser = xml_parser_create();
382 xml_set_element_handler ($xml_parser, 'wgXMLstartElement', 'wgXMLendElement');
383 xml_set_character_data_handler ($xml_parser, 'wgXMLcharacterData');
384 if (!($fp = fopen($filename, 'r'))) {
385 die('could not open XML input');
387 while ($data = fread($fp, 4096)) {
388 if (!xml_parse($xml_parser, $data, feof($fp))) {
389 die(sprintf("XML error: %s at line %d",
390 xml_error_string(xml_get_error_code($xml_parser)),
391 xml_get_current_line_number($xml_parser)));
394 xml_parser_free($xml_parser);
396 // return the remaining root element we copied in the beginning
400 function scanString ( $input ) {
401 global $ancStack, $rootElem;
404 $xml_parser = xml_parser_create();
405 xml_set_element_handler ($xml_parser, 'wgXMLstartElement', 'wgXMLendElement');
406 xml_set_character_data_handler ($xml_parser, 'wgXMLcharacterData');
408 if (!xml_parse ($xml_parser, $input, true)) {
409 die (sprintf ("XML error: %s at line %d",
410 xml_error_string(xml_get_error_code($xml_parser)),
411 xml_get_current_line_number($xml_parser)));
413 xml_parser_free ($xml_parser);
415 // return the remaining root element we copied in the beginning
421 class ParserXML
EXTENDS Parser
429 # Cleared with clearState():
430 var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
431 var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
434 var $mOptions, $mTitle, $mOutputType,
435 $mTemplates, // cache of already loaded templates, avoids
436 // multiple SQL queries for the same string
437 $mTemplatePath; // stores an unsorted hash of all the templates already loaded
438 // in this path. Used for loop detection.
440 var $nowikicount , $mCurrentTemplateOptions ;
449 function ParserXML() {
450 $this->mTemplates
= array();
451 $this->mTemplatePath
= array();
452 $this->mTagHooks
= array();
461 function clearState() {
462 $this->mOutput
= new ParserOutput
;
463 $this->mAutonumber
= 0;
464 $this->mLastSection
= "";
465 $this->mDTopen
= false;
466 $this->mVariables
= false;
467 $this->mIncludeCount
= array();
468 $this->mStripState
= array();
469 $this->mArgStack
= array();
470 $this->mInPre
= false;
474 * Turns the wikitext into XML by calling the external parser
477 function runXMLparser ( &$text ) {
480 $tmpfname = tempnam("/tmp", "FOO");
481 $handle = fopen($tmpfname, "w");
482 fwrite($handle, $text);
484 exec ( $wgWiki2xml . " < " . $tmpfname , $a ) ;
485 $text = implode ( "\n" , $a ) ;
489 function plain_parse ( &$text , $inline = false , $templateOptions = array () ) {
490 $this->runXMLparser ( $text ) ;
493 $result = $w->scanString( $text );
495 $oldTemplateOptions = $this->mCurrentTemplateOptions
;
496 $this->mCurrentTemplateOptions
= $templateOptions ;
498 if ( $inline ) { # Inline rendering off for templates
499 if ( count ( $result->children
) == 1 )
500 $result->children
[0]->name
= "IGNORE" ;
503 if ( 1 ) $text = $result->makeXHTML ( $this ) ; # No debugging info
504 else $text = $result->makeXHTML ( $this ) . "<hr>" . $text . "<hr>" . $result->myPrint();
505 $this->mCurrentTemplateOptions
= $oldTemplateOptions ;
508 function parse( $text, &$title, $options, $linestart = true, $clearState = true ) {
509 $this->plain_parse ( $text ) ;
510 $this->mOutput
->setText ( $text ) ;
511 return $this->mOutput
;