/extensions/Wikidata/php-tools/XMLImport.php

https://github.com/ChuguluGames/mediawiki-svn · PHP · 226 lines · 109 code · 29 blank · 88 comment · 8 complexity · ef5a205a1c3411ec5ae029528a9ae9d2 MD5 · raw file

  1. <?php
  2. $depth = array();
  3. $specificXMLParser;
  4. interface XMLElementHandler {
  5. public function getHandlerForNewElement( $name );
  6. public function setAttributes( $attributes );
  7. public function processData( $data );
  8. public function close();
  9. public function notify( $childHandler );
  10. }
  11. interface XMLParser {
  12. public function startElement( $parser, $name, $attributes );
  13. public function characterData( $parser, $data );
  14. public function endElement( $parser );
  15. }
  16. class BaseXMLParser implements XMLParser {
  17. public $stack;
  18. public function __construct() {
  19. $this->stack = array();
  20. }
  21. public function startElement( $parser, $name, $attributes ) {
  22. $handler = end( $this->stack )->getHandlerForNewElement( $name );
  23. $handler->setAttributes( $attributes );
  24. $this->stack[] = $handler;
  25. }
  26. public function characterData( $parser, $data ) {
  27. end( $this->stack )->processData( $data );
  28. }
  29. public function endElement( $parser ) {
  30. $handler = array_pop( $this->stack );
  31. $handler->close();
  32. if ( count( $this->stack ) > 0 )
  33. end( $this->stack )->notify( $handler );
  34. }
  35. }
  36. class DefaultXMLElementHandler implements XMLElementHandler {
  37. public $name;
  38. public $attributes;
  39. public $data = "";
  40. public function getHandlerForNewElement( $name ) {
  41. $result = new DefaultXMLElementHandler();
  42. $result->name = $name;
  43. return $result;
  44. }
  45. public function setAttributes( $attributes ) {
  46. $this->attributes = $attributes;
  47. }
  48. public function processData( $data ) {
  49. $this->data .= $data;
  50. }
  51. public function close() {
  52. }
  53. public function notify( $childHandler ) {
  54. }
  55. }
  56. class DummyXMLElementHandler implements XMLElementHandler {
  57. public function getHandlerForNewElement( $name ) {
  58. return $this;
  59. }
  60. public function setAttributes( $attributes ) {
  61. }
  62. public function processData( $data ) {
  63. }
  64. public function close() {
  65. }
  66. public function notify( $childHandler ) {
  67. }
  68. }
  69. // class ChildrenXMLElementHandler implements XMLElementHandler {
  70. // public $currentChild;
  71. // public $children;
  72. //
  73. // public function __construct() {
  74. // $this->children = array();
  75. // }
  76. //
  77. // public function addCurrentChildElementHandler($childName, $childElementHandler) {
  78. // $this->children[]=$childElementHandler;
  79. // $this->currentChild = $childElementHandler;
  80. // }
  81. //
  82. // public function closed() {
  83. // return !($this->currentChild && !$this->currentChild->closed());
  84. // }
  85. //
  86. // public function newElement($childName) {
  87. // if($this->currentChild && !$this->currentChild->closed()) {
  88. // return $this->currentChild->newElement($childName);
  89. // }
  90. // else {
  91. // return false;
  92. // }
  93. // }
  94. //
  95. // public function endElement($name) {
  96. // if($this->currentChild && !$this->currentChild->closed()){
  97. // return $this->currentChild->endElement($name);
  98. // }
  99. // else {
  100. // return false;
  101. // }
  102. // }
  103. //
  104. // public function setAttributes($attributes) {
  105. // if($this->currentChild && !$this->currentChild->closed()){
  106. // return $this->currentChild->setAttributes($attributes);
  107. // }
  108. // else {
  109. // return false;
  110. // }
  111. // }
  112. //
  113. // public function setData($data) {
  114. // if($this->currentChild && !$this->currentChild->closed()){
  115. // return $this->currentChild->setData($data);
  116. // }
  117. // else {
  118. // return false;
  119. // }
  120. // }
  121. // }
  122. //
  123. // class EmptyXMLElementHandler implements XMLElementHandler {
  124. // public $name;
  125. // public $m_closed;
  126. //
  127. // public function __construct() {
  128. // $this->m_closed = false;
  129. // }
  130. //
  131. // public function setAttributes($attributes) {
  132. // return !$this->closed();
  133. // }
  134. //
  135. // public function closed() {
  136. // return $this->m_closed;
  137. // }
  138. //
  139. // public function newElement($childName) {
  140. // return !$this->closed();
  141. // }
  142. //
  143. // public function endElement($name) {
  144. // if ($this->closed()) {
  145. // return false;
  146. // }
  147. // else {
  148. // $this->m_closed = ($this->name == $name);
  149. // return true;
  150. // }
  151. // }
  152. //
  153. // public function setData($data) {
  154. // return !$this->closed();
  155. // }
  156. // }
  157. function parseXML( $fileHandle, $xmlParser ) {
  158. $standardXmlParser = xml_parser_create();
  159. xml_set_element_handler( $standardXmlParser, array( $xmlParser, "startElement" ), array( $xmlParser, "endElement" ) );
  160. xml_set_character_data_handler( $standardXmlParser, array( $xmlParser, "characterData" ) );
  161. while ( $data = fread( $fileHandle, 32 * 1024 * 1024 ) ) {
  162. if ( !xml_parse( $standardXmlParser, $data, feof( $fileHandle ) ) ) {
  163. die( sprintf( "XML error: %s at line %d",
  164. xml_error_string( xml_get_error_code( $standardXmlParser ) ),
  165. xml_get_current_line_number( $standardXmlParser ) ) );
  166. }
  167. }
  168. xml_parser_free( $standardXmlParser );
  169. }
  170. function startXMLLayout( $parser, $name, $attrs ) {
  171. global $depth;
  172. for ( $i = 0; $i < $depth[$parser]; $i++ ) {
  173. echo " ";
  174. }
  175. echo "$name\n";
  176. foreach ( $attrs as $key => $value ) {
  177. for ( $i = 0; $i < $depth[$parser]; $i++ ) {
  178. echo " ";
  179. }
  180. echo "$key = $value\n";
  181. }
  182. $depth[$parser]++;
  183. }
  184. function characterXMLLayout( $parser, $data ) {
  185. global $depth;
  186. for ( $i = 0; $i < $depth[$parser]; $i++ ) {
  187. echo " ";
  188. }
  189. echo "$data\n";
  190. }
  191. function endXMLLayout( $parser, $name ) {
  192. global $depth;
  193. if ( $name == "ENTRY" ) {
  194. die( '\nfirst entry end\n' );
  195. }
  196. $depth[$parser]--;
  197. }