1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-06-06 22:45:02 +02:00

[ticket/15540] Add types and phpdoc

PHPBB3-15540
This commit is contained in:
rubencm 2021-03-22 16:57:40 +01:00
parent 53dce77b49
commit 911a31d898
7 changed files with 115 additions and 101 deletions

View File

@ -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 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() public function update_data()
{ {
switch ($this->config['search_type']) switch ($this->config['search_type'])
{ {
case '\\phpbb\\search\\fulltext_mysql': case '\\phpbb\\search\\fulltext_mysql':
$new_search_type = 'phpbb\\search\\backend\\fulltext_mysql'; $new_search_type = fulltext_mysql::class;
break; break;
case '\\phpbb\\search\\fulltext_postgres': case '\\phpbb\\search\\fulltext_postgres':
$new_search_type = 'phpbb\\search\\backend\\fulltext_postgres'; $new_search_type = fulltext_postgres::class;
break; break;
case '\\phpbb\\search\\fulltext_sphinx': case '\\phpbb\\search\\fulltext_sphinx':
$new_search_type = 'phpbb\\search\\backend\\fulltext_sphinx'; $new_search_type = fulltext_sphinx::class;
break; break;
default: default:
$new_search_type = 'phpbb\\search\\backend\\fulltext_native'; $new_search_type = fulltext_native::class;
} }
return [ return [

View File

@ -13,22 +13,15 @@
namespace phpbb\search\backend; 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 * optional base class for search plugins providing simple caching based on ACM
* and functions to retrieve ignore_words and synonyms * and functions to retrieve ignore_words and synonyms
*/ */
abstract class base abstract class base implements search_backend_interface
{ {
var $ignore_words = array(); public const SEARCH_RESULT_NOT_IN_CACHE = 0;
var $match_synonym = array(); public const SEARCH_RESULT_IN_CACHE = 1;
var $replace_synonym = array(); public const SEARCH_RESULT_INCOMPLETE = 2;
/** /**
* Retrieves cached search results * Retrieves cached search results
@ -40,16 +33,16 @@ abstract class base
* @param int $per_page number of ids each page is supposed to contain * @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 * @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 * @return int self::SEARCH_RESULT_NOT_IN_CACHE or self::SEARCH_RESULT_IN_CACHE or self::SEARCH_RESULT_INCOMPLETE
*/ */
function obtain_ids($search_key, &$result_count, &$id_ary, &$start, $per_page, $sort_dir) public function obtain_ids(string $search_key, &$result_count, &$id_ary, &$start, $per_page, string $sort_dir): int
{ {
global $cache; global $cache;
if (!($stored_ids = $cache->get('_search_results_' . $search_key))) if (!($stored_ids = $cache->get('_search_results_' . $search_key)))
{ {
// no search results cached for this search_key // no search results cached for this search_key
return SEARCH_RESULT_NOT_IN_CACHE; return self::SEARCH_RESULT_NOT_IN_CACHE;
} }
else else
{ {
@ -79,7 +72,7 @@ abstract class base
// the user requested a page past the last index // the user requested a page past the last index
if ($start < 0) if ($start < 0)
{ {
return SEARCH_RESULT_NOT_IN_CACHE; return self::SEARCH_RESULT_NOT_IN_CACHE;
} }
} }
@ -103,9 +96,9 @@ abstract class base
if (!$complete) if (!$complete)
{ {
return SEARCH_RESULT_INCOMPLETE; return self::SEARCH_RESULT_INCOMPLETE;
} }
return SEARCH_RESULT_IN_CACHE; return self::SEARCH_RESULT_IN_CACHE;
} }
} }
@ -123,7 +116,7 @@ abstract class base
* *
* @return null * @return null
*/ */
function save_ids($search_key, $keywords, $author_ary, $result_count, &$id_ary, $start, $sort_dir) 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; global $cache, $config, $db, $user;
@ -224,15 +217,16 @@ abstract class base
$db->sql_query($sql); $db->sql_query($sql);
} }
unset($store); unset($store, $store_ids, $id_range);
unset($store_ids);
unset($id_range);
} }
/** /**
* Removes old entries from the search results table and removes searches with keywords that contain a word in $words. * 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
*/ */
function destroy_cache($words, $authors = false) public function destroy_cache($words, $authors = false): void
{ {
global $db, $cache, $config; global $db, $cache, $config;
@ -381,7 +375,7 @@ abstract class base
$posters[] = $row['poster_id']; $posters[] = $row['poster_id'];
$forum_ids[] = $row['forum_id']; $forum_ids[] = $row['forum_id'];
} }
$db->sql_freeresult($result); $result->sql_freeresult($result);
$row_count += count($ids); $row_count += count($ids);
if (count($ids)) if (count($ids))

