Skip to content

wp import --authors=<file>.csv fails with DivisionByZeroError instead of creating the mapping file #111

Open

Description

@softglazee

Bug Report

Describe the current, buggy behavior

The help for --authors says the command will "read author mapping associations from a CSV, or create a CSV for editing if the file path doesn't exist".

When the CSV file does not exist yet, wp import now stops with a fatal error instead of writing the file:

$ wp import export.xml --authors=mapping.csv
Starting the import process...
Fatal error: Uncaught DivisionByZeroError: Division by zero in phar:///tmp/wp.phar/vendor/wp-cli/import-command/src/Import_Command.php:732
Stack trace:
#0 phar:///tmp/wp.phar/vendor/wp-cli/import-command/src/Import_Command.php(617): Import_Command->suggest_user('admin', 'admin@example.c...')
#1 phar:///tmp/wp.phar/vendor/wp-cli/import-command/src/Import_Command.php(566): Import_Command->create_author_mapping_file('mapping.csv', Array)
#2 phar:///tmp/wp.phar/vendor/wp-cli/import-command/src/Import_Command.php(290): Import_Command->process_author_mapping('mapping.csv', Array)
...
$ echo $?
1

mapping.csv is created, but it is empty:

$ wc -c mapping.csv
0 mapping.csv

If I run the same command again, it reads the empty file and imports everything without any author mapping. There is no warning.

The cause is in suggest_user(). It loads the site users only when ! isset( $this->blog_users ). Since a50c132 (#94) the property is declared as private $blog_users = [];, so isset() is always true. get_users() never runs, the loop over the users never runs, and array_sum( $shortestavg ) / count( $shortestavg ) divides by zero.

WP-CLI 2.12.0 (the current stable phar, saved as /tmp/stable.phar) still works on the same site. It writes the file and stops as expected:

$ php /tmp/stable.phar import /tmp/repro/export.xml --authors=/tmp/repro/mapping.csv
Starting the import process...
Error: Please update author mapping file before continuing: /tmp/repro/mapping.csv
$ cat /tmp/repro/mapping.csv
old_user_login,new_user_login
admin,admin

Describe how other contributors can replicate this bug

  • Use a site with at least one post, for example a fresh install with "Hello world!".
  • wp plugin install wordpress-importer --activate
  • wp export --filename_format=export.xml
  • wp import export.xml --authors=mapping.csv (the file mapping.csv must not exist yet)
  • The command ends with Fatal error: Uncaught DivisionByZeroError: Division by zero and leaves an empty mapping.csv.

Describe what you would expect as the correct outcome

The mapping file is written with a suggestion for each author, and the command stops so I can edit it:

$ wp import export.xml --authors=mapping.csv
Starting the import process...
Error: Please update author mapping file before continuing: mapping.csv
$ cat mapping.csv
old_user_login,new_user_login
admin,admin

Let us know what environment you are running this on

OS:	Linux 6.18.33.2-microsoft-standard-WSL2 #1 SMP PREEMPT_DYNAMIC Thu Jun 18 21:54:43 UTC 2026 x86_64
Shell:	
PHP binary:	/usr/local/bin/php
PHP version:	8.5.9
PHP memory limit:	-1
php.ini used:	/usr/local/etc/php/php-cli.ini
MySQL binary:	/usr/bin/mariadb
MySQL version:	mariadb from 11.8.9-MariaDB, client 15.2 for Linux (x86_64) using readline 5.1
SQL modes:	
WP-CLI root dir:	phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:	phar://wp-cli.phar/vendor
WP_CLI phar path:	phar:///tmp/wp.phar
WP-CLI packages dir:	/tmp/.wp-cli/packages/
WP-CLI cache dir:	/tmp/.wp-cli/cache
WP-CLI global config:	
WP-CLI project config:	
WP-CLI version:	3.0.0-alpha-836ccac

WordPress 7.2-beta1-64691 (nightly), WordPress Importer 0.9.7, WP_DEBUG true. In the commands above, wp is the nightly phar run as php -d error_reporting=-1 -d display_errors=stderr /tmp/wp.phar. I also reproduced it with a Behat scenario on the import-command main branch (60399a0).

Provide a possible solution

  • Declare $blog_users without a default value, so it is null until suggest_user() loads the users.
  • Only compare with the average when $shortestavg is not empty. Otherwise the same error happens when the first site user matches the imported login exactly but has a different email: the loop breaks before anything is added to $shortestavg.

I have a pull request with both changes and a Behat scenario ready.

Provide additional context/Screenshots

  • The isset() check is old. The property declaration came with PHPStan level 9 #94 (PHPStan level 9), which is in v2.0.15 and later, including v3.0.0. WP-CLI 2.12.0 bundles an older version and shows a "Creation of dynamic property Import_Command::$blog_users" deprecation on PHP 8.5 instead of the fatal error.
  • No Behat scenario uses --authors=<file>.csv today, so the tests did not catch it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions