xml.inc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * @file
  4. * The Node export XML format handler.
  5. *
  6. * Adds XML format to Node export.
  7. */
  8. /**
  9. * Export callback.
  10. */
  11. function node_export_xml_export($nodes, $format) {
  12. $xml_code = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
  13. $xml_code .= "<node_export created=\"" . date('r') . "\">\n";
  14. $xml_code .= node_export_xml_encode($nodes);
  15. $xml_code .= "</node_export>";
  16. return $xml_code;
  17. }
  18. /**
  19. * Import callback.
  20. */
  21. function node_export_xml_import($code_string) {
  22. // Check for "<?xml" at the start.
  23. if (substr(ltrim($code_string), 0, 5) == "<?xml") {
  24. // Decode the XML.
  25. $xml_class = new NodeExportXmlDecoder();
  26. $result = $xml_class->decode($code_string);
  27. // Convert the nodes into objects.
  28. if (!isset($result['success'])) {
  29. foreach($result as $k => $v) {
  30. $result[$k] = (object)$v;
  31. }
  32. }
  33. return $result;
  34. }
  35. }
  36. /**
  37. * Build XML string recursively.
  38. */
  39. function node_export_xml_encode($var, $iteration = 0) {
  40. $xml_code = "";
  41. $tab = '';
  42. for ($i = 0; $i <= $iteration; $i++) {
  43. $tab = $tab . " ";
  44. }
  45. $iteration++;
  46. foreach ($var as $key => $value) {
  47. $attributes = array();
  48. if (is_bool($value)) {
  49. $attributes['type'] = 'boolean';
  50. }
  51. elseif (is_null($value)) {
  52. $attributes['type'] = 'NULL';
  53. }
  54. elseif (is_object($value)) {
  55. if ($iteration == 1 && isset($value->nid) && isset($value->type)) {
  56. // Assume first-level object with a nid and type is a stdClass node.
  57. $key = "node";
  58. }
  59. else {
  60. $attributes['class'] = get_class($value);
  61. }
  62. $value = (array)$value;
  63. }
  64. if (is_array($value) && array_values($value) === $value) {
  65. $attributes['_numeric_keys'] = "1";
  66. }
  67. $attr_string = "";
  68. foreach ($attributes as $attr_name => $attr_value) {
  69. $attr_string .= ' ' . $attr_name . '="' . $attr_value . '"';
  70. }
  71. if (is_numeric($key)) {
  72. $key = "n" . $key;
  73. }
  74. $xml_code .= $tab . "<" . $key . $attr_string . ">";
  75. if (is_array($value)) {
  76. if (!empty($value)) {
  77. $xml_code .= "\n";
  78. $xml_code .= node_export_xml_encode($value, $iteration);
  79. if (!is_numeric($key)) {
  80. $xml_code .= $tab;
  81. }
  82. }
  83. }
  84. elseif (is_numeric($value)) {
  85. $xml_code .= $value;
  86. }
  87. elseif (is_bool($value)) {
  88. $xml_code .= ($value ? 'TRUE' : 'FALSE');
  89. }
  90. elseif (is_string($value)) {
  91. $xml_code .= htmlspecialchars($value);
  92. }
  93. $xml_code .= "</" . $key . ">\n";
  94. }
  95. return $xml_code;
  96. }
  97. /**
  98. * Class for parsing Node export XML.
  99. */
  100. class NodeExportXmlDecoder {
  101. var $stack;
  102. var $output;
  103. function decode($code_string) {
  104. $parser = xml_parser_create();
  105. xml_set_element_handler($parser, array(&$this, 'start_handler'), array(&$this, 'end_handler'));
  106. xml_set_character_data_handler($parser, array(&$this, 'data_handler'));
  107. $this->stack = array(
  108. array(
  109. 'name' => 'node_export',
  110. 'attributes' => array(),
  111. 'children' => array(),
  112. 'data' => '',
  113. )
  114. );
  115. if (!xml_parse($parser, $code_string)) {
  116. $errors[] = "Node export XML import was unsuccessful, error details follow. No nodes imported.";
  117. $line = xml_get_current_line_number($parser);
  118. $column = xml_get_current_column_number($parser);
  119. $error = xml_error_string(xml_get_error_code($parser));
  120. $errors[] = "Line " . $line . ", Column " . $column .": ". $error;
  121. $lines = explode("\n", $code_string, $line + 1);
  122. $errors[] = "<pre>". htmlspecialchars($lines[$line - 1]) ."</pre>";
  123. xml_parser_free($parser);
  124. return array(
  125. 'success' => FALSE,
  126. 'output' => $errors,
  127. );
  128. }
  129. xml_parser_free($parser);
  130. $tmp = $this->build($this->stack[0]);
  131. if (count($tmp) == 1) {
  132. $this->output = array_pop($tmp);
  133. }
  134. else {
  135. $this->output = array();
  136. }
  137. unset($this->stack);
  138. return $this->output;
  139. }
  140. function build($stack) {
  141. $result = array();
  142. if (count($stack['children']) > 0) {
  143. $keycount = array();
  144. foreach ($stack['children'] as $child) {
  145. $keycount[] = $child['name'];
  146. }
  147. if (count(array_unique($keycount)) != count($keycount)) {
  148. // Enumerated array.
  149. $children = array();
  150. foreach ($stack['children'] as $child) {
  151. $children[] = $this->build($child);
  152. }
  153. }
  154. else {
  155. // Associative array.
  156. $children = array();
  157. foreach ($stack['children'] as $child) {
  158. if (!empty($stack['attributes']['_NUMERIC_KEYS'])) {
  159. $child['name'] = intval(substr($child['name'], 1));
  160. }
  161. $children[$child['name']] = $this->build($child);
  162. }
  163. }
  164. $result = array_merge($result, $children);
  165. }
  166. if (count($result) == 0) {
  167. // An atomic value.
  168. $return = trim($stack['data']);
  169. if (isset($stack['attributes']['TYPE'])) {
  170. if ($stack['attributes']['TYPE'] == 'boolean') {
  171. return (trim($stack['data']) == 'TRUE' ? TRUE : FALSE);
  172. }
  173. elseif ($stack['attributes']['TYPE'] == 'NULL') {
  174. return NULL;
  175. }
  176. }
  177. return htmlspecialchars_decode(trim($stack['data']));
  178. }
  179. else {
  180. // An array or object.
  181. if (isset($stack['attributes']['CLASS'])) {
  182. $object = new $stack['attributes']['CLASS']();
  183. foreach ($result as $k => $v) {
  184. $object->$k = $v;
  185. }
  186. $result = $object;
  187. }
  188. return $result;
  189. }
  190. }
  191. function start_handler($parser, $name, $attributes = array()) {
  192. $token = array();
  193. $token['name'] = strtolower($name);
  194. $token['attributes'] = $attributes;
  195. $token['data'] = '';
  196. $token['children'] = array();
  197. $this->stack[] = $token;
  198. }
  199. function end_handler($parser, $name, $attributes = array()) {
  200. $token = array_pop($this->stack);
  201. $this->stack[count($this->stack) - 1]['children'][] = $token;
  202. }
  203. function data_handler($parser, $data) {
  204. $this->stack[count($this->stack) - 1]['data'] .= $data;
  205. }
  206. }
  207. /**
  208. * Callback for actions.
  209. */
  210. function node_export_xml_action_form($context, &$form_state) {
  211. return node_export_action_form($context, $form_state, 'xml');
  212. }