Skip to content

Commit 590377d

Browse files
Copilotswissspidy
andauthored
Fix --skip-duplicates to use --file_name for duplicate check; add @require-wp-5.3 Behat scenario
Agent-Logs-Url: https://github.com/wp-cli/media-command/sessions/43c9f132-60d4-48c3-93e9-1e252883fa46 Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 433247d commit 590377d

2 files changed

Lines changed: 71 additions & 4 deletions

File tree

features/media-import.feature

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,3 +430,54 @@ Feature: Manage WordPress attachments
430430
Success: Imported 1 of 2 items (1 skipped).
431431
"""
432432
And the return code should be 0
433+
434+
@require-wp-5.3
435+
Scenario: Skip importing a file that was stored with a scaled suffix by WP 5.3+
436+
Given download:
437+
| path | url |
438+
| {CACHE_DIR}/large-image.jpg | http://wp-cli.github.io/behat-data/large-image.jpg |
439+
And I run `wp option update uploads_use_yearmonth_folders 0`
440+
441+
When I run `wp media import {CACHE_DIR}/large-image.jpg --porcelain`
442+
Then save STDOUT as {ATTACHMENT_ID}
443+
And STDOUT should not be empty
444+
And the wp-content/uploads/large-image-scaled.jpg file should exist
445+
446+
When I run `wp media import {CACHE_DIR}/large-image.jpg --skip-duplicates`
447+
Then STDOUT should contain:
448+
"""
449+
Skipped importing file
450+
"""
451+
And STDOUT should contain:
452+
"""
453+
already exists as attachment ID {ATTACHMENT_ID}
454+
"""
455+
And STDOUT should contain:
456+
"""
457+
Success: Imported 0 of 1 items (1 skipped).
458+
"""
459+
And the return code should be 0
460+
461+
Scenario: Skip importing a file when a custom --file_name was used in the original import
462+
Given download:
463+
| path | url |
464+
| {CACHE_DIR}/large-image.jpg | http://wp-cli.github.io/behat-data/large-image.jpg |
465+
466+
When I run `wp media import {CACHE_DIR}/large-image.jpg --file_name=custom --porcelain`
467+
Then save STDOUT as {ATTACHMENT_ID}
468+
And STDOUT should not be empty
469+
470+
When I run `wp media import {CACHE_DIR}/large-image.jpg --file_name=custom --skip-duplicates`
471+
Then STDOUT should contain:
472+
"""
473+
Skipped importing file
474+
"""
475+
And STDOUT should contain:
476+
"""
477+
already exists as attachment ID {ATTACHMENT_ID}
478+
"""
479+
And STDOUT should contain:
480+
"""
481+
Success: Imported 0 of 1 items (1 skipped).
482+
"""
483+
And the return code should be 0

src/Media_Command.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -607,8 +607,16 @@ public function import( $args, $assoc_args = array() ) {
607607
++$errors;
608608
continue;
609609
}
610+
$src_basename = Path::basename( $file );
610611
if ( Utils\get_flag_value( $assoc_args, 'skip-duplicates' ) ) {
611-
$existing = $this->find_duplicate_attachment( Path::basename( $file ) );
612+
$check_basename = $src_basename;
613+
if ( ! empty( $assoc_args['file_name'] ) ) {
614+
$resolved_name = $this->get_image_name( $src_basename, $assoc_args['file_name'] );
615+
if ( ! empty( $resolved_name ) ) {
616+
$check_basename = $resolved_name;
617+
}
618+
}
619+
$existing = $this->find_duplicate_attachment( $check_basename );
612620
if ( false !== $existing ) {
613621
if ( ! $porcelain ) {
614622
WP_CLI::log( "Skipped importing file '$orig_filename'. Reason: already exists as attachment ID $existing." );
@@ -622,14 +630,22 @@ public function import( $args, $assoc_args = array() ) {
622630
} else {
623631
$tempfile = $this->make_copy( $file );
624632
}
625-
$name = Path::basename( $file );
633+
$name = $src_basename;
626634

627635
if ( Utils\get_flag_value( $assoc_args, 'preserve-filetime' ) ) {
628636
$file_time = @filemtime( $file );
629637
}
630638
} else {
639+
$src_basename = (string) explode( '?', Path::basename( $file ), 2 )[0];
631640
if ( Utils\get_flag_value( $assoc_args, 'skip-duplicates' ) ) {
632-
$existing = $this->find_duplicate_attachment( (string) explode( '?', Path::basename( $file ), 2 )[0] );
641+
$check_basename = $src_basename;
642+
if ( ! empty( $assoc_args['file_name'] ) ) {
643+
$resolved_name = $this->get_image_name( $src_basename, $assoc_args['file_name'] );
644+
if ( ! empty( $resolved_name ) ) {
645+
$check_basename = $resolved_name;
646+
}
647+
}
648+
$existing = $this->find_duplicate_attachment( $check_basename );
633649
if ( false !== $existing ) {
634650
if ( ! $porcelain ) {
635651
WP_CLI::log( "Skipped importing file '$orig_filename'. Reason: already exists as attachment ID $existing." );
@@ -650,7 +666,7 @@ public function import( $args, $assoc_args = array() ) {
650666
++$errors;
651667
continue;
652668
}
653-
$name = (string) explode( '?', Path::basename( $file ), 2 )[0];
669+
$name = $src_basename;
654670
}
655671
}
656672

0 commit comments

Comments
 (0)