Skip to content

Commit dc8cf6b

Browse files
swissspidyclaude
andauthored
Load terms, meta and comments for batches of posts (#147)
* Load terms, meta and comments for batches of posts Exporting a post ran separate queries for its terms, its meta, its comments and each comment's meta. Load all of them for the batch of posts that is already fetched together instead, which cuts the number of queries per batch of 100 posts from several hundred to about six. The output is unchanged: terms, meta and comments are ordered the same way as the per-post queries order them, `_edit_lock` and the `wxr_export_skip_postmeta` filter are still respected, and spam comments are still skipped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01D26yjkN2BiqCXT6p6o1WqS * Use the already loaded comment meta in the WXR formatter Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01D26yjkN2BiqCXT6p6o1WqS * Filter post meta while its post is the global post Callbacks of the `wxr_export_skip_postmeta` filter can rely on the global post, which is only set to the post the meta belongs to in exportify_post(). Load the meta for the whole batch, but filter it there instead of while loading the batch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01D26yjkN2BiqCXT6p6o1WqS * 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 * Test the order of terms with the same name across batches Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01D26yjkN2BiqCXT6p6o1WqS * Find the batch of a post by its position in the list of post IDs `--post__in` IDs are deduplicated with array_unique() and array_filter(), which keep the original keys. Flipping that array mapped IDs to keys rather than positions, so with duplicate IDs batches started at the wrong offset or past the end of the list, which queried `IN ()`. Also compare term names rather than slugs in the multi-batch scenario, as the database can return terms with the same name in any order. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01D26yjkN2BiqCXT6p6o1WqS * Query terms per post for taxonomies registered with query arguments The arguments a taxonomy is registered with override the ones passed to wp_get_object_terms(). With `number`, the batched query limited the terms of the whole batch, and with `fields` the terms could come back without the object ID, so posts lost their terms in the export. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01D26yjkN2BiqCXT6p6o1WqS --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 1fc05f3 commit dc8cf6b

3 files changed

Lines changed: 389 additions & 4 deletions

File tree

‎features/export.feature‎

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,3 +1402,182 @@ Feature: Export content.
14021402
"""
14031403
<wp:meta_value><![CDATA[term_metavalue]]></wp:meta_value>
14041404
"""
1405+
1406+
Scenario: Export attaches terms, meta and comments to the right posts across batches
1407+
Given a WP install
1408+
And I run `wp site empty --yes`
1409+
And I run `wp post generate --count=150`
1410+
And I run `wp post create --post_title='Last post' --post_status=publish --tags_input='Zoo,apple,beta' --porcelain`
1411+
And save STDOUT as {LAST_POST_ID}
1412+
And I run `wp term create category Zoo --slug=zoo-category`
1413+
And I run `wp post term add {LAST_POST_ID} category zoo-category`
1414+
And I run `wp post meta add {LAST_POST_ID} my_meta_key my_meta_value`
1415+
And I run `wp post meta add {LAST_POST_ID} _edit_lock 123:1`
1416+
And I run `wp comment create --comment_post_ID={LAST_POST_ID} --comment_content='Approved comment' --comment_approved=1 --porcelain`
1417+
And save STDOUT as {COMMENT_ID}
1418+
And I run `wp comment meta add {COMMENT_ID} my_comment_meta_key my_comment_meta_value`
1419+
And I run `wp comment create --comment_post_ID={LAST_POST_ID} --comment_content='Spam comment' --comment_approved=spam`
1420+
And a wp-content/mu-plugins/skip-meta.php file:
1421+
"""
1422+
<?php
1423+
// Only export meta while the post it belongs to is the current post.
1424+
add_filter(
1425+
'wxr_export_skip_postmeta',
1426+
function ( $skip, $meta_key, $meta ) {
1427+
return $skip || (int) $meta->post_id !== get_the_ID();
1428+
},
1429+
10,
1430+
3
1431+
);
1432+
"""
1433+
And a check-export.php file:
1434+
"""
1435+
<?php
1436+
$xml = simplexml_load_file( $args[0] );
1437+
$found = 0;
1438+
foreach ( $xml->channel->item as $item ) {
1439+
$wp = $item->children( 'http://wordpress.org/export/1.2/' );
1440+
$tags = array();
1441+
$terms = array();
1442+
foreach ( $item->category as $category ) {
1443+
if ( 'post_tag' === (string) $category['domain'] ) {
1444+
$tags[] = (string) $category['nicename'];
1445+
}
1446+
$terms[] = (string) $category;
1447+
}
1448+
$meta = array();
1449+
foreach ( $wp->postmeta as $postmeta ) {
1450+
if ( in_array( (string) $postmeta->meta_key, array( '_pingme', '_encloseme' ), true ) ) {
1451+
continue;
1452+
}
1453+
$meta[] = (string) $postmeta->meta_key . '=' . (string) $postmeta->meta_value;
1454+
}
1455+
$comments = array();
1456+
foreach ( $wp->comment as $comment ) {
1457+
$comment_meta = array();
1458+
foreach ( $comment->commentmeta as $commentmeta ) {
1459+
$comment_meta[] = (string) $commentmeta->meta_key . '=' . (string) $commentmeta->meta_value;
1460+
}
1461+
$comments[] = (string) $comment->comment_content . ' [' . implode( ',', $comment_meta ) . ']';
1462+
}
1463+
if ( (string) $wp->post_id === '{LAST_POST_ID}' ) {
1464+
++$found;
1465+
// The terms should be in the same order as the database returns them for the post. Compare
1466+
// their names, as the database can return terms with the same name in any order.
1467+
$expected = wp_list_pluck( wp_get_object_terms( (int) $wp->post_id, get_object_taxonomies( 'post' ) ), 'name' );
1468+
echo 'terms in database order: ' . ( $terms === $expected ? 'yes' : 'no' ) . "\n";
1469+
sort( $tags );
1470+
echo 'tags: ' . implode( ',', $tags ) . "\n";
1471+
echo 'meta: ' . implode( ',', $meta ) . "\n";
1472+
echo 'comments: ' . implode( ',', $comments ) . "\n";
1473+
} elseif ( $tags || $meta || $comments || array( 'Uncategorized' ) !== $terms ) {
1474+
echo 'unexpected data on post ' . $wp->post_id . "\n";
1475+
}
1476+
}
1477+
echo "found: $found\n";
1478+
"""
1479+
1480+
When I run `wp export --post_type=post`
1481+
Then save STDOUT 'Writing to file %s' as {EXPORT_FILE}
1482+
1483+
When I run `wp eval-file check-export.php {EXPORT_FILE}`
1484+
Then STDOUT should be:
1485+
"""
1486+
terms in database order: yes
1487+
tags: apple,beta,zoo
1488+
meta: my_meta_key=my_meta_value
1489+
comments: Approved comment [my_comment_meta_key=my_comment_meta_value]
1490+
found: 1
1491+
"""
1492+
1493+
# Duplicate IDs in --post__in leave gaps in the keys of the list of post IDs.
1494+
When I run `wp post list --post_type=post --orderby=ID --order=ASC --format=ids`
1495+
Then save STDOUT as {POST_IDS}
1496+
1497+
When I run `wp eval 'echo implode( ",", array_merge( ...array_map( function ( $id ) { return array( $id, $id ); }, explode( " ", "{POST_IDS}" ) ) ) );'`
1498+
Then save STDOUT as {DUPLICATED_POST_IDS}
1499+
1500+
Given a wp-content/mu-plugins/count-batches.php file:
1501+
"""
1502+
<?php
1503+
// Count the queries that load the post meta of a batch of posts.
1504+
$GLOBALS['batch_loads'] = 0;
1505+
add_filter(
1506+
'query',
1507+
function ( $query ) {
1508+
if ( false !== strpos( $query, 'postmeta WHERE post_id IN (' ) ) {
1509+
++$GLOBALS['batch_loads'];
1510+
}
1511+
return $query;
1512+
}
1513+
);
1514+
register_shutdown_function(
1515+
function () {
1516+
if ( $GLOBALS['batch_loads'] ) {
1517+
file_put_contents( ABSPATH . 'batch-loads.txt', $GLOBALS['batch_loads'] );
1518+
}
1519+
}
1520+
);
1521+
"""
1522+
1523+
When I run `wp export --post__in={DUPLICATED_POST_IDS} --filename_format=duplicated.xml`
1524+
Then save STDOUT 'Writing to file %s' as {DUPLICATED_EXPORT_FILE}
1525+
And the batch-loads.txt file should be:
1526+
"""
1527+
2
1528+
"""
1529+
1530+
When I run `wp eval-file check-export.php {DUPLICATED_EXPORT_FILE}`
1531+
Then STDOUT should be:
1532+
"""
1533+
terms in database order: yes
1534+
tags: apple,beta,zoo
1535+
meta: my_meta_key=my_meta_value
1536+
comments: Approved comment [my_comment_meta_key=my_comment_meta_value]
1537+
found: 1
1538+
"""
1539+
1540+
Scenario: Export the terms of taxonomies registered with arguments for querying terms
1541+
Given a WP install
1542+
And a wp-content/mu-plugins/taxonomies.php file:
1543+
"""
1544+
<?php
1545+
// These arguments override the ones passed to wp_get_object_terms().
1546+
add_action(
1547+
'init',
1548+
function () {
1549+
register_taxonomy( 'limited', 'post', array( 'args' => array( 'number' => 1 ) ) );
1550+
register_taxonomy( 'plain', 'post', array( 'args' => array( 'fields' => 'all' ) ) );
1551+
}
1552+
);
1553+
"""
1554+
And I run `wp term create limited Alpha`
1555+
And I run `wp term create limited Beta`
1556+
And I run `wp term create plain Gamma`
1557+
And I run `wp term create plain Delta`
1558+
And I run `wp post create --post_title='First post' --post_status=publish --porcelain`
1559+
And save STDOUT as {FIRST_POST_ID}
1560+
And I run `wp post term add {FIRST_POST_ID} limited alpha`
1561+
And I run `wp post term add {FIRST_POST_ID} plain gamma`
1562+
And I run `wp post create --post_title='Second post' --post_status=publish --porcelain`
1563+
And save STDOUT as {SECOND_POST_ID}
1564+
And I run `wp post term add {SECOND_POST_ID} limited beta`
1565+
And I run `wp post term add {SECOND_POST_ID} plain delta`
1566+
1567+
When I run `wp export --post_type=post --stdout`
1568+
Then STDOUT should contain:
1569+
"""
1570+
<category domain="limited" nicename="alpha"><![CDATA[Alpha]]></category>
1571+
"""
1572+
And STDOUT should contain:
1573+
"""
1574+
<category domain="limited" nicename="beta"><![CDATA[Beta]]></category>
1575+
"""
1576+
And STDOUT should contain:
1577+
"""
1578+
<category domain="plain" nicename="gamma"><![CDATA[Gamma]]></category>
1579+
"""
1580+
And STDOUT should contain:
1581+
"""
1582+
<category domain="plain" nicename="delta"><![CDATA[Delta]]></category>
1583+
"""

0 commit comments

Comments
 (0)