@@ -92,8 +92,6 @@ class WP_WXR_Sorted_Reader extends WP_WXR_Reader {
92
92
* @return WP_WXR_Sorted_Reader The reader.
93
93
*/
94
94
public static function create ( WP_Byte_Reader $ upstream = null , $ cursor = null , $ options = array () ) {
95
- global $ wpdb ;
96
-
97
95
// Initialize WP_WXR_Reader.
98
96
$ reader = parent ::create ( $ upstream , $ cursor , $ options );
99
97
@@ -122,16 +120,17 @@ protected function read_next_entity() {
122
120
if ( ! empty ( $ next_cursor ) ) {
123
121
$ next_cursor = json_decode ( $ next_cursor , true );
124
122
125
- if ( ! empty ( $ next_cursor ) ) {
123
+ /* if ( ! empty( $next_cursor ) ) {
126
124
$this->last_post_id = $next_cursor['last_post_id'];
127
125
$this->last_comment_id = $next_cursor['last_comment_id'];
128
126
$this->last_term_id = $next_cursor['last_term_id'];
129
127
$this->upstream->seek( $next_cursor['upstream'] );
130
128
131
129
// Reset the XML processor to the cursor.
132
- $ this ->xml ->reset_to ( $ next_cursor ['xml ' ] );
130
+ // $this->xml->reset_to( $next_cursor['xml'] );
131
+ $this->xml = WP_XML_Processor::create_for_streaming( '', $next_cursor['xml'] );
133
132
echo "Reset to {$next_cursor['xml']}\n";
134
- }
133
+ }*/
135
134
}
136
135
137
136
return parent ::read_next_entity ();
@@ -458,29 +457,33 @@ public function add_next_entity( $entity = null ) {
458
457
public function update_mapped_id ( $ entity , $ new_id ) {
459
458
global $ wpdb ;
460
459
460
+ if ( is_null ( $ new_id ) ) {
461
+ return ;
462
+ }
463
+
461
464
$ entity_type = $ entity ->get_type ();
462
465
463
466
if ( ! array_key_exists ( $ entity_type , self ::ENTITY_TYPES ) ) {
464
467
return ;
465
468
}
466
469
467
- $ data = $ entity ->get_data ();
468
- $ entity_id = (string ) $ data [ self ::ENTITY_TYPES_ID [ $ entity_type ] ];
469
- $ existing_entity = $ this ->get_mapped_ids ( $ entity_id , self ::ENTITY_TYPES [ $ entity_type ] );
470
+ $ data = $ entity ->get_data ();
470
471
471
- if ( $ existing_entity && is_null ( $ existing_entity ['mapped_id ' ] ) ) {
472
- // Update the mapped ID.
473
- $ wpdb ->update (
474
- self ::get_table_name (),
475
- array ( 'mapped_id ' => (string ) $ new_id ),
476
- array (
477
- 'entity_id ' => $ entity_id ,
478
- 'entity_type ' => $ entity_type ,
479
- 'session_id ' => $ this ->current_session ,
480
- ),
481
- array ( '%s ' )
482
- );
483
- }
472
+ // Update the mapped ID.
473
+ $ wpdb ->update (
474
+ self ::get_table_name (),
475
+ array (
476
+ 'cursor_id ' => null ,
477
+ 'mapped_id ' => (string ) $ new_id ,
478
+ ),
479
+ array (
480
+ 'entity_id ' => (string ) $ data [ self ::ENTITY_TYPES_ID [ $ entity_type ] ],
481
+ 'entity_type ' => self ::ENTITY_TYPES [ $ entity_type ],
482
+ 'session_id ' => $ this ->current_session ,
483
+ 'mapped_id ' => null ,
484
+ ),
485
+ array ( '%s ' )
486
+ );
484
487
}
485
488
486
489
/**
@@ -501,6 +504,7 @@ private function get_next_cursor() {
501
504
'SELECT id, cursor_id
502
505
FROM %i
503
506
WHERE session_id = %d
507
+ AND cursor_id IS NOT NULL
504
508
ORDER BY sort_order DESC, id ASC
505
509
LIMIT 1 ' ,
506
510
self ::get_table_name (),
@@ -511,11 +515,11 @@ private function get_next_cursor() {
511
515
512
516
if ( $ results && 1 === count ( $ results ) ) {
513
517
// Delete the row we just retrieved.
514
- $ wpdb ->delete (
518
+ /* $wpdb->delete(
515
519
self::get_table_name(),
516
520
array( 'id' => $results[0]['id'] ),
517
521
array( '%d' )
518
- );
522
+ );*/
519
523
520
524
return $ results [0 ]['cursor_id ' ];
521
525
}
@@ -550,14 +554,18 @@ public function get_entity(): WP_Imported_Entity {
550
554
551
555
// Get the mapped IDs of the entity.
552
556
$ entity_data = $ entity ->get_data ();
553
- /*$mapped_entity = $this->get_mapped_ids(
554
- $entity_data[ self::ENTITY_TYPES_ID[ $entity_type ] ],
555
- self::ENTITY_TYPES[ $entity_type ]
556
- );*/
557
557
558
- // if ( $mapped_entity ) {
559
558
// Get entity parents.
560
559
switch ( $ entity_type ) {
560
+ case 'category ' :
561
+ // The ID is the parent category ID.
562
+ $ mapped_ids = $ this ->get_mapped_ids ( $ entity_data ['parent ' ], self ::ENTITY_TYPES ['category ' ] );
563
+
564
+ if ( $ mapped_ids && ! is_null ( $ mapped_ids ['mapped_id ' ] ) ) {
565
+ // Save the mapped ID of category parent.
566
+ $ entity_data ['parent ' ] = $ mapped_ids ['mapped_id ' ];
567
+ }
568
+ break ;
561
569
case 'comment ' :
562
570
// The ID is the post ID.
563
571
$ mapped_ids = $ this ->get_mapped_ids ( $ entity_data ['post_id ' ], self ::ENTITY_TYPES ['post ' ] );
@@ -594,26 +602,16 @@ public function get_entity(): WP_Imported_Entity {
594
602
$ entity_data ['post_id ' ] = $ mapped_ids ['mapped_id ' ];
595
603
}
596
604
break ;
597
- case 'term_meta ' :
605
+ // TODO: add term meta mapping. See https://github.com/WordPress/wordpress-playground/pull/2105
606
+ /*case 'term_meta':
598
607
// The ID is the term ID.
599
608
$mapped_ids = $this->get_mapped_ids( $entity_data['term_id'], self::ENTITY_TYPES['term'] );
600
609
601
610
if ( $mapped_ids && ! is_null( $mapped_ids['mapped_id'] ) ) {
602
611
// Save the mapped ID of term meta parent term.
603
612
$entity_data['term_id'] = $mapped_ids['mapped_id'];
604
- }
613
+ }*/
605
614
}
606
- // }
607
-
608
- /*if ( $mapped_entity ) {
609
- if ( ! is_null( $mapped_entity['mapped_id'] ) ) {
610
- // This is used to skip an entity if it has already been mapped.
611
- // $entity_data[ $id_field ] = $mapped_entity['mapped_id'];
612
- $entity_data['_already_mapped'] = true;
613
- } else {
614
- $entity_data['_already_mapped'] = false;
615
- }
616
- }*/
617
615
618
616
$ entity ->set_data ( $ entity_data );
619
617
0 commit comments