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