From 98ab5478e6a74b4325f002c1a5daae9d6dbe80e1 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Thu, 12 Jun 2014 14:20:58 +0200 Subject: [PATCH 01/18] [ticket/12692] Add a console command to manage the thumbnails PHPBB3-12692 --- .../default/container/services_console.yml | 27 ++++ phpBB/language/en/acp/common.php | 6 + .../console/command/thumbnail/delete.php | 111 +++++++++++++++ .../console/command/thumbnail/generate.php | 133 ++++++++++++++++++ .../console/command/thumbnail/recreate.php | 84 +++++++++++ 5 files changed, 361 insertions(+) create mode 100644 phpBB/phpbb/console/command/thumbnail/delete.php create mode 100644 phpBB/phpbb/console/command/thumbnail/generate.php create mode 100644 phpBB/phpbb/console/command/thumbnail/recreate.php diff --git a/phpBB/config/default/container/services_console.yml b/phpBB/config/default/container/services_console.yml index f39218ed9c..15f597520c 100644 --- a/phpBB/config/default/container/services_console.yml +++ b/phpBB/config/default/container/services_console.yml @@ -156,3 +156,30 @@ services: - @text_reparser_collection tags: - { name: console.command } + + console.command.thumbnail.generate: + class: phpbb\console\command\thumbnail\generate + arguments: + - @dbal.conn + - @user + - @cache + - %core.root_path% + - %core.php_ext% + tags: + - { name: console.command } + + console.command.thumbnail.recreate: + class: phpbb\console\command\thumbnail\recreate + arguments: + - @user + tags: + - { name: console.command } + + console.command.thumbnail.delete: + class: phpbb\console\command\thumbnail\delete + arguments: + - @dbal.conn + - @user + - %core.root_path% + tags: + - { name: console.command } diff --git a/phpBB/language/en/acp/common.php b/phpBB/language/en/acp/common.php index fdbc4aebd0..77dc5b6b62 100644 --- a/phpBB/language/en/acp/common.php +++ b/phpBB/language/en/acp/common.php @@ -225,6 +225,12 @@ $lang = array_merge($lang, array( 'BACK' => 'Back', + 'CLI_DESCRIPTION_CRON_LIST' => 'Prints a list of ready and unready cron jobs.', + 'CLI_DESCRIPTION_CRON_RUN' => 'Runs all ready cron tasks.', + 'CLI_DESCRIPTION_CRON_RUN_ARGUMENT_1' => 'Name of the task to be run', + + 'CLI_DESCRIPTION_OPTION_SHELL' => 'Launch the shell.', + 'COLOUR_SWATCH' => 'Web-safe colour swatch', 'CONFIG_UPDATED' => 'Configuration updated successfully.', 'CRON_LOCK_ERROR' => 'Could not obtain cron lock.', diff --git a/phpBB/phpbb/console/command/thumbnail/delete.php b/phpBB/phpbb/console/command/thumbnail/delete.php new file mode 100644 index 0000000000..81f1baf1db --- /dev/null +++ b/phpBB/phpbb/console/command/thumbnail/delete.php @@ -0,0 +1,111 @@ + +* @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\thumbnail; + +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Output\OutputInterface; + +class delete extends \phpbb\console\command\command +{ + /** @var \phpbb\db\driver\driver_interface */ + protected $db; + + /** @var \phpbb\user */ + protected $user; + + /** + * phpBB root path + * @var string + */ + protected $phpbb_root_path; + + /** + * Constructor + * + * @param \phpbb\db\driver\driver_interface $db Database connection + * @param \phpbb\user $user The user object (used to get language information) + * @param string $phpbb_root_path Root path + */ + public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\user $user, $phpbb_root_path) + { + $this->db = $db; + $this->user = $user; + $this->phpbb_root_path = $phpbb_root_path; + parent::__construct(); + } + + /** + * Sets the command name and description + * + * @return null + */ + protected function configure() + { + $this + ->setName('thumbnail:delete') + ->setDescription($this->user->lang('CLI_DESCRIPTION_THUMBNAIL_DELETE')) + ; + } + + /** + * Executes the command thumbnail:delete. + * + * @param InputInterface $input The input stream used to get the argument and verboe option. + * @param OutputInterface $output The output stream, used for printing verbose-mode and error information. + * + * @return int 0 if all is ok, 1 if a thumbnail couldn't be deleted. + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $sql = 'SELECT attach_id, physical_filename, extension, real_filename, mimetype + FROM ' . ATTACHMENTS_TABLE . ' + WHERE thumbnail = 1'; + $result = $this->db->sql_query($sql); + + $thumbnail_deleted = array(); + $return = 0; + while ($row = $this->db->sql_fetchrow($result)) + { + $thumbnail_path = $this->phpbb_root_path . 'files/thumb_' . $row['physical_filename']; + + if (@unlink($thumbnail_path)) + { + $thumbnail_deleted[] = $row['attach_id']; + if ($input->getOption('verbose')) + { + $output->writeln($this->user->lang('THUMBNAIL_DELETED', $row['real_filename'], $row['physical_filename'])); + } + } + else + { + if ($input->getOption('verbose')) + { + $return = 1; + $output->writeln('' . $this->user->lang('THUMBNAIL_SKIPPED', $row['real_filename'], $row['physical_filename']) . ''); + } + } + } + $this->db->sql_freeresult($result); + + if (sizeof($thumbnail_deleted)) + { + $sql = 'UPDATE ' . ATTACHMENTS_TABLE . ' + SET thumbnail = 0 + WHERE ' . $this->db->sql_in_set('attach_id', $thumbnail_deleted); + $this->db->sql_query($sql); + } + + return $return; + } +} diff --git a/phpBB/phpbb/console/command/thumbnail/generate.php b/phpBB/phpbb/console/command/thumbnail/generate.php new file mode 100644 index 0000000000..dcf60e4fe2 --- /dev/null +++ b/phpBB/phpbb/console/command/thumbnail/generate.php @@ -0,0 +1,133 @@ + +* @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\thumbnail; + +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class generate extends \phpbb\console\command\command +{ + /** @var \phpbb\db\driver\driver_interface */ + protected $db; + + /** @var \phpbb\user */ + protected $user; + + /** @var \phpbb\cache\service */ + protected $cache; + + /** + * phpBB root path + * @var string + */ + protected $phpbb_root_path; + + /** + * PHP extension. + * + * @var string + */ + protected $php_ext; + + /** + * Constructor + * + * @param \phpbb\db\driver\driver_interface $db Database connection + * @param \phpbb\user $user The user object (used to get language information) + * @param \phpbb\cache\service $cache The cache service + * @param string $phpbb_root_path Root path + * @param string $php_ext PHP extension + */ + public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\user $user, \phpbb\cache\service $cache, $phpbb_root_path, $php_ext) + { + $this->db = $db; + $this->user = $user; + $this->cache = $cache; + $this->phpbb_root_path = $phpbb_root_path; + $this->php_ext = $php_ext; + parent::__construct(); + } + + /** + * Sets the command name and description + * + * @return null + */ + protected function configure() + { + $this + ->setName('thumbnail:generate') + ->setDescription($this->user->lang('CLI_DESCRIPTION_THUMBNAIL_GENERATE')) + ; + } + + /** + * Executes the command thumbnail:generate. + * + * @param InputInterface $input The input stream used to get the argument and verboe option. + * @param OutputInterface $output The output stream, used for printing verbose-mode and error information. + * + * @return int 0. + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $extensions = $this->cache->obtain_attach_extensions(true); + + $sql = 'SELECT attach_id, physical_filename, extension, real_filename, mimetype + FROM ' . ATTACHMENTS_TABLE . ' + WHERE thumbnail = 0'; + $result = $this->db->sql_query($sql); + + if (!function_exists('create_thumbnail')) + { + require($this->phpbb_root_path . 'includes/functions_posting.' . $this->php_ext); + } + + $thumbnail_created = array(); + while ($row = $this->db->sql_fetchrow($result)) + { + if (isset($extensions[$row['extension']]['display_cat']) && $extensions[$row['extension']]['display_cat'] == ATTACHMENT_CATEGORY_IMAGE) + { + $source = $this->phpbb_root_path . 'files/' . $row['physical_filename']; + $destination = $this->phpbb_root_path . 'files/thumb_' . $row['physical_filename']; + + if (!create_thumbnail($source, $destination, $row['mimetype'])) + { + if ($input->getOption('verbose')) + { + $output->writeln('' . $this->user->lang('THUMBNAIL_SKIPPED', $row['real_filename'], $row['physical_filename']) . ''); + } + } + else + { + $thumbnail_created[] = $row['attach_id']; + if ($input->getOption('verbose')) + { + $output->writeln($this->user->lang('THUMBNAIL_GENERATED', $row['real_filename'], $row['physical_filename'])); + } + } + } + } + $this->db->sql_freeresult($result); + + if (sizeof($thumbnail_created)) + { + $sql = 'UPDATE ' . ATTACHMENTS_TABLE . ' + SET thumbnail = 1 + WHERE ' . $this->db->sql_in_set('attach_id', $thumbnail_created); + $this->db->sql_query($sql); + } + + return 0; + } +} diff --git a/phpBB/phpbb/console/command/thumbnail/recreate.php b/phpBB/phpbb/console/command/thumbnail/recreate.php new file mode 100644 index 0000000000..569642f2a4 --- /dev/null +++ b/phpBB/phpbb/console/command/thumbnail/recreate.php @@ -0,0 +1,84 @@ + +* @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\thumbnail; + +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Output\OutputInterface; + +class recreate extends \phpbb\console\command\command +{ + /** @var \phpbb\user */ + protected $user; + + /** + * Constructor + * + * @param \phpbb\user $user The user object (used to get language information) + */ + public function __construct(\phpbb\user $user) + { + $this->user = $user; + parent::__construct(); + } + + /** + * Sets the command name and description + * + * @return null + */ + protected function configure() + { + $this + ->setName('thumbnail:recreate') + ->setDescription($this->user->lang('CLI_DESCRIPTION_THUMBNAIL_RECREATE')) + ; + } + + /** + * Executes the command thumbnail:recreate. + * + * @param InputInterface $input The input stream used to get the argument and verboe option. + * @param OutputInterface $output The output stream, used for printing verbose-mode and error information. + * + * @return int 0 if all is ok, 1 if a thumbnail couldn't be deleted. + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $parameters = array( + 'command' => 'thumbnail:delete' + ); + + if ($input->getOption('verbose')) + { + $parameters['-v'] = true; + } + + $this->getApplication()->setAutoExit(false); + + $input_delete = new ArrayInput($parameters); + $return = $this->getApplication()->run($input_delete, $output); + + if ($return == 0) + { + $parameters['command'] = 'thumbnail:generate'; + + $input_create = new ArrayInput($parameters); + $return = $this->getApplication()->run($input_create, $output); + } + + $this->getApplication()->setAutoExit(true); + + return $return; + } +} From d517ed846be665ec04d9ca000d5f26f8a9571206 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Thu, 12 Jun 2014 23:16:51 +0200 Subject: [PATCH 02/18] [ticket/12692] Add unit test PHPBB3-12692 --- tests/console/fixtures/png | Bin 0 -> 129 bytes tests/console/fixtures/thumbnail.xml | 40 ++++++++++ tests/console/fixtures/txt | 2 + tests/console/thumbnail_test.php | 107 +++++++++++++++++++++++++++ 4 files changed, 149 insertions(+) create mode 100644 tests/console/fixtures/png create mode 100644 tests/console/fixtures/thumbnail.xml create mode 100644 tests/console/fixtures/txt create mode 100644 tests/console/thumbnail_test.php diff --git a/tests/console/fixtures/png b/tests/console/fixtures/png new file mode 100644 index 0000000000000000000000000000000000000000..c143a26a06cad7fc560ea51b8d716883c664a64a GIT binary patch literal 129 zcmeAS@N?(olHy`uVBq!ia0vp^EFjFm1|(O0oL2{=7>k44ofy`glX(e}Nq6*hWMJ6X z&;2Kn70Bl-@Q5sCVBk9f!i-b3`J{n@VxBIJArj%qfB0E=JQ_H?4l^n;vavC+#j>!> TyE$hqPz8ghtDnm{r-UW|uGbvP literal 0 HcmV?d00001 diff --git a/tests/console/fixtures/thumbnail.xml b/tests/console/fixtures/thumbnail.xml new file mode 100644 index 0000000000..8037523633 --- /dev/null +++ b/tests/console/fixtures/thumbnail.xml @@ -0,0 +1,40 @@ + + + + attach_id + physical_filename + real_filename + thumbnail + extension + mimetype + attach_comment + + + 1 + test_png_1 + real_test.png + 0 + png + image/png + + + + 2 + test_png_2 + real_test.png + 1 + png + image/png + + + + 10 + test_txt + real_test.txt + 0 + txt + text/plain + + +
+
diff --git a/tests/console/fixtures/txt b/tests/console/fixtures/txt new file mode 100644 index 0000000000..a78c858f5c --- /dev/null +++ b/tests/console/fixtures/txt @@ -0,0 +1,2 @@ +mime trigger +The HTML tags should remain uppercase so that case-insensitivity can be checked. diff --git a/tests/console/thumbnail_test.php b/tests/console/thumbnail_test.php new file mode 100644 index 0000000000..7ac18d931a --- /dev/null +++ b/tests/console/thumbnail_test.php @@ -0,0 +1,107 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +use Symfony\Component\Console\Application; +use Symfony\Component\Console\Tester\CommandTester; +use phpbb\console\command\thumbnail\generate; +use phpbb\console\command\thumbnail\delete; +use phpbb\console\command\thumbnail\recreate; + +class phpbb_console_command_thumbnail_test extends phpbb_database_test_case +{ + protected $db; + protected $config; + protected $cache; + protected $user; + protected $phpEx; + protected $phpbb_root_path; + protected $application; + + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/thumbnail.xml'); + } + + public function setUp() + { + global $config, $phpbb_root_path, $phpEx; + + parent::setUp(); + + $config = $this->config = new \phpbb\config\config(array( + 'img_min_thumb_filesize' => 2, + 'img_max_thumb_width' => 2, + 'img_imagick' => '', + )); + + $this->db = $this->db = $this->new_dbal(); + $this->user = $this->getMock('\phpbb\user'); + $this->phpbb_root_path = $phpbb_root_path; + $this->phpEx = $phpEx; + + $this->cache = $this->getMock('\phpbb\cache\service', array(), array(new phpbb_mock_cache(), $this->config, $this->db, $this->phpbb_root_path, $this->phpEx)); + $this->cache->expects($this->any())->method('obtain_attach_extensions')->will($this->returnValue(array( + 'png' => array('display_cat' => ATTACHMENT_CATEGORY_IMAGE), + 'txt' => array('display_cat' => ATTACHMENT_CATEGORY_NONE), + ))); + + $this->application = new Application(); + $this->application->add(new generate($this->db, $this->user, $this->cache, $this->phpbb_root_path, $this->phpEx)); + $this->application->add(new delete($this->db, $this->user, $this->phpbb_root_path)); + $this->application->add(new recreate($this->user)); + } + + public function test_thumbnails() + { + copy(dirname(__FILE__) . '/fixtures/png', $this->phpbb_root_path . 'files/test_png_1'); + copy(dirname(__FILE__) . '/fixtures/png', $this->phpbb_root_path . 'files/test_png_2'); + copy(dirname(__FILE__) . '/fixtures/png', $this->phpbb_root_path . 'files/thumb_test_png_2'); + copy(dirname(__FILE__) . '/fixtures/txt', $this->phpbb_root_path . 'files/test_txt'); + + $command_tester = $this->get_command_tester('thumbnail:generate'); + $exit_status = $command_tester->execute(array('command' => 'thumbnail:generate')); + + $this->assertSame(true, file_exists($this->phpbb_root_path . 'files/thumb_test_png_1')); + $this->assertSame(true, file_exists($this->phpbb_root_path . 'files/thumb_test_png_2')); + $this->assertSame(false, file_exists($this->phpbb_root_path . 'files/thumb_test_txt')); + $this->assertSame(0, $exit_status); + + $command_tester = $this->get_command_tester('thumbnail:delete'); + $exit_status = $command_tester->execute(array('command' => 'thumbnail:delete')); + + $this->assertSame(false, file_exists($this->phpbb_root_path . 'files/thumb_test_png_1')); + $this->assertSame(false, file_exists($this->phpbb_root_path . 'files/thumb_test_png_2')); + $this->assertSame(false, file_exists($this->phpbb_root_path . 'files/thumb_test_txt')); + $this->assertSame(0, $exit_status); + + $command_tester = $this->get_command_tester('thumbnail:recreate'); + $exit_status = $command_tester->execute(array('command' => 'thumbnail:recreate')); + + $this->assertSame(true, file_exists($this->phpbb_root_path . 'files/thumb_test_png_1')); + $this->assertSame(true, file_exists($this->phpbb_root_path . 'files/thumb_test_png_2')); + $this->assertSame(false, file_exists($this->phpbb_root_path . 'files/thumb_test_txt')); + $this->assertSame(0, $exit_status); + + unlink($this->phpbb_root_path . 'files/test_png_1'); + unlink($this->phpbb_root_path . 'files/test_png_2'); + unlink($this->phpbb_root_path . 'files/test_txt'); + unlink($this->phpbb_root_path . 'files/thumb_test_png_1'); + unlink($this->phpbb_root_path . 'files/thumb_test_png_2'); + } + + public function get_command_tester($command_name) + { + $command = $this->application->find($command_name); + return new CommandTester($command); + } +} From 074dfdbdfea364cc2796d69c4d27535ab19fdac7 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Fri, 13 Jun 2014 15:46:09 +0200 Subject: [PATCH 03/18] [ticket/12692] Update doc blocks PHPBB3-12692 --- phpBB/language/en/acp/common.php | 2 +- phpBB/phpbb/console/command/thumbnail/delete.php | 4 +++- phpBB/phpbb/console/command/thumbnail/generate.php | 2 ++ phpBB/phpbb/console/command/thumbnail/recreate.php | 2 ++ 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/phpBB/language/en/acp/common.php b/phpBB/language/en/acp/common.php index 77dc5b6b62..c2c3fda7a0 100644 --- a/phpBB/language/en/acp/common.php +++ b/phpBB/language/en/acp/common.php @@ -308,7 +308,7 @@ $lang = array_merge($lang, array( 'SHOW_ALL_OPERATIONS' => 'Show all operations', - 'TASKS_NOT_READY' => 'Not ready tasks:', + 'TASKS_NOT_READY' => 'Not ready tasks:', 'TASKS_READY' => 'Ready tasks:', 'TOTAL_SIZE' => 'Total size', diff --git a/phpBB/phpbb/console/command/thumbnail/delete.php b/phpBB/phpbb/console/command/thumbnail/delete.php index 81f1baf1db..707c05ffea 100644 --- a/phpBB/phpbb/console/command/thumbnail/delete.php +++ b/phpBB/phpbb/console/command/thumbnail/delete.php @@ -61,7 +61,9 @@ class delete extends \phpbb\console\command\command /** * Executes the command thumbnail:delete. * - * @param InputInterface $input The input stream used to get the argument and verboe option. + * Delete all the existing thumbnails (and update the database in consequences). + * + * @param InputInterface $input The input stream used to get the argument and verbose option. * @param OutputInterface $output The output stream, used for printing verbose-mode and error information. * * @return int 0 if all is ok, 1 if a thumbnail couldn't be deleted. diff --git a/phpBB/phpbb/console/command/thumbnail/generate.php b/phpBB/phpbb/console/command/thumbnail/generate.php index dcf60e4fe2..bbe6677650 100644 --- a/phpBB/phpbb/console/command/thumbnail/generate.php +++ b/phpBB/phpbb/console/command/thumbnail/generate.php @@ -74,6 +74,8 @@ class generate extends \phpbb\console\command\command /** * Executes the command thumbnail:generate. * + * Generate a thumbnail for all attachments which need one and don't have it yet. + * * @param InputInterface $input The input stream used to get the argument and verboe option. * @param OutputInterface $output The output stream, used for printing verbose-mode and error information. * diff --git a/phpBB/phpbb/console/command/thumbnail/recreate.php b/phpBB/phpbb/console/command/thumbnail/recreate.php index 569642f2a4..71c5d90654 100644 --- a/phpBB/phpbb/console/command/thumbnail/recreate.php +++ b/phpBB/phpbb/console/command/thumbnail/recreate.php @@ -48,6 +48,8 @@ class recreate extends \phpbb\console\command\command /** * Executes the command thumbnail:recreate. * + * This command is a "macro" to execute thumbnail:delete and then thumbnail:generate. + * * @param InputInterface $input The input stream used to get the argument and verboe option. * @param OutputInterface $output The output stream, used for printing verbose-mode and error information. * From 3a0883e93ebd542c1cf67203b4c17456d8308426 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Fri, 13 Jun 2014 21:43:44 +0200 Subject: [PATCH 04/18] [ticket/12692] Use !empty() instead of sizeof() PHPBB3-12692 --- phpBB/phpbb/console/command/thumbnail/delete.php | 2 +- phpBB/phpbb/console/command/thumbnail/generate.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/phpBB/phpbb/console/command/thumbnail/delete.php b/phpBB/phpbb/console/command/thumbnail/delete.php index 707c05ffea..ba6fdf7ce6 100644 --- a/phpBB/phpbb/console/command/thumbnail/delete.php +++ b/phpBB/phpbb/console/command/thumbnail/delete.php @@ -100,7 +100,7 @@ class delete extends \phpbb\console\command\command } $this->db->sql_freeresult($result); - if (sizeof($thumbnail_deleted)) + if (!empty($thumbnail_deleted)) { $sql = 'UPDATE ' . ATTACHMENTS_TABLE . ' SET thumbnail = 0 diff --git a/phpBB/phpbb/console/command/thumbnail/generate.php b/phpBB/phpbb/console/command/thumbnail/generate.php index bbe6677650..ac9d18d933 100644 --- a/phpBB/phpbb/console/command/thumbnail/generate.php +++ b/phpBB/phpbb/console/command/thumbnail/generate.php @@ -122,7 +122,7 @@ class generate extends \phpbb\console\command\command } $this->db->sql_freeresult($result); - if (sizeof($thumbnail_created)) + if (!empty($thumbnail_created)) { $sql = 'UPDATE ' . ATTACHMENTS_TABLE . ' SET thumbnail = 1 From 3924428050d4225c073876bd0e22cf4e9f54138e Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Fri, 13 Jun 2014 21:47:35 +0200 Subject: [PATCH 05/18] [ticket/12692] Use strict comparison in thumbnail:recreate PHPBB3-12692 --- phpBB/phpbb/console/command/thumbnail/recreate.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/phpbb/console/command/thumbnail/recreate.php b/phpBB/phpbb/console/command/thumbnail/recreate.php index 71c5d90654..69ea67e410 100644 --- a/phpBB/phpbb/console/command/thumbnail/recreate.php +++ b/phpBB/phpbb/console/command/thumbnail/recreate.php @@ -71,7 +71,7 @@ class recreate extends \phpbb\console\command\command $input_delete = new ArrayInput($parameters); $return = $this->getApplication()->run($input_delete, $output); - if ($return == 0) + if ($return === 0) { $parameters['command'] = 'thumbnail:generate'; From ed0e1590800e00b19115ad1712e75f33d3080a5e Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Tue, 17 Jun 2014 22:13:34 +0200 Subject: [PATCH 06/18] [ticket/12692] Fix coding style PHPBB3-12692 --- phpBB/phpbb/console/command/thumbnail/delete.php | 2 +- phpBB/phpbb/console/command/thumbnail/recreate.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/phpBB/phpbb/console/command/thumbnail/delete.php b/phpBB/phpbb/console/command/thumbnail/delete.php index ba6fdf7ce6..52a6c424e6 100644 --- a/phpBB/phpbb/console/command/thumbnail/delete.php +++ b/phpBB/phpbb/console/command/thumbnail/delete.php @@ -16,7 +16,7 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Output\OutputInterface; -class delete extends \phpbb\console\command\command +class delete extends \phpbb\console\command\command { /** @var \phpbb\db\driver\driver_interface */ protected $db; diff --git a/phpBB/phpbb/console/command/thumbnail/recreate.php b/phpBB/phpbb/console/command/thumbnail/recreate.php index 69ea67e410..dd10da9106 100644 --- a/phpBB/phpbb/console/command/thumbnail/recreate.php +++ b/phpBB/phpbb/console/command/thumbnail/recreate.php @@ -16,7 +16,7 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Output\OutputInterface; -class recreate extends \phpbb\console\command\command +class recreate extends \phpbb\console\command\command { /** @var \phpbb\user */ protected $user; From 3f0fa70e3afb0ce2dbca72bd74b17c93a2756e02 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Tue, 17 Jun 2014 22:14:26 +0200 Subject: [PATCH 07/18] [ticket/12692] Update comments PHPBB3-12692 --- phpBB/phpbb/console/command/thumbnail/delete.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/phpbb/console/command/thumbnail/delete.php b/phpBB/phpbb/console/command/thumbnail/delete.php index 52a6c424e6..82a53727ab 100644 --- a/phpBB/phpbb/console/command/thumbnail/delete.php +++ b/phpBB/phpbb/console/command/thumbnail/delete.php @@ -61,7 +61,7 @@ class delete extends \phpbb\console\command\command /** * Executes the command thumbnail:delete. * - * Delete all the existing thumbnails (and update the database in consequences). + * Deletes all existing thumbnails and updates the database accordingly. * * @param InputInterface $input The input stream used to get the argument and verbose option. * @param OutputInterface $output The output stream, used for printing verbose-mode and error information. From 6b5fdc730fe1b73bab9ecfe748009bc90d1950f3 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Tue, 17 Jun 2014 22:16:28 +0200 Subject: [PATCH 08/18] [ticket/12692] Remove a not and swap the blocks in the corresponding if PHPBB3-12692 --- .../phpbb/console/command/thumbnail/generate.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/phpBB/phpbb/console/command/thumbnail/generate.php b/phpBB/phpbb/console/command/thumbnail/generate.php index ac9d18d933..1cfb2159d9 100644 --- a/phpBB/phpbb/console/command/thumbnail/generate.php +++ b/phpBB/phpbb/console/command/thumbnail/generate.php @@ -103,14 +103,7 @@ class generate extends \phpbb\console\command\command $source = $this->phpbb_root_path . 'files/' . $row['physical_filename']; $destination = $this->phpbb_root_path . 'files/thumb_' . $row['physical_filename']; - if (!create_thumbnail($source, $destination, $row['mimetype'])) - { - if ($input->getOption('verbose')) - { - $output->writeln('' . $this->user->lang('THUMBNAIL_SKIPPED', $row['real_filename'], $row['physical_filename']) . ''); - } - } - else + if (create_thumbnail($source, $destination, $row['mimetype'])) { $thumbnail_created[] = $row['attach_id']; if ($input->getOption('verbose')) @@ -118,6 +111,13 @@ class generate extends \phpbb\console\command\command $output->writeln($this->user->lang('THUMBNAIL_GENERATED', $row['real_filename'], $row['physical_filename'])); } } + else + { + if ($input->getOption('verbose')) + { + $output->writeln('' . $this->user->lang('THUMBNAIL_SKIPPED', $row['real_filename'], $row['physical_filename']) . ''); + } + } } } $this->db->sql_freeresult($result); From c96cc2cb05de05c9599ca27241e903ea6fada3a3 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Tue, 17 Jun 2014 22:17:26 +0200 Subject: [PATCH 09/18] [ticket/12692] Cast the ids to int PHPBB3-12692 --- phpBB/phpbb/console/command/thumbnail/generate.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/phpbb/console/command/thumbnail/generate.php b/phpBB/phpbb/console/command/thumbnail/generate.php index 1cfb2159d9..1070b7b37a 100644 --- a/phpBB/phpbb/console/command/thumbnail/generate.php +++ b/phpBB/phpbb/console/command/thumbnail/generate.php @@ -105,7 +105,7 @@ class generate extends \phpbb\console\command\command if (create_thumbnail($source, $destination, $row['mimetype'])) { - $thumbnail_created[] = $row['attach_id']; + $thumbnail_created[] = (int) $row['attach_id']; if ($input->getOption('verbose')) { $output->writeln($this->user->lang('THUMBNAIL_GENERATED', $row['real_filename'], $row['physical_filename'])); From 54be6b1f622cf394f3cb2c7eb5f2c27072018baa Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Tue, 17 Jun 2014 22:24:45 +0200 Subject: [PATCH 10/18] [ticket/12692] Update the database regularly PHPBB3-12692 --- .../console/command/thumbnail/delete.php | 25 ++++++++++++++++--- .../console/command/thumbnail/generate.php | 25 ++++++++++++++++--- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/phpBB/phpbb/console/command/thumbnail/delete.php b/phpBB/phpbb/console/command/thumbnail/delete.php index 82a53727ab..7c66011d5c 100644 --- a/phpBB/phpbb/console/command/thumbnail/delete.php +++ b/phpBB/phpbb/console/command/thumbnail/delete.php @@ -84,6 +84,13 @@ class delete extends \phpbb\console\command\command if (@unlink($thumbnail_path)) { $thumbnail_deleted[] = $row['attach_id']; + + if (sizeof($thumbnail_deleted) === 250) + { + $this->commit_changes($thumbnail_deleted); + $thumbnail_deleted = array(); + } + if ($input->getOption('verbose')) { $output->writeln($this->user->lang('THUMBNAIL_DELETED', $row['real_filename'], $row['physical_filename'])); @@ -102,12 +109,22 @@ class delete extends \phpbb\console\command\command if (!empty($thumbnail_deleted)) { - $sql = 'UPDATE ' . ATTACHMENTS_TABLE . ' - SET thumbnail = 0 - WHERE ' . $this->db->sql_in_set('attach_id', $thumbnail_deleted); - $this->db->sql_query($sql); + $this->commit_changes($thumbnail_deleted); } return $return; } + + /** + * Commits the changes to the database + * + * @param array $thumbnail_deleted + */ + protected function commit_changes(array $thumbnail_deleted) + { + $sql = 'UPDATE ' . ATTACHMENTS_TABLE . ' + SET thumbnail = 0 + WHERE ' . $this->db->sql_in_set('attach_id', $thumbnail_deleted); + $this->db->sql_query($sql); + } } diff --git a/phpBB/phpbb/console/command/thumbnail/generate.php b/phpBB/phpbb/console/command/thumbnail/generate.php index 1070b7b37a..c27d91d4d5 100644 --- a/phpBB/phpbb/console/command/thumbnail/generate.php +++ b/phpBB/phpbb/console/command/thumbnail/generate.php @@ -106,6 +106,13 @@ class generate extends \phpbb\console\command\command if (create_thumbnail($source, $destination, $row['mimetype'])) { $thumbnail_created[] = (int) $row['attach_id']; + + if (sizeof($thumbnail_created) === 250) + { + $this->commit_changes($thumbnail_created); + $thumbnail_created = array(); + } + if ($input->getOption('verbose')) { $output->writeln($this->user->lang('THUMBNAIL_GENERATED', $row['real_filename'], $row['physical_filename'])); @@ -124,12 +131,22 @@ class generate extends \phpbb\console\command\command if (!empty($thumbnail_created)) { - $sql = 'UPDATE ' . ATTACHMENTS_TABLE . ' - SET thumbnail = 1 - WHERE ' . $this->db->sql_in_set('attach_id', $thumbnail_created); - $this->db->sql_query($sql); + $this->commit_changes($thumbnail_created); } return 0; } + + /** + * Commits the changes to the database + * + * @param array $thumbnail_created + */ + protected function commit_changes(array $thumbnail_created) + { + $sql = 'UPDATE ' . ATTACHMENTS_TABLE . ' + SET thumbnail = 1 + WHERE ' . $this->db->sql_in_set('attach_id', $thumbnail_created); + $this->db->sql_query($sql); + } } From 487fea8cfffe872e888ebcc3e1f5538b72bcaec1 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Tue, 22 Jul 2014 16:24:40 +0200 Subject: [PATCH 11/18] [ticket/12692] Move the language strings to cli.php PHPBB3-12692 --- .../default/container/services_console.yml | 4 ++-- phpBB/language/en/cli.php | 13 ++++++++++--- .../console/command/thumbnail/delete.php | 16 +++++++--------- .../console/command/thumbnail/generate.php | 19 ++++++++++--------- .../console/command/thumbnail/recreate.php | 14 -------------- tests/console/thumbnail_test.php | 4 ++-- 6 files changed, 31 insertions(+), 39 deletions(-) diff --git a/phpBB/config/default/container/services_console.yml b/phpBB/config/default/container/services_console.yml index 15f597520c..010bde025d 100644 --- a/phpBB/config/default/container/services_console.yml +++ b/phpBB/config/default/container/services_console.yml @@ -160,8 +160,8 @@ services: console.command.thumbnail.generate: class: phpbb\console\command\thumbnail\generate arguments: - - @dbal.conn - @user + - @dbal.conn - @cache - %core.root_path% - %core.php_ext% @@ -178,8 +178,8 @@ services: console.command.thumbnail.delete: class: phpbb\console\command\thumbnail\delete arguments: - - @dbal.conn - @user + - @dbal.conn - %core.root_path% tags: - { name: console.command } diff --git a/phpBB/language/en/cli.php b/phpBB/language/en/cli.php index d45c52ac5d..f0e5dd6120 100644 --- a/phpBB/language/en/cli.php +++ b/phpBB/language/en/cli.php @@ -72,6 +72,10 @@ $lang = array_merge($lang, array( 'CLI_DESCRIPTION_SET_ATOMIC_CONFIG' => 'Sets a configuration option’s value only if the old matches the current value', 'CLI_DESCRIPTION_SET_CONFIG' => 'Sets a configuration option’s value', + 'CLI_DESCRIPTION_THUMBNAIL_DELETE' => 'Delete all existing thumbnails.', + 'CLI_DESCRIPTION_THUMBNAIL_GENERATE' => 'Generate all missing thumbnails.', + 'CLI_DESCRIPTION_THUMBNAIL_RECREATE' => 'Recreate all thumbnails.', + 'CLI_EXTENSION_DISABLE_FAILURE' => 'Could not disable extension %s', 'CLI_EXTENSION_DISABLE_SUCCESS' => 'Successfully disabled extension %s', 'CLI_EXTENSION_ENABLE_FAILURE' => 'Could not enable extension %s', @@ -80,13 +84,16 @@ $lang = array_merge($lang, array( 'CLI_EXTENSION_PURGE_FAILURE' => 'Could not purge extension %s', 'CLI_EXTENSION_PURGE_SUCCESS' => 'Successfully purged extension %s', 'CLI_EXTENSION_NOT_FOUND' => 'No extensions were found.', - 'CLI_EXTENSIONS_AVAILABLE' => 'Available', - 'CLI_EXTENSIONS_DISABLED' => 'Disabled', - 'CLI_EXTENSIONS_ENABLED' => 'Enabled', 'CLI_FIXUP_RECALCULATE_EMAIL_HASH_SUCCESS' => 'Successfully recalculated all email hashes.', 'CLI_REPARSER_REPARSE_REPARSING' => 'Reparsing %1$s (range %2$d..%3$d)', 'CLI_REPARSER_REPARSE_REPARSING_START' => 'Reparsing %s...', 'CLI_REPARSER_REPARSE_SUCCESS' => 'Reparsing ended with success', + + // In all the case %1$s is the logical name of the file and %2$s the real name on the filesystem + // eg: big_image.png (2_a51529ae7932008cf8454a95af84cacd) generated. + 'THUMBNAIL_DELETED' => '%1$s (%2$s) deleted.', + 'THUMBNAIL_SKIPPED' => '%1$s (%2$s) skipped.', + 'THUMBNAIL_GENERATED' => '%1$s (%2$s) generated.', )); diff --git a/phpBB/phpbb/console/command/thumbnail/delete.php b/phpBB/phpbb/console/command/thumbnail/delete.php index 7c66011d5c..b60ea5238f 100644 --- a/phpBB/phpbb/console/command/thumbnail/delete.php +++ b/phpBB/phpbb/console/command/thumbnail/delete.php @@ -13,17 +13,15 @@ namespace phpbb\console\command\thumbnail; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Output\OutputInterface; class delete extends \phpbb\console\command\command { - /** @var \phpbb\db\driver\driver_interface */ + /** + * @var \phpbb\db\driver\driver_interface + */ protected $db; - /** @var \phpbb\user */ - protected $user; - /** * phpBB root path * @var string @@ -33,16 +31,16 @@ class delete extends \phpbb\console\command\command /** * Constructor * - * @param \phpbb\db\driver\driver_interface $db Database connection * @param \phpbb\user $user The user object (used to get language information) + * @param \phpbb\db\driver\driver_interface $db Database connection * @param string $phpbb_root_path Root path */ - public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\user $user, $phpbb_root_path) + public function __construct(\phpbb\user $user, \phpbb\db\driver\driver_interface $db, $phpbb_root_path) { $this->db = $db; - $this->user = $user; $this->phpbb_root_path = $phpbb_root_path; - parent::__construct(); + + parent::__construct($user); } /** diff --git a/phpBB/phpbb/console/command/thumbnail/generate.php b/phpBB/phpbb/console/command/thumbnail/generate.php index c27d91d4d5..640d971b5d 100644 --- a/phpBB/phpbb/console/command/thumbnail/generate.php +++ b/phpBB/phpbb/console/command/thumbnail/generate.php @@ -17,13 +17,14 @@ use Symfony\Component\Console\Output\OutputInterface; class generate extends \phpbb\console\command\command { - /** @var \phpbb\db\driver\driver_interface */ + /** + * @var \phpbb\db\driver\driver_interface + */ protected $db; - /** @var \phpbb\user */ - protected $user; - - /** @var \phpbb\cache\service */ + /** + * @var \phpbb\cache\service + */ protected $cache; /** @@ -42,20 +43,20 @@ class generate extends \phpbb\console\command\command /** * Constructor * - * @param \phpbb\db\driver\driver_interface $db Database connection * @param \phpbb\user $user The user object (used to get language information) + * @param \phpbb\db\driver\driver_interface $db Database connection * @param \phpbb\cache\service $cache The cache service * @param string $phpbb_root_path Root path * @param string $php_ext PHP extension */ - public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\user $user, \phpbb\cache\service $cache, $phpbb_root_path, $php_ext) + public function __construct(\phpbb\user $user, \phpbb\db\driver\driver_interface $db, \phpbb\cache\service $cache, $phpbb_root_path, $php_ext) { $this->db = $db; - $this->user = $user; $this->cache = $cache; $this->phpbb_root_path = $phpbb_root_path; $this->php_ext = $php_ext; - parent::__construct(); + + parent::__construct($user); } /** diff --git a/phpBB/phpbb/console/command/thumbnail/recreate.php b/phpBB/phpbb/console/command/thumbnail/recreate.php index dd10da9106..624e4e507f 100644 --- a/phpBB/phpbb/console/command/thumbnail/recreate.php +++ b/phpBB/phpbb/console/command/thumbnail/recreate.php @@ -18,20 +18,6 @@ use Symfony\Component\Console\Output\OutputInterface; class recreate extends \phpbb\console\command\command { - /** @var \phpbb\user */ - protected $user; - - /** - * Constructor - * - * @param \phpbb\user $user The user object (used to get language information) - */ - public function __construct(\phpbb\user $user) - { - $this->user = $user; - parent::__construct(); - } - /** * Sets the command name and description * diff --git a/tests/console/thumbnail_test.php b/tests/console/thumbnail_test.php index 7ac18d931a..11d2717f73 100644 --- a/tests/console/thumbnail_test.php +++ b/tests/console/thumbnail_test.php @@ -56,8 +56,8 @@ class phpbb_console_command_thumbnail_test extends phpbb_database_test_case ))); $this->application = new Application(); - $this->application->add(new generate($this->db, $this->user, $this->cache, $this->phpbb_root_path, $this->phpEx)); - $this->application->add(new delete($this->db, $this->user, $this->phpbb_root_path)); + $this->application->add(new generate($this->user, $this->db, $this->cache, $this->phpbb_root_path, $this->phpEx)); + $this->application->add(new delete($this->user, $this->db, $this->phpbb_root_path)); $this->application->add(new recreate($this->user)); } From a55c83b77168e77fd4d0101b8c58741f442b2d39 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Fri, 8 Aug 2014 15:22:05 +0200 Subject: [PATCH 12/18] [ticket/12692] Add a proper extension to the fixtures PHPBB3-12692 --- tests/console/fixtures/{png => png.png} | Bin tests/console/fixtures/{txt => txt.txt} | 0 tests/console/thumbnail_test.php | 10 ++++++---- 3 files changed, 6 insertions(+), 4 deletions(-) rename tests/console/fixtures/{png => png.png} (100%) rename tests/console/fixtures/{txt => txt.txt} (100%) diff --git a/tests/console/fixtures/png b/tests/console/fixtures/png.png similarity index 100% rename from tests/console/fixtures/png rename to tests/console/fixtures/png.png diff --git a/tests/console/fixtures/txt b/tests/console/fixtures/txt.txt similarity index 100% rename from tests/console/fixtures/txt rename to tests/console/fixtures/txt.txt diff --git a/tests/console/thumbnail_test.php b/tests/console/thumbnail_test.php index 11d2717f73..6af8e7e174 100644 --- a/tests/console/thumbnail_test.php +++ b/tests/console/thumbnail_test.php @@ -11,6 +11,8 @@ * */ +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; + use Symfony\Component\Console\Application; use Symfony\Component\Console\Tester\CommandTester; use phpbb\console\command\thumbnail\generate; @@ -63,10 +65,10 @@ class phpbb_console_command_thumbnail_test extends phpbb_database_test_case public function test_thumbnails() { - copy(dirname(__FILE__) . '/fixtures/png', $this->phpbb_root_path . 'files/test_png_1'); - copy(dirname(__FILE__) . '/fixtures/png', $this->phpbb_root_path . 'files/test_png_2'); - copy(dirname(__FILE__) . '/fixtures/png', $this->phpbb_root_path . 'files/thumb_test_png_2'); - copy(dirname(__FILE__) . '/fixtures/txt', $this->phpbb_root_path . 'files/test_txt'); + copy(dirname(__FILE__) . '/fixtures/png.png', $this->phpbb_root_path . 'files/test_png_1'); + copy(dirname(__FILE__) . '/fixtures/png.png', $this->phpbb_root_path . 'files/test_png_2'); + copy(dirname(__FILE__) . '/fixtures/png.png', $this->phpbb_root_path . 'files/thumb_test_png_2'); + copy(dirname(__FILE__) . '/fixtures/txt.txt', $this->phpbb_root_path . 'files/test_txt'); $command_tester = $this->get_command_tester('thumbnail:generate'); $exit_status = $command_tester->execute(array('command' => 'thumbnail:generate')); From 24e39545ae77a78fb1f5350b4fe5658c924ad75c Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Mon, 18 Aug 2014 15:18:08 +0200 Subject: [PATCH 13/18] [ticket/12692] Add output PHPBB3-12692 --- phpBB/language/en/cli.php | 6 ++++ .../console/command/thumbnail/delete.php | 30 +++++++++++++++++++ .../console/command/thumbnail/generate.php | 30 +++++++++++++++++++ .../console/command/thumbnail/recreate.php | 3 ++ 4 files changed, 69 insertions(+) diff --git a/phpBB/language/en/cli.php b/phpBB/language/en/cli.php index f0e5dd6120..7dd8403ed4 100644 --- a/phpBB/language/en/cli.php +++ b/phpBB/language/en/cli.php @@ -96,4 +96,10 @@ $lang = array_merge($lang, array( 'THUMBNAIL_DELETED' => '%1$s (%2$s) deleted.', 'THUMBNAIL_SKIPPED' => '%1$s (%2$s) skipped.', 'THUMBNAIL_GENERATED' => '%1$s (%2$s) generated.', + + 'THUMBNAIL_DELETING' => 'Deleting the thumbnails...', + 'THUMBNAIL_GENERATING' => 'Generating the thumbnails...', + + 'NO_THUMBNAIL_TO_GENERATE' => 'No thumbnail to generate.', + 'NO_THUMBNAIL_TO_DELETE' => 'No thumbnail to delete.', )); diff --git a/phpBB/phpbb/console/command/thumbnail/delete.php b/phpBB/phpbb/console/command/thumbnail/delete.php index b60ea5238f..b57fcc681f 100644 --- a/phpBB/phpbb/console/command/thumbnail/delete.php +++ b/phpBB/phpbb/console/command/thumbnail/delete.php @@ -68,11 +68,31 @@ class delete extends \phpbb\console\command\command */ protected function execute(InputInterface $input, OutputInterface $output) { + $sql = 'SELECT COUNT(*) AS nb_missing_thumbnails + FROM ' . ATTACHMENTS_TABLE . ' + WHERE thumbnail = 1'; + $result = $this->db->sql_query($sql); + $row = $this->db->sql_fetchrow($result); + $this->db->sql_freeresult($result); + + $nb_missing_thumbnails = (int) $row['nb_missing_thumbnails']; + if ($nb_missing_thumbnails === 0) + { + $output->writeln('' . $this->user->lang('NO_THUMBNAIL_TO_DELETE') . ''); + return 0; + } + $sql = 'SELECT attach_id, physical_filename, extension, real_filename, mimetype FROM ' . ATTACHMENTS_TABLE . ' WHERE thumbnail = 1'; $result = $this->db->sql_query($sql); + if (!$input->getOption('verbose')) + { + $progress = $this->getHelper('progress'); + $progress->start($output, $nb_missing_thumbnails); + } + $thumbnail_deleted = array(); $return = 0; while ($row = $this->db->sql_fetchrow($result)) @@ -102,6 +122,11 @@ class delete extends \phpbb\console\command\command $output->writeln('' . $this->user->lang('THUMBNAIL_SKIPPED', $row['real_filename'], $row['physical_filename']) . ''); } } + + if (!$input->getOption('verbose')) + { + $progress->advance(); + } } $this->db->sql_freeresult($result); @@ -110,6 +135,11 @@ class delete extends \phpbb\console\command\command $this->commit_changes($thumbnail_deleted); } + if (!$input->getOption('verbose')) + { + $progress->finish(); + } + return $return; } diff --git a/phpBB/phpbb/console/command/thumbnail/generate.php b/phpBB/phpbb/console/command/thumbnail/generate.php index 640d971b5d..068bd0ff94 100644 --- a/phpBB/phpbb/console/command/thumbnail/generate.php +++ b/phpBB/phpbb/console/command/thumbnail/generate.php @@ -84,6 +84,20 @@ class generate extends \phpbb\console\command\command */ protected function execute(InputInterface $input, OutputInterface $output) { + $sql = 'SELECT COUNT(*) AS nb_missing_thumbnails + FROM ' . ATTACHMENTS_TABLE . ' + WHERE thumbnail = 0'; + $result = $this->db->sql_query($sql); + $row = $this->db->sql_fetchrow($result); + $this->db->sql_freeresult($result); + + $nb_missing_thumbnails = (int) $row['nb_missing_thumbnails']; + if ($nb_missing_thumbnails === 0) + { + $output->writeln('' . $this->user->lang('NO_THUMBNAIL_TO_GENERATE') . ''); + return 0; + } + $extensions = $this->cache->obtain_attach_extensions(true); $sql = 'SELECT attach_id, physical_filename, extension, real_filename, mimetype @@ -96,6 +110,12 @@ class generate extends \phpbb\console\command\command require($this->phpbb_root_path . 'includes/functions_posting.' . $this->php_ext); } + if (!$input->getOption('verbose')) + { + $progress = $this->getHelper('progress'); + $progress->start($output, $nb_missing_thumbnails); + } + $thumbnail_created = array(); while ($row = $this->db->sql_fetchrow($result)) { @@ -127,6 +147,11 @@ class generate extends \phpbb\console\command\command } } } + + if (!$input->getOption('verbose')) + { + $progress->advance(); + } } $this->db->sql_freeresult($result); @@ -135,6 +160,11 @@ class generate extends \phpbb\console\command\command $this->commit_changes($thumbnail_created); } + if (!$input->getOption('verbose')) + { + $progress->finish(); + } + return 0; } diff --git a/phpBB/phpbb/console/command/thumbnail/recreate.php b/phpBB/phpbb/console/command/thumbnail/recreate.php index 624e4e507f..ec093161af 100644 --- a/phpBB/phpbb/console/command/thumbnail/recreate.php +++ b/phpBB/phpbb/console/command/thumbnail/recreate.php @@ -54,6 +54,7 @@ class recreate extends \phpbb\console\command\command $this->getApplication()->setAutoExit(false); + $output->writeln('' . $this->user->lang('THUMBNAIL_DELETING') . ''); $input_delete = new ArrayInput($parameters); $return = $this->getApplication()->run($input_delete, $output); @@ -61,6 +62,8 @@ class recreate extends \phpbb\console\command\command { $parameters['command'] = 'thumbnail:generate'; + $output->writeln(''); + $output->writeln('' . $this->user->lang('THUMBNAIL_GENERATING') . ''); $input_create = new ArrayInput($parameters); $return = $this->getApplication()->run($input_create, $output); } From 0f789f4d5ac39f056569544eb1fad3545d80e9d3 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Mon, 18 Aug 2014 16:23:12 +0200 Subject: [PATCH 14/18] [ticket/12692] Fix languages files PHPBB3-12692 --- phpBB/language/en/acp/common.php | 8 +------- phpBB/phpbb/console/command/thumbnail/delete.php | 3 +-- phpBB/phpbb/console/command/thumbnail/generate.php | 3 +-- 3 files changed, 3 insertions(+), 11 deletions(-) diff --git a/phpBB/language/en/acp/common.php b/phpBB/language/en/acp/common.php index c2c3fda7a0..fdbc4aebd0 100644 --- a/phpBB/language/en/acp/common.php +++ b/phpBB/language/en/acp/common.php @@ -225,12 +225,6 @@ $lang = array_merge($lang, array( 'BACK' => 'Back', - 'CLI_DESCRIPTION_CRON_LIST' => 'Prints a list of ready and unready cron jobs.', - 'CLI_DESCRIPTION_CRON_RUN' => 'Runs all ready cron tasks.', - 'CLI_DESCRIPTION_CRON_RUN_ARGUMENT_1' => 'Name of the task to be run', - - 'CLI_DESCRIPTION_OPTION_SHELL' => 'Launch the shell.', - 'COLOUR_SWATCH' => 'Web-safe colour swatch', 'CONFIG_UPDATED' => 'Configuration updated successfully.', 'CRON_LOCK_ERROR' => 'Could not obtain cron lock.', @@ -308,7 +302,7 @@ $lang = array_merge($lang, array( 'SHOW_ALL_OPERATIONS' => 'Show all operations', - 'TASKS_NOT_READY' => 'Not ready tasks:', + 'TASKS_NOT_READY' => 'Not ready tasks:', 'TASKS_READY' => 'Ready tasks:', 'TOTAL_SIZE' => 'Total size', diff --git a/phpBB/phpbb/console/command/thumbnail/delete.php b/phpBB/phpbb/console/command/thumbnail/delete.php index b57fcc681f..3a60271fc2 100644 --- a/phpBB/phpbb/console/command/thumbnail/delete.php +++ b/phpBB/phpbb/console/command/thumbnail/delete.php @@ -72,10 +72,9 @@ class delete extends \phpbb\console\command\command FROM ' . ATTACHMENTS_TABLE . ' WHERE thumbnail = 1'; $result = $this->db->sql_query($sql); - $row = $this->db->sql_fetchrow($result); + $nb_missing_thumbnails = (int) $this->db->sql_fetchfield('nb_missing_thumbnails'); $this->db->sql_freeresult($result); - $nb_missing_thumbnails = (int) $row['nb_missing_thumbnails']; if ($nb_missing_thumbnails === 0) { $output->writeln('' . $this->user->lang('NO_THUMBNAIL_TO_DELETE') . ''); diff --git a/phpBB/phpbb/console/command/thumbnail/generate.php b/phpBB/phpbb/console/command/thumbnail/generate.php index 068bd0ff94..d7530881f1 100644 --- a/phpBB/phpbb/console/command/thumbnail/generate.php +++ b/phpBB/phpbb/console/command/thumbnail/generate.php @@ -88,10 +88,9 @@ class generate extends \phpbb\console\command\command FROM ' . ATTACHMENTS_TABLE . ' WHERE thumbnail = 0'; $result = $this->db->sql_query($sql); - $row = $this->db->sql_fetchrow($result); + $nb_missing_thumbnails = (int) $this->db->sql_fetchfield('nb_missing_thumbnails'); $this->db->sql_freeresult($result); - $nb_missing_thumbnails = (int) $row['nb_missing_thumbnails']; if ($nb_missing_thumbnails === 0) { $output->writeln('' . $this->user->lang('NO_THUMBNAIL_TO_GENERATE') . ''); From 556504df24a72d0cd5fbbd5b266f815f1353ab4c Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Mon, 18 Aug 2014 16:27:09 +0200 Subject: [PATCH 15/18] [ticket/12692] Fix tests PHPBB3-12692 --- tests/console/thumbnail_test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/console/thumbnail_test.php b/tests/console/thumbnail_test.php index 6af8e7e174..094d8ca051 100644 --- a/tests/console/thumbnail_test.php +++ b/tests/console/thumbnail_test.php @@ -47,7 +47,7 @@ class phpbb_console_command_thumbnail_test extends phpbb_database_test_case )); $this->db = $this->db = $this->new_dbal(); - $this->user = $this->getMock('\phpbb\user'); + $this->user = $this->getMock('\phpbb\user', array(), array('\phpbb\datetime')); $this->phpbb_root_path = $phpbb_root_path; $this->phpEx = $phpEx; From 96c5165a655062bb12cf8150262459d883911d8c Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Wed, 27 Aug 2014 16:22:10 +0200 Subject: [PATCH 16/18] [ticket/12692] Fix languages vars and services order PHPBB3-12692 --- .../default/container/services_console.yml | 18 +++++++++--------- phpBB/language/en/cli.php | 8 ++++---- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/phpBB/config/default/container/services_console.yml b/phpBB/config/default/container/services_console.yml index 010bde025d..0e6a1b7706 100644 --- a/phpBB/config/default/container/services_console.yml +++ b/phpBB/config/default/container/services_console.yml @@ -157,6 +157,15 @@ services: tags: - { name: console.command } + console.command.thumbnail.delete: + class: phpbb\console\command\thumbnail\delete + arguments: + - @user + - @dbal.conn + - %core.root_path% + tags: + - { name: console.command } + console.command.thumbnail.generate: class: phpbb\console\command\thumbnail\generate arguments: @@ -174,12 +183,3 @@ services: - @user tags: - { name: console.command } - - console.command.thumbnail.delete: - class: phpbb\console\command\thumbnail\delete - arguments: - - @user - - @dbal.conn - - %core.root_path% - tags: - - { name: console.command } diff --git a/phpBB/language/en/cli.php b/phpBB/language/en/cli.php index 7dd8403ed4..28fae83d5f 100644 --- a/phpBB/language/en/cli.php +++ b/phpBB/language/en/cli.php @@ -97,9 +97,9 @@ $lang = array_merge($lang, array( 'THUMBNAIL_SKIPPED' => '%1$s (%2$s) skipped.', 'THUMBNAIL_GENERATED' => '%1$s (%2$s) generated.', - 'THUMBNAIL_DELETING' => 'Deleting the thumbnails...', - 'THUMBNAIL_GENERATING' => 'Generating the thumbnails...', + 'THUMBNAIL_DELETING' => 'Deleting thumbnails…', + 'THUMBNAIL_GENERATING' => 'Generating thumbnails…', - 'NO_THUMBNAIL_TO_GENERATE' => 'No thumbnail to generate.', - 'NO_THUMBNAIL_TO_DELETE' => 'No thumbnail to delete.', + 'NO_THUMBNAIL_TO_GENERATE' => 'No thumbnails to generate.', + 'NO_THUMBNAIL_TO_DELETE' => 'No thumbnails to delete.', )); From e3e293f5a6b38bb85f57841756a362e26b09088b Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sat, 30 Aug 2014 18:17:23 +0200 Subject: [PATCH 17/18] [ticket/12692] Cleanup language file PHPBB3-12692 --- phpBB/language/en/cli.php | 15 +++++++-------- phpBB/phpbb/console/command/thumbnail/delete.php | 6 +++--- .../phpbb/console/command/thumbnail/generate.php | 6 +++--- .../phpbb/console/command/thumbnail/recreate.php | 4 ++-- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/phpBB/language/en/cli.php b/phpBB/language/en/cli.php index 28fae83d5f..d494ef8c55 100644 --- a/phpBB/language/en/cli.php +++ b/phpBB/language/en/cli.php @@ -93,13 +93,12 @@ $lang = array_merge($lang, array( // In all the case %1$s is the logical name of the file and %2$s the real name on the filesystem // eg: big_image.png (2_a51529ae7932008cf8454a95af84cacd) generated. - 'THUMBNAIL_DELETED' => '%1$s (%2$s) deleted.', - 'THUMBNAIL_SKIPPED' => '%1$s (%2$s) skipped.', - 'THUMBNAIL_GENERATED' => '%1$s (%2$s) generated.', + 'CLI_THUMBNAIL_DELETED' => '%1$s (%2$s) deleted.', + 'CLI_THUMBNAIL_DELETING' => 'Deleting thumbnails…', + 'CLI_THUMBNAIL_SKIPPED' => '%1$s (%2$s) skipped.', + 'CLI_THUMBNAIL_GENERATED' => '%1$s (%2$s) generated.', + 'CLI_THUMBNAIL_GENERATING' => 'Generating thumbnails…', - 'THUMBNAIL_DELETING' => 'Deleting thumbnails…', - 'THUMBNAIL_GENERATING' => 'Generating thumbnails…', - - 'NO_THUMBNAIL_TO_GENERATE' => 'No thumbnails to generate.', - 'NO_THUMBNAIL_TO_DELETE' => 'No thumbnails to delete.', + 'CLI_THUMBNAIL_NOTHING_TO_GENERATE' => 'No thumbnails to generate.', + 'CLI_THUMBNAIL_NOTHING_TO_DELETE' => 'No thumbnails to delete.', )); diff --git a/phpBB/phpbb/console/command/thumbnail/delete.php b/phpBB/phpbb/console/command/thumbnail/delete.php index 3a60271fc2..fb63f7c510 100644 --- a/phpBB/phpbb/console/command/thumbnail/delete.php +++ b/phpBB/phpbb/console/command/thumbnail/delete.php @@ -77,7 +77,7 @@ class delete extends \phpbb\console\command\command if ($nb_missing_thumbnails === 0) { - $output->writeln('' . $this->user->lang('NO_THUMBNAIL_TO_DELETE') . ''); + $output->writeln('' . $this->user->lang('CLI_THUMBNAIL_NOTHING_TO_DELETE') . ''); return 0; } @@ -110,7 +110,7 @@ class delete extends \phpbb\console\command\command if ($input->getOption('verbose')) { - $output->writeln($this->user->lang('THUMBNAIL_DELETED', $row['real_filename'], $row['physical_filename'])); + $output->writeln($this->user->lang('CLI_THUMBNAIL_DELETED', $row['real_filename'], $row['physical_filename'])); } } else @@ -118,7 +118,7 @@ class delete extends \phpbb\console\command\command if ($input->getOption('verbose')) { $return = 1; - $output->writeln('' . $this->user->lang('THUMBNAIL_SKIPPED', $row['real_filename'], $row['physical_filename']) . ''); + $output->writeln('' . $this->user->lang('CLI_THUMBNAIL_SKIPPED', $row['real_filename'], $row['physical_filename']) . ''); } } diff --git a/phpBB/phpbb/console/command/thumbnail/generate.php b/phpBB/phpbb/console/command/thumbnail/generate.php index d7530881f1..0f4a40bd9f 100644 --- a/phpBB/phpbb/console/command/thumbnail/generate.php +++ b/phpBB/phpbb/console/command/thumbnail/generate.php @@ -93,7 +93,7 @@ class generate extends \phpbb\console\command\command if ($nb_missing_thumbnails === 0) { - $output->writeln('' . $this->user->lang('NO_THUMBNAIL_TO_GENERATE') . ''); + $output->writeln('' . $this->user->lang('CLI_THUMBNAIL_NOTHING_TO_GENERATE') . ''); return 0; } @@ -135,14 +135,14 @@ class generate extends \phpbb\console\command\command if ($input->getOption('verbose')) { - $output->writeln($this->user->lang('THUMBNAIL_GENERATED', $row['real_filename'], $row['physical_filename'])); + $output->writeln($this->user->lang('CLI_THUMBNAIL_GENERATED', $row['real_filename'], $row['physical_filename'])); } } else { if ($input->getOption('verbose')) { - $output->writeln('' . $this->user->lang('THUMBNAIL_SKIPPED', $row['real_filename'], $row['physical_filename']) . ''); + $output->writeln('' . $this->user->lang('CLI_THUMBNAIL_SKIPPED', $row['real_filename'], $row['physical_filename']) . ''); } } } diff --git a/phpBB/phpbb/console/command/thumbnail/recreate.php b/phpBB/phpbb/console/command/thumbnail/recreate.php index ec093161af..5d3edbd699 100644 --- a/phpBB/phpbb/console/command/thumbnail/recreate.php +++ b/phpBB/phpbb/console/command/thumbnail/recreate.php @@ -54,7 +54,7 @@ class recreate extends \phpbb\console\command\command $this->getApplication()->setAutoExit(false); - $output->writeln('' . $this->user->lang('THUMBNAIL_DELETING') . ''); + $output->writeln('' . $this->user->lang('CLI_THUMBNAIL_DELETING') . ''); $input_delete = new ArrayInput($parameters); $return = $this->getApplication()->run($input_delete, $output); @@ -63,7 +63,7 @@ class recreate extends \phpbb\console\command\command $parameters['command'] = 'thumbnail:generate'; $output->writeln(''); - $output->writeln('' . $this->user->lang('THUMBNAIL_GENERATING') . ''); + $output->writeln('' . $this->user->lang('CLI_THUMBNAIL_GENERATING') . ''); $input_create = new ArrayInput($parameters); $return = $this->getApplication()->run($input_create, $output); } From 618065ec16030f1d142667473ee2ff42cd80e72b Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Wed, 8 Jul 2015 16:58:45 +0200 Subject: [PATCH 18/18] [ticket/12692] Fix tests and update style PHPBB3-12692 --- phpBB/language/en/cli.php | 11 ++- .../console/command/thumbnail/delete.php | 63 +++++++++++------ .../console/command/thumbnail/generate.php | 64 +++++++++++------ .../console/command/thumbnail/recreate.php | 5 +- tests/console/thumbnail_test.php | 69 +++++++++++-------- 5 files changed, 134 insertions(+), 78 deletions(-) diff --git a/phpBB/language/en/cli.php b/phpBB/language/en/cli.php index d494ef8c55..ec1d63eabf 100644 --- a/phpBB/language/en/cli.php +++ b/phpBB/language/en/cli.php @@ -84,20 +84,25 @@ $lang = array_merge($lang, array( 'CLI_EXTENSION_PURGE_FAILURE' => 'Could not purge extension %s', 'CLI_EXTENSION_PURGE_SUCCESS' => 'Successfully purged extension %s', 'CLI_EXTENSION_NOT_FOUND' => 'No extensions were found.', + 'CLI_EXTENSIONS_AVAILABLE' => 'Available', + 'CLI_EXTENSIONS_DISABLED' => 'Disabled', + 'CLI_EXTENSIONS_ENABLED' => 'Enabled', 'CLI_FIXUP_RECALCULATE_EMAIL_HASH_SUCCESS' => 'Successfully recalculated all email hashes.', 'CLI_REPARSER_REPARSE_REPARSING' => 'Reparsing %1$s (range %2$d..%3$d)', 'CLI_REPARSER_REPARSE_REPARSING_START' => 'Reparsing %s...', 'CLI_REPARSER_REPARSE_SUCCESS' => 'Reparsing ended with success', - + // In all the case %1$s is the logical name of the file and %2$s the real name on the filesystem // eg: big_image.png (2_a51529ae7932008cf8454a95af84cacd) generated. 'CLI_THUMBNAIL_DELETED' => '%1$s (%2$s) deleted.', - 'CLI_THUMBNAIL_DELETING' => 'Deleting thumbnails…', + 'CLI_THUMBNAIL_DELETING' => 'Deleting thumbnails', 'CLI_THUMBNAIL_SKIPPED' => '%1$s (%2$s) skipped.', 'CLI_THUMBNAIL_GENERATED' => '%1$s (%2$s) generated.', - 'CLI_THUMBNAIL_GENERATING' => 'Generating thumbnails…', + 'CLI_THUMBNAIL_GENERATING' => 'Generating thumbnails', + 'CLI_THUMBNAIL_GENERATING_DONE' => 'All thumbnails have been regenerated.', + 'CLI_THUMBNAIL_DELETING_DONE' => 'All thumbnails have been deleted.', 'CLI_THUMBNAIL_NOTHING_TO_GENERATE' => 'No thumbnails to generate.', 'CLI_THUMBNAIL_NOTHING_TO_DELETE' => 'No thumbnails to delete.', diff --git a/phpBB/phpbb/console/command/thumbnail/delete.php b/phpBB/phpbb/console/command/thumbnail/delete.php index fb63f7c510..e8e4cf568e 100644 --- a/phpBB/phpbb/console/command/thumbnail/delete.php +++ b/phpBB/phpbb/console/command/thumbnail/delete.php @@ -14,6 +14,7 @@ namespace phpbb\console\command\thumbnail; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; class delete extends \phpbb\console\command\command { @@ -68,6 +69,10 @@ class delete extends \phpbb\console\command\command */ protected function execute(InputInterface $input, OutputInterface $output) { + $io = new SymfonyStyle($input, $output); + + $io->section($this->user->lang('CLI_THUMBNAIL_DELETING')); + $sql = 'SELECT COUNT(*) AS nb_missing_thumbnails FROM ' . ATTACHMENTS_TABLE . ' WHERE thumbnail = 1'; @@ -77,7 +82,7 @@ class delete extends \phpbb\console\command\command if ($nb_missing_thumbnails === 0) { - $output->writeln('' . $this->user->lang('CLI_THUMBNAIL_NOTHING_TO_DELETE') . ''); + $io->warning($this->user->lang('CLI_THUMBNAIL_NOTHING_TO_DELETE')); return 0; } @@ -86,11 +91,36 @@ class delete extends \phpbb\console\command\command WHERE thumbnail = 1'; $result = $this->db->sql_query($sql); - if (!$input->getOption('verbose')) + $progress = $io->createProgressBar($nb_missing_thumbnails); + if ($output->getVerbosity() === OutputInterface::VERBOSITY_VERBOSE) { - $progress = $this->getHelper('progress'); - $progress->start($output, $nb_missing_thumbnails); + $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" . + " %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 + } + + $progress->setMessage($this->user->lang('CLI_THUMBNAIL_DELETING')); + + $progress->start(); $thumbnail_deleted = array(); $return = 0; @@ -108,24 +138,15 @@ class delete extends \phpbb\console\command\command $thumbnail_deleted = array(); } - if ($input->getOption('verbose')) - { - $output->writeln($this->user->lang('CLI_THUMBNAIL_DELETED', $row['real_filename'], $row['physical_filename'])); - } + $progress->setMessage($this->user->lang('CLI_THUMBNAIL_DELETED', $row['real_filename'], $row['physical_filename'])); } else { - if ($input->getOption('verbose')) - { - $return = 1; - $output->writeln('' . $this->user->lang('CLI_THUMBNAIL_SKIPPED', $row['real_filename'], $row['physical_filename']) . ''); - } + $return = 1; + $progress->setMessage('' . $this->user->lang('CLI_THUMBNAIL_SKIPPED', $row['real_filename'], $row['physical_filename']) . ''); } - if (!$input->getOption('verbose')) - { - $progress->advance(); - } + $progress->advance(); } $this->db->sql_freeresult($result); @@ -134,10 +155,10 @@ class delete extends \phpbb\console\command\command $this->commit_changes($thumbnail_deleted); } - if (!$input->getOption('verbose')) - { - $progress->finish(); - } + $progress->finish(); + + $io->newLine(2); + $io->success($this->user->lang('CLI_THUMBNAIL_DELETING_DONE')); return $return; } diff --git a/phpBB/phpbb/console/command/thumbnail/generate.php b/phpBB/phpbb/console/command/thumbnail/generate.php index 0f4a40bd9f..e677db3a97 100644 --- a/phpBB/phpbb/console/command/thumbnail/generate.php +++ b/phpBB/phpbb/console/command/thumbnail/generate.php @@ -10,10 +10,12 @@ * the docs/CREDITS.txt file. * */ + namespace phpbb\console\command\thumbnail; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; class generate extends \phpbb\console\command\command { @@ -84,6 +86,10 @@ class generate extends \phpbb\console\command\command */ protected function execute(InputInterface $input, OutputInterface $output) { + $io = new SymfonyStyle($input, $output); + + $io->section($this->user->lang('CLI_THUMBNAIL_GENERATING')); + $sql = 'SELECT COUNT(*) AS nb_missing_thumbnails FROM ' . ATTACHMENTS_TABLE . ' WHERE thumbnail = 0'; @@ -93,7 +99,7 @@ class generate extends \phpbb\console\command\command if ($nb_missing_thumbnails === 0) { - $output->writeln('' . $this->user->lang('CLI_THUMBNAIL_NOTHING_TO_GENERATE') . ''); + $io->warning($this->user->lang('CLI_THUMBNAIL_NOTHING_TO_GENERATE')); return 0; } @@ -109,11 +115,36 @@ class generate extends \phpbb\console\command\command require($this->phpbb_root_path . 'includes/functions_posting.' . $this->php_ext); } - if (!$input->getOption('verbose')) + $progress = $io->createProgressBar($nb_missing_thumbnails); + if ($output->getVerbosity() === OutputInterface::VERBOSITY_VERBOSE) { - $progress = $this->getHelper('progress'); - $progress->start($output, $nb_missing_thumbnails); + $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" . + " %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 + } + + $progress->setMessage($this->user->lang('CLI_THUMBNAIL_GENERATING')); + + $progress->start(); $thumbnail_created = array(); while ($row = $this->db->sql_fetchrow($result)) @@ -127,30 +158,21 @@ class generate extends \phpbb\console\command\command { $thumbnail_created[] = (int) $row['attach_id']; - if (sizeof($thumbnail_created) === 250) + if (count($thumbnail_created) === 250) { $this->commit_changes($thumbnail_created); $thumbnail_created = array(); } - if ($input->getOption('verbose')) - { - $output->writeln($this->user->lang('CLI_THUMBNAIL_GENERATED', $row['real_filename'], $row['physical_filename'])); - } + $progress->setMessage($this->user->lang('CLI_THUMBNAIL_GENERATED', $row['real_filename'], $row['physical_filename'])); } else { - if ($input->getOption('verbose')) - { - $output->writeln('' . $this->user->lang('CLI_THUMBNAIL_SKIPPED', $row['real_filename'], $row['physical_filename']) . ''); - } + $progress->setMessage('' . $this->user->lang('CLI_THUMBNAIL_SKIPPED', $row['real_filename'], $row['physical_filename']) . ''); } } - if (!$input->getOption('verbose')) - { - $progress->advance(); - } + $progress->advance(); } $this->db->sql_freeresult($result); @@ -159,10 +181,10 @@ class generate extends \phpbb\console\command\command $this->commit_changes($thumbnail_created); } - if (!$input->getOption('verbose')) - { - $progress->finish(); - } + $progress->finish(); + + $io->newLine(2); + $io->success($this->user->lang('CLI_THUMBNAIL_GENERATING_DONE')); return 0; } diff --git a/phpBB/phpbb/console/command/thumbnail/recreate.php b/phpBB/phpbb/console/command/thumbnail/recreate.php index 5d3edbd699..382da290bf 100644 --- a/phpBB/phpbb/console/command/thumbnail/recreate.php +++ b/phpBB/phpbb/console/command/thumbnail/recreate.php @@ -49,12 +49,11 @@ class recreate extends \phpbb\console\command\command if ($input->getOption('verbose')) { - $parameters['-v'] = true; + $parameters['-' . str_repeat('v', $output->getVerbosity() - 1)] = true; } $this->getApplication()->setAutoExit(false); - $output->writeln('' . $this->user->lang('CLI_THUMBNAIL_DELETING') . ''); $input_delete = new ArrayInput($parameters); $return = $this->getApplication()->run($input_delete, $output); @@ -62,8 +61,6 @@ class recreate extends \phpbb\console\command\command { $parameters['command'] = 'thumbnail:generate'; - $output->writeln(''); - $output->writeln('' . $this->user->lang('CLI_THUMBNAIL_GENERATING') . ''); $input_create = new ArrayInput($parameters); $return = $this->getApplication()->run($input_create, $output); } diff --git a/tests/console/thumbnail_test.php b/tests/console/thumbnail_test.php index 094d8ca051..b5ed02b5e7 100644 --- a/tests/console/thumbnail_test.php +++ b/tests/console/thumbnail_test.php @@ -11,6 +11,7 @@ * */ +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_compatibility.php'; require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; use Symfony\Component\Console\Application; @@ -36,7 +37,7 @@ class phpbb_console_command_thumbnail_test extends phpbb_database_test_case public function setUp() { - global $config, $phpbb_root_path, $phpEx; + global $config, $phpbb_root_path, $phpEx, $phpbb_filesystem; parent::setUp(); @@ -47,12 +48,15 @@ class phpbb_console_command_thumbnail_test extends phpbb_database_test_case )); $this->db = $this->db = $this->new_dbal(); - $this->user = $this->getMock('\phpbb\user', array(), array('\phpbb\datetime')); + $this->user = $this->getMock('\phpbb\user', array(), array( + new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)), + '\phpbb\datetime') + ); $this->phpbb_root_path = $phpbb_root_path; $this->phpEx = $phpEx; $this->cache = $this->getMock('\phpbb\cache\service', array(), array(new phpbb_mock_cache(), $this->config, $this->db, $this->phpbb_root_path, $this->phpEx)); - $this->cache->expects($this->any())->method('obtain_attach_extensions')->will($this->returnValue(array( + $this->cache->expects(self::any())->method('obtain_attach_extensions')->will(self::returnValue(array( 'png' => array('display_cat' => ATTACHMENT_CATEGORY_IMAGE), 'txt' => array('display_cat' => ATTACHMENT_CATEGORY_NONE), ))); @@ -61,38 +65,18 @@ class phpbb_console_command_thumbnail_test extends phpbb_database_test_case $this->application->add(new generate($this->user, $this->db, $this->cache, $this->phpbb_root_path, $this->phpEx)); $this->application->add(new delete($this->user, $this->db, $this->phpbb_root_path)); $this->application->add(new recreate($this->user)); - } - public function test_thumbnails() - { + $phpbb_filesystem = new \phpbb\filesystem\filesystem(); + copy(dirname(__FILE__) . '/fixtures/png.png', $this->phpbb_root_path . 'files/test_png_1'); copy(dirname(__FILE__) . '/fixtures/png.png', $this->phpbb_root_path . 'files/test_png_2'); copy(dirname(__FILE__) . '/fixtures/png.png', $this->phpbb_root_path . 'files/thumb_test_png_2'); copy(dirname(__FILE__) . '/fixtures/txt.txt', $this->phpbb_root_path . 'files/test_txt'); + } - $command_tester = $this->get_command_tester('thumbnail:generate'); - $exit_status = $command_tester->execute(array('command' => 'thumbnail:generate')); - - $this->assertSame(true, file_exists($this->phpbb_root_path . 'files/thumb_test_png_1')); - $this->assertSame(true, file_exists($this->phpbb_root_path . 'files/thumb_test_png_2')); - $this->assertSame(false, file_exists($this->phpbb_root_path . 'files/thumb_test_txt')); - $this->assertSame(0, $exit_status); - - $command_tester = $this->get_command_tester('thumbnail:delete'); - $exit_status = $command_tester->execute(array('command' => 'thumbnail:delete')); - - $this->assertSame(false, file_exists($this->phpbb_root_path . 'files/thumb_test_png_1')); - $this->assertSame(false, file_exists($this->phpbb_root_path . 'files/thumb_test_png_2')); - $this->assertSame(false, file_exists($this->phpbb_root_path . 'files/thumb_test_txt')); - $this->assertSame(0, $exit_status); - - $command_tester = $this->get_command_tester('thumbnail:recreate'); - $exit_status = $command_tester->execute(array('command' => 'thumbnail:recreate')); - - $this->assertSame(true, file_exists($this->phpbb_root_path . 'files/thumb_test_png_1')); - $this->assertSame(true, file_exists($this->phpbb_root_path . 'files/thumb_test_png_2')); - $this->assertSame(false, file_exists($this->phpbb_root_path . 'files/thumb_test_txt')); - $this->assertSame(0, $exit_status); + protected function tearDown() + { + parent::tearDown(); unlink($this->phpbb_root_path . 'files/test_png_1'); unlink($this->phpbb_root_path . 'files/test_png_2'); @@ -101,6 +85,33 @@ class phpbb_console_command_thumbnail_test extends phpbb_database_test_case unlink($this->phpbb_root_path . 'files/thumb_test_png_2'); } + public function test_thumbnails() + { + $command_tester = $this->get_command_tester('thumbnail:generate'); + $exit_status = $command_tester->execute(array('command' => 'thumbnail:generate')); + + self::assertSame(true, file_exists($this->phpbb_root_path . 'files/thumb_test_png_1')); + self::assertSame(true, file_exists($this->phpbb_root_path . 'files/thumb_test_png_2')); + self::assertSame(false, file_exists($this->phpbb_root_path . 'files/thumb_test_txt')); + self::assertSame(0, $exit_status); + + $command_tester = $this->get_command_tester('thumbnail:delete'); + $exit_status = $command_tester->execute(array('command' => 'thumbnail:delete')); + + self::assertSame(false, file_exists($this->phpbb_root_path . 'files/thumb_test_png_1')); + self::assertSame(false, file_exists($this->phpbb_root_path . 'files/thumb_test_png_2')); + self::assertSame(false, file_exists($this->phpbb_root_path . 'files/thumb_test_txt')); + self::assertSame(0, $exit_status); + + $command_tester = $this->get_command_tester('thumbnail:recreate'); + $exit_status = $command_tester->execute(array('command' => 'thumbnail:recreate')); + + self::assertSame(true, file_exists($this->phpbb_root_path . 'files/thumb_test_png_1')); + self::assertSame(true, file_exists($this->phpbb_root_path . 'files/thumb_test_png_2')); + self::assertSame(false, file_exists($this->phpbb_root_path . 'files/thumb_test_txt')); + self::assertSame(0, $exit_status); + } + public function get_command_tester($command_name) { $command = $this->application->find($command_name);