83f4c0990406a58a70ff0d6625366401b0dc32ba
[lhc/web/wiklou.git] / includes / ParserXML.php
1 <?php
2 require_once ( "Parser.php" ) ;
3
4 /**
5 * This should one day become the XML->(X)HTML parser
6 * Based on work by Jan Hidders and Magnus Manske
7 * To use, set
8 * $wgUseXMLparser = true ;
9 * $wgEnableParserCache = false ;
10 * $wgWiki2xml to the path and executable of the command line version (cli)
11 * in LocalSettings.php
12 * @package MediaWiki
13 * @subpackage Experimental
14 */
15
16 /**
17 * the base class for an element
18 */
19 class element {
20 var $name = '';
21 var $attrs = array();
22 var $children = array();
23
24 /**
25 * This finds the ATTRS element and returns the ATTR sub-children as a single string
26 */
27 function getSourceAttrs ()
28 {
29 $ret = "" ;
30 foreach ($this->children as $child)
31 {
32 if ( !is_string($child) AND $child->name == "ATTRS" )
33 {
34 $ret = $child->makeXHTML ( $parser );
35 }
36 }
37 return $ret ;
38 }
39
40 /**
41 * This collects the ATTR thingies for getSourceAttrs()
42 */
43 function getTheseAttrs ()
44 {
45 $ret = array() ;
46 foreach ($this->children as $child)
47 {
48 if ( !is_string($child) AND $child->name == "ATTR" )
49 {
50 $ret[] = $child->attrs["NAME"] . "='" . $child->children[0] . "'" ;
51 }
52 }
53 return implode ( " " , $ret ) ;
54 }
55
56 /**
57 * This function generates the XHTML for the entire subtree
58 */
59 function sub_makeXHTML ( &$parser , $tag = "" , $attr = "" )
60 {
61 $ret = "" ;
62
63 $attr2 = $this->getSourceAttrs () ;
64 if ( $attr != "" AND $attr2 != "" ) $attr .= " " ;
65 $attr .= $attr2 ;
66
67 if ( $tag != "" )
68 {
69 $ret .= "<" . $tag ;
70 if ( $attr != "" ) $ret .= " " . $attr ;
71 $ret .= ">" ;
72 }
73
74 foreach ($this->children as $child) {
75 if ( is_string($child) ) {
76 $ret .= $child ;
77 } else if ( $child->name != "ATTRS" ) {
78 $ret .= $child->makeXHTML ( $parser );
79 }
80 }
81 if ( $tag != "" )
82 $ret .= "</" . $tag . ">\n" ;
83 return $ret ;
84 }
85
86 /**
87 * Link functions
88 */
89 function createInternalLink ( &$parser , $target , $display_title , $options )
90 {
91 global $wgUser ;
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
98
99
100 $nt = Title::newFromText ( $target ) ;
101 $fl = strtoupper ( $this->attrs["FORCEDLINK"] ) == "YES" ;
102
103 if ( $fl || count ( $tp ) == 1 ) $title = $target ; # Plain and simple case
104 else # There's stuff missing here...
105 {
106 if ( $nt->getNamespace() == NS_IMAGE )
107 {
108 $options[] = $display_title ;
109 return $skin->makeImageLinkObj ( $nt , implode ( "|" , $options ) ) ;
110 }
111 else $title = $target ; # Default
112 }
113
114 if ( $language != "" ) # External link within the WikiMedia project
115 {
116 return "{language link}" ;
117 }
118 else if ( $namespace != "" ) # Link to another namespace, check for image/media stuff
119 {
120 return "{namespace link}" ;
121 }
122 else
123 {
124 return $skin->makeLink ( $target , $display_title ) ;
125 }
126 }
127
128 function makeInternalLink ( &$parser )
129 {
130 $target = "" ;
131 $option = array () ;
132 foreach ($this->children as $child) {
133 if ( is_string($child) ) {
134 # This shouldn't be the case!
135 } else {
136 if ( $child->name == "LINKTARGET" )
137 $target = trim ( $child->makeXHTML ( $parser ) ) ;
138 else
139 $option[] = trim ( $child->makeXHTML ( $parser ) ) ;
140 }
141 }
142
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 ) ;
146 }
147
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 ) ;
158 }
159
160 $a = 0 ;
161 $tv = array () ; # Template variables
162 foreach ( $parts AS $part ) {
163 $a++ ;
164 $x = explode ( "=" , $part , 2 ) ;
165 if ( count ( $x ) == 1 ) $key = "{$a}" ;
166 else $key = $x[0] ;
167 $value = array_pop ( $x ) ;
168 $tv[$key] = $value ;
169 }
170 $art = new Article ( $nt ) ;
171 $text = $art->getContent ( false ) ;
172 $parser->plain_parse ( $text , true , $tv ) ;
173
174 return $text ;
175 }
176
177 /**
178 * This function actually converts wikiXML into XHTML tags
179 */
180 function makeXHTML ( &$parser )
181 {
182 $ret = "" ;
183 $n = $this->name ; # Shortcut
184
185 if ( $n == "EXTENSION" ) # Fix allowed HTML
186 {
187 $old_n = $n ;
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
202 }
203
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" ) ;
214
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" ) ;
220
221 # Links
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 ) ;
228
229 else if ( $n == "TEMPLATE" )
230 {
231 $parts = $this->sub_makeXHTML ( $parser ) ;
232 $parts = explode ( "|" , $parts ) ;
233 $title = array_shift ( $parts ) ;
234 $ret .= $this->getTemplateXHTML ( $title , $parts , &$parser ) ;
235 }
236 else if ( $n == "TEMPLATEVAR" )
237 {
238 $x = $this->sub_makeXHTML ( $parser ) ;
239 if ( isset ( $parser->mCurrentTemplateOptions["{$x}"] ) )
240 $ret .= $parser->mCurrentTemplateOptions["{$x}"] ;
241 }
242
243 else if ( $n == "IGNORE" ) # Internal use, not generated by wiki2xml parser
244 $ret .= $this->sub_makeXHTML ( $parser ) ;
245
246 else if ( $n == "NOWIKI" )
247 {
248 $parser->nowiki++ ;
249 $ret .= $this->sub_makeXHTML ( $parser , "" ) ;
250 $parser->nowiki-- ;
251 }
252
253 # Unknown HTML extension
254 else if ( $n == "EXTENSION" ) # This is currently a dummy!!!
255 {
256 $ext = $this->attrs["NAME"] ;
257
258 $ret .= "&lt;" . $ext . "&gt;" ;
259 $ret .= $this->sub_makeXHTML ( $parser ) ;
260 $ret .= "&lt;/" . $ext . "&gt; " ;
261 }
262
263 # Table stuff
264 else if ( $n == "TABLE" )
265 {
266 $ret .= $this->sub_makeXHTML ( $parser , "table" ) ;
267 }
268 else if ( $n == "TABLEROW" )
269 {
270 $ret .= $this->sub_makeXHTML ( $parser , "tr" ) ;
271 }
272 else if ( $n == "TABLECELL" )
273 {
274 $ret .= $this->sub_makeXHTML ( $parser , "td" ) ;
275 }
276 else if ( $n == "TABLEHEAD" )
277 {
278 $ret .= $this->sub_makeXHTML ( $parser , "th" ) ;
279 }
280 else if ( $n == "CAPTION" )
281 {
282 $ret .= $this->sub_makeXHTML ( $parser , "caption" ) ;
283 }
284
285 else if ( $n == "ATTRS" ) # SPECIAL CASE : returning attributes
286 {
287 return $this->getTheseAttrs () ;
288 }
289
290 # Lists
291 else if ( $n == "LISTITEM" )
292 $ret .= $this->sub_makeXHTML ( $parser , "li" ) ;
293 else if ( $n == "LIST" )
294 {
295 $type = "ol" ; # Default
296 if ( $this->attrs["TYPE"] == "bullet" ) $type = "ul" ;
297 $ret .= $this->sub_makeXHTML ( $parser , $type ) ;
298 }
299
300 # Something else entirely
301 else
302 {
303 $ret .= "&lt;" . $n . "&gt;" ;
304 $ret .= $this->sub_makeXHTML ( $parser ) ;
305 $ret .= "&lt;/" . $n . "&gt; " ;
306 }
307
308 $ret = "\n{$ret}\n" ;
309 $ret = str_replace ( "\n\n" , "\n" , $ret ) ;
310 return $ret ;
311 }
312
313 /**
314 * A function for additional debugging output
315 */
316 function myPrint() {
317 $ret = "<ul>\n";
318 $ret .= "<li> <b> Name: </b> $this->name </li>\n";
319 // print attributes
320 $ret .= '<li> <b> Attributes: </b>';
321 foreach ($this->attrs as $name => $value) {
322 $ret .= "$name => $value; " ;
323 }
324 $ret .= " </li>\n";
325 // print children
326 foreach ($this->children as $child) {
327 if ( is_string($child) ) {
328 $ret .= "<li> $child </li>\n";
329 } else {
330 $ret .= $child->myPrint();
331 }
332 }
333 $ret .= "</ul>\n";
334 return $ret;
335 }
336 }
337
338 $ancStack = array(); // the stack with ancestral elements
339
340 // Three global functions needed for parsing, sorry guys
341 function wgXMLstartElement($parser, $name, $attrs) {
342 global $ancStack;
343
344 $newElem = new element;
345 $newElem->name = $name;
346 $newElem->attrs = $attrs;
347
348 array_push($ancStack, $newElem);
349 }
350
351 function wgXMLendElement($parser, $name) {
352 global $ancStack, $rootElem;
353 // pop element off stack
354 $elem = array_pop ($ancStack);
355 if (count ($ancStack) == 0)
356 $rootElem = $elem;
357 else
358 // add it to its parent
359 array_push ($ancStack[count($ancStack)-1]->children, $elem);
360 }
361
362 function wgXMLcharacterData($parser, $data) {
363 global $ancStack;
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);
368 }
369 }
370
371
372 /**
373 * Here's the class that generates a nice tree
374 */
375 class xml2php {
376
377 function &scanFile( $filename ) {
378 global $ancStack, $rootElem;
379 $ancStack = array();
380
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');
386 }
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)));
392 }
393 }
394 xml_parser_free($xml_parser);
395
396 // return the remaining root element we copied in the beginning
397 return $rootElem;
398 }
399
400 function scanString ( $input ) {
401 global $ancStack, $rootElem;
402 $ancStack = array();
403
404 $xml_parser = xml_parser_create();
405 xml_set_element_handler ($xml_parser, 'wgXMLstartElement', 'wgXMLendElement');
406 xml_set_character_data_handler ($xml_parser, 'wgXMLcharacterData');
407
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)));
412 }
413 xml_parser_free ($xml_parser);
414
415 // return the remaining root element we copied in the beginning
416 return $rootElem;
417 }
418
419 }
420
421 class ParserXML EXTENDS Parser
422 {
423 /**#@+
424 * @access private
425 */
426 # Persistent:
427 var $mTagHooks;
428
429 # Cleared with clearState():
430 var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
431 var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
432
433 # Temporary:
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.
439
440 var $nowikicount , $mCurrentTemplateOptions ;
441
442 /**#@-*/
443
444 /**
445 * Constructor
446 *
447 * @access public
448 */
449 function ParserXML() {
450 $this->mTemplates = array();
451 $this->mTemplatePath = array();
452 $this->mTagHooks = array();
453 $this->clearState();
454 }
455
456 /**
457 * Clear Parser state
458 *
459 * @access private
460 */
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;
471 }
472
473 /**
474 * Turns the wikitext into XML by calling the external parser
475 *
476 */
477 function runXMLparser ( &$text ) {
478 global $wgWiki2xml ;
479
480 $tmpfname = tempnam("/tmp", "FOO");
481 $handle = fopen($tmpfname, "w");
482 fwrite($handle, $text);
483 fclose($handle);
484 exec ( $wgWiki2xml . " < " . $tmpfname , $a ) ;
485 $text = implode ( "\n" , $a ) ;
486 unlink($tmpfname);
487 }
488
489 function plain_parse ( &$text , $inline = false , $templateOptions = array () ) {
490 $this->runXMLparser ( $text ) ;
491 $nowikicount = 0 ;
492 $w = new xml2php;
493 $result = $w->scanString( $text );
494
495 $oldTemplateOptions = $this->mCurrentTemplateOptions ;
496 $this->mCurrentTemplateOptions = $templateOptions ;
497
498 if ( $inline ) { # Inline rendering off for templates
499 if ( count ( $result->children ) == 1 )
500 $result->children[0]->name = "IGNORE" ;
501 }
502
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 ;
506 }
507
508 function parse( $text, &$title, $options, $linestart = true, $clearState = true ) {
509 $this->plain_parse ( $text ) ;
510 $this->mOutput->setText ( $text ) ;
511 return $this->mOutput;
512 }
513
514 }
515
516 ?>