/[drupal]/contributions/modules/bookimport/drupal_export_parser.php
ViewVC logotype

Contents of /contributions/modules/bookimport/drupal_export_parser.php

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Thu Mar 2 19:04:03 2006 UTC (3 years, 8 months ago) by puregin
Branch: MAIN
CVS Tags: DRUPAL-4-7--1-0, HEAD
Branch point for: DRUPAL-4-7
Changes since 1.1: +34 -3 lines
File MIME type: text/x-php
  - Added code to parse teaser elements
  - Added parameter to control how to deal with import of PHP pages
    (this is passed through to the element handlers)
1 <?php
2 /**
3 * This is a helper class for PHP's XML parser, which implements
4 * behaviour for the parser, namely how it show handle start
5 * tags, end tags, and character data.
6 *
7 */
8 class drupal_export_parser {
9
10 var $node = array();
11 var $current_tag = '';
12 var $current_title = '';
13 var $current_body = '';
14 var $current_teaser = '';
15 var $current_parent = 0;
16 var $current_nodeid = 0;
17 var $action;
18 var $phpimport;
19
20 var $end_elt_callback;
21 var $start_elt_callback;
22
23 /**
24 *
25 */
26 function drupal_export_parser($start_callback = '', $end_callback = '') {
27 global $end_elt_callback;
28 global $start_elt_callback;
29 global $node;
30
31 $node = array();
32 $this->end_elt_callback = $end_callback;
33 $this->start_elt_callback = $start_callback;
34
35 $end_elt_callback = $end_callback;
36 $start_elt_callback = $start_callback;
37
38 if (DEBUG > 1) {
39 echo "constructor for class drupal_export_parser\n";
40 echo "setting end_callback to '$end_elt_callback'\n";
41 echo "setting start_callback to '$start_elt_callback'\n";
42 }
43 }
44
45 /**
46 * This function is called each time the XML parser encounters
47 * an opening tag.
48 */
49 function start_element($parser, $tag, $attributes) {
50 global $current_tag;
51 global $node;
52 global $current_body;
53 global $current_teaser;
54 global $current_parent;
55 global $current_nodeid;
56 global $start_elt_callback;
57
58 if (DEBUG > 0) { echo "start_element($tag)...\n";
59 if (count($attributes) > 0) {
60 print_r($attributes);
61 }
62 }
63
64 if ('book' == $tag) {
65 $current_body = '';
66 $current_teaser = '';
67 $current_title = '';
68 }
69 if ('node' == $tag) {
70 // prepare to create node; save node id attribute;
71 // save content (body) in a node
72
73 $tnode = new node();
74 $nodeid = preg_replace('@^(node-)(\d+)$@', '\2', $attributes['id']);
75 $tnode->set_id($nodeid);
76 $tnode->set_body($current_body);
77 $current_body = '';
78
79 $current_nodeid = $tnode->id;
80 $tnode->set_parent($current_parent);
81
82 if (DEBUG > 2) {
83 echo "current node id: $current_nodeid\n";
84 echo "current parent id:$current_parent\n";
85 echo "parent of $current_nodeid is $current_parent\n";
86 }
87
88 if (DEBUG > 1) { echo "creating node $current_nodeid\n"; }
89 // push the node
90 // echo "<b>push</b> node .. count before = ". count($node) ."\n";
91 array_push($node, $tnode);
92 // echo "count after: ". count($node) . "\n";
93
94 //echo "node is: ";
95 //print_r($node);
96
97 $current_parent = $current_nodeid;
98
99 if (function_exists($start_elt_callback)) {
100 // echo "calling callback $start_elt_callback\n";
101 call_user_func($start_elt_callback, $tnode, $tnode->id, $current_parent, $this->action, $this->phpimport);
102 }
103 else {
104 // echo "can't call callback: $start_elt_callback!\n";
105 }
106
107 }
108 if ('title' == $tag) {
109 $current_title = '';
110 }
111 if ('nodeinfo' == $tag) {
112 // echo "nodeinfo: pop node ..$node \n";
113 // echo "<b>pop</b> node .. count before = ". count($node) ."\n";
114 $tnode = array_pop($node);
115 // echo "count after: ". count($node) . "\n";
116
117 $tnode->set_depth($attributes['depth']);
118 $tnode->set_weight($attributes['weight']);
119 $tnode->set_md5_body($attributes['md5_body']);
120 $tnode->set_nodetype($attributes['type']);
121 $tnode->set_author($attributes['author']);
122 $tnode->set_uid($attributes['uid']);
123 $tnode->set_created($attributes['created']);
124 $tnode->set_status($attributes['status']);
125 $tnode->set_format($attributes['format']);
126 $tnode->set_sticky($attributes['sticky']);
127 $tnode->set_promote($attributes['promote']);
128
129 // echo "<b>push</b> node .. count before = ". count($node) ."\n";
130 array_push($node, $tnode);
131 // echo "count after: ". count($node) . "\n";
132
133 $current_body = '';
134 }
135 if ('content' == $tag) {
136 $current_body = '';
137 }
138 if ('teaser' == $tag) {
139 $current_teaser = '';
140 }
141 $current_tag = $tag;
142 }
143
144 /**
145 * This function is called each time the XML parser encounters
146 * a closing tag.
147 */
148 function end_element($parser, $tag) {
149 global $current_title;
150 global $current_body;
151 global $current_teaser;
152 global $current_tag;
153 global $current_parent;
154 global $end_elt_callback;
155 global $node;
156
157 if (DEBUG > 0) { echo "end_element($current_tag)...\n"; }
158
159 if ('book' == $tag) {
160 $current_body = '';
161 $current_title = '';
162 }
163 if ('node' == $tag) {
164 // echo "<b>pop</b> node .. count before = ". count($node) ."\n";
165 $tnode = array_pop($node);
166 // echo "count after: ". count($node) . "\n";
167
168 if (function_exists($end_elt_callback)) {
169 // echo "calling callback $end_elt_callback\n";
170 call_user_func($end_elt_callback, $tnode, $tnode->id, $tnode->parent, $this->action, $this->phpimport);
171 }
172 else {
173 // echo "can't call callback: $end_elt_callback!\n";
174 }
175
176 // Done with this node
177
178 // reset to start new body;
179 $current_body = '';
180 $current_teaser = '';
181 $current_tag = '';
182 $current_parent = $tnode->get_parent();
183 }
184 if ('title' == $tag) {
185 // Set the title of tnode
186 // echo "end_element: pop node ..\n";
187 // print_r($node);
188
189 // echo "<b>pop</b> node .. count before = ". count($node) ."\n";
190 $tnode = array_pop($node);
191 // echo "count after: ". count($node) . "\n";
192
193 // echo "end_element: tnode = \n";
194 // print_r($node);
195
196
197 $tnode->set_title($current_title);
198
199 // push the node
200 // echo "<b>push</b> node .. count before = ". count($node) ."\n";
201 array_push($node, $tnode);
202 // echo "count after: ". count($node) . "\n";
203
204 $current_tag = '';
205 $current_title = '';
206 }
207 if ('content' == $tag) {
208 // echo "<b>pop</b> node .. count before = ". count($node) ."\n";
209 $tnode = array_pop($node);
210 // echo "count after: ". count($node) . "\n";
211
212 $tnode -> set_body($current_body);
213
214 // push the node
215 // echo "<b>push</b> node .. count before = ". count($node) ."\n";
216 array_push($node, $tnode);
217 // echo "count after: ". count($node) . "\n";
218
219 // start new body;
220 $current_body = '';
221 $current_teaser = '';
222 $current_tag = '';
223 }
224 if ('teaser' == $tag) {
225 // echo "<b>pop</b> node .. count before = ". count($node) ."\n";
226 $tnode = array_pop($node);
227 // echo "count after: ". count($node) . "\n";
228
229 $tnode -> set_teaser($current_teaser);
230
231 // push the node
232 // echo "<b>push</b> node .. count before = ". count($node) ."\n";
233 array_push($node, $tnode);
234 // echo "count after: ". count($node) . "\n";
235
236 // start new teaser;
237 $current_teaser = '';
238 $current_tag = '';
239 }
240 if ('nodeinfo' == $tag) {
241 // echo "done nodeinfo: $current_body\n";
242
243
244 // start new body;
245 $current_body = '';
246 $current_teaser = '';
247 $current_tag = '';
248 }
249 }
250
251 /**
252 * This function is called each time the XML parser encounters
253 * character data (e.g., the contents of a tag)
254 */
255 function character_data($parser, $data) {
256 global $current_title;
257 global $current_body;
258 global $current_teaser;
259 global $current_tag;
260 global $node;
261
262 if (DEBUG > 3) { echo "character_data($current_tag"; };
263 if (DEBUG > 4) { echo ", $data"; };
264 if (DEBUG > 3) { echo ")\n"; };
265
266 if ('title' == $current_tag) {
267 $current_title .= $data;
268 // echo "char data:current_title = $current_title;<br/>";
269 }
270 else if ('content' == $current_tag) {
271 $current_body .= $data;
272 }
273 else if ('teaser' == $current_tag) {
274 $current_teaser .= $data;
275 }
276 else if ('nodeinfo' == $current_tag) {
277 // echo "chardata: nodeinfo: $data";
278 $current_body .= $data;
279 }
280 else {
281 // print "cdata:";
282 }
283 }
284 }
285 ?>

  ViewVC Help
Powered by ViewVC 1.1.2