Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
44 / 44 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| reclean | |
100.00% |
44 / 44 |
|
100.00% |
5 / 5 |
9 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| configure | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
2 | |||
| reclean_usernames | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
4 | |||
| get_count | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * |
| 4 | * This file is part of the phpBB Forum Software package. |
| 5 | * |
| 6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
| 7 | * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | * |
| 9 | * For full copyright and license information, please see |
| 10 | * the docs/CREDITS.txt file. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | namespace phpbb\console\command\user; |
| 15 | |
| 16 | use phpbb\console\command\command; |
| 17 | use phpbb\db\driver\driver_interface; |
| 18 | use phpbb\language\language; |
| 19 | use phpbb\user; |
| 20 | use Symfony\Component\Console\Command\Command as symfony_command; |
| 21 | use Symfony\Component\Console\Helper\ProgressBar; |
| 22 | use Symfony\Component\Console\Input\InputInterface; |
| 23 | use Symfony\Component\Console\Output\OutputInterface; |
| 24 | use Symfony\Component\Console\Style\SymfonyStyle; |
| 25 | |
| 26 | class reclean extends command |
| 27 | { |
| 28 | /** @var driver_interface */ |
| 29 | protected $db; |
| 30 | |
| 31 | /** @var language */ |
| 32 | protected $language; |
| 33 | |
| 34 | /** @var int A count of the number of re-cleaned user names */ |
| 35 | protected $processed; |
| 36 | |
| 37 | /** @var ProgressBar */ |
| 38 | protected $progress; |
| 39 | |
| 40 | /** |
| 41 | * Construct method |
| 42 | * |
| 43 | * @param user $user |
| 44 | * @param driver_interface $db |
| 45 | * @param language $language |
| 46 | */ |
| 47 | public function __construct(user $user, driver_interface $db, language $language) |
| 48 | { |
| 49 | $this->db = $db; |
| 50 | $this->language = $language; |
| 51 | |
| 52 | parent::__construct($user); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Sets the command name and description |
| 57 | * |
| 58 | * @return void |
| 59 | */ |
| 60 | protected function configure() |
| 61 | { |
| 62 | $this |
| 63 | ->setName('user:reclean') |
| 64 | ->setDescription($this->language->lang('CLI_DESCRIPTION_USER_RECLEAN')) |
| 65 | ->setHelp($this->language->lang('CLI_HELP_USER_RECLEAN')) |
| 66 | ; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Executes the command user:reclean |
| 71 | * |
| 72 | * Cleans user names that are unclean. |
| 73 | * |
| 74 | * @param InputInterface $input The input stream used to get the options |
| 75 | * @param OutputInterface $output The output stream, used to print messages |
| 76 | * |
| 77 | * @return int 0 if all is well, 1 if any errors occurred |
| 78 | */ |
| 79 | protected function execute(InputInterface $input, OutputInterface $output): int |
| 80 | { |
| 81 | $io = new SymfonyStyle($input, $output); |
| 82 | |
| 83 | $io->section($this->language->lang('CLI_USER_RECLEAN_START')); |
| 84 | |
| 85 | $this->processed = 0; |
| 86 | |
| 87 | $this->progress = $this->create_progress_bar($this->get_count(), $io, $output); |
| 88 | $this->progress->setMessage($this->language->lang('CLI_USER_RECLEAN_START')); |
| 89 | $this->progress->start(); |
| 90 | |
| 91 | $stage = 0; |
| 92 | while ($stage !== true) |
| 93 | { |
| 94 | $stage = $this->reclean_usernames($stage); |
| 95 | } |
| 96 | |
| 97 | $this->progress->finish(); |
| 98 | |
| 99 | $io->newLine(2); |
| 100 | $io->success($this->language->lang('CLI_USER_RECLEAN_DONE', $this->processed)); |
| 101 | |
| 102 | return symfony_command::SUCCESS; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Re-clean user names |
| 107 | * Only user names that are unclean will be re-cleaned |
| 108 | * |
| 109 | * @param int $start An offset index |
| 110 | * @return bool|int Return the next offset index or true if all records have been processed. |
| 111 | */ |
| 112 | protected function reclean_usernames($start = 0) |
| 113 | { |
| 114 | $limit = 500; |
| 115 | $i = 0; |
| 116 | |
| 117 | $this->db->sql_transaction('begin'); |
| 118 | |
| 119 | $sql = 'SELECT user_id, username, username_clean FROM ' . USERS_TABLE; |
| 120 | $result = $this->db->sql_query_limit($sql, $limit, $start); |
| 121 | while ($row = $this->db->sql_fetchrow($result)) |
| 122 | { |
| 123 | $i++; |
| 124 | $username_clean = $this->db->sql_escape(utf8_clean_string($row['username'])); |
| 125 | |
| 126 | if ($username_clean != $row['username_clean']) |
| 127 | { |
| 128 | $sql = 'UPDATE ' . USERS_TABLE . " |
| 129 | SET username_clean = '$username_clean' |
| 130 | WHERE user_id = {$row['user_id']}"; |
| 131 | $this->db->sql_query($sql); |
| 132 | |
| 133 | $this->processed++; |
| 134 | } |
| 135 | |
| 136 | $this->progress->advance(); |
| 137 | } |
| 138 | $this->db->sql_freeresult($result); |
| 139 | |
| 140 | $this->db->sql_transaction('commit'); |
| 141 | |
| 142 | return ($i < $limit) ? true : $start + $i; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Get the count of users in the database |
| 147 | * |
| 148 | * @return int |
| 149 | */ |
| 150 | protected function get_count() |
| 151 | { |
| 152 | $sql = 'SELECT COUNT(user_id) AS count FROM ' . USERS_TABLE; |
| 153 | $result = $this->db->sql_query($sql); |
| 154 | $count = (int) $this->db->sql_fetchfield('count'); |
| 155 | $this->db->sql_freeresult($result); |
| 156 | |
| 157 | return $count; |
| 158 | } |
| 159 | } |