Skip to content

Commit 600421d

Browse files
committed
Align batches with the posts iterator and keep the database term order
The posts iterator queries chunks of post IDs without an ORDER BY, so the first post it returns is not necessarily the first ID of its chunk. Start the batch at the chunk boundary, as otherwise e.g. an export of `--post__in` IDs in descending order loaded a batch for every post. Terms come back from the database ordered by name in its collation, so keep that order and only order terms with the same name by their term_taxonomy_id, instead of sorting them by name in PHP. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01D26yjkN2BiqCXT6p6o1WqS
1 parent 3c78416 commit 600421d

2 files changed

Lines changed: 43 additions & 11 deletions

File tree

‎features/export.feature‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,7 +1370,7 @@ Feature: Export content.
13701370
Given a WP install
13711371
And I run `wp site empty --yes`
13721372
And I run `wp post generate --count=150`
1373-
And I run `wp post create --post_title='Last post' --post_status=publish --tags_input='alpha,beta' --porcelain`
1373+
And I run `wp post create --post_title='Last post' --post_status=publish --tags_input='Zoo,apple,beta' --porcelain`
13741374
And save STDOUT as {LAST_POST_ID}
13751375
And I run `wp post meta add {LAST_POST_ID} my_meta_key my_meta_value`
13761376
And I run `wp post meta add {LAST_POST_ID} _edit_lock 123:1`
@@ -1421,6 +1421,10 @@ Feature: Export content.
14211421
}
14221422
if ( (string) $wp->post_id === '{LAST_POST_ID}' ) {
14231423
++$found;
1424+
// The terms should be in the same order as the database returns them for the post.
1425+
$expected = wp_list_pluck( wp_get_object_terms( (int) $wp->post_id, 'post_tag' ), 'slug' );
1426+
echo 'tags in database order: ' . ( $tags === $expected ? 'yes' : 'no' ) . "\n";
1427+
sort( $tags );
14241428
echo 'tags: ' . implode( ',', $tags ) . "\n";
14251429
echo 'meta: ' . implode( ',', $meta ) . "\n";
14261430
echo 'comments: ' . implode( ',', $comments ) . "\n";
@@ -1437,7 +1441,8 @@ Feature: Export content.
14371441
When I run `wp eval-file check-export.php {EXPORT_FILE}`
14381442
Then STDOUT should be:
14391443
"""
1440-
tags: alpha,beta
1444+
tags in database order: yes
1445+
tags: apple,beta,zoo
14411446
meta: my_meta_key=my_meta_value
14421447
comments: Approved comment [my_comment_meta_key=my_comment_meta_value]
14431448
found: 1

‎src/WP_Export_Query.php‎

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,9 @@ private function load_batch_data( $post_id ) {
460460
return;
461461
}
462462

463-
$post_ids = array_map( 'intval', array_slice( $this->post_ids, $this->post_id_positions[ $post_id ], self::QUERY_CHUNK ) );
463+
// Load the same chunk of posts that the posts iterator queries, which returns them in no particular order.
464+
$start = intdiv( $this->post_id_positions[ $post_id ], self::QUERY_CHUNK ) * self::QUERY_CHUNK;
465+
$post_ids = array_map( 'intval', array_slice( $this->post_ids, $start, self::QUERY_CHUNK ) );
464466
$in_ids = implode( ',', $post_ids );
465467

466468
$terms_by_post = array_fill_keys( $post_ids, [] );
@@ -495,15 +497,10 @@ private function load_batch_data( $post_id ) {
495497
}
496498
}
497499

498-
// Terms are ordered by name. Order terms with the same name, e.g. a category and a tag,
499-
// by their term_taxonomy_id like the query for a single post returns them.
500+
// The terms are ordered by name in the database's collation. Order terms with the same name,
501+
// e.g. a category and a tag, by their term_taxonomy_id like the query for a single post returns them.
500502
foreach ( $terms_by_post as &$terms ) {
501-
usort(
502-
$terms,
503-
static function ( $a, $b ) {
504-
return [ $a->name, $a->term_taxonomy_id ] <=> [ $b->name, $b->term_taxonomy_id ];
505-
}
506-
);
503+
$terms = self::order_terms_with_same_name( $terms );
507504
}
508505
unset( $terms );
509506

@@ -541,6 +538,36 @@ static function ( $a, $b ) {
541538
}
542539
}
543540

541+
/**
542+
* Order runs of terms with the same name by their term_taxonomy_id, keeping the order of the runs.
543+
*
544+
* @param array<\WP_Term> $terms Terms ordered by name.
545+
* @return array<\WP_Term>
546+
*/
547+
private static function order_terms_with_same_name( $terms ) {
548+
$ordered = [];
549+
$count = count( $terms );
550+
$i = 0;
551+
while ( $i < $count ) {
552+
$run = [ $terms[ $i ] ];
553+
++$i;
554+
while ( $i < $count && $terms[ $i ]->name === $run[0]->name ) {
555+
$run[] = $terms[ $i ];
556+
++$i;
557+
}
558+
if ( count( $run ) > 1 ) {
559+
usort(
560+
$run,
561+
static function ( $a, $b ) {
562+
return $a->term_taxonomy_id <=> $b->term_taxonomy_id;
563+
}
564+
);
565+
}
566+
array_push( $ordered, ...$run );
567+
}
568+
return $ordered;
569+
}
570+
544571
private static function get_terms_for_post( $post ) {
545572
$taxonomies = get_object_taxonomies( $post->post_type );
546573
if ( empty( $taxonomies ) ) {

0 commit comments

Comments
 (0)