-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.php
410 lines (365 loc) · 11.3 KB
/
parser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
<?php
/*
Code taken from WordPress and WordPress Importer Plugin.
GPLv2 or later
*/
/**
* WordPress Error API.
*
* Contains the WP_Error class and the is_wp_error() function.
*
* @package WordPress
*/
/**
* WordPress Error class.
*
* Container for checking for WordPress errors and error messages. Return
* WP_Error and use {@link is_wp_error()} to check if this class is returned.
* Many core WordPress functions pass this class in the event of an error and
* if not handled properly will result in code errors.
*
* @package WordPress
* @since 2.1.0
*/
class WP_Error {
/**
* Stores the list of errors.
*
* @since 2.1.0
* @var array
* @access private
*/
var $errors = array();
/**
* Stores the list of data for error codes.
*
* @since 2.1.0
* @var array
* @access private
*/
var $error_data = array();
/**
* Constructor - Sets up error message.
*
* If code parameter is empty then nothing will be done. It is possible to
* add multiple messages to the same code, but with other methods in the
* class.
*
* All parameters are optional, but if the code parameter is set, then the
* data parameter is optional.
*
* @since 2.1.0
*
* @param string|int $code Error code
* @param string $message Error message
* @param mixed $data Optional. Error data.
* @return WP_Error
*/
function __construct($code = '', $message = '', $data = '') {
if ( empty($code) )
return;
$this->errors[$code][] = $message;
if ( ! empty($data) )
$this->error_data[$code] = $data;
}
/**
* Retrieve all error codes.
*
* @since 2.1.0
* @access public
*
* @return array List of error codes, if available.
*/
function get_error_codes() {
if ( empty($this->errors) )
return array();
return array_keys($this->errors);
}
/**
* Retrieve first error code available.
*
* @since 2.1.0
* @access public
*
* @return string|int Empty string, if no error codes.
*/
function get_error_code() {
$codes = $this->get_error_codes();
if ( empty($codes) )
return '';
return $codes[0];
}
/**
* Retrieve all error messages or error messages matching code.
*
* @since 2.1.0
*
* @param string|int $code Optional. Retrieve messages matching code, if exists.
* @return array Error strings on success, or empty array on failure (if using code parameter).
*/
function get_error_messages($code = '') {
// Return all messages if no code specified.
if ( empty($code) ) {
$all_messages = array();
foreach ( (array) $this->errors as $code => $messages )
$all_messages = array_merge($all_messages, $messages);
return $all_messages;
}
if ( isset($this->errors[$code]) )
return $this->errors[$code];
else
return array();
}
/**
* Get single error message.
*
* This will get the first message available for the code. If no code is
* given then the first code available will be used.
*
* @since 2.1.0
*
* @param string|int $code Optional. Error code to retrieve message.
* @return string
*/
function get_error_message($code = '') {
if ( empty($code) )
$code = $this->get_error_code();
$messages = $this->get_error_messages($code);
if ( empty($messages) )
return '';
return $messages[0];
}
/**
* Retrieve error data for error code.
*
* @since 2.1.0
*
* @param string|int $code Optional. Error code.
* @return mixed Null, if no errors.
*/
function get_error_data($code = '') {
if ( empty($code) )
$code = $this->get_error_code();
if ( isset($this->error_data[$code]) )
return $this->error_data[$code];
return null;
}
/**
* Append more error messages to list of error messages.
*
* @since 2.1.0
* @access public
*
* @param string|int $code Error code.
* @param string $message Error message.
* @param mixed $data Optional. Error data.
*/
function add($code, $message, $data = '') {
$this->errors[$code][] = $message;
if ( ! empty($data) )
$this->error_data[$code] = $data;
}
/**
* Add data for error code.
*
* The error code can only contain one error data.
*
* @since 2.1.0
*
* @param mixed $data Error data.
* @param string|int $code Error code.
*/
function add_data($data, $code = '') {
if ( empty($code) )
$code = $this->get_error_code();
$this->error_data[$code] = $data;
}
}
/**
* Check whether variable is a WordPress Error.
*
* Looks at the object and if a WP_Error class. Does not check to see if the
* parent is also WP_Error, so can't inherit WP_Error and still use this
* function.
*
* @since 2.1.0
*
* @param mixed $thing Check if unknown variable is WordPress Error object.
* @return bool True, if WP_Error. False, if not WP_Error.
*/
function is_wp_error($thing) {
if ( is_object($thing) && is_a($thing, 'WP_Error') )
return true;
return false;
}
/**
* Butchered version of WordPress eXtended RSS file parser implementations
*
* @package WordPress
* @subpackage Importer
*/
/**
* WordPress Importer class for managing parsing of WXR files.
*/
class WXR_Parser {
function parse( $file ) {
// Attempt to use proper XML parsers first
if ( extension_loaded( 'simplexml' ) ) {
$parser = new WXR_Parser_SimpleXML;
$result = $parser->parse( $file );
// If SimpleXML succeeds or this is an invalid WXR file then return the results
if ( 'SimpleXML_parse_error' != $result->get_error_code() )
return $result;
}
}
}
/**
* WXR Parser that makes use of the SimpleXML PHP extension.
*/
class WXR_Parser_SimpleXML {
function parse( $file ) {
$authors = $posts = $categories = $tags = $terms = array();
$internal_errors = libxml_use_internal_errors(true);
$xml = simplexml_load_file( $file );
// halt if loading produces an error
if ( ! $xml )
return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this WXR file', 'wordpress-importer' ), libxml_get_errors() );
$wxr_version = $xml->xpath('/rss/channel/wp:wxr_version');
if ( ! $wxr_version )
return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) );
$wxr_version = (string) trim( $wxr_version[0] );
// confirm that we are dealing with the correct file format
if ( ! preg_match( '/^\d+\.\d+$/', $wxr_version ) )
return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) );
$base_url = $xml->xpath('/rss/channel/wp:base_site_url');
$base_url = (string) trim( $base_url[0] );
$namespaces = $xml->getDocNamespaces();
if ( ! isset( $namespaces['wp'] ) )
$namespaces['wp'] = 'http://wordpress.org/export/1.1/';
if ( ! isset( $namespaces['excerpt'] ) )
$namespaces['excerpt'] = 'http://wordpress.org/export/1.1/excerpt/';
// grab authors
foreach ( $xml->xpath('/rss/channel/wp:author') as $author_arr ) {
$a = $author_arr->children( $namespaces['wp'] );
$login = (string) $a->author_login;
$authors[$login] = array(
'author_id' => (int) $a->author_id,
'author_login' => $login,
'author_email' => (string) $a->author_email,
'author_display_name' => (string) $a->author_display_name,
'author_first_name' => (string) $a->author_first_name,
'author_last_name' => (string) $a->author_last_name
);
}
// grab cats, tags and terms
foreach ( $xml->xpath('/rss/channel/wp:category') as $term_arr ) {
$t = $term_arr->children( $namespaces['wp'] );
$categories[] = array(
'term_id' => (int) $t->term_id,
'category_nicename' => (string) $t->category_nicename,
'category_parent' => (string) $t->category_parent,
'cat_name' => (string) $t->cat_name,
'category_description' => (string) $t->category_description
);
}
foreach ( $xml->xpath('/rss/channel/wp:tag') as $term_arr ) {
$t = $term_arr->children( $namespaces['wp'] );
$tags[] = array(
'term_id' => (int) $t->term_id,
'tag_slug' => (string) $t->tag_slug,
'tag_name' => (string) $t->tag_name,
'tag_description' => (string) $t->tag_description
);
}
foreach ( $xml->xpath('/rss/channel/wp:term') as $term_arr ) {
$t = $term_arr->children( $namespaces['wp'] );
$terms[] = array(
'term_id' => (int) $t->term_id,
'term_taxonomy' => (string) $t->term_taxonomy,
'slug' => (string) $t->term_slug,
'term_parent' => (string) $t->term_parent,
'term_name' => (string) $t->term_name,
'term_description' => (string) $t->term_description
);
}
// grab posts
foreach ( $xml->channel->item as $item ) {
$post = array(
'post_title' => (string) $item->title,
'guid' => (string) $item->guid,
);
$dc = $item->children( 'http://purl.org/dc/elements/1.1/' );
$post['post_author'] = (string) $dc->creator;
$content = $item->children( 'http://purl.org/rss/1.0/modules/content/' );
$excerpt = $item->children( $namespaces['excerpt'] );
$post['post_content'] = (string) $content->encoded;
$post['post_excerpt'] = (string) $excerpt->encoded;
$wp = $item->children( $namespaces['wp'] );
$post['post_id'] = (int) $wp->post_id;
$post['post_date'] = (string) $wp->post_date;
$post['post_date_gmt'] = (string) $wp->post_date_gmt;
$post['comment_status'] = (string) $wp->comment_status;
$post['ping_status'] = (string) $wp->ping_status;
$post['post_name'] = (string) $wp->post_name;
$post['status'] = (string) $wp->status;
$post['post_parent'] = (int) $wp->post_parent;
$post['menu_order'] = (int) $wp->menu_order;
$post['post_type'] = (string) $wp->post_type;
$post['post_password'] = (string) $wp->post_password;
$post['is_sticky'] = (int) $wp->is_sticky;
if ( isset($wp->attachment_url) )
$post['attachment_url'] = (string) $wp->attachment_url;
foreach ( $item->category as $c ) {
$att = $c->attributes();
if ( isset( $att['nicename'] ) )
$post['terms'][] = array(
'name' => (string) $c,
'slug' => (string) $att['nicename'],
'domain' => (string) $att['domain']
);
}
foreach ( $wp->postmeta as $meta ) {
$post['postmeta'][] = array(
'key' => (string) $meta->meta_key,
'value' => (string) $meta->meta_value
);
}
foreach ( $wp->comment as $comment ) {
$meta = array();
if ( isset( $comment->commentmeta ) ) {
foreach ( $comment->commentmeta as $m ) {
$meta[] = array(
'key' => (string) $m->meta_key,
'value' => (string) $m->meta_value
);
}
}
$post['comments'][] = array(
'comment_id' => (int) $comment->comment_id,
'comment_author' => (string) $comment->comment_author,
'comment_author_email' => (string) $comment->comment_author_email,
'comment_author_IP' => (string) $comment->comment_author_IP,
'comment_author_url' => (string) $comment->comment_author_url,
'comment_date' => (string) $comment->comment_date,
'comment_date_gmt' => (string) $comment->comment_date_gmt,
'comment_content' => (string) $comment->comment_content,
'comment_approved' => (string) $comment->comment_approved,
'comment_type' => (string) $comment->comment_type,
'comment_parent' => (string) $comment->comment_parent,
'comment_user_id' => (int) $comment->comment_user_id,
'commentmeta' => $meta,
);
}
$posts[] = $post;
}
return array(
'authors' => $authors,
'posts' => $posts,
'categories' => $categories,
'tags' => $tags,
'terms' => $terms,
'base_url' => $base_url,
'version' => $wxr_version
);
}
}