View File

@ -13,6 +13,11 @@
namespace phpbb\search\backend; 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 * Fulltext search for MySQL
*/ */
@ -32,25 +37,25 @@ class fulltext_mysql extends base implements search_backend_interface
/** /**
* Config object * Config object
* @var \phpbb\config\config * @var config
*/ */
protected $config; protected $config;
/** /**
* Database connection * Database connection
* @var \phpbb\db\driver\driver_interface * @var driver_interface
*/ */
protected $db; protected $db;
/** /**
* phpBB event dispatcher object * phpBB event dispatcher object
* @var \phpbb\event\dispatcher_interface * @var dispatcher_interface
*/ */
protected $phpbb_dispatcher; protected $phpbb_dispatcher;
/** /**
* User object * User object
* @var \phpbb\user * @var user
*/ */
protected $user; protected $user;
@ -78,14 +83,14 @@ class fulltext_mysql extends base implements search_backend_interface
* Constructor * Constructor
* Creates a new \phpbb\search\backend\fulltext_mysql, which is used as a search backend * Creates a new \phpbb\search\backend\fulltext_mysql, which is used as a search backend
* *
* @param \phpbb\config\config $config Config object * @param config $config Config object
* @param \phpbb\db\driver\driver_interface $db Database object * @param driver_interface $db Database object
* @param \phpbb\event\dispatcher_interface $phpbb_dispatcher Event dispatcher object * @param dispatcher_interface $phpbb_dispatcher Event dispatcher object
* @param \phpbb\user $user User object * @param user $user User object
* @param string $phpbb_root_path Relative path to phpBB root * @param string $phpbb_root_path Relative path to phpBB root
* @param string $phpEx PHP file extension * @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->config = $config;
$this->db = $db; $this->db = $db;
@ -106,7 +111,7 @@ class fulltext_mysql extends base implements search_backend_interface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function get_name() public function get_name(): string
{ {
return 'MySQL Fulltext'; return 'MySQL Fulltext';
} }
@ -114,7 +119,7 @@ class fulltext_mysql extends base implements search_backend_interface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function get_search_query() public function get_search_query(): string
{ {
return $this->search_query; return $this->search_query;
} }
@ -122,7 +127,7 @@ class fulltext_mysql extends base implements search_backend_interface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function get_common_words() public function get_common_words(): array
{ {
return $this->common_words; return $this->common_words;
} }
@ -130,7 +135,7 @@ class fulltext_mysql extends base implements search_backend_interface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function get_word_length() public function get_word_length(): array
{ {
return $this->word_length; 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 * We also require https://bugs.mysql.com/bug.php?id=67004 to be
* fixed for proper overall operation. Hence we require 5.6.8. * fixed for proper overall operation. Hence we require 5.6.8.
*/ */
|| $engine === 'InnoDB' || ($engine === 'InnoDB'
&& phpbb_version_compare($this->db->sql_server_info(true), '5.6.8', '>='); && phpbb_version_compare($this->db->sql_server_info(true), '5.6.8', '>='));
if (!$fulltext_supported) if (!$fulltext_supported)
{ {
@ -204,7 +209,7 @@ class fulltext_mysql extends base implements search_backend_interface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function split_keywords(&$keywords, $terms) public function split_keywords(&$keywords, $terms): bool
{ {
if ($terms == 'all') if ($terms == 'all')
{ {
@ -342,8 +347,10 @@ class fulltext_mysql extends base implements search_backend_interface
/** /**
* Turns text into an array of words * Turns text into an array of words
* @param string $text contains post text/subject * @param string $text contains post text/subject
*
* @return array
*/ */
public function split_message($text) public function split_message($text): array
{ {
// Split words // Split words
$text = preg_replace('#([^\p{L}\p{N}\'*])#u', '$1$1', str_replace('\'\'', '\' \'', trim($text))); $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 // try reading the results from cache
$result_count = 0; $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; return $result_count;
} }
@ -679,7 +686,7 @@ class fulltext_mysql extends base implements search_backend_interface
// try reading the results from cache // try reading the results from cache
$result_count = 0; $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; return $result_count;
} }

View File

@ -120,7 +120,7 @@ class fulltext_native extends base implements search_backend_interface
* @param string $phpbb_root_path phpBB root path * @param string $phpbb_root_path phpBB root path
* @param string $phpEx PHP file extension * @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->config = $config;
$this->db = $db; $this->db = $db;
@ -589,7 +589,7 @@ class fulltext_native extends base implements search_backend_interface
// try reading the results from cache // try reading the results from cache
$total_results = 0; $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; return $total_results;
} }
@ -1053,7 +1053,7 @@ class fulltext_native extends base implements search_backend_interface
// try reading the results from cache // try reading the results from cache
$total_results = 0; $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; return $total_results;
} }

View File

@ -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 $phpbb_root_path Relative path to phpBB root
* @param string $phpEx PHP file extension * @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->config = $config;
$this->db = $db; $this->db = $db;
@ -364,7 +364,7 @@ class fulltext_postgres extends base implements search_backend_interface
// try reading the results from cache // try reading the results from cache
$result_count = 0; $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; return $result_count;
} }
@ -624,7 +624,7 @@ class fulltext_postgres extends base implements search_backend_interface
// try reading the results from cache // try reading the results from cache
$result_count = 0; $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; return $result_count;
} }

