r5291 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r5290‎ | r5291 | r5292 >
Date:14:43, 18 September 2004
Author:timwi
Status:old
Tags:
Comment:
This didn't seem to work because of some sort of change to PHP's handling of
references to objects or something. I fixed it by adding elements to their parent
when they are closed, not when they are opened.

Also fixed indentation and stuff like that.
myPrint() now returns string instead of just printing everything.
Modified paths:
  • /trunk/phase3/includes/ParserXML.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/ParserXML.php
@@ -10,67 +10,63 @@
1111 * @package MediaWiki
1212 */
1313 class element {
14 - var $name = '';
15 - var $attrs = array();
16 - var $children = array();
17 -
18 - function myPrint() {
19 - echo '<UL>';
20 - echo "<LI> <B> Name: </B> $this->name";
21 - // print attributes
22 - echo '<LI> <B> Attributes: </B>';
23 - foreach ($this->attrs as $name => $value) {
24 - echo "$name => $value; " ;
 14+ var $name = '';
 15+ var $attrs = array();
 16+ var $children = array();
 17+
 18+ function myPrint() {
 19+ $ret = "<ul>\n";
 20+ $ret .= "<li> <b> Name: </b> $this->name </li>\n";
 21+ // print attributes
 22+ $ret .= '<li> <b> Attributes: </b>';
 23+ foreach ($this->attrs as $name => $value) {
 24+ $ret .= "$name => $value; " ;
 25+ }
 26+ $ret .= " </li>\n";
 27+ // print children
 28+ foreach ($this->children as $child) {
 29+ if ( is_string($child) ) {
 30+ $ret .= "<li> $child </li>\n";
 31+ } else {
 32+ $ret .= $child->myPrint();
 33+ }
 34+ }
 35+ $ret .= "</ul>\n";
 36+ return $ret;
2537 }
26 - // print children
27 - foreach ($this->children as $child) {
28 - if ( is_string($child) ) {
29 - echo '<LI> '.$child;
30 - } else {
31 - $child->myPrint();
32 - }
33 - }
34 - echo '</UL>';
35 - }
36 -
3738 }
3839
3940 $ancStack = array(); // the stack with ancestral elements
4041
4142 // Three global functions needed for parsing, sorry guys
4243 function wgXMLstartElement($parser, $name, $attrs) {
43 - global $ancStack, $rootElem;
 44+ global $ancStack;
4445
45 - $newElem = new element;
46 - $newElem->name = $name;
47 - $newElem->attrs = $attrs;
48 - array_push($ancStack, $newElem);
49 - // add to parent if parent exists
50 - $nrAncs = count($ancStack)-1;
51 - if ( $nrAncs > 0 ) {
52 - array_push($ancStack[$nrAncs-1]->children, &$ancStack[$nrAncs]);
53 - } else {
54 - // make extra copy of root element and alias it with the original
55 - array_push($ancStack, &$ancStack[0]);
56 - }
 46+ $newElem = new element;
 47+ $newElem->name = $name;
 48+ $newElem->attrs = $attrs;
 49+
 50+ array_push($ancStack, $newElem);
5751 }
5852
5953 function wgXMLendElement($parser, $name) {
60 - global $ancStack, $rootElem;
61 -
62 - // pop element of stack
63 - array_pop($ancStack);
 54+ global $ancStack, $rootElem;
 55+ // pop element off stack
 56+ $elem = array_pop ($ancStack);
 57+ if (count ($ancStack) == 0)
 58+ $rootElem = $elem;
 59+ else
 60+ // add it to its parent
 61+ array_push ($ancStack[count($ancStack)-1]->children, $elem);
6462 }
6563
6664 function wgXMLcharacterData($parser, $data) {
67 - global $ancStack, $rootElem;
68 -
69 - $data = trim ( $data ) ; // Don't add blank lines, they're no use...
70 -
71 - // add to parent if parent exists
72 - if ( $ancStack && $data != "" ) {
73 - array_push($ancStack[count($ancStack)-1]->children, $data);
74 - }
 65+ global $ancStack;
 66+ $data = trim ($data); // Don't add blank lines, they're no use...
 67+ // add to parent if parent exists
 68+ if ( $ancStack && $data != "" ) {
 69+ array_push ($ancStack[count($ancStack)-1]->children, $data);
 70+ }
7571 }
7672
7773
@@ -81,36 +77,56 @@
8278 */
8379 class xml2php {
8480
85 - function &scanFile( $filename ) {
86 - global $ancStack;
87 - $ancStack = array();
88 -
89 - $xml_parser = xml_parser_create();
90 - xml_set_element_handler($xml_parser, 'wgXMLstartElement', 'wgXMLendElement');
91 - xml_set_character_data_handler($xml_parser, 'wgXMLcharacterData');
92 - if (!($fp = fopen($filename, 'r'))) {
93 - die('could not open XML input');
94 - }
95 - while ($data = fread($fp, 4096)) {
96 - if (!xml_parse($xml_parser, $data, feof($fp))) {
97 - die(sprintf("XML error: %s at line %d",
98 - xml_error_string(xml_get_error_code($xml_parser)),
99 - xml_get_current_line_number($xml_parser)));
100 - }
101 - }
102 - xml_parser_free($xml_parser);
 81+ function &scanFile( $filename ) {
 82+ global $ancStack, $rootElem;
 83+ $ancStack = array();
10384
104 - // return the remaining root element we copied in the beginning
105 - return $ancStack[0];
106 - }
107 -
 85+ $xml_parser = xml_parser_create();
 86+ xml_set_element_handler ($xml_parser, 'wgXMLstartElement', 'wgXMLendElement');
 87+ xml_set_character_data_handler ($xml_parser, 'wgXMLcharacterData');
 88+ if (!($fp = fopen($filename, 'r'))) {
 89+ die('could not open XML input');
 90+ }
 91+ while ($data = fread($fp, 4096)) {
 92+ if (!xml_parse($xml_parser, $data, feof($fp))) {
 93+ die(sprintf("XML error: %s at line %d",
 94+ xml_error_string(xml_get_error_code($xml_parser)),
 95+ xml_get_current_line_number($xml_parser)));
 96+ }
 97+ }
 98+ xml_parser_free($xml_parser);
 99+
 100+ // return the remaining root element we copied in the beginning
 101+ return $rootElem;
 102+ }
 103+
 104+ function scanString ( $input ) {
 105+ global $ancStack, $rootElem;
 106+ $ancStack = array();
 107+
 108+ $xml_parser = xml_parser_create();
 109+ xml_set_element_handler ($xml_parser, 'wgXMLstartElement', 'wgXMLendElement');
 110+ xml_set_character_data_handler ($xml_parser, 'wgXMLcharacterData');
 111+
 112+ if (!xml_parse ($xml_parser, $input, true)) {
 113+ die (sprintf ("XML error: %s at line %d",
 114+ xml_error_string(xml_get_error_code($xml_parser)),
 115+ xml_get_current_line_number($xml_parser)));
 116+ }
 117+ xml_parser_free ($xml_parser);
 118+
 119+ // return the remaining root element we copied in the beginning
 120+ return $rootElem;
 121+ }
 122+
108123 }
109124
110 -$w = new xml2php;
111 -$filename = 'sample.xml';
112 -$result = $w->scanFile( $filename );
113 -$result->myPrint();
 125+/* Example code:
114126
115 -return 0;
 127+ $w = new xml2php;
 128+ $filename = 'sample.xml';
 129+ $result = $w->scanFile( $filename );
 130+ print $result->myPrint();
 131+*/
116132
117133 ?>

Status & tagging log