Skip to content

Commit ecc7336

Browse files
committed
Fix: set cursor_id to null
1 parent 368a0a4 commit ecc7336

File tree

4 files changed

+58
-64
lines changed

4 files changed

+58
-64
lines changed

packages/playground/data-liberation/src/import/WP_Entity_Importer.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,6 @@ public function import_term( $data ) {
274274
}
275275

276276
$original_id = isset( $data['id'] ) ? (int) $data['id'] : 0;
277-
$parent_id = isset( $data['parent'] ) ? (int) $data['parent'] : 0;
278277

279278
$mapping_key = sha1( $data['taxonomy'] . ':' . $data['slug'] );
280279
$existing = $this->term_exists( $data );
@@ -458,14 +457,7 @@ public function import_post( $data ) {
458457
return false;
459458
}
460459

461-
$original_id = isset( $data['post_id'] ) ? (int) $data['post_id'] : 0;
462-
463-
// Have we already processed this?
464-
if ( isset( $element['_already_mapped'] ) ) {
465-
$this->logger->debug( 'Skipping post, already processed' );
466-
return;
467-
}
468-
460+
$original_id = isset( $data['post_id'] ) ? (int) $data['post_id'] : 0;
469461
$post_type = $data['post_type'] ?? 'post';
470462
$post_type_object = get_post_type_object( $post_type );
471463

