1313
1414namespace phpbb \console \command \reparser ;
1515
16+ use phpbb \exception \runtime_exception ;
1617use Symfony \Component \Console \Input \InputInterface ;
1718use Symfony \Component \Console \Input \InputArgument ;
1819use Symfony \Component \Console \Input \InputOption ;
2122
2223class reparse extends \phpbb \console \command \command
2324{
24- /**
25- * @var \phpbb\config\db_text
26- */
27- protected $ config_text ;
28-
2925 /**
3026 * @var InputInterface
3127 */
@@ -41,28 +37,40 @@ class reparse extends \phpbb\console\command\command
4137 */
4238 protected $ output ;
4339
40+ /**
41+ * @var \phpbb\lock\db
42+ */
43+ protected $ reparse_lock ;
44+
45+ /**
46+ * @var \phpbb\textreparser\manager
47+ */
48+ protected $ reparser_manager ;
49+
4450 /**
4551 * @var \phpbb\di\service_collection
4652 */
4753 protected $ reparsers ;
4854
4955 /**
50- * @var array Reparser names as keys, and their last $current ID as values
56+ * @var array The reparser's last $current ID as values
5157 */
5258 protected $ resume_data ;
5359
5460 /**
5561 * Constructor
5662 *
5763 * @param \phpbb\user $user
64+ * @param \phpbb\lock\db $reparse_lock
65+ * @param \phpbb\textreparser\manager $reparser_manager
5866 * @param \phpbb\di\service_collection $reparsers
59- * @param \phpbb\config\db_text $config_text
6067 */
61- public function __construct (\phpbb \user $ user , \phpbb \di \ service_collection $ reparsers , \phpbb \config \ db_text $ config_text )
68+ public function __construct (\phpbb \user $ user , \phpbb \lock \ db $ reparse_lock , \phpbb \textreparser \ manager $ reparser_manager , \ phpbb \ di \ service_collection $ reparsers )
6269 {
6370 require_once __DIR__ . '/../../../../includes/functions_content.php ' ;
6471
65- $ this ->config_text = $ config_text ;
72+ $ this ->reparse_lock = $ reparse_lock ;
73+ $ this ->reparser_manager = $ reparser_manager ;
6674 $ this ->reparsers = $ reparsers ;
6775 parent ::__construct ($ user );
6876 }
@@ -163,7 +171,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
163171 $ this ->input = $ input ;
164172 $ this ->output = $ output ;
165173 $ this ->io = new SymfonyStyle ($ input , $ output );
166- $ this ->load_resume_data ();
174+
175+ if (!$ this ->reparse_lock ->acquire ())
176+ {
177+ throw new runtime_exception ('REPARSE_LOCK_ERROR ' , array (), null , 1 );
178+ }
167179
168180 $ name = $ input ->getArgument ('reparser-name ' );
169181 if (isset ($ name ))
@@ -185,6 +197,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
185197
186198 $ this ->io ->success ($ this ->user ->lang ('CLI_REPARSER_REPARSE_SUCCESS ' ));
187199
200+ $ this ->reparse_lock ->release ();
201+
188202 return 0 ;
189203 }
190204
@@ -194,36 +208,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
194208 * Will use the last saved value if --resume is set and the option was not specified
195209 * on the command line
196210 *
197- * @param string $reparser_name Reparser name
198211 * @param string $option_name Option name
199212 * @return integer
200213 */
201- protected function get_option ($ reparser_name , $ option_name )
214+ protected function get_option ($ option_name )
202215 {
203216 // Return the option from the resume_data if applicable
204- if ($ this ->input ->getOption ('resume ' ) && isset ($ this ->resume_data [$ reparser_name ][ $ option_name ]) && !$ this ->input ->hasParameterOption ('-- ' . $ option_name ))
217+ if ($ this ->input ->getOption ('resume ' ) && isset ($ this ->resume_data [$ option_name ]) && !$ this ->input ->hasParameterOption ('-- ' . $ option_name ))
205218 {
206- return $ this ->resume_data [$ reparser_name ][ $ option_name ];
219+ return $ this ->resume_data [$ option_name ];
207220 }
208221
209- $ value = $ this ->input ->getOption ($ option_name );
210-
211- // range-max has no default value, it must be computed for each reparser
212- if ($ option_name === 'range-max ' && $ value === null )
213- {
214- $ value = $ this ->reparsers [$ reparser_name ]->get_max_id ();
215- }
216-
217- return $ value ;
218- }
219-
220- /**
221- * Load the resume data from the database
222- */
223- protected function load_resume_data ()
224- {
225- $ resume_data = $ this ->config_text ->get ('reparser_resume ' );
226- $ this ->resume_data = (empty ($ resume_data )) ? array () : unserialize ($ resume_data );
222+ return $ this ->input ->getOption ($ option_name );
227223 }
228224
229225 /**
@@ -234,6 +230,7 @@ protected function load_resume_data()
234230 protected function reparse ($ name )
235231 {
236232 $ reparser = $ this ->reparsers [$ name ];
233+ $ this ->resume_data = $ this ->reparser_manager ->get_resume_data ($ name );
237234 if ($ this ->input ->getOption ('dry-run ' ))
238235 {
239236 $ reparser ->disable_save ();
@@ -244,9 +241,15 @@ protected function reparse($name)
244241 }
245242
246243 // Start at range-max if specified or at the highest ID otherwise
247- $ max = $ this ->get_option ($ name , 'range-max ' );
248- $ min = $ this ->get_option ($ name , 'range-min ' );
249- $ size = $ this ->get_option ($ name , 'range-size ' );
244+ $ max = $ this ->get_option ('range-max ' );
245+ $ min = $ this ->get_option ('range-min ' );
246+ $ size = $ this ->get_option ('range-size ' );
247+
248+ // range-max has no default value, it must be computed for each reparser
249+ if ($ max == null )
250+ {
251+ $ max = $ reparser ->get_max_id ();
252+ }
250253
251254 if ($ max < $ min )
252255 {
@@ -272,34 +275,10 @@ protected function reparse($name)
272275 $ current = $ start - 1 ;
273276 $ progress ->setProgress ($ max + 1 - $ start );
274277
275- $ this ->update_resume_data ($ name , $ current );
278+ $ this ->reparser_manager -> update_resume_data ($ name , $ min , $ current, $ size , ! $ this -> input -> getOption ( ' dry-run ' ) );
276279 }
277280 $ progress ->finish ();
278281
279282 $ this ->io ->newLine (2 );
280283 }
281-
282- /**
283- * Save the resume data to the database
284- */
285- protected function save_resume_data ()
286- {
287- $ this ->config_text ->set ('reparser_resume ' , serialize ($ this ->resume_data ));
288- }
289-
290- /**
291- * Save the resume data to the database
292- *
293- * @param string $name Reparser name
294- * @param string $current Current ID
295- */
296- protected function update_resume_data ($ name , $ current )
297- {
298- $ this ->resume_data [$ name ] = array (
299- 'range-min ' => $ this ->get_option ($ name , 'range-min ' ),
300- 'range-max ' => $ current ,
301- 'range-size ' => $ this ->get_option ($ name , 'range-size ' ),
302- );
303- $ this ->save_resume_data ();
304- }
305284}
0 commit comments