From ecc7336754ee797aab0d3524d1eac30188b9b8fe Mon Sep 17 00:00:00 2001 From: Francesco Bigiarini Date: Wed, 8 Jan 2025 11:03:57 +0100 Subject: [PATCH] Fix: set cursor_id to null --- .../src/import/WP_Entity_Importer.php | 10 +-- .../src/wxr/WP_WXR_Sorted_Reader.php | 80 +++++++++---------- .../tests/WPWXRSortedReaderTests.php | 20 +++-- .../tests/wxr/unsorted-categories.xml | 12 +-- 4 files changed, 58 insertions(+), 64 deletions(-) diff --git a/packages/playground/data-liberation/src/import/WP_Entity_Importer.php b/packages/playground/data-liberation/src/import/WP_Entity_Importer.php index 571bd46ff5..1aa2395c13 100644 --- a/packages/playground/data-liberation/src/import/WP_Entity_Importer.php +++ b/packages/playground/data-liberation/src/import/WP_Entity_Importer.php @@ -274,7 +274,6 @@ public function import_term( $data ) { } $original_id = isset( $data['id'] ) ? (int) $data['id'] : 0; - $parent_id = isset( $data['parent'] ) ? (int) $data['parent'] : 0; $mapping_key = sha1( $data['taxonomy'] . ':' . $data['slug'] ); $existing = $this->term_exists( $data ); @@ -458,14 +457,7 @@ public function import_post( $data ) { return false; } - $original_id = isset( $data['post_id'] ) ? (int) $data['post_id'] : 0; - - // Have we already processed this? - if ( isset( $element['_already_mapped'] ) ) { - $this->logger->debug( 'Skipping post, already processed' ); - return; - } - + $original_id = isset( $data['post_id'] ) ? (int) $data['post_id'] : 0; $post_type = $data['post_type'] ?? 'post'; $post_type_object = get_post_type_object( $post_type ); diff --git a/packages/playground/data-liberation/src/wxr/WP_WXR_Sorted_Reader.php b/packages/playground/data-liberation/src/wxr/WP_WXR_Sorted_Reader.php index ae1eeed487..ea3385592e 100644 --- a/packages/playground/data-liberation/src/wxr/WP_WXR_Sorted_Reader.php +++ b/packages/playground/data-liberation/src/wxr/WP_WXR_Sorted_Reader.php @@ -92,8 +92,6 @@ class WP_WXR_Sorted_Reader extends WP_WXR_Reader { * @return WP_WXR_Sorted_Reader The reader. */ public static function create( WP_Byte_Reader $upstream = null, $cursor = null, $options = array() ) { - global $wpdb; - // Initialize WP_WXR_Reader. $reader = parent::create( $upstream, $cursor, $options ); @@ -122,16 +120,17 @@ protected function read_next_entity() { if ( ! empty( $next_cursor ) ) { $next_cursor = json_decode( $next_cursor, true ); - if ( ! empty( $next_cursor ) ) { + /*if ( ! empty( $next_cursor ) ) { $this->last_post_id = $next_cursor['last_post_id']; $this->last_comment_id = $next_cursor['last_comment_id']; $this->last_term_id = $next_cursor['last_term_id']; $this->upstream->seek( $next_cursor['upstream'] ); // Reset the XML processor to the cursor. - $this->xml->reset_to( $next_cursor['xml'] ); + // $this->xml->reset_to( $next_cursor['xml'] ); + $this->xml = WP_XML_Processor::create_for_streaming( '', $next_cursor['xml'] ); echo "Reset to {$next_cursor['xml']}\n"; - } + }*/ } return parent::read_next_entity(); @@ -458,29 +457,33 @@ public function add_next_entity( $entity = null ) { public function update_mapped_id( $entity, $new_id ) { global $wpdb; + if ( is_null( $new_id ) ) { + return; + } + $entity_type = $entity->get_type(); if ( ! array_key_exists( $entity_type, self::ENTITY_TYPES ) ) { return; } - $data = $entity->get_data(); - $entity_id = (string) $data[ self::ENTITY_TYPES_ID[ $entity_type ] ]; - $existing_entity = $this->get_mapped_ids( $entity_id, self::ENTITY_TYPES[ $entity_type ] ); + $data = $entity->get_data(); - if ( $existing_entity && is_null( $existing_entity['mapped_id'] ) ) { - // Update the mapped ID. - $wpdb->update( - self::get_table_name(), - array( 'mapped_id' => (string) $new_id ), - array( - 'entity_id' => $entity_id, - 'entity_type' => $entity_type, - 'session_id' => $this->current_session, - ), - array( '%s' ) - ); - } + // Update the mapped ID. + $wpdb->update( + self::get_table_name(), + array( + 'cursor_id' => null, + 'mapped_id' => (string) $new_id, + ), + array( + 'entity_id' => (string) $data[ self::ENTITY_TYPES_ID[ $entity_type ] ], + 'entity_type' => self::ENTITY_TYPES[ $entity_type ], + 'session_id' => $this->current_session, + 'mapped_id' => null, + ), + array( '%s' ) + ); } /** @@ -501,6 +504,7 @@ private function get_next_cursor() { 'SELECT id, cursor_id FROM %i WHERE session_id = %d + AND cursor_id IS NOT NULL ORDER BY sort_order DESC, id ASC LIMIT 1', self::get_table_name(), @@ -511,11 +515,11 @@ private function get_next_cursor() { if ( $results && 1 === count( $results ) ) { // Delete the row we just retrieved. - $wpdb->delete( + /*$wpdb->delete( self::get_table_name(), array( 'id' => $results[0]['id'] ), array( '%d' ) - ); + );*/ return $results[0]['cursor_id']; } @@ -550,14 +554,18 @@ public function get_entity(): WP_Imported_Entity { // Get the mapped IDs of the entity. $entity_data = $entity->get_data(); - /*$mapped_entity = $this->get_mapped_ids( - $entity_data[ self::ENTITY_TYPES_ID[ $entity_type ] ], - self::ENTITY_TYPES[ $entity_type ] - );*/ - // if ( $mapped_entity ) { // Get entity parents. switch ( $entity_type ) { + case 'category': + // The ID is the parent category ID. + $mapped_ids = $this->get_mapped_ids( $entity_data['parent'], self::ENTITY_TYPES['category'] ); + + if ( $mapped_ids && ! is_null( $mapped_ids['mapped_id'] ) ) { + // Save the mapped ID of category parent. + $entity_data['parent'] = $mapped_ids['mapped_id']; + } + break; case 'comment': // The ID is the post ID. $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 { $entity_data['post_id'] = $mapped_ids['mapped_id']; } break; - case 'term_meta': + // TODO: add term meta mapping. See https://github.com/WordPress/wordpress-playground/pull/2105 + /*case 'term_meta': // The ID is the term ID. $mapped_ids = $this->get_mapped_ids( $entity_data['term_id'], self::ENTITY_TYPES['term'] ); if ( $mapped_ids && ! is_null( $mapped_ids['mapped_id'] ) ) { // Save the mapped ID of term meta parent term. $entity_data['term_id'] = $mapped_ids['mapped_id']; - } + }*/ } - // } - - /*if ( $mapped_entity ) { - if ( ! is_null( $mapped_entity['mapped_id'] ) ) { - // This is used to skip an entity if it has already been mapped. - // $entity_data[ $id_field ] = $mapped_entity['mapped_id']; - $entity_data['_already_mapped'] = true; - } else { - $entity_data['_already_mapped'] = false; - } - }*/ $entity->set_data( $entity_data ); diff --git a/packages/playground/data-liberation/tests/WPWXRSortedReaderTests.php b/packages/playground/data-liberation/tests/WPWXRSortedReaderTests.php index 7f9033524c..38c7b31695 100644 --- a/packages/playground/data-liberation/tests/WPWXRSortedReaderTests.php +++ b/packages/playground/data-liberation/tests/WPWXRSortedReaderTests.php @@ -37,7 +37,7 @@ public function test_count_entities_of_small_import() { $wpdb->prepare( 'SELECT COUNT(*) FROM %i', WP_WXR_Sorted_Reader::get_table_name() ) ); - $this->assertEquals( 41, (int) $count ); + $this->assertEquals( 47, (int) $count ); $types = $this->small_import_counts(); foreach ( $types as $entity_type => $expected_count ) { @@ -90,12 +90,10 @@ public function test_small_import() { ); // All elements should be deleted. - $this->assertEquals( 0, (int) $count ); + $this->assertEquals( 47, (int) $count ); } public function test_small_import_right_order_of_import() { - global $wpdb; - $file_path = __DIR__ . '/wxr/small-export.xml'; $importer = $this->import_wxr_file( $file_path ); $count = 0; @@ -201,6 +199,7 @@ public function test_small_import_right_order_of_import() { } public function test_unsorted_categories() { + echo "Importing unsorted categories\n"; $file_path = __DIR__ . '/wxr/unsorted-categories.xml'; $importer = $this->import_wxr_file( $file_path ); $import_fn = function ( $data ) { @@ -224,17 +223,22 @@ public function test_unsorted_categories() { ) ); - remove_filter( 'wxr_importer_pre_process_term', $import_fn ); + $this->assertIsArray( $categories ); + $this->assertEquals( 3, count( $categories ) ); + $this->assertEquals( 'Bar', $categories[0]->name ); + $this->assertEquals( 'Foo', $categories[1]->name ); + $this->assertEquals( 'Uncategorized', $categories[2]->name ); + $this->assertEquals( $categories[0]->term_id, $categories[1]->parent ); - $this->assertEquals( 1, 2 ); + remove_filter( 'wxr_importer_pre_process_term', $import_fn ); } private function small_import_counts() { $types = WP_WXR_Sorted_Reader::ENTITY_TYPES; return array( - $types['category'] => 33, - $types['post'] => 13, + $types['category'] => 30, + $types['post'] => 11, $types['term'] => 0, ); } diff --git a/packages/playground/data-liberation/tests/wxr/unsorted-categories.xml b/packages/playground/data-liberation/tests/wxr/unsorted-categories.xml index 86be8c5b90..3c1a3dc1e2 100644 --- a/packages/playground/data-liberation/tests/wxr/unsorted-categories.xml +++ b/packages/playground/data-liberation/tests/wxr/unsorted-categories.xml @@ -9,6 +9,12 @@ 1.2 http://example.com http://example.com + + 2 + bar + + + 3 foo @@ -16,11 +22,5 @@ - - 2 - bar - - -