Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
delete
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 4
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 configure
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 1
42
 commit_changes
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
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*/
13namespace phpbb\console\command\thumbnail;
14
15use phpbb\db\driver\driver_interface;
16use phpbb\language\language;
17use phpbb\storage\exception\storage_exception;
18use phpbb\storage\storage;
19use phpbb\user;
20use Symfony\Component\Console\Command\Command as symfony_command;
21use Symfony\Component\Console\Input\InputInterface;
22use Symfony\Component\Console\Output\OutputInterface;
23use Symfony\Component\Console\Style\SymfonyStyle;
24
25class delete extends \phpbb\console\command\command
26{
27    /**
28    * @var driver_interface
29    */
30    protected $db;
31
32    /**
33     * @var language
34     */
35    protected $language;
36
37    /**
38     * @var storage
39     */
40    protected $storage;
41
42    /**
43    * phpBB root path
44    * @var string
45    */
46    protected $phpbb_root_path;
47
48    /**
49    * Constructor
50    *
51    * @param user $user The user object (used to get language information)
52    * @param driver_interface $db Database connection
53    * @param language $language Language
54    * @param storage $storage Storage
55    */
56    public function __construct(user $user, driver_interface $db, language $language, storage $storage)
57    {
58        $this->db = $db;
59        $this->language = $language;
60        $this->storage = $storage;
61
62        parent::__construct($user);
63    }
64
65    /**
66    * Sets the command name and description
67    *
68    * @return void
69    */
70    protected function configure()
71    {
72        $this
73            ->setName('thumbnail:delete')
74            ->setDescription($this->language->lang('CLI_DESCRIPTION_THUMBNAIL_DELETE'))
75        ;
76    }
77
78    /**
79    * Executes the command thumbnail:delete.
80    *
81    * Deletes all existing thumbnails and updates the database accordingly.
82    *
83    * @param InputInterface $input The input stream used to get the argument and verbose option.
84    * @param OutputInterface $output The output stream, used for printing verbose-mode and error information.
85    *
86    * @return int 0 if all is ok, 1 if a thumbnail couldn't be deleted.
87    */
88    protected function execute(InputInterface $input, OutputInterface $output): int
89    {
90        $io = new SymfonyStyle($input, $output);
91
92        $io->section($this->language->lang('CLI_THUMBNAIL_DELETING'));
93
94        $sql = 'SELECT COUNT(*) AS nb_missing_thumbnails
95            FROM ' . ATTACHMENTS_TABLE . '
96            WHERE thumbnail = 1';
97        $result = $this->db->sql_query($sql);
98        $nb_missing_thumbnails = (int) $this->db->sql_fetchfield('nb_missing_thumbnails');
99        $this->db->sql_freeresult($result);
100
101        if ($nb_missing_thumbnails === 0)
102        {
103            $io->warning($this->language->lang('CLI_THUMBNAIL_NOTHING_TO_DELETE'));
104            return symfony_command::SUCCESS;
105        }
106
107        $sql = 'SELECT attach_id, physical_filename, extension, real_filename, mimetype
108            FROM ' . ATTACHMENTS_TABLE . '
109            WHERE thumbnail = 1';
110        $result = $this->db->sql_query($sql);
111
112        $progress = $this->create_progress_bar($nb_missing_thumbnails, $io, $output);
113
114        $progress->setMessage($this->language->lang('CLI_THUMBNAIL_DELETING'));
115
116        $progress->start();
117
118        $thumbnail_deleted = array();
119        $return = symfony_command::SUCCESS;
120        while ($row = $this->db->sql_fetchrow($result))
121        {
122            try
123            {
124                $this->storage->delete('thumb_' . $row['physical_filename']);
125
126                $thumbnail_deleted[] = $row['attach_id'];
127
128                if (count($thumbnail_deleted) === 250)
129                {
130                    $this->commit_changes($thumbnail_deleted);
131                    $thumbnail_deleted = array();
132                }
133
134                $progress->setMessage($this->language->lang('CLI_THUMBNAIL_DELETED', $row['real_filename'], $row['physical_filename']));
135            }
136            catch (storage_exception $e)
137            {
138                $return = symfony_command::FAILURE;
139                $progress->setMessage('<error>' . $this->language->lang('CLI_THUMBNAIL_SKIPPED', $row['real_filename'], $row['physical_filename']) . '</error>');
140
141            }
142
143            $progress->advance();
144        }
145        $this->db->sql_freeresult($result);
146
147        if (!empty($thumbnail_deleted))
148        {
149            $this->commit_changes($thumbnail_deleted);
150        }
151
152        $progress->finish();
153
154        $io->newLine(2);
155        $io->success($this->language->lang('CLI_THUMBNAIL_DELETING_DONE'));
156
157        return $return;
158    }
159
160    /**
161    * Commits the changes to the database
162    *
163    * @param array $thumbnail_deleted
164    */
165    protected function commit_changes(array $thumbnail_deleted)
166    {
167        $sql = 'UPDATE ' . ATTACHMENTS_TABLE . '
168                SET thumbnail = 0
169                WHERE ' . $this->db->sql_in_set('attach_id', $thumbnail_deleted);
170        $this->db->sql_query($sql);
171    }
172}