From 7d505b3dfab9ac499b6a27327eabcb4a251fbaf7 Mon Sep 17 00:00:00 2001 From: softglazee Date: Sat, 24 Oct 2026 07:35:47 +0500 Subject: [PATCH] Fix author mapping file creation failing with DivisionByZeroError When `--authors` points to a CSV file that does not exist yet, suggest_user() is used to fill in suggested users. Since the $blog_users property got an empty array as default value, the isset() check in suggest_user() is always true, so the site users were never loaded and the average check divided by zero. Load the users again on first use, and skip the average check when no distances were collected, which also happens when the first user is an exact login match. --- features/import.feature | 46 +++++++++++++++++++++++++++++++++++++++++ src/Import_Command.php | 11 ++++++++-- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/features/import.feature b/features/import.feature index 9381e0e3..d366e12e 100644 --- a/features/import.feature +++ b/features/import.feature @@ -583,3 +583,49 @@ Feature: Import content. 1 """ + @require-wp-5.2 @require-mysql + Scenario: Create an author mapping file and import with it + Given a WP install + And I run `wp plugin install --activate wordpress-importer` + And I run `wp site empty --yes` + And I run `wp user create jdoe jdoe@example.com --porcelain` + And save STDOUT as {JDOE_ID} + And I run `wp post create --post_title='Post by admin' --post_author=1 --post_status=publish` + And I run `wp post create --post_title='Post by jdoe' --post_author={JDOE_ID} --post_status=publish` + + When I run `wp export --filename_format=authors.{n}.xml` + Then save STDOUT 'Writing to file %s' as {EXPORT_FILE} + + When I run `wp site empty --yes` + And I run `wp user update 1 --user_email=admin@example.org --skip-email` + And I run `wp user update jdoe --user_email=jdoe@example.org --skip-email` + And I try `wp import {EXPORT_FILE} --authors=mapping.csv` + Then STDERR should be: + """ + Error: Please update author mapping file before continuing: mapping.csv + """ + And the return code should be 1 + And the mapping.csv file should contain: + """ + old_user_login,new_user_login + """ + And the mapping.csv file should contain: + """ + admin,admin + """ + And the mapping.csv file should contain: + """ + jdoe,jdoe + """ + + When I run `wp import {EXPORT_FILE} --authors=mapping.csv` + Then STDOUT should contain: + """ + Success: Finished importing from + """ + + When I run `wp post list --author={JDOE_ID} --field=post_title` + Then STDOUT should be: + """ + Post by jdoe + """ diff --git a/src/Import_Command.php b/src/Import_Command.php index d0f75dcb..1e37610d 100644 --- a/src/Import_Command.php +++ b/src/Import_Command.php @@ -2,7 +2,14 @@ class Import_Command extends WP_CLI_Command { - private $blog_users = []; + /** + * Users of the current site, loaded on first use by suggest_user(). + * + * Must stay null until then, as suggest_user() checks it with isset(). + * + * @var \WP_User[]|null + */ + private $blog_users; public $processed_posts = []; @@ -729,7 +736,7 @@ private function suggest_user( $author_user_login, $author_user_email = '' ) { $shortestavg[] = $lev; } // in case all usernames have a common pattern - if ( $shortest > ( array_sum( $shortestavg ) / count( $shortestavg ) ) ) { + if ( [] !== $shortestavg && $shortest > ( array_sum( $shortestavg ) / count( $shortestavg ) ) ) { return ''; }