Special:Export will now return *real* XML when using XML parser
[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 /**
149 * This function actually converts wikiXML into XHTML tags
150 */
151 function makeXHTML ( &$parser )
152 {
153 $ret = "" ;
154 $n = $this->name ; # Shortcut
155
156 if ( $n == "EXTENSION" ) # Fix allowed HTML
157 {
158 $old_n = $n ;
159 $ext = strtoupper ( $this->attrs["NAME"] ) ;
160 if ( $ext == "B" || $ext == "STRONG" ) $n = "BOLD" ;
161 else if ( $ext == "I" || $ext == "EM" ) $n = "ITALICS" ;
162 else if ( $ext == "U" ) $n = "UNDERLINED" ; # Hey, virtual wiki tag! ;-)
163 else if ( $ext == "S" ) $n = "STRIKE" ;
164 else if ( $ext == "P" ) $n = "PARAGRAPH" ;
165 else if ( $ext == "TABLE" ) $n = "TABLE" ;
166 else if ( $ext == "TR" ) $n = "TABLEROW" ;
167 else if ( $ext == "TD" ) $n = "TABLECELL" ;
168 else if ( $ext == "TH" ) $n = "TABLEHEAD" ;
169 else if ( $ext == "CAPTION" ) $n = "CAPTION" ;
170 else if ( $ext == "NOWIKI" ) $n = "NOWIKI" ;
171 if ( $n != $old_n ) unset ( $this->attrs["NAME"] ) ; # Cleanup
172 else if ( $parser->nowiki > 0 ) $n = "" ; # No "real" wiki tags allowed
173 }
174
175 if ( $n == "ARTICLE" )
176 $ret .= $this->sub_makeXHTML ( $parser ) ;
177 else if ( $n == "HEADING" )
178 $ret .= $this->sub_makeXHTML ( $parser , "h" . $this->attrs["LEVEL"] ) ;
179 else if ( $n == "PARAGRAPH" )
180 $ret .= $this->sub_makeXHTML ( $parser , "p" ) ;
181 else if ( $n == "BOLD" )
182 $ret .= $this->sub_makeXHTML ( $parser , "strong" ) ;
183 else if ( $n == "ITALICS" )
184 $ret .= $this->sub_makeXHTML ( $parser , "em" ) ;
185
186 # These don't exist as wiki markup
187 else if ( $n == "UNDERLINED" )
188 $ret .= $this->sub_makeXHTML ( $parser , "u" ) ;
189 else if ( $n == "STRIKE" )
190 $ret .= $this->sub_makeXHTML ( $parser , "strike" ) ;
191
192 # Links
193 else if ( $n == "LINK" )
194 $ret .= $this->makeInternalLink ( $parser ) ;
195 else if ( $n == "LINKTARGET" )
196 $ret .= $this->sub_makeXHTML ( $parser ) ;
197 else if ( $n == "LINKOPTION" )
198 $ret .= $this->sub_makeXHTML ( $parser ) ;
199
200 else if ( $n == "NOWIKI" )
201 {
202 $parser->nowiki++ ;
203 $ret .= $this->sub_makeXHTML ( $parser , "" ) ;
204 $parser->nowiki-- ;
205 }
206
207 # Unknown HTML extension
208 else if ( $n == "EXTENSION" ) # This is currently a dummy!!!
209 {
210 $ext = $this->attrs["NAME"] ;
211
212 $ret .= "&lt;" . $ext . "&gt;" ;
213 $ret .= $this->sub_makeXHTML ( $parser ) ;
214 $ret .= "&lt;/" . $ext . "&gt; " ;
215 }
216
217 # Table stuff
218 else if ( $n == "TABLE" )
219 {
220 $ret .= $this->sub_makeXHTML ( $parser , "table" ) ;
221 }
222 else if ( $n == "TABLEROW" )
223 {
224 $ret .= $this->sub_makeXHTML ( $parser , "tr" ) ;
225 }
226 else if ( $n == "TABLECELL" )
227 {
228 $ret .= $this->sub_makeXHTML ( $parser , "td" ) ;
229 }
230 else if ( $n == "TABLEHEAD" )
231 {
232 $ret .= $this->sub_makeXHTML ( $parser , "th" ) ;
233 }
234 else if ( $n == "CAPTION" )
235 {
236 $ret .= $this->sub_makeXHTML ( $parser , "caption" ) ;
237 }
238
239 else if ( $n == "ATTRS" ) # SPECIAL CASE : returning attributes
240 {
241 return $this->getTheseAttrs () ;
242 }
243
244 # Lists
245 else if ( $n == "LISTITEM" )
246 $ret .= $this->sub_makeXHTML ( $parser , "li" ) ;
247 else if ( $n == "LIST" )
248 {
249 $type = "ol" ; # Default
250 if ( $this->attrs["TYPE"] == "bullet" ) $type = "ul" ;
251 $ret .= $this->sub_makeXHTML ( $parser , $type ) ;
252 }
253
254 # Something else entirely
255 else
256 {
257 $ret .= "&lt;" . $n . "&gt;" ;
258 $ret .= $this->sub_makeXHTML ( $parser ) ;
259 $ret .= "&lt;/" . $n . "&gt; " ;
260 }
261
262 $ret = "\n{$ret}\n" ;
263 $ret = str_replace ( "\n\n" , "\n" , $ret ) ;
264 return $ret ;
265 }
266
267 /**
268 * A function for additional debugging output
269 */
270 function myPrint() {
271 $ret = "<ul>\n";
272 $ret .= "<li> <b> Name: </b> $this->name </li>\n";
273 // print attributes
274 $ret .= '<li> <b> Attributes: </b>';
275 foreach ($this->attrs as $name => $value) {
276 $ret .= "$name => $value; " ;
277 }
278 $ret .= " </li>\n";
279 // print children
280 foreach ($this->children as $child) {
281 if ( is_string($child) ) {
282 $ret .= "<li> $child </li>\n";
283 } else {
284 $ret .= $child->myPrint();
285 }
286 }
287 $ret .= "</ul>\n";
288 return $ret;
289 }
290 }
291
292 $ancStack = array(); // the stack with ancestral elements
293
294 // Three global functions needed for parsing, sorry guys
295 function wgXMLstartElement($parser, $name, $attrs) {
296 global $ancStack;
297
298 $newElem = new element;
299 $newElem->name = $name;
300 $newElem->attrs = $attrs;
301
302 array_push($ancStack, $newElem);
303 }
304
305 function wgXMLendElement($parser, $name) {
306 global $ancStack, $rootElem;
307 // pop element off stack
308 $elem = array_pop ($ancStack);
309 if (count ($ancStack) == 0)
310 $rootElem = $elem;
311 else
312 // add it to its parent
313 array_push ($ancStack[count($ancStack)-1]->children, $elem);
314 }
315
316 function wgXMLcharacterData($parser, $data) {
317 global $ancStack;
318 $data = trim ($data); // Don't add blank lines, they're no use...
319 // add to parent if parent exists
320 if ( $ancStack && $data != "" ) {
321 array_push ($ancStack[count($ancStack)-1]->children, $data);
322 }
323 }
324
325
326 /**
327 * Here's the class that generates a nice tree
328 */
329 class xml2php {
330
331 function &scanFile( $filename ) {
332 global $ancStack, $rootElem;
333 $ancStack = array();
334
335 $xml_parser = xml_parser_create();
336 xml_set_element_handler ($xml_parser, 'wgXMLstartElement', 'wgXMLendElement');
337 xml_set_character_data_handler ($xml_parser, 'wgXMLcharacterData');
338 if (!($fp = fopen($filename, 'r'))) {
339 die('could not open XML input');
340 }
341 while ($data = fread($fp, 4096)) {
342 if (!xml_parse($xml_parser, $data, feof($fp))) {
343 die(sprintf("XML error: %s at line %d",
344 xml_error_string(xml_get_error_code($xml_parser)),
345 xml_get_current_line_number($xml_parser)));
346 }
347 }
348 xml_parser_free($xml_parser);
349
350 // return the remaining root element we copied in the beginning
351 return $rootElem;
352 }
353
354 function scanString ( $input ) {
355 global $ancStack, $rootElem;
356 $ancStack = array();
357
358 $xml_parser = xml_parser_create();
359 xml_set_element_handler ($xml_parser, 'wgXMLstartElement', 'wgXMLendElement');
360 xml_set_character_data_handler ($xml_parser, 'wgXMLcharacterData');
361
362 if (!xml_parse ($xml_parser, $input, true)) {
363 die (sprintf ("XML error: %s at line %d",
364 xml_error_string(xml_get_error_code($xml_parser)),
365 xml_get_current_line_number($xml_parser)));
366 }
367 xml_parser_free ($xml_parser);
368
369 // return the remaining root element we copied in the beginning
370 return $rootElem;
371 }
372
373 }
374
375 /* Example code:
376
377 $w = new xml2php;
378 $filename = 'sample.xml';
379 $result = $w->scanFile( $filename );
380 print $result->myPrint();
381 */
382
383 $dummytext = "<article><heading level='2'> R-type </heading><paragraph><link><linktarget>image:a.jpg</linktarget><linkoption>1</linkoption><linkoption>2</linkoption><linkoption>3</linkoption><linkoption>text</linkoption></link></paragraph><paragraph>The <link><linktarget>video game</linktarget><linkoption>computer game</linkoption></link> <bold>R-type</bold> is <extension name='nowiki'>cool &amp; stuff</extension> because:</paragraph><list type='bullet'><listitem>it's nice</listitem><listitem>it's fast</listitem><listitem>it has:<list type='bullet'><listitem>graphics</listitem><listitem>sound</listitem></list></listitem></list><table><tablerow><tablecell>Version 1 </tablecell><tablecell>not bad</tablecell></tablerow><tablerow><tablecell>Version 2 </tablecell><tablecell>much better </tablecell></tablerow></table><paragraph>This is a || token in the middle of text.</paragraph></article>" ;
384
385 class ParserXML EXTENDS Parser
386 {
387 /**#@+
388 * @access private
389 */
390 # Persistent:
391 var $mTagHooks;
392
393 # Cleared with clearState():
394 var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
395 var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
396
397 # Temporary:
398 var $mOptions, $mTitle, $mOutputType,
399 $mTemplates, // cache of already loaded templates, avoids
400 // multiple SQL queries for the same string
401 $mTemplatePath; // stores an unsorted hash of all the templates already loaded
402 // in this path. Used for loop detection.
403
404 var $nowikicount ;
405
406 /**#@-*/
407
408 /**
409 * Constructor
410 *
411 * @access public
412 */
413 function ParserXML() {
414 $this->mTemplates = array();
415 $this->mTemplatePath = array();
416 $this->mTagHooks = array();
417 $this->clearState();
418 }
419
420 /**
421 * Clear Parser state
422 *
423 * @access private
424 */
425 function clearState() {
426 $this->mOutput = new ParserOutput;
427 $this->mAutonumber = 0;
428 $this->mLastSection = "";
429 $this->mDTopen = false;
430 $this->mVariables = false;
431 $this->mIncludeCount = array();
432 $this->mStripState = array();
433 $this->mArgStack = array();
434 $this->mInPre = false;
435 }
436
437 /**
438 * Turns the wikitext into XML by calling the external parser
439 *
440 */
441 function runXMLparser ( &$text ) {
442 global $wgWiki2xml ;
443
444 $tmpfname = tempnam("/tmp", "FOO");
445 $handle = fopen($tmpfname, "w");
446 fwrite($handle, $text);
447 fclose($handle);
448 exec ( $wgWiki2xml . " < " . $tmpfname , $a ) ;
449 $text = implode ( "\n" , $a ) ;
450 unlink($tmpfname);
451 }
452
453 function parse( $text, &$title, $options, $linestart = true, $clearState = true ) {
454 $this->runXMLparser ( $text ) ;
455 $nowikicount = 0 ;
456 $w = new xml2php;
457 $result = $w->scanString( $text );
458
459 if ( 1 ) $text = $result->makeXHTML ( $this ) ; # No debugging info
460 else $text = $result->makeXHTML ( $this ) . "<hr>" . $text . "<hr>" . $result->myPrint();
461
462 $this->mOutput->setText ( $text ) ;
463 return $this->mOutput;
464 }
465
466 }
467
468 ?>