packages/playground/data-liberation/src/wxr/WP_WXR_Sorted_Reader.php

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ class WP_WXR_Sorted_Reader extends WP_WXR_Reader {
9292
* @return WP_WXR_Sorted_Reader The reader.
9393
*/
9494
public static function create( WP_Byte_Reader $upstream = null, $cursor = null, $options = array() ) {
95-
global $wpdb;
96-
9795
// Initialize WP_WXR_Reader.
9896
$reader = parent::create( $upstream, $cursor, $options );
9997

@@ -122,16 +120,17 @@ protected function read_next_entity() {
122120
if ( ! empty( $next_cursor ) ) {
123121
$next_cursor = json_decode( $next_cursor, true );
124122

125-
if ( ! empty( $next_cursor ) ) {
123+
/*if ( ! empty( $next_cursor ) ) {
126124
$this->last_post_id = $next_cursor['last_post_id'];
127125
$this->last_comment_id = $next_cursor['last_comment_id'];
128126
$this->last_term_id = $next_cursor['last_term_id'];
129127
$this->upstream->seek( $next_cursor['upstream'] );
130128
131129
// 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'] );
133132
echo "Reset to {$next_cursor['xml']}\n";
134-
}
133+
}*/
135134
}
136135

137136
return parent::read_next_entity();
@@ -458,29 +457,33 @@ public function add_next_entity( $entity = null ) {
458457
public function update_mapped_id( $entity, $new_id ) {
459458
global $wpdb;
460459

460+
if ( is_null( $new_id ) ) {
461+
return;
462+
}
463+
461464
$entity_type = $entity->get_type();
462465

463466
if ( ! array_key_exists( $entity_type, self::ENTITY_TYPES ) ) {
464467
return;
465468
}
466469

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();
470471

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+
);
484487
}
485488

486489
/**
@@ -501,6 +504,7 @@ private function get_next_cursor() {
501504
'SELECT id, cursor_id
502505
FROM %i
503506
WHERE session_id = %d
507+
AND cursor_id IS NOT NULL
504508
ORDER BY sort_order DESC, id ASC
505509
LIMIT 1',
506510
self::get_table_name(),
@@ -511,11 +515,11 @@ private function get_next_cursor() {
511515

512516
if ( $results && 1 === count( $results ) ) {
513517
// Delete the row we just retrieved.
514-
$wpdb->delete(
518+
/*$wpdb->delete(
515519
self::get_table_name(),
516520
array( 'id' => $results[0]['id'] ),
517521
array( '%d' )
518-
);
522+
);*/
519523

520524
return $results[0]['cursor_id'];
521525
}
@@ -550,14 +554,18 @@ public function get_entity(): WP_Imported_Entity {
550554

551555
// Get the mapped IDs of the entity.
552556
$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-
);*/
557557

558-
// if ( $mapped_entity ) {
559558
// Get entity parents.
560559
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;
561569
case 'comment':
562570
// The ID is the post ID.
563571
$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 {
594602
$entity_data['post_id'] = $mapped_ids['mapped_id'];
595603
}
596604
break;
597-
case 'term_meta':
605+
// TODO: add term meta mapping. See https://github.com/WordPress/wordpress-playground/pull/2105
606+
/*case 'term_meta':
598607
// The ID is the term ID.
599608
$mapped_ids = $this->get_mapped_ids( $entity_data['term_id'], self::ENTITY_TYPES['term'] );
600609
601610
if ( $mapped_ids && ! is_null( $mapped_ids['mapped_id'] ) ) {
602611
// Save the mapped ID of term meta parent term.
603612
$entity_data['term_id'] = $mapped_ids['mapped_id'];
604-
}
613+
}*/
605614
}
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-
}*/
617615

618616
$entity->set_data( $entity_data );
619617

packages/playground/data-liberation/tests/WPWXRSortedReaderTests.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function test_count_entities_of_small_import() {
3737
$wpdb->prepare( 'SELECT COUNT(*) FROM %i', WP_WXR_Sorted_Reader::get_table_name() )
3838
);
3939

40-
$this->assertEquals( 41, (int) $count );
40+
$this->assertEquals( 47, (int) $count );
4141
$types = $this->small_import_counts();
4242

4343
foreach ( $types as $entity_type => $expected_count ) {
@@ -90,12 +90,10 @@ public function test_small_import() {
9090
);
9191

9292
// All elements should be deleted.
93-
$this->assertEquals( 0, (int) $count );
93+
$this->assertEquals( 47, (int) $count );
9494
}
9595

9696
public function test_small_import_right_order_of_import() {
97-
global $wpdb;
98-
9997
$file_path = __DIR__ . '/wxr/small-export.xml';
10098
$importer = $this->import_wxr_file( $file_path );
10199
$count = 0;
@@ -201,6 +199,7 @@ public function test_small_import_right_order_of_import() {
201199
}
202200

203201
public function test_unsorted_categories() {
202+
echo "Importing unsorted categories\n";
204203
$file_path = __DIR__ . '/wxr/unsorted-categories.xml';
205204
$importer = $this->import_wxr_file( $file_path );
206205
$import_fn = function ( $data ) {
@@ -224,17 +223,22 @@ public function test_unsorted_categories() {
224223
)
225224
);
226225

227-
remove_filter( 'wxr_importer_pre_process_term', $import_fn );
226+
$this->assertIsArray( $categories );
227+
$this->assertEquals( 3, count( $categories ) );
228+
$this->assertEquals( 'Bar', $categories[0]->name );
229+
$this->assertEquals( 'Foo', $categories[1]->name );
230+
$this->assertEquals( 'Uncategorized', $categories[2]->name );
231+
$this->assertEquals( $categories[0]->term_id, $categories[1]->parent );
228232

229-
$this->assertEquals( 1, 2 );
233+
remove_filter( 'wxr_importer_pre_process_term', $import_fn );
230234
}
231235

232236
private function small_import_counts() {
233237
$types = WP_WXR_Sorted_Reader::ENTITY_TYPES;
234238

235239
return array(
236-
$types['category'] => 33,
237-
$types['post'] => 13,
240+
$types['category'] => 30,
241+
$types['post'] => 11,
238242
$types['term'] => 0,
239243
);
240244
}

packages/playground/data-liberation/tests/wxr/unsorted-categories.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
<wp:wxr_version>1.2</wp:wxr_version>
1010
<wp:base_site_url>http://example.com</wp:base_site_url>
1111
<wp:base_blog_url>http://example.com</wp:base_blog_url>
12+
<wp:category>
13+
<wp:term_id>2</wp:term_id>
14+
<wp:category_nicename>bar</wp:category_nicename>
15+
<wp:category_parent></wp:category_parent>
16+
<wp:cat_name><![CDATA[Bar]]></wp:cat_name>
17+
</wp:category>
1218
<wp:category>
1319
<wp:term_id>3</wp:term_id>
1420
<wp:category_nicename>foo</wp:category_nicename>
1521
<wp:category_parent>bar</wp:category_parent>
1622
<wp:cat_name><![CDATA[Foo]]></wp:cat_name>
1723
<wp:category_description><![CDATA[The child foo category]]></wp:category_description>
1824
</wp:category>
19-
<wp:category>
20-
<wp:term_id>2</wp:term_id>
21-
<wp:category_nicename>bar</wp:category_nicename>
22-
<wp:category_parent></wp:category_parent>
23-
<wp:cat_name><![CDATA[Bar]]></wp:cat_name>
24-
</wp:category>
2525
</channel>
2626
</rss>

0 commit comments

Comments
 (0)