Skip to content

Commit e8d715d

Browse files
Copilotswissspidy
andauthored
Limit sitename length in export filename to prevent filesystem errors (#133)
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 1d7a7f9 commit e8d715d

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

features/export.feature

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,20 @@ Feature: Export content.
648648
000.xml
649649
"""
650650

651+
Scenario: Export a site with a very long site name produces a filename within a reasonable length
652+
Given a WP install
653+
And I run `wp option update blogname 'This is a very long site name that exceeds fifty characters and should be truncated in the export filename'`
654+
655+
When I run `wp export`
656+
Then STDOUT should contain:
657+
"""
658+
thisisaverylongsitenamethatexceedsfiftycharactersa.wordpress.
659+
"""
660+
And STDOUT should not contain:
661+
"""
662+
thisisaverylongsitenamethatexceedsfiftycharactersandshouldbetruncated
663+
"""
664+
651665
@require-wp-5.2 @require-mysql
652666
Scenario: Export a site and skip the comments
653667
Given a WP install

src/Export_Command.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
*/
2121
class Export_Command extends WP_CLI_Command {
2222

23+
/**
24+
* Maximum length of the site name component in the export filename.
25+
*/
26+
const MAX_FILENAME_SITENAME_LENGTH = 50;
27+
2328
/**
2429
* Initialize the array of arguments that will be eventually be passed to export_wp.
2530
*
@@ -222,6 +227,7 @@ private static function get_filename_template( $filename_format ) {
222227
if ( empty( $sitename ) ) {
223228
$sitename = 'site';
224229
}
230+
$sitename = function_exists( 'mb_substr' ) ? mb_substr( $sitename, 0, self::MAX_FILENAME_SITENAME_LENGTH ) : substr( $sitename, 0, self::MAX_FILENAME_SITENAME_LENGTH );
225231
return str_replace( [ '{site}', '{date}', '{n}' ], [ $sitename, date( 'Y-m-d' ), '%03d' ], $filename_format ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
226232
}
227233

0 commit comments

Comments
 (0)