Fix http://bugzilla.wikipedia.org/show_bug.cgi?id=511 . Stop showing whatlinkshere...
[mediawiki.git] / includes / ParserXML.php
blobe2f1770b55477f92926418edf6a07dad70d990d6
1 <?
2 /**
3 * This should one day become the XML->(X)HTML parser
4 * Based on work by Jan Hidders and Magnus Manske
5 * @package MediaWiki
6 */
8 /**
9 * the base class for an element
10 * @package MediaWiki
12 class element {
13 var $name = '';
14 var $attrs = array();
15 var $children = array();
17 function myPrint() {
18 $ret = "<ul>\n";
19 $ret .= "<li> <b> Name: </b> $this->name </li>\n";
20 // print attributes
21 $ret .= '<li> <b> Attributes: </b>';
22 foreach ($this->attrs as $name => $value) {
23 $ret .= "$name => $value; " ;
25 $ret .= " </li>\n";
26 // print children
27 foreach ($this->children as $child) {
28 if ( is_string($child) ) {
29 $ret .= "<li> $child </li>\n";
30 } else {
31 $ret .= $child->myPrint();
34 $ret .= "</ul>\n";
35 return $ret;
39 $ancStack = array(); // the stack with ancestral elements
41 // Three global functions needed for parsing, sorry guys
42 function wgXMLstartElement($parser, $name, $attrs) {
43 global $ancStack;
45 $newElem = new element;
46 $newElem->name = $name;
47 $newElem->attrs = $attrs;
49 array_push($ancStack, $newElem);
52 function wgXMLendElement($parser, $name) {
53 global $ancStack, $rootElem;
54 // pop element off stack
55 $elem = array_pop ($ancStack);
56 if (count ($ancStack) == 0)
57 $rootElem = $elem;
58 else
59 // add it to its parent
60 array_push ($ancStack[count($ancStack)-1]->children, $elem);
63 function wgXMLcharacterData($parser, $data) {
64 global $ancStack;
65 $data = trim ($data); // Don't add blank lines, they're no use...
66 // add to parent if parent exists
67 if ( $ancStack && $data != "" ) {
68 array_push ($ancStack[count($ancStack)-1]->children, $data);
73 /**
74 * Here's the class that generates a nice tree
75 * package parserxml
76 * @package MediaWiki
78 class xml2php {
80 function &scanFile( $filename ) {
81 global $ancStack, $rootElem;
82 $ancStack = array();
84 $xml_parser = xml_parser_create();
85 xml_set_element_handler ($xml_parser, 'wgXMLstartElement', 'wgXMLendElement');
86 xml_set_character_data_handler ($xml_parser, 'wgXMLcharacterData');
87 if (!($fp = fopen($filename, 'r'))) {
88 die('could not open XML input');
90 while ($data = fread($fp, 4096)) {
91 if (!xml_parse($xml_parser, $data, feof($fp))) {
92 die(sprintf("XML error: %s at line %d",
93 xml_error_string(xml_get_error_code($xml_parser)),
94 xml_get_current_line_number($xml_parser)));
97 xml_parser_free($xml_parser);
99 // return the remaining root element we copied in the beginning
100 return $rootElem;
103 function scanString ( $input ) {
104 global $ancStack, $rootElem;
105 $ancStack = array();
107 $xml_parser = xml_parser_create();
108 xml_set_element_handler ($xml_parser, 'wgXMLstartElement', 'wgXMLendElement');
109 xml_set_character_data_handler ($xml_parser, 'wgXMLcharacterData');
111 if (!xml_parse ($xml_parser, $input, true)) {
112 die (sprintf ("XML error: %s at line %d",
113 xml_error_string(xml_get_error_code($xml_parser)),
114 xml_get_current_line_number($xml_parser)));
116 xml_parser_free ($xml_parser);
118 // return the remaining root element we copied in the beginning
119 return $rootElem;
124 /* Example code:
126 $w = new xml2php;
127 $filename = 'sample.xml';
128 $result = $w->scanFile( $filename );
129 print $result->myPrint();