From 911a31d898c86e612f15315efc27faf9349ac33d Mon Sep 17 00:00:00 2001 From: rubencm Date: Mon, 22 Mar 2021 16:57:40 +0100 Subject: [PATCH] [ticket/15540] Add types and phpdoc PHPBB3-15540 --- .../{v330 => v400}/search_backend_update.php | 28 ++++-- phpBB/phpbb/search/backend/base.php | 96 +++++++++---------- phpBB/phpbb/search/backend/fulltext_mysql.php | 51 +++++----- .../phpbb/search/backend/fulltext_native.php | 6 +- .../search/backend/fulltext_postgres.php | 6 +- .../phpbb/search/backend/fulltext_sphinx.php | 25 ++--- phpBB/phpbb/search/search_backend_factory.php | 4 +- 7 files changed, 115 insertions(+), 101 deletions(-) rename phpBB/phpbb/db/migration/data/{v330 => v400}/search_backend_update.php (55%) diff --git a/phpBB/phpbb/db/migration/data/v330/search_backend_update.php b/phpBB/phpbb/db/migration/data/v400/search_backend_update.php similarity index 55% rename from phpBB/phpbb/db/migration/data/v330/search_backend_update.php rename to phpBB/phpbb/db/migration/data/v400/search_backend_update.php index f7c979fc5e..f94bd67107 100644 --- a/phpBB/phpbb/db/migration/data/v330/search_backend_update.php +++ b/phpBB/phpbb/db/migration/data/v400/search_backend_update.php @@ -11,25 +11,37 @@ * */ -namespace phpbb\db\migration\data\v330; +namespace phpbb\db\migration\data\v400; + +use phpbb\search\backend\fulltext_mysql; +use phpbb\search\backend\fulltext_postgres; +use phpbb\search\backend\fulltext_sphinx; +use phpbb\search\backend\fulltext_native; class search_backend_update extends \phpbb\db\migration\migration { + static public function depends_on() + { + return [ + '\phpbb\db\migration\data\v400\dev', + ]; + } + public function update_data() { switch ($this->config['search_type']) { case '\\phpbb\\search\\fulltext_mysql': - $new_search_type = 'phpbb\\search\\backend\\fulltext_mysql'; - break; + $new_search_type = fulltext_mysql::class; + break; case '\\phpbb\\search\\fulltext_postgres': - $new_search_type = 'phpbb\\search\\backend\\fulltext_postgres'; - break; + $new_search_type = fulltext_postgres::class; + break; case '\\phpbb\\search\\fulltext_sphinx': - $new_search_type = 'phpbb\\search\\backend\\fulltext_sphinx'; - break; + $new_search_type = fulltext_sphinx::class; + break; default: - $new_search_type = 'phpbb\\search\\backend\\fulltext_native'; + $new_search_type = fulltext_native::class; } return [ diff --git a/phpBB/phpbb/search/backend/base.php b/phpBB/phpbb/search/backend/base.php index 019226a8cc..c6e0934da5 100644 --- a/phpBB/phpbb/search/backend/base.php +++ b/phpBB/phpbb/search/backend/base.php @@ -13,43 +13,36 @@ namespace phpbb\search\backend; -/** -* @ignore -*/ -define('SEARCH_RESULT_NOT_IN_CACHE', 0); -define('SEARCH_RESULT_IN_CACHE', 1); -define('SEARCH_RESULT_INCOMPLETE', 2); - /** * optional base class for search plugins providing simple caching based on ACM * and functions to retrieve ignore_words and synonyms */ -abstract class base +abstract class base implements search_backend_interface { - var $ignore_words = array(); - var $match_synonym = array(); - var $replace_synonym = array(); + public const SEARCH_RESULT_NOT_IN_CACHE = 0; + public const SEARCH_RESULT_IN_CACHE = 1; + public const SEARCH_RESULT_INCOMPLETE = 2; /** - * 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 $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 SEARCH_RESULT_NOT_IN_CACHE or SEARCH_RESULT_IN_CACHE or SEARCH_RESULT_INCOMPLETE - */ - function obtain_ids($search_key, &$result_count, &$id_ary, &$start, $per_page, $sort_dir) + * 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 $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 + */ + public function obtain_ids(string $search_key, &$result_count, &$id_ary, &$start, $per_page, string $sort_dir): int { global $cache; if (!($stored_ids = $cache->get('_search_results_' . $search_key))) { // no search results cached for this search_key - return SEARCH_RESULT_NOT_IN_CACHE; + return self::SEARCH_RESULT_NOT_IN_CACHE; } else { @@ -79,7 +72,7 @@ abstract class base // the user requested a page past the last index if ($start < 0) { - return SEARCH_RESULT_NOT_IN_CACHE; + return self::SEARCH_RESULT_NOT_IN_CACHE; } } @@ -103,27 +96,27 @@ abstract class base if (!$complete) { - return SEARCH_RESULT_INCOMPLETE; + return self::SEARCH_RESULT_INCOMPLETE; } - return SEARCH_RESULT_IN_CACHE; + return self::SEARCH_RESULT_IN_CACHE; } } /** - * Caches post/topic ids - * - * @param string $search_key an md5 string generated from all the passed search options to identify the results - * @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 - * 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 - */ - function save_ids($search_key, $keywords, $author_ary, $result_count, &$id_ary, $start, $sort_dir) + * Caches post/topic ids + * + * @param string $search_key an md5 string generated from all the passed search options to identify the results + * @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 + * 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 + */ + public function save_ids(string $search_key, string $keywords, $author_ary, int $result_count, &$id_ary, int $start, string $sort_dir) { global $cache, $config, $db, $user; @@ -224,15 +217,16 @@ abstract class base $db->sql_query($sql); } - unset($store); - unset($store_ids); - unset($id_range); + unset($store, $store_ids, $id_range); } /** - * Removes old entries from the search results table and removes searches with keywords that contain a word in $words. - */ - function destroy_cache($words, $authors = false) + * Removes old entries from the search results table and removes searches with keywords that contain a word in $words. + * + * @param array $words + * @param array|bool $authors + */ + public function destroy_cache($words, $authors = false): void { global $db, $cache, $config; @@ -369,9 +363,9 @@ abstract class base while (still_on_time() && $post_counter <= $acp_module->max_post_id) { $sql = 'SELECT post_id, poster_id, forum_id - FROM ' . POSTS_TABLE . ' - WHERE post_id >= ' . (int) ($post_counter + 1) . ' - AND post_id <= ' . (int) ($post_counter + $acp_module->batch_size); + FROM ' . POSTS_TABLE . ' + WHERE post_id >= ' . (int) ($post_counter + 1) . ' + AND post_id <= ' . (int) ($post_counter + $acp_module->batch_size); $result = $this->db->sql_query($sql); $ids = $posters = $forum_ids = array(); @@ -381,7 +375,7 @@ abstract class base $posters[] = $row['poster_id']; $forum_ids[] = $row['forum_id']; } - $db->sql_freeresult($result); + $result->sql_freeresult($result); $row_count += count($ids); if (count($ids)) diff --git a/phpBB/phpbb/search/backend/fulltext_mysql.php b/phpBB/phpbb/search/backend/fulltext_mysql.php index 2da5ab2221..116ab67201 100644 --- a/phpBB/phpbb/search/backend/fulltext_mysql.php +++ b/phpBB/phpbb/search/backend/fulltext_mysql.php @@ -13,6 +13,11 @@ namespace phpbb\search\backend; +use phpbb\config\config; +use phpbb\db\driver\driver_interface; +use phpbb\event\dispatcher_interface; +use phpbb\user; + /** * Fulltext search for MySQL */ @@ -32,25 +37,25 @@ class fulltext_mysql extends base implements search_backend_interface /** * Config object - * @var \phpbb\config\config + * @var config */ protected $config; /** * Database connection - * @var \phpbb\db\driver\driver_interface + * @var driver_interface */ protected $db; /** * phpBB event dispatcher object - * @var \phpbb\event\dispatcher_interface + * @var dispatcher_interface */ protected $phpbb_dispatcher; /** * User object - * @var \phpbb\user + * @var user */ protected $user; @@ -78,14 +83,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 \phpbb\config\config $config Config object - * @param \phpbb\db\driver\driver_interface $db Database object - * @param \phpbb\event\dispatcher_interface $phpbb_dispatcher Event dispatcher object - * @param \phpbb\user $user User object + * @param config $config Config object + * @param driver_interface $db Database object + * @param dispatcher_interface $phpbb_dispatcher Event dispatcher object + * @param user $user User object * @param string $phpbb_root_path Relative path to phpBB root * @param string $phpEx PHP file extension */ - public function __construct($config, $db, $phpbb_dispatcher, $user, $phpbb_root_path, $phpEx) + public function __construct(config $config, driver_interface $db, dispatcher_interface $phpbb_dispatcher, user $user, string $phpbb_root_path, string $phpEx) { $this->config = $config; $this->db = $db; @@ -106,7 +111,7 @@ class fulltext_mysql extends base implements search_backend_interface /** * {@inheritdoc} */ - public function get_name() + public function get_name(): string { return 'MySQL Fulltext'; } @@ -114,7 +119,7 @@ class fulltext_mysql extends base implements search_backend_interface /** * {@inheritdoc} */ - public function get_search_query() + public function get_search_query(): string { return $this->search_query; } @@ -122,7 +127,7 @@ class fulltext_mysql extends base implements search_backend_interface /** * {@inheritdoc} */ - public function get_common_words() + public function get_common_words(): array { return $this->common_words; } @@ -130,7 +135,7 @@ class fulltext_mysql extends base implements search_backend_interface /** * {@inheritdoc} */ - public function get_word_length() + public function get_word_length(): array { return $this->word_length; } @@ -168,8 +173,8 @@ class fulltext_mysql extends base implements search_backend_interface * We also require https://bugs.mysql.com/bug.php?id=67004 to be * fixed for proper overall operation. Hence we require 5.6.8. */ - || $engine === 'InnoDB' - && phpbb_version_compare($this->db->sql_server_info(true), '5.6.8', '>='); + || ($engine === 'InnoDB' + && phpbb_version_compare($this->db->sql_server_info(true), '5.6.8', '>=')); if (!$fulltext_supported) { @@ -204,7 +209,7 @@ class fulltext_mysql extends base implements search_backend_interface /** * {@inheritdoc} */ - public function split_keywords(&$keywords, $terms) + public function split_keywords(&$keywords, $terms): bool { if ($terms == 'all') { @@ -340,10 +345,12 @@ class fulltext_mysql extends base implements search_backend_interface } /** - * Turns text into an array of words - * @param string $text contains post text/subject - */ - public function split_message($text) + * Turns text into an array of words + * @param string $text contains post text/subject + * + * @return array + */ + public function split_message($text): array { // Split words $text = preg_replace('#([^\p{L}\p{N}\'*])#u', '$1$1', str_replace('\'\'', '\' \'', trim($text))); @@ -429,7 +436,7 @@ class fulltext_mysql extends base implements search_backend_interface // try reading the results from cache $result_count = 0; - if ($this->obtain_ids($search_key, $result_count, $id_ary, $start, $per_page, $sort_dir) == SEARCH_RESULT_IN_CACHE) + if ($this->obtain_ids($search_key, $result_count, $id_ary, $start, $per_page, $sort_dir) == self::SEARCH_RESULT_IN_CACHE) { return $result_count; } @@ -679,7 +686,7 @@ class fulltext_mysql extends base implements search_backend_interface // try reading the results from cache $result_count = 0; - if ($this->obtain_ids($search_key, $result_count, $id_ary, $start, $per_page, $sort_dir) == SEARCH_RESULT_IN_CACHE) + if ($this->obtain_ids($search_key, $result_count, $id_ary, $start, $per_page, $sort_dir) == self::SEARCH_RESULT_IN_CACHE) { return $result_count; } diff --git a/phpBB/phpbb/search/backend/fulltext_native.php b/phpBB/phpbb/search/backend/fulltext_native.php index 6bf3224fed..b7105f8cd6 100644 --- a/phpBB/phpbb/search/backend/fulltext_native.php +++ b/phpBB/phpbb/search/backend/fulltext_native.php @@ -120,7 +120,7 @@ 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, $db, $phpbb_dispatcher, $user, $phpbb_root_path, $phpEx) + public function __construct(config $config, driver_interface $db, dispatcher_interface $phpbb_dispatcher, user $user, string $phpbb_root_path, string $phpEx) { $this->config = $config; $this->db = $db; @@ -589,7 +589,7 @@ class fulltext_native extends base implements search_backend_interface // try reading the results from cache $total_results = 0; - if ($this->obtain_ids($search_key, $total_results, $id_ary, $start, $per_page, $sort_dir) == SEARCH_RESULT_IN_CACHE) + if ($this->obtain_ids($search_key, $total_results, $id_ary, $start, $per_page, $sort_dir) == self::SEARCH_RESULT_IN_CACHE) { return $total_results; } @@ -1053,7 +1053,7 @@ class fulltext_native extends base implements search_backend_interface // try reading the results from cache $total_results = 0; - if ($this->obtain_ids($search_key, $total_results, $id_ary, $start, $per_page, $sort_dir) == SEARCH_RESULT_IN_CACHE) + if ($this->obtain_ids($search_key, $total_results, $id_ary, $start, $per_page, $sort_dir) == self::SEARCH_RESULT_IN_CACHE) { return $total_results; } diff --git a/phpBB/phpbb/search/backend/fulltext_postgres.php b/phpBB/phpbb/search/backend/fulltext_postgres.php index 087f76559b..41b28bff61 100644 --- a/phpBB/phpbb/search/backend/fulltext_postgres.php +++ b/phpBB/phpbb/search/backend/fulltext_postgres.php @@ -103,7 +103,7 @@ class fulltext_postgres extends base implements search_backend_interface * @param string $phpbb_root_path Relative path to phpBB root * @param string $phpEx PHP file extension */ - public function __construct($config, $db, $phpbb_dispatcher, $user, $phpbb_root_path, $phpEx) + public function __construct(config $config, driver_interface $db, dispatcher_interface $phpbb_dispatcher, user $user, string $phpbb_root_path, string $phpEx) { $this->config = $config; $this->db = $db; @@ -364,7 +364,7 @@ class fulltext_postgres extends base implements search_backend_interface // try reading the results from cache $result_count = 0; - if ($this->obtain_ids($search_key, $result_count, $id_ary, $start, $per_page, $sort_dir) == SEARCH_RESULT_IN_CACHE) + if ($this->obtain_ids($search_key, $result_count, $id_ary, $start, $per_page, $sort_dir) == self::SEARCH_RESULT_IN_CACHE) { return $result_count; } @@ -624,7 +624,7 @@ class fulltext_postgres extends base implements search_backend_interface // try reading the results from cache $result_count = 0; - if ($this->obtain_ids($search_key, $result_count, $id_ary, $start, $per_page, $sort_dir) == SEARCH_RESULT_IN_CACHE) + if ($this->obtain_ids($search_key, $result_count, $id_ary, $start, $per_page, $sort_dir) == self::SEARCH_RESULT_IN_CACHE) { return $result_count; } diff --git a/phpBB/phpbb/search/backend/fulltext_sphinx.php b/phpBB/phpbb/search/backend/fulltext_sphinx.php index 4212eddfb1..da8c949b56 100644 --- a/phpBB/phpbb/search/backend/fulltext_sphinx.php +++ b/phpBB/phpbb/search/backend/fulltext_sphinx.php @@ -15,18 +15,19 @@ namespace phpbb\search\backend; use phpbb\auth\auth; use phpbb\config\config; +use phpbb\db\driver\driver_interface; use phpbb\event\dispatcher_interface; use phpbb\user; -define('SPHINX_MAX_MATCHES', 20000); -define('SPHINX_CONNECT_RETRIES', 3); -define('SPHINX_CONNECT_WAIT_TIME', 300); - /** * Fulltext search based on the sphinx search daemon */ class fulltext_sphinx implements search_backend_interface { + protected const SPHINX_MAX_MATCHES = 20000; + protected const SPHINX_CONNECT_RETRIES = 3; + protected const SPHINX_CONNECT_WAIT_TIME = 300; + /** * Associative array holding index stats * @var array @@ -131,14 +132,14 @@ class fulltext_sphinx implements search_backend_interface * * @param auth $auth Auth object * @param config $config Config object - * @param $db + * @param driver_interface $db Database object * @param dispatcher_interface $phpbb_dispatcher Event dispatcher object * @param user $user User object * @param string $phpbb_root_path Relative path to phpBB root * @param string $phpEx PHP file extension * @throws \Exception */ - public function __construct($auth, $config, $db, $phpbb_dispatcher, $user, $phpbb_root_path, $phpEx) + public function __construct(auth $auth, config $config, driver_interface $db, dispatcher_interface $phpbb_dispatcher, user $user, string $phpbb_root_path, string $phpEx) { $this->auth = $auth; $this->config = $config; @@ -684,15 +685,15 @@ class fulltext_sphinx implements search_backend_interface $this->sphinx->SetFilter('deleted', array(0)); - $this->sphinx->SetLimits((int) $start, (int) $per_page, max(SPHINX_MAX_MATCHES, (int) $start + $per_page)); + $this->sphinx->SetLimits((int) $start, (int) $per_page, max(self::SPHINX_MAX_MATCHES, (int) $start + $per_page)); $result = $this->sphinx->Query($search_query_prefix . $this->sphinx_clean_search_string(str_replace('"', '"', $this->search_query)), $this->indexes); // Could be connection to localhost:9312 failed (errno=111, // msg=Connection refused) during rotate, retry if so - $retries = SPHINX_CONNECT_RETRIES; + $retries = self::SPHINX_CONNECT_RETRIES; while (!$result && (strpos($this->sphinx->GetLastError(), "errno=111,") !== false) && $retries--) { - usleep(SPHINX_CONNECT_WAIT_TIME); + usleep(self::SPHINX_CONNECT_WAIT_TIME); $result = $this->sphinx->Query($search_query_prefix . $this->sphinx_clean_search_string(str_replace('"', '"', $this->search_query)), $this->indexes); } @@ -715,15 +716,15 @@ class fulltext_sphinx implements search_backend_interface { $start = floor(($result_count - 1) / $per_page) * $per_page; - $this->sphinx->SetLimits((int) $start, (int) $per_page, max(SPHINX_MAX_MATCHES, (int) $start + $per_page)); + $this->sphinx->SetLimits((int) $start, (int) $per_page, max(self::SPHINX_MAX_MATCHES, (int) $start + $per_page)); $result = $this->sphinx->Query($search_query_prefix . $this->sphinx_clean_search_string(str_replace('"', '"', $this->search_query)), $this->indexes); // Could be connection to localhost:9312 failed (errno=111, // msg=Connection refused) during rotate, retry if so - $retries = SPHINX_CONNECT_RETRIES; + $retries = self::SPHINX_CONNECT_RETRIES; while (!$result && (strpos($this->sphinx->GetLastError(), "errno=111,") !== false) && $retries--) { - usleep(SPHINX_CONNECT_WAIT_TIME); + usleep(self::SPHINX_CONNECT_WAIT_TIME); $result = $this->sphinx->Query($search_query_prefix . $this->sphinx_clean_search_string(str_replace('"', '"', $this->search_query)), $this->indexes); } } diff --git a/phpBB/phpbb/search/search_backend_factory.php b/phpBB/phpbb/search/search_backend_factory.php index ab103cae73..5ab6bce8af 100644 --- a/phpBB/phpbb/search/search_backend_factory.php +++ b/phpBB/phpbb/search/search_backend_factory.php @@ -48,7 +48,7 @@ class search_backend_factory * * @return search_backend_interface */ - public function get($class) + public function get($class): search_backend_interface { return $this->search_backends->get_by_class($class); } @@ -58,7 +58,7 @@ class search_backend_factory * * @return search_backend_interface */ - public function get_active() + public function get_active(): search_backend_interface { return $this->get($this->config['search_type']); }