mirror of
https://github.com/phpbb/phpbb.git
synced 2025-04-22 16:51:56 +02:00
[ticket/14561] Add a progress bar to reclean command
PHPBB3-14561
This commit is contained in:
parent
4b789c0418
commit
afb69d7cd2
@ -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.',
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user