mirror of
https://github.com/phpbb/phpbb.git
synced 2025-07-31 22:10:45 +02:00
[ticket/12683] Move state related code to helper class
PHPBB3-12683
This commit is contained in:
@@ -21,6 +21,7 @@ use phpbb\post\post_helper;
|
||||
use phpbb\search\exception\index_created_exception;
|
||||
use phpbb\search\exception\no_search_backend_found_exception;
|
||||
use phpbb\search\search_backend_factory;
|
||||
use phpbb\search\state_helper;
|
||||
use phpbb\user;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
@@ -29,10 +30,6 @@ use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
class create extends command
|
||||
{
|
||||
protected const STATE_SEARCH_TYPE = 0;
|
||||
protected const STATE_ACTION = 1;
|
||||
protected const STATE_POST_COUNTER = 2;
|
||||
|
||||
/** @var config */
|
||||
protected $config;
|
||||
|
||||
@@ -48,23 +45,28 @@ class create extends command
|
||||
/** @var search_backend_factory */
|
||||
protected $search_backend_factory;
|
||||
|
||||
/** @var state_helper */
|
||||
protected $state_helper;
|
||||
|
||||
/**
|
||||
* Construct method
|
||||
*
|
||||
* @param config $config
|
||||
* @param language $language
|
||||
* @param log $log
|
||||
* @param post_helper $post_helper
|
||||
* @param search_backend_factory $search_backend_factory
|
||||
* @param user $user
|
||||
* @param config $config
|
||||
* @param language $language
|
||||
* @param log $log
|
||||
* @param post_helper $post_helper
|
||||
* @param search_backend_factory $search_backend_factory
|
||||
* @param state_helper $state_helper
|
||||
* @param user $user
|
||||
*/
|
||||
public function __construct(config $config, language $language, log $log, post_helper $post_helper, search_backend_factory $search_backend_factory, user $user)
|
||||
public function __construct(config $config, language $language, log $log, post_helper $post_helper, search_backend_factory $search_backend_factory, state_helper $state_helper, user $user)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->language = $language;
|
||||
$this->log = $log;
|
||||
$this->post_helper = $post_helper;
|
||||
$this->search_backend_factory = $search_backend_factory;
|
||||
$this->state_helper = $state_helper;
|
||||
|
||||
parent::__construct($user);
|
||||
}
|
||||
@@ -116,9 +118,8 @@ class create extends command
|
||||
return command::FAILURE;
|
||||
}
|
||||
|
||||
if (!empty($this->config['search_indexing_state']))
|
||||
if ($this->state_helper->is_action_in_progress())
|
||||
{
|
||||
var_dump($this->config['search_indexing_state']);
|
||||
$io->error($this->language->lang('CLI_SEARCHINDEX_ACTION_IN_PROGRESS', $search_backend));
|
||||
return command::FAILURE;
|
||||
}
|
||||
@@ -129,17 +130,12 @@ class create extends command
|
||||
$progress->setMessage('');
|
||||
$progress->start();
|
||||
|
||||
$state = [
|
||||
self::STATE_SEARCH_TYPE => $search->get_type(),
|
||||
self::STATE_ACTION => 'create',
|
||||
self::STATE_POST_COUNTER => 0
|
||||
];
|
||||
$this->save_state($state);
|
||||
$this->state_helper->init($search->get_type(), 'create');
|
||||
|
||||
$counter = &$state[self::STATE_POST_COUNTER];
|
||||
$counter = 0;
|
||||
while (($status = $search->create_index($counter)) !== null)
|
||||
{
|
||||
$this->save_state($state);
|
||||
$this->state_helper->update_counter($status['post_counter']);
|
||||
|
||||
$progress->setMaxSteps($status['max_post_id']);
|
||||
$progress->setProgress($status['post_counter']);
|
||||
@@ -152,7 +148,7 @@ class create extends command
|
||||
}
|
||||
catch(index_created_exception $e)
|
||||
{
|
||||
$this->save_state([]);
|
||||
$this->state_helper->clear_state();
|
||||
$io->error($this->language->lang('CLI_SEARCHINDEX_ALREADY_CREATED', $name));
|
||||
return command::FAILURE;
|
||||
}
|
||||
@@ -164,21 +160,11 @@ class create extends command
|
||||
|
||||
$search->tidy();
|
||||
|
||||
$this->save_state([]);
|
||||
$this->state_helper->clear_state();
|
||||
|
||||
$this->log->add('admin', ANONYMOUS, '', 'LOG_SEARCH_INDEX_CREATED', false, array($name));
|
||||
$io->success($this->language->lang('CLI_SEARCHINDEX_CREATE_SUCCESS', $name));
|
||||
|
||||
return command::SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $state
|
||||
*/
|
||||
private function save_state(array $state = []): void
|
||||
{
|
||||
ksort($state);
|
||||
|
||||
$this->config->set('search_indexing_state', implode(',', $state), true);
|
||||
}
|
||||
}
|
||||
|
@@ -21,6 +21,7 @@ use phpbb\post\post_helper;
|
||||
use phpbb\search\exception\index_empty_exception;
|
||||
use phpbb\search\exception\no_search_backend_found_exception;
|
||||
use phpbb\search\search_backend_factory;
|
||||
use phpbb\search\state_helper;
|
||||
use phpbb\user;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
@@ -29,10 +30,6 @@ use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
class delete extends command
|
||||
{
|
||||
protected const STATE_SEARCH_TYPE = 0;
|
||||
protected const STATE_ACTION = 1;
|
||||
protected const STATE_POST_COUNTER = 2;
|
||||
|
||||
/** @var config */
|
||||
protected $config;
|
||||
|
||||
@@ -48,23 +45,28 @@ class delete extends command
|
||||
/** @var search_backend_factory */
|
||||
protected $search_backend_factory;
|
||||
|
||||
/** @var state_helper */
|
||||
protected $state_helper;
|
||||
|
||||
/**
|
||||
* Construct method
|
||||
*
|
||||
* @param config $config
|
||||
* @param language $language
|
||||
* @param log $log
|
||||
* @param post_helper $post_helper
|
||||
* @param search_backend_factory $search_backend_factory
|
||||
* @param user $user
|
||||
* @param config $config
|
||||
* @param language $language
|
||||
* @param log $log
|
||||
* @param post_helper $post_helper
|
||||
* @param search_backend_factory $search_backend_factory
|
||||
* @param state_helper $state_helper
|
||||
* @param user $user
|
||||
*/
|
||||
public function __construct(config $config, language $language, log $log, post_helper $post_helper, search_backend_factory $search_backend_factory, user $user)
|
||||
public function __construct(config $config, language $language, log $log, post_helper $post_helper, search_backend_factory $search_backend_factory, state_helper $state_helper, user $user)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->language = $language;
|
||||
$this->log = $log;
|
||||
$this->post_helper = $post_helper;
|
||||
$this->search_backend_factory = $search_backend_factory;
|
||||
$this->state_helper = $state_helper;
|
||||
|
||||
parent::__construct($user);
|
||||
}
|
||||
@@ -116,7 +118,7 @@ class delete extends command
|
||||
return command::FAILURE;
|
||||
}
|
||||
|
||||
if (!empty($this->config['search_indexing_state']))
|
||||
if ($this->state_helper->is_action_in_progress())
|
||||
{
|
||||
$io->error($this->language->lang('CLI_SEARCHINDEX_ACTION_IN_PROGRESS', $search_backend));
|
||||
return command::FAILURE;
|
||||
@@ -128,17 +130,12 @@ class delete extends command
|
||||
$progress->setMessage('');
|
||||
$progress->start();
|
||||
|
||||
$state = [
|
||||
self::STATE_SEARCH_TYPE => $search->get_type(),
|
||||
self::STATE_ACTION => 'delete',
|
||||
self::STATE_POST_COUNTER => 0
|
||||
];
|
||||
$this->save_state($state);
|
||||
$this->state_helper->init($search->get_type(), 'delete');
|
||||
|
||||
$counter = &$state[self::STATE_POST_COUNTER];
|
||||
$counter = 0;
|
||||
while (($status = $search->delete_index($counter)) !== null)
|
||||
{
|
||||
$this->save_state($state);
|
||||
$this->state_helper->update_counter($status['post_counter']);
|
||||
|
||||
$progress->setMaxSteps($status['max_post_id']);
|
||||
$progress->setProgress($status['post_counter']);
|
||||
@@ -151,7 +148,7 @@ class delete extends command
|
||||
}
|
||||
catch(index_empty_exception $e)
|
||||
{
|
||||
$this->save_state([]);
|
||||
$this->state_helper->clear_state();
|
||||
$io->error($this->language->lang('CLI_SEARCHINDEX_NO_CREATED', $name));
|
||||
return command::FAILURE;
|
||||
}
|
||||
@@ -163,21 +160,11 @@ class delete extends command
|
||||
|
||||
$search->tidy();
|
||||
|
||||
$this->save_state([]);
|
||||
$this->state_helper->clear_state();
|
||||
|
||||
$this->log->add('admin', ANONYMOUS, '', 'LOG_SEARCH_INDEX_REMOVED', false, array($name));
|
||||
$io->success($this->language->lang('CLI_SEARCHINDEX_DELETE_SUCCESS', $name));
|
||||
|
||||
return command::SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $state
|
||||
*/
|
||||
private function save_state(array $state = []): void
|
||||
{
|
||||
ksort($state);
|
||||
|
||||
$this->config->set('search_indexing_state', implode(',', $state), true);
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,20 @@
|
||||
<?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\search\exception;
|
||||
|
||||
|
||||
class action_in_progress_exception extends search_exception
|
||||
{
|
||||
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
<?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\search\exception;
|
||||
|
||||
|
||||
class no_action_in_progress_exception extends search_exception
|
||||
{
|
||||
|
||||
}
|
170
phpBB/phpbb/search/state_helper.php
Normal file
170
phpBB/phpbb/search/state_helper.php
Normal file
@@ -0,0 +1,170 @@
|
||||
<?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\search;
|
||||
|
||||
use phpbb\config\config;
|
||||
use phpbb\search\exception\action_in_progress_exception;
|
||||
use phpbb\search\exception\no_action_in_progress_exception;
|
||||
|
||||
class state_helper
|
||||
{
|
||||
protected const STATE_SEARCH_TYPE = 0;
|
||||
protected const STATE_ACTION = 1;
|
||||
protected const STATE_POST_COUNTER = 2;
|
||||
|
||||
/** @var config */
|
||||
protected $config;
|
||||
|
||||
/** @var search_backend_factory */
|
||||
protected $search_backend_factory;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param \phpbb\config\config $config
|
||||
* @param \phpbb\search\search_backend_factory $search_backend_factory
|
||||
*/
|
||||
public function __construct(config $config, search_backend_factory $search_backend_factory)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->search_backend_factory = $search_backend_factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if there is an action in progress
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_action_in_progress(): bool
|
||||
{
|
||||
return !empty($this->config['search_indexing_state']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*
|
||||
* @throws no_action_in_progress_exception If there is no action in progress
|
||||
*/
|
||||
public function type(): string
|
||||
{
|
||||
$state = $this->load_state();
|
||||
|
||||
return $state[self::STATE_SEARCH_TYPE];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*
|
||||
* @throws no_action_in_progress_exception If there is no action in progress
|
||||
*/
|
||||
public function action(): string
|
||||
{
|
||||
$state = $this->load_state();
|
||||
|
||||
return $state[self::STATE_ACTION];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*
|
||||
* @throws no_action_in_progress_exception If there is no action in progress
|
||||
*/
|
||||
public function counter(): int
|
||||
{
|
||||
$state = $this->load_state();
|
||||
|
||||
return $state[self::STATE_POST_COUNTER];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $search_type
|
||||
* @param string $action
|
||||
*
|
||||
* @throws action_in_progress_exception If there is an action in progress
|
||||
* @throws no_search_backend_found_exception If search backend don't exist
|
||||
*/
|
||||
public function init(string $search_type, string $action): void
|
||||
{
|
||||
// Is not possible to start a new process when there is one already running
|
||||
if ($this->is_action_in_progress())
|
||||
{
|
||||
throw new action_in_progress_exception();
|
||||
}
|
||||
|
||||
// Make sure the search type exist (if not, the next line launch an exception)
|
||||
$this->search_backend_factory->get($search_type);
|
||||
|
||||
// Make sure the action is correct (just in case)
|
||||
if (!in_array($action, ['create', 'delete']))
|
||||
{
|
||||
throw new \RuntimeException('Invalid action');
|
||||
}
|
||||
|
||||
$state = [
|
||||
self::STATE_SEARCH_TYPE => $search_type,
|
||||
self::STATE_ACTION => $action,
|
||||
self::STATE_POST_COUNTER => 0
|
||||
];
|
||||
|
||||
$this->save_state($state);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $counter
|
||||
*
|
||||
* @throws \phpbb\search\exception\no_action_in_progress_exception If there is no action in progress
|
||||
*/
|
||||
public function update_counter(int $counter): void
|
||||
{
|
||||
$state = $this->load_state();
|
||||
|
||||
$state[self::STATE_POST_COUNTER] = $counter;
|
||||
|
||||
$this->save_state($state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the state
|
||||
*/
|
||||
public function clear_state(): void
|
||||
{
|
||||
$this->save_state([]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*
|
||||
* @throws no_action_in_progress_exception If there is no action in progress
|
||||
*/
|
||||
private function load_state(): array
|
||||
{
|
||||
// Is not possible to execute an action over state if is empty
|
||||
if (!$this->is_action_in_progress())
|
||||
{
|
||||
throw new no_action_in_progress_exception();
|
||||
}
|
||||
|
||||
return explode(',', $this->config['search_indexing_state']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $state
|
||||
*/
|
||||
private function save_state(array $state = []): void
|
||||
{
|
||||
ksort($state);
|
||||
|
||||
$this->config->set('search_indexing_state', implode(',', $state), true);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user