From ce54ee5e6f43089c857f830114f356121cdae12f Mon Sep 17 00:00:00 2001 From: rubencm Date: Wed, 24 Mar 2021 12:27:07 +0100 Subject: [PATCH 01/20] [ticket/12683] Add CLI command to generate the search index PHPBB3-12683 --- .../default/container/services_console.yml | 32 +++++ phpBB/language/en/cli.php | 10 ++ .../console/command/searchindex/create.php | 120 ++++++++++++++++ .../console/command/searchindex/delete.php | 129 ++++++++++++++++++ .../console/command/searchindex/list_all.php | 92 +++++++++++++ 5 files changed, 383 insertions(+) create mode 100644 phpBB/phpbb/console/command/searchindex/create.php create mode 100644 phpBB/phpbb/console/command/searchindex/delete.php create mode 100644 phpBB/phpbb/console/command/searchindex/list_all.php diff --git a/phpBB/config/default/container/services_console.yml b/phpBB/config/default/container/services_console.yml index 74cfade99d..422a99992f 100644 --- a/phpBB/config/default/container/services_console.yml +++ b/phpBB/config/default/container/services_console.yml @@ -246,6 +246,38 @@ services: tags: - { name: console.command } + console.command.searchindex.list_all: + class: phpbb\console\command\searchindex\list_all + arguments: + - '@config' + - '@language' + - '@search.backend_collection' + - '@user' + tags: + - { name: console.command } + + console.command.searchindex.create: + class: phpbb\console\command\searchindex\create + arguments: + - '@config' + - '@language' + - '@log' + - '@search.backend_factory' + - '@user' + tags: + - { name: console.command } + + console.command.searchindex.delete: + class: phpbb\console\command\searchindex\delete + arguments: + - '@config' + - '@language' + - '@log' + - '@search.backend_factory' + - '@user' + tags: + - { name: console.command } + console.command.thumbnail.delete: class: phpbb\console\command\thumbnail\delete arguments: diff --git a/phpBB/language/en/cli.php b/phpBB/language/en/cli.php index bdd76a32e5..85e778509e 100644 --- a/phpBB/language/en/cli.php +++ b/phpBB/language/en/cli.php @@ -83,6 +83,10 @@ $lang = array_merge($lang, array( 'CLI_DESCRIPTION_SET_ATOMIC_CONFIG' => 'Sets a configuration option’s value only if the old matches the current value', 'CLI_DESCRIPTION_SET_CONFIG' => 'Sets a configuration option’s value', + 'CLI_DESCRIPTION_SEARCHINDEX_DELETE' => 'Delete search index.', + 'CLI_DESCRIPTION_SEARCHINDEX_CREATE' => 'Create search index.', + 'CLI_DESCRIPTION_SEARCHINDEX_LIST' => 'List all search backends.', + 'CLI_DESCRIPTION_THUMBNAIL_DELETE' => 'Delete all existing thumbnails.', 'CLI_DESCRIPTION_THUMBNAIL_GENERATE' => 'Generate all missing thumbnails.', 'CLI_DESCRIPTION_THUMBNAIL_RECREATE' => 'Recreate all thumbnails.', @@ -142,6 +146,12 @@ $lang = array_merge($lang, array( 'CLI_REPARSER_REPARSE_REPARSING_START' => 'Reparsing %s...', 'CLI_REPARSER_REPARSE_SUCCESS' => 'Reparsing ended with success', + 'CLI_SEARCHINDEX_SEARCH_BACKEND_NAME' => 'Backend class', + 'CLI_SEARCHINDEX_CREATE_SUCCESS' => 'Search index created successfully', + 'CLI_SEARCHINDEX_CREATE_FAILURE' => 'Error creating search index', + 'CLI_SEARCHINDEX_DELETE_SUCCESS' => 'Search index deleted successfully', + 'CLI_SEARCHINDEX_DELETE_FAILURE' => 'Error deleting search index', + // In all the case %1$s is the logical name of the file and %2$s the real name on the filesystem // eg: big_image.png (2_a51529ae7932008cf8454a95af84cacd) generated. 'CLI_THUMBNAIL_DELETED' => '%1$s (%2$s) deleted.', diff --git a/phpBB/phpbb/console/command/searchindex/create.php b/phpBB/phpbb/console/command/searchindex/create.php new file mode 100644 index 0000000000..410d00f625 --- /dev/null +++ b/phpBB/phpbb/console/command/searchindex/create.php @@ -0,0 +1,120 @@ + + * @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\searchindex; + +use phpbb\config\config; +use phpbb\console\command\command; +use phpbb\language\language; +use phpbb\log\log; +use phpbb\search\search_backend_factory; +use phpbb\user; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; + +class create extends command +{ + /** @var config */ + protected $config; + + /** @var language */ + protected $language; + + /** @var log */ + protected $log; + + /** @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 + */ + public function __construct(config $config, language $language, log $log, search_backend_factory $search_backend_factory, user $user) + { + $this->config = $config; + $this->language = $language; + $this->log = $log; + $this->search_backend_factory = $search_backend_factory; + + parent::__construct($user); + } + + /** + * Sets the command name and description + * + * @return null + */ + protected function configure() + { + $this + ->setName('searchindex:create') + ->setDescription($this->language->lang('CLI_DESCRIPTION_SEARCHINDEX_CREATE')) + ->addArgument( + 'search-backend', + InputArgument::REQUIRED, + $this->user->lang('CLI_SEARCHINDEX_SEARCH_BACKEND_NAME') + ) + ; + } + + /** + * Executes the command searchindex:create + * + * Create search index + * + * @param InputInterface $input The input stream used to get the options + * @param OutputInterface $output The output stream, used to print messages + * + * @return int 0 if all is well, 1 if any errors occurred + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $io = new SymfonyStyle($input, $output); + + $search_backend = $input->getArgument('search-backend'); + $search = $this->search_backend_factory->get($search_backend); + $name = $search->get_name(); + + try + { + $counter = 0; + while (($status = $search->create_index($counter)) !== null) + { + $this->config->set('search_indexing_state', implode(',', $this->state), true); + + $io->success($counter); + $io->success(print_r($status, 1)); + } + + $search->tidy(); + } + catch (\Exception $e) + { + $io->error($this->user->lang('CLI_SEARCHINDEX_CREATE_FAILURE', $name)); + return 1; + } + + $this->log->add('admin', ANONYMOUS, '', 'LOG_SEARCH_INDEX_CREATED', false, array($name)); + $io->success($this->user->lang('CLI_SEARCHINDEX_CREATE_SUCCESS', $name)); + + return 0; + } +} diff --git a/phpBB/phpbb/console/command/searchindex/delete.php b/phpBB/phpbb/console/command/searchindex/delete.php new file mode 100644 index 0000000000..927376902c --- /dev/null +++ b/phpBB/phpbb/console/command/searchindex/delete.php @@ -0,0 +1,129 @@ + + * @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\searchindex; + +use phpbb\config\config; +use phpbb\console\command\command; +use phpbb\language\language; +use phpbb\log\log; +use phpbb\search\search_backend_factory; +use phpbb\user; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; + +class delete extends command +{ + /** @var config */ + protected $config; + + /** @var language */ + protected $language; + + /** @var log */ + protected $log; + + /** @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 + */ + public function __construct(config $config, language $language, log $log, search_backend_factory $search_backend_factory, user $user) + { + $this->config = $config; + $this->language = $language; + $this->log = $log; + $this->search_backend_factory = $search_backend_factory; + + parent::__construct($user); + } + + /** + * Sets the command name and description + * + * @return null + */ + protected function configure() + { + $this + ->setName('searchindex:delete') + ->setDescription($this->language->lang('CLI_DESCRIPTION_SEARCHINDEX_DELETE')) + ->addArgument( + 'search-backend', + InputArgument::REQUIRED, + $this->user->lang('CLI_SEARCHINDEX_SEARCH_BACKEND_NAME') + ) + ; + } + + /** + * Executes the command searchindex:delete + * + * Delete search index + * + * @param InputInterface $input The input stream used to get the options + * @param OutputInterface $output The output stream, used to print messages + * + * @return int 0 if all is well, 1 if any errors occurred + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $io = new SymfonyStyle($input, $output); + + $search_backend = $input->getArgument('search-backend'); + $search = $this->search_backend_factory->get($search_backend); + $name = $search->get_name(); + + try + { + $this->state = explode(',', $this->config['search_indexing_state']); + $this->max_post_id = $this->get_max_post_id(); + + $search->delete_index($this, ''); + $search->tidy(); + } + catch (\Exception $e) + { + $io->error($this->user->lang('CLI_SEARCHINDEX_DELETE_FAILURE', $name)); + return 1; + } + + $this->log->add('admin', ANONYMOUS, '', 'LOG_SEARCH_INDEX_REMOVED', false, array($name)); + $io->success($this->user->lang('CLI_SEARCHINDEX_DELETE_SUCCESS', $name)); + + return 0; + } + + function save_state($state = false) + { + global $config; + + if ($state) + { + $this->state = $state; + } + + ksort($this->state); + + $config->set('search_indexing_state', implode(',', $this->state), true); + } +} diff --git a/phpBB/phpbb/console/command/searchindex/list_all.php b/phpBB/phpbb/console/command/searchindex/list_all.php new file mode 100644 index 0000000000..6faeaffa8f --- /dev/null +++ b/phpBB/phpbb/console/command/searchindex/list_all.php @@ -0,0 +1,92 @@ + + * @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\searchindex; + +use phpbb\config\config; +use phpbb\console\command\command; +use phpbb\di\service_collection; +use phpbb\language\language; +use phpbb\user; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; + +class list_all extends command +{ + /** @var config */ + protected $config; + + /** @var language */ + protected $language; + + /** @var service_collection */ + protected $search_backend_collection; + + /** + * Construct method + * + * @param config $config + * @param language $language + * @param service_collection $search_backend_collection + * @param user $user + */ + public function __construct(config $config, language $language, service_collection $search_backend_collection, user $user) + { + $this->config = $config; + $this->language = $language; + $this->search_backend_collection = $search_backend_collection; + + parent::__construct($user); + } + + /** + * Sets the command name and description + * + * @return null + */ + protected function configure() + { + $this + ->setName('searchindex:list') + ->setDescription($this->language->lang('CLI_DESCRIPTION_SEARCHINDEX_LIST')) + ; + } + + /** + * Executes the command searchindex:list + * + * List all search backends. + * + * @param InputInterface $input The input stream used to get the options + * @param OutputInterface $output The output stream, used to print messages + * + * @return int 0 if all is well, 1 if any errors occurred + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $io = new SymfonyStyle($input, $output); + + $search_backends = []; + foreach ($this->search_backend_collection as $search_backend) + { + $name = get_class($search_backend); + $active = ($name == $this->config['search_type']) ? '(' . $this->language->lang('ACTIVE') . ') ' : ''; + $search_backends[] = '' . $name . ' ' . $active . $search_backend->get_name(); + } + + $io->listing($search_backends); + + return 0; + } +} From 0a24704b4f301fcddba79e90a7b43b1e1b87e43a Mon Sep 17 00:00:00 2001 From: rubencm Date: Wed, 24 Mar 2021 18:50:29 +0100 Subject: [PATCH 02/20] [ticket/12683] Add progress bar to create_index PHPBB3-12683 --- phpBB/includes/acp/acp_main.php | 11 +---- phpBB/includes/functions.php | 1 + phpBB/includes/functions_admin.php | 11 +---- phpBB/includes/functions_posting.php | 11 +---- phpBB/includes/mcp/mcp_main.php | 11 +---- phpBB/includes/mcp/mcp_post.php | 11 +---- phpBB/includes/mcp/mcp_topic.php | 11 +---- phpBB/language/en/cli.php | 1 + .../console/command/searchindex/create.php | 39 +++++++++++++----- .../console/command/searchindex/delete.php | 39 ++++++++---------- phpBB/phpbb/search/backend/base.php | 41 +++++++++++++------ .../no_search_backend_found_exception.php | 19 +++++++++ .../search/exception/search_exception.php | 21 ++++++++++ phpBB/phpbb/search/search_backend_factory.php | 24 ++++++++++- phpBB/search.php | 11 +---- 15 files changed, 151 insertions(+), 111 deletions(-) create mode 100644 phpBB/phpbb/search/exception/no_search_backend_found_exception.php create mode 100644 phpBB/phpbb/search/exception/search_exception.php diff --git a/phpBB/includes/acp/acp_main.php b/phpBB/includes/acp/acp_main.php index b89c4e3779..71e9ed7a2e 100644 --- a/phpBB/includes/acp/acp_main.php +++ b/phpBB/includes/acp/acp_main.php @@ -652,16 +652,9 @@ class acp_main $search_backend_factory = $phpbb_container->get('search.backend_factory'); $search = $search_backend_factory->get_active(); } - catch (RuntimeException $e) + catch (\phpbb\search\exception\no_search_backend_found_exception $e) { - if (strpos($e->getMessage(), 'No service found') === 0) - { - trigger_error('NO_SUCH_SEARCH_MODULE'); - } - else - { - throw $e; - } + trigger_error('NO_SUCH_SEARCH_MODULE'); } if (!$search->index_created()) diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 05ea03d446..d3ba635a16 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -206,6 +206,7 @@ function get_formatted_filesize($value, $string_only = true, $allowed_units = fa */ function still_on_time($extra_time = 15) { + // TODO: Check the bug with this, it should be possible to restart the start time static $max_execution_time, $start_time; $current_time = microtime(true); diff --git a/phpBB/includes/functions_admin.php b/phpBB/includes/functions_admin.php index fa4dac7e57..27f9292ac5 100644 --- a/phpBB/includes/functions_admin.php +++ b/phpBB/includes/functions_admin.php @@ -1092,16 +1092,9 @@ function delete_posts($where_type, $where_ids, $auto_sync = true, $posted_sync = $search_backend_factory = $phpbb_container->get('search.backend_factory'); $search = $search_backend_factory->get_active(); } - catch (RuntimeException $e) + catch (\phpbb\search\exception\no_search_backend_found_exception $e) { - if (strpos($e->getMessage(), 'No service found') === 0) - { - trigger_error('NO_SUCH_SEARCH_MODULE'); - } - else - { - throw $e; - } + trigger_error('NO_SUCH_SEARCH_MODULE'); } $search->index_remove($post_ids, $poster_ids, $forum_ids); diff --git a/phpBB/includes/functions_posting.php b/phpBB/includes/functions_posting.php index 770280195c..80f0e31e74 100644 --- a/phpBB/includes/functions_posting.php +++ b/phpBB/includes/functions_posting.php @@ -2356,16 +2356,9 @@ function submit_post($mode, $subject, $username, $topic_type, &$poll_ary, &$data $search_backend_factory = $phpbb_container->get('search.backend_factory'); $search = $search_backend_factory->get_active(); } - catch (RuntimeException $e) + catch (\phpbb\search\exception\no_search_backend_found_exception $e) { - if (strpos($e->getMessage(), 'No service found') === 0) - { - trigger_error('NO_SUCH_SEARCH_MODULE'); - } - else - { - throw $e; - } + trigger_error('NO_SUCH_SEARCH_MODULE'); } $search->index($mode, (int) $data_ary['post_id'], $data_ary['message'], $subject, $poster_id, (int) $data_ary['forum_id']); diff --git a/phpBB/includes/mcp/mcp_main.php b/phpBB/includes/mcp/mcp_main.php index b5ad7ffc8c..7dab25074f 100644 --- a/phpBB/includes/mcp/mcp_main.php +++ b/phpBB/includes/mcp/mcp_main.php @@ -1397,16 +1397,9 @@ function mcp_fork_topic($topic_ids) $search_backend_factory = $phpbb_container->get('search.backend_factory'); $search = $search_backend_factory->get_active(); } - catch (RuntimeException $e) + catch (\phpbb\search\exception\no_search_backend_found_exception $e) { - if (strpos($e->getMessage(), 'No service found') === 0) - { - trigger_error('NO_SUCH_SEARCH_MODULE'); - } - else - { - throw $e; - } + trigger_error('NO_SUCH_SEARCH_MODULE'); } $search_mode = 'post'; } diff --git a/phpBB/includes/mcp/mcp_post.php b/phpBB/includes/mcp/mcp_post.php index bb402b048d..8e7c0342e8 100644 --- a/phpBB/includes/mcp/mcp_post.php +++ b/phpBB/includes/mcp/mcp_post.php @@ -637,16 +637,9 @@ function change_poster(&$post_info, $userdata) $search_backend_factory = $phpbb_container->get('search.backend_factory'); $search = $search_backend_factory->get_active(); } - catch (RuntimeException $e) + catch (\phpbb\search\exception\no_search_backend_found_exception $e) { - if (strpos($e->getMessage(), 'No service found') === 0) - { - trigger_error('NO_SUCH_SEARCH_MODULE'); - } - else - { - throw $e; - } + trigger_error('NO_SUCH_SEARCH_MODULE'); } $search->index_remove([], [$post_info['user_id'], $userdata['user_id']], []); diff --git a/phpBB/includes/mcp/mcp_topic.php b/phpBB/includes/mcp/mcp_topic.php index 69c7800b81..77f6bea757 100644 --- a/phpBB/includes/mcp/mcp_topic.php +++ b/phpBB/includes/mcp/mcp_topic.php @@ -680,16 +680,9 @@ function split_topic($action, $topic_id, $to_forum_id, $subject) $search_backend_factory = $phpbb_container->get('search.backend_factory'); $search = $search_backend_factory->get_active(); } - catch (RuntimeException $e) + catch (\phpbb\search\exception\no_search_backend_found_exception $e) { - if (strpos($e->getMessage(), 'No service found') === 0) - { - trigger_error('NO_SUCH_SEARCH_MODULE'); - } - else - { - throw $e; - } + trigger_error('NO_SUCH_SEARCH_MODULE'); } $search->index('edit', (int) $first_post_data['post_id'], $first_post_data['post_text'], $subject, (int) $first_post_data['poster_id'], (int) $first_post_data['forum_id']); diff --git a/phpBB/language/en/cli.php b/phpBB/language/en/cli.php index 85e778509e..168d7f36e1 100644 --- a/phpBB/language/en/cli.php +++ b/phpBB/language/en/cli.php @@ -147,6 +147,7 @@ $lang = array_merge($lang, array( 'CLI_REPARSER_REPARSE_SUCCESS' => 'Reparsing ended with success', 'CLI_SEARCHINDEX_SEARCH_BACKEND_NAME' => 'Backend class', + 'CLI_SEARCHINDEX_BACKEND_NOT_FOUND' => 'Search module not found', 'CLI_SEARCHINDEX_CREATE_SUCCESS' => 'Search index created successfully', 'CLI_SEARCHINDEX_CREATE_FAILURE' => 'Error creating search index', 'CLI_SEARCHINDEX_DELETE_SUCCESS' => 'Search index deleted successfully', diff --git a/phpBB/phpbb/console/command/searchindex/create.php b/phpBB/phpbb/console/command/searchindex/create.php index 410d00f625..d21e33466c 100644 --- a/phpBB/phpbb/console/command/searchindex/create.php +++ b/phpBB/phpbb/console/command/searchindex/create.php @@ -17,6 +17,7 @@ use phpbb\config\config; use phpbb\console\command\command; use phpbb\language\language; use phpbb\log\log; +use phpbb\search\exception\no_search_backend_found_exception; use phpbb\search\search_backend_factory; use phpbb\user; use Symfony\Component\Console\Input\InputArgument; @@ -70,7 +71,7 @@ class create extends command ->addArgument( 'search-backend', InputArgument::REQUIRED, - $this->user->lang('CLI_SEARCHINDEX_SEARCH_BACKEND_NAME') + $this->language->lang('CLI_SEARCHINDEX_SEARCH_BACKEND_NAME') ) ; } @@ -90,31 +91,47 @@ class create extends command $io = new SymfonyStyle($input, $output); $search_backend = $input->getArgument('search-backend'); - $search = $this->search_backend_factory->get($search_backend); - $name = $search->get_name(); + + try + { + $search = $this->search_backend_factory->get($search_backend); + $name = $search->get_name(); + } + catch (no_search_backend_found_exception $e) + { + $io->error($this->language->lang('CLI_SEARCHINDEX_BACKEND_NOT_FOUND', $search_backend)); + return command::FAILURE; + } try { $counter = 0; + + $progress = $this->create_progress_bar(1, $io, $output, true); + $progress->start(); + while (($status = $search->create_index($counter)) !== null) { - $this->config->set('search_indexing_state', implode(',', $this->state), true); + $progress->setMaxSteps($status['max_post_id']); + $progress->setProgress($status['post_counter']); + $progress->setMessage(round($status['rows_per_second'], 2) . ' rows/s'); - $io->success($counter); - $io->success(print_r($status, 1)); + $progress->advance(); } - $search->tidy(); + $progress->finish(); + + $io->newLine(2); } catch (\Exception $e) { - $io->error($this->user->lang('CLI_SEARCHINDEX_CREATE_FAILURE', $name)); - return 1; + $io->error($this->language->lang('CLI_SEARCHINDEX_CREATE_FAILURE', $name)); + return command::FAILURE; } $this->log->add('admin', ANONYMOUS, '', 'LOG_SEARCH_INDEX_CREATED', false, array($name)); - $io->success($this->user->lang('CLI_SEARCHINDEX_CREATE_SUCCESS', $name)); + $io->success($this->language->lang('CLI_SEARCHINDEX_CREATE_SUCCESS', $name)); - return 0; + return command::SUCCESS; } } diff --git a/phpBB/phpbb/console/command/searchindex/delete.php b/phpBB/phpbb/console/command/searchindex/delete.php index 927376902c..eff25bb475 100644 --- a/phpBB/phpbb/console/command/searchindex/delete.php +++ b/phpBB/phpbb/console/command/searchindex/delete.php @@ -17,6 +17,7 @@ use phpbb\config\config; use phpbb\console\command\command; use phpbb\language\language; use phpbb\log\log; +use phpbb\search\exception\no_search_backend_found_exception; use phpbb\search\search_backend_factory; use phpbb\user; use Symfony\Component\Console\Input\InputArgument; @@ -70,7 +71,7 @@ class delete extends command ->addArgument( 'search-backend', InputArgument::REQUIRED, - $this->user->lang('CLI_SEARCHINDEX_SEARCH_BACKEND_NAME') + $this->language->lang('CLI_SEARCHINDEX_SEARCH_BACKEND_NAME') ) ; } @@ -90,40 +91,32 @@ class delete extends command $io = new SymfonyStyle($input, $output); $search_backend = $input->getArgument('search-backend'); - $search = $this->search_backend_factory->get($search_backend); - $name = $search->get_name(); try { - $this->state = explode(',', $this->config['search_indexing_state']); - $this->max_post_id = $this->get_max_post_id(); + $search = $this->search_backend_factory->get($search_backend); + $name = $search->get_name(); + } + catch (no_search_backend_found_exception $e) + { + $io->error($this->language->lang('CLI_SEARCHINDEX_BACKEND_NOT_FOUND', $search_backend)); + return command::FAILURE; + } + try + { $search->delete_index($this, ''); $search->tidy(); } catch (\Exception $e) { - $io->error($this->user->lang('CLI_SEARCHINDEX_DELETE_FAILURE', $name)); - return 1; + $io->error($this->language->lang('CLI_SEARCHINDEX_DELETE_FAILURE', $name)); + return command::FAILURE; } $this->log->add('admin', ANONYMOUS, '', 'LOG_SEARCH_INDEX_REMOVED', false, array($name)); - $io->success($this->user->lang('CLI_SEARCHINDEX_DELETE_SUCCESS', $name)); + $io->success($this->language->lang('CLI_SEARCHINDEX_DELETE_SUCCESS', $name)); - return 0; - } - - function save_state($state = false) - { - global $config; - - if ($state) - { - $this->state = $state; - } - - ksort($this->state); - - $config->set('search_indexing_state', implode(',', $this->state), true); + return command::SUCCESS; } } diff --git a/phpBB/phpbb/search/backend/base.php b/phpBB/phpbb/search/backend/base.php index 4003e102ef..4b8ff6ebc5 100644 --- a/phpBB/phpbb/search/backend/base.php +++ b/phpBB/phpbb/search/backend/base.php @@ -1,15 +1,15 @@ -* @license GNU General Public License, version 2 (GPL-2.0) -* -* For full copyright and license information, please see -* the docs/CREDITS.txt file. -* -*/ + * + * This file is part of the phpBB Forum Software package. + * + * @copyright (c) phpBB Limited + * @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\backend; @@ -19,9 +19,9 @@ use phpbb\db\driver\driver_interface; use phpbb\user; /** -* optional base class for search plugins providing simple caching based on ACM -* and functions to retrieve ignore_words and synonyms -*/ + * optional base class for search plugins providing simple caching based on ACM + * and functions to retrieve ignore_words and synonyms + */ abstract class base implements search_backend_interface { public const SEARCH_RESULT_NOT_IN_CACHE = 0; @@ -348,8 +348,17 @@ abstract class base implements search_backend_interface $row_count++; $post_counter = $row['post_id']; } + + // With cli process only one batch each time to be able to track progress + if (PHP_SAPI === 'cli') + { + break; + } } + // TODO: With cli if the previous bucle have stoped because of lack of time, launch an exception, because is an error + // cli commands should be executed in one step + // pretend the number of posts was as big as the number of ids we indexed so far // just an estimation as it includes deleted posts $num_posts = $this->config['num_posts']; @@ -399,6 +408,12 @@ abstract class base implements search_backend_interface $this->index_remove($ids, $posters, $forum_ids); $post_counter = $ids[count($ids) - 1]; } + + // With cli process only one batch each time to be able to track progress + if (PHP_SAPI === 'cli') + { + break; + } } if ($post_counter < $max_post_id) diff --git a/phpBB/phpbb/search/exception/no_search_backend_found_exception.php b/phpBB/phpbb/search/exception/no_search_backend_found_exception.php new file mode 100644 index 0000000000..237677db23 --- /dev/null +++ b/phpBB/phpbb/search/exception/no_search_backend_found_exception.php @@ -0,0 +1,19 @@ + + * @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_search_backend_found_exception extends search_exception +{ + +} \ No newline at end of file diff --git a/phpBB/phpbb/search/exception/search_exception.php b/phpBB/phpbb/search/exception/search_exception.php new file mode 100644 index 0000000000..c63a679431 --- /dev/null +++ b/phpBB/phpbb/search/exception/search_exception.php @@ -0,0 +1,21 @@ + + * @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; + +use phpbb\exception\runtime_exception; + +class search_exception extends runtime_exception +{ + // TODO: Launch this exception from search instead of RuntimeException +} \ No newline at end of file diff --git a/phpBB/phpbb/search/search_backend_factory.php b/phpBB/phpbb/search/search_backend_factory.php index eac31885ed..deabbdaba7 100644 --- a/phpBB/phpbb/search/search_backend_factory.php +++ b/phpBB/phpbb/search/search_backend_factory.php @@ -16,6 +16,8 @@ namespace phpbb\search; use phpbb\config\config; use phpbb\di\service_collection; use phpbb\search\backend\search_backend_interface; +use phpbb\search\exception\no_search_backend_found_exception; +use RuntimeException; class search_backend_factory { @@ -46,16 +48,36 @@ class search_backend_factory * * @param string $class * + * @throws no_search_backend_found_exception + * * @return search_backend_interface */ public function get(string $class): search_backend_interface { - return $this->search_backends->get_by_class($class); + try + { + $search = $this->search_backends->get_by_class($class); + } + catch (RuntimeException $e) + { + if (strpos($e->getMessage(), 'No service found') === 0) + { + throw new no_search_backend_found_exception(); + } + else + { + throw $e; + } + } + + return $search; } /** * Obtains active search backend * + * @throws no_search_backend_found_exception + * * @return search_backend_interface */ public function get_active(): search_backend_interface diff --git a/phpBB/search.php b/phpBB/search.php index f786964620..68325bda7f 100644 --- a/phpBB/search.php +++ b/phpBB/search.php @@ -299,16 +299,9 @@ if ($keywords || $author || $author_id || $search_id || $submit) $search_backend_factory = $phpbb_container->get('search.backend_factory'); $search = $search_backend_factory->get_active(); } - catch (RuntimeException $e) + catch (\phpbb\search\exception\no_search_backend_found_exception $e) { - if (strpos($e->getMessage(), 'No service found') === 0) - { - trigger_error('NO_SUCH_SEARCH_MODULE'); - } - else - { - throw $e; - } + trigger_error('NO_SUCH_SEARCH_MODULE'); } // let the search module split up the keywords From 75bdbcf4fe59c8cebcb13c03c9edfdc73e8cce3d Mon Sep 17 00:00:00 2001 From: rubencm Date: Wed, 24 Mar 2021 19:13:56 +0100 Subject: [PATCH 03/20] [ticket/12683] Fix create index command PHPBB3-12683 --- phpBB/phpbb/console/command/searchindex/create.php | 8 ++++---- phpBB/phpbb/search/backend/base.php | 4 +++- .../exception/no_search_backend_found_exception.php | 2 +- phpBB/phpbb/search/exception/search_exception.php | 2 +- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/phpBB/phpbb/console/command/searchindex/create.php b/phpBB/phpbb/console/command/searchindex/create.php index d21e33466c..7352c75be0 100644 --- a/phpBB/phpbb/console/command/searchindex/create.php +++ b/phpBB/phpbb/console/command/searchindex/create.php @@ -90,6 +90,8 @@ class create extends command { $io = new SymfonyStyle($input, $output); + $io->section($this->language->lang('CLI_DESCRIPTION_SEARCHINDEX_CREATE')); + $search_backend = $input->getArgument('search-backend'); try @@ -105,18 +107,16 @@ class create extends command try { - $counter = 0; - $progress = $this->create_progress_bar(1, $io, $output, true); + $progress->setMessage(''); $progress->start(); + $counter = 0; while (($status = $search->create_index($counter)) !== null) { $progress->setMaxSteps($status['max_post_id']); $progress->setProgress($status['post_counter']); $progress->setMessage(round($status['rows_per_second'], 2) . ' rows/s'); - - $progress->advance(); } $progress->finish(); diff --git a/phpBB/phpbb/search/backend/base.php b/phpBB/phpbb/search/backend/base.php index 4b8ff6ebc5..4fc2dd9108 100644 --- a/phpBB/phpbb/search/backend/base.php +++ b/phpBB/phpbb/search/backend/base.php @@ -391,7 +391,9 @@ abstract class base implements search_backend_interface $starttime = microtime(true); $row_count = 0; - while (still_on_time() && $post_counter <= $max_post_id) + + $still_on_time = PHP_SAPI === 'cli' ? true : still_on_time(); + while ($still_on_time && $post_counter <= $max_post_id) { $rows = $this->get_posts_batch_after($post_counter); $ids = $posters = $forum_ids = array(); diff --git a/phpBB/phpbb/search/exception/no_search_backend_found_exception.php b/phpBB/phpbb/search/exception/no_search_backend_found_exception.php index 237677db23..d5c2e43ad4 100644 --- a/phpBB/phpbb/search/exception/no_search_backend_found_exception.php +++ b/phpBB/phpbb/search/exception/no_search_backend_found_exception.php @@ -16,4 +16,4 @@ namespace phpbb\search\exception; class no_search_backend_found_exception extends search_exception { -} \ No newline at end of file +} diff --git a/phpBB/phpbb/search/exception/search_exception.php b/phpBB/phpbb/search/exception/search_exception.php index c63a679431..962d30b879 100644 --- a/phpBB/phpbb/search/exception/search_exception.php +++ b/phpBB/phpbb/search/exception/search_exception.php @@ -18,4 +18,4 @@ use phpbb\exception\runtime_exception; class search_exception extends runtime_exception { // TODO: Launch this exception from search instead of RuntimeException -} \ No newline at end of file +} From c2f3ba44bd6fa66b6ea86781cf17c41b4e761173 Mon Sep 17 00:00:00 2001 From: rubencm Date: Wed, 24 Mar 2021 19:25:03 +0100 Subject: [PATCH 04/20] [ticket/12683] Add progress bar to delete index PHPBB3-12683 --- phpBB/includes/functions.php | 1 - .../console/command/searchindex/create.php | 2 +- .../console/command/searchindex/delete.php | 22 ++++++++++++++++--- phpBB/phpbb/search/backend/base.php | 6 ++--- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index d3ba635a16..05ea03d446 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -206,7 +206,6 @@ function get_formatted_filesize($value, $string_only = true, $allowed_units = fa */ function still_on_time($extra_time = 15) { - // TODO: Check the bug with this, it should be possible to restart the start time static $max_execution_time, $start_time; $current_time = microtime(true); diff --git a/phpBB/phpbb/console/command/searchindex/create.php b/phpBB/phpbb/console/command/searchindex/create.php index 7352c75be0..e77f833ffb 100644 --- a/phpBB/phpbb/console/command/searchindex/create.php +++ b/phpBB/phpbb/console/command/searchindex/create.php @@ -86,7 +86,7 @@ class create extends command * * @return int 0 if all is well, 1 if any errors occurred */ - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); diff --git a/phpBB/phpbb/console/command/searchindex/delete.php b/phpBB/phpbb/console/command/searchindex/delete.php index eff25bb475..45176f785a 100644 --- a/phpBB/phpbb/console/command/searchindex/delete.php +++ b/phpBB/phpbb/console/command/searchindex/delete.php @@ -86,10 +86,12 @@ class delete extends command * * @return int 0 if all is well, 1 if any errors occurred */ - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); + $io->section($this->language->lang('CLI_DESCRIPTION_SEARCHINDEX_DELETE')); + $search_backend = $input->getArgument('search-backend'); try @@ -105,8 +107,22 @@ class delete extends command try { - $search->delete_index($this, ''); - $search->tidy(); + // 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->setMessage(''); + $progress->start(); + + $counter = 0; + while (($status = $search->delete_index($counter)) !== null) + { + $progress->setMaxSteps($status['max_post_id']); + $progress->setProgress($status['post_counter']); + $progress->setMessage(round($status['rows_per_second'], 2) . ' rows/s'); + } + + $progress->finish(); + + $io->newLine(2); } catch (\Exception $e) { diff --git a/phpBB/phpbb/search/backend/base.php b/phpBB/phpbb/search/backend/base.php index 4fc2dd9108..3a0049947d 100644 --- a/phpBB/phpbb/search/backend/base.php +++ b/phpBB/phpbb/search/backend/base.php @@ -329,7 +329,8 @@ abstract class base implements search_backend_interface $starttime = microtime(true); $row_count = 0; - while (still_on_time() && $post_counter <= $max_post_id) + $still_on_time = PHP_SAPI === 'cli' ? true : still_on_time(); + while ($still_on_time && $post_counter <= $max_post_id) { $rows = $this->get_posts_batch_after($post_counter); @@ -356,9 +357,6 @@ abstract class base implements search_backend_interface } } - // TODO: With cli if the previous bucle have stoped because of lack of time, launch an exception, because is an error - // cli commands should be executed in one step - // pretend the number of posts was as big as the number of ids we indexed so far // just an estimation as it includes deleted posts $num_posts = $this->config['num_posts']; From 0c3e084d3518eec9bacf4d4a1854a1b3a189e04d Mon Sep 17 00:00:00 2001 From: rubencm Date: Wed, 24 Mar 2021 20:36:40 +0100 Subject: [PATCH 05/20] [ticket/12683] Return constant PHPBB3-12683 --- phpBB/phpbb/console/command/searchindex/list_all.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/phpbb/console/command/searchindex/list_all.php b/phpBB/phpbb/console/command/searchindex/list_all.php index 6faeaffa8f..3db054f389 100644 --- a/phpBB/phpbb/console/command/searchindex/list_all.php +++ b/phpBB/phpbb/console/command/searchindex/list_all.php @@ -87,6 +87,6 @@ class list_all extends command $io->listing($search_backends); - return 0; + return command::SUCCESS; } } From f1a2558cfe8b0ead453dcb556440719462c1af40 Mon Sep 17 00:00:00 2001 From: rubencm Date: Sun, 28 Mar 2021 06:18:14 +0200 Subject: [PATCH 06/20] [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 --- phpBB/config/default/container/services.yml | 1 + .../default/container/services_console.yml | 2 + .../default/container/services_post.yml | 5 ++ phpBB/language/en/cli.php | 4 ++ .../console/command/searchindex/create.php | 65 ++++++++++++++++--- .../console/command/searchindex/delete.php | 65 ++++++++++++++++--- .../console/command/searchindex/list_all.php | 11 +++- phpBB/phpbb/post/post_helper.php | 44 +++++++++++++ phpBB/phpbb/search/backend/base.php | 12 ++++ phpBB/phpbb/search/backend/fulltext_mysql.php | 12 ++++ .../phpbb/search/backend/fulltext_native.php | 6 ++ .../search/backend/fulltext_postgres.php | 12 ++++ .../phpbb/search/backend/fulltext_sphinx.php | 42 +++++++----- .../exception/index_created_exception.php | 20 ++++++ .../exception/index_empty_exception.php | 20 ++++++ 15 files changed, 282 insertions(+), 39 deletions(-) create mode 100644 phpBB/config/default/container/services_post.yml create mode 100644 phpBB/phpbb/post/post_helper.php create mode 100644 phpBB/phpbb/search/exception/index_created_exception.php create mode 100644 phpBB/phpbb/search/exception/index_empty_exception.php diff --git a/phpBB/config/default/container/services.yml b/phpBB/config/default/container/services.yml index 73260f7a92..547053631b 100644 --- a/phpBB/config/default/container/services.yml +++ b/phpBB/config/default/container/services.yml @@ -23,6 +23,7 @@ imports: - { resource: services_notification.yml } - { resource: services_password.yml } - { resource: services_php.yml } + - { resource: services_post.yml } - { resource: services_profilefield.yml } - { resource: services_report.yml } - { resource: services_routing.yml } diff --git a/phpBB/config/default/container/services_console.yml b/phpBB/config/default/container/services_console.yml index 422a99992f..a7e3f7b136 100644 --- a/phpBB/config/default/container/services_console.yml +++ b/phpBB/config/default/container/services_console.yml @@ -262,6 +262,7 @@ services: - '@config' - '@language' - '@log' + - '@post.helper' - '@search.backend_factory' - '@user' tags: @@ -273,6 +274,7 @@ services: - '@config' - '@language' - '@log' + - '@post.helper' - '@search.backend_factory' - '@user' tags: diff --git a/phpBB/config/default/container/services_post.yml b/phpBB/config/default/container/services_post.yml new file mode 100644 index 0000000000..f6ad5ba80f --- /dev/null +++ b/phpBB/config/default/container/services_post.yml @@ -0,0 +1,5 @@ +services: + post.helper: + class: phpbb\post\post_helper + arguments: + - '@dbal.conn' diff --git a/phpBB/language/en/cli.php b/phpBB/language/en/cli.php index 168d7f36e1..82c1c3d62a 100644 --- a/phpBB/language/en/cli.php +++ b/phpBB/language/en/cli.php @@ -152,6 +152,10 @@ $lang = array_merge($lang, array( 'CLI_SEARCHINDEX_CREATE_FAILURE' => 'Error creating search index', 'CLI_SEARCHINDEX_DELETE_SUCCESS' => 'Search index deleted successfully', 'CLI_SEARCHINDEX_DELETE_FAILURE' => 'Error deleting search index', + 'CLI_SEARCHINDEX_ALREADY_CREATED' => 'Search index is already created, try removing it first', + 'CLI_SEARCHINDEX_NO_CREATED' => 'Search index is already empty, try creating it first', + 'CLI_SEARCHINDEX_ACTION_IN_PROGRESS' => 'There is an action currently in progress. CLI doesn\'t support incomplete index/delete actions, please solve it from the ACP', + 'CLI_SEARCHINDEX_ACTIVE_NOT_INDEXED' => 'Active search backend isn\'t indexed', // In all the case %1$s is the logical name of the file and %2$s the real name on the filesystem // eg: big_image.png (2_a51529ae7932008cf8454a95af84cacd) generated. diff --git a/phpBB/phpbb/console/command/searchindex/create.php b/phpBB/phpbb/console/command/searchindex/create.php index e77f833ffb..9985373d20 100644 --- a/phpBB/phpbb/console/command/searchindex/create.php +++ b/phpBB/phpbb/console/command/searchindex/create.php @@ -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); + } } diff --git a/phpBB/phpbb/console/command/searchindex/delete.php b/phpBB/phpbb/console/command/searchindex/delete.php index 45176f785a..ec101ac084 100644 --- a/phpBB/phpbb/console/command/searchindex/delete.php +++ b/phpBB/phpbb/console/command/searchindex/delete.php @@ -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); + } } diff --git a/phpBB/phpbb/console/command/searchindex/list_all.php b/phpBB/phpbb/console/command/searchindex/list_all.php index 3db054f389..cfa26e7893 100644 --- a/phpBB/phpbb/console/command/searchindex/list_all.php +++ b/phpBB/phpbb/console/command/searchindex/list_all.php @@ -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']) ? '(' . $this->language->lang('ACTIVE') . ') ' : ''; + $name = $search_backend->get_type(); + $active = ($name === $this->config['search_type']) ? '(' . $this->language->lang('ACTIVE') . ') ' : ''; $search_backends[] = '' . $name . ' ' . $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); diff --git a/phpBB/phpbb/post/post_helper.php b/phpBB/phpbb/post/post_helper.php new file mode 100644 index 0000000000..f039771e10 --- /dev/null +++ b/phpBB/phpbb/post/post_helper.php @@ -0,0 +1,44 @@ + + * @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\post; + + +use phpbb\db\driver\driver_interface; + +class post_helper +{ + /** + * @var driver_interface + */ + protected $db; + + public function __construct(driver_interface $db) + { + $this->db = $db; + } + + /** + * Get last post id + */ + public function get_max_post_id(): int + { + $sql = 'SELECT MAX(post_id) as max_post_id + FROM '. POSTS_TABLE; + $result = $this->db->sql_query($sql); + $max_post_id = (int) $this->db->sql_fetchfield('max_post_id'); + $this->db->sql_freeresult($result); + + return $max_post_id; + } +} diff --git a/phpBB/phpbb/search/backend/base.php b/phpBB/phpbb/search/backend/base.php index 3a0049947d..e1b0f142a2 100644 --- a/phpBB/phpbb/search/backend/base.php +++ b/phpBB/phpbb/search/backend/base.php @@ -16,6 +16,8 @@ namespace phpbb\search\backend; use phpbb\cache\service; use phpbb\config\config; use phpbb\db\driver\driver_interface; +use phpbb\search\exception\index_created_exception; +use phpbb\search\exception\index_empty_exception; use phpbb\user; /** @@ -323,6 +325,11 @@ abstract class base implements search_backend_interface */ public function create_index(int &$post_counter = 0): ?array { + if ($this->index_created()) + { + throw new index_created_exception(); + } + $max_post_id = $this->get_max_post_id(); $forums_indexing_enabled = $this->forum_ids_with_indexing_enabled(); @@ -385,6 +392,11 @@ abstract class base implements search_backend_interface */ public function delete_index(int &$post_counter = null): ?array { + if (!$this->index_created()) + { + throw new index_empty_exception(); + } + $max_post_id = $this->get_max_post_id(); $starttime = microtime(true); diff --git a/phpBB/phpbb/search/backend/fulltext_mysql.php b/phpBB/phpbb/search/backend/fulltext_mysql.php index 3e47cb4668..febaee3495 100644 --- a/phpBB/phpbb/search/backend/fulltext_mysql.php +++ b/phpBB/phpbb/search/backend/fulltext_mysql.php @@ -17,6 +17,8 @@ use phpbb\config\config; use phpbb\db\driver\driver_interface; use phpbb\event\dispatcher_interface; use phpbb\language\language; +use phpbb\search\exception\index_created_exception; +use phpbb\search\exception\index_empty_exception; use phpbb\user; use RuntimeException; @@ -913,6 +915,11 @@ class fulltext_mysql extends base implements search_backend_interface */ public function create_index(int &$post_counter = 0): ?array { + if ($this->index_created()) + { + throw new index_created_exception(); + } + // Make sure we can actually use MySQL with fulltext indexes if ($error = $this->init()) { @@ -985,6 +992,11 @@ class fulltext_mysql extends base implements search_backend_interface */ public function delete_index(int &$post_counter = null): ?array { + if (!$this->index_created()) + { + throw new index_empty_exception(); + } + // Make sure we can actually use MySQL with fulltext indexes if ($error = $this->init()) { diff --git a/phpBB/phpbb/search/backend/fulltext_native.php b/phpBB/phpbb/search/backend/fulltext_native.php index a5c9bb8202..f4d141dd59 100644 --- a/phpBB/phpbb/search/backend/fulltext_native.php +++ b/phpBB/phpbb/search/backend/fulltext_native.php @@ -17,6 +17,7 @@ use phpbb\config\config; use phpbb\db\driver\driver_interface; use phpbb\event\dispatcher_interface; use phpbb\language\language; +use phpbb\search\exception\index_empty_exception; use phpbb\user; /** @@ -1598,6 +1599,11 @@ class fulltext_native extends base implements search_backend_interface */ public function delete_index(int &$post_counter = null): ?array { + if (!$this->index_created()) + { + throw new index_empty_exception(); + } + $sql_queries = []; switch ($this->db->get_sql_layer()) diff --git a/phpBB/phpbb/search/backend/fulltext_postgres.php b/phpBB/phpbb/search/backend/fulltext_postgres.php index bd2c24224c..077eee4f30 100644 --- a/phpBB/phpbb/search/backend/fulltext_postgres.php +++ b/phpBB/phpbb/search/backend/fulltext_postgres.php @@ -17,6 +17,8 @@ use phpbb\config\config; use phpbb\db\driver\driver_interface; use phpbb\event\dispatcher_interface; use phpbb\language\language; +use phpbb\search\exception\index_created_exception; +use phpbb\search\exception\index_empty_exception; use phpbb\user; use RuntimeException; @@ -868,6 +870,11 @@ class fulltext_postgres extends base implements search_backend_interface */ public function create_index(int &$post_counter = 0): ?array { + if ($this->index_created()) + { + throw new index_created_exception(); + } + // Make sure we can actually use PostgreSQL with fulltext indexes if ($error = $this->init()) { @@ -927,6 +934,11 @@ class fulltext_postgres extends base implements search_backend_interface */ public function delete_index(int &$post_counter = null): ?array { + if (!$this->index_created()) + { + throw new index_empty_exception(); + } + // Make sure we can actually use PostgreSQL with fulltext indexes if ($error = $this->init()) { diff --git a/phpBB/phpbb/search/backend/fulltext_sphinx.php b/phpBB/phpbb/search/backend/fulltext_sphinx.php index 709690b4f2..74ff23bbf8 100644 --- a/phpBB/phpbb/search/backend/fulltext_sphinx.php +++ b/phpBB/phpbb/search/backend/fulltext_sphinx.php @@ -20,6 +20,7 @@ use phpbb\db\tools\tools_interface; use phpbb\event\dispatcher_interface; use phpbb\language\language; use phpbb\log\log; +use phpbb\search\exception\index_empty_exception; use phpbb\user; /** @@ -633,23 +634,28 @@ class fulltext_sphinx implements search_backend_interface { if (!$this->index_created()) { - $table_data = array( - 'COLUMNS' => array( - 'counter_id' => array('UINT', 0), - 'max_doc_id' => array('UINT', 0), - ), - 'PRIMARY_KEY' => 'counter_id', - ); - $this->db_tools->sql_create_table(SPHINX_TABLE, $table_data); - - $data = array( - 'counter_id' => '1', - 'max_doc_id' => '0', - ); - $sql = 'INSERT INTO ' . SPHINX_TABLE . ' ' . $this->db->sql_build_array('INSERT', $data); - $this->db->sql_query($sql); + throw new index_empty_exception(); } + $table_data = array( + 'COLUMNS' => array( + 'counter_id' => array('UINT', 0), + 'max_doc_id' => array('UINT', 0), + ), + 'PRIMARY_KEY' => 'counter_id', + ); + $this->db_tools->sql_create_table(SPHINX_TABLE, $table_data); + + $sql = 'TRUNCATE TABLE ' . SPHINX_TABLE; + $this->db->sql_query($sql); + + $data = array( + 'counter_id' => '1', + 'max_doc_id' => '0', + ); + $sql = 'INSERT INTO ' . SPHINX_TABLE . ' ' . $this->db->sql_build_array('INSERT', $data); + $this->db->sql_query($sql); + return null; } @@ -658,11 +664,13 @@ class fulltext_sphinx implements search_backend_interface */ public function delete_index(int &$post_counter = null): ?array { - if ($this->index_created()) + if (!$this->index_created()) { - $this->db_tools->sql_table_drop(SPHINX_TABLE); + throw new index_empty_exception(); } + $this->db_tools->sql_table_drop(SPHINX_TABLE); + return null; } diff --git a/phpBB/phpbb/search/exception/index_created_exception.php b/phpBB/phpbb/search/exception/index_created_exception.php new file mode 100644 index 0000000000..9be91bfbb8 --- /dev/null +++ b/phpBB/phpbb/search/exception/index_created_exception.php @@ -0,0 +1,20 @@ + + * @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 index_created_exception extends search_exception +{ + +} diff --git a/phpBB/phpbb/search/exception/index_empty_exception.php b/phpBB/phpbb/search/exception/index_empty_exception.php new file mode 100644 index 0000000000..a6a55698de --- /dev/null +++ b/phpBB/phpbb/search/exception/index_empty_exception.php @@ -0,0 +1,20 @@ + + * @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 index_empty_exception extends search_exception +{ + +} From 565c806eda902df8acbc975a3210eedceaca05e7 Mon Sep 17 00:00:00 2001 From: rubencm Date: Fri, 9 Apr 2021 18:20:50 +0200 Subject: [PATCH 07/20] [ticket/12683] Move state related code to helper class PHPBB3-12683 --- .../default/container/services_console.yml | 2 + .../default/container/services_search.yml | 6 + phpBB/includes/acp/acp_search.php | 56 ++---- .../console/command/searchindex/create.php | 52 ++---- .../console/command/searchindex/delete.php | 51 ++---- .../action_in_progress_exception.php | 20 +++ .../no_action_in_progress_exception.php | 20 +++ phpBB/phpbb/search/state_helper.php | 170 ++++++++++++++++++ 8 files changed, 275 insertions(+), 102 deletions(-) create mode 100644 phpBB/phpbb/search/exception/action_in_progress_exception.php create mode 100644 phpBB/phpbb/search/exception/no_action_in_progress_exception.php create mode 100644 phpBB/phpbb/search/state_helper.php diff --git a/phpBB/config/default/container/services_console.yml b/phpBB/config/default/container/services_console.yml index a7e3f7b136..ff29a83e22 100644 --- a/phpBB/config/default/container/services_console.yml +++ b/phpBB/config/default/container/services_console.yml @@ -264,6 +264,7 @@ services: - '@log' - '@post.helper' - '@search.backend_factory' + - '@search.state_helper' - '@user' tags: - { name: console.command } @@ -276,6 +277,7 @@ services: - '@log' - '@post.helper' - '@search.backend_factory' + - '@search.state_helper' - '@user' tags: - { name: console.command } diff --git a/phpBB/config/default/container/services_search.yml b/phpBB/config/default/container/services_search.yml index 1dba3732be..dc29617bf8 100644 --- a/phpBB/config/default/container/services_search.yml +++ b/phpBB/config/default/container/services_search.yml @@ -1,5 +1,11 @@ services: + search.state_helper: + class: phpbb\search\state_helper + arguments: + - '@config' + - '@search.backend_collection' + # Search backends search.fulltext.mysql: class: phpbb\search\backend\fulltext_mysql diff --git a/phpBB/includes/acp/acp_search.php b/phpBB/includes/acp/acp_search.php index e2cbea4956..667e74baaa 100644 --- a/phpBB/includes/acp/acp_search.php +++ b/phpBB/includes/acp/acp_search.php @@ -21,6 +21,7 @@ use phpbb\language\language; use phpbb\log\log; use phpbb\request\request; use phpbb\search\search_backend_factory; +use phpbb\search\state_helper; use phpbb\template\template; use phpbb\user; @@ -35,10 +36,6 @@ class acp_search public $tpl_name; public $page_title; - protected const STATE_SEARCH_TYPE = 0; - protected const STATE_ACTION = 1; - protected const STATE_POST_COUNTER = 2; - /** @var config */ protected $config; @@ -57,6 +54,9 @@ class acp_search /** @var search_backend_factory */ protected $search_backend_factory; + /** @var state_helper */ + protected $search_state_helper; + /** @var template */ protected $template; @@ -79,6 +79,7 @@ class acp_search $this->request = $request; $this->search_backend_collection = $phpbb_container->get('search.backend_collection'); $this->search_backend_factory = $phpbb_container->get('search.backend_factory'); + $this->search_state_helper = $phpbb_container->get('search.state_helper'); $this->template = $template; $this->user = $user; $this->phpbb_admin_path = $phpbb_admin_path; @@ -272,7 +273,6 @@ class acp_search public function index(string $id, string $mode): void { $action = $this->request->variable('action', ''); - $state = !empty($this->config['search_indexing_state']) ? explode(',', $this->config['search_indexing_state']) : []; if ($action && !$this->request->is_set_post('cancel')) { @@ -284,7 +284,7 @@ class acp_search case 'create': case 'delete': - $this->index_action($id, $mode, $action, $state); + $this->index_action($id, $mode, $action); break; default: @@ -296,13 +296,12 @@ class acp_search // If clicked to cancel the indexing progress (acp_search_index_inprogress form) if ($this->request->is_set_post('cancel')) { - $state = []; - $this->save_state($state); + $this->search_state_helper->clear_state(); } - if (!empty($state)) + if ($this->search_state_helper->is_action_in_progress()) { - $this->index_inprogress($id, $mode, $state[self::STATE_ACTION]); + $this->index_inprogress($id, $mode); } else { @@ -368,9 +367,8 @@ class acp_search * @param string $id * @param string $mode * @param string $action - * @param array $state */ - private function index_action(string $id, string $mode, string $action, array $state): void + private function index_action(string $id, string $mode, string $action): void { // For some this may be of help... @ini_set('memory_limit', '128M'); @@ -381,29 +379,23 @@ class acp_search } // Entering here for the first time - if (empty($state)) + if (!$this->search_state_helper->is_action_in_progress()) { if ($this->request->is_set_post('search_type', '')) { - $state = [ - self::STATE_SEARCH_TYPE => $this->request->variable('search_type', ''), - self::STATE_ACTION => $action, - self::STATE_POST_COUNTER => 0 - ]; + $this->search_state_helper->init($this->request->variable('search_type', ''), $action); } else { trigger_error($this->language->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING); } - - $this->save_state($state); // Create new state in the database } - $type = $state[self::STATE_SEARCH_TYPE]; - $action = $state[self::STATE_ACTION]; - $post_counter = &$state[self::STATE_POST_COUNTER]; - // Execute create/delete + $type = $this->search_state_helper->type(); + $action = $this->search_state_helper->action(); + $post_counter = $this->search_state_helper->counter(); + $search = $this->search_backend_factory->get($type); try @@ -411,7 +403,7 @@ class acp_search $status = ($action == 'create') ? $search->create_index($post_counter) : $search->delete_index($post_counter); if ($status) // Status is not null, so action is in progress.... { - $this->save_state($state); // update $post_counter in $state in the database + $this->search_state_helper->update_counter($status['post_counter']); $u_action = append_sid($this->phpbb_admin_path . "index." . $this->php_ex, "i=$id&mode=$mode&action=$action&hash=" . generate_link_hash('acp_search'), false); meta_refresh(1, $u_action); @@ -423,13 +415,13 @@ class acp_search } catch (Exception $e) { - $this->save_state([]); // Unexpected error, cancel action + $this->search_state_helper->clear_state(); // Unexpected error, cancel action trigger_error($e->getMessage() . adm_back_link($this->u_action) . $this->close_popup_js(), E_USER_WARNING); } $search->tidy(); - $this->save_state([]); // finished operation, cancel action + $this->search_state_helper->clear_state(); // finished operation, cancel action $log_operation = ($action == 'create') ? 'LOG_SEARCH_INDEX_CREATED' : 'LOG_SEARCH_INDEX_REMOVED'; $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, $log_operation, false, [$search->get_name()]); @@ -473,14 +465,4 @@ class acp_search "// ]]>\n" . "\n"; } - - /** - * @param array $state - */ - private function save_state(array $state = []): void - { - ksort($state); - - $this->config->set('search_indexing_state', implode(',', $state), true); - } } diff --git a/phpBB/phpbb/console/command/searchindex/create.php b/phpBB/phpbb/console/command/searchindex/create.php index 9985373d20..2ee2c0f35a 100644 --- a/phpBB/phpbb/console/command/searchindex/create.php +++ b/phpBB/phpbb/console/command/searchindex/create.php @@ -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); - } } diff --git a/phpBB/phpbb/console/command/searchindex/delete.php b/phpBB/phpbb/console/command/searchindex/delete.php index ec101ac084..c2ba8f0476 100644 --- a/phpBB/phpbb/console/command/searchindex/delete.php +++ b/phpBB/phpbb/console/command/searchindex/delete.php @@ -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); - } } diff --git a/phpBB/phpbb/search/exception/action_in_progress_exception.php b/phpBB/phpbb/search/exception/action_in_progress_exception.php new file mode 100644 index 0000000000..183c00f512 --- /dev/null +++ b/phpBB/phpbb/search/exception/action_in_progress_exception.php @@ -0,0 +1,20 @@ + + * @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 +{ + +} diff --git a/phpBB/phpbb/search/exception/no_action_in_progress_exception.php b/phpBB/phpbb/search/exception/no_action_in_progress_exception.php new file mode 100644 index 0000000000..4c04b8c720 --- /dev/null +++ b/phpBB/phpbb/search/exception/no_action_in_progress_exception.php @@ -0,0 +1,20 @@ + + * @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 +{ + +} diff --git a/phpBB/phpbb/search/state_helper.php b/phpBB/phpbb/search/state_helper.php new file mode 100644 index 0000000000..b3e5dd0aea --- /dev/null +++ b/phpBB/phpbb/search/state_helper.php @@ -0,0 +1,170 @@ + + * @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); + } +} From f4977853be8447e9c0ffea2147fbbf085902c91d Mon Sep 17 00:00:00 2001 From: rubencm Date: Fri, 9 Apr 2021 19:36:54 +0200 Subject: [PATCH 08/20] [ticket/12683] Pass tables via parameter and small fix PHPBB3-12683 --- .../default/container/services_search.yml | 7 +- phpBB/includes/acp/acp_search.php | 29 +++--- .../console/command/searchindex/create.php | 2 +- .../console/command/searchindex/delete.php | 2 +- phpBB/phpbb/post/post_helper.php | 1 - phpBB/phpbb/search/backend/base.php | 27 +++-- phpBB/phpbb/search/backend/fulltext_mysql.php | 27 ++--- .../phpbb/search/backend/fulltext_native.php | 98 +++++++++++-------- .../search/backend/fulltext_postgres.php | 27 ++--- .../phpbb/search/backend/fulltext_sphinx.php | 2 +- .../action_in_progress_exception.php | 1 - .../exception/index_created_exception.php | 1 - .../exception/index_empty_exception.php | 1 - .../no_action_in_progress_exception.php | 1 - .../search/exception/search_exception.php | 2 +- phpBB/phpbb/search/state_helper.php | 7 +- 16 files changed, 132 insertions(+), 103 deletions(-) diff --git a/phpBB/config/default/container/services_search.yml b/phpBB/config/default/container/services_search.yml index dc29617bf8..fceb254eaa 100644 --- a/phpBB/config/default/container/services_search.yml +++ b/phpBB/config/default/container/services_search.yml @@ -4,7 +4,7 @@ services: class: phpbb\search\state_helper arguments: - '@config' - - '@search.backend_collection' + - '@search.backend_factory' # Search backends search.fulltext.mysql: @@ -15,6 +15,7 @@ services: - '@dispatcher' - '@language' - '@user' + - '%tables.search_results%' - '%core.root_path%' - '%core.php_ext%' tags: @@ -28,6 +29,9 @@ services: - '@dispatcher' - '@language' - '@user' + - '%tables.search_results%' + - '%tables.search_wordlist%' + - '%tables.search_wordmatch%' - '%core.root_path%' - '%core.php_ext%' tags: @@ -41,6 +45,7 @@ services: - '@dispatcher' - '@language' - '@user' + - '%tables.search_results%' - '%core.root_path%' - '%core.php_ext%' tags: diff --git a/phpBB/includes/acp/acp_search.php b/phpBB/includes/acp/acp_search.php index 667e74baaa..cece03476b 100644 --- a/phpBB/includes/acp/acp_search.php +++ b/phpBB/includes/acp/acp_search.php @@ -323,21 +323,24 @@ class acp_search foreach ($this->search_backend_collection as $search) { - $this->template->assign_block_vars('backends', [ - 'NAME' => $search->get_name(), - 'TYPE' => $search->get_type(), + if ($search->is_available()) + { + $this->template->assign_block_vars('backends', [ + 'NAME' => $search->get_name(), + 'TYPE' => $search->get_type(), - 'S_ACTIVE' => $search->get_type() === $this->config['search_type'], - 'S_HIDDEN_FIELDS' => build_hidden_fields(['search_type' => $search->get_type()]), - 'S_INDEXED' => $search->index_created(), - 'S_STATS' => $search->index_stats(), - ]); + 'S_ACTIVE' => $search->get_type() === $this->config['search_type'], + 'S_HIDDEN_FIELDS' => build_hidden_fields(['search_type' => $search->get_type()]), + 'S_INDEXED' => $search->index_created(), + 'S_STATS' => $search->index_stats(), + ]); + + $this->template->assign_vars([ + 'U_ACTION' => $this->u_action . '&hash=' . generate_link_hash('acp_search'), + 'UA_PROGRESS_BAR' => addslashes($this->u_action . '&action=progress_bar'), + ]); + } } - - $this->template->assign_vars([ - 'U_ACTION' => $this->u_action . '&hash=' . generate_link_hash('acp_search'), - 'UA_PROGRESS_BAR' => addslashes($this->u_action . '&action=progress_bar'), - ]); } /** diff --git a/phpBB/phpbb/console/command/searchindex/create.php b/phpBB/phpbb/console/command/searchindex/create.php index 2ee2c0f35a..0bd291b99e 100644 --- a/phpBB/phpbb/console/command/searchindex/create.php +++ b/phpBB/phpbb/console/command/searchindex/create.php @@ -146,7 +146,7 @@ class create extends command $io->newLine(2); } - catch(index_created_exception $e) + catch (index_created_exception $e) { $this->state_helper->clear_state(); $io->error($this->language->lang('CLI_SEARCHINDEX_ALREADY_CREATED', $name)); diff --git a/phpBB/phpbb/console/command/searchindex/delete.php b/phpBB/phpbb/console/command/searchindex/delete.php index c2ba8f0476..8ffaa808f3 100644 --- a/phpBB/phpbb/console/command/searchindex/delete.php +++ b/phpBB/phpbb/console/command/searchindex/delete.php @@ -146,7 +146,7 @@ class delete extends command $io->newLine(2); } - catch(index_empty_exception $e) + catch (index_empty_exception $e) { $this->state_helper->clear_state(); $io->error($this->language->lang('CLI_SEARCHINDEX_NO_CREATED', $name)); diff --git a/phpBB/phpbb/post/post_helper.php b/phpBB/phpbb/post/post_helper.php index f039771e10..a70d38fb1c 100644 --- a/phpBB/phpbb/post/post_helper.php +++ b/phpBB/phpbb/post/post_helper.php @@ -13,7 +13,6 @@ namespace phpbb\post; - use phpbb\db\driver\driver_interface; class post_helper diff --git a/phpBB/phpbb/search/backend/base.php b/phpBB/phpbb/search/backend/base.php index e1b0f142a2..e554e0cc71 100644 --- a/phpBB/phpbb/search/backend/base.php +++ b/phpBB/phpbb/search/backend/base.php @@ -53,20 +53,27 @@ abstract class base implements search_backend_interface */ protected $user; + /** + * @var string + */ + protected $search_results_table; + /** * Constructor. * - * @param service $cache - * @param config $config + * @param service $cache + * @param config $config * @param driver_interface $db - * @param user $user + * @param user $user + * @param string $search_results_table */ - public function __construct(service $cache, config $config, driver_interface $db, user $user) + public function __construct(service $cache, config $config, driver_interface $db, user $user, string $search_results_table) { $this->cache = $cache; $this->config = $config; $this->db = $db; $this->user = $user; + $this->search_results_table = $search_results_table; } /** @@ -182,7 +189,7 @@ abstract class base implements search_backend_interface if (!empty($keywords) || count($author_ary)) { $sql = 'SELECT search_time - FROM ' . SEARCH_RESULTS_TABLE . ' + FROM ' . $this->search_results_table . ' WHERE search_key = \'' . $this->db->sql_escape($search_key) . '\''; $result = $this->db->sql_query($sql); @@ -195,7 +202,7 @@ abstract class base implements search_backend_interface 'search_authors' => ' ' . implode(' ', $author_ary) . ' ' ); - $sql = 'INSERT INTO ' . SEARCH_RESULTS_TABLE . ' ' . $this->db->sql_build_array('INSERT', $sql_ary); + $sql = 'INSERT INTO ' . $this->search_results_table . ' ' . $this->db->sql_build_array('INSERT', $sql_ary); $this->db->sql_query($sql); } $this->db->sql_freeresult($result); @@ -255,7 +262,7 @@ abstract class base implements search_backend_interface } $this->cache->put('_search_results_' . $search_key, $store, $this->config['search_store_results']); - $sql = 'UPDATE ' . SEARCH_RESULTS_TABLE . ' + $sql = 'UPDATE ' . $this->search_results_table . ' SET search_time = ' . time() . ' WHERE search_key = \'' . $this->db->sql_escape($search_key) . '\''; $this->db->sql_query($sql); @@ -282,7 +289,7 @@ abstract class base implements search_backend_interface } $sql = 'SELECT search_key - FROM ' . SEARCH_RESULTS_TABLE . " + FROM ' . $this->search_results_table . " WHERE search_keywords LIKE '%*%' $sql_where"; $result = $this->db->sql_query($sql); @@ -303,7 +310,7 @@ abstract class base implements search_backend_interface } $sql = 'SELECT search_key - FROM ' . SEARCH_RESULTS_TABLE . " + FROM ' . $this->search_results_table . " WHERE $sql_where"; $result = $this->db->sql_query($sql); @@ -315,7 +322,7 @@ abstract class base implements search_backend_interface } $sql = 'DELETE - FROM ' . SEARCH_RESULTS_TABLE . ' + FROM ' . $this->search_results_table . ' WHERE search_time < ' . (time() - (int) $this->config['search_store_results']); $this->db->sql_query($sql); } diff --git a/phpBB/phpbb/search/backend/fulltext_mysql.php b/phpBB/phpbb/search/backend/fulltext_mysql.php index febaee3495..e9fce05fc1 100644 --- a/phpBB/phpbb/search/backend/fulltext_mysql.php +++ b/phpBB/phpbb/search/backend/fulltext_mysql.php @@ -19,8 +19,8 @@ use phpbb\event\dispatcher_interface; use phpbb\language\language; use phpbb\search\exception\index_created_exception; use phpbb\search\exception\index_empty_exception; +use phpbb\search\exception\search_exception; use phpbb\user; -use RuntimeException; /** * Fulltext search for MySQL @@ -74,19 +74,20 @@ class fulltext_mysql extends base implements search_backend_interface * Constructor * Creates a new \phpbb\search\backend\fulltext_mysql, which is used as a search backend * - * @param config $config Config object - * @param driver_interface $db Database object + * @param config $config Config object + * @param driver_interface $db Database object * @param dispatcher_interface $phpbb_dispatcher Event dispatcher object - * @param language $language - * @param user $user User object - * @param string $phpbb_root_path Relative path to phpBB root - * @param string $phpEx PHP file extension + * @param language $language + * @param user $user User object + * @param string $search_results_table + * @param string $phpbb_root_path Relative path to phpBB root + * @param string $phpEx PHP file extension */ - public function __construct(config $config, driver_interface $db, dispatcher_interface $phpbb_dispatcher, language $language, user $user, string $phpbb_root_path, string $phpEx) + public function __construct(config $config, driver_interface $db, dispatcher_interface $phpbb_dispatcher, language $language, user $user, string $search_results_table, string $phpbb_root_path, string $phpEx) { global $cache; - parent::__construct($cache, $config, $db, $user); + parent::__construct($cache, $config, $db, $user, $search_results_table); $this->phpbb_dispatcher = $phpbb_dispatcher; $this->language = $language; @@ -923,7 +924,7 @@ class fulltext_mysql extends base implements search_backend_interface // Make sure we can actually use MySQL with fulltext indexes if ($error = $this->init()) { - throw new RuntimeException($error); + throw new search_exception($error); } if (empty($this->stats)) @@ -982,7 +983,7 @@ class fulltext_mysql extends base implements search_backend_interface $this->db->sql_query($sql_query); } - $this->db->sql_query('TRUNCATE TABLE ' . SEARCH_RESULTS_TABLE); + $this->db->sql_query('TRUNCATE TABLE ' . $this->search_results_table); return null; } @@ -1000,7 +1001,7 @@ class fulltext_mysql extends base implements search_backend_interface // Make sure we can actually use MySQL with fulltext indexes if ($error = $this->init()) { - throw new RuntimeException($error); + throw new search_exception($error); } if (empty($this->stats)) @@ -1053,7 +1054,7 @@ class fulltext_mysql extends base implements search_backend_interface $this->db->sql_query($sql_query); } - $this->db->sql_query('TRUNCATE TABLE ' . SEARCH_RESULTS_TABLE); + $this->db->sql_query('TRUNCATE TABLE ' . $this->search_results_table); return null; } diff --git a/phpBB/phpbb/search/backend/fulltext_native.php b/phpBB/phpbb/search/backend/fulltext_native.php index f4d141dd59..dac6ebf803 100644 --- a/phpBB/phpbb/search/backend/fulltext_native.php +++ b/phpBB/phpbb/search/backend/fulltext_native.php @@ -99,25 +99,41 @@ class fulltext_native extends base implements search_backend_interface */ protected $language; + /** + * @var string + */ + protected $search_wordlist_table; + + /** + * @var string + */ + protected $search_wordmatch_table; + /** * Initialises the fulltext_native search backend with min/max word length * - * @param config $config Config object - * @param driver_interface $db Database object + * @param config $config Config object + * @param driver_interface $db Database object * @param dispatcher_interface $phpbb_dispatcher Event dispatcher object - * @param language $language - * @param user $user User object - * @param string $phpbb_root_path phpBB root path - * @param string $phpEx PHP file extension + * @param language $language + * @param user $user User object + * @param string $search_results_table + * @param string $search_wordlist_table + * @param string $search_wordmatch_table + * @param string $phpbb_root_path phpBB root path + * @param string $phpEx PHP file extension */ - public function __construct(config $config, driver_interface $db, dispatcher_interface $phpbb_dispatcher, language $language, user $user, string $phpbb_root_path, string $phpEx) + public function __construct(config $config, driver_interface $db, dispatcher_interface $phpbb_dispatcher, language $language, user $user, string $search_results_table, string $search_wordlist_table, string $search_wordmatch_table, string $phpbb_root_path, string $phpEx) { global $cache; - parent::__construct($cache, $config, $db, $user); + parent::__construct($cache, $config, $db, $user, $search_results_table); $this->phpbb_dispatcher = $phpbb_dispatcher; $this->language = $language; + $this->search_wordlist_table = $search_wordlist_table; + $this->search_wordmatch_table = $search_wordmatch_table; + $this->phpbb_root_path = $phpbb_root_path; $this->php_ext = $phpEx; @@ -336,7 +352,7 @@ class fulltext_native extends base implements search_backend_interface if (count($exact_words)) { $sql = 'SELECT word_id, word_text, word_common - FROM ' . SEARCH_WORDLIST_TABLE . ' + FROM ' . $this->search_wordlist_table . ' WHERE ' . $this->db->sql_in_set('word_text', $exact_words) . ' ORDER BY word_count ASC'; $result = $this->db->sql_query($sql); @@ -608,8 +624,8 @@ class fulltext_native extends base implements search_backend_interface $sql_array = array( 'SELECT' => ($type == 'posts') ? 'DISTINCT p.post_id' : 'DISTINCT p.topic_id', 'FROM' => array( - SEARCH_WORDMATCH_TABLE => array(), - SEARCH_WORDLIST_TABLE => array(), + $this->search_wordmatch_table => array(), + $this->search_wordlist_table => array(), ), 'LEFT_JOIN' => array(array( 'FROM' => array(POSTS_TABLE => 'p'), @@ -661,7 +677,7 @@ class fulltext_native extends base implements search_backend_interface if (is_string($id)) { $sql_array['LEFT_JOIN'][] = array( - 'FROM' => array(SEARCH_WORDLIST_TABLE => 'w' . $w_num), + 'FROM' => array($this->search_wordlist_table => 'w' . $w_num), 'ON' => "w$w_num.word_text LIKE $id" ); $word_ids[] = "w$w_num.word_id"; @@ -681,7 +697,7 @@ class fulltext_native extends base implements search_backend_interface } else if (is_string($subquery)) { - $sql_array['FROM'][SEARCH_WORDLIST_TABLE][] = 'w' . $w_num; + $sql_array['FROM'][$this->search_wordlist_table][] = 'w' . $w_num; $sql_where[] = "w$w_num.word_text LIKE $subquery"; $sql_where[] = "m$m_num.word_id = w$w_num.word_id"; @@ -694,7 +710,7 @@ class fulltext_native extends base implements search_backend_interface $sql_where[] = "m$m_num.word_id = $subquery"; } - $sql_array['FROM'][SEARCH_WORDMATCH_TABLE][] = 'm' . $m_num; + $sql_array['FROM'][$this->search_wordmatch_table][] = 'm' . $m_num; if ($title_match) { @@ -713,7 +729,7 @@ class fulltext_native extends base implements search_backend_interface if (is_string($subquery)) { $sql_array['LEFT_JOIN'][] = array( - 'FROM' => array(SEARCH_WORDLIST_TABLE => 'w' . $w_num), + 'FROM' => array($this->search_wordlist_table => 'w' . $w_num), 'ON' => "w$w_num.word_text LIKE $subquery" ); @@ -727,7 +743,7 @@ class fulltext_native extends base implements search_backend_interface if (count($this->must_not_contain_ids)) { $sql_array['LEFT_JOIN'][] = array( - 'FROM' => array(SEARCH_WORDMATCH_TABLE => 'm' . $m_num), + 'FROM' => array($this->search_wordmatch_table => 'm' . $m_num), 'ON' => $this->db->sql_in_set("m$m_num.word_id", $this->must_not_contain_ids) . (($title_match) ? " AND m$m_num.$title_match" : '') . " AND m$m_num.post_id = m0.post_id" ); @@ -743,7 +759,7 @@ class fulltext_native extends base implements search_backend_interface if (is_string($id)) { $sql_array['LEFT_JOIN'][] = array( - 'FROM' => array(SEARCH_WORDLIST_TABLE => 'w' . $w_num), + 'FROM' => array($this->search_wordlist_table => 'w' . $w_num), 'ON' => "w$w_num.word_text LIKE $id" ); $id = "w$w_num.word_id"; @@ -753,7 +769,7 @@ class fulltext_native extends base implements search_backend_interface } $sql_array['LEFT_JOIN'][] = array( - 'FROM' => array(SEARCH_WORDMATCH_TABLE => 'm' . $m_num), + 'FROM' => array($this->search_wordmatch_table => 'm' . $m_num), 'ON' => "m$m_num.word_id = $id AND m$m_num.post_id = m0.post_id" . (($title_match) ? " AND m$m_num.$title_match" : '') ); $is_null_joins[] = "m$m_num.word_id IS NULL"; @@ -1311,7 +1327,7 @@ class fulltext_native extends base implements search_backend_interface $words['del']['title'] = array(); $sql = 'SELECT w.word_id, w.word_text, m.title_match - FROM ' . SEARCH_WORDLIST_TABLE . ' w, ' . SEARCH_WORDMATCH_TABLE . " m + FROM ' . $this->search_wordlist_table . ' w, ' . $this->search_wordmatch_table . " m WHERE m.post_id = $post_id AND w.word_id = m.word_id"; $result = $this->db->sql_query($sql); @@ -1380,7 +1396,7 @@ class fulltext_native extends base implements search_backend_interface if (count($unique_add_words)) { $sql = 'SELECT word_id, word_text - FROM ' . SEARCH_WORDLIST_TABLE . ' + FROM ' . $this->search_wordlist_table . ' WHERE ' . $this->db->sql_in_set('word_text', $unique_add_words); $result = $this->db->sql_query($sql); @@ -1402,7 +1418,7 @@ class fulltext_native extends base implements search_backend_interface $sql_ary[] = array('word_text' => (string) $word, 'word_count' => 0); } $this->db->sql_return_on_error(true); - $this->db->sql_multi_insert(SEARCH_WORDLIST_TABLE, $sql_ary); + $this->db->sql_multi_insert($this->search_wordlist_table, $sql_ary); $this->db->sql_return_on_error(false); } unset($new_words, $sql_ary); @@ -1425,13 +1441,13 @@ class fulltext_native extends base implements search_backend_interface $sql_in[] = $cur_words[$word_in][$word]; } - $sql = 'DELETE FROM ' . SEARCH_WORDMATCH_TABLE . ' + $sql = 'DELETE FROM ' . $this->search_wordmatch_table . ' WHERE ' . $this->db->sql_in_set('word_id', $sql_in) . ' AND post_id = ' . intval($post_id) . " AND title_match = $title_match"; $this->db->sql_query($sql); - $sql = 'UPDATE ' . SEARCH_WORDLIST_TABLE . ' + $sql = 'UPDATE ' . $this->search_wordlist_table . ' SET word_count = word_count - 1 WHERE ' . $this->db->sql_in_set('word_id', $sql_in) . ' AND word_count > 0'; @@ -1448,13 +1464,13 @@ class fulltext_native extends base implements search_backend_interface if (count($word_ary)) { - $sql = 'INSERT INTO ' . SEARCH_WORDMATCH_TABLE . ' (post_id, word_id, title_match) + $sql = 'INSERT INTO ' . $this->search_wordmatch_table . ' (post_id, word_id, title_match) SELECT ' . (int) $post_id . ', word_id, ' . (int) $title_match . ' - FROM ' . SEARCH_WORDLIST_TABLE . ' + FROM ' . $this->search_wordlist_table . ' WHERE ' . $this->db->sql_in_set('word_text', $word_ary); $this->db->sql_query($sql); - $sql = 'UPDATE ' . SEARCH_WORDLIST_TABLE . ' + $sql = 'UPDATE ' . $this->search_wordlist_table . ' SET word_count = word_count + 1 WHERE ' . $this->db->sql_in_set('word_text', $word_ary); $this->db->sql_query($sql); @@ -1480,7 +1496,7 @@ class fulltext_native extends base implements search_backend_interface if (count($post_ids)) { $sql = 'SELECT w.word_id, w.word_text, m.title_match - FROM ' . SEARCH_WORDMATCH_TABLE . ' m, ' . SEARCH_WORDLIST_TABLE . ' w + FROM ' . $this->search_wordmatch_table . ' m, ' . $this->search_wordlist_table . ' w WHERE ' . $this->db->sql_in_set('m.post_id', $post_ids) . ' AND w.word_id = m.word_id'; $result = $this->db->sql_query($sql); @@ -1502,7 +1518,7 @@ class fulltext_native extends base implements search_backend_interface if (count($title_word_ids)) { - $sql = 'UPDATE ' . SEARCH_WORDLIST_TABLE . ' + $sql = 'UPDATE ' . $this->search_wordlist_table . ' SET word_count = word_count - 1 WHERE ' . $this->db->sql_in_set('word_id', $title_word_ids) . ' AND word_count > 0'; @@ -1511,7 +1527,7 @@ class fulltext_native extends base implements search_backend_interface if (count($message_word_ids)) { - $sql = 'UPDATE ' . SEARCH_WORDLIST_TABLE . ' + $sql = 'UPDATE ' . $this->search_wordlist_table . ' SET word_count = word_count - 1 WHERE ' . $this->db->sql_in_set('word_id', $message_word_ids) . ' AND word_count > 0'; @@ -1521,7 +1537,7 @@ class fulltext_native extends base implements search_backend_interface unset($title_word_ids); unset($message_word_ids); - $sql = 'DELETE FROM ' . SEARCH_WORDMATCH_TABLE . ' + $sql = 'DELETE FROM ' . $this->search_wordmatch_table . ' WHERE ' . $this->db->sql_in_set('post_id', $post_ids); $this->db->sql_query($sql); } @@ -1550,7 +1566,7 @@ class fulltext_native extends base implements search_backend_interface $common_threshold = ((double) $this->config['fulltext_native_common_thres']) / 100.0; // First, get the IDs of common words $sql = 'SELECT word_id, word_text - FROM ' . SEARCH_WORDLIST_TABLE . ' + FROM ' . $this->search_wordlist_table . ' WHERE word_count > ' . floor($this->config['num_posts'] * $common_threshold) . ' OR word_common = 1'; $result = $this->db->sql_query($sql); @@ -1566,7 +1582,7 @@ class fulltext_native extends base implements search_backend_interface if (count($sql_in)) { // Flag the words - $sql = 'UPDATE ' . SEARCH_WORDLIST_TABLE . ' + $sql = 'UPDATE ' . $this->search_wordlist_table . ' SET word_common = 1 WHERE ' . $this->db->sql_in_set('word_id', $sql_in); $this->db->sql_query($sql); @@ -1576,7 +1592,7 @@ class fulltext_native extends base implements search_backend_interface $this->config->set('search_last_gc', time(), false); // Delete the matches - $sql = 'DELETE FROM ' . SEARCH_WORDMATCH_TABLE . ' + $sql = 'DELETE FROM ' . $this->search_wordmatch_table . ' WHERE ' . $this->db->sql_in_set('word_id', $sql_in); $this->db->sql_query($sql); } @@ -1609,15 +1625,15 @@ class fulltext_native extends base implements search_backend_interface switch ($this->db->get_sql_layer()) { case 'sqlite3': - $sql_queries[] = 'DELETE FROM ' . SEARCH_WORDLIST_TABLE; - $sql_queries[] = 'DELETE FROM ' . SEARCH_WORDMATCH_TABLE; - $sql_queries[] = 'DELETE FROM ' . SEARCH_RESULTS_TABLE; + $sql_queries[] = 'DELETE FROM ' . $this->search_wordlist_table; + $sql_queries[] = 'DELETE FROM ' . $this->search_wordmatch_table; + $sql_queries[] = 'DELETE FROM ' . $this->search_results_table; break; default: - $sql_queries[] = 'TRUNCATE TABLE ' . SEARCH_WORDLIST_TABLE; - $sql_queries[] = 'TRUNCATE TABLE ' . SEARCH_WORDMATCH_TABLE; - $sql_queries[] = 'TRUNCATE TABLE ' . SEARCH_RESULTS_TABLE; + $sql_queries[] = 'TRUNCATE TABLE ' . $this->search_wordlist_table; + $sql_queries[] = 'TRUNCATE TABLE ' . $this->search_wordmatch_table; + $sql_queries[] = 'TRUNCATE TABLE ' . $this->search_results_table; break; } @@ -1678,8 +1694,8 @@ class fulltext_native extends base implements search_backend_interface */ protected function get_stats() { - $this->stats['total_words'] = $this->db->get_estimated_row_count(SEARCH_WORDLIST_TABLE); - $this->stats['total_matches'] = $this->db->get_estimated_row_count(SEARCH_WORDMATCH_TABLE); + $this->stats['total_words'] = $this->db->get_estimated_row_count($this->search_wordlist_table); + $this->stats['total_matches'] = $this->db->get_estimated_row_count($this->search_wordmatch_table); } /** diff --git a/phpBB/phpbb/search/backend/fulltext_postgres.php b/phpBB/phpbb/search/backend/fulltext_postgres.php index 077eee4f30..9b9822d2da 100644 --- a/phpBB/phpbb/search/backend/fulltext_postgres.php +++ b/phpBB/phpbb/search/backend/fulltext_postgres.php @@ -19,8 +19,8 @@ use phpbb\event\dispatcher_interface; use phpbb\language\language; use phpbb\search\exception\index_created_exception; use phpbb\search\exception\index_empty_exception; +use phpbb\search\exception\search_exception; use phpbb\user; -use RuntimeException; /** * Fulltext search for PostgreSQL @@ -86,19 +86,20 @@ class fulltext_postgres extends base implements search_backend_interface * Constructor * Creates a new \phpbb\search\backend\fulltext_postgres, which is used as a search backend * - * @param config $config Config object - * @param driver_interface $db Database object + * @param config $config Config object + * @param driver_interface $db Database object * @param dispatcher_interface $phpbb_dispatcher Event dispatcher object - * @param language $language - * @param user $user User object - * @param string $phpbb_root_path Relative path to phpBB root - * @param string $phpEx PHP file extension + * @param language $language + * @param user $user User object + * @param string $search_results_table + * @param string $phpbb_root_path Relative path to phpBB root + * @param string $phpEx PHP file extension */ - public function __construct(config $config, driver_interface $db, dispatcher_interface $phpbb_dispatcher, language $language, user $user, string $phpbb_root_path, string $phpEx) + public function __construct(config $config, driver_interface $db, dispatcher_interface $phpbb_dispatcher, language $language, user $user, string $search_results_table, string $phpbb_root_path, string $phpEx) { global $cache; - parent::__construct($cache, $config, $db, $user); + parent::__construct($cache, $config, $db, $user, $search_results_table); $this->phpbb_dispatcher = $phpbb_dispatcher; $this->language = $language; @@ -878,7 +879,7 @@ class fulltext_postgres extends base implements search_backend_interface // Make sure we can actually use PostgreSQL with fulltext indexes if ($error = $this->init()) { - throw new RuntimeException($error); + throw new search_exception($error); } if (empty($this->stats)) @@ -924,7 +925,7 @@ class fulltext_postgres extends base implements search_backend_interface $this->db->sql_query($sql_query); } - $this->db->sql_query('TRUNCATE TABLE ' . SEARCH_RESULTS_TABLE); + $this->db->sql_query('TRUNCATE TABLE ' . $this->search_results_table); return null; } @@ -942,7 +943,7 @@ class fulltext_postgres extends base implements search_backend_interface // Make sure we can actually use PostgreSQL with fulltext indexes if ($error = $this->init()) { - throw new RuntimeException($error); + throw new search_exception($error); } if (empty($this->stats)) @@ -988,7 +989,7 @@ class fulltext_postgres extends base implements search_backend_interface $this->db->sql_query($sql_query); } - $this->db->sql_query('TRUNCATE TABLE ' . SEARCH_RESULTS_TABLE); + $this->db->sql_query('TRUNCATE TABLE ' . $this->search_results_table); return null; } diff --git a/phpBB/phpbb/search/backend/fulltext_sphinx.php b/phpBB/phpbb/search/backend/fulltext_sphinx.php index 74ff23bbf8..53342e6214 100644 --- a/phpBB/phpbb/search/backend/fulltext_sphinx.php +++ b/phpBB/phpbb/search/backend/fulltext_sphinx.php @@ -632,7 +632,7 @@ class fulltext_sphinx implements search_backend_interface */ public function create_index(int &$post_counter = 0): ?array { - if (!$this->index_created()) + if ($this->index_created()) { throw new index_empty_exception(); } diff --git a/phpBB/phpbb/search/exception/action_in_progress_exception.php b/phpBB/phpbb/search/exception/action_in_progress_exception.php index 183c00f512..fdf9326414 100644 --- a/phpBB/phpbb/search/exception/action_in_progress_exception.php +++ b/phpBB/phpbb/search/exception/action_in_progress_exception.php @@ -13,7 +13,6 @@ namespace phpbb\search\exception; - class action_in_progress_exception extends search_exception { diff --git a/phpBB/phpbb/search/exception/index_created_exception.php b/phpBB/phpbb/search/exception/index_created_exception.php index 9be91bfbb8..e46f995baf 100644 --- a/phpBB/phpbb/search/exception/index_created_exception.php +++ b/phpBB/phpbb/search/exception/index_created_exception.php @@ -13,7 +13,6 @@ namespace phpbb\search\exception; - class index_created_exception extends search_exception { diff --git a/phpBB/phpbb/search/exception/index_empty_exception.php b/phpBB/phpbb/search/exception/index_empty_exception.php index a6a55698de..6045866235 100644 --- a/phpBB/phpbb/search/exception/index_empty_exception.php +++ b/phpBB/phpbb/search/exception/index_empty_exception.php @@ -13,7 +13,6 @@ namespace phpbb\search\exception; - class index_empty_exception extends search_exception { diff --git a/phpBB/phpbb/search/exception/no_action_in_progress_exception.php b/phpBB/phpbb/search/exception/no_action_in_progress_exception.php index 4c04b8c720..15ce250366 100644 --- a/phpBB/phpbb/search/exception/no_action_in_progress_exception.php +++ b/phpBB/phpbb/search/exception/no_action_in_progress_exception.php @@ -13,7 +13,6 @@ namespace phpbb\search\exception; - class no_action_in_progress_exception extends search_exception { diff --git a/phpBB/phpbb/search/exception/search_exception.php b/phpBB/phpbb/search/exception/search_exception.php index 962d30b879..18119b75a4 100644 --- a/phpBB/phpbb/search/exception/search_exception.php +++ b/phpBB/phpbb/search/exception/search_exception.php @@ -17,5 +17,5 @@ use phpbb\exception\runtime_exception; class search_exception extends runtime_exception { - // TODO: Launch this exception from search instead of RuntimeException + } diff --git a/phpBB/phpbb/search/state_helper.php b/phpBB/phpbb/search/state_helper.php index b3e5dd0aea..078096f2d4 100644 --- a/phpBB/phpbb/search/state_helper.php +++ b/phpBB/phpbb/search/state_helper.php @@ -16,6 +16,7 @@ namespace phpbb\search; use phpbb\config\config; use phpbb\search\exception\action_in_progress_exception; use phpbb\search\exception\no_action_in_progress_exception; +use phpbb\search\exception\search_exception; class state_helper { @@ -32,8 +33,8 @@ class state_helper /** * Constructor. * - * @param \phpbb\config\config $config - * @param \phpbb\search\search_backend_factory $search_backend_factory + * @param config $config + * @param search_backend_factory $search_backend_factory */ public function __construct(config $config, search_backend_factory $search_backend_factory) { @@ -108,7 +109,7 @@ class state_helper // Make sure the action is correct (just in case) if (!in_array($action, ['create', 'delete'])) { - throw new \RuntimeException('Invalid action'); + throw new search_exception('Invalid action'); } $state = [ From 1cef5f83a00077ec26336ec904bcba3f8ca1b0c8 Mon Sep 17 00:00:00 2001 From: rubencm Date: Fri, 9 Apr 2021 19:45:15 +0200 Subject: [PATCH 09/20] [ticket/12683] Fix test PHPBB3-12683 --- .../install/module/install_data/task/create_search_index.php | 3 +++ phpBB/phpbb/search/state_helper.php | 2 +- tests/search/mysql_test.php | 2 +- tests/search/native_test.php | 2 +- tests/search/postgres_test.php | 2 +- 5 files changed, 7 insertions(+), 4 deletions(-) diff --git a/phpBB/phpbb/install/module/install_data/task/create_search_index.php b/phpBB/phpbb/install/module/install_data/task/create_search_index.php index c8f77c105f..9f80e5e784 100644 --- a/phpBB/phpbb/install/module/install_data/task/create_search_index.php +++ b/phpBB/phpbb/install/module/install_data/task/create_search_index.php @@ -133,6 +133,9 @@ class create_search_index extends database_task $this->phpbb_dispatcher, $container->get('language'), $this->user, + SEARCH_RESULTS_TABLE, + SEARCH_WORDLIST_TABLE, + SEARCH_WORDMATCH_TABLE, $this->phpbb_root_path, $this->php_ext ); diff --git a/phpBB/phpbb/search/state_helper.php b/phpBB/phpbb/search/state_helper.php index 078096f2d4..b50860d145 100644 --- a/phpBB/phpbb/search/state_helper.php +++ b/phpBB/phpbb/search/state_helper.php @@ -124,7 +124,7 @@ class state_helper /** * @param int $counter * - * @throws \phpbb\search\exception\no_action_in_progress_exception If there is no action in progress + * @throws no_action_in_progress_exception If there is no action in progress */ public function update_counter(int $counter): void { diff --git a/tests/search/mysql_test.php b/tests/search/mysql_test.php index 1e822b92af..fe7326bb3e 100644 --- a/tests/search/mysql_test.php +++ b/tests/search/mysql_test.php @@ -40,6 +40,6 @@ class phpbb_search_mysql_test extends phpbb_search_common_test_case $this->db = $this->new_dbal(); $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); $class = self::get_search_wrapper('\phpbb\search\backend\fulltext_mysql'); - $this->search = new $class($config, $this->db, $phpbb_dispatcher, $language, $user, $phpbb_root_path, $phpEx); + $this->search = new $class($config, $this->db, $phpbb_dispatcher, $language, $user, SEARCH_RESULTS_TABLE, $phpbb_root_path, $phpEx); } } diff --git a/tests/search/native_test.php b/tests/search/native_test.php index 6d8a03f4aa..90702655d0 100644 --- a/tests/search/native_test.php +++ b/tests/search/native_test.php @@ -38,7 +38,7 @@ class phpbb_search_native_test extends phpbb_search_test_case $class = self::get_search_wrapper('\phpbb\search\backend\fulltext_native'); $config['fulltext_native_min_chars'] = 2; $config['fulltext_native_max_chars'] = 14; - $this->search = new $class($config, $this->db, $phpbb_dispatcher, $language, $user, $phpbb_root_path, $phpEx); + $this->search = new $class($config, $this->db, $phpbb_dispatcher, $language, $user, SEARCH_RESULTS_TABLE, SEARCH_WORDLIST_TABLE, SEARCH_WORDMATCH_TABLE, $phpbb_root_path, $phpEx); } public function keywords() diff --git a/tests/search/postgres_test.php b/tests/search/postgres_test.php index 545ffafd50..18c5a46f81 100644 --- a/tests/search/postgres_test.php +++ b/tests/search/postgres_test.php @@ -40,6 +40,6 @@ class phpbb_search_postgres_test extends phpbb_search_common_test_case $this->db = $this->new_dbal(); $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); $class = self::get_search_wrapper('\phpbb\search\backend\fulltext_postgres'); - $this->search = new $class($config, $this->db, $phpbb_dispatcher, $language, $user, $phpbb_root_path, $phpEx); + $this->search = new $class($config, $this->db, $phpbb_dispatcher, $language, $user, SEARCH_RESULTS_TABLE, $phpbb_root_path, $phpEx); } } From 9a6d42770ec9491a0fe1cebf037fc6a45c3f5ebe Mon Sep 17 00:00:00 2001 From: rubencm Date: Sat, 10 Apr 2021 17:53:06 +0200 Subject: [PATCH 10/20] [ticket/12683] Remove check to avoid index twice (ticket 16755) PHPBB3-12683 --- phpBB/includes/acp/acp_search.php | 29 +++++++++---------- phpBB/language/en/cli.php | 2 -- .../console/command/searchindex/create.php | 7 ----- .../console/command/searchindex/delete.php | 7 ----- phpBB/phpbb/search/backend/base.php | 12 -------- phpBB/phpbb/search/backend/fulltext_mysql.php | 12 -------- .../phpbb/search/backend/fulltext_native.php | 6 ---- .../search/backend/fulltext_postgres.php | 12 -------- .../phpbb/search/backend/fulltext_sphinx.php | 11 ------- .../exception/index_created_exception.php | 19 ------------ .../exception/index_empty_exception.php | 19 ------------ 11 files changed, 13 insertions(+), 123 deletions(-) delete mode 100644 phpBB/phpbb/search/exception/index_created_exception.php delete mode 100644 phpBB/phpbb/search/exception/index_empty_exception.php diff --git a/phpBB/includes/acp/acp_search.php b/phpBB/includes/acp/acp_search.php index cece03476b..a2fd6dd7b1 100644 --- a/phpBB/includes/acp/acp_search.php +++ b/phpBB/includes/acp/acp_search.php @@ -323,24 +323,21 @@ class acp_search foreach ($this->search_backend_collection as $search) { - if ($search->is_available()) - { - $this->template->assign_block_vars('backends', [ - 'NAME' => $search->get_name(), - 'TYPE' => $search->get_type(), + $this->template->assign_block_vars('backends', [ + 'NAME' => $search->get_name(), + 'TYPE' => $search->get_type(), - 'S_ACTIVE' => $search->get_type() === $this->config['search_type'], - 'S_HIDDEN_FIELDS' => build_hidden_fields(['search_type' => $search->get_type()]), - 'S_INDEXED' => $search->index_created(), - 'S_STATS' => $search->index_stats(), - ]); - - $this->template->assign_vars([ - 'U_ACTION' => $this->u_action . '&hash=' . generate_link_hash('acp_search'), - 'UA_PROGRESS_BAR' => addslashes($this->u_action . '&action=progress_bar'), - ]); - } + 'S_ACTIVE' => $search->get_type() === $this->config['search_type'], + 'S_HIDDEN_FIELDS' => build_hidden_fields(['search_type' => $search->get_type()]), + 'S_INDEXED' => $search->index_created(), + 'S_STATS' => $search->index_stats(), + ]); } + + $this->template->assign_vars([ + 'U_ACTION' => $this->u_action . '&hash=' . generate_link_hash('acp_search'), + 'UA_PROGRESS_BAR' => addslashes($this->u_action . '&action=progress_bar'), + ]); } /** diff --git a/phpBB/language/en/cli.php b/phpBB/language/en/cli.php index 82c1c3d62a..ac1f642b26 100644 --- a/phpBB/language/en/cli.php +++ b/phpBB/language/en/cli.php @@ -152,8 +152,6 @@ $lang = array_merge($lang, array( 'CLI_SEARCHINDEX_CREATE_FAILURE' => 'Error creating search index', 'CLI_SEARCHINDEX_DELETE_SUCCESS' => 'Search index deleted successfully', 'CLI_SEARCHINDEX_DELETE_FAILURE' => 'Error deleting search index', - 'CLI_SEARCHINDEX_ALREADY_CREATED' => 'Search index is already created, try removing it first', - 'CLI_SEARCHINDEX_NO_CREATED' => 'Search index is already empty, try creating it first', 'CLI_SEARCHINDEX_ACTION_IN_PROGRESS' => 'There is an action currently in progress. CLI doesn\'t support incomplete index/delete actions, please solve it from the ACP', 'CLI_SEARCHINDEX_ACTIVE_NOT_INDEXED' => 'Active search backend isn\'t indexed', diff --git a/phpBB/phpbb/console/command/searchindex/create.php b/phpBB/phpbb/console/command/searchindex/create.php index 0bd291b99e..f10184eb4f 100644 --- a/phpBB/phpbb/console/command/searchindex/create.php +++ b/phpBB/phpbb/console/command/searchindex/create.php @@ -18,7 +18,6 @@ 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\search\state_helper; @@ -146,12 +145,6 @@ class create extends command $io->newLine(2); } - catch (index_created_exception $e) - { - $this->state_helper->clear_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)); diff --git a/phpBB/phpbb/console/command/searchindex/delete.php b/phpBB/phpbb/console/command/searchindex/delete.php index 8ffaa808f3..6981ef9807 100644 --- a/phpBB/phpbb/console/command/searchindex/delete.php +++ b/phpBB/phpbb/console/command/searchindex/delete.php @@ -18,7 +18,6 @@ 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\search\state_helper; @@ -146,12 +145,6 @@ class delete extends command $io->newLine(2); } - catch (index_empty_exception $e) - { - $this->state_helper->clear_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)); diff --git a/phpBB/phpbb/search/backend/base.php b/phpBB/phpbb/search/backend/base.php index e554e0cc71..2ddd893400 100644 --- a/phpBB/phpbb/search/backend/base.php +++ b/phpBB/phpbb/search/backend/base.php @@ -16,8 +16,6 @@ namespace phpbb\search\backend; use phpbb\cache\service; use phpbb\config\config; use phpbb\db\driver\driver_interface; -use phpbb\search\exception\index_created_exception; -use phpbb\search\exception\index_empty_exception; use phpbb\user; /** @@ -332,11 +330,6 @@ abstract class base implements search_backend_interface */ public function create_index(int &$post_counter = 0): ?array { - if ($this->index_created()) - { - throw new index_created_exception(); - } - $max_post_id = $this->get_max_post_id(); $forums_indexing_enabled = $this->forum_ids_with_indexing_enabled(); @@ -399,11 +392,6 @@ abstract class base implements search_backend_interface */ public function delete_index(int &$post_counter = null): ?array { - if (!$this->index_created()) - { - throw new index_empty_exception(); - } - $max_post_id = $this->get_max_post_id(); $starttime = microtime(true); diff --git a/phpBB/phpbb/search/backend/fulltext_mysql.php b/phpBB/phpbb/search/backend/fulltext_mysql.php index e9fce05fc1..6b30408ce8 100644 --- a/phpBB/phpbb/search/backend/fulltext_mysql.php +++ b/phpBB/phpbb/search/backend/fulltext_mysql.php @@ -17,8 +17,6 @@ use phpbb\config\config; use phpbb\db\driver\driver_interface; use phpbb\event\dispatcher_interface; use phpbb\language\language; -use phpbb\search\exception\index_created_exception; -use phpbb\search\exception\index_empty_exception; use phpbb\search\exception\search_exception; use phpbb\user; @@ -916,11 +914,6 @@ class fulltext_mysql extends base implements search_backend_interface */ public function create_index(int &$post_counter = 0): ?array { - if ($this->index_created()) - { - throw new index_created_exception(); - } - // Make sure we can actually use MySQL with fulltext indexes if ($error = $this->init()) { @@ -993,11 +986,6 @@ class fulltext_mysql extends base implements search_backend_interface */ public function delete_index(int &$post_counter = null): ?array { - if (!$this->index_created()) - { - throw new index_empty_exception(); - } - // Make sure we can actually use MySQL with fulltext indexes if ($error = $this->init()) { diff --git a/phpBB/phpbb/search/backend/fulltext_native.php b/phpBB/phpbb/search/backend/fulltext_native.php index dac6ebf803..57e5ba99d6 100644 --- a/phpBB/phpbb/search/backend/fulltext_native.php +++ b/phpBB/phpbb/search/backend/fulltext_native.php @@ -17,7 +17,6 @@ use phpbb\config\config; use phpbb\db\driver\driver_interface; use phpbb\event\dispatcher_interface; use phpbb\language\language; -use phpbb\search\exception\index_empty_exception; use phpbb\user; /** @@ -1615,11 +1614,6 @@ class fulltext_native extends base implements search_backend_interface */ public function delete_index(int &$post_counter = null): ?array { - if (!$this->index_created()) - { - throw new index_empty_exception(); - } - $sql_queries = []; switch ($this->db->get_sql_layer()) diff --git a/phpBB/phpbb/search/backend/fulltext_postgres.php b/phpBB/phpbb/search/backend/fulltext_postgres.php index 9b9822d2da..fbd10dd081 100644 --- a/phpBB/phpbb/search/backend/fulltext_postgres.php +++ b/phpBB/phpbb/search/backend/fulltext_postgres.php @@ -17,8 +17,6 @@ use phpbb\config\config; use phpbb\db\driver\driver_interface; use phpbb\event\dispatcher_interface; use phpbb\language\language; -use phpbb\search\exception\index_created_exception; -use phpbb\search\exception\index_empty_exception; use phpbb\search\exception\search_exception; use phpbb\user; @@ -871,11 +869,6 @@ class fulltext_postgres extends base implements search_backend_interface */ public function create_index(int &$post_counter = 0): ?array { - if ($this->index_created()) - { - throw new index_created_exception(); - } - // Make sure we can actually use PostgreSQL with fulltext indexes if ($error = $this->init()) { @@ -935,11 +928,6 @@ class fulltext_postgres extends base implements search_backend_interface */ public function delete_index(int &$post_counter = null): ?array { - if (!$this->index_created()) - { - throw new index_empty_exception(); - } - // Make sure we can actually use PostgreSQL with fulltext indexes if ($error = $this->init()) { diff --git a/phpBB/phpbb/search/backend/fulltext_sphinx.php b/phpBB/phpbb/search/backend/fulltext_sphinx.php index 53342e6214..8023bc161a 100644 --- a/phpBB/phpbb/search/backend/fulltext_sphinx.php +++ b/phpBB/phpbb/search/backend/fulltext_sphinx.php @@ -20,7 +20,6 @@ use phpbb\db\tools\tools_interface; use phpbb\event\dispatcher_interface; use phpbb\language\language; use phpbb\log\log; -use phpbb\search\exception\index_empty_exception; use phpbb\user; /** @@ -632,11 +631,6 @@ class fulltext_sphinx implements search_backend_interface */ public function create_index(int &$post_counter = 0): ?array { - if ($this->index_created()) - { - throw new index_empty_exception(); - } - $table_data = array( 'COLUMNS' => array( 'counter_id' => array('UINT', 0), @@ -664,11 +658,6 @@ class fulltext_sphinx implements search_backend_interface */ public function delete_index(int &$post_counter = null): ?array { - if (!$this->index_created()) - { - throw new index_empty_exception(); - } - $this->db_tools->sql_table_drop(SPHINX_TABLE); return null; diff --git a/phpBB/phpbb/search/exception/index_created_exception.php b/phpBB/phpbb/search/exception/index_created_exception.php deleted file mode 100644 index e46f995baf..0000000000 --- a/phpBB/phpbb/search/exception/index_created_exception.php +++ /dev/null @@ -1,19 +0,0 @@ - - * @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 index_created_exception extends search_exception -{ - -} diff --git a/phpBB/phpbb/search/exception/index_empty_exception.php b/phpBB/phpbb/search/exception/index_empty_exception.php deleted file mode 100644 index 6045866235..0000000000 --- a/phpBB/phpbb/search/exception/index_empty_exception.php +++ /dev/null @@ -1,19 +0,0 @@ - - * @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 index_empty_exception extends search_exception -{ - -} From d55f1e04ebb1f101e8f5f4429a7e7db5782394c9 Mon Sep 17 00:00:00 2001 From: rubencm Date: Wed, 14 Apr 2021 22:17:34 +0200 Subject: [PATCH 11/20] [ticket/12683] phpdoc and spaces PHPBB3-12683 --- phpBB/config/default/container/services.yml | 2 +- ...services_post.yml => services_posting.yml} | 0 phpBB/includes/acp/acp_search.php | 16 +++++++------- phpBB/language/en/cli.php | 4 ++-- .../console/command/searchindex/create.php | 18 +++++++-------- .../console/command/searchindex/delete.php | 18 +++++++-------- phpBB/phpbb/search/backend/base.php | 10 ++++----- phpBB/phpbb/search/backend/fulltext_mysql.php | 16 +++++++------- .../phpbb/search/backend/fulltext_native.php | 20 ++++++++--------- .../search/backend/fulltext_postgres.php | 16 +++++++------- .../action_in_progress_exception.php | 1 - .../no_action_in_progress_exception.php | 1 - .../no_search_backend_found_exception.php | 1 - .../search/exception/search_exception.php | 1 - phpBB/phpbb/search/state_helper.php | 22 ++++++++++++++----- 15 files changed, 76 insertions(+), 70 deletions(-) rename phpBB/config/default/container/{services_post.yml => services_posting.yml} (100%) diff --git a/phpBB/config/default/container/services.yml b/phpBB/config/default/container/services.yml index 547053631b..aa997b8360 100644 --- a/phpBB/config/default/container/services.yml +++ b/phpBB/config/default/container/services.yml @@ -23,7 +23,7 @@ imports: - { resource: services_notification.yml } - { resource: services_password.yml } - { resource: services_php.yml } - - { resource: services_post.yml } + - { resource: services_posting.yml } - { resource: services_profilefield.yml } - { resource: services_report.yml } - { resource: services_routing.yml } diff --git a/phpBB/config/default/container/services_post.yml b/phpBB/config/default/container/services_posting.yml similarity index 100% rename from phpBB/config/default/container/services_post.yml rename to phpBB/config/default/container/services_posting.yml diff --git a/phpBB/includes/acp/acp_search.php b/phpBB/includes/acp/acp_search.php index a2fd6dd7b1..d5b2f3335b 100644 --- a/phpBB/includes/acp/acp_search.php +++ b/phpBB/includes/acp/acp_search.php @@ -324,19 +324,19 @@ class acp_search foreach ($this->search_backend_collection as $search) { $this->template->assign_block_vars('backends', [ - 'NAME' => $search->get_name(), - 'TYPE' => $search->get_type(), + 'NAME' => $search->get_name(), + 'TYPE' => $search->get_type(), - 'S_ACTIVE' => $search->get_type() === $this->config['search_type'], - 'S_HIDDEN_FIELDS' => build_hidden_fields(['search_type' => $search->get_type()]), - 'S_INDEXED' => $search->index_created(), - 'S_STATS' => $search->index_stats(), + 'S_ACTIVE' => $search->get_type() === $this->config['search_type'], + 'S_HIDDEN_FIELDS' => build_hidden_fields(['search_type' => $search->get_type()]), + 'S_INDEXED' => $search->index_created(), + 'S_STATS' => $search->index_stats(), ]); } $this->template->assign_vars([ - 'U_ACTION' => $this->u_action . '&hash=' . generate_link_hash('acp_search'), - 'UA_PROGRESS_BAR' => addslashes($this->u_action . '&action=progress_bar'), + 'U_ACTION' => $this->u_action . '&hash=' . generate_link_hash('acp_search'), + 'UA_PROGRESS_BAR' => addslashes($this->u_action . '&action=progress_bar'), ]); } diff --git a/phpBB/language/en/cli.php b/phpBB/language/en/cli.php index ac1f642b26..35f6f0b1df 100644 --- a/phpBB/language/en/cli.php +++ b/phpBB/language/en/cli.php @@ -152,8 +152,8 @@ $lang = array_merge($lang, array( 'CLI_SEARCHINDEX_CREATE_FAILURE' => 'Error creating search index', 'CLI_SEARCHINDEX_DELETE_SUCCESS' => 'Search index deleted successfully', 'CLI_SEARCHINDEX_DELETE_FAILURE' => 'Error deleting search index', - 'CLI_SEARCHINDEX_ACTION_IN_PROGRESS' => 'There is an action currently in progress. CLI doesn\'t support incomplete index/delete actions, please solve it from the ACP', - 'CLI_SEARCHINDEX_ACTIVE_NOT_INDEXED' => 'Active search backend isn\'t indexed', + 'CLI_SEARCHINDEX_ACTION_IN_PROGRESS' => 'There is an action currently in progress. CLI doesn’t support incomplete index/delete actions, please solve it from the ACP', + 'CLI_SEARCHINDEX_ACTIVE_NOT_INDEXED' => 'Active search backend isn’t indexed', // In all the case %1$s is the logical name of the file and %2$s the real name on the filesystem // eg: big_image.png (2_a51529ae7932008cf8454a95af84cacd) generated. diff --git a/phpBB/phpbb/console/command/searchindex/create.php b/phpBB/phpbb/console/command/searchindex/create.php index f10184eb4f..9615bb4621 100644 --- a/phpBB/phpbb/console/command/searchindex/create.php +++ b/phpBB/phpbb/console/command/searchindex/create.php @@ -50,13 +50,13 @@ class create extends command /** * Construct method * - * @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 + * @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, state_helper $state_helper, user $user) { @@ -93,8 +93,8 @@ class create extends command * * Create search index * - * @param InputInterface $input The input stream used to get the options - * @param OutputInterface $output The output stream, used to print messages + * @param InputInterface $input The input stream used to get the options + * @param OutputInterface $output The output stream, used to print messages * * @return int 0 if all is well, 1 if any errors occurred */ diff --git a/phpBB/phpbb/console/command/searchindex/delete.php b/phpBB/phpbb/console/command/searchindex/delete.php index 6981ef9807..56506c44b4 100644 --- a/phpBB/phpbb/console/command/searchindex/delete.php +++ b/phpBB/phpbb/console/command/searchindex/delete.php @@ -50,13 +50,13 @@ class delete extends command /** * Construct method * - * @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 + * @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, state_helper $state_helper, user $user) { @@ -93,8 +93,8 @@ class delete extends command * * Delete search index * - * @param InputInterface $input The input stream used to get the options - * @param OutputInterface $output The output stream, used to print messages + * @param InputInterface $input The input stream used to get the options + * @param OutputInterface $output The output stream, used to print messages * * @return int 0 if all is well, 1 if any errors occurred */ diff --git a/phpBB/phpbb/search/backend/base.php b/phpBB/phpbb/search/backend/base.php index 2ddd893400..76be5d8cfe 100644 --- a/phpBB/phpbb/search/backend/base.php +++ b/phpBB/phpbb/search/backend/base.php @@ -59,11 +59,11 @@ abstract class base implements search_backend_interface /** * Constructor. * - * @param service $cache - * @param config $config - * @param driver_interface $db - * @param user $user - * @param string $search_results_table + * @param service $cache + * @param config $config + * @param driver_interface $db + * @param user $user + * @param string $search_results_table */ public function __construct(service $cache, config $config, driver_interface $db, user $user, string $search_results_table) { diff --git a/phpBB/phpbb/search/backend/fulltext_mysql.php b/phpBB/phpbb/search/backend/fulltext_mysql.php index 6b30408ce8..7635b503fa 100644 --- a/phpBB/phpbb/search/backend/fulltext_mysql.php +++ b/phpBB/phpbb/search/backend/fulltext_mysql.php @@ -72,14 +72,14 @@ class fulltext_mysql extends base implements search_backend_interface * Constructor * Creates a new \phpbb\search\backend\fulltext_mysql, which is used as a search backend * - * @param config $config Config object - * @param driver_interface $db Database object - * @param dispatcher_interface $phpbb_dispatcher Event dispatcher object - * @param language $language - * @param user $user User object - * @param string $search_results_table - * @param string $phpbb_root_path Relative path to phpBB root - * @param string $phpEx PHP file extension + * @param config $config Config object + * @param driver_interface $db Database object + * @param dispatcher_interface $phpbb_dispatcher Event dispatcher object + * @param language $language + * @param user $user User object + * @param string $search_results_table + * @param string $phpbb_root_path Relative path to phpBB root + * @param string $phpEx PHP file extension */ public function __construct(config $config, driver_interface $db, dispatcher_interface $phpbb_dispatcher, language $language, user $user, string $search_results_table, string $phpbb_root_path, string $phpEx) { diff --git a/phpBB/phpbb/search/backend/fulltext_native.php b/phpBB/phpbb/search/backend/fulltext_native.php index 57e5ba99d6..6c97ba7a52 100644 --- a/phpBB/phpbb/search/backend/fulltext_native.php +++ b/phpBB/phpbb/search/backend/fulltext_native.php @@ -111,16 +111,16 @@ class fulltext_native extends base implements search_backend_interface /** * Initialises the fulltext_native search backend with min/max word length * - * @param config $config Config object - * @param driver_interface $db Database object - * @param dispatcher_interface $phpbb_dispatcher Event dispatcher object - * @param language $language - * @param user $user User object - * @param string $search_results_table - * @param string $search_wordlist_table - * @param string $search_wordmatch_table - * @param string $phpbb_root_path phpBB root path - * @param string $phpEx PHP file extension + * @param config $config Config object + * @param driver_interface $db Database object + * @param dispatcher_interface $phpbb_dispatcher Event dispatcher object + * @param language $language + * @param user $user User object + * @param string $search_results_table + * @param string $search_wordlist_table + * @param string $search_wordmatch_table + * @param string $phpbb_root_path phpBB root path + * @param string $phpEx PHP file extension */ public function __construct(config $config, driver_interface $db, dispatcher_interface $phpbb_dispatcher, language $language, user $user, string $search_results_table, string $search_wordlist_table, string $search_wordmatch_table, string $phpbb_root_path, string $phpEx) { diff --git a/phpBB/phpbb/search/backend/fulltext_postgres.php b/phpBB/phpbb/search/backend/fulltext_postgres.php index fbd10dd081..efd6ecc148 100644 --- a/phpBB/phpbb/search/backend/fulltext_postgres.php +++ b/phpBB/phpbb/search/backend/fulltext_postgres.php @@ -84,14 +84,14 @@ class fulltext_postgres extends base implements search_backend_interface * Constructor * Creates a new \phpbb\search\backend\fulltext_postgres, which is used as a search backend * - * @param config $config Config object - * @param driver_interface $db Database object - * @param dispatcher_interface $phpbb_dispatcher Event dispatcher object - * @param language $language - * @param user $user User object - * @param string $search_results_table - * @param string $phpbb_root_path Relative path to phpBB root - * @param string $phpEx PHP file extension + * @param config $config Config object + * @param driver_interface $db Database object + * @param dispatcher_interface $phpbb_dispatcher Event dispatcher object + * @param language $language + * @param user $user User object + * @param string $search_results_table + * @param string $phpbb_root_path Relative path to phpBB root + * @param string $phpEx PHP file extension */ public function __construct(config $config, driver_interface $db, dispatcher_interface $phpbb_dispatcher, language $language, user $user, string $search_results_table, string $phpbb_root_path, string $phpEx) { diff --git a/phpBB/phpbb/search/exception/action_in_progress_exception.php b/phpBB/phpbb/search/exception/action_in_progress_exception.php index fdf9326414..9187f2bff6 100644 --- a/phpBB/phpbb/search/exception/action_in_progress_exception.php +++ b/phpBB/phpbb/search/exception/action_in_progress_exception.php @@ -15,5 +15,4 @@ namespace phpbb\search\exception; class action_in_progress_exception extends search_exception { - } diff --git a/phpBB/phpbb/search/exception/no_action_in_progress_exception.php b/phpBB/phpbb/search/exception/no_action_in_progress_exception.php index 15ce250366..2f8a9e28c5 100644 --- a/phpBB/phpbb/search/exception/no_action_in_progress_exception.php +++ b/phpBB/phpbb/search/exception/no_action_in_progress_exception.php @@ -15,5 +15,4 @@ namespace phpbb\search\exception; class no_action_in_progress_exception extends search_exception { - } diff --git a/phpBB/phpbb/search/exception/no_search_backend_found_exception.php b/phpBB/phpbb/search/exception/no_search_backend_found_exception.php index d5c2e43ad4..752db7ce17 100644 --- a/phpBB/phpbb/search/exception/no_search_backend_found_exception.php +++ b/phpBB/phpbb/search/exception/no_search_backend_found_exception.php @@ -15,5 +15,4 @@ namespace phpbb\search\exception; class no_search_backend_found_exception extends search_exception { - } diff --git a/phpBB/phpbb/search/exception/search_exception.php b/phpBB/phpbb/search/exception/search_exception.php index 18119b75a4..c1b904d5f4 100644 --- a/phpBB/phpbb/search/exception/search_exception.php +++ b/phpBB/phpbb/search/exception/search_exception.php @@ -17,5 +17,4 @@ use phpbb\exception\runtime_exception; class search_exception extends runtime_exception { - } diff --git a/phpBB/phpbb/search/state_helper.php b/phpBB/phpbb/search/state_helper.php index b50860d145..ba546f6173 100644 --- a/phpBB/phpbb/search/state_helper.php +++ b/phpBB/phpbb/search/state_helper.php @@ -16,6 +16,7 @@ namespace phpbb\search; use phpbb\config\config; use phpbb\search\exception\action_in_progress_exception; use phpbb\search\exception\no_action_in_progress_exception; +use phpbb\search\exception\no_search_backend_found_exception; use phpbb\search\exception\search_exception; class state_helper @@ -33,8 +34,8 @@ class state_helper /** * Constructor. * - * @param config $config - * @param search_backend_factory $search_backend_factory + * @param config $config + * @param search_backend_factory $search_backend_factory */ public function __construct(config $config, search_backend_factory $search_backend_factory) { @@ -53,7 +54,7 @@ class state_helper } /** - * @return string + * @return string The class name of the search backend * * @throws no_action_in_progress_exception If there is no action in progress */ @@ -65,7 +66,7 @@ class state_helper } /** - * @return string + * @return string The action that is being executed, can be 'create' or 'delete' * * @throws no_action_in_progress_exception If there is no action in progress */ @@ -77,7 +78,7 @@ class state_helper } /** - * @return int + * @return int The post counter * * @throws no_action_in_progress_exception If there is no action in progress */ @@ -89,11 +90,14 @@ class state_helper } /** + * Start a indexing or delete process. + * * @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 + * @throws search_exception If action isn't valid */ public function init(string $search_type, string $action): void { @@ -103,7 +107,7 @@ class state_helper throw new action_in_progress_exception(); } - // Make sure the search type exist (if not, the next line launch an exception) + // Make sure the search type exists (if not, throw an exception) $this->search_backend_factory->get($search_type); // Make sure the action is correct (just in case) @@ -122,6 +126,8 @@ class state_helper } /** + * Set the post counter + * * @param int $counter * * @throws no_action_in_progress_exception If there is no action in progress @@ -144,6 +150,8 @@ class state_helper } /** + * Load the state from the database + * * @return array * * @throws no_action_in_progress_exception If there is no action in progress @@ -160,6 +168,8 @@ class state_helper } /** + * Save the specified state in the database + * * @param array $state */ private function save_state(array $state = []): void From ac227a9ea6e0d699ee3d28e34ba5fb682da53a74 Mon Sep 17 00:00:00 2001 From: Ruben Calvo Date: Mon, 22 Nov 2021 21:10:16 +0100 Subject: [PATCH 12/20] [ticket/12683] Add basic tests PHPBB3-12683 --- .../default/container/services_posting.yml | 2 +- .../console/command/searchindex/create.php | 2 +- .../console/command/searchindex/delete.php | 2 +- phpBB/phpbb/{post => posting}/post_helper.php | 2 +- tests/console/searchindex/base.php | 74 ++++++++++++ tests/console/searchindex/create_test.php | 70 ++++++++++++ tests/console/searchindex/delete_test.php | 70 ++++++++++++ tests/console/searchindex/list_all_test.php | 38 +++++++ .../searchindex/mock/search_backend_mock.php | 105 ++++++++++++++++++ 9 files changed, 361 insertions(+), 4 deletions(-) rename phpBB/phpbb/{post => posting}/post_helper.php (96%) create mode 100644 tests/console/searchindex/base.php create mode 100644 tests/console/searchindex/create_test.php create mode 100644 tests/console/searchindex/delete_test.php create mode 100644 tests/console/searchindex/list_all_test.php create mode 100644 tests/console/searchindex/mock/search_backend_mock.php diff --git a/phpBB/config/default/container/services_posting.yml b/phpBB/config/default/container/services_posting.yml index f6ad5ba80f..aa1a3e1959 100644 --- a/phpBB/config/default/container/services_posting.yml +++ b/phpBB/config/default/container/services_posting.yml @@ -1,5 +1,5 @@ services: post.helper: - class: phpbb\post\post_helper + class: phpbb\posting\post_helper arguments: - '@dbal.conn' diff --git a/phpBB/phpbb/console/command/searchindex/create.php b/phpBB/phpbb/console/command/searchindex/create.php index 9615bb4621..6652e38159 100644 --- a/phpBB/phpbb/console/command/searchindex/create.php +++ b/phpBB/phpbb/console/command/searchindex/create.php @@ -17,7 +17,7 @@ use phpbb\config\config; use phpbb\console\command\command; use phpbb\language\language; use phpbb\log\log; -use phpbb\post\post_helper; +use phpbb\posting\post_helper; use phpbb\search\exception\no_search_backend_found_exception; use phpbb\search\search_backend_factory; use phpbb\search\state_helper; diff --git a/phpBB/phpbb/console/command/searchindex/delete.php b/phpBB/phpbb/console/command/searchindex/delete.php index 56506c44b4..618bdce03d 100644 --- a/phpBB/phpbb/console/command/searchindex/delete.php +++ b/phpBB/phpbb/console/command/searchindex/delete.php @@ -17,7 +17,7 @@ use phpbb\config\config; use phpbb\console\command\command; use phpbb\language\language; use phpbb\log\log; -use phpbb\post\post_helper; +use phpbb\posting\post_helper; use phpbb\search\exception\no_search_backend_found_exception; use phpbb\search\search_backend_factory; use phpbb\search\state_helper; diff --git a/phpBB/phpbb/post/post_helper.php b/phpBB/phpbb/posting/post_helper.php similarity index 96% rename from phpBB/phpbb/post/post_helper.php rename to phpBB/phpbb/posting/post_helper.php index a70d38fb1c..2fbba9688b 100644 --- a/phpBB/phpbb/post/post_helper.php +++ b/phpBB/phpbb/posting/post_helper.php @@ -11,7 +11,7 @@ * */ -namespace phpbb\post; +namespace phpbb\posting; use phpbb\db\driver\driver_interface; diff --git a/tests/console/searchindex/base.php b/tests/console/searchindex/base.php new file mode 100644 index 0000000000..15372a5582 --- /dev/null +++ b/tests/console/searchindex/base.php @@ -0,0 +1,74 @@ +config = new \phpbb\config\config([ + 'search_indexing_state' => [], + 'search_type' => 'search_backend_mock' + ]); + + $lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx); + $this->language = new \phpbb\language\language($lang_loader); + + $this->log = $this->createMock('\phpbb\log\log'); + + $this->post_helper = $this->createMock('\phpbb\posting\post_helper'); + $this->post_helper + ->method('get_max_post_id') + ->willReturn(1000); + + $this->user = $this->createMock('\phpbb\user'); + + $phpbb_container = new phpbb_mock_container_builder(); + $search_backend_mock = new search_backend_mock(); + $this->search_backend_collection = new \phpbb\di\service_collection($phpbb_container); + $this->search_backend_collection->add('search_backend_mock'); + $this->search_backend_collection->add_service_class('search_backend_mock', 'search_backend_mock'); + $phpbb_container->set('search_backend_mock', $search_backend_mock); + + $this->search_backend_factory = new search_backend_factory($this->config, $this->search_backend_collection); + + $this->state_helper = new state_helper($this->config, $this->search_backend_factory); + + parent::setUp(); + } +} diff --git a/tests/console/searchindex/create_test.php b/tests/console/searchindex/create_test.php new file mode 100644 index 0000000000..43178e8408 --- /dev/null +++ b/tests/console/searchindex/create_test.php @@ -0,0 +1,70 @@ +add(new create( + $this->config, + $this->language, + $this->log, + $this->post_helper, + $this->search_backend_factory, + $this->state_helper, + $this->user + )); + + $command = $application->find('searchindex:create'); + + return new CommandTester($command); + } + + public function test_create() + { + $command_tester = $this->get_command_tester(); + + $command_tester->execute([ + 'search-backend' => 'search_backend_mock', + ]); + + $this->assertEquals(Command::SUCCESS, $command_tester->getStatusCode()); + $this->assertStringContainsString('CLI_SEARCHINDEX_CREATE_SUCCESS', $command_tester->getDisplay()); + } + + public function test_create_when_search_backend_dont_exist() + { + $command_tester = $this->get_command_tester(); + + $command_tester->execute([ + 'search-backend' => 'missing', + ]); + + $this->assertEquals(Command::FAILURE, $command_tester->getStatusCode()); + $this->assertStringContainsString('CLI_SEARCHINDEX_BACKEND_NOT_FOUND', $command_tester->getDisplay()); + } + + public function test_create_when_action_in_progress() + { + $this->config['search_indexing_state'] = ['not', 'empty']; + + $command_tester = $this->get_command_tester(); + + $command_tester->execute([ + 'search-backend' => 'search_backend_mock', + ]); + + $this->assertEquals(Command::FAILURE, $command_tester->getStatusCode()); + $this->assertStringContainsString('CLI_SEARCHINDEX_ACTION_IN_PROGRESS', $command_tester->getDisplay()); + + $this->config['search_indexing_state'] = []; + } +} diff --git a/tests/console/searchindex/delete_test.php b/tests/console/searchindex/delete_test.php new file mode 100644 index 0000000000..2e17a99e48 --- /dev/null +++ b/tests/console/searchindex/delete_test.php @@ -0,0 +1,70 @@ +add(new delete( + $this->config, + $this->language, + $this->log, + $this->post_helper, + $this->search_backend_factory, + $this->state_helper, + $this->user + )); + + $command = $application->find('searchindex:delete'); + + return new CommandTester($command); + } + + public function test_delete() + { + $command_tester = $this->get_command_tester(); + + $command_tester->execute([ + 'search-backend' => 'search_backend_mock', + ]); + + $this->assertEquals(Command::SUCCESS, $command_tester->getStatusCode()); + $this->assertStringContainsString('CLI_SEARCHINDEX_DELETE_SUCCESS', $command_tester->getDisplay()); + } + + public function test_delete_when_search_backend_dont_exist() + { + $command_tester = $this->get_command_tester(); + + $command_tester->execute([ + 'search-backend' => 'missing', + ]); + + $this->assertEquals(Command::FAILURE, $command_tester->getStatusCode()); + $this->assertStringContainsString('CLI_SEARCHINDEX_BACKEND_NOT_FOUND', $command_tester->getDisplay()); + } + + public function test_delete_when_action_in_progress() + { + $this->config['search_indexing_state'] = ['not', 'empty']; + + $command_tester = $this->get_command_tester(); + + $command_tester->execute([ + 'search-backend' => 'search_backend_mock', + ]); + + $this->assertEquals(Command::FAILURE, $command_tester->getStatusCode()); + $this->assertStringContainsString('CLI_SEARCHINDEX_ACTION_IN_PROGRESS', $command_tester->getDisplay()); + + $this->config['search_indexing_state'] = []; + } +} diff --git a/tests/console/searchindex/list_all_test.php b/tests/console/searchindex/list_all_test.php new file mode 100644 index 0000000000..b2308b3806 --- /dev/null +++ b/tests/console/searchindex/list_all_test.php @@ -0,0 +1,38 @@ +add(new list_all( + $this->config, + $this->language, + $this->search_backend_collection, + $this->user + )); + + $command = $application->find('searchindex:list'); + + return new CommandTester($command); + } + + public function test_list() + { + $command_tester = $this->get_command_tester(); + + $command_tester->execute([]); + + $this->assertEquals(Command::SUCCESS, $command_tester->getStatusCode()); + $this->assertStringContainsString('Mock search backend', $command_tester->getDisplay()); + $this->assertStringContainsString('ACTIVE', $command_tester->getDisplay()); + } +} diff --git a/tests/console/searchindex/mock/search_backend_mock.php b/tests/console/searchindex/mock/search_backend_mock.php new file mode 100644 index 0000000000..0f2db43eaf --- /dev/null +++ b/tests/console/searchindex/mock/search_backend_mock.php @@ -0,0 +1,105 @@ +index_created = true; + return null; + } + + public function delete_index(int &$post_counter = 0): ?array + { + $this->index_created = true; + return null; + } + + public function index_created(): bool + { + return $this->index_created; + } + + public function index_stats() + { + return []; + } + + public function get_acp_options(): array + { + return []; + } + + public function get_type(): string + { + return static::class; + } +} From 2e917b60599b464b057d22d5d00f93d858ae22fc Mon Sep 17 00:00:00 2001 From: Ruben Calvo Date: Tue, 23 Nov 2021 09:37:22 +0100 Subject: [PATCH 13/20] [ticket/12683] Remove unused property PHPBB3-12683 --- phpBB/phpbb/console/command/searchindex/create.php | 8 +------- phpBB/phpbb/console/command/searchindex/delete.php | 8 +------- phpBB/phpbb/console/command/searchindex/list_all.php | 2 +- tests/console/searchindex/create_test.php | 1 - tests/console/searchindex/delete_test.php | 1 - 5 files changed, 3 insertions(+), 17 deletions(-) diff --git a/phpBB/phpbb/console/command/searchindex/create.php b/phpBB/phpbb/console/command/searchindex/create.php index 6652e38159..ce282e94f8 100644 --- a/phpBB/phpbb/console/command/searchindex/create.php +++ b/phpBB/phpbb/console/command/searchindex/create.php @@ -13,7 +13,6 @@ namespace phpbb\console\command\searchindex; -use phpbb\config\config; use phpbb\console\command\command; use phpbb\language\language; use phpbb\log\log; @@ -29,9 +28,6 @@ use Symfony\Component\Console\Style\SymfonyStyle; class create extends command { - /** @var config */ - protected $config; - /** @var language */ protected $language; @@ -50,7 +46,6 @@ class create extends command /** * Construct method * - * @param config $config * @param language $language * @param log $log * @param post_helper $post_helper @@ -58,9 +53,8 @@ class create extends command * @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, state_helper $state_helper, user $user) + public function __construct(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; diff --git a/phpBB/phpbb/console/command/searchindex/delete.php b/phpBB/phpbb/console/command/searchindex/delete.php index 618bdce03d..5fe2514eea 100644 --- a/phpBB/phpbb/console/command/searchindex/delete.php +++ b/phpBB/phpbb/console/command/searchindex/delete.php @@ -13,7 +13,6 @@ namespace phpbb\console\command\searchindex; -use phpbb\config\config; use phpbb\console\command\command; use phpbb\language\language; use phpbb\log\log; @@ -29,9 +28,6 @@ use Symfony\Component\Console\Style\SymfonyStyle; class delete extends command { - /** @var config */ - protected $config; - /** @var language */ protected $language; @@ -50,7 +46,6 @@ class delete extends command /** * Construct method * - * @param config $config * @param language $language * @param log $log * @param post_helper $post_helper @@ -58,9 +53,8 @@ class delete extends command * @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, state_helper $state_helper, user $user) + public function __construct(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; diff --git a/phpBB/phpbb/console/command/searchindex/list_all.php b/phpBB/phpbb/console/command/searchindex/list_all.php index cfa26e7893..cf22b8e538 100644 --- a/phpBB/phpbb/console/command/searchindex/list_all.php +++ b/phpBB/phpbb/console/command/searchindex/list_all.php @@ -81,7 +81,7 @@ class list_all extends command foreach ($this->search_backend_collection as $search_backend) { $name = $search_backend->get_type(); - $active = ($name === $this->config['search_type']) ? '(' . $this->language->lang('ACTIVE') . ') ' : ''; + $active = ($name === $this->config['search_type']) ? '(' . $this->language->lang('$') . ') ' : ''; $search_backends[] = '' . $name . ' ' . $active . $search_backend->get_name(); if ($name === $this->config['search_type'] && !$search_backend->index_created()) diff --git a/tests/console/searchindex/create_test.php b/tests/console/searchindex/create_test.php index 43178e8408..fef0117e7b 100644 --- a/tests/console/searchindex/create_test.php +++ b/tests/console/searchindex/create_test.php @@ -14,7 +14,6 @@ class create_test extends base { $application = new Application(); $application->add(new create( - $this->config, $this->language, $this->log, $this->post_helper, diff --git a/tests/console/searchindex/delete_test.php b/tests/console/searchindex/delete_test.php index 2e17a99e48..953fe556fc 100644 --- a/tests/console/searchindex/delete_test.php +++ b/tests/console/searchindex/delete_test.php @@ -14,7 +14,6 @@ class delete_test extends base { $application = new Application(); $application->add(new delete( - $this->config, $this->language, $this->log, $this->post_helper, From 23ce5121a490cc9408ffc4c700308b1157d16ccb Mon Sep 17 00:00:00 2001 From: Ruben Calvo Date: Tue, 30 Nov 2021 00:04:46 +0100 Subject: [PATCH 14/20] [ticket/12683] Code review PHPBB3-12683 --- phpBB/language/en/cli.php | 2 +- .../phpbb/console/command/searchindex/create.php | 10 +++++----- .../phpbb/console/command/searchindex/delete.php | 10 +++++----- .../console/command/searchindex/list_all.php | 3 ++- phpBB/phpbb/search/backend/fulltext_native.php | 12 +++--------- phpBB/phpbb/search/state_helper.php | 6 +++--- tests/console/searchindex/create_test.php | 6 +++--- tests/console/searchindex/delete_test.php | 6 +++--- tests/console/searchindex/list_all_test.php | 6 +++--- ...ase.php => phpbb_console_searchindex_base.php} | 15 +++++++++++++-- .../searchindex => }/mock/search_backend_mock.php | 0 11 files changed, 41 insertions(+), 35 deletions(-) rename tests/console/searchindex/{base.php => phpbb_console_searchindex_base.php} (82%) rename tests/{console/searchindex => }/mock/search_backend_mock.php (100%) diff --git a/phpBB/language/en/cli.php b/phpBB/language/en/cli.php index 35f6f0b1df..8ad12848ae 100644 --- a/phpBB/language/en/cli.php +++ b/phpBB/language/en/cli.php @@ -152,7 +152,7 @@ $lang = array_merge($lang, array( 'CLI_SEARCHINDEX_CREATE_FAILURE' => 'Error creating search index', 'CLI_SEARCHINDEX_DELETE_SUCCESS' => 'Search index deleted successfully', 'CLI_SEARCHINDEX_DELETE_FAILURE' => 'Error deleting search index', - 'CLI_SEARCHINDEX_ACTION_IN_PROGRESS' => 'There is an action currently in progress. CLI doesn’t support incomplete index/delete actions, please solve it from the ACP', + 'CLI_SEARCHINDEX_ACTION_IN_PROGRESS' => 'There is an action currently in progress. CLI doesn’t support incomplete index/delete actions, please solve it from the ACP.', 'CLI_SEARCHINDEX_ACTIVE_NOT_INDEXED' => 'Active search backend isn’t indexed', // In all the case %1$s is the logical name of the file and %2$s the real name on the filesystem diff --git a/phpBB/phpbb/console/command/searchindex/create.php b/phpBB/phpbb/console/command/searchindex/create.php index ce282e94f8..9d6c71a67b 100644 --- a/phpBB/phpbb/console/command/searchindex/create.php +++ b/phpBB/phpbb/console/command/searchindex/create.php @@ -21,6 +21,7 @@ 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\Command\Command as symfony_command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -108,13 +109,13 @@ class create extends command catch (no_search_backend_found_exception $e) { $io->error($this->language->lang('CLI_SEARCHINDEX_BACKEND_NOT_FOUND', $search_backend)); - return command::FAILURE; + return symfony_command::FAILURE; } if ($this->state_helper->is_action_in_progress()) { $io->error($this->language->lang('CLI_SEARCHINDEX_ACTION_IN_PROGRESS', $search_backend)); - return command::FAILURE; + return symfony_command::FAILURE; } try @@ -130,7 +131,6 @@ class create extends command { $this->state_helper->update_counter($status['post_counter']); - $progress->setMaxSteps($status['max_post_id']); $progress->setProgress($status['post_counter']); $progress->setMessage(round($status['rows_per_second'], 2) . ' rows/s'); } @@ -142,7 +142,7 @@ class create extends command catch (\Exception $e) { $io->error($this->language->lang('CLI_SEARCHINDEX_CREATE_FAILURE', $name)); - return command::FAILURE; + return symfony_command::FAILURE; } $search->tidy(); @@ -152,6 +152,6 @@ class create extends command $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; + return symfony_command::SUCCESS; } } diff --git a/phpBB/phpbb/console/command/searchindex/delete.php b/phpBB/phpbb/console/command/searchindex/delete.php index 5fe2514eea..78e045b5c8 100644 --- a/phpBB/phpbb/console/command/searchindex/delete.php +++ b/phpBB/phpbb/console/command/searchindex/delete.php @@ -21,6 +21,7 @@ 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\Command\Command as symfony_command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -108,13 +109,13 @@ class delete extends command catch (no_search_backend_found_exception $e) { $io->error($this->language->lang('CLI_SEARCHINDEX_BACKEND_NOT_FOUND', $search_backend)); - return command::FAILURE; + return symfony_command::FAILURE; } if ($this->state_helper->is_action_in_progress()) { $io->error($this->language->lang('CLI_SEARCHINDEX_ACTION_IN_PROGRESS', $search_backend)); - return command::FAILURE; + return symfony_command::FAILURE; } try @@ -130,7 +131,6 @@ class delete extends command { $this->state_helper->update_counter($status['post_counter']); - $progress->setMaxSteps($status['max_post_id']); $progress->setProgress($status['post_counter']); $progress->setMessage(round($status['rows_per_second'], 2) . ' rows/s'); } @@ -142,7 +142,7 @@ class delete extends command catch (\Exception $e) { $io->error($this->language->lang('CLI_SEARCHINDEX_DELETE_FAILURE', $name)); - return command::FAILURE; + return symfony_command::FAILURE; } $search->tidy(); @@ -152,6 +152,6 @@ class delete extends command $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; + return symfony_command::SUCCESS; } } diff --git a/phpBB/phpbb/console/command/searchindex/list_all.php b/phpBB/phpbb/console/command/searchindex/list_all.php index cf22b8e538..566efc1921 100644 --- a/phpBB/phpbb/console/command/searchindex/list_all.php +++ b/phpBB/phpbb/console/command/searchindex/list_all.php @@ -18,6 +18,7 @@ use phpbb\console\command\command; use phpbb\di\service_collection; use phpbb\language\language; use phpbb\user; +use Symfony\Component\Console\Command\Command as symfony_command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; @@ -92,6 +93,6 @@ class list_all extends command $io->listing($search_backends); - return command::SUCCESS; + return symfony_command::SUCCESS; } } diff --git a/phpBB/phpbb/search/backend/fulltext_native.php b/phpBB/phpbb/search/backend/fulltext_native.php index 6c97ba7a52..3d76638d67 100644 --- a/phpBB/phpbb/search/backend/fulltext_native.php +++ b/phpBB/phpbb/search/backend/fulltext_native.php @@ -93,19 +93,13 @@ class fulltext_native extends base implements search_backend_interface */ protected $phpbb_dispatcher; - /** - * @var language - */ + /** @var language */ protected $language; - /** - * @var string - */ + /** @var string */ protected $search_wordlist_table; - /** - * @var string - */ + /** @var string */ protected $search_wordmatch_table; /** diff --git a/phpBB/phpbb/search/state_helper.php b/phpBB/phpbb/search/state_helper.php index ba546f6173..4380d43980 100644 --- a/phpBB/phpbb/search/state_helper.php +++ b/phpBB/phpbb/search/state_helper.php @@ -44,7 +44,7 @@ class state_helper } /** - * Returns if there is an action in progress + * Returns whether there is an action in progress * * @return bool */ @@ -101,7 +101,7 @@ class state_helper */ public function init(string $search_type, string $action): void { - // Is not possible to start a new process when there is one already running + // It's 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(); @@ -158,7 +158,7 @@ class state_helper */ private function load_state(): array { - // Is not possible to execute an action over state if is empty + // Is not possible to execute an action over state if it's empty if (!$this->is_action_in_progress()) { throw new no_action_in_progress_exception(); diff --git a/tests/console/searchindex/create_test.php b/tests/console/searchindex/create_test.php index fef0117e7b..88c42ab897 100644 --- a/tests/console/searchindex/create_test.php +++ b/tests/console/searchindex/create_test.php @@ -5,10 +5,10 @@ use Symfony\Component\Console\Application; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Tester\CommandTester; -require_once __DIR__ . '/base.php'; -require_once __DIR__ . '/mock/search_backend_mock.php'; +require_once __DIR__ . '/phpbb_console_searchindex_base.php'; +require_once __DIR__ . '/../../mock/search_backend_mock.php'; -class create_test extends base +class phpbb_console_searchindex_create_test extends phpbb_console_searchindex_base { public function get_command_tester() { diff --git a/tests/console/searchindex/delete_test.php b/tests/console/searchindex/delete_test.php index 953fe556fc..5551231afa 100644 --- a/tests/console/searchindex/delete_test.php +++ b/tests/console/searchindex/delete_test.php @@ -5,10 +5,10 @@ use Symfony\Component\Console\Application; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Tester\CommandTester; -require_once __DIR__ . '/base.php'; -require_once __DIR__ . '/mock/search_backend_mock.php'; +require_once __DIR__ . '/phpbb_console_searchindex_base.php'; +require_once __DIR__ . '/../../mock/search_backend_mock.php'; -class delete_test extends base +class phpbb_console_searchindex_delete_test extends phpbb_console_searchindex_base { public function get_command_tester() { diff --git a/tests/console/searchindex/list_all_test.php b/tests/console/searchindex/list_all_test.php index b2308b3806..deeb262b43 100644 --- a/tests/console/searchindex/list_all_test.php +++ b/tests/console/searchindex/list_all_test.php @@ -5,10 +5,10 @@ use Symfony\Component\Console\Application; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Tester\CommandTester; -require_once __DIR__ . '/base.php'; -require_once __DIR__ . '/mock/search_backend_mock.php'; +require_once __DIR__ . '/phpbb_console_searchindex_base.php'; +require_once __DIR__ . '/../../mock/search_backend_mock.php'; -class list_all_test extends base +class phpbb_console_searchindex_list_all_test extends phpbb_console_searchindex_base { public function get_command_tester() { diff --git a/tests/console/searchindex/base.php b/tests/console/searchindex/phpbb_console_searchindex_base.php similarity index 82% rename from tests/console/searchindex/base.php rename to tests/console/searchindex/phpbb_console_searchindex_base.php index 15372a5582..58d850cc6e 100644 --- a/tests/console/searchindex/base.php +++ b/tests/console/searchindex/phpbb_console_searchindex_base.php @@ -1,4 +1,15 @@ + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ use phpbb\config\config; use phpbb\di\service_collection; @@ -9,9 +20,9 @@ use phpbb\search\search_backend_factory; use phpbb\search\state_helper; use phpbb\user; -require_once __DIR__ . '/mock/search_backend_mock.php'; +require_once __DIR__ . '/../../mock/search_backend_mock.php'; -class base extends phpbb_test_case +class phpbb_console_searchindex_base extends phpbb_test_case { /** @var config */ protected $config; diff --git a/tests/console/searchindex/mock/search_backend_mock.php b/tests/mock/search_backend_mock.php similarity index 100% rename from tests/console/searchindex/mock/search_backend_mock.php rename to tests/mock/search_backend_mock.php From e6af4c4def3cf9e28aab64e77db269b26ad18e2f Mon Sep 17 00:00:00 2001 From: Ruben Calvo Date: Wed, 2 Feb 2022 00:54:30 +0100 Subject: [PATCH 15/20] [ticket/12683] Fix post index creation PHPBB3-12683 --- phpBB/includes/acp/acp_search.php | 5 +++-- phpBB/phpbb/search/backend/base.php | 12 +++++------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/phpBB/includes/acp/acp_search.php b/phpBB/includes/acp/acp_search.php index d5b2f3335b..f13b9c8dea 100644 --- a/phpBB/includes/acp/acp_search.php +++ b/phpBB/includes/acp/acp_search.php @@ -345,13 +345,14 @@ class acp_search * * @param string $id * @param string $mode - * @param string $action Action in progress: 'create' or 'delete' */ - private function index_inprogress(string $id, string $mode, string $action): void + private function index_inprogress(string $id, string $mode): void { $this->tpl_name = 'acp_search_index_inprogress'; $this->page_title = 'ACP_SEARCH_INDEX'; + $action = $this->search_state_helper->action(); + $this->template->assign_vars([ 'U_ACTION' => $this->u_action . '&action=' . $action . '&hash=' . generate_link_hash('acp_search'), 'UA_PROGRESS_BAR' => addslashes($this->u_action . '&action=progress_bar'), diff --git a/phpBB/phpbb/search/backend/base.php b/phpBB/phpbb/search/backend/base.php index 76be5d8cfe..e195e0f4c0 100644 --- a/phpBB/phpbb/search/backend/base.php +++ b/phpBB/phpbb/search/backend/base.php @@ -336,8 +336,7 @@ abstract class base implements search_backend_interface $starttime = microtime(true); $row_count = 0; - $still_on_time = PHP_SAPI === 'cli' ? true : still_on_time(); - while ($still_on_time && $post_counter <= $max_post_id) + while (still_on_time() && $post_counter < $max_post_id) { $rows = $this->get_posts_batch_after($post_counter); @@ -354,7 +353,7 @@ abstract class base implements search_backend_interface $this->index('post', (int) $row['post_id'], $row['post_text'], $row['post_subject'], (int) $row['poster_id'], (int) $row['forum_id']); } $row_count++; - $post_counter = $row['post_id']; + $post_counter = (int) $row['post_id']; } // With cli process only one batch each time to be able to track progress @@ -371,7 +370,7 @@ abstract class base implements search_backend_interface $this->tidy(); $this->config['num_posts'] = $num_posts; - if ($post_counter < $max_post_id) + if ($post_counter < $max_post_id) // If there are still post to index { $totaltime = microtime(true) - $starttime; $rows_per_second = $row_count / $totaltime; @@ -397,8 +396,7 @@ abstract class base implements search_backend_interface $starttime = microtime(true); $row_count = 0; - $still_on_time = PHP_SAPI === 'cli' ? true : still_on_time(); - while ($still_on_time && $post_counter <= $max_post_id) + while (still_on_time() && $post_counter < $max_post_id) { $rows = $this->get_posts_batch_after($post_counter); $ids = $posters = $forum_ids = array(); @@ -423,7 +421,7 @@ abstract class base implements search_backend_interface } } - if ($post_counter < $max_post_id) + if ($post_counter < $max_post_id) // If there are still post delete from index { $totaltime = microtime(true) - $starttime; $rows_per_second = $row_count / $totaltime; From 1c191868de317f132879bb1bee5ddea29f57c798 Mon Sep 17 00:00:00 2001 From: Ruben Calvo Date: Thu, 17 Mar 2022 02:59:34 +0100 Subject: [PATCH 16/20] [ticket/12683] Undo changes in fulltext_sphinx.php PHPBB3-12683 --- .../phpbb/search/backend/fulltext_sphinx.php | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/phpBB/phpbb/search/backend/fulltext_sphinx.php b/phpBB/phpbb/search/backend/fulltext_sphinx.php index 8023bc161a..709690b4f2 100644 --- a/phpBB/phpbb/search/backend/fulltext_sphinx.php +++ b/phpBB/phpbb/search/backend/fulltext_sphinx.php @@ -631,24 +631,24 @@ class fulltext_sphinx implements search_backend_interface */ public function create_index(int &$post_counter = 0): ?array { - $table_data = array( - 'COLUMNS' => array( - 'counter_id' => array('UINT', 0), - 'max_doc_id' => array('UINT', 0), - ), - 'PRIMARY_KEY' => 'counter_id', - ); - $this->db_tools->sql_create_table(SPHINX_TABLE, $table_data); + if (!$this->index_created()) + { + $table_data = array( + 'COLUMNS' => array( + 'counter_id' => array('UINT', 0), + 'max_doc_id' => array('UINT', 0), + ), + 'PRIMARY_KEY' => 'counter_id', + ); + $this->db_tools->sql_create_table(SPHINX_TABLE, $table_data); - $sql = 'TRUNCATE TABLE ' . SPHINX_TABLE; - $this->db->sql_query($sql); - - $data = array( - 'counter_id' => '1', - 'max_doc_id' => '0', - ); - $sql = 'INSERT INTO ' . SPHINX_TABLE . ' ' . $this->db->sql_build_array('INSERT', $data); - $this->db->sql_query($sql); + $data = array( + 'counter_id' => '1', + 'max_doc_id' => '0', + ); + $sql = 'INSERT INTO ' . SPHINX_TABLE . ' ' . $this->db->sql_build_array('INSERT', $data); + $this->db->sql_query($sql); + } return null; } @@ -658,7 +658,10 @@ class fulltext_sphinx implements search_backend_interface */ public function delete_index(int &$post_counter = null): ?array { - $this->db_tools->sql_table_drop(SPHINX_TABLE); + if ($this->index_created()) + { + $this->db_tools->sql_table_drop(SPHINX_TABLE); + } return null; } From 8cea785f36f8b8326c31ebe2e1e7efadf4242512 Mon Sep 17 00:00:00 2001 From: Ruben Calvo Date: Sat, 10 Dec 2022 00:23:44 +0100 Subject: [PATCH 17/20] [ticket/12683] Improve exception handling PHPBB3-12683 --- .../default/container/services_console.yml | 2 -- phpBB/language/en/acp/search.php | 2 ++ phpBB/language/en/common.php | 4 ++++ .../console/command/searchindex/list_all.php | 2 +- phpBB/phpbb/cron/task/core/tidy_search.php | 3 ++- phpBB/phpbb/di/exception/di_exception.php | 21 +++++++++++++++++++ ...multiple_service_definitions_exception.php | 8 +++++++ .../exception/service_not_found_exception.php | 8 +++++++ phpBB/phpbb/di/service_collection.php | 6 ++++-- phpBB/phpbb/search/search_backend_factory.php | 13 +++--------- phpBB/phpbb/search/state_helper.php | 2 +- 11 files changed, 54 insertions(+), 17 deletions(-) create mode 100644 phpBB/phpbb/di/exception/di_exception.php create mode 100644 phpBB/phpbb/di/exception/multiple_service_definitions_exception.php create mode 100644 phpBB/phpbb/di/exception/service_not_found_exception.php diff --git a/phpBB/config/default/container/services_console.yml b/phpBB/config/default/container/services_console.yml index ff29a83e22..f23d205b8f 100644 --- a/phpBB/config/default/container/services_console.yml +++ b/phpBB/config/default/container/services_console.yml @@ -259,7 +259,6 @@ services: console.command.searchindex.create: class: phpbb\console\command\searchindex\create arguments: - - '@config' - '@language' - '@log' - '@post.helper' @@ -272,7 +271,6 @@ services: console.command.searchindex.delete: class: phpbb\console\command\searchindex\delete arguments: - - '@config' - '@language' - '@log' - '@post.helper' diff --git a/phpBB/language/en/acp/search.php b/phpBB/language/en/acp/search.php index a79c67fd8e..183ccf0d7b 100644 --- a/phpBB/language/en/acp/search.php +++ b/phpBB/language/en/acp/search.php @@ -91,6 +91,8 @@ $lang = array_merge($lang, array( 'GENERAL_SEARCH_SETTINGS' => 'General search settings', 'GO_TO_SEARCH_INDEX' => 'Go to search index page', + 'INVALID_ACTION' => 'Invalid action', + 'INDEX_STATS' => 'Index statistics', 'INDEXING_IN_PROGRESS' => 'Indexing in progress', 'INDEXING_IN_PROGRESS_EXPLAIN' => 'The search backend is currently indexing all posts on the board. This can take from a few minutes to a few hours depending on your board’s size.', diff --git a/phpBB/language/en/common.php b/phpBB/language/en/common.php index 6a10e8b846..8333755f87 100644 --- a/phpBB/language/en/common.php +++ b/phpBB/language/en/common.php @@ -215,6 +215,9 @@ $lang = array_merge($lang, array( 2 => 'Downloaded %d times', ), + 'DI_SERVICE_NOT_FOUND' => 'No service found for class "%s" in collection.', + 'DI_MULTIPLE_SERVICE_DEFINITIONS' => 'More than one service definitions found for class "%s" in collection.', + 'EDIT_POST' => 'Edit post', 'ELLIPSIS' => '…', 'EMAIL' => 'Email', // Short form for EMAIL_ADDRESS @@ -715,6 +718,7 @@ $lang = array_merge($lang, array( 'SEARCH_UNANSWERED' => 'Unanswered topics', 'SEARCH_UNREAD' => 'Unread posts', 'SEARCH_USER_POSTS' => 'Search user’s posts', + 'SEARCH_BACKEND_NOT_FOUND' => 'No search backend could be found.', 'SECONDS' => 'Seconds', 'SEE_ALL' => 'See All', 'SELECT' => 'Select', diff --git a/phpBB/phpbb/console/command/searchindex/list_all.php b/phpBB/phpbb/console/command/searchindex/list_all.php index 566efc1921..d23f46ea20 100644 --- a/phpBB/phpbb/console/command/searchindex/list_all.php +++ b/phpBB/phpbb/console/command/searchindex/list_all.php @@ -82,7 +82,7 @@ class list_all extends command foreach ($this->search_backend_collection as $search_backend) { $name = $search_backend->get_type(); - $active = ($name === $this->config['search_type']) ? '(' . $this->language->lang('$') . ') ' : ''; + $active = ($name === $this->config['search_type']) ? '(' . $this->language->lang('ACTIVE') . ') ' : ''; $search_backends[] = '' . $name . ' ' . $active . $search_backend->get_name(); if ($name === $this->config['search_type'] && !$search_backend->index_created()) diff --git a/phpBB/phpbb/cron/task/core/tidy_search.php b/phpBB/phpbb/cron/task/core/tidy_search.php index d3f5d41bc0..a29a403627 100644 --- a/phpBB/phpbb/cron/task/core/tidy_search.php +++ b/phpBB/phpbb/cron/task/core/tidy_search.php @@ -15,6 +15,7 @@ namespace phpbb\cron\task\core; use phpbb\config\config; use phpbb\cron\task\base; +use phpbb\di\exception\di_exception; use phpbb\search\backend\search_backend_interface; use phpbb\search\search_backend_factory; @@ -88,7 +89,7 @@ class tidy_search extends base $this->active_search = $this->search_backend_factory->get_active(); } } - catch (\RuntimeException $e) + catch (di_exception $e) { return false; } diff --git a/phpBB/phpbb/di/exception/di_exception.php b/phpBB/phpbb/di/exception/di_exception.php new file mode 100644 index 0000000000..957e4f1b81 --- /dev/null +++ b/phpBB/phpbb/di/exception/di_exception.php @@ -0,0 +1,21 @@ + + * @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\di\exception; + +use phpbb\exception\runtime_exception; + +class di_exception extends runtime_exception +{ + +} diff --git a/phpBB/phpbb/di/exception/multiple_service_definitions_exception.php b/phpBB/phpbb/di/exception/multiple_service_definitions_exception.php new file mode 100644 index 0000000000..66dc71489e --- /dev/null +++ b/phpBB/phpbb/di/exception/multiple_service_definitions_exception.php @@ -0,0 +1,8 @@ +offsetGet($service_id); diff --git a/phpBB/phpbb/search/search_backend_factory.php b/phpBB/phpbb/search/search_backend_factory.php index deabbdaba7..019d3f6804 100644 --- a/phpBB/phpbb/search/search_backend_factory.php +++ b/phpBB/phpbb/search/search_backend_factory.php @@ -14,10 +14,10 @@ namespace phpbb\search; use phpbb\config\config; +use phpbb\di\exception\service_not_found_exception; use phpbb\di\service_collection; use phpbb\search\backend\search_backend_interface; use phpbb\search\exception\no_search_backend_found_exception; -use RuntimeException; class search_backend_factory { @@ -58,16 +58,9 @@ class search_backend_factory { $search = $this->search_backends->get_by_class($class); } - catch (RuntimeException $e) + catch (service_not_found_exception $e) { - if (strpos($e->getMessage(), 'No service found') === 0) - { - throw new no_search_backend_found_exception(); - } - else - { - throw $e; - } + throw new no_search_backend_found_exception('SEARCH_BACKEND_NOT_FOUND', [], $e); } return $search; diff --git a/phpBB/phpbb/search/state_helper.php b/phpBB/phpbb/search/state_helper.php index 4380d43980..809ac38fe7 100644 --- a/phpBB/phpbb/search/state_helper.php +++ b/phpBB/phpbb/search/state_helper.php @@ -113,7 +113,7 @@ class state_helper // Make sure the action is correct (just in case) if (!in_array($action, ['create', 'delete'])) { - throw new search_exception('Invalid action'); + throw new search_exception('INVALID_ACTION'); } $state = [ From f9f55eb0125b6263682981ab4c1be62b70d90882 Mon Sep 17 00:00:00 2001 From: Ruben Calvo Date: Sun, 11 Dec 2022 16:42:02 +0100 Subject: [PATCH 18/20] [ticket/12683] Improve exception handling PHPBB3-12683 --- phpBB/language/en/cli.php | 1 + phpBB/language/en/common.php | 4 +-- .../console/command/searchindex/create.php | 10 ++++++++ .../console/command/searchindex/delete.php | 10 ++++++++ ...multiple_service_definitions_exception.php | 11 ++++++++ .../exception/service_not_found_exception.php | 11 ++++++++ tests/console/searchindex/create_test.php | 12 +++++++++ tests/console/searchindex/delete_test.php | 12 +++++++++ .../phpbb_console_searchindex_base.php | 10 +++++++- tests/di/service_collection_test.php | 4 +-- tests/mock/search_backend_mock.php | 12 +++++++++ .../search_backend_mock_not_available.php | 25 +++++++++++++++++++ 12 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 tests/mock/search_backend_mock_not_available.php diff --git a/phpBB/language/en/cli.php b/phpBB/language/en/cli.php index 8ad12848ae..80240b9cf2 100644 --- a/phpBB/language/en/cli.php +++ b/phpBB/language/en/cli.php @@ -154,6 +154,7 @@ $lang = array_merge($lang, array( 'CLI_SEARCHINDEX_DELETE_FAILURE' => 'Error deleting search index', 'CLI_SEARCHINDEX_ACTION_IN_PROGRESS' => 'There is an action currently in progress. CLI doesn’t support incomplete index/delete actions, please solve it from the ACP.', 'CLI_SEARCHINDEX_ACTIVE_NOT_INDEXED' => 'Active search backend isn’t indexed', + 'CLI_SEARCHINDEX_BACKEND_NOT_AVAILABLE' => 'Search backend isn’t available.', // In all the case %1$s is the logical name of the file and %2$s the real name on the filesystem // eg: big_image.png (2_a51529ae7932008cf8454a95af84cacd) generated. diff --git a/phpBB/language/en/common.php b/phpBB/language/en/common.php index 8333755f87..8d71ca2952 100644 --- a/phpBB/language/en/common.php +++ b/phpBB/language/en/common.php @@ -215,8 +215,8 @@ $lang = array_merge($lang, array( 2 => 'Downloaded %d times', ), - 'DI_SERVICE_NOT_FOUND' => 'No service found for class "%s" in collection.', - 'DI_MULTIPLE_SERVICE_DEFINITIONS' => 'More than one service definitions found for class "%s" in collection.', + 'DI_SERVICE_NOT_FOUND' => 'Service for class "%1$s" not found in collection.', + 'DI_MULTIPLE_SERVICE_DEFINITIONS' => 'More than one service definitions found for class "%1$s" in collection.', 'EDIT_POST' => 'Edit post', 'ELLIPSIS' => '…', diff --git a/phpBB/phpbb/console/command/searchindex/create.php b/phpBB/phpbb/console/command/searchindex/create.php index 9d6c71a67b..9a1d54444e 100644 --- a/phpBB/phpbb/console/command/searchindex/create.php +++ b/phpBB/phpbb/console/command/searchindex/create.php @@ -62,6 +62,8 @@ class create extends command $this->search_backend_factory = $search_backend_factory; $this->state_helper = $state_helper; + $this->language->add_lang(array('acp/common', 'acp/search')); + parent::__construct($user); } @@ -118,6 +120,12 @@ class create extends command return symfony_command::FAILURE; } + if (!$search->is_available()) + { + $io->error($this->language->lang('CLI_SEARCHINDEX_BACKEND_NOT_AVAILABLE', $search_backend)); + return symfony_command::FAILURE; + } + try { $progress = $this->create_progress_bar($this->post_helper->get_max_post_id(), $io, $output, true); @@ -141,6 +149,8 @@ class create extends command } catch (\Exception $e) { + $this->state_helper->clear_state(); // Unexpected error, cancel action + $io->error($e->getMessage()); // Show also exception message like in acp $io->error($this->language->lang('CLI_SEARCHINDEX_CREATE_FAILURE', $name)); return symfony_command::FAILURE; } diff --git a/phpBB/phpbb/console/command/searchindex/delete.php b/phpBB/phpbb/console/command/searchindex/delete.php index 78e045b5c8..02c94cf082 100644 --- a/phpBB/phpbb/console/command/searchindex/delete.php +++ b/phpBB/phpbb/console/command/searchindex/delete.php @@ -62,6 +62,8 @@ class delete extends command $this->search_backend_factory = $search_backend_factory; $this->state_helper = $state_helper; + $this->language->add_lang(array('acp/common', 'acp/search')); + parent::__construct($user); } @@ -118,6 +120,12 @@ class delete extends command return symfony_command::FAILURE; } + if (!$search->is_available()) + { + $io->error($this->language->lang('CLI_SEARCHINDEX_BACKEND_NOT_AVAILABLE', $search_backend)); + return symfony_command::FAILURE; + } + try { $progress = $this->create_progress_bar($this->post_helper->get_max_post_id(), $io, $output, true); @@ -141,6 +149,8 @@ class delete extends command } catch (\Exception $e) { + $this->state_helper->clear_state(); // Unexpected error, cancel action + $io->error($e->getMessage()); // Show also exception message like in acp $io->error($this->language->lang('CLI_SEARCHINDEX_DELETE_FAILURE', $name)); return symfony_command::FAILURE; } diff --git a/phpBB/phpbb/di/exception/multiple_service_definitions_exception.php b/phpBB/phpbb/di/exception/multiple_service_definitions_exception.php index 66dc71489e..1c89b7a229 100644 --- a/phpBB/phpbb/di/exception/multiple_service_definitions_exception.php +++ b/phpBB/phpbb/di/exception/multiple_service_definitions_exception.php @@ -1,4 +1,15 @@ + * @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\di\exception; diff --git a/phpBB/phpbb/di/exception/service_not_found_exception.php b/phpBB/phpbb/di/exception/service_not_found_exception.php index 22f5c89ed4..06cb909d9c 100644 --- a/phpBB/phpbb/di/exception/service_not_found_exception.php +++ b/phpBB/phpbb/di/exception/service_not_found_exception.php @@ -1,4 +1,15 @@ + * @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\di\exception; diff --git a/tests/console/searchindex/create_test.php b/tests/console/searchindex/create_test.php index 88c42ab897..4e6a438f29 100644 --- a/tests/console/searchindex/create_test.php +++ b/tests/console/searchindex/create_test.php @@ -66,4 +66,16 @@ class phpbb_console_searchindex_create_test extends phpbb_console_searchindex_ba $this->config['search_indexing_state'] = []; } + + public function test_create_when_search_backend_not_available() + { + $command_tester = $this->get_command_tester(); + + $command_tester->execute([ + 'search-backend' => 'search_backend_mock_not_available', + ]); + + $this->assertEquals(Command::FAILURE, $command_tester->getStatusCode()); + $this->assertStringContainsString('CLI_SEARCHINDEX_BACKEND_NOT_AVAILABLE', $command_tester->getDisplay()); + } } diff --git a/tests/console/searchindex/delete_test.php b/tests/console/searchindex/delete_test.php index 5551231afa..1d920648ba 100644 --- a/tests/console/searchindex/delete_test.php +++ b/tests/console/searchindex/delete_test.php @@ -66,4 +66,16 @@ class phpbb_console_searchindex_delete_test extends phpbb_console_searchindex_ba $this->config['search_indexing_state'] = []; } + + public function test_delete_when_search_backend_not_available() + { + $command_tester = $this->get_command_tester(); + + $command_tester->execute([ + 'search-backend' => 'search_backend_mock_not_available', + ]); + + $this->assertEquals(Command::FAILURE, $command_tester->getStatusCode()); + $this->assertStringContainsString('CLI_SEARCHINDEX_BACKEND_NOT_AVAILABLE', $command_tester->getDisplay()); + } } diff --git a/tests/console/searchindex/phpbb_console_searchindex_base.php b/tests/console/searchindex/phpbb_console_searchindex_base.php index 58d850cc6e..209d12281a 100644 --- a/tests/console/searchindex/phpbb_console_searchindex_base.php +++ b/tests/console/searchindex/phpbb_console_searchindex_base.php @@ -21,6 +21,7 @@ use phpbb\search\state_helper; use phpbb\user; require_once __DIR__ . '/../../mock/search_backend_mock.php'; +require_once __DIR__ . '/../../mock/search_backend_mock_not_available.php'; class phpbb_console_searchindex_base extends phpbb_test_case { @@ -70,12 +71,18 @@ class phpbb_console_searchindex_base extends phpbb_test_case $this->user = $this->createMock('\phpbb\user'); $phpbb_container = new phpbb_mock_container_builder(); - $search_backend_mock = new search_backend_mock(); $this->search_backend_collection = new \phpbb\di\service_collection($phpbb_container); + + $search_backend_mock = new search_backend_mock(); $this->search_backend_collection->add('search_backend_mock'); $this->search_backend_collection->add_service_class('search_backend_mock', 'search_backend_mock'); $phpbb_container->set('search_backend_mock', $search_backend_mock); + $search_backend_mock_not_available = new search_backend_mock_not_available(); + $this->search_backend_collection->add('search_backend_mock_not_available'); + $this->search_backend_collection->add_service_class('search_backend_mock_not_available', 'search_backend_mock_not_available'); + $phpbb_container->set('search_backend_mock_not_available', $search_backend_mock_not_available); + $this->search_backend_factory = new search_backend_factory($this->config, $this->search_backend_collection); $this->state_helper = new state_helper($this->config, $this->search_backend_factory); @@ -83,3 +90,4 @@ class phpbb_console_searchindex_base extends phpbb_test_case parent::setUp(); } } + diff --git a/tests/di/service_collection_test.php b/tests/di/service_collection_test.php index fd0e13e48b..7fc3e94e41 100644 --- a/tests/di/service_collection_test.php +++ b/tests/di/service_collection_test.php @@ -57,7 +57,7 @@ class phpbb_service_collection_test extends \phpbb_test_case public function test_get_by_class_many_services_exception() { $this->expectException('RuntimeException'); - $this->expectExceptionMessage('More than one service definitions found for class "bar_class" in collection.'); + $this->expectExceptionMessage('DI_MULTIPLE_SERVICE_DEFINITIONS'); $this->service_collection->get_by_class('bar_class'); } @@ -65,7 +65,7 @@ class phpbb_service_collection_test extends \phpbb_test_case public function test_get_by_class_no_service_exception() { $this->expectException('RuntimeException'); - $this->expectExceptionMessage('No service found for class "baz_class" in collection.'); + $this->expectExceptionMessage('DI_SERVICE_NOT_FOUND'); $this->service_collection->get_by_class('baz_class'); } diff --git a/tests/mock/search_backend_mock.php b/tests/mock/search_backend_mock.php index 0f2db43eaf..84dd26385e 100644 --- a/tests/mock/search_backend_mock.php +++ b/tests/mock/search_backend_mock.php @@ -1,4 +1,15 @@ + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ use phpbb\search\backend\search_backend_interface; @@ -103,3 +114,4 @@ class search_backend_mock implements search_backend_interface return static::class; } } + diff --git a/tests/mock/search_backend_mock_not_available.php b/tests/mock/search_backend_mock_not_available.php new file mode 100644 index 0000000000..5b299d334f --- /dev/null +++ b/tests/mock/search_backend_mock_not_available.php @@ -0,0 +1,25 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +class search_backend_mock_not_available extends search_backend_mock +{ + public function get_name(): string + { + return 'Mock unavailable search backend'; + } + + public function is_available(): bool + { + return false; + } +} From 6de699d318019324fd69a4468b2079b5d0d10ee0 Mon Sep 17 00:00:00 2001 From: Ruben Calvo Date: Sun, 11 Dec 2022 16:54:22 +0100 Subject: [PATCH 19/20] [ticket/12683] Small code style improvements PHPBB3-12683 --- phpBB/phpbb/console/command/reparser/list_all.php | 6 ++---- phpBB/phpbb/console/command/searchindex/create.php | 6 ++---- phpBB/phpbb/console/command/searchindex/delete.php | 6 ++---- phpBB/phpbb/console/command/searchindex/list_all.php | 8 +++----- phpBB/phpbb/di/exception/di_exception.php | 1 - .../exception/multiple_service_definitions_exception.php | 1 - phpBB/phpbb/di/exception/service_not_found_exception.php | 1 - phpBB/phpbb/search/backend/fulltext_native.php | 4 +++- phpBB/phpbb/search/state_helper.php | 4 ++-- 9 files changed, 14 insertions(+), 23 deletions(-) diff --git a/phpBB/phpbb/console/command/reparser/list_all.php b/phpBB/phpbb/console/command/reparser/list_all.php index ae90c1a68f..83c301e00f 100644 --- a/phpBB/phpbb/console/command/reparser/list_all.php +++ b/phpBB/phpbb/console/command/reparser/list_all.php @@ -49,10 +49,8 @@ class list_all extends \phpbb\console\command\command */ protected function configure() { - $this - ->setName('reparser:list') - ->setDescription($this->user->lang('CLI_DESCRIPTION_REPARSER_LIST')) - ; + $this->setName('reparser:list') + ->setDescription($this->user->lang('CLI_DESCRIPTION_REPARSER_LIST')); } /** diff --git a/phpBB/phpbb/console/command/searchindex/create.php b/phpBB/phpbb/console/command/searchindex/create.php index 9a1d54444e..2b368a475d 100644 --- a/phpBB/phpbb/console/command/searchindex/create.php +++ b/phpBB/phpbb/console/command/searchindex/create.php @@ -74,15 +74,13 @@ class create extends command */ protected function configure() { - $this - ->setName('searchindex:create') + $this->setName('searchindex:create') ->setDescription($this->language->lang('CLI_DESCRIPTION_SEARCHINDEX_CREATE')) ->addArgument( 'search-backend', InputArgument::REQUIRED, $this->language->lang('CLI_SEARCHINDEX_SEARCH_BACKEND_NAME') - ) - ; + ); } /** diff --git a/phpBB/phpbb/console/command/searchindex/delete.php b/phpBB/phpbb/console/command/searchindex/delete.php index 02c94cf082..b17622d68c 100644 --- a/phpBB/phpbb/console/command/searchindex/delete.php +++ b/phpBB/phpbb/console/command/searchindex/delete.php @@ -74,15 +74,13 @@ class delete extends command */ protected function configure() { - $this - ->setName('searchindex:delete') + $this->setName('searchindex:delete') ->setDescription($this->language->lang('CLI_DESCRIPTION_SEARCHINDEX_DELETE')) ->addArgument( 'search-backend', InputArgument::REQUIRED, $this->language->lang('CLI_SEARCHINDEX_SEARCH_BACKEND_NAME') - ) - ; + ); } /** diff --git a/phpBB/phpbb/console/command/searchindex/list_all.php b/phpBB/phpbb/console/command/searchindex/list_all.php index d23f46ea20..e88949fd2a 100644 --- a/phpBB/phpbb/console/command/searchindex/list_all.php +++ b/phpBB/phpbb/console/command/searchindex/list_all.php @@ -58,10 +58,8 @@ class list_all extends command */ protected function configure() { - $this - ->setName('searchindex:list') - ->setDescription($this->language->lang('CLI_DESCRIPTION_SEARCHINDEX_LIST')) - ; + $this->setName('searchindex:list') + ->setDescription($this->language->lang('CLI_DESCRIPTION_SEARCHINDEX_LIST')); } /** @@ -74,7 +72,7 @@ class list_all extends command * * @return int 0 if all is well, 1 if any errors occurred */ - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); diff --git a/phpBB/phpbb/di/exception/di_exception.php b/phpBB/phpbb/di/exception/di_exception.php index 957e4f1b81..b4f3984b39 100644 --- a/phpBB/phpbb/di/exception/di_exception.php +++ b/phpBB/phpbb/di/exception/di_exception.php @@ -17,5 +17,4 @@ use phpbb\exception\runtime_exception; class di_exception extends runtime_exception { - } diff --git a/phpBB/phpbb/di/exception/multiple_service_definitions_exception.php b/phpBB/phpbb/di/exception/multiple_service_definitions_exception.php index 1c89b7a229..e29d47d26c 100644 --- a/phpBB/phpbb/di/exception/multiple_service_definitions_exception.php +++ b/phpBB/phpbb/di/exception/multiple_service_definitions_exception.php @@ -15,5 +15,4 @@ namespace phpbb\di\exception; class multiple_service_definitions_exception extends di_exception { - } diff --git a/phpBB/phpbb/di/exception/service_not_found_exception.php b/phpBB/phpbb/di/exception/service_not_found_exception.php index 06cb909d9c..79c69c85f6 100644 --- a/phpBB/phpbb/di/exception/service_not_found_exception.php +++ b/phpBB/phpbb/di/exception/service_not_found_exception.php @@ -15,5 +15,4 @@ namespace phpbb\di\exception; class service_not_found_exception extends di_exception { - } diff --git a/phpBB/phpbb/search/backend/fulltext_native.php b/phpBB/phpbb/search/backend/fulltext_native.php index 3d76638d67..aefc73b8cd 100644 --- a/phpBB/phpbb/search/backend/fulltext_native.php +++ b/phpBB/phpbb/search/backend/fulltext_native.php @@ -116,7 +116,9 @@ class fulltext_native extends base implements search_backend_interface * @param string $phpbb_root_path phpBB root path * @param string $phpEx PHP file extension */ - public function __construct(config $config, driver_interface $db, dispatcher_interface $phpbb_dispatcher, language $language, user $user, string $search_results_table, string $search_wordlist_table, string $search_wordmatch_table, string $phpbb_root_path, string $phpEx) + public function __construct(config $config, driver_interface $db, dispatcher_interface $phpbb_dispatcher, + language $language, user $user, string $search_results_table, string $search_wordlist_table, + string $search_wordmatch_table, string $phpbb_root_path, string $phpEx) { global $cache; diff --git a/phpBB/phpbb/search/state_helper.php b/phpBB/phpbb/search/state_helper.php index 809ac38fe7..b024c3dfe4 100644 --- a/phpBB/phpbb/search/state_helper.php +++ b/phpBB/phpbb/search/state_helper.php @@ -146,7 +146,7 @@ class state_helper */ public function clear_state(): void { - $this->save_state([]); + $this->save_state(); } /** @@ -176,6 +176,6 @@ class state_helper { ksort($state); - $this->config->set('search_indexing_state', implode(',', $state), true); + $this->config->set('search_indexing_state', implode(',', $state)); } } From d6b0827d270ba816c1be0f9be4dfc6ec8f84b15c Mon Sep 17 00:00:00 2001 From: Ruben Calvo Date: Sun, 11 Dec 2022 21:22:01 +0100 Subject: [PATCH 20/20] [ticket/12683] Add copyright header PHPBB3-12683 --- tests/console/searchindex/create_test.php | 11 +++++++++++ tests/console/searchindex/delete_test.php | 11 +++++++++++ tests/console/searchindex/list_all_test.php | 11 +++++++++++ 3 files changed, 33 insertions(+) diff --git a/tests/console/searchindex/create_test.php b/tests/console/searchindex/create_test.php index 4e6a438f29..8d4958442c 100644 --- a/tests/console/searchindex/create_test.php +++ b/tests/console/searchindex/create_test.php @@ -1,4 +1,15 @@ + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ use phpbb\console\command\searchindex\create; use Symfony\Component\Console\Application; diff --git a/tests/console/searchindex/delete_test.php b/tests/console/searchindex/delete_test.php index 1d920648ba..ac7762effd 100644 --- a/tests/console/searchindex/delete_test.php +++ b/tests/console/searchindex/delete_test.php @@ -1,4 +1,15 @@ + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ use phpbb\console\command\searchindex\delete; use Symfony\Component\Console\Application; diff --git a/tests/console/searchindex/list_all_test.php b/tests/console/searchindex/list_all_test.php index deeb262b43..7968032644 100644 --- a/tests/console/searchindex/list_all_test.php +++ b/tests/console/searchindex/list_all_test.php @@ -1,4 +1,15 @@ + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ use phpbb\console\command\searchindex\list_all; use Symfony\Component\Console\Application;