-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathWXREntityReader.php
More file actions
978 lines (902 loc) · 29.9 KB
/
WXREntityReader.php
File metadata and controls
978 lines (902 loc) · 29.9 KB
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
<?php
namespace WordPress\DataLiberation\EntityReader;
use WordPress\ByteStream\ReadStream\ByteReadStream;
use WordPress\DataLiberation\ImportEntity;
use WordPress\XML\XMLProcessor;
use WordPress\XML\XMLUnsupportedException;
use function WordPress\Polyfill\_doing_it_wrong;
/**
* Data Liberation API: WP_WXR_Entity_Reader class
*
* Reads WordPress eXtended RSS (WXR) files and emits entities like posts,
* comments, users, and terms. Enables efficient processing of large WXR
* files without loading everything into memory.
*
* Note this is just a reader. It doesn't import any data into WordPress. It
* only reads meaningful entities from the WXR file.
*
* ## Design goals
*
* WP_WXR_Entity_Reader is built with the following characteristics in mind:
*
* * Speed – it should be as fast as possible
* * No PHP extensions required – it can run on any PHP installation
* * Reliability – no random crashes when encountering malformed XML or UTF-8 sequences
* * Low, predictable memory footprint to support 1000GB+ WXR files
* * Ability to pause, finish execution, and resume later, e.g. after a fatal error
*
* ## Implementation
*
* `WP_WXR_Entity_Reader` uses the `WP_XML_Processor` to find XML tags representing meaningful
* WordPress entities. The reader knows the WXR schema and only looks for relevant elements.
* For example, it knows that posts are stored in `rss > channel > item` and comments are
* stored in `rss > channel > item > `wp:comment`.
*
* The `$wxr->next_entity()` method stream-parses the next entity from the WXR document and
* exposes it to the API consumer via `$wxr->get_entity_type()` and `$wxr->get_entity_date()`.
* The next call to `$wxr->next_entity()` remembers where the parsing has stopped and parses
* the next entity after that point.
*
* Example:
*
* $reader = WP_WXR_Entity_Reader::create_for_streaming();
*
* // Add data as it becomes available
* $reader->append_bytes( fread( $file_handle, 65536 ) );
*
* // Process entities
* while ( $reader->next_entity() ) {
* switch ( $wxr_reader->get_entity_type() ) {
* case 'post':
* // ... process post ...
* break;
*
* case 'comment':
* // ... process comment ...
* break;
*
* case 'site_option':
* // ... process site option ...
* break;
*
* // ... process other entity types ...
* }
* }
*
* // Check if we need more input
* if ( $reader->is_paused_at_incomplete_input() ) {
* // Add more data and continue processing
* $reader->append_bytes( fread( $file_handle, 65536 ) );
* }
*
* The next_entity() -> fread -> break usage pattern may seem a bit tedious. This is expected. Even
* if the WXR parsing part of the WP_WXR_Entity_Reader offers a high-level API, working with byte streams
* requires reasoning on a much lower level. The StreamChain class shipped in this repository will
* make the API consumption easier with its transformation–oriented API for chaining data processors.
*
* Similarly to `WP_XML_Processor`, the `WP_WXR_Entity_Reader` enters a paused state when it doesn't
* have enough XML bytes to parse the entire entity.
*
* ## Caveats
*
* ### Extensibility
*
* `WP_WXR_Entity_Reader` ignores any XML elements it doesn't recognize. The WXR format is extensible
* so in the future the reader may start supporting registration of custom handlers for unknown
* tags in the future.
*
* ### Nested entities intertwined with data
*
* `WP_WXR_Entity_Reader` flushes the current entity whenever another entity starts. The upside is
* simplicity and a tiny memory footprint. The downside is that it's possible to craft a WXR
* document where some information would be lost. For example:
*
* ```xml
* <rss>
* <channel>
* <item>
* <title>Page with comments</title>
* <link>http://wpthemetestdata.wordpress.com/about/page-with-comments/</link>
* <wp:postmeta>
* <wp:meta_key>_wp_page_template</wp:meta_key>
* <wp:meta_value><![CDATA[default]]></wp:meta_value>
* </wp:postmeta>
* <wp:post_id>146</wp:post_id>
* </item>
* </channel>
* </rss>
* ```
*
* `WP_WXR_Entity_Reader` would accumulate post data until the `wp:post_meta` tag. Then it would emit a
* `post` entity and accumulate the meta information until the `</wp:postmeta>` closer. Then it
* would advance to `<wp:post_id>` and **ignore it**.
*
* This is not a problem in all the `.wxr` files I saw. Still, it is important to note this limitation.
* It is possible there is a `.wxr` generator somewhere out there that intertwines post fields with post
* meta and comments. If this ever comes up, we could:
*
* * Emit the `post` entity first, then all the nested entities, and then emit a special `post_update` entity.
* * Do multiple passes over the WXR file – one for each level of nesting, e.g. 1. Insert posts, 2. Insert Comments, 3. Insert comment meta
*
* Buffering all the post meta and comments seems like a bad idea – there might be gigabytes of data.
*
* ## Remaining work
*
* @TODO:
*
* - Revisit the need to implement the Iterator interface.
*
* @since WP_VERSION
*/
class WXREntityReader implements EntityReader {
/**
* The XML processor used to parse the WXR file.
*
* @since WP_VERSION
* @var WP_XML_Processor
*/
private $xml;
/**
* The name of the XML tag containing information about the WordPress entity
* currently being extracted from the WXR file.
*
* @since WP_VERSION
* @var string|null
*/
private $entity_tag;
/**
* The name of the current WordPress entity, such as 'post' or 'comment'.
*
* @since WP_VERSION
* @var string|null
*/
private $entity_type;
/**
* The data accumulated for the current entity.
*
* @since WP_VERSION
* @var array
*/
private $entity_data;
/**
* The byte offset of the current entity in the original input stream.
*
* @since WP_VERSION
* @var int
*/
private $entity_opener_byte_offset;
/**
* Whether the current entity has been emitted.
*
* @since WP_VERSION
* @var bool
*/
private $entity_finished = false;
/**
* The number of entities read so far.
*
* @since WP_VERSION
* @var int
*/
private $entities_read_so_far = 0;
/**
* The attributes from the last opening tag.
*
* @since WP_VERSION
* @var array
*/
private $last_opener_attributes = array();
/**
* The ID of the last processed post.
*
* @since WP_VERSION
* @var int|null
*/
private $last_post_id = null;
/**
* The ID of the last processed comment.
*
* @since WP_VERSION
* @var int|null
*/
private $last_comment_id = null;
/**
* Buffer for accumulating text content between tags.
*
* @since WP_VERSION
* @var string
*/
private $text_buffer = '';
/**
* Stream to pull bytes from when the input bytes are exhausted.
*
* @var WP_Byte_Producer
*/
private $upstream;
/**
* Whether the reader has finished processing the input stream.
*
* @var bool
*/
private $is_finished = false;
/**
* Mapping of WXR tags representing site options to their WordPress options names.
* These tags are only matched if they are children of the <channel> element.
*
* @since WP_VERSION
* @var array
*/
private $KNOWN_SITE_OPTIONS = [];
/**
* Mapping of WXR tags to their corresponding entity types and field mappings.
*
* @since WP_VERSION
* @var array
*/
private $KNOWN_ENITIES = [];
public static function create( ?ByteReadStream $upstream = null, $cursor = null ) {
$xml_cursor = null;
if ( null !== $cursor ) {
$cursor = json_decode( $cursor, true );
if ( false === $cursor ) {
_doing_it_wrong(
__METHOD__,
'Invalid cursor provided for WP_WXR_Entity_Reader::create().',
null
);
return false;
}
$xml_cursor = $cursor['xml'];
}
$xml = XMLProcessor::create_for_streaming( '', $xml_cursor );
$reader = new WXREntityReader( $xml );
if ( null !== $cursor ) {
$reader->last_post_id = $cursor['last_post_id'];
$reader->last_comment_id = $cursor['last_comment_id'];
}
if ( null !== $upstream ) {
$reader->connect_upstream( $upstream );
if ( null !== $cursor ) {
if ( ! isset( $cursor['upstream'] ) ) {
// No upstream cursor means we've processed the
// entire input stream.
$xml->input_finished();
$xml->next_token();
} else {
$upstream->seek( $cursor['upstream'] );
}
}
}
return $reader;
}
/**
* Constructor.
*
* @param WP_XML_Processor $xml The XML processor to use.
*
* @since WP_VERSION
*
*/
protected function __construct( XMLProcessor $xml ) {
$this->xml = $xml;
// Every XML element is a combination of a long-form namespace and a
// local element name, e.g. a syntax <wp:post_id> could actually refer
// to a (https://wordpress.org/export/1.0/, post_id) element.
//
// Namespaces are paramount for parsing XML and cannot be ignored. Elements
// element must be matched based on both their namespace and local name.
//
// Unfortunately, different WXR files defined the `wp` namespace in a different way.
// Folks use a mixture of HTTP vs HTTPS protocols and version numbers. We must
// account for all possible options to parse these documents correctly.
$wxr_namespaces = [
'http://wordpress.org/export/1.0/',
'https://wordpress.org/export/1.0/',
'http://wordpress.org/export/1.1/',
'https://wordpress.org/export/1.1/',
'http://wordpress.org/export/1.2/',
'https://wordpress.org/export/1.2/',
];
$this->KNOWN_ENITIES = [
'item' => array(
'type' => 'post',
'fields' => array(
'title' => 'post_title',
'link' => 'link',
'guid' => 'guid',
'description' => 'post_excerpt',
'pubDate' => 'post_published_at',
'{http://purl.org/dc/elements/1.1/}creator' => 'post_author',
'{http://purl.org/rss/1.0/modules/content/}encoded' => 'post_content',
'{http://wordpress.org/export/1.0/excerpt/}encoded' => 'post_excerpt',
'{http://wordpress.org/export/1.1/excerpt/}encoded' => 'post_excerpt',
'{http://wordpress.org/export/1.2/excerpt/}encoded' => 'post_excerpt',
)
)
];
foreach($wxr_namespaces as $wxr_namespace) {
$this->KNOWN_SITE_OPTIONS = array_merge($this->KNOWN_SITE_OPTIONS, array(
'{'.$wxr_namespace.'}base_blog_url' => 'home',
'{'.$wxr_namespace.'}base_site_url' => 'siteurl',
'title' => 'blogname',
));
$this->KNOWN_ENITIES['item']['fields'] = array_merge($this->KNOWN_ENITIES['item']['fields'], array(
'{'.$wxr_namespace.'}post_id' => 'post_id',
'{'.$wxr_namespace.'}status' => 'post_status',
'{'.$wxr_namespace.'}post_date' => 'post_date',
'{'.$wxr_namespace.'}post_date_gmt' => 'post_date_gmt',
'{'.$wxr_namespace.'}post_modified' => 'post_modified',
'{'.$wxr_namespace.'}post_modified_gmt' => 'post_modified_gmt',
'{'.$wxr_namespace.'}comment_status' => 'comment_status',
'{'.$wxr_namespace.'}ping_status' => 'ping_status',
'{'.$wxr_namespace.'}post_name' => 'post_name',
'{'.$wxr_namespace.'}post_parent' => 'post_parent',
'{'.$wxr_namespace.'}menu_order' => 'menu_order',
'{'.$wxr_namespace.'}post_type' => 'post_type',
'{'.$wxr_namespace.'}post_password' => 'post_password',
'{'.$wxr_namespace.'}is_sticky' => 'is_sticky',
'{'.$wxr_namespace.'}attachment_url' => 'attachment_url',
));
$this->KNOWN_ENITIES = array_merge($this->KNOWN_ENITIES, array(
'{'.$wxr_namespace.'}comment' => array(
'type' => 'comment',
'fields' => array(
'{'.$wxr_namespace.'}comment_id' => 'comment_id',
'{'.$wxr_namespace.'}comment_author' => 'comment_author',
'{'.$wxr_namespace.'}comment_author_email' => 'comment_author_email',
'{'.$wxr_namespace.'}comment_author_url' => 'comment_author_url',
'{'.$wxr_namespace.'}comment_author_IP' => 'comment_author_IP',
'{'.$wxr_namespace.'}comment_date' => 'comment_date',
'{'.$wxr_namespace.'}comment_date_gmt' => 'comment_date_gmt',
'{'.$wxr_namespace.'}comment_content' => 'comment_content',
'{'.$wxr_namespace.'}comment_approved' => 'comment_approved',
'{'.$wxr_namespace.'}comment_type' => 'comment_type',
'{'.$wxr_namespace.'}comment_parent' => 'comment_parent',
'{'.$wxr_namespace.'}comment_user_id' => 'comment_user_id',
),
),
'{'.$wxr_namespace.'}commentmeta' => array(
'type' => 'comment_meta',
'fields' => array(
'{'.$wxr_namespace.'}meta_key' => 'meta_key',
'{'.$wxr_namespace.'}meta_value' => 'meta_value',
),
),
'{'.$wxr_namespace.'}author' => array(
'type' => 'user',
'fields' => array(
'{'.$wxr_namespace.'}author_id' => 'ID',
'{'.$wxr_namespace.'}author_login' => 'user_login',
'{'.$wxr_namespace.'}author_email' => 'user_email',
'{'.$wxr_namespace.'}author_display_name' => 'display_name',
'{'.$wxr_namespace.'}author_first_name' => 'first_name',
'{'.$wxr_namespace.'}author_last_name' => 'last_name',
),
),
'{'.$wxr_namespace.'}postmeta' => array(
'type' => 'post_meta',
'fields' => array(
'{'.$wxr_namespace.'}meta_key' => 'meta_key',
'{'.$wxr_namespace.'}meta_value' => 'meta_value',
),
),
'{'.$wxr_namespace.'}term' => array(
'type' => 'term',
'fields' => array(
'{'.$wxr_namespace.'}term_id' => 'term_id',
'{'.$wxr_namespace.'}term_taxonomy' => 'taxonomy',
'{'.$wxr_namespace.'}term_slug' => 'slug',
'{'.$wxr_namespace.'}term_parent' => 'parent',
'{'.$wxr_namespace.'}term_name' => 'name',
),
),
'{'.$wxr_namespace.'}tag' => array(
'type' => 'tag',
'fields' => array(
'{'.$wxr_namespace.'}term_id' => 'term_id',
'{'.$wxr_namespace.'}tag_slug' => 'slug',
'{'.$wxr_namespace.'}tag_name' => 'name',
'{'.$wxr_namespace.'}tag_description' => 'description',
),
),
'{'.$wxr_namespace.'}category' => array(
'type' => 'category',
'fields' => array(
'{'.$wxr_namespace.'}category_nicename' => 'slug',
'{'.$wxr_namespace.'}category_parent' => 'parent',
'{'.$wxr_namespace.'}cat_name' => 'name',
'{'.$wxr_namespace.'}category_description' => 'description',
),
),
));
}
}
public function get_reentrancy_cursor() {
/**
* @TODO: Instead of adjusting the XML cursor internals, adjust the get_reentrancy_cursor()
* call to support $bookmark_name, e.g. $this->xml->get_reentrancy_cursor( 'last_entity' );
* If the cursor internal data was a part of every bookmark, this would have worked
* even after evicting the actual bytes where $last_entity is stored.
*/
$xml_cursor = $this->xml->get_reentrancy_cursor();
$xml_cursor = json_decode( base64_decode( $xml_cursor ), true );
$xml_cursor['upstream_bytes_forgotten'] = $this->entity_opener_byte_offset;
$xml_cursor = base64_encode( json_encode( $xml_cursor ) );
return json_encode(
array(
'xml' => $xml_cursor,
'upstream' => $this->entity_opener_byte_offset,
'last_post_id' => $this->last_post_id,
'last_comment_id' => $this->last_comment_id,
)
);
}
/**
* Gets the data for the current entity.
*
* @return ImportEntity The entity.
* @since WP_VERSION
*/
public function get_entity() {
if ( ! $this->get_entity_type() ) {
return false;
}
return new ImportEntity(
$this->get_entity_type(),
$this->entity_data
);
}
/**
* Gets the type of the current entity.
*
* @return string|false The entity type, or false if no entity is being processed.
* @since WP_VERSION
*
*/
private function get_entity_type() {
if ( null !== $this->entity_type ) {
return $this->entity_type;
}
if ( null === $this->entity_tag ) {
return false;
}
if ( ! array_key_exists( $this->entity_tag, $this->KNOWN_ENITIES ) ) {
return false;
}
return $this->KNOWN_ENITIES[ $this->entity_tag ]['type'];
}
/**
* Gets the ID of the last processed post.
*
* @return int|null The post ID, or null if no posts have been processed.
* @since WP_VERSION
*
*/
public function get_last_post_id() {
return $this->last_post_id;
}
/**
* Gets the ID of the last processed comment.
*
* @return int|null The comment ID, or null if no comments have been processed.
* @since WP_VERSION
*
*/
public function get_last_comment_id() {
return $this->last_comment_id;
}
/**
* Appends bytes to the input stream.
*
* @param string $bytes The bytes to append.
*
* @since WP_VERSION
*
*/
public function append_bytes( string $bytes ): void {
$this->xml->append_bytes( $bytes );
}
/**
* Marks the input as finished.
*
* @since WP_VERSION
*/
public function input_finished(): void {
$this->xml->input_finished();
}
/**
* Checks if processing is finished.
*
* @return bool Whether processing is finished.
* @since WP_VERSION
*
*/
public function is_finished(): bool {
return $this->is_finished;
}
/**
* Checks if processing is paused waiting for more input.
*
* @return bool Whether processing is paused.
* @since WP_VERSION
*
*/
public function is_paused_at_incomplete_input(): bool {
return $this->xml->is_paused_at_incomplete_input();
}
/**
* Gets the last error that occurred.
*
* @return string|null The error message, or null if no error occurred.
* @since WP_VERSION
*
*/
public function get_last_error(): ?string {
return $this->xml->get_last_error();
}
public function get_xml_exception(): ?XMLUnsupportedException {
return $this->xml->get_exception();
}
/**
* Advances to the next entity in the WXR file.
*
* @return bool Whether another entity was found.
* @since WP_VERSION
*
*/
public function next_entity() {
if ( $this->is_finished ) {
return false;
}
while ( true ) {
if ( $this->read_next_entity() ) {
return true;
}
// If the read failed because of incomplete input data,
// try pulling more bytes from upstream before giving up.
if ( $this->is_paused_at_incomplete_input() ) {
if ( $this->pull_upstream_bytes() ) {
continue;
} else {
break;
}
}
$this->is_finished = true;
break;
}
return false;
}
/**
* Advances to the next entity in the WXR file.
*
* @return bool Whether another entity was found.
* @since WP_VERSION
*
*/
private function read_next_entity() {
if ( $this->xml->is_finished() ) {
$this->after_entity();
return false;
}
if ( $this->xml->is_paused_at_incomplete_input() ) {
return false;
}
/**
* This is the first call after emitting an entity.
* Remove the previous entity details from the internal state
* and prepare for the next entity.
*/
if ( $this->entity_type && $this->entity_finished ) {
$this->after_entity();
// If we finished processing the entity on a closing tag, advance the XML processor to
// the next token. Otherwise the array_key_exists( $tag, static::KNOWN_ENITIES ) branch
// below will cause an infinite loop.
if ( $this->xml->is_tag_closer() ) {
if ( false === $this->xml->next_token() ) {
return false;
}
}
}
/**
* Main parsing loop. It advances the XML parser state until a full entity
* is available.
*/
do {
$breadcrumbs = $this->xml->get_breadcrumbs();
// Don't process anything outside the <rss> <channel> hierarchy.
if (
count( $breadcrumbs ) < 2 ||
$breadcrumbs[0] !== ['', 'rss'] ||
$breadcrumbs[1] !== ['', 'channel']
) {
continue;
}
/*
* Buffer text and CDATA sections until we find the next tag.
* Each tag may contain multiple text or CDATA sections so we can't
* just assume that a single `get_modifiable_text()` call would get
* the entire text content of an element.
*/
if (
$this->xml->get_token_type() === '#text' ||
$this->xml->get_token_type() === '#cdata-section'
) {
$this->text_buffer .= $this->xml->get_modifiable_text();
continue;
}
// We're only interested in tags after this point.
if ( $this->xml->get_token_type() !== '#tag' ) {
continue;
}
if ( count( $breadcrumbs ) <= 2 && $this->xml->is_tag_opener() ) {
$this->entity_opener_byte_offset = $this->xml->get_token_byte_offset_in_the_input_stream();
}
$tag_with_namespace = $this->xml->get_tag_namespace_and_local_name();
/**
* Custom adjustment: the Accessibility WXR file uses a non-standard
* wp:wp_author tag.
*
* @TODO: Should WP_WXR_Entity_Reader care about such non-standard tags when
* the regular WXR importer would ignore them? Perhaps a warning
* and an upstream PR would be a better solution.
*/
if ( $tag_with_namespace === '{http://wordpress.org/export/1.2/}wp_author' ) {
$tag_with_namespace = '{http://wordpress.org/export/1.2/}author';
}
/**
* If the tag is a known entity root, assume the previous entity is
* finished, emit it, and start processing the new entity the next
* time this function is called.
*/
if ( array_key_exists( $tag_with_namespace, $this->KNOWN_ENITIES ) ) {
if ( $this->entity_type && ! $this->entity_finished ) {
$this->emit_entity();
return true;
}
$this->after_entity();
// Only tag openers indicate a new entity. Closers just mean
// the previous entity is finished.
if ( $this->xml->is_tag_opener() ) {
$this->set_entity_tag( $tag_with_namespace );
$this->entity_opener_byte_offset = $this->xml->get_token_byte_offset_in_the_input_stream();
}
continue;
}
/**
* We're inside of an entity tag at this point.
*
* The following code assumes that we'll only see three types of tags:
*
* * Empty elements – such as <wp:comment_content />, that we'll ignore
* * XML element openers with only text nodes inside them.
* * XML element closers.
*
* Specifically, we don't expect to see any nested XML elements such as:
*
* <wp:comment_content>
* <title>Pygmalion</title>
* Long time ago...
* </wp:comment_content>
*
* The semantics of such a structure is not clear. The WP_WXR_Entity_Reader will
* enter an error state when it encounters such a structure.
*
* Such nesting wasn't found in any WXR files analyzed when building
* this class. If it actually is a part of the WXR standard, every
* supported nested element will need a custom handler.
*/
/**
* Buffer the XML tag opener attributes for later use.
*
* In WXR files, entity attributes come from two sources:
* * XML attributes on the tag itself
* * Text content between the opening and closing tags
*
* We store the XML attributes when encountering an opening tag,
* but wait until the closing tag to process the entity attributes.
* Why? Because only at that point we have both the attributes
* and all the related text nodes.
*/
if ( $this->xml->is_tag_opener() ) {
$this->last_opener_attributes = array();
// Get non-namespaced attributes.
$names = $this->xml->get_attribute_names_with_prefix( '', '' );
foreach ( $names as list($namespace, $name) ) {
$this->last_opener_attributes[ $name ] = $this->xml->get_attribute( $namespace, $name );
}
$this->text_buffer = '';
$is_site_option_opener = (
count( $this->xml->get_breadcrumbs() ) === 3 &&
$this->xml->matches_breadcrumbs( array( 'rss', 'channel', '*' ) ) &&
array_key_exists( $this->xml->get_tag_namespace_and_local_name(), $this->KNOWN_SITE_OPTIONS )
);
if ( $is_site_option_opener ) {
$this->entity_opener_byte_offset = $this->xml->get_token_byte_offset_in_the_input_stream();
}
continue;
}
/**
* At this point we're looking for the nearest tag closer so we can
* turn the buffered data into an entity attribute.
*/
if ( ! $this->xml->is_tag_closer() ) {
continue;
}
if (
! $this->entity_finished &&
$this->xml->get_breadcrumbs() === array( array( '', 'rss' ), array( '', 'channel' ) )
) {
// Look for site options in children of the <channel> tag.
if ( $this->parse_site_option() ) {
return true;
} else {
// Keep looking for an entity if none was found in the current tag.
continue;
}
}
/**
* Special handling to accumulate categories stored inside the <category>
* tag found inside <item> tags.
*
* For example, we want to convert this:
*
* <category><![CDATA[Uncategorized]]></category>
* <category domain="category" nicename="wordpress">
* <![CDATA[WordPress]]>
* </category>
*
* Into this:
*
* 'terms' => [
* [ 'taxonomy' => 'category', 'slug' => '', 'description' => 'Uncategorized' ],
* [ 'taxonomy' => 'category', 'slug' => 'WordPress', 'description' => 'WordPress' ],
* ]
*/
if (
$this->entity_type === 'post' &&
$this->xml->get_tag_local_name() === 'category' &&
array_key_exists( 'domain', $this->last_opener_attributes ) &&
array_key_exists( 'nicename', $this->last_opener_attributes )
) {
$this->entity_data['terms'][] = array(
'taxonomy' => $this->last_opener_attributes['domain'],
'slug' => $this->last_opener_attributes['nicename'],
/**
* trim() – in XML, trailing whitespace in text nodes must be ignored.
* @TODO: Only trim text nodes, not CDATA sections.
*/
'description' => trim( $this->text_buffer ),
);
$this->text_buffer = '';
continue;
}
/**
* Store the text content of known tags as the value of the corresponding
* entity attribute as defined by the $KNOWN_ENITIES mapping.
*
* Ignores tags unlisted in the $KNOWN_ENITIES mapping.
*
* The WXR format is extensible so this reader could potentially
* support registering custom handlers for unknown tags in the future.
*/
if ( ! isset( $this->KNOWN_ENITIES[ $this->entity_tag ]['fields'][ $tag_with_namespace ] ) ) {
continue;
}
$key = $this->KNOWN_ENITIES[ $this->entity_tag ]['fields'][ $tag_with_namespace ];
/**
* trim() – in XML, trailing whitespace in text nodes must be ignored.
* @TODO: Only trim text nodes, not CDATA sections.
*/
$this->entity_data[ $key ] = trim( $this->text_buffer );
$this->text_buffer = '';
} while ( $this->xml->next_token() );
if ( $this->is_paused_at_incomplete_input() ) {
return false;
}
/**
* Emit the last unemitted entity after parsing all the data.
*/
if (
$this->is_finished() &&
$this->entity_type &&
! $this->entity_finished
) {
$this->emit_entity();
return true;
}
return false;
}
/**
* Emits a site option entity from known children of the <channel>
* tag, e.g. <wp:base_blog_url> or <title>.
*
* @return bool Whether a site_option entity was emitted.
*/
private function parse_site_option() {
if ( ! array_key_exists( $this->xml->get_tag_namespace_and_local_name(), $this->KNOWN_SITE_OPTIONS ) ) {
return false;
}
$this->entity_type = 'site_option';
$this->entity_data = array(
'option_name' => $this->KNOWN_SITE_OPTIONS[ $this->xml->get_tag_namespace_and_local_name() ],
'option_value' => $this->text_buffer,
);
$this->emit_entity();
return true;
}
/**
* Connects a byte stream to automatically pull bytes from once
* the last input chunk have been processed.
*
* @param WP_Byte_Reader $stream The upstream stream.
*/
public function connect_upstream( ByteReadStream $stream ) {
$this->upstream = $stream;
}
/**
* Appends another chunk of bytes from upstream if available.
*/
private function pull_upstream_bytes() {
if ( ! $this->upstream ) {
return false;
}
if ( $this->upstream->reached_end_of_data() ) {
$this->input_finished();
return false;
}
$available_bytes = $this->upstream->pull( 65536 );
$this->append_bytes( $this->upstream->consume( $available_bytes ) );
return true;
}
/**
* Marks the current entity as emitted and updates tracking variables.
*
* @since WP_VERSION
*/
private function emit_entity() {
if ( $this->entity_type === 'post' ) {
// Not all posts have a `<wp:post_id>` tag.
$this->last_post_id = $this->entity_data['post_id'] ?? null;
} elseif ( $this->entity_type === 'post_meta' ) {
$this->entity_data['post_id'] = $this->last_post_id;
} elseif ( $this->entity_type === 'comment' ) {
$this->last_comment_id = $this->entity_data['comment_id'];
$this->entity_data['post_id'] = $this->last_post_id;
} elseif ( $this->entity_type === 'comment_meta' ) {
$this->entity_data['comment_id'] = $this->last_comment_id;
} elseif ( $this->entity_type === 'tag' ) {
$this->entity_data['taxonomy'] = 'post_tag';
} elseif ( $this->entity_type === 'category' ) {
$this->entity_data['taxonomy'] = 'category';
}
$this->entity_finished = true;
++ $this->entities_read_so_far;
}
/**
* Sets the current entity tag and type.
*
* @param string $tag The entity tag name.
*
* @since WP_VERSION
*
*/
private function set_entity_tag( string $tag_with_namespace ) {
$this->entity_tag = $tag_with_namespace;
if ( array_key_exists( $tag_with_namespace, $this->KNOWN_ENITIES ) ) {
$this->entity_type = $this->KNOWN_ENITIES[ $tag_with_namespace ]['type'];
}
}
/**
* Resets the state after processing an entity.
*
* @since WP_VERSION
*/
private function after_entity() {
$this->entity_tag = null;
$this->entity_type = null;
$this->entity_data = array();
$this->entity_finished = false;
$this->text_buffer = '';
$this->last_opener_attributes = array();
}
}