From 16f9b4630cfc3c6247894ac82ac6b95577075753 Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Sat, 26 Mar 2016 12:38:07 -0700 Subject: [ticket/14561] Reclean usernames command PHPBB3-14561 --- phpBB/phpbb/console/command/user/reclean.php | 125 +++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 phpBB/phpbb/console/command/user/reclean.php (limited to 'phpBB/phpbb/console/command/user/reclean.php') diff --git a/phpBB/phpbb/console/command/user/reclean.php b/phpBB/phpbb/console/command/user/reclean.php new file mode 100644 index 0000000000..c53d766cce --- /dev/null +++ b/phpBB/phpbb/console/command/user/reclean.php @@ -0,0 +1,125 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +namespace phpbb\console\command\user; + +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Question\ConfirmationQuestion; +use Symfony\Component\Console\Question\Question; +use Symfony\Component\Console\Style\SymfonyStyle; + +class reclean extends \phpbb\console\command\command +{ + /** @var \phpbb\db\driver\driver_interface */ + protected $db; + + /** @var \phpbb\language\language */ + protected $language; + + /** @var int A count of the number of re-cleaned user names */ + protected $processed; + + /** + * Construct method + * + * @param \phpbb\user $user + * @param \phpbb\db\driver\driver_interface $db + * @param \phpbb\language\language $language + */ + public function __construct(\phpbb\user $user, \phpbb\db\driver\driver_interface $db, \phpbb\language\language $language) + { + $this->db = $db; + $this->language = $language; + + parent::__construct($user); + } + + /** + * Sets the command name and description + * + * @return null + */ + protected function configure() + { + $this + ->setName('user:reclean') + ->setDescription($this->language->lang('CLI_DESCRIPTION_USER_RECLEAN')) + ; + } + + /** + * Executes the command user:reclean + * + * Cleans user names that are unclean. + * + * @param InputInterface $input The input stream used to get the options + * @param OutputInterface $output The output stream, used to print messages + * + * @return int 0 if all is well, 1 if any errors occurred + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $this->processed = 0; + + $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)); + return 0; + } + + /** + * Re-clean user names + * Only user names that are unclean will be re-cleaned + * + * @param int $start An offset index + * @return bool|int Return the next offset index or true if all records have been processed. + */ + protected function reclean_usernames($start = 0) + { + $limit = 500; + $i = 0; + + $this->db->sql_transaction('begin'); + + $sql = 'SELECT user_id, username, username_clean FROM ' . USERS_TABLE; + $result = $this->db->sql_query_limit($sql, $limit, $start); + while ($row = $this->db->sql_fetchrow($result)) + { + $i++; + $username_clean = $this->db->sql_escape(utf8_clean_string($row['username'])); + + if ($username_clean != $row['username_clean']) + { + $sql = 'UPDATE ' . USERS_TABLE . " + SET username_clean = '$username_clean' + WHERE user_id = {$row['user_id']}"; + $this->db->sql_query($sql); + + $this->processed++; + } + } + $this->db->sql_freeresult($result); + + $this->db->sql_transaction('commit'); + + return ($i < $limit) ? true : $start + $i; + } +} -- cgit v1.2.1 From 13f365916caf9b01312da3717359316faa576521 Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Sat, 26 Mar 2016 14:50:45 -0700 Subject: [ticket/14561] Remove unused use statements PHPBB3-14561 --- phpBB/phpbb/console/command/user/reclean.php | 4 ---- 1 file changed, 4 deletions(-) (limited to 'phpBB/phpbb/console/command/user/reclean.php') diff --git a/phpBB/phpbb/console/command/user/reclean.php b/phpBB/phpbb/console/command/user/reclean.php index c53d766cce..cd5fc60a05 100644 --- a/phpBB/phpbb/console/command/user/reclean.php +++ b/phpBB/phpbb/console/command/user/reclean.php @@ -13,12 +13,8 @@ namespace phpbb\console\command\user; -use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Question\ConfirmationQuestion; -use Symfony\Component\Console\Question\Question; use Symfony\Component\Console\Style\SymfonyStyle; class reclean extends \phpbb\console\command\command -- cgit v1.2.1 From aee3eec439b39ac1f8aa79582b302a499a23acc0 Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Sun, 27 Mar 2016 09:31:47 -0700 Subject: [ticket/14561] Import classes with use statements PHPBB3-14561 --- phpBB/phpbb/console/command/user/reclean.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'phpBB/phpbb/console/command/user/reclean.php') diff --git a/phpBB/phpbb/console/command/user/reclean.php b/phpBB/phpbb/console/command/user/reclean.php index cd5fc60a05..e2f95c16d8 100644 --- a/phpBB/phpbb/console/command/user/reclean.php +++ b/phpBB/phpbb/console/command/user/reclean.php @@ -13,16 +13,20 @@ namespace phpbb\console\command\user; +use phpbb\console\command\command; +use phpbb\db\driver\driver_interface; +use phpbb\language\language; +use phpbb\user; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; -class reclean extends \phpbb\console\command\command +class reclean extends command { - /** @var \phpbb\db\driver\driver_interface */ + /** @var driver_interface */ protected $db; - /** @var \phpbb\language\language */ + /** @var language */ protected $language; /** @var int A count of the number of re-cleaned user names */ @@ -31,11 +35,11 @@ class reclean extends \phpbb\console\command\command /** * Construct method * - * @param \phpbb\user $user - * @param \phpbb\db\driver\driver_interface $db - * @param \phpbb\language\language $language + * @param user $user + * @param driver_interface $db + * @param language $language */ - public function __construct(\phpbb\user $user, \phpbb\db\driver\driver_interface $db, \phpbb\language\language $language) + public function __construct(user $user, driver_interface $db, language $language) { $this->db = $db; $this->language = $language; -- cgit v1.2.1 From ed0f151d863d7449d73336b3697e37259812215e Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Sun, 27 Mar 2016 09:47:45 -0700 Subject: [ticket/14561] Add extra help explaining reclean command PHPBB3-14561 --- phpBB/phpbb/console/command/user/reclean.php | 1 + 1 file changed, 1 insertion(+) (limited to 'phpBB/phpbb/console/command/user/reclean.php') diff --git a/phpBB/phpbb/console/command/user/reclean.php b/phpBB/phpbb/console/command/user/reclean.php index e2f95c16d8..ba8a638e7b 100644 --- a/phpBB/phpbb/console/command/user/reclean.php +++ b/phpBB/phpbb/console/command/user/reclean.php @@ -57,6 +57,7 @@ class reclean extends command $this ->setName('user:reclean') ->setDescription($this->language->lang('CLI_DESCRIPTION_USER_RECLEAN')) + ->setHelp($this->language->lang('CLI_HELP_USER_RECLEAN')) ; } -- cgit v1.2.1 From afb69d7cd280df65b22b1a338d3023aebf2e3f0c Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Sun, 27 Mar 2016 11:25:19 -0700 Subject: [ticket/14561] Add a progress bar to reclean command PHPBB3-14561 --- phpBB/phpbb/console/command/user/reclean.php | 76 +++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 2 deletions(-) (limited to 'phpBB/phpbb/console/command/user/reclean.php') 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('[%percent:3s%%] %message%'); + $progress->setOverwrite(false); + } + else if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) + { + $progress->setFormat('[%current:s%/%max:s%][%elapsed%/%estimated%][%memory%] %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; + } } -- cgit v1.2.1 From 2b90591a317fd75c7c8c4cf690d7209935f3e810 Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Mon, 28 Mar 2016 15:26:56 -0700 Subject: [ticket/14561] Small change to progress bar output PHPBB3-14561 --- phpBB/phpbb/console/command/user/reclean.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/phpbb/console/command/user/reclean.php') diff --git a/phpBB/phpbb/console/command/user/reclean.php b/phpBB/phpbb/console/command/user/reclean.php index 20c2816be5..e298c285be 100644 --- a/phpBB/phpbb/console/command/user/reclean.php +++ b/phpBB/phpbb/console/command/user/reclean.php @@ -167,7 +167,7 @@ class reclean extends command $io->newLine(2); $progress->setFormat( " %current:s%/%max:s% %bar% %percent:3s%%\n" . - " %message% %elapsed:6s%/%estimated:-6s% %memory:6s%\n"); + " %elapsed:6s%/%estimated:-6s% %memory:6s%\n"); $progress->setBarWidth(60); } -- cgit v1.2.1