Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 85
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 / 85
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 / 36
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 / 27
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                'resume',
98                null,
99                InputOption::VALUE_NONE,
100                $this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_OPT_RESUME')
101            )
102            ->addOption(
103                'range-min',
104                null,
105                InputOption::VALUE_REQUIRED,
106                $this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_OPT_RANGE_MIN'),
107                1
108            )
109            ->addOption(
110                'range-max',
111                null,
112                InputOption::VALUE_REQUIRED,
113                $this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_OPT_RANGE_MAX')
114            )
115            ->addOption(
116                'range-size',
117                null,
118                InputOption::VALUE_REQUIRED,
119                $this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_OPT_RANGE_SIZE'),
120                100
121            );
122        ;
123    }
124
125    /**
126    * Executes the command reparser:reparse
127    *
128    * @param InputInterface $input
129    * @param OutputInterface $output
130    * @return int
131    */
132    protected function execute(InputInterface $input, OutputInterface $output)
133    {
134        $this->input = $input;
135        $this->output = $output;
136        $this->io = new SymfonyStyle($input, $output);
137
138        if (!$this->reparse_lock->acquire())
139        {
140            throw new runtime_exception('REPARSE_LOCK_ERROR', array(), null, 1);
141        }
142
143        $name = $input->getArgument('reparser-name');
144        if ($name)
145        {
146            $name = $this->reparser_manager->find_reparser($name);
147            $this->reparse($name);
148        }
149        else
150        {
151            foreach ($this->reparsers as $name => $service)
152            {
153                $this->reparse($name);
154            }
155        }
156
157        $this->io->success($this->user->lang('CLI_REPARSER_REPARSE_SUCCESS'));
158
159        $this->reparse_lock->release();
160
161        return symfony_command::SUCCESS;
162    }
163
164    /**
165    * Get an option value, adjusted for given reparser
166    *
167    * Will use the last saved value if --resume is set and the option was not specified
168    * on the command line
169    *
170    * @param  string  $option_name   Option name
171    * @return integer
172    */
173    protected function get_option($option_name)
174    {
175        // Return the option from the resume_data if applicable
176        if ($this->input->getOption('resume') && isset($this->resume_data[$option_name]) && !$this->input->hasParameterOption('--' . $option_name))
177        {
178            return $this->resume_data[$option_name];
179        }
180
181        return $this->input->getOption($option_name);
182    }
183
184    /**
185    * Reparse all text handled by given reparser within given range
186    *
187    * @param string $name Reparser service name
188    */
189    protected function reparse($name)
190    {
191        $reparser = $this->reparsers[$name];
192        $this->resume_data = $this->reparser_manager->get_resume_data($name);
193        if ($this->input->getOption('dry-run'))
194        {
195            $reparser->disable_save();
196        }
197        else
198        {
199            $reparser->enable_save();
200        }
201
202        // Start at range-max if specified or at the highest ID otherwise
203        $max  = $this->get_option('range-max');
204        $min  = $this->get_option('range-min');
205        $size = $this->get_option('range-size');
206
207        // range-max has no default value, it must be computed for each reparser
208        if ($max === null)
209        {
210            $max = $reparser->get_max_id();
211        }
212
213        if ($max < $min)
214        {
215            return;
216        }
217
218        $this->io->section($this->user->lang('CLI_REPARSER_REPARSE_REPARSING', $reparser->get_name(), $min, $max));
219
220        $progress = $this->create_progress_bar($max, $this->io, $this->output, true);
221        $progress->setMessage($this->user->lang('CLI_REPARSER_REPARSE_REPARSING_START', $reparser->get_name()));
222        $progress->start();
223
224        // Start from $max and decrement $current by $size until we reach $min
225        $current = $max;
226        while ($current >= $min)
227        {
228            $start = max($min, $current + 1 - $size);
229            $end   = max($min, $current);
230
231            $progress->setMessage($this->user->lang('CLI_REPARSER_REPARSE_REPARSING', $reparser->get_name(), $start, $end));
232            $reparser->reparse_range($start, $end);
233
234            $current = $start - 1;
235            $progress->setProgress($max + 1 - $start);
236
237            $this->reparser_manager->update_resume_data($name, $min, $current, $size, !$this->input->getOption('dry-run'));
238        }
239        $progress->finish();
240
241        $this->io->newLine(2);
242    }
243}