Skip to content

Commit ecc0962

Browse files
authored
fix(export): create nested profile paths and report failed writes (#465) (#470)
1 parent f6925f4 commit ecc0962

4 files changed

Lines changed: 71 additions & 5 deletions

File tree

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -570,9 +570,10 @@ public static function export( array $options ): array {
570570
$storage->set_meta_resolver( array( self::class, 'post_meta_rows' ) );
571571
$storage->set_terms_resolver( array( self::class, 'post_term_rows' ) );
572572

573-
$written = array();
574-
$skipped = array();
575-
$sample = array();
573+
$written = array();
574+
$skipped = array();
575+
$sample = array();
576+
$write_failed = false;
576577
foreach ( $posts as $post ) {
577578
$expected = self::expected_export_path( $post, $layout_profile );
578579
if ( count( $sample ) < 10 ) {
@@ -616,11 +617,18 @@ public static function export( array $options ): array {
616617
update_post_meta( $filtered_export_post_id, self::SOURCE_IDENTITY_META, (string) ( $filtered_export_post->_source_identity ?? $relative ) );
617618
update_post_meta( $filtered_export_post_id, self::SOURCE_HASH_META, self::source_hash( $file ) );
618619
}
620+
} else {
621+
$write_failed = true;
622+
$skipped[] = array(
623+
'id' => (int) ( $post->ID ?? 0 ),
624+
'path' => $expected,
625+
'reason' => 'Markdown storage could not write the post.',
626+
);
619627
}
620628
}
621629

622630
return array(
623-
'success' => true,
631+
'success' => ! $write_failed,
624632
'mode' => $dry_run ? 'dry-run' : 'apply',
625633
'path' => $content_dir,
626634
'post_types' => $post_types,

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,23 @@ private function profile_absolute_path( string $relative, bool $allow_missing =
936936
$root = realpath( $this->content_dir );
937937
}
938938
$path = rtrim( $this->content_dir, '/' ) . '/' . $relative;
939-
$target = realpath( $allow_missing ? dirname( $path ) : $path );
939+
$ancestor = $allow_missing ? dirname( $path ) : $path;
940+
if ( $allow_missing ) {
941+
// Validate before mkdir: missing parents are safe only when their
942+
// nearest existing ancestor resolves inside the content root.
943+
// Stop at dangling symlinks too, so they fail closed below.
944+
while ( ! file_exists( $ancestor ) && ! is_link( $ancestor ) ) {
945+
$parent = dirname( $ancestor );
946+
if ( $parent === $ancestor ) {
947+
return null;
948+
}
949+
$ancestor = $parent;
950+
}
951+
if ( ! is_dir( $ancestor ) ) {
952+
return null;
953+
}
954+
}
955+
$target = realpath( $ancestor );
940956
if ( false === $target || ( $target !== $root && ! str_starts_with( $target, $root . '/' ) ) ) {
941957
return null;
942958
}

‎tests/smoke-content-layout-profiles.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,23 @@ function apply_filters( $hook_name, $value, ...$args ) {
101101
$failures[] = 'fixture profile did not export page /foo to content/foo.md';
102102
}
103103

104+
$nested_path = 'plugins/foo/docs/user/page.md';
105+
$nested = $storage->write_post( (object) array( 'ID' => 20, 'post_type' => 'page', 'post_name' => 'plugins/foo/docs/user/page', 'post_content' => 'Nested body' ) );
106+
if ( $root . '/' . $nested_path !== $nested || ! is_file( $root . '/' . $nested_path ) || ! str_contains( (string) file_get_contents( $root . '/' . $nested_path ), 'Nested body' ) ) {
107+
$failures[] = 'profile write did not create missing nested target directories';
108+
}
109+
if ( @symlink( $outside, $root . '/escape-dir' ) ) {
110+
$escaped = $storage->write_post( (object) array( 'ID' => 21, 'post_name' => 'escape-dir/missing/deep/page' ) );
111+
if ( false !== $escaped || file_exists( $outside . '/missing' ) ) {
112+
$failures[] = 'nested profile write followed an outside ancestor symlink';
113+
}
114+
}
115+
if ( @symlink( $outside . '/nonexistent', $root . '/dangling-dir' ) ) {
116+
if ( false !== $storage->write_post( (object) array( 'ID' => 22, 'post_name' => 'dangling-dir/missing/page' ) ) || file_exists( $outside . '/nonexistent' ) ) {
117+
$failures[] = 'nested profile write accepted a dangling ancestor symlink';
118+
}
119+
}
120+
104121
$duplicate = $storage->write_post( (object) array( 'ID' => 2, 'post_type' => 'page', 'post_name' => 'foo', 'post_status' => 'publish', 'post_title' => 'Duplicate', 'post_content' => 'Duplicate' ) );
105122
if ( false !== $duplicate ) {
106123
$failures[] = 'duplicate route identity was not rejected';

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,31 @@ static function ( string $content, array $context ): string {
371371
$failures[] = 'export CLI did not route --content-dir to the markdown content root';
372372
}
373373

374+
require_once dirname( __DIR__ ) . '/inc/class-wp-markdown-content-layout-profiles.php';
375+
markdown_db_register_content_layout_profile(
376+
'nested-export-fixture',
377+
array(
378+
'enumerate' => static fn(): array => array( 'plugins/foo/docs/user/home.md' ),
379+
'map_source' => static fn(): array => array( 'post_type' => 'page', 'post_name' => 'home' ),
380+
'path_for_post' => static fn(): string => 'plugins/foo/docs/user/home.md',
381+
)
382+
);
383+
$nested_options = array( 'path' => $tmp_root . '/nested-export', 'post_types' => 'page', 'layout_profile' => 'nested-export-fixture' );
384+
$nested_export = WP_Markdown_CLI::export( $nested_options );
385+
if ( true !== $nested_export['success'] || 1 !== $nested_export['written_count'] || 0 !== $nested_export['skipped_count'] || ! is_file( $nested_options['path'] . '/plugins/foo/docs/user/home.md' ) ) {
386+
$failures[] = 'nested layout export did not write into a fresh root';
387+
}
388+
// A conflicting route must be accounted for rather than silently disappearing.
389+
file_put_contents( $nested_options['path'] . '/plugins/foo/docs/user/home.md', "---\nid: 999\n---\n\nOther post\n" );
390+
$failed_export = WP_Markdown_CLI::export( $nested_options );
391+
if ( false !== $failed_export['success'] || 1 !== $failed_export['candidate_count'] || 0 !== $failed_export['written_count'] || 1 !== $failed_export['skipped_count'] || 17 !== ( $failed_export['skipped'][0]['id'] ?? null ) || 'plugins/foo/docs/user/home.md' !== ( $failed_export['skipped'][0]['path'] ?? null ) || empty( $failed_export['skipped'][0]['reason'] ) ) {
392+
$failures[] = 'failed export write was not reported as a skip with unsuccessful status';
393+
}
394+
$dry_export = WP_Markdown_CLI::export( array_merge( $nested_options, array( 'dry_run' => true ) ) );
395+
if ( true !== $dry_export['success'] || 0 !== $dry_export['written_count'] || 0 !== $dry_export['skipped_count'] ) {
396+
$failures[] = 'dry-run export was incorrectly reported as a failed write';
397+
}
398+
374399
mdi_mysql_import_export_rm_rf( $tmp_root );
375400

376401
if ( ! empty( $failures ) ) {

0 commit comments

Comments
 (0)