Skip to content

Commit

Permalink
Fix: set cursor_id to null
Browse files Browse the repository at this point in the history
  • Loading branch information
zaerl committed Jan 8, 2025
1 parent 368a0a4 commit ecc7336
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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' )
);
}

/**
Expand All @@ -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(),
Expand All @@ -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'];
}
Expand Down Expand Up @@ -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'] );
Expand Down Expand Up @@ -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 );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 ) {
Expand All @@ -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,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
<wp:wxr_version>1.2</wp:wxr_version>
<wp:base_site_url>http://example.com</wp:base_site_url>
<wp:base_blog_url>http://example.com</wp:base_blog_url>
<wp:category>
<wp:term_id>2</wp:term_id>
<wp:category_nicename>bar</wp:category_nicename>
<wp:category_parent></wp:category_parent>
<wp:cat_name><![CDATA[Bar]]></wp:cat_name>
</wp:category>
<wp:category>
<wp:term_id>3</wp:term_id>
<wp:category_nicename>foo</wp:category_nicename>
<wp:category_parent>bar</wp:category_parent>
<wp:cat_name><![CDATA[Foo]]></wp:cat_name>
<wp:category_description><![CDATA[The child foo category]]></wp:category_description>
</wp:category>
<wp:category>
<wp:term_id>2</wp:term_id>
<wp:category_nicename>bar</wp:category_nicename>
<wp:category_parent></wp:category_parent>
<wp:cat_name><![CDATA[Bar]]></wp:cat_name>
</wp:category>
</channel>
</rss>

0 comments on commit ecc7336

Please sign in to comment.