diff options
author | Matt Friedman <maf675@gmail.com> | 2016-03-27 11:25:19 -0700 |
---|---|---|
committer | Matt Friedman <maf675@gmail.com> | 2016-03-31 15:07:59 -0700 |
commit | afb69d7cd280df65b22b1a338d3023aebf2e3f0c (patch) | |
tree | 00a05d14f1112c7eefa1dc21cb5e62cb047f78c6 | |
parent | 4b789c041844396f3a5e6a51142c45c13d2edd59 (diff) | |
download | forums-afb69d7cd280df65b22b1a338d3023aebf2e3f0c.tar forums-afb69d7cd280df65b22b1a338d3023aebf2e3f0c.tar.gz forums-afb69d7cd280df65b22b1a338d3023aebf2e3f0c.tar.bz2 forums-afb69d7cd280df65b22b1a338d3023aebf2e3f0c.tar.xz forums-afb69d7cd280df65b22b1a338d3023aebf2e3f0c.zip |
[ticket/14561] Add a progress bar to reclean command
PHPBB3-14561
-rw-r--r-- | phpBB/language/en/cli.php | 3 | ||||
-rw-r--r-- | phpBB/phpbb/console/command/user/reclean.php | 76 | ||||
-rw-r--r-- | tests/console/user/reclean_test.php | 7 |
3 files changed, 78 insertions, 8 deletions
diff --git a/phpBB/language/en/cli.php b/phpBB/language/en/cli.php index 872aa464b5..db4b5f9ec6 100644 --- a/phpBB/language/en/cli.php +++ b/phpBB/language/en/cli.php @@ -136,7 +136,8 @@ $lang = array_merge($lang, array( 'CLI_USER_ADD_SUCCESS' => 'Successfully added user %s.', 'CLI_USER_DELETE_CONFIRM' => 'Are you sure you want to delete ā%sā? [y/N]', - 'CLI_USER_RECLEAN_SUCCESS' => [ + 'CLI_USER_RECLEAN_START' => 'Re-cleaning usernames', + 'CLI_USER_RECLEAN_DONE' => [ 0 => 'Re-cleaning complete. No usernames needed to be cleaned.', 1 => 'Re-cleaning complete. %d username was cleaned.', 2 => 'Re-cleaning complete. %d usernames were cleaned.', diff --git a/phpBB/phpbb/console/command/user/reclean.php b/phpBB/phpbb/console/command/user/reclean.php index ba8a638e7b..20c2816be5 100644 --- a/phpBB/phpbb/console/command/user/reclean.php +++ b/phpBB/phpbb/console/command/user/reclean.php @@ -17,6 +17,7 @@ use phpbb\console\command\command; use phpbb\db\driver\driver_interface; use phpbb\language\language; use phpbb\user; +use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; @@ -32,6 +33,9 @@ class reclean extends command /** @var int A count of the number of re-cleaned user names */ protected $processed; + /** @var ProgressBar */ + protected $progress; + /** * Construct method * @@ -73,16 +77,27 @@ class reclean extends command */ protected function execute(InputInterface $input, OutputInterface $output) { + $io = new SymfonyStyle($input, $output); + + $io->section($this->language->lang('CLI_USER_RECLEAN_START')); + $this->processed = 0; + $this->progress = $this->create_progress_bar($this->get_count(), $io, $output); + $this->progress->setMessage($this->language->lang('CLI_USER_RECLEAN_START')); + $this->progress->start(); + $stage = 0; while ($stage !== true) { $stage = $this->reclean_usernames($stage); } - $io = new SymfonyStyle($input, $output); - $io->success($this->language->lang('CLI_USER_RECLEAN_SUCCESS', $this->processed)); + $this->progress->finish(); + + $io->newLine(2); + $io->success($this->language->lang('CLI_USER_RECLEAN_DONE', $this->processed)); + return 0; } @@ -116,6 +131,8 @@ class reclean extends command $this->processed++; } + + $this->progress->advance(); } $this->db->sql_freeresult($result); @@ -123,4 +140,59 @@ class reclean extends command return ($i < $limit) ? true : $start + $i; } + + /** + * Create a styled progress bar + * + * @param integer $max Max value for the progress bar + * @param SymfonyStyle $io + * @param OutputInterface $output The output stream, used to print messages + * @return ProgressBar + */ + protected function create_progress_bar($max, SymfonyStyle $io, OutputInterface $output) + { + $progress = $io->createProgressBar($max); + if ($output->getVerbosity() === OutputInterface::VERBOSITY_VERBOSE) + { + $progress->setFormat('<info>[%percent:3s%%]</info> %message%'); + $progress->setOverwrite(false); + } + else if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) + { + $progress->setFormat('<info>[%current:s%/%max:s%]</info><comment>[%elapsed%/%estimated%][%memory%]</comment> %message%'); + $progress->setOverwrite(false); + } + else + { + $io->newLine(2); + $progress->setFormat( + " %current:s%/%max:s% %bar% %percent:3s%%\n" . + " %message% %elapsed:6s%/%estimated:-6s% %memory:6s%\n"); + $progress->setBarWidth(60); + } + + if (!defined('PHP_WINDOWS_VERSION_BUILD')) + { + $progress->setEmptyBarCharacter('ā'); // light shade character \u2591 + $progress->setProgressCharacter(''); + $progress->setBarCharacter('ā'); // dark shade character \u2593 + } + + return $progress; + } + + /** + * Get the count of users in the database + * + * @return int + */ + protected function get_count() + { + $sql = 'SELECT COUNT(user_id) AS count FROM ' . USERS_TABLE; + $result = $this->db->sql_query($sql); + $count = (int) $this->db->sql_fetchfield('count'); + $this->db->sql_freeresult($result); + + return $count; + } } diff --git a/tests/console/user/reclean_test.php b/tests/console/user/reclean_test.php index 2b28484084..1bf0b8ef5a 100644 --- a/tests/console/user/reclean_test.php +++ b/tests/console/user/reclean_test.php @@ -38,11 +38,8 @@ class phpbb_console_user_reclean_test extends phpbb_console_user_base { $command_tester = $this->get_command_tester(); - $command_tester->execute(array( - 'command' => $this->command_name, - )); - - $this->assertContains('CLI_USER_RECLEAN_SUCCESS', $command_tester->getDisplay()); + $exit_status = $command_tester->execute(array('command' => $this->command_name)); + $this->assertSame(0, $exit_status); $result = $this->db->sql_query('SELECT user_id FROM ' . USERS_TABLE . " WHERE username_clean = 'test unclean'"); $row = $this->db->sql_fetchrow($result); |