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