View File

@ -15,18 +15,19 @@ namespace phpbb\search\backend;
use phpbb\auth\auth; use phpbb\auth\auth;
use phpbb\config\config; use phpbb\config\config;
use phpbb\db\driver\driver_interface;
use phpbb\event\dispatcher_interface; use phpbb\event\dispatcher_interface;
use phpbb\user; 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 * Fulltext search based on the sphinx search daemon
*/ */
class fulltext_sphinx implements search_backend_interface 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 * Associative array holding index stats
* @var array * @var array
@ -131,14 +132,14 @@ class fulltext_sphinx implements search_backend_interface
* *
* @param auth $auth Auth object * @param auth $auth Auth object
* @param config $config Config object * @param config $config Config object
* @param $db * @param driver_interface $db Database object
* @param dispatcher_interface $phpbb_dispatcher Event dispatcher object * @param dispatcher_interface $phpbb_dispatcher Event dispatcher object
* @param user $user User object * @param user $user User object
* @param string $phpbb_root_path Relative path to phpBB root * @param string $phpbb_root_path Relative path to phpBB root
* @param string $phpEx PHP file extension * @param string $phpEx PHP file extension
* @throws \Exception * @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->auth = $auth;
$this->config = $config; $this->config = $config;
@ -684,15 +685,15 @@ class fulltext_sphinx implements search_backend_interface
$this->sphinx->SetFilter('deleted', array(0)); $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('&quot;', '"', $this->search_query)), $this->indexes); $result = $this->sphinx->Query($search_query_prefix . $this->sphinx_clean_search_string(str_replace('&quot;', '"', $this->search_query)), $this->indexes);
// Could be connection to localhost:9312 failed (errno=111, // Could be connection to localhost:9312 failed (errno=111,
// msg=Connection refused) during rotate, retry if so // 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--) 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('&quot;', '"', $this->search_query)), $this->indexes); $result = $this->sphinx->Query($search_query_prefix . $this->sphinx_clean_search_string(str_replace('&quot;', '"', $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; $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('&quot;', '"', $this->search_query)), $this->indexes); $result = $this->sphinx->Query($search_query_prefix . $this->sphinx_clean_search_string(str_replace('&quot;', '"', $this->search_query)), $this->indexes);
// Could be connection to localhost:9312 failed (errno=111, // Could be connection to localhost:9312 failed (errno=111,
// msg=Connection refused) during rotate, retry if so // 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--) 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('&quot;', '"', $this->search_query)), $this->indexes); $result = $this->sphinx->Query($search_query_prefix . $this->sphinx_clean_search_string(str_replace('&quot;', '"', $this->search_query)), $this->indexes);
} }
} }

View File

@ -48,7 +48,7 @@ class search_backend_factory
* *
* @return search_backend_interface * @return search_backend_interface
*/ */
public function get($class) public function get($class): search_backend_interface
{ {
return $this->search_backends->get_by_class($class); return $this->search_backends->get_by_class($class);
} }
@ -58,7 +58,7 @@ class search_backend_factory
* *
* @return search_backend_interface * @return search_backend_interface
*/ */
public function get_active() public function get_active(): search_backend_interface
{ {
return $this->get($this->config['search_type']); return $this->get($this->config['search_type']);
} }