aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/console/command/reparser/reparse.php
blob: 63124b4b8cca7e3c70fd640d929fed969b2ccd95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

namespace phpbb\console\command\reparser;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class reparse extends \phpbb\console\command\command
{
	/**
	* @var \phpbb\config\db_text
	*/
	protected $config_text;

	/**
	* @var InputInterface
	*/
	protected $input;

	/**
	* @var SymfonyStyle
	*/
	protected $io;

	/**
	* @var OutputInterface
	*/
	protected $output;

	/**
	* @var \phpbb\di\service_collection
	*/
	protected $reparsers;

	/**
	* @var array Reparser names as keys, and their last $current ID as values
	*/
	protected $resume_data;

	/**
	* Constructor
	*
	* @param \phpbb\user $user
	* @param \phpbb\di\service_collection $reparsers
	* @param \phpbb\config\db_text $config_text
	*/
	public function __construct(\phpbb\user $user, \phpbb\di\service_collection $reparsers, \phpbb\config\db_text $config_text)
	{
		require_once __DIR__ . '/../../../../includes/functions_content.php';

		$this->config_text = $config_text;
		$this->reparsers = $reparsers;
		parent::__construct($user);
	}

	/**
	* Sets the command name and description
	*
	* @return null
	*/
	protected function configure()
	{
		$this
			->setName('reparser:reparse')
			->setDescription($this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE'))
			->addArgument('reparser-name', InputArgument::OPTIONAL, $this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_ARG_1'))
			->addOption(
				'dry-run',
				null,
				InputOption::VALUE_NONE,
				$this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_OPT_DRY_RUN')
			)
			->addOption(
				'resume',
				null,
				InputOption::VALUE_NONE,
				$this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_OPT_RESUME')
			)
			->addOption(
				'range-min',
				null,
				InputOption::VALUE_REQUIRED,
				$this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_OPT_RANGE_MIN'),
				1
			)
			->addOption(
				'range-max',
				null,
				InputOption::VALUE_REQUIRED,
				$this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_OPT_RANGE_MAX')
			)
			->addOption(
				'range-size',
				null,
				InputOption::VALUE_REQUIRED,
				$this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_OPT_RANGE_SIZE'),
				100
			);
		;
	}

	/**
	* Create a styled progress bar
	*
	* @param  integer $max Max value for the progress bar
	* @return \Symfony\Component\Console\Helper\ProgressBar
	*/
	protected function create_progress_bar($max)
	{
		$progress = $this->io->createProgressBar($max);
		if ($this->output->getVerbosity() === OutputInterface::VERBOSITY_VERBOSE)
		{
			$progress->setFormat('<info>[%percent:3s%%]</info> %message%');
			$progress->setOverwrite(false);
		}
		else if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE)
		{
			$progress->setFormat('<info>[%current:s%/%max:s%]</info><comment>[%elapsed%/%estimated%][%memory%]</comment> %message%');
			$progress->setOverwrite(false);
		}
		else
		{
			$this->io->newLine(2);
			$progress->setFormat(
				"    %current:s%/%max:s% %bar%  %percent:3s%%\n" .
				"        %message% %elapsed:6s%/%estimated:-6s% %memory:6s%\n");
			$progress->setBarWidth(60);
		}

		if (!defined('PHP_WINDOWS_VERSION_BUILD'))
		{
			$progress->setEmptyBarCharacter('░'); // light shade character \u2591
			$progress->setProgressCharacter('');
			$progress->setBarCharacter('▓'); // dark shade character \u2593
		}

		return $progress;
	}

	/**
	* Executes the command reparser:reparse
	*
	* @param InputInterface $input
	* @param OutputInterface $output
	* @return integer
	*/
	protected function execute(InputInterface $input, OutputInterface $output)
	{
		$this->input = $input;
		$this->output = $output;
		$this->io = new SymfonyStyle($input, $output);
		$this->load_resume_data();

		$name = $input->getArgument('reparser-name');
		if (isset($name))
		{
			// Allow "post_text" to be an alias for "text_reparser.post_text"
			if (!isset($this->reparsers[$name]))
			{
				$name = 'text_reparser.' . $name;
			}
			$this->reparse($name);
		}
		else
		{
			foreach ($this->reparsers as $name => $service)
			{
				$this->reparse($name);
			}
		}

		$this->io->success($this->user->lang('CLI_REPARSER_REPARSE_SUCCESS'));

		return 0;
	}

	/**
	* Get an option value, adjusted for given reparser
	*
	* Will use the last saved value if --resume is set and the option was not specified
	* on the command line
	*
	* @param  string  $reparser_name Reparser name
	* @param  string  $option_name   Option name
	* @return integer
	*/
	protected function get_option($reparser_name, $option_name)
	{
		// Return the option from the resume_data if applicable
		if ($this->input->getOption('resume') && isset($this->resume_data[$reparser_name][$option_name]) && !$this->input->hasParameterOption('--' . $option_name))
		{
			return $this->resume_data[$reparser_name][$option_name];
		}

		$value = $this->input->getOption($option_name);

		// range-max has no default value, it must be computed for each reparser
		if ($option_name === 'range-max' && $value === null)
		{
			$value = $this->reparsers[$reparser_name]->get_max_id();
		}

		return $value;
	}

	/**
	* Load the resume data from the database
	*/
	protected function load_resume_data()
	{
		$resume_data = $this->config_text->get('reparser_resume');
		$this->resume_data = (empty($resume_data)) ? array() : unserialize($resume_data);
	}

	/**
	* Reparse all text handled by given reparser within given range
	*
	* @param string $name Reparser name
	*/
	protected function reparse($name)
	{
		$reparser = $this->reparsers[$name];
		if ($this->input->getOption('dry-run'))
		{
			$reparser->disable_save();
		}
		else
		{
			$reparser->enable_save();
		}

		// Start at range-max if specified or at the highest ID otherwise
		$max  = $this->get_option($name, 'range-max');
		$min  = $this->get_option($name, 'range-min');
		$size = $this->get_option($name, 'range-size');

		if ($max < $min)
		{
			return;
		}

		$this->io->section($this->user->lang('CLI_REPARSER_REPARSE_REPARSING', preg_replace('(^text_reparser\\.)', '', $name), $min, $max));

		$progress = $this->create_progress_bar($max);
		$progress->setMessage($this->user->lang('CLI_REPARSER_REPARSE_REPARSING_START', preg_replace('(^text_reparser\\.)', '', $name)));
		$progress->start();

		// Start from $max and decrement $current by $size until we reach $min
		$current = $max;
		while ($current >= $min)
		{
			$start = max($min, $current + 1 - $size);
			$end   = max($min, $current);

			$progress->setMessage($this->user->lang('CLI_REPARSER_REPARSE_REPARSING', preg_replace('(^text_reparser\\.)', '', $name), $start, $end));
			$reparser->reparse_range($start, $end);

			$current = $start - 1;
			$progress->setProgress($max + 1 - $start);

			$this->update_resume_data($name, $current);
		}
		$progress->finish();

		$this->io->newLine(2);
	}

	/**
	* Save the resume data to the database
	*/
	protected function save_resume_data()
	{
		$this->config_text->set('reparser_resume', serialize($this->resume_data));
	}

	/**
	* Save the resume data to the database
	*
	* @param string $name    Reparser name
	* @param string $current Current ID
	*/
	protected function update_resume_data($name, $current)
	{
		$this->resume_data[$name] = array(
			'range-min'  => $this->get_option($name, 'range-min'),
			'range-max'  => $current,
			'range-size' => $this->get_option($name, 'range-size'),
		);
		$this->save_resume_data();
	}
}