Skip to content

Commit 35ff148

Browse files
authored
fix: avoid rewriting unchanged imports (#475) (#476)
1 parent 67dc49a commit 35ff148

3 files changed

Lines changed: 149 additions & 5 deletions

File tree

‎README.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,17 @@ is a no-op. Other engines are rejected.
157157
- UNION branches need equal projection counts and compatible column types.
158158
Global ORDER BY accepts first-branch output names or positive ordinals.
159159

160+
### Import results
161+
162+
CLI imports and the `markdown-db/import` ability report `create_count`,
163+
`update_count`, `unchanged_count`, and `skipped_count`, with corresponding
164+
`created`, `updated`, `unchanged`, and `skipped` lists. Each `unchanged` entry
165+
contains the stored post `id` and source `path`, in both dry-run and apply.
166+
Existing posts whose final filtered post fields and managed source metadata
167+
already match are unchanged: no post write, revision, or modified-date bump
168+
occurs. Only dates supplied in the final post array are compared; dateless
169+
sources leave stored dates alone. Dry-run reports changes without writing.
170+
160171
### Reconciliation
161172

162173
Shared three-way reconciliation compares canonical and WordPress state against

‎inc/class-wp-markdown-cli.php‎

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public static function register(): void {
9494
'description' => 'Import MDI markdown files into the current WordPress database.',
9595
'category' => 'markdown-db',
9696
'input_schema' => self::import_input_schema(),
97-
'output_schema' => array( 'type' => 'object' ),
97+
'output_schema' => self::import_output_schema(),
9898
'execute_callback' => array( self::class, 'import' ),
9999
'permission_callback' => array( self::class, 'can_manage_markdown_db' ),
100100
)
@@ -428,6 +428,7 @@ static function ( object $a, object $b ): int {
428428
$id_map = array();
429429
$created = array();
430430
$updated = array();
431+
$unchanged = array();
431432
$skipped = array();
432433
foreach ( $storage->get_skipped_files() as $path => $reason ) {
433434
$skipped[] = array( 'path' => self::relative_path( $path, $content_dir ), 'reason' => $reason );
@@ -476,6 +477,15 @@ static function ( object $a, object $b ): int {
476477
$postarr['post_content'] = $converted_content;
477478
$postarr = self::apply_post_data_filter( $postarr, $context, $post );
478479

480+
if ( 'update' === $operation && self::import_is_unchanged( $existing_id, $postarr, $source_path, $source_hash ) ) {
481+
$operation = 'unchanged';
482+
--$update_count;
483+
$unchanged[] = array( 'id' => $existing_id, 'path' => $source_path );
484+
if ( $old_id > 0 ) {
485+
$id_map[ $old_id ] = $existing_id;
486+
}
487+
}
488+
479489
if ( count( $sample ) < 10 ) {
480490
$sample[] = array(
481491
'operation' => $operation,
@@ -485,7 +495,7 @@ static function ( object $a, object $b ): int {
485495
);
486496
}
487497

488-
if ( $dry_run ) {
498+
if ( $dry_run || 'unchanged' === $operation ) {
489499
continue;
490500
}
491501

@@ -536,9 +546,11 @@ static function ( object $a, object $b ): int {
536546
'candidate_count' => count( $posts ),
537547
'create_count' => $dry_run ? $create_count : count( $created ),
538548
'update_count' => $dry_run ? $update_count : count( $updated ),
549+
'unchanged_count' => count( $unchanged ),
539550
'skipped_count' => count( $skipped ),
540551
'created' => $created,
541552
'updated' => $updated,
553+
'unchanged' => $unchanged,
542554
'skipped' => $skipped,
543555
'sample' => $sample,
544556
);
@@ -661,7 +673,7 @@ private static function emit_cli_result( array $result, string $format, string $
661673
WP_CLI::line( sprintf( '%s mode: %s', $label, $result['mode'] ?? '' ) );
662674
WP_CLI::line( sprintf( 'Path: %s', $result['path'] ?? '' ) );
663675
WP_CLI::line( sprintf( 'Candidates: %d', (int) ( $result['candidate_count'] ?? 0 ) ) );
664-
foreach ( array( 'create_count', 'update_count', 'written_count', 'skipped_count' ) as $key ) {
676+
foreach ( array( 'create_count', 'update_count', 'unchanged_count', 'written_count', 'skipped_count' ) as $key ) {
665677
if ( isset( $result[ $key ] ) ) {
666678
WP_CLI::line( sprintf( '%s: %d', ucfirst( str_replace( '_', ' ', $key ) ), (int) $result[ $key ] ) );
667679
}
@@ -671,6 +683,28 @@ private static function emit_cli_result( array $result, string $format, string $
671683
}
672684
}
673685

686+
private static function import_output_schema(): array {
687+
$row = array(
688+
'type' => 'object',
689+
'properties' => array( 'id' => array( 'type' => 'integer' ), 'path' => array( 'type' => 'string' ) ),
690+
'required' => array( 'id', 'path' ),
691+
);
692+
$properties = array(
693+
'success' => array( 'type' => 'boolean' ),
694+
'mode' => array( 'type' => 'string' ),
695+
'path' => array( 'type' => 'string' ),
696+
'sample' => array( 'type' => 'array', 'items' => array( 'type' => 'object' ) ),
697+
'skipped' => array( 'type' => 'array', 'items' => array( 'type' => 'object' ) ),
698+
);
699+
foreach ( array( 'candidate_count', 'create_count', 'update_count', 'unchanged_count', 'skipped_count' ) as $key ) {
700+
$properties[ $key ] = array( 'type' => 'integer', 'minimum' => 0 );
701+
}
702+
foreach ( array( 'created', 'updated', 'unchanged' ) as $key ) {
703+
$properties[ $key ] = array( 'type' => 'array', 'items' => $row );
704+
}
705+
return array( 'type' => 'object', 'properties' => $properties );
706+
}
707+
674708
private static function import_input_schema(): array {
675709
return array(
676710
'type' => 'object',
@@ -891,6 +925,38 @@ private static function find_existing_post_id( object $post ): int {
891925
return 0;
892926
}
893927

928+
/** Compare only supplied fields; omitted dates must remain owned by WordPress. */
929+
private static function import_is_unchanged( int $post_id, array $postarr, string $source_path, string $source_hash ): bool {
930+
$stored = function_exists( 'get_post' ) ? get_post( $post_id ) : null;
931+
if ( ! $stored || ! function_exists( 'get_post_meta' ) ) {
932+
return false;
933+
}
934+
935+
// wp_insert_post consumes slashed input and stores unslashed values.
936+
$postarr = wp_unslash( wp_slash( $postarr ) );
937+
$integer_fields = array( 'ID', 'post_author', 'post_parent', 'menu_order', 'comment_count' );
938+
foreach ( $postarr as $field => $value ) {
939+
// Unknown/filter-added write directives cannot safely be treated as a no-op.
940+
if ( ! property_exists( $stored, $field ) || ! is_scalar( $value ) ) {
941+
return false;
942+
}
943+
if ( in_array( $field, $integer_fields, true ) ) {
944+
if ( (int) $value !== (int) $stored->$field ) {
945+
return false;
946+
}
947+
} elseif ( (string) $value !== (string) $stored->$field ) {
948+
return false;
949+
}
950+
}
951+
952+
foreach ( array( self::SOURCE_PATH_META => $source_path, self::SOURCE_IDENTITY_META => $source_path, self::SOURCE_HASH_META => $source_hash ) as $key => $value ) {
953+
if ( (string) get_post_meta( $post_id, $key, true ) !== $value ) {
954+
return false;
955+
}
956+
}
957+
return true;
958+
}
959+
894960
private static function post_array( object $post, int $existing_id, array $id_map ): array {
895961
$old_parent = (int) ( $post->post_parent ?? 0 );
896962
$parent_id = $old_parent > 0 ? (int) ( $id_map[ $old_parent ] ?? $old_parent ) : 0;

‎tests/smoke-mysql-import-export-abilities.php‎

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ function wp_unslash( $value ) {
196196
}
197197

198198
function wp_insert_post( array $postarr, bool $wp_error = false ) {
199+
$GLOBALS['mdi_insert_calls'] = ( $GLOBALS['mdi_insert_calls'] ?? 0 ) + 1;
199200
$postarr = wp_unslash( $postarr );
200201
$id = (int) ( $postarr['ID'] ?? $postarr['import_id'] ?? 0 );
201202
if ( $id <= 0 ) {
@@ -365,6 +366,29 @@ static function ( string $content, array $context ): string {
365366
$failures[] = 'import ability did not preserve frontmatter terms';
366367
}
367368

369+
$calls_before = $GLOBALS['mdi_insert_calls'];
370+
foreach ( array( true, false ) as $dry_run ) {
371+
$repeat = WP_Markdown_CLI::import( array( 'path' => $content_dir, 'dry_run' => $dry_run ) );
372+
if ( 0 !== $repeat['update_count'] || 1 !== $repeat['unchanged_count'] || array( array( 'id' => 17, 'path' => 'page/home.md' ) ) !== $repeat['unchanged'] || $calls_before !== $GLOBALS['mdi_insert_calls'] ) {
373+
$failures[] = 'unchanged dated re-import wrote a post or was not reported unchanged';
374+
}
375+
}
376+
// A final post-data filter must participate in change detection.
377+
add_filter( 'markdown_db_import_post_data', static function ( array $postarr ): array {
378+
$postarr['post_status'] = 'draft';
379+
return $postarr;
380+
} );
381+
foreach ( array( true, false ) as $dry_run ) {
382+
$status_import = WP_Markdown_CLI::import( array( 'path' => $content_dir, 'dry_run' => $dry_run ) );
383+
if ( 1 !== $status_import['update_count'] || 0 !== $status_import['unchanged_count'] ) {
384+
$failures[] = 'post-data status filter was not counted as a change';
385+
}
386+
}
387+
if ( $calls_before + 1 !== $GLOBALS['mdi_insert_calls'] || 'draft' !== $GLOBALS['mdi_test_posts'][17]->post_status ) {
388+
$failures[] = 'status filter did not write exactly once in apply';
389+
}
390+
$GLOBALS['mdi_test_filters']['markdown_db_import_post_data'] = array();
391+
368392
file_put_contents(
369393
$content_dir . '/page/home.md',
370394
str_replace( array( 'title: "Home"', 'Original body.' ), array( 'title: "Home Updated"', 'Updated body.' ), file_get_contents( $content_dir . '/page/home.md' ) )
@@ -461,7 +485,7 @@ static function ( string $content, array $context ): string {
461485
$plain_id = $plain_import['created'][0]['id'] ?? 0;
462486
$before = $GLOBALS['mdi_test_posts'][ $plain_id ] ?? null;
463487
$repeat = WP_Markdown_CLI::import( $plain_options );
464-
if ( 1 !== $plain_import['create_count'] || 1 !== $repeat['update_count'] || 0 !== $repeat['create_count'] || ! $before || 'Plain home' !== $before->post_title || 'plain-home' !== $before->post_name || 'Plain body.' !== $before->post_content || $before != $GLOBALS['mdi_test_posts'][ $plain_id ] ) {
488+
if ( 1 !== $plain_import['create_count'] || 0 !== $repeat['update_count'] || 1 !== $repeat['unchanged_count'] || 0 !== $repeat['create_count'] || ! $before || 'Plain home' !== $before->post_title || 'plain-home' !== $before->post_name || 'Plain body.' !== $before->post_content || $before != $GLOBALS['mdi_test_posts'][ $plain_id ] ) {
465489
$failures[] = 'plain docs import/re-import did not update the same source without content changes';
466490
}
467491
$fresh_plain_storage = new WP_Markdown_Storage( $plain_root );
@@ -482,7 +506,11 @@ static function ( string $content, array $context ): string {
482506
$GLOBALS['mdi_test_posts'][ $plain_id ]->post_date = $stored_date;
483507
$GLOBALS['mdi_test_posts'][ $plain_id ]->post_date_gmt = $stored_date;
484508
file_put_contents( $plain_path, "# Plain home\n\nUpdated body.\n" );
485-
WP_Markdown_CLI::import( $plain_options );
509+
$calls_before = $GLOBALS['mdi_insert_calls'];
510+
$changed_import = WP_Markdown_CLI::import( $plain_options );
511+
if ( 1 !== $changed_import['update_count'] || 0 !== $changed_import['unchanged_count'] || $calls_before + 1 !== $GLOBALS['mdi_insert_calls'] ) {
512+
$failures[] = 'one changed file did not produce exactly one write';
513+
}
486514
$updated = $GLOBALS['mdi_test_posts'][ $plain_id ];
487515
if ( $stored_date !== $updated->post_date || $stored_date !== $updated->post_date_gmt || 'Updated body.' !== $updated->post_content ) {
488516
$failures[] = 'dateless import reset the existing publish date or did not update content';
@@ -525,6 +553,45 @@ static function ( string $content, array $context ): string {
525553
}
526554
}
527555
}
556+
// Batch regression: changing one of two sources must leave the other untouched.
557+
$GLOBALS['mdi_test_posts'] = array();
558+
$GLOBALS['mdi_test_meta'] = array();
559+
$batch_root = $tmp_root . '/batch';
560+
mkdir( $batch_root . '/page', 0755, true );
561+
foreach ( array( 201, 202 ) as $id ) {
562+
file_put_contents( $batch_root . '/page/item-' . $id . '.md', "---\nid: $id\ntitle: Item $id\ntype: page\nslug: item-$id\n---\n\nBody $id.\n" );
563+
}
564+
$batch_options = array( 'path' => $batch_root, 'no_convert' => true );
565+
WP_Markdown_CLI::import( $batch_options );
566+
// WordPress returns numeric columns as strings from the database.
567+
foreach ( $GLOBALS['mdi_test_posts'] as $stored ) {
568+
foreach ( array( 'post_author', 'post_parent', 'menu_order', 'comment_count' ) as $field ) {
569+
$stored->$field = (string) $stored->$field;
570+
}
571+
}
572+
$calls_before = $GLOBALS['mdi_insert_calls'];
573+
foreach ( array( true, false ) as $dry_run ) {
574+
$batch = WP_Markdown_CLI::import( array_merge( $batch_options, array( 'dry_run' => $dry_run ) ) );
575+
if ( 0 !== $batch['update_count'] || 2 !== $batch['unchanged_count'] || $calls_before !== $GLOBALS['mdi_insert_calls'] ) {
576+
$failures[] = 'unchanged batch did not avoid all writes';
577+
}
578+
}
579+
file_put_contents( $batch_root . '/page/item-201.md', "---\nid: 201\ntitle: Changed\ntype: page\nslug: item-201\n---\n\nChanged body.\n" );
580+
foreach ( array( true, false ) as $dry_run ) {
581+
$batch = WP_Markdown_CLI::import( array_merge( $batch_options, array( 'dry_run' => $dry_run ) ) );
582+
if ( 1 !== $batch['update_count'] || 1 !== $batch['unchanged_count'] ) {
583+
$failures[] = 'one changed source did not isolate the batch update';
584+
}
585+
}
586+
if ( $calls_before + 1 !== $GLOBALS['mdi_insert_calls'] ) {
587+
$failures[] = 'one changed source did not write exactly once';
588+
}
589+
// Managed metadata drift must still trigger a write even when fields match.
590+
$GLOBALS['mdi_test_meta'][202]['_markdown_source_hash'] = 'stale';
591+
$batch = WP_Markdown_CLI::import( $batch_options );
592+
if ( 1 !== $batch['update_count'] || 1 !== $batch['unchanged_count'] ) {
593+
$failures[] = 'managed source metadata drift was ignored';
594+
}
528595
mdi_mysql_import_export_rm_rf( $tmp_root );
529596

530597
if ( ! empty( $failures ) ) {

0 commit comments

Comments
 (0)