From 6ae68baa2e5fa052763429d1ddc783989e72c24b Mon Sep 17 00:00:00 2001 From: rubencm Date: Tue, 23 Mar 2021 22:23:10 +0100 Subject: [PATCH] [ticket/15540] Code changes PHPBB3-15540 --- .../default/container/services_search.yml | 8 ++-- phpBB/includes/acp/acp_search.php | 40 ++++++++++--------- phpBB/includes/functions_convert.php | 28 ------------- phpBB/install/convert/convert.php | 2 - phpBB/install/convert/convertor.php | 2 - .../install_data/task/create_search_index.php | 2 +- phpBB/phpbb/search/backend/base.php | 22 +++++----- phpBB/phpbb/search/backend/fulltext_mysql.php | 5 +-- .../phpbb/search/backend/fulltext_native.php | 2 +- .../search/backend/fulltext_postgres.php | 8 ++-- .../phpbb/search/backend/fulltext_sphinx.php | 2 +- .../backend/search_backend_interface.php | 2 +- phpBB/phpbb/search/search_backend_factory.php | 4 +- phpBB/search.php | 2 +- 14 files changed, 50 insertions(+), 79 deletions(-) diff --git a/phpBB/config/default/container/services_search.yml b/phpBB/config/default/container/services_search.yml index f056f71295..1dba3732be 100644 --- a/phpBB/config/default/container/services_search.yml +++ b/phpBB/config/default/container/services_search.yml @@ -1,8 +1,8 @@ services: # Search backends - search.fulltext.native: - class: phpbb\search\backend\fulltext_native + search.fulltext.mysql: + class: phpbb\search\backend\fulltext_mysql arguments: - '@config' - '@dbal.conn' @@ -14,8 +14,8 @@ services: tags: - { name: search.backend } - search.fulltext.mysql: - class: phpbb\search\backend\fulltext_mysql + search.fulltext.native: + class: phpbb\search\backend\fulltext_native arguments: - '@config' - '@dbal.conn' diff --git a/phpBB/includes/acp/acp_search.php b/phpBB/includes/acp/acp_search.php index 751760e2fc..0723376f6f 100644 --- a/phpBB/includes/acp/acp_search.php +++ b/phpBB/includes/acp/acp_search.php @@ -25,6 +25,10 @@ class acp_search var $state; var $search; + protected const STATE_SEARCH_TYPE = 0; + protected const STATE_ACTION = 1; + protected const STATE_POST_COUNTER = 2; + function main($id, $mode) { global $user; @@ -80,7 +84,6 @@ class acp_search // Only show available search backends if ($search->is_available()) { - $name = $search->get_name(); $type = get_class($search); @@ -252,25 +255,24 @@ class acp_search break; case 'delete': - $this->state[1] = 'delete'; + $this->state[self::STATE_ACTION] = 'delete'; break; case 'create': - $this->state[1] = 'create'; + $this->state[self::STATE_ACTION] = 'create'; break; default: trigger_error('NO_ACTION', E_USER_ERROR); - break; } - if (empty($this->state[0])) + if (empty($this->state[self::STATE_SEARCH_TYPE])) { - $this->state[0] = $request->variable('search_type', ''); + $this->state[self::STATE_SEARCH_TYPE] = $request->variable('search_type', ''); } $search_backend_factory = $phpbb_container->get('search.backend_factory'); - $this->search = $search_backend_factory->get($this->state[0]); + $this->search = $search_backend_factory->get($this->state[self::STATE_SEARCH_TYPE]); $name = $this->search->get_name(); @@ -283,8 +285,8 @@ class acp_search case 'delete': try { - $this->state[2] = $this->state[2] ?? 0; - if ($status = $this->search->delete_index($this->state[2])) // Status is not null, so deleting is in progress.... + $this->state[self::STATE_POST_COUNTER] = $this->state[self::STATE_POST_COUNTER] ?? 0; + if ($status = $this->search->delete_index($this->state[self::STATE_POST_COUNTER])) // Status is not null, so deleting is in progress.... { // save the current state $this->save_state(); @@ -296,14 +298,14 @@ class acp_search } catch (Exception $e) { - $this->state = array(''); + $this->state = []; $this->save_state(); trigger_error($e->getMessage() . adm_back_link($this->u_action) . $this->close_popup_js(), E_USER_WARNING); } $this->search->tidy(); - $this->state = array(''); + $this->state = []; $this->save_state(); $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_SEARCH_INDEX_REMOVED', false, array($name)); @@ -313,8 +315,8 @@ class acp_search case 'create': try { - $this->state[2] = $this->state[2] ?? 0; - if ($status = $this->search->create_index($this->state[2])) // Status is not null, so indexing is in progress.... + $this->state[self::STATE_POST_COUNTER] = $this->state[self::STATE_POST_COUNTER] ?? 0; + if ($status = $this->search->create_index($this->state[self::STATE_POST_COUNTER])) // Status is not null, so indexing is in progress.... { // save the current state $this->save_state(); @@ -327,7 +329,7 @@ class acp_search catch (Exception $e) { // Error executing create_index - $this->state = array(''); + $this->state = []; $this->save_state(); trigger_error($e->getMessage() . adm_back_link($this->u_action) . $this->close_popup_js(), E_USER_WARNING); } @@ -336,7 +338,7 @@ class acp_search $this->search->tidy(); - $this->state = array(''); + $this->state = []; $this->save_state(); $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_SEARCH_INDEX_CREATED', false, array($name)); @@ -409,13 +411,13 @@ class acp_search 'UA_PROGRESS_BAR' => addslashes(append_sid("{$phpbb_admin_path}index.$phpEx", "i=$id&mode=$mode&action=progress_bar")), )); - if (isset($this->state[1])) + if (isset($this->state[self::STATE_ACTION])) { $template->assign_vars(array( 'S_CONTINUE_INDEXING' => $this->state[1], - 'U_CONTINUE_INDEXING' => $this->u_action . '&action=' . $this->state[1] . '&hash=' . generate_link_hash('acp_search'), - 'L_CONTINUE' => ($this->state[1] == 'create') ? $user->lang['CONTINUE_INDEXING'] : $user->lang['CONTINUE_DELETING_INDEX'], - 'L_CONTINUE_EXPLAIN' => ($this->state[1] == 'create') ? $user->lang['CONTINUE_INDEXING_EXPLAIN'] : $user->lang['CONTINUE_DELETING_INDEX_EXPLAIN']) + 'U_CONTINUE_INDEXING' => $this->u_action . '&action=' . $this->state[self::STATE_ACTION] . '&hash=' . generate_link_hash('acp_search'), + 'L_CONTINUE' => ($this->state[self::STATE_ACTION] == 'create') ? $user->lang['CONTINUE_INDEXING'] : $user->lang['CONTINUE_DELETING_INDEX'], + 'L_CONTINUE_EXPLAIN' => ($this->state[self::STATE_ACTION] == 'create') ? $user->lang['CONTINUE_INDEXING_EXPLAIN'] : $user->lang['CONTINUE_DELETING_INDEX_EXPLAIN']) ); } } diff --git a/phpBB/includes/functions_convert.php b/phpBB/includes/functions_convert.php index 056f968cfc..4bb10fc8d1 100644 --- a/phpBB/includes/functions_convert.php +++ b/phpBB/includes/functions_convert.php @@ -1039,34 +1039,6 @@ function set_user_options() return $option_field; } -/** -* Index messages on the fly as we convert them -* @todo naderman, can you check that this works with the new search plugins as it's use is currently disabled (and thus untested) -function search_indexing($message = '') -{ - global $fulltext_search, $convert_row; - - if (!isset($convert_row['post_id'])) - { - return; - } - - if (!$message) - { - if (!isset($convert_row['message'])) - { - return; - } - - $message = $convert_row['message']; - } - - $title = (isset($convert_row['title'])) ? $convert_row['title'] : ''; - - $fulltext_search->index('post', $convert_row['post_id'], $message, $title, $convert_row['poster_id'], $convert_row['forum_id']); -} -*/ - function make_unique_filename($filename) { if (!strlen($filename)) diff --git a/phpBB/install/convert/convert.php b/phpBB/install/convert/convert.php index 3e9e562f2e..9c460cc954 100644 --- a/phpBB/install/convert/convert.php +++ b/phpBB/install/convert/convert.php @@ -39,8 +39,6 @@ class convert var $src_truncate_statement = 'DELETE FROM '; var $truncate_statement = 'DELETE FROM '; - var $fulltext_search; - // Batch size, can be adjusted by the conversion file // For big boards a value of 6000 seems to be optimal var $batch_size = 2000; diff --git a/phpBB/install/convert/convertor.php b/phpBB/install/convert/convertor.php index ab87243952..b52a0b03bc 100644 --- a/phpBB/install/convert/convertor.php +++ b/phpBB/install/convert/convertor.php @@ -224,8 +224,6 @@ class convertor trigger_error('NO_SUCH_SEARCH_MODULE'); } - $convert->fulltext_search = new $search_type($config, $db, $phpbb_dispatcher, $user, $phpbb_root_path, $phpEx); - include_once($phpbb_root_path . 'includes/message_parser.' . $phpEx); $message_parser = new \parse_message(); 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..79b53f329a 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 @@ -180,7 +180,7 @@ class create_search_index extends database_task /** * {@inheritdoc} */ - public static function get_step_count() : int + static public function get_step_count() : int { return 1; } diff --git a/phpBB/phpbb/search/backend/base.php b/phpBB/phpbb/search/backend/base.php index 1bf16c8ea1..0245c72c2a 100644 --- a/phpBB/phpbb/search/backend/base.php +++ b/phpBB/phpbb/search/backend/base.php @@ -71,15 +71,15 @@ abstract class base implements search_backend_interface * Retrieves cached search results * * @param string $search_key an md5 string generated from all the passed search options to identify the results - * @param int &$result_count will contain the number of all results for the search (not only for the current page) - * @param array &$id_ary is filled with the ids belonging to the requested page that are stored in the cache - * @param int &$start indicates the first index of the page + * @param int &$result_count will contain the number of all results for the search (not only for the current page) + * @param array &$id_ary is filled with the ids belonging to the requested page that are stored in the cache + * @param int &$start indicates the first index of the page * @param int $per_page number of ids each page is supposed to contain * @param string $sort_dir is either a or d representing ASC and DESC * * @return int self::SEARCH_RESULT_NOT_IN_CACHE or self::SEARCH_RESULT_IN_CACHE or self::SEARCH_RESULT_INCOMPLETE */ - protected function obtain_ids(string $search_key, &$result_count, &$id_ary, &$start, $per_page, string $sort_dir): int + protected function obtain_ids(string $search_key, int &$result_count, array &$id_ary, int &$start, int $per_page, string $sort_dir): int { if (!($stored_ids = $this->cache->get('_search_results_' . $search_key))) { @@ -89,7 +89,7 @@ abstract class base implements search_backend_interface else { $result_count = $stored_ids[-1]; - $reverse_ids = ($stored_ids[-2] != $sort_dir) ? true : false; + $reverse_ids = $stored_ids[-2] != $sort_dir; $complete = true; // Change start parameter in case out of bounds @@ -106,7 +106,7 @@ abstract class base implements search_backend_interface } // change the start to the actual end of the current request if the sort direction differs - // from the dirction in the cache and reverse the ids later + // from the direction in the cache and reverse the ids later if ($reverse_ids) { $start = $result_count - $start - $per_page; @@ -151,14 +151,14 @@ abstract class base implements search_backend_interface * @param string $keywords contains the keywords as entered by the user * @param array $author_ary an array of author ids, if the author should be ignored during the search the array is empty * @param int $result_count contains the number of all results for the search (not only for the current page) - * @param array &$id_ary contains a list of post or topic ids that shall be cached, the first element + * @param array &$id_ary contains a list of post or topic ids that shall be cached, the first element * must have the absolute index $start in the result set. * @param int $start indicates the first index of the page * @param string $sort_dir is either a or d representing ASC and DESC * - * @return null + * @return void */ - protected function save_ids(string $search_key, string $keywords, $author_ary, int $result_count, &$id_ary, int $start, string $sort_dir) + protected function save_ids(string $search_key, string $keywords, array $author_ary, int $result_count, array &$id_ary, int $start, string $sort_dir): void { global $user; @@ -268,7 +268,7 @@ abstract class base implements search_backend_interface * @param array $words * @param array|bool $authors */ - protected function destroy_cache($words, $authors = false): void + protected function destroy_cache(array $words, $authors = false): void { // clear all searches that searched for the specified words if (count($words)) @@ -415,6 +415,8 @@ abstract class base implements search_backend_interface 'rows_per_second' => $rows_per_second, ]; } + + return null; } /** diff --git a/phpBB/phpbb/search/backend/fulltext_mysql.php b/phpBB/phpbb/search/backend/fulltext_mysql.php index 22f72e0aaf..0885e441a7 100644 --- a/phpBB/phpbb/search/backend/fulltext_mysql.php +++ b/phpBB/phpbb/search/backend/fulltext_mysql.php @@ -880,8 +880,7 @@ class fulltext_mysql extends base implements search_backend_interface ); extract($this->phpbb_dispatcher->trigger_event('core.search_mysql_index_before', compact($vars))); - unset($split_text); - unset($split_title); + unset($split_text, $split_title); // destroy cached search results containing any of the words removed or added $this->destroy_cache($words, array($poster_id)); @@ -1146,7 +1145,7 @@ class fulltext_mysql extends base implements search_backend_interface /** * {@inheritdoc} */ - public function acp(): array + public function get_acp_options(): array { $tpl = '
diff --git a/phpBB/phpbb/search/backend/fulltext_native.php b/phpBB/phpbb/search/backend/fulltext_native.php index 616b5adcc9..24a7d7d9bc 100644 --- a/phpBB/phpbb/search/backend/fulltext_native.php +++ b/phpBB/phpbb/search/backend/fulltext_native.php @@ -1996,7 +1996,7 @@ class fulltext_native extends base implements search_backend_interface /** * {@inheritdoc} */ - public function acp(): array + public function get_acp_options(): array { /** * if we need any options, copied from fulltext_native for now, will have to be adjusted or removed diff --git a/phpBB/phpbb/search/backend/fulltext_postgres.php b/phpBB/phpbb/search/backend/fulltext_postgres.php index 824326a4f3..b1855f3119 100644 --- a/phpBB/phpbb/search/backend/fulltext_postgres.php +++ b/phpBB/phpbb/search/backend/fulltext_postgres.php @@ -343,16 +343,16 @@ class fulltext_postgres extends base implements search_backend_interface case 'u': $sql_sort_table = USERS_TABLE . ' u, '; $sql_sort_join = ($type == 'posts') ? ' AND u.user_id = p.poster_id ' : ' AND u.user_id = t.topic_poster '; - break; + break; case 't': $join_topic = true; - break; + break; case 'f': $sql_sort_table = FORUMS_TABLE . ' f, '; $sql_sort_join = ' AND f.forum_id = p.forum_id '; - break; + break; } // Build some display specific sql strings @@ -1081,7 +1081,7 @@ class fulltext_postgres extends base implements search_backend_interface /** * {@inheritdoc} */ - public function acp(): array + public function get_acp_options(): array { $tpl = '
diff --git a/phpBB/phpbb/search/backend/fulltext_sphinx.php b/phpBB/phpbb/search/backend/fulltext_sphinx.php index c59be909ac..cc6c0b8afe 100644 --- a/phpBB/phpbb/search/backend/fulltext_sphinx.php +++ b/phpBB/phpbb/search/backend/fulltext_sphinx.php @@ -783,7 +783,7 @@ class fulltext_sphinx implements search_backend_interface /** * {@inheritdoc} */ - public function acp(): array + public function get_acp_options(): array { $config_vars = array( 'fulltext_sphinx_data_path' => 'string', diff --git a/phpBB/phpbb/search/backend/search_backend_interface.php b/phpBB/phpbb/search/backend/search_backend_interface.php index 4a31b74014..1fdc138028 100644 --- a/phpBB/phpbb/search/backend/search_backend_interface.php +++ b/phpBB/phpbb/search/backend/search_backend_interface.php @@ -192,5 +192,5 @@ interface search_backend_interface * * @return array array containing template and config variables */ - public function acp(): array; + public function get_acp_options(): array; } diff --git a/phpBB/phpbb/search/search_backend_factory.php b/phpBB/phpbb/search/search_backend_factory.php index 5ab6bce8af..eac31885ed 100644 --- a/phpBB/phpbb/search/search_backend_factory.php +++ b/phpBB/phpbb/search/search_backend_factory.php @@ -44,11 +44,11 @@ class search_backend_factory /** * Obtains a specified search backend * - * @param string $class + * @param string $class * * @return search_backend_interface */ - public function get($class): search_backend_interface + public function get(string $class): search_backend_interface { return $this->search_backends->get_by_class($class); } diff --git a/phpBB/search.php b/phpBB/search.php index 93ec09e334..8decc78bb5 100644 --- a/phpBB/search.php +++ b/phpBB/search.php @@ -29,7 +29,7 @@ $mode = $request->variable('mode', ''); $search_id = $request->variable('search_id', ''); $start = max($request->variable('start', 0), 0); $post_id = $request->variable('p', 0); -$topic_id = (int) $request->variable('t', 0); +$topic_id = $request->variable('t', 0); $view = $request->variable('view', ''); $submit = $request->variable('submit', false);