Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
generate
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 4
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
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 / 41
0.00% covered (danger)
0.00%
0 / 1
90
 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*/
13
14namespace phpbb\console\command\thumbnail;
15
16use Symfony\Component\Console\Command\Command as symfony_command;
17use Symfony\Component\Console\Input\InputInterface;
18use Symfony\Component\Console\Output\OutputInterface;
19use Symfony\Component\Console\Style\SymfonyStyle;
20
21class generate extends \phpbb\console\command\command
22{
23    /**
24    * @var \phpbb\config\config
25    */
26    protected $config;
27
28    /**
29    * @var \phpbb\db\driver\driver_interface
30    */
31    protected $db;
32
33    /**
34    * @var \phpbb\cache\service
35    */
36    protected $cache;
37
38    /**
39    * phpBB root path
40    * @var string
41    */
42    protected $phpbb_root_path;
43
44    /**
45    * PHP extension.
46    *
47    * @var string
48    */
49    protected $php_ext;
50
51    /**
52    * Constructor
53    *
54    * @param \phpbb\config\config $config The config
55    * @param \phpbb\user $user The user object (used to get language information)
56    * @param \phpbb\db\driver\driver_interface $db Database connection
57    * @param \phpbb\cache\service $cache The cache service
58    * @param string $phpbb_root_path Root path
59    * @param string $php_ext PHP extension
60    */
61    public function __construct(\phpbb\config\config $config, \phpbb\user $user, \phpbb\db\driver\driver_interface $db, \phpbb\cache\service $cache, $phpbb_root_path, $php_ext)
62    {
63        $this->config = $config;
64        $this->db = $db;
65        $this->cache = $cache;
66        $this->phpbb_root_path = $phpbb_root_path;
67        $this->php_ext = $php_ext;
68
69        parent::__construct($user);
70    }
71
72    /**
73    * Sets the command name and description
74    *
75    * @return void
76    */
77    protected function configure()
78    {
79        $this
80            ->setName('thumbnail:generate')
81            ->setDescription($this->user->lang('CLI_DESCRIPTION_THUMBNAIL_GENERATE'))
82        ;
83    }
84
85    /**
86    * Executes the command thumbnail:generate.
87    *
88    * Generate a thumbnail for all attachments which need one and don't have it yet.
89    *
90    * @param InputInterface $input The input stream used to get the argument and verboe option.
91    * @param OutputInterface $output The output stream, used for printing verbose-mode and error information.
92    *
93    * @return int 0.
94    */
95    protected function execute(InputInterface $input, OutputInterface $output)
96    {
97        $io = new SymfonyStyle($input, $output);
98
99        $io->section($this->user->lang('CLI_THUMBNAIL_GENERATING'));
100
101        $sql = 'SELECT COUNT(*) AS nb_missing_thumbnails
102            FROM ' . ATTACHMENTS_TABLE . '
103            WHERE thumbnail = 0';
104        $result = $this->db->sql_query($sql);
105        $nb_missing_thumbnails = (int) $this->db->sql_fetchfield('nb_missing_thumbnails');
106        $this->db->sql_freeresult($result);
107
108        if ($nb_missing_thumbnails === 0)
109        {
110            $io->warning($this->user->lang('CLI_THUMBNAIL_NOTHING_TO_GENERATE'));
111            return symfony_command::SUCCESS;
112        }
113
114        $extensions = $this->cache->obtain_attach_extensions(true);
115
116        $sql = 'SELECT attach_id, physical_filename, extension, real_filename, mimetype
117            FROM ' . ATTACHMENTS_TABLE . '
118            WHERE thumbnail = 0';
119        $result = $this->db->sql_query($sql);
120
121        if (!function_exists('create_thumbnail'))
122        {
123            require($this->phpbb_root_path . 'includes/functions_posting.' . $this->php_ext);
124        }
125
126        $progress = $this->create_progress_bar($nb_missing_thumbnails, $io, $output);
127
128        $progress->setMessage($this->user->lang('CLI_THUMBNAIL_GENERATING'));
129
130        $progress->start();
131
132        $thumbnail_created = array();
133        while ($row = $this->db->sql_fetchrow($result))
134        {
135            if (isset($extensions[$row['extension']]['display_cat']) && $extensions[$row['extension']]['display_cat'] == \phpbb\attachment\attachment_category::IMAGE)
136            {
137                $source = $this->phpbb_root_path . $this->config['upload_path'] . '/' . $row['physical_filename'];
138                $destination = $this->phpbb_root_path . $this->config['upload_path'] . '/thumb_' . $row['physical_filename'];
139
140                if (create_thumbnail($source, $destination, $row['mimetype']))
141                {
142                    $thumbnail_created[] = (int) $row['attach_id'];
143
144                    if (count($thumbnail_created) === 250)
145                    {
146                        $this->commit_changes($thumbnail_created);
147                        $thumbnail_created = array();
148                    }
149
150                    $progress->setMessage($this->user->lang('CLI_THUMBNAIL_GENERATED', $row['real_filename'], $row['physical_filename']));
151                }
152                else
153                {
154                    $progress->setMessage('<info>' . $this->user->lang('CLI_THUMBNAIL_SKIPPED', $row['real_filename'], $row['physical_filename']) . '</info>');
155                }
156            }
157
158            $progress->advance();
159        }
160        $this->db->sql_freeresult($result);
161
162        if (!empty($thumbnail_created))
163        {
164            $this->commit_changes($thumbnail_created);
165        }
166
167        $progress->finish();
168
169        $io->newLine(2);
170        $io->success($this->user->lang('CLI_THUMBNAIL_GENERATING_DONE'));
171
172        return symfony_command::SUCCESS;
173    }
174
175    /**
176    * Commits the changes to the database
177    *
178    * @param array $thumbnail_created
179    */
180    protected function commit_changes(array $thumbnail_created)
181    {
182        $sql = 'UPDATE ' . ATTACHMENTS_TABLE . '
183                SET thumbnail = 1
184                WHERE ' . $this->db->sql_in_set('attach_id', $thumbnail_created);
185        $this->db->sql_query($sql);
186    }
187}