mirror of
https://github.com/phpbb/phpbb.git
synced 2025-07-30 21:40:43 +02:00
[ticket/16738] Multiple changes
Add post helper to get last post id Launch exception if an action is in progress Launch an exception if the action is already done When listing search backend in cli, if the active backend is not indexed an error is launched Add state to commands Fix small error in sphinx search backend PHPBB3-16738
This commit is contained in:
@@ -17,6 +17,8 @@ use phpbb\config\config;
|
||||
use phpbb\console\command\command;
|
||||
use phpbb\language\language;
|
||||
use phpbb\log\log;
|
||||
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\user;
|
||||
@@ -27,6 +29,10 @@ 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;
|
||||
|
||||
@@ -36,23 +42,28 @@ class create extends command
|
||||
/** @var log */
|
||||
protected $log;
|
||||
|
||||
/** @var post_helper */
|
||||
protected $post_helper;
|
||||
|
||||
/** @var search_backend_factory */
|
||||
protected $search_backend_factory;
|
||||
|
||||
/**
|
||||
* Construct method
|
||||
*
|
||||
* @param config $config
|
||||
* @param language $language
|
||||
* @param log $log
|
||||
* @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 user $user
|
||||
*/
|
||||
public function __construct(config $config, language $language, log $log, 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, user $user)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->language = $language;
|
||||
$this->log = $log;
|
||||
$this->post_helper = $post_helper;
|
||||
$this->search_backend_factory = $search_backend_factory;
|
||||
|
||||
parent::__construct($user);
|
||||
@@ -61,7 +72,7 @@ class create extends command
|
||||
/**
|
||||
* Sets the command name and description
|
||||
*
|
||||
* @return null
|
||||
* @return void
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
@@ -105,15 +116,31 @@ class create extends command
|
||||
return command::FAILURE;
|
||||
}
|
||||
|
||||
if (!empty($this->config['search_indexing_state']))
|
||||
{
|
||||
var_dump($this->config['search_indexing_state']);
|
||||
$io->error($this->language->lang('CLI_SEARCHINDEX_ACTION_IN_PROGRESS', $search_backend));
|
||||
return command::FAILURE;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
$progress = $this->create_progress_bar(1, $io, $output, true);
|
||||
$progress = $this->create_progress_bar($this->post_helper->get_max_post_id(), $io, $output, true);
|
||||
$progress->setMessage('');
|
||||
$progress->start();
|
||||
|
||||
$counter = 0;
|
||||
$state = [
|
||||
self::STATE_SEARCH_TYPE => $search->get_type(),
|
||||
self::STATE_ACTION => 'create',
|
||||
self::STATE_POST_COUNTER => 0
|
||||
];
|
||||
$this->save_state($state);
|
||||
|
||||
$counter = &$state[self::STATE_POST_COUNTER];
|
||||
while (($status = $search->create_index($counter)) !== null)
|
||||
{
|
||||
$this->save_state($state);
|
||||
|
||||
$progress->setMaxSteps($status['max_post_id']);
|
||||
$progress->setProgress($status['post_counter']);
|
||||
$progress->setMessage(round($status['rows_per_second'], 2) . ' rows/s');
|
||||
@@ -123,15 +150,35 @@ class create extends command
|
||||
|
||||
$io->newLine(2);
|
||||
}
|
||||
catch(index_created_exception $e)
|
||||
{
|
||||
$this->save_state([]);
|
||||
$io->error($this->language->lang('CLI_SEARCHINDEX_ALREADY_CREATED', $name));
|
||||
return command::FAILURE;
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
$io->error($this->language->lang('CLI_SEARCHINDEX_CREATE_FAILURE', $name));
|
||||
return command::FAILURE;
|
||||
}
|
||||
|
||||
$search->tidy();
|
||||
|
||||
$this->save_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);
|
||||
}
|
||||
}
|
||||
|
@@ -17,6 +17,8 @@ use phpbb\config\config;
|
||||
use phpbb\console\command\command;
|
||||
use phpbb\language\language;
|
||||
use phpbb\log\log;
|
||||
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\user;
|
||||
@@ -27,6 +29,10 @@ 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;
|
||||
|
||||
@@ -36,23 +42,28 @@ class delete extends command
|
||||
/** @var log */
|
||||
protected $log;
|
||||
|
||||
/** @var post_helper */
|
||||
protected $post_helper;
|
||||
|
||||
/** @var search_backend_factory */
|
||||
protected $search_backend_factory;
|
||||
|
||||
/**
|
||||
* Construct method
|
||||
*
|
||||
* @param config $config
|
||||
* @param language $language
|
||||
* @param log $log
|
||||
* @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 user $user
|
||||
*/
|
||||
public function __construct(config $config, language $language, log $log, 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, user $user)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->language = $language;
|
||||
$this->log = $log;
|
||||
$this->post_helper = $post_helper;
|
||||
$this->search_backend_factory = $search_backend_factory;
|
||||
|
||||
parent::__construct($user);
|
||||
@@ -61,7 +72,7 @@ class delete extends command
|
||||
/**
|
||||
* Sets the command name and description
|
||||
*
|
||||
* @return null
|
||||
* @return void
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
@@ -105,16 +116,30 @@ class delete extends command
|
||||
return command::FAILURE;
|
||||
}
|
||||
|
||||
if (!empty($this->config['search_indexing_state']))
|
||||
{
|
||||
$io->error($this->language->lang('CLI_SEARCHINDEX_ACTION_IN_PROGRESS', $search_backend));
|
||||
return command::FAILURE;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// TODO: Read the max_post_id from db because the bucle is not always executed
|
||||
$progress = $this->create_progress_bar(1, $io, $output, true);
|
||||
$progress = $this->create_progress_bar($this->post_helper->get_max_post_id(), $io, $output, true);
|
||||
$progress->setMessage('');
|
||||
$progress->start();
|
||||
|
||||
$counter = 0;
|
||||
$state = [
|
||||
self::STATE_SEARCH_TYPE => $search->get_type(),
|
||||
self::STATE_ACTION => 'delete',
|
||||
self::STATE_POST_COUNTER => 0
|
||||
];
|
||||
$this->save_state($state);
|
||||
|
||||
$counter = &$state[self::STATE_POST_COUNTER];
|
||||
while (($status = $search->delete_index($counter)) !== null)
|
||||
{
|
||||
$this->save_state($state);
|
||||
|
||||
$progress->setMaxSteps($status['max_post_id']);
|
||||
$progress->setProgress($status['post_counter']);
|
||||
$progress->setMessage(round($status['rows_per_second'], 2) . ' rows/s');
|
||||
@@ -124,15 +149,35 @@ class delete extends command
|
||||
|
||||
$io->newLine(2);
|
||||
}
|
||||
catch(index_empty_exception $e)
|
||||
{
|
||||
$this->save_state([]);
|
||||
$io->error($this->language->lang('CLI_SEARCHINDEX_NO_CREATED', $name));
|
||||
return command::FAILURE;
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
$io->error($this->language->lang('CLI_SEARCHINDEX_DELETE_FAILURE', $name));
|
||||
return command::FAILURE;
|
||||
}
|
||||
|
||||
$search->tidy();
|
||||
|
||||
$this->save_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);
|
||||
}
|
||||
}
|
||||
|
@@ -53,7 +53,7 @@ class list_all extends command
|
||||
/**
|
||||
* Sets the command name and description
|
||||
*
|
||||
* @return null
|
||||
* @return void
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
@@ -80,9 +80,14 @@ class list_all extends command
|
||||
$search_backends = [];
|
||||
foreach ($this->search_backend_collection as $search_backend)
|
||||
{
|
||||
$name = get_class($search_backend);
|
||||
$active = ($name == $this->config['search_type']) ? '(<comment>' . $this->language->lang('ACTIVE') . '</comment>) ' : '';
|
||||
$name = $search_backend->get_type();
|
||||
$active = ($name === $this->config['search_type']) ? '(<comment>' . $this->language->lang('ACTIVE') . '</comment>) ' : '';
|
||||
$search_backends[] = '<info>' . $name . '</info> ' . $active . $search_backend->get_name();
|
||||
|
||||
if ($name === $this->config['search_type'] && !$search_backend->index_created())
|
||||
{
|
||||
$io->error($this->language->lang('CLI_SEARCHINDEX_ACTIVE_NOT_INDEXED'));
|
||||
}
|
||||
}
|
||||
|
||||
$io->listing($search_backends);
|
||||
|
Reference in New Issue
Block a user