Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 92
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
reparse
0.00% covered (danger)
0.00%
0 / 92
0.00% covered (danger)
0.00%
0 / 5
240
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 configure
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
20
 get_option
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
20
 reparse
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
30
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\reparser;
15
16use phpbb\exception\runtime_exception;
17use Symfony\Component\Console\Command\Command as symfony_command;
18use Symfony\Component\Console\Input\InputInterface;
19use Symfony\Component\Console\Input\InputArgument;
20use Symfony\Component\Console\Input\InputOption;
21use Symfony\Component\Console\Output\OutputInterface;
22use Symfony\Component\Console\Style\SymfonyStyle;
23
24class reparse extends \phpbb\console\command\command
25{
26    /**
27    * @var InputInterface
28    */
29    protected $input;
30
31    /**
32    * @var SymfonyStyle
33    */
34    protected $io;
35
36    /**
37    * @var OutputInterface
38    */
39    protected $output;
40
41    /**
42     * @var \phpbb\lock\db
43     */
44    protected $reparse_lock;
45
46    /**
47     * @var \phpbb\textreparser\manager
48     */
49    protected $reparser_manager;
50
51    /**
52    * @var \phpbb\di\service_collection
53    */
54    protected $reparsers;
55
56    /**
57    * @var array The reparser's last $current ID as values
58    */
59    protected $resume_data;
60
61    /**
62    * Constructor
63    *
64    * @param \phpbb\user $user
65    * @param \phpbb\lock\db $reparse_lock
66    * @param \phpbb\textreparser\manager $reparser_manager
67    * @param \phpbb\di\service_collection $reparsers
68    */
69    public function __construct(\phpbb\user $user, \phpbb\lock\db $reparse_lock, \phpbb\textreparser\manager $reparser_manager, \phpbb\di\service_collection $reparsers)
70    {
71        require_once __DIR__ . '/../../../../includes/functions_content.php';
72
73        $this->reparse_lock = $reparse_lock;
74        $this->reparser_manager = $reparser_manager;
75        $this->reparsers = $reparsers;
76        parent::__construct($user);
77    }
78
79    /**
80    * Sets the command name and description
81    *
82    * @return void
83    */
84    protected function configure()
85    {
86        $this
87            ->setName('reparser:reparse')
88            ->setDescription($this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE'))
89            ->addArgument('reparser-name', InputArgument::OPTIONAL, $this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_ARG_1'))
90            ->addOption(
91                'dry-run',
92                null,
93                InputOption::VALUE_NONE,
94                $this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_OPT_DRY_RUN')
95            )
96            ->addOption(
97                'force-bbcode-reparsing',
98                null,
99                InputOption::VALUE_NONE,
100                $this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_OPT_FORCE_BBCODE')
101            )
102            ->addOption(
103                'resume',
104                null,
105                InputOption::VALUE_NONE,
106                $this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_OPT_RESUME')
107            )
108            ->addOption(
109                'range-min',
110                null,
111                InputOption::VALUE_REQUIRED,
112                $this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_OPT_RANGE_MIN'),
113                1
114            )
115            ->addOption(
116                'range-max',
117                null,
118                InputOption::VALUE_REQUIRED,
119                $this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_OPT_RANGE_MAX')
120            )
121            ->addOption(
122                'range-size',
123                null,
124                InputOption::VALUE_REQUIRED,
125                $this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_OPT_RANGE_SIZE'),
126                100
127            );
128        ;
129    }
130
131    /**
132    * Executes the command reparser:reparse
133    *
134    * @param InputInterface $input
135    * @param OutputInterface $output
136    * @return int
137    */
138    protected function execute(InputInterface $input, OutputInterface $output)
139    {
140        $this->input = $input;
141        $this->output = $output;
142        $this->io = new SymfonyStyle($input, $output);
143
144        if (!$this->reparse_lock->acquire())
145        {
146            throw new runtime_exception('REPARSE_LOCK_ERROR', array(), null, 1);
147        }
148
149        $name = $input->getArgument('reparser-name');
150        if ($name)
151        {
152            $name = $this->reparser_manager->find_reparser($name);
153            $this->reparse($name);
154        }
155        else
156        {
157            foreach ($this->reparsers as $name => $service)
158            {
159                $this->reparse($name);
160            }
161        }
162
163        $this->io->success($this->user->lang('CLI_REPARSER_REPARSE_SUCCESS'));
164
165        $this->reparse_lock->release();
166
167        return symfony_command::SUCCESS;
168    }
169
170    /**
171    * Get an option value, adjusted for given reparser
172    *
173    * Will use the last saved value if --resume is set and the option was not specified
174    * on the command line
175    *
176    * @param  string  $option_name   Option name
177    * @return integer
178    */
179    protected function get_option($option_name)
180    {
181        // Return the option from the resume_data if applicable
182        if ($this->input->getOption('resume') && isset($this->resume_data[$option_name]) && !$this->input->hasParameterOption('--' . $option_name))
183        {
184            return $this->resume_data[$option_name];
185        }
186
187        return $this->input->getOption($option_name);
188    }
189
190    /**
191    * Reparse all text handled by given reparser within given range
192    *
193    * @param string $name Reparser service name
194    */
195    protected function reparse($name)
196    {
197        $reparser = $this->reparsers[$name];
198        $this->resume_data = $this->reparser_manager->get_resume_data($name);
199        if ($this->input->getOption('dry-run'))
200        {
201            $reparser->disable_save();
202        }
203        else
204        {
205            $reparser->enable_save();
206        }
207
208        // Start at range-max if specified or at the highest ID otherwise
209        $max  = $this->get_option('range-max');
210        $min  = $this->get_option('range-min');
211        $size = $this->get_option('range-size');
212
213        // range-max has no default value, it must be computed for each reparser
214        if ($max === null)
215        {
216            $max = $reparser->get_max_id();
217        }
218
219        if ($max < $min)
220        {
221            return;
222        }
223
224        $this->io->section($this->user->lang('CLI_REPARSER_REPARSE_REPARSING', $reparser->get_name(), $min, $max));
225
226        $progress = $this->create_progress_bar($max, $this->io, $this->output, true);
227        $progress->setMessage($this->user->lang('CLI_REPARSER_REPARSE_REPARSING_START', $reparser->get_name()));
228        $progress->start();
229
230        // Start from $max and decrement $current by $size until we reach $min
231        $current = $max;
232
233        $force_bbcode_reparsing = (bool) $this->get_option('force-bbcode-reparsing');
234        while ($current >= $min)
235        {
236            $start = max($min, $current + 1 - $size);
237            $end   = max($min, $current);
238
239            $progress->setMessage($this->user->lang('CLI_REPARSER_REPARSE_REPARSING', $reparser->get_name(), $start, $end));
240            $reparser->reparse_range($start, $end, $force_bbcode_reparsing);
241
242            $current = $start - 1;
243            $progress->setProgress($max + 1 - $start);
244
245            $this->reparser_manager->update_resume_data($name, $min, $current, $size, !$this->input->getOption('dry-run'));
246        }
247        $progress->finish();
248
249        $this->io->newLine(2);
250    }
251}