1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-08-13 04:04:12 +02:00

Merge pull request #5440 from rubencm/ticket/15540

[ticket/15540] Refactor search backend classes to Symfony services
This commit is contained in:
Marc Alexander
2021-03-24 09:58:52 +01:00
committed by GitHub
44 changed files with 2065 additions and 2125 deletions

View File

@@ -13,6 +13,8 @@
namespace phpbb\cache;
use phpbb\cache\driver\driver_interface;
use phpbb\config\config;
use phpbb\json\sanitizer as json_sanitizer;
/**
@@ -23,14 +25,14 @@ class service
/**
* Cache driver.
*
* @var \phpbb\cache\driver\driver_interface
* @var driver_interface
*/
protected $driver;
/**
* The config.
*
* @var \phpbb\config\config
* @var config
*/
protected $config;
@@ -58,13 +60,13 @@ class service
/**
* Creates a cache service around a cache driver
*
* @param \phpbb\cache\driver\driver_interface $driver The cache driver
* @param \phpbb\config\config $config The config
* @param driver_interface $driver The cache driver
* @param config $config The config
* @param \phpbb\db\driver\driver_interface $db Database connection
* @param string $phpbb_root_path Root path
* @param string $php_ext PHP file extension
*/
public function __construct(\phpbb\cache\driver\driver_interface $driver, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, $phpbb_root_path, $php_ext)
public function __construct(driver_interface $driver, config $config, \phpbb\db\driver\driver_interface $db, $phpbb_root_path, $php_ext)
{
$this->set_driver($driver);
$this->config = $config;
@@ -76,7 +78,7 @@ class service
/**
* Returns the cache driver used by this cache service.
*
* @return \phpbb\cache\driver\driver_interface The cache driver
* @return driver_interface The cache driver
*/
public function get_driver()
{
@@ -86,9 +88,9 @@ class service
/**
* Replaces the cache driver used by this cache service.
*
* @param \phpbb\cache\driver\driver_interface $driver The cache driver
* @param driver_interface $driver The cache driver
*/
public function set_driver(\phpbb\cache\driver\driver_interface $driver)
public function set_driver(driver_interface $driver)
{
$this->driver = $driver;
}

View File

@@ -0,0 +1,51 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @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\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 = fulltext_mysql::class;
break;
case '\\phpbb\\search\\fulltext_postgres':
$new_search_type = fulltext_postgres::class;
break;
case '\\phpbb\\search\\fulltext_sphinx':
$new_search_type = fulltext_sphinx::class;
break;
default:
$new_search_type = fulltext_native::class;
}
return [
['config.update', ['search_type', $new_search_type]],
];
}
}

View File

@@ -23,7 +23,7 @@ use phpbb\install\helper\container_factory;
use phpbb\install\helper\database;
use phpbb\install\helper\iohandler\iohandler_interface;
use phpbb\install\sequential_task;
use phpbb\search\fulltext_native;
use phpbb\search\backend\fulltext_native;
use phpbb\user;
class create_search_index extends database_task
@@ -98,12 +98,12 @@ class create_search_index extends database_task
/**
* Constructor
*
* @param config $config Installer config.
* @param database $db_helper Database helper.
* @param container_factory $container Installer's DI container
* @param iohandler_interface $iohandler IO manager.
* @param string $phpbb_root_path phpBB root path
* @param string $php_ext PHP file extension
* @param config $config Installer config.
* @param database $db_helper Database helper.
* @param container_factory $container Installer's DI container
* @param iohandler_interface $iohandler IO manager.
* @param string $phpbb_root_path phpBB root path
* @param string $php_ext PHP file extension
*/
public function __construct(
config $config,
@@ -127,16 +127,14 @@ class create_search_index extends database_task
$this->posts_table = $container->get_parameter('tables.posts');
$this->error = false;
$this->search_indexer = new fulltext_native(
$this->error,
$this->phpbb_root_path,
$this->php_ext,
$this->auth,
$this->config,
$this->db,
$this->phpbb_dispatcher,
$container->get('language'),
$this->user,
$this->phpbb_dispatcher
$this->phpbb_root_path,
$this->php_ext
);
parent::__construct($this->conn, $iohandler, true);
@@ -171,11 +169,11 @@ class create_search_index extends database_task
{
$this->search_indexer->index(
'post',
$value['post_id'],
(int) $value['post_id'],
$value['post_text'],
$value['post_subject'],
$value['poster_id'],
$value['forum_id']
(int) $value['poster_id'],
(int) $value['forum_id']
);
}

View File

@@ -0,0 +1,483 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @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;
use phpbb\cache\service;
use phpbb\config\config;
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
*/
abstract class base implements search_backend_interface
{
public const SEARCH_RESULT_NOT_IN_CACHE = 0;
public const SEARCH_RESULT_IN_CACHE = 1;
public const SEARCH_RESULT_INCOMPLETE = 2;
// Batch size for create_index and delete_index
private const BATCH_SIZE = 100;
/**
* @var service
*/
protected $cache;
/**
* @var config
*/
protected $config;
/**
* @var driver_interface
*/
protected $db;
/**
* @var user
*/
protected $user;
/**
* Constructor.
*
* @param service $cache
* @param config $config
* @param driver_interface $db
* @param user $user
*/
public function __construct(service $cache, config $config, driver_interface $db, user $user)
{
$this->cache = $cache;
$this->config = $config;
$this->db = $db;
$this->user = $user;
}
/**
* 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
*/
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)))
{
// no search results cached for this search_key
return self::SEARCH_RESULT_NOT_IN_CACHE;
}
else
{
$result_count = $stored_ids[-1];
$reverse_ids = $stored_ids[-2] != $sort_dir;
$complete = true;
// Change start parameter in case out of bounds
if ($result_count)
{
if ($start < 0)
{
$start = 0;
}
else if ($start >= $result_count)
{
$start = floor(($result_count - 1) / $per_page) * $per_page;
}
}
// change the start to the actual end of the current request if the sort direction differs
// from the direction in the cache and reverse the ids later
if ($reverse_ids)
{
$start = $result_count - $start - $per_page;
// the user requested a page past the last index
if ($start < 0)
{
return self::SEARCH_RESULT_NOT_IN_CACHE;
}
}
for ($i = $start, $n = $start + $per_page; ($i < $n) && ($i < $result_count); $i++)
{
if (!isset($stored_ids[$i]))
{
$complete = false;
}
else
{
$id_ary[] = $stored_ids[$i];
}
}
unset($stored_ids);
if ($reverse_ids)
{
$id_ary = array_reverse($id_ary);
}
if (!$complete)
{
return self::SEARCH_RESULT_INCOMPLETE;
}
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 void
*/
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;
$length = min(count($id_ary), $this->config['search_block_size']);
// nothing to cache so exit
if (!$length)
{
return;
}
$store_ids = array_slice($id_ary, 0, $length);
// create a new resultset if there is none for this search_key yet
// or add the ids to the existing resultset
if (!($store = $this->cache->get('_search_results_' . $search_key)))
{
// add the current keywords to the recent searches in the cache which are listed on the search page
if (!empty($keywords) || count($author_ary))
{
$sql = 'SELECT search_time
FROM ' . SEARCH_RESULTS_TABLE . '
WHERE search_key = \'' . $this->db->sql_escape($search_key) . '\'';
$result = $this->db->sql_query($sql);
if (!$this->db->sql_fetchrow($result))
{
$sql_ary = array(
'search_key' => $search_key,
'search_time' => time(),
'search_keywords' => $keywords,
'search_authors' => ' ' . implode(' ', $author_ary) . ' '
);
$sql = 'INSERT INTO ' . SEARCH_RESULTS_TABLE . ' ' . $this->db->sql_build_array('INSERT', $sql_ary);
$this->db->sql_query($sql);
}
$this->db->sql_freeresult($result);
}
$sql = 'UPDATE ' . USERS_TABLE . '
SET user_last_search = ' . time() . '
WHERE user_id = ' . $user->data['user_id'];
$this->db->sql_query($sql);
$store = array(-1 => $result_count, -2 => $sort_dir);
$id_range = range($start, $start + $length - 1);
}
else
{
// we use one set of results for both sort directions so we have to calculate the indizes
// for the reversed array and we also have to reverse the ids themselves
if ($store[-2] != $sort_dir)
{
$store_ids = array_reverse($store_ids);
$id_range = range($store[-1] - $start - $length, $store[-1] - $start - 1);
}
else
{
$id_range = range($start, $start + $length - 1);
}
}
$store_ids = array_combine($id_range, $store_ids);
// append the ids
if (is_array($store_ids))
{
$store += $store_ids;
// if the cache is too big
if (count($store) - 2 > 20 * $this->config['search_block_size'])
{
// remove everything in front of two blocks in front of the current start index
for ($i = 0, $n = $id_range[0] - 2 * $this->config['search_block_size']; $i < $n; $i++)
{
if (isset($store[$i]))
{
unset($store[$i]);
}
}
// remove everything after two blocks after the current stop index
end($id_range);
for ($i = $store[-1] - 1, $n = current($id_range) + 2 * $this->config['search_block_size']; $i > $n; $i--)
{
if (isset($store[$i]))
{
unset($store[$i]);
}
}
}
$this->cache->put('_search_results_' . $search_key, $store, $this->config['search_store_results']);
$sql = 'UPDATE ' . SEARCH_RESULTS_TABLE . '
SET search_time = ' . time() . '
WHERE search_key = \'' . $this->db->sql_escape($search_key) . '\'';
$this->db->sql_query($sql);
}
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.
*
* @param array $words
* @param array|bool $authors
*/
protected function destroy_cache(array $words, $authors = false): void
{
// clear all searches that searched for the specified words
if (count($words))
{
$sql_where = '';
foreach ($words as $word)
{
$sql_where .= " OR search_keywords " . $this->db->sql_like_expression($this->db->get_any_char() . $word . $this->db->get_any_char());
}
$sql = 'SELECT search_key
FROM ' . SEARCH_RESULTS_TABLE . "
WHERE search_keywords LIKE '%*%' $sql_where";
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
$this->cache->destroy('_search_results_' . $row['search_key']);
}
$this->db->sql_freeresult($result);
}
// clear all searches that searched for the specified authors
if (is_array($authors) && count($authors))
{
$sql_where = '';
foreach ($authors as $author)
{
$sql_where .= (($sql_where) ? ' OR ' : '') . 'search_authors ' . $this->db->sql_like_expression($this->db->get_any_char() . ' ' . (int) $author . ' ' . $this->db->get_any_char());
}
$sql = 'SELECT search_key
FROM ' . SEARCH_RESULTS_TABLE . "
WHERE $sql_where";
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
$this->cache->destroy('_search_results_' . $row['search_key']);
}
$this->db->sql_freeresult($result);
}
$sql = 'DELETE
FROM ' . SEARCH_RESULTS_TABLE . '
WHERE search_time < ' . (time() - (int) $this->config['search_store_results']);
$this->db->sql_query($sql);
}
/**
* {@inheritdoc}
*/
public function create_index(int &$post_counter = 0): ?array
{
$max_post_id = $this->get_max_post_id();
$forums_indexing_enabled = $this->forum_ids_with_indexing_enabled();
$starttime = microtime(true);
$row_count = 0;
while (still_on_time() && $post_counter <= $max_post_id)
{
$rows = $this->get_posts_between($post_counter + 1, $post_counter + self::BATCH_SIZE);
if ($this->db->sql_buffer_nested_transactions())
{
$rows = iterator_to_array($rows);
}
foreach ($rows as $row)
{
// Indexing enabled for this forum
if (in_array($row['forum_id'], $forums_indexing_enabled, true))
{
$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 += self::BATCH_SIZE;
}
// 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'];
$this->config['num_posts'] = min($this->config['num_posts'], $post_counter);
$this->tidy();
$this->config['num_posts'] = $num_posts;
if ($post_counter <= $max_post_id)
{
$totaltime = microtime(true) - $starttime;
$rows_per_second = $row_count / $totaltime;
return [
'row_count' => $row_count,
'post_counter' => $post_counter,
'max_post_id' => $max_post_id,
'rows_per_second' => $rows_per_second,
];
}
return null;
}
/**
* {@inheritdoc}
*/
public function delete_index(int &$post_counter = null): ?array
{
$max_post_id = $this->get_max_post_id();
$starttime = microtime(true);
$row_count = 0;
while (still_on_time() && $post_counter <= $max_post_id)
{
$rows = $this->get_posts_between($post_counter + 1, $post_counter + self::BATCH_SIZE);
$ids = $posters = $forum_ids = array();
foreach ($rows as $row)
{
$ids[] = $row['post_id'];
$posters[] = $row['poster_id'];
$forum_ids[] = $row['forum_id'];
}
$row_count += count($ids);
if (count($ids))
{
$this->index_remove($ids, $posters, $forum_ids);
}
$post_counter += self::BATCH_SIZE;
}
if ($post_counter <= $max_post_id)
{
$totaltime = microtime(true) - $starttime;
$rows_per_second = $row_count / $totaltime;
return [
'row_count' => $row_count,
'post_counter' => $post_counter,
'max_post_id' => $max_post_id,
'rows_per_second' => $rows_per_second,
];
}
return null;
}
/**
* Return the ids of the forums that have indexing enabled
*
* @return array
*/
protected function forum_ids_with_indexing_enabled(): array
{
$forums = [];
$sql = 'SELECT forum_id, enable_indexing
FROM ' . FORUMS_TABLE;
$result = $this->db->sql_query($sql, 3600);
while ($row = $this->db->sql_fetchrow($result))
{
if ((bool) $row['enable_indexing'])
{
$forums[] = $row['forum_id'];
}
}
$this->db->sql_freeresult($result);
return $forums;
}
/**
* Get posts between 2 ids
*
* @param int $initial_id
* @param int $final_id
* @return \Generator
*/
protected function get_posts_between(int $initial_id, int $final_id): \Generator
{
$sql = 'SELECT post_id, post_subject, post_text, poster_id, forum_id
FROM ' . POSTS_TABLE . '
WHERE post_id >= ' . $initial_id . '
AND post_id <= ' . $final_id;
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
yield $row;
}
$this->db->sql_freeresult($result);
}
/**
* Get post with higher id
*/
protected 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;
}
}

View File

@@ -11,12 +11,19 @@
*
*/
namespace phpbb\search;
namespace phpbb\search\backend;
use phpbb\config\config;
use phpbb\db\driver\driver_interface;
use phpbb\event\dispatcher_interface;
use phpbb\language\language;
use phpbb\user;
use RuntimeException;
/**
* Fulltext search for MySQL
*/
class fulltext_mysql extends \phpbb\search\base
class fulltext_mysql extends base implements search_backend_interface
{
/**
* Associative array holding index stats
@@ -30,29 +37,16 @@ class fulltext_mysql extends \phpbb\search\base
*/
protected $split_words = array();
/**
* Config object
* @var \phpbb\config\config
*/
protected $config;
/**
* Database connection
* @var \phpbb\db\driver\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 language
*/
protected $user;
protected $language;
/**
* Associative array stores the min and max word length to be searched
@@ -76,23 +70,23 @@ class fulltext_mysql extends \phpbb\search\base
/**
* Constructor
* Creates a new \phpbb\search\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 string|bool $error Any error that occurs is passed on through this reference variable otherwise false
* @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 \phpbb\auth\auth $auth Auth object
* @param \phpbb\config\config $config Config object
* @param \phpbb\db\driver\driver_interface $db Database object
* @param \phpbb\user $user User object
* @param \phpbb\event\dispatcher_interface $phpbb_dispatcher Event dispatcher object
*/
public function __construct(&$error, $phpbb_root_path, $phpEx, $auth, $config, $db, $user, $phpbb_dispatcher)
public function __construct(config $config, driver_interface $db, dispatcher_interface $phpbb_dispatcher, language $language, user $user, string $phpbb_root_path, string $phpEx)
{
$this->config = $config;
$this->db = $db;
global $cache;
parent::__construct($cache, $config, $db, $user);
$this->phpbb_dispatcher = $phpbb_dispatcher;
$this->user = $user;
$this->language = $language;
$this->word_length = array('min' => $this->config['fulltext_mysql_min_word_len'], 'max' => $this->config['fulltext_mysql_max_word_len']);
@@ -103,75 +97,45 @@ class fulltext_mysql extends \phpbb\search\base
{
include($phpbb_root_path . 'includes/utf/utf_tools.' . $phpEx);
}
$error = false;
}
/**
* Returns the name of this search backend to be displayed to administrators
*
* @return string Name
*/
public function get_name()
* {@inheritdoc}
*/
public function get_name(): string
{
return 'MySQL Fulltext';
}
/**
* Returns the search_query
*
* @return string search query
* {@inheritdoc}
*/
public function get_search_query()
{
return $this->search_query;
}
/**
* Returns the common_words array
*
* @return array common words that are ignored by search backend
*/
public function get_common_words()
{
return $this->common_words;
}
/**
* Returns the word_length array
*
* @return array min and max word length for searching
*/
public function get_word_length()
{
return $this->word_length;
}
/**
* Checks for correct MySQL version and stores min/max word length in the config
*
* @return string|bool Language key of the error/incompatibility occurred
*/
public function init()
public function is_available(): bool
{
// Check if we are using mysql
if ($this->db->get_sql_layer() != 'mysqli')
{
return $this->user->lang['FULLTEXT_MYSQL_INCOMPATIBLE_DATABASE'];
return false;
}
return true;
}
/**
* {@inheritdoc}
*/
public function init()
{
if (!$this->is_available())
{
return $this->language->lang('FULLTEXT_MYSQL_INCOMPATIBLE_DATABASE');
}
$result = $this->db->sql_query('SHOW TABLE STATUS LIKE \'' . POSTS_TABLE . '\'');
$info = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
$engine = '';
if (isset($info['Engine']))
{
$engine = $info['Engine'];
}
else if (isset($info['Type']))
{
$engine = $info['Type'];
}
$engine = $info['Engine'] ?? $info['Type'] ?? '';
$fulltext_supported = $engine === 'Aria' || $engine === 'MyISAM'
/**
@@ -180,12 +144,12 @@ class fulltext_mysql extends \phpbb\search\base
* 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)
{
return $this->user->lang['FULLTEXT_MYSQL_NOT_SUPPORTED'];
return $this->language->lang('FULLTEXT_MYSQL_NOT_SUPPORTED');
}
$sql = 'SHOW VARIABLES
@@ -214,14 +178,33 @@ class fulltext_mysql extends \phpbb\search\base
}
/**
* Splits keywords entered by a user into an array of words stored in $this->split_words
* Stores the tidied search query in $this->search_query
*
* @param string &$keywords Contains the keyword as entered by the user
* @param string $terms is either 'all' or 'any'
* @return bool false if no valid keywords were found and otherwise true
*/
public function split_keywords(&$keywords, $terms)
* {@inheritdoc}
*/
public function get_search_query(): string
{
return $this->search_query;
}
/**
* {@inheritdoc}
*/
public function get_common_words(): array
{
return $this->common_words;
}
/**
* {@inheritdoc}
*/
public function get_word_length()
{
return $this->word_length;
}
/**
* {@inheritdoc}
*/
public function split_keywords(string &$keywords, string $terms): bool
{
if ($terms == 'all')
{
@@ -243,7 +226,7 @@ class fulltext_mysql extends \phpbb\search\base
// We limit the number of allowed keywords to minimize load on the database
if ($this->config['max_num_search_keywords'] && count($this->split_words) > $this->config['max_num_search_keywords'])
{
trigger_error($this->user->lang('MAX_NUM_SEARCH_KEYWORDS_REFINE', (int) $this->config['max_num_search_keywords'], count($this->split_words)));
trigger_error($this->language->lang('MAX_NUM_SEARCH_KEYWORDS_REFINE', (int) $this->config['max_num_search_keywords'], count($this->split_words)));
}
// to allow phrase search, we need to concatenate quoted words
@@ -357,52 +340,9 @@ class fulltext_mysql extends \phpbb\search\base
}
/**
* Turns text into an array of words
* @param string $text contains post text/subject
*/
public function split_message($text)
{
// Split words
$text = preg_replace('#([^\p{L}\p{N}\'*])#u', '$1$1', str_replace('\'\'', '\' \'', trim($text)));
$matches = array();
preg_match_all('#(?:[^\p{L}\p{N}*]|^)([+\-|]?(?:[\p{L}\p{N}*]+\'?)*[\p{L}\p{N}*])(?:[^\p{L}\p{N}*]|$)#u', $text, $matches);
$text = $matches[1];
// remove too short or too long words
$text = array_values($text);
for ($i = 0, $n = count($text); $i < $n; $i++)
{
$text[$i] = trim($text[$i]);
if (utf8_strlen($text[$i]) < $this->config['fulltext_mysql_min_word_len'] || utf8_strlen($text[$i]) > $this->config['fulltext_mysql_max_word_len'])
{
unset($text[$i]);
}
}
return array_values($text);
}
/**
* Performs a search on keywords depending on display specific params. You have to run split_keywords() first
*
* @param string $type contains either posts or topics depending on what should be searched for
* @param string $fields contains either titleonly (topic titles should be searched), msgonly (only message bodies should be searched), firstpost (only subject and body of the first post should be searched) or all (all post bodies and subjects should be searched)
* @param string $terms is either 'all' (use query as entered, words without prefix should default to "have to be in field") or 'any' (ignore search query parts and just return all posts that contain any of the specified words)
* @param array $sort_by_sql contains SQL code for the ORDER BY part of a query
* @param string $sort_key is the key of $sort_by_sql for the selected sorting
* @param string $sort_dir is either a or d representing ASC and DESC
* @param string $sort_days specifies the maximum amount of days a post may be old
* @param array $ex_fid_ary specifies an array of forum ids which should not be searched
* @param string $post_visibility specifies which types of posts the user can view in which forums
* @param int $topic_id is set to 0 or a topic id, if it is not 0 then only posts in this topic should be searched
* @param array $author_ary an array of author ids if the author should be ignored during the search the array is empty
* @param string $author_name specifies the author match, when ANONYMOUS is also a search-match
* @param array &$id_ary passed by reference, to be filled with ids for the page specified by $start and $per_page, should be ordered
* @param int $start indicates the first index of the page
* @param int $per_page number of ids each page is supposed to contain
* @return boolean|int total number of results
*/
public function keyword_search($type, $fields, $terms, $sort_by_sql, $sort_key, $sort_dir, $sort_days, $ex_fid_ary, $post_visibility, $topic_id, $author_ary, $author_name, &$id_ary, &$start, $per_page)
* {@inheritdoc}
*/
public function keyword_search(string $type, string $fields, string $terms, array $sort_by_sql, string $sort_key, string $sort_dir, string $sort_days, array $ex_fid_ary, string $post_visibility, int $topic_id, array $author_ary, string $author_name, array &$id_ary, int &$start, int $per_page)
{
// No keywords? No posts
if (!$this->search_query)
@@ -463,7 +403,7 @@ class fulltext_mysql extends \phpbb\search\base
// 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;
}
@@ -648,25 +588,9 @@ class fulltext_mysql extends \phpbb\search\base
}
/**
* Performs a search on an author's posts without caring about message contents. Depends on display specific params
*
* @param string $type contains either posts or topics depending on what should be searched for
* @param boolean $firstpost_only if true, only topic starting posts will be considered
* @param array $sort_by_sql contains SQL code for the ORDER BY part of a query
* @param string $sort_key is the key of $sort_by_sql for the selected sorting
* @param string $sort_dir is either a or d representing ASC and DESC
* @param string $sort_days specifies the maximum amount of days a post may be old
* @param array $ex_fid_ary specifies an array of forum ids which should not be searched
* @param string $post_visibility specifies which types of posts the user can view in which forums
* @param int $topic_id is set to 0 or a topic id, if it is not 0 then only posts in this topic should be searched
* @param array $author_ary an array of author ids
* @param string $author_name specifies the author match, when ANONYMOUS is also a search-match
* @param array &$id_ary passed by reference, to be filled with ids for the page specified by $start and $per_page, should be ordered
* @param int $start indicates the first index of the page
* @param int $per_page number of ids each page is supposed to contain
* @return boolean|int total number of results
*/
public function author_search($type, $firstpost_only, $sort_by_sql, $sort_key, $sort_dir, $sort_days, $ex_fid_ary, $post_visibility, $topic_id, $author_ary, $author_name, &$id_ary, &$start, $per_page)
* {@inheritdoc}
*/
public function author_search(string $type, bool $firstpost_only, array $sort_by_sql, string $sort_key, string $sort_dir, string $sort_days, array $ex_fid_ary, string $post_visibility, int $topic_id, array $author_ary, string $author_name, array &$id_ary, int &$start, int $per_page)
{
// No author? No posts
if (!count($author_ary))
@@ -729,7 +653,7 @@ class fulltext_mysql extends \phpbb\search\base
// 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;
}
@@ -910,16 +834,17 @@ class fulltext_mysql extends \phpbb\search\base
}
/**
* Destroys cached search results, that contained one of the new words in a post so the results won't be outdated
*
* @param string $mode contains the post mode: edit, post, reply, quote ...
* @param int $post_id contains the post id of the post to index
* @param string $message contains the post text of the post
* @param string $subject contains the subject of the post to index
* @param int $poster_id contains the user id of the poster
* @param int $forum_id contains the forum id of parent forum of the post
*/
public function index($mode, $post_id, &$message, &$subject, $poster_id, $forum_id)
* {@inheritdoc}
*/
public function supports_phrase_search(): bool
{
return false;
}
/**
* {@inheritdoc}
*/
public function index(string $mode, int $post_id, string &$message, string &$subject, int $poster_id, int $forum_id)
{
// Split old and new post/subject to obtain array of words
$split_text = $this->split_message($message);
@@ -955,8 +880,7 @@ class fulltext_mysql extends \phpbb\search\base
);
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));
@@ -965,35 +889,33 @@ class fulltext_mysql extends \phpbb\search\base
}
/**
* Destroy cached results, that might be outdated after deleting a post
*/
public function index_remove($post_ids, $author_ids, $forum_ids)
* {@inheritdoc}
*/
public function index_remove(array $post_ids, array $author_ids, array $forum_ids): void
{
$this->destroy_cache(array(), array_unique($author_ids));
$this->destroy_cache([], array_unique($author_ids));
}
/**
* Destroy old cache entries
*/
public function tidy()
* {@inheritdoc}
*/
public function tidy(): void
{
// destroy too old cached search results
$this->destroy_cache(array());
$this->destroy_cache([]);
$this->config->set('search_last_gc', time(), false);
}
/**
* Create fulltext index
*
* @return string|bool error string is returned incase of errors otherwise false
*/
public function create_index($acp_module, $u_action)
* {@inheritdoc}
*/
public function create_index(int &$post_counter = 0): ?array
{
// Make sure we can actually use MySQL with fulltext indexes
if ($error = $this->init())
{
return $error;
throw new RuntimeException($error);
}
if (empty($this->stats))
@@ -1054,20 +976,18 @@ class fulltext_mysql extends \phpbb\search\base
$this->db->sql_query('TRUNCATE TABLE ' . SEARCH_RESULTS_TABLE);
return false;
return null;
}
/**
* Drop fulltext index
*
* @return string|bool error string is returned incase of errors otherwise false
*/
public function delete_index($acp_module, $u_action)
* {@inheritdoc}
*/
public function delete_index(int &$post_counter = null): ?array
{
// Make sure we can actually use MySQL with fulltext indexes
if ($error = $this->init())
{
return $error;
throw new RuntimeException($error);
}
if (empty($this->stats))
@@ -1122,13 +1042,13 @@ class fulltext_mysql extends \phpbb\search\base
$this->db->sql_query('TRUNCATE TABLE ' . SEARCH_RESULTS_TABLE);
return false;
return null;
}
/**
* Returns true if both FULLTEXT indexes exist
* {@inheritdoc}
*/
public function index_created()
public function index_created(): bool
{
if (empty($this->stats))
{
@@ -1139,8 +1059,8 @@ class fulltext_mysql extends \phpbb\search\base
}
/**
* Returns an associative array containing information about the indexes
*/
* {@inheritdoc}
*/
public function index_stats()
{
if (empty($this->stats))
@@ -1149,7 +1069,7 @@ class fulltext_mysql extends \phpbb\search\base
}
return array(
$this->user->lang['FULLTEXT_MYSQL_TOTAL_POSTS'] => ($this->index_created()) ? $this->stats['total_posts'] : 0,
$this->language->lang('FULLTEXT_MYSQL_TOTAL_POSTS') => ($this->index_created()) ? $this->stats['total_posts'] : 0,
);
}
@@ -1195,19 +1115,45 @@ class fulltext_mysql extends \phpbb\search\base
}
/**
* Display a note, that UTF-8 support is not available with certain versions of PHP
*
* @return associative array containing template and config variables
*/
public function acp()
* Turns text into an array of words
* @param string $text contains post text/subject
*
* @return array
*/
protected function split_message($text): array
{
// Split words
$text = preg_replace('#([^\p{L}\p{N}\'*])#u', '$1$1', str_replace('\'\'', '\' \'', trim($text)));
$matches = array();
preg_match_all('#(?:[^\p{L}\p{N}*]|^)([+\-|]?(?:[\p{L}\p{N}*]+\'?)*[\p{L}\p{N}*])(?:[^\p{L}\p{N}*]|$)#u', $text, $matches);
$text = $matches[1];
// remove too short or too long words
$text = array_values($text);
for ($i = 0, $n = count($text); $i < $n; $i++)
{
$text[$i] = trim($text[$i]);
if (utf8_strlen($text[$i]) < $this->config['fulltext_mysql_min_word_len'] || utf8_strlen($text[$i]) > $this->config['fulltext_mysql_max_word_len'])
{
unset($text[$i]);
}
}
return array_values($text);
}
/**
* {@inheritdoc}
*/
public function get_acp_options(): array
{
$tpl = '
<dl>
<dt><label>' . $this->user->lang['MIN_SEARCH_CHARS'] . $this->user->lang['COLON'] . '</label><br /><span>' . $this->user->lang['FULLTEXT_MYSQL_MIN_SEARCH_CHARS_EXPLAIN'] . '</span></dt>
<dt><label>' . $this->language->lang('MIN_SEARCH_CHARS') . $this->language->lang('COLON') . '</label><br /><span>' . $this->language->lang('FULLTEXT_MYSQL_MIN_SEARCH_CHARS_EXPLAIN') . '</span></dt>
<dd>' . $this->config['fulltext_mysql_min_word_len'] . '</dd>
</dl>
<dl>
<dt><label>' . $this->user->lang['MAX_SEARCH_CHARS'] . $this->user->lang['COLON'] . '</label><br /><span>' . $this->user->lang['FULLTEXT_MYSQL_MAX_SEARCH_CHARS_EXPLAIN'] . '</span></dt>
<dt><label>' . $this->language->lang('MAX_SEARCH_CHARS') . $this->language->lang('COLON') . '</label><br /><span>' . $this->language->lang('FULLTEXT_MYSQL_MAX_SEARCH_CHARS_EXPLAIN') . '</span></dt>
<dd>' . $this->config['fulltext_mysql_max_word_len'] . '</dd>
</dl>
';

View File

@@ -11,19 +11,25 @@
*
*/
namespace phpbb\search;
namespace phpbb\search\backend;
use phpbb\config\config;
use phpbb\db\driver\driver_interface;
use phpbb\event\dispatcher_interface;
use phpbb\language\language;
use phpbb\user;
/**
* phpBB's own db driven fulltext search, version 2
*/
class fulltext_native extends \phpbb\search\base
class fulltext_native extends base implements search_backend_interface
{
const UTF8_HANGUL_FIRST = "\xEA\xB0\x80";
const UTF8_HANGUL_LAST = "\xED\x9E\xA3";
const UTF8_CJK_FIRST = "\xE4\xB8\x80";
const UTF8_CJK_LAST = "\xE9\xBE\xBB";
const UTF8_CJK_B_FIRST = "\xF0\xA0\x80\x80";
const UTF8_CJK_B_LAST = "\xF0\xAA\x9B\x96";
protected const UTF8_HANGUL_FIRST = "\xEA\xB0\x80";
protected const UTF8_HANGUL_LAST = "\xED\x9E\xA3";
protected const UTF8_CJK_FIRST = "\xE4\xB8\x80";
protected const UTF8_CJK_LAST = "\xE9\xBE\xBB";
protected const UTF8_CJK_B_FIRST = "\xF0\xA0\x80\x80";
protected const UTF8_CJK_B_LAST = "\xF0\xAA\x9B\x96";
/**
* Associative array holding index stats
@@ -81,50 +87,38 @@ class fulltext_native extends \phpbb\search\base
*/
protected $php_ext;
/**
* Config object
* @var \phpbb\config\config
*/
protected $config;
/**
* Database connection
* @var \phpbb\db\driver\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 language
*/
protected $user;
protected $language;
/**
* Initialises the fulltext_native search backend with min/max word length
*
* @param boolean|string &$error is passed by reference and should either be set to false on success or an error message on failure
* @param string $phpbb_root_path phpBB root path
* @param string $phpEx PHP file extension
* @param \phpbb\auth\auth $auth Auth object
* @param \phpbb\config\config $config Config object
* @param \phpbb\db\driver\driver_interface $db Database object
* @param \phpbb\user $user User object
* @param \phpbb\event\dispatcher_interface $phpbb_dispatcher Event dispatcher object
*/
public function __construct(&$error, $phpbb_root_path, $phpEx, $auth, $config, $db, $user, $phpbb_dispatcher)
* 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 $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)
{
global $cache;
parent::__construct($cache, $config, $db, $user);
$this->phpbb_dispatcher = $phpbb_dispatcher;
$this->language = $language;
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $phpEx;
$this->config = $config;
$this->db = $db;
$this->phpbb_dispatcher = $phpbb_dispatcher;
$this->user = $user;
$this->word_length = array('min' => (int) $this->config['fulltext_native_min_chars'], 'max' => (int) $this->config['fulltext_native_max_chars']);
@@ -135,44 +129,50 @@ class fulltext_native extends \phpbb\search\base
{
include($this->phpbb_root_path . 'includes/utf/utf_tools.' . $this->php_ext);
}
$error = false;
}
/**
* Returns the name of this search backend to be displayed to administrators
*
* @return string Name
* {@inheritdoc}
*/
public function get_name()
public function get_name(): string
{
return 'phpBB Native Fulltext';
}
/**
* Returns the search_query
*
* @return string search query
* {@inheritdoc}
*/
public function get_search_query()
public function is_available(): bool
{
return true;
}
/**
* {@inheritdoc}
*/
public function init()
{
return false;
}
/**
* {@inheritdoc}
*/
public function get_search_query(): string
{
return $this->search_query;
}
/**
* Returns the common_words array
*
* @return array common words that are ignored by search backend
* {@inheritdoc}
*/
public function get_common_words()
public function get_common_words(): array
{
return $this->common_words;
}
/**
* Returns the word_length array
*
* @return array min and max word length for searching
* {@inheritdoc}
*/
public function get_word_length()
{
@@ -180,21 +180,9 @@ class fulltext_native extends \phpbb\search\base
}
/**
* This function fills $this->search_query with the cleaned user search query
*
* If $terms is 'any' then the words will be extracted from the search query
* and combined with | inside brackets. They will afterwards be treated like
* an standard search query.
*
* Then it analyses the query and fills the internal arrays $must_not_contain_ids,
* $must_contain_ids and $must_exclude_one_ids which are later used by keyword_search()
*
* @param string $keywords contains the search query string as entered by the user
* @param string $terms is either 'all' (use search query as entered, default words to 'must be contained in post')
* or 'any' (find all posts containing at least one of the given words)
* @return boolean false if no valid keywords were found and otherwise true
*/
public function split_keywords($keywords, $terms)
* {@inheritdoc}
*/
public function split_keywords(string &$keywords, string $terms): bool
{
$tokens = '+-|()* ';
@@ -294,7 +282,7 @@ class fulltext_native extends \phpbb\search\base
// We limit the number of allowed keywords to minimize load on the database
if ($this->config['max_num_search_keywords'] && $num_keywords > $this->config['max_num_search_keywords'])
{
trigger_error($this->user->lang('MAX_NUM_SEARCH_KEYWORDS_REFINE', (int) $this->config['max_num_search_keywords'], $num_keywords));
trigger_error($this->language->lang('MAX_NUM_SEARCH_KEYWORDS_REFINE', (int) $this->config['max_num_search_keywords'], $num_keywords));
}
// $keywords input format: each word separated by a space, words in a bracket are not separated
@@ -468,7 +456,7 @@ class fulltext_native extends \phpbb\search\base
// throw an error if we shall not ignore unexistant words
else if (!$ignore_no_id && count($non_common_words))
{
trigger_error(sprintf($this->user->lang['WORDS_IN_NO_POST'], implode($this->user->lang['COMMA_SEPARATOR'], $non_common_words)));
trigger_error(sprintf($this->language->lang('WORDS_IN_NO_POST'), implode($this->language->lang('COMMA_SEPARATOR'), $non_common_words)));
}
unset($non_common_words);
}
@@ -514,26 +502,9 @@ class fulltext_native extends \phpbb\search\base
}
/**
* Performs a search on keywords depending on display specific params. You have to run split_keywords() first
*
* @param string $type contains either posts or topics depending on what should be searched for
* @param string $fields contains either titleonly (topic titles should be searched), msgonly (only message bodies should be searched), firstpost (only subject and body of the first post should be searched) or all (all post bodies and subjects should be searched)
* @param string $terms is either 'all' (use query as entered, words without prefix should default to "have to be in field") or 'any' (ignore search query parts and just return all posts that contain any of the specified words)
* @param array $sort_by_sql contains SQL code for the ORDER BY part of a query
* @param string $sort_key is the key of $sort_by_sql for the selected sorting
* @param string $sort_dir is either a or d representing ASC and DESC
* @param string $sort_days specifies the maximum amount of days a post may be old
* @param array $ex_fid_ary specifies an array of forum ids which should not be searched
* @param string $post_visibility specifies which types of posts the user can view in which forums
* @param int $topic_id is set to 0 or a topic id, if it is not 0 then only posts in this topic should be searched
* @param array $author_ary an array of author ids if the author should be ignored during the search the array is empty
* @param string $author_name specifies the author match, when ANONYMOUS is also a search-match
* @param array &$id_ary passed by reference, to be filled with ids for the page specified by $start and $per_page, should be ordered
* @param int $start indicates the first index of the page
* @param int $per_page number of ids each page is supposed to contain
* @return boolean|int total number of results
*/
public function keyword_search($type, $fields, $terms, $sort_by_sql, $sort_key, $sort_dir, $sort_days, $ex_fid_ary, $post_visibility, $topic_id, $author_ary, $author_name, &$id_ary, &$start, $per_page)
* {@inheritdoc}
*/
public function keyword_search(string $type, string $fields, string $terms, array $sort_by_sql, string $sort_key, string $sort_dir, string $sort_days, array $ex_fid_ary, string $post_visibility, int $topic_id, array $author_ary, string $author_name, array &$id_ary, int &$start, int $per_page)
{
// No keywords? No posts.
if (empty($this->search_query))
@@ -612,7 +583,7 @@ class fulltext_native extends \phpbb\search\base
// 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;
}
@@ -1016,25 +987,9 @@ class fulltext_native extends \phpbb\search\base
}
/**
* Performs a search on an author's posts without caring about message contents. Depends on display specific params
*
* @param string $type contains either posts or topics depending on what should be searched for
* @param boolean $firstpost_only if true, only topic starting posts will be considered
* @param array $sort_by_sql contains SQL code for the ORDER BY part of a query
* @param string $sort_key is the key of $sort_by_sql for the selected sorting
* @param string $sort_dir is either a or d representing ASC and DESC
* @param string $sort_days specifies the maximum amount of days a post may be old
* @param array $ex_fid_ary specifies an array of forum ids which should not be searched
* @param string $post_visibility specifies which types of posts the user can view in which forums
* @param int $topic_id is set to 0 or a topic id, if it is not 0 then only posts in this topic should be searched
* @param array $author_ary an array of author ids
* @param string $author_name specifies the author match, when ANONYMOUS is also a search-match
* @param array &$id_ary passed by reference, to be filled with ids for the page specified by $start and $per_page, should be ordered
* @param int $start indicates the first index of the page
* @param int $per_page number of ids each page is supposed to contain
* @return boolean|int total number of results
*/
public function author_search($type, $firstpost_only, $sort_by_sql, $sort_key, $sort_dir, $sort_days, $ex_fid_ary, $post_visibility, $topic_id, $author_ary, $author_name, &$id_ary, &$start, $per_page)
* {@inheritdoc}
*/
public function author_search(string $type, bool $firstpost_only, array $sort_by_sql, string $sort_key, string $sort_dir, string $sort_days, array $ex_fid_ary, string $post_visibility, int $topic_id, array $author_ary, string $author_name, array &$id_ary, int &$start, int $per_page)
{
// No author? No posts
if (!count($author_ary))
@@ -1092,7 +1047,7 @@ class fulltext_native extends \phpbb\search\base
// 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;
}
@@ -1326,91 +1281,17 @@ class fulltext_native extends \phpbb\search\base
}
/**
* Split a text into words of a given length
*
* The text is converted to UTF-8, cleaned up, and split. Then, words that
* conform to the defined length range are returned in an array.
*
* NOTE: duplicates are NOT removed from the return array
*
* @param string $text Text to split, encoded in UTF-8
* @return array Array of UTF-8 words
*/
public function split_message($text)
* {@inheritdoc}
*/
public function supports_phrase_search(): bool
{
$match = $words = array();
/**
* Taken from the original code
*/
// Do not index code
$match[] = '#\[code(?:=.*?)?(\:?[0-9a-z]{5,})\].*?\[\/code(\:?[0-9a-z]{5,})\]#is';
// BBcode
$match[] = '#\[\/?[a-z0-9\*\+\-]+(?:=.*?)?(?::[a-z])?(\:?[0-9a-z]{5,})\]#';
$min = $this->word_length['min'];
$isset_min = $min - 1;
/**
* Clean up the string, remove HTML tags, remove BBCodes
*/
$word = strtok($this->cleanup(preg_replace($match, ' ', strip_tags($text)), -1), ' ');
while (strlen($word))
{
if (strlen($word) > 255 || strlen($word) <= $isset_min)
{
/**
* Words longer than 255 bytes are ignored. This will have to be
* changed whenever we change the length of search_wordlist.word_text
*
* Words shorter than $isset_min bytes are ignored, too
*/
$word = strtok(' ');
continue;
}
$len = utf8_strlen($word);
/**
* Test whether the word is too short to be indexed.
*
* Note that this limit does NOT apply to CJK and Hangul
*/
if ($len < $min)
{
/**
* Note: this could be optimized. If the codepoint is lower than Hangul's range
* we know that it will also be lower than CJK ranges
*/
if ((strncmp($word, self::UTF8_HANGUL_FIRST, 3) < 0 || strncmp($word, self::UTF8_HANGUL_LAST, 3) > 0)
&& (strncmp($word, self::UTF8_CJK_FIRST, 3) < 0 || strncmp($word, self::UTF8_CJK_LAST, 3) > 0)
&& (strncmp($word, self::UTF8_CJK_B_FIRST, 4) < 0 || strncmp($word, self::UTF8_CJK_B_LAST, 4) > 0))
{
$word = strtok(' ');
continue;
}
}
$words[] = $word;
$word = strtok(' ');
}
return $words;
return false;
}
/**
* Updates wordlist and wordmatch tables when a message is posted or changed
*
* @param string $mode Contains the post mode: edit, post, reply, quote
* @param int $post_id The id of the post which is modified/created
* @param string &$message New or updated post content
* @param string &$subject New or updated post subject
* @param int $poster_id Post author's user id
* @param int $forum_id The id of the forum in which the post is located
* {@inheritdoc}
*/
public function index($mode, $post_id, &$message, &$subject, $poster_id, $forum_id)
public function index(string $mode, int $post_id, string &$message, string &$subject, int $poster_id, int $forum_id)
{
if (!$this->config['fulltext_native_load_upd'])
{
@@ -1597,9 +1478,9 @@ class fulltext_native extends \phpbb\search\base
}
/**
* Removes entries from the wordmatch table for the specified post_ids
*/
public function index_remove($post_ids, $author_ids, $forum_ids)
* {@inheritdoc}
*/
public function index_remove(array $post_ids, array $author_ids, array $forum_ids): void
{
if (count($post_ids))
{
@@ -1654,10 +1535,9 @@ class fulltext_native extends \phpbb\search\base
}
/**
* Tidy up indexes: Tag 'common words' and remove
* words no longer referenced in the match table
*/
public function tidy()
* {@inheritdoc}
*/
public function tidy(): void
{
// Is the fulltext indexer disabled? If yes then we need not
// carry on ... it's okay ... I know when I'm not wanted boo hoo
@@ -1717,10 +1597,12 @@ class fulltext_native extends \phpbb\search\base
$this->config->set('search_last_gc', time(), false);
}
// create_index is inherited from base.php
/**
* Deletes all words from the index
*/
public function delete_index($acp_module, $u_action)
* {@inheritdoc}
*/
public function delete_index(int &$post_counter = null): ?array
{
$sql_queries = [];
@@ -1759,24 +1641,26 @@ class fulltext_native extends \phpbb\search\base
{
$this->db->sql_query($sql_query);
}
return null;
}
/**
* Returns true if both FULLTEXT indexes exist
* {@inheritdoc}
*/
public function index_created()
public function index_created(): bool
{
if (!count($this->stats))
{
$this->get_stats();
}
return ($this->stats['total_words'] && $this->stats['total_matches']) ? true : false;
return $this->stats['total_words'] && $this->stats['total_matches'];
}
/**
* Returns an associative array containing information about the indexes
*/
* {@inheritdoc}
*/
public function index_stats()
{
if (!count($this->stats))
@@ -1785,16 +1669,94 @@ class fulltext_native extends \phpbb\search\base
}
return array(
$this->user->lang['TOTAL_WORDS'] => $this->stats['total_words'],
$this->user->lang['TOTAL_MATCHES'] => $this->stats['total_matches']);
$this->language->lang('TOTAL_WORDS') => $this->stats['total_words'],
$this->language->lang('TOTAL_MATCHES') => $this->stats['total_matches']);
}
/**
* Computes the stats and store them in the $this->stats associative array
*/
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);
}
/**
* Split a text into words of a given length
*
* The text is converted to UTF-8, cleaned up, and split. Then, words that
* conform to the defined length range are returned in an array.
*
* NOTE: duplicates are NOT removed from the return array
*
* @param string $text Text to split, encoded in UTF-8
* @return array Array of UTF-8 words
*/
protected function split_message($text)
{
$match = $words = array();
/**
* Taken from the original code
*/
// Do not index code
$match[] = '#\[code(?:=.*?)?(\:?[0-9a-z]{5,})\].*?\[\/code(\:?[0-9a-z]{5,})\]#is';
// BBcode
$match[] = '#\[\/?[a-z0-9\*\+\-]+(?:=.*?)?(?::[a-z])?(\:?[0-9a-z]{5,})\]#';
$min = $this->word_length['min'];
$isset_min = $min - 1;
/**
* Clean up the string, remove HTML tags, remove BBCodes
*/
$word = strtok($this->cleanup(preg_replace($match, ' ', strip_tags($text)), -1), ' ');
while (strlen($word))
{
if (strlen($word) > 255 || strlen($word) <= $isset_min)
{
/**
* Words longer than 255 bytes are ignored. This will have to be
* changed whenever we change the length of search_wordlist.word_text
*
* Words shorter than $isset_min bytes are ignored, too
*/
$word = strtok(' ');
continue;
}
$len = utf8_strlen($word);
/**
* Test whether the word is too short to be indexed.
*
* Note that this limit does NOT apply to CJK and Hangul
*/
if ($len < $min)
{
/**
* Note: this could be optimized. If the codepoint is lower than Hangul's range
* we know that it will also be lower than CJK ranges
*/
if ((strncmp($word, self::UTF8_HANGUL_FIRST, 3) < 0 || strncmp($word, self::UTF8_HANGUL_LAST, 3) > 0)
&& (strncmp($word, self::UTF8_CJK_FIRST, 3) < 0 || strncmp($word, self::UTF8_CJK_LAST, 3) > 0)
&& (strncmp($word, self::UTF8_CJK_B_FIRST, 4) < 0 || strncmp($word, self::UTF8_CJK_B_LAST, 4) > 0))
{
$word = strtok(' ');
continue;
}
}
$words[] = $word;
$word = strtok(' ');
}
return $words;
}
/**
* Clean up a text to remove non-alphanumeric characters
*
@@ -2032,9 +1994,9 @@ class fulltext_native extends \phpbb\search\base
}
/**
* Returns a list of options for the ACP to display
*/
public function acp()
* {@inheritdoc}
*/
public function get_acp_options(): array
{
/**
* if we need any options, copied from fulltext_native for now, will have to be adjusted or removed
@@ -2042,19 +2004,19 @@ class fulltext_native extends \phpbb\search\base
$tpl = '
<dl>
<dt><label for="fulltext_native_load_upd">' . $this->user->lang['YES_SEARCH_UPDATE'] . $this->user->lang['COLON'] . '</label><br /><span>' . $this->user->lang['YES_SEARCH_UPDATE_EXPLAIN'] . '</span></dt>
<dd><label><input type="radio" id="fulltext_native_load_upd" name="config[fulltext_native_load_upd]" value="1"' . (($this->config['fulltext_native_load_upd']) ? ' checked="checked"' : '') . ' class="radio" /> ' . $this->user->lang['YES'] . '</label><label><input type="radio" name="config[fulltext_native_load_upd]" value="0"' . ((!$this->config['fulltext_native_load_upd']) ? ' checked="checked"' : '') . ' class="radio" /> ' . $this->user->lang['NO'] . '</label></dd>
<dt><label for="fulltext_native_load_upd">' . $this->language->lang('YES_SEARCH_UPDATE') . $this->language->lang('COLON') . '</label><br /><span>' . $this->language->lang('YES_SEARCH_UPDATE_EXPLAIN') . '</span></dt>
<dd><label><input type="radio" id="fulltext_native_load_upd" name="config[fulltext_native_load_upd]" value="1"' . (($this->config['fulltext_native_load_upd']) ? ' checked="checked"' : '') . ' class="radio" /> ' . $this->language->lang('YES') . '</label><label><input type="radio" name="config[fulltext_native_load_upd]" value="0"' . ((!$this->config['fulltext_native_load_upd']) ? ' checked="checked"' : '') . ' class="radio" /> ' . $this->language->lang('NO') . '</label></dd>
</dl>
<dl>
<dt><label for="fulltext_native_min_chars">' . $this->user->lang['MIN_SEARCH_CHARS'] . $this->user->lang['COLON'] . '</label><br /><span>' . $this->user->lang['MIN_SEARCH_CHARS_EXPLAIN'] . '</span></dt>
<dt><label for="fulltext_native_min_chars">' . $this->language->lang('MIN_SEARCH_CHARS') . $this->language->lang('COLON') . '</label><br /><span>' . $this->language->lang('MIN_SEARCH_CHARS_EXPLAIN') . '</span></dt>
<dd><input id="fulltext_native_min_chars" type="number" min="0" max="255" name="config[fulltext_native_min_chars]" value="' . (int) $this->config['fulltext_native_min_chars'] . '" /></dd>
</dl>
<dl>
<dt><label for="fulltext_native_max_chars">' . $this->user->lang['MAX_SEARCH_CHARS'] . $this->user->lang['COLON'] . '</label><br /><span>' . $this->user->lang['MAX_SEARCH_CHARS_EXPLAIN'] . '</span></dt>
<dt><label for="fulltext_native_max_chars">' . $this->language->lang('MAX_SEARCH_CHARS') . $this->language->lang('COLON') . '</label><br /><span>' . $this->language->lang('MAX_SEARCH_CHARS_EXPLAIN') . '</span></dt>
<dd><input id="fulltext_native_max_chars" type="number" min="0" max="255" name="config[fulltext_native_max_chars]" value="' . (int) $this->config['fulltext_native_max_chars'] . '" /></dd>
</dl>
<dl>
<dt><label for="fulltext_native_common_thres">' . $this->user->lang['COMMON_WORD_THRESHOLD'] . $this->user->lang['COLON'] . '</label><br /><span>' . $this->user->lang['COMMON_WORD_THRESHOLD_EXPLAIN'] . '</span></dt>
<dt><label for="fulltext_native_common_thres">' . $this->language->lang('COMMON_WORD_THRESHOLD') . $this->language->lang('COLON') . '</label><br /><span>' . $this->language->lang('COMMON_WORD_THRESHOLD_EXPLAIN') . '</span></dt>
<dd><input id="fulltext_native_common_thres" type="text" name="config[fulltext_native_common_thres]" value="' . (double) $this->config['fulltext_native_common_thres'] . '" /> %</dd>
</dl>
';

View File

@@ -11,12 +11,19 @@
*
*/
namespace phpbb\search;
namespace phpbb\search\backend;
use phpbb\config\config;
use phpbb\db\driver\driver_interface;
use phpbb\event\dispatcher_interface;
use phpbb\language\language;
use phpbb\user;
use RuntimeException;
/**
* Fulltext search for PostgreSQL
*/
class fulltext_postgres extends \phpbb\search\base
class fulltext_postgres extends base implements search_backend_interface
{
/**
* Associative array holding index stats
@@ -43,30 +50,16 @@ class fulltext_postgres extends \phpbb\search\base
*/
protected $phrase_search = false;
/**
* Config object
* @var \phpbb\config\config
*/
protected $config;
/**
* Database connection
* @var \phpbb\db\driver\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 language
*/
protected $user;
protected $language;
/**
* Contains tidied search query.
* Operators are prefixed in search query and common words excluded
@@ -89,23 +82,23 @@ class fulltext_postgres extends \phpbb\search\base
/**
* Constructor
* Creates a new \phpbb\search\fulltext_postgres, which is used as a search backend
* Creates a new \phpbb\search\backend\fulltext_postgres, which is used as a search backend
*
* @param string|bool $error Any error that occurs is passed on through this reference variable otherwise false
* @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 \phpbb\auth\auth $auth Auth object
* @param \phpbb\config\config $config Config object
* @param \phpbb\db\driver\driver_interface Database object
* @param \phpbb\user $user User object
* @param \phpbb\event\dispatcher_interface $phpbb_dispatcher Event dispatcher object
*/
public function __construct(&$error, $phpbb_root_path, $phpEx, $auth, $config, $db, $user, $phpbb_dispatcher)
public function __construct(config $config, driver_interface $db, dispatcher_interface $phpbb_dispatcher, language $language, user $user, string $phpbb_root_path, string $phpEx)
{
$this->config = $config;
$this->db = $db;
global $cache;
parent::__construct($cache, $config, $db, $user);
$this->phpbb_dispatcher = $phpbb_dispatcher;
$this->user = $user;
$this->language = $language;
$this->word_length = array('min' => $this->config['fulltext_postgres_min_word_len'], 'max' => $this->config['fulltext_postgres_max_word_len']);
@@ -116,44 +109,55 @@ class fulltext_postgres extends \phpbb\search\base
{
include($phpbb_root_path . 'includes/utf/utf_tools.' . $phpEx);
}
$error = false;
}
/**
* Returns the name of this search backend to be displayed to administrators
*
* @return string Name
*/
public function get_name()
* {@inheritdoc}
*/
public function get_name(): string
{
return 'PostgreSQL Fulltext';
}
/**
* Returns the search_query
*
* @return string search query
* {@inheritdoc}
*/
public function get_search_query()
public function is_available(): bool
{
return $this->db->get_sql_layer() == 'postgres';
}
/**
* {@inheritdoc}
*/
public function init()
{
if (!$this->is_available())
{
return $this->language->lang('FULLTEXT_POSTGRES_INCOMPATIBLE_DATABASE');
}
return false;
}
/**
* {@inheritdoc}
*/
public function get_search_query(): string
{
return $this->search_query;
}
/**
* Returns the common_words array
*
* @return array common words that are ignored by search backend
* {@inheritdoc}
*/
public function get_common_words()
public function get_common_words(): array
{
return $this->common_words;
}
/**
* Returns the word_length array
*
* @return array min and max word length for searching
* {@inheritdoc}
*/
public function get_word_length()
{
@@ -161,39 +165,9 @@ class fulltext_postgres extends \phpbb\search\base
}
/**
* Returns if phrase search is supported or not
*
* @return bool
* {@inheritdoc}
*/
public function supports_phrase_search()
{
return $this->phrase_search;
}
/**
* Checks for correct PostgreSQL version and stores min/max word length in the config
*
* @return string|bool Language key of the error/incompatibility occurred
*/
public function init()
{
if ($this->db->get_sql_layer() != 'postgres')
{
return $this->user->lang['FULLTEXT_POSTGRES_INCOMPATIBLE_DATABASE'];
}
return false;
}
/**
* Splits keywords entered by a user into an array of words stored in $this->split_words
* Stores the tidied search query in $this->search_query
*
* @param string &$keywords Contains the keyword as entered by the user
* @param string $terms is either 'all' or 'any'
* @return bool false if no valid keywords were found and otherwise true
*/
public function split_keywords(&$keywords, $terms)
public function split_keywords(string &$keywords, string $terms): bool
{
if ($terms == 'all')
{
@@ -280,53 +254,11 @@ class fulltext_postgres extends \phpbb\search\base
return false;
}
/**
* Turns text into an array of words
* @param string $text contains post text/subject
*/
public function split_message($text)
{
// Split words
$text = preg_replace('#([^\p{L}\p{N}\'*])#u', '$1$1', str_replace('\'\'', '\' \'', trim($text)));
$matches = array();
preg_match_all('#(?:[^\p{L}\p{N}*]|^)([+\-|]?(?:[\p{L}\p{N}*]+\'?)*[\p{L}\p{N}*])(?:[^\p{L}\p{N}*]|$)#u', $text, $matches);
$text = $matches[1];
// remove too short or too long words
$text = array_values($text);
for ($i = 0, $n = count($text); $i < $n; $i++)
{
$text[$i] = trim($text[$i]);
if (utf8_strlen($text[$i]) < $this->config['fulltext_postgres_min_word_len'] || utf8_strlen($text[$i]) > $this->config['fulltext_postgres_max_word_len'])
{
unset($text[$i]);
}
}
return array_values($text);
}
/**
* Performs a search on keywords depending on display specific params. You have to run split_keywords() first
*
* @param string $type contains either posts or topics depending on what should be searched for
* @param string $fields contains either titleonly (topic titles should be searched), msgonly (only message bodies should be searched), firstpost (only subject and body of the first post should be searched) or all (all post bodies and subjects should be searched)
* @param string $terms is either 'all' (use query as entered, words without prefix should default to "have to be in field") or 'any' (ignore search query parts and just return all posts that contain any of the specified words)
* @param array $sort_by_sql contains SQL code for the ORDER BY part of a query
* @param string $sort_key is the key of $sort_by_sql for the selected sorting
* @param string $sort_dir is either a or d representing ASC and DESC
* @param string $sort_days specifies the maximum amount of days a post may be old
* @param array $ex_fid_ary specifies an array of forum ids which should not be searched
* @param string $post_visibility specifies which types of posts the user can view in which forums
* @param int $topic_id is set to 0 or a topic id, if it is not 0 then only posts in this topic should be searched
* @param array $author_ary an array of author ids if the author should be ignored during the search the array is empty
* @param string $author_name specifies the author match, when ANONYMOUS is also a search-match
* @param array &$id_ary passed by reference, to be filled with ids for the page specified by $start and $per_page, should be ordered
* @param int $start indicates the first index of the page
* @param int $per_page number of ids each page is supposed to contain
* @return boolean|int total number of results
*/
public function keyword_search($type, $fields, $terms, $sort_by_sql, $sort_key, $sort_dir, $sort_days, $ex_fid_ary, $post_visibility, $topic_id, $author_ary, $author_name, &$id_ary, &$start, $per_page)
* {@inheritdoc}
*/
public function keyword_search(string $type, string $fields, string $terms, array $sort_by_sql, string $sort_key, string $sort_dir, string $sort_days, array $ex_fid_ary, string $post_visibility, int $topic_id, array $author_ary, string $author_name, array &$id_ary, int &$start, int $per_page)
{
// No keywords? No posts
if (!$this->search_query)
@@ -355,21 +287,21 @@ class fulltext_postgres extends \phpbb\search\base
);
/**
* Allow changing the search_key for cached results
*
* @event core.search_postgres_by_keyword_modify_search_key
* @var array search_key_array Array with search parameters to generate the search_key
* @var string type Searching type ('posts', 'topics')
* @var string fields Searching fields ('titleonly', 'msgonly', 'firstpost', 'all')
* @var string terms Searching terms ('all', 'any')
* @var int sort_days Time, in days, of the oldest possible post to list
* @var string sort_key The sort type used from the possible sort types
* @var int topic_id Limit the search to this topic_id only
* @var array ex_fid_ary Which forums not to search on
* @var string post_visibility Post visibility data
* @var array author_ary Array of user_id containing the users to filter the results to
* @since 3.1.7-RC1
*/
* Allow changing the search_key for cached results
*
* @event core.search_postgres_by_keyword_modify_search_key
* @var array search_key_array Array with search parameters to generate the search_key
* @var string type Searching type ('posts', 'topics')
* @var string fields Searching fields ('titleonly', 'msgonly', 'firstpost', 'all')
* @var string terms Searching terms ('all', 'any')
* @var int sort_days Time, in days, of the oldest possible post to list
* @var string sort_key The sort type used from the possible sort types
* @var int topic_id Limit the search to this topic_id only
* @var array ex_fid_ary Which forums not to search on
* @var string post_visibility Post visibility data
* @var array author_ary Array of user_id containing the users to filter the results to
* @since 3.1.7-RC1
*/
$vars = array(
'search_key_array',
'type',
@@ -393,7 +325,7 @@ class fulltext_postgres extends \phpbb\search\base
// 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;
}
@@ -430,53 +362,53 @@ class fulltext_postgres extends \phpbb\search\base
$sql_match = 'p.post_subject';
$sql_match_where = ' AND p.post_id = t.topic_first_post_id';
$join_topic = true;
break;
break;
case 'msgonly':
$sql_match = 'p.post_text';
$sql_match_where = '';
break;
break;
case 'firstpost':
$sql_match = 'p.post_subject, p.post_text';
$sql_match_where = ' AND p.post_id = t.topic_first_post_id';
$join_topic = true;
break;
break;
default:
$sql_match = 'p.post_subject, p.post_text';
$sql_match_where = '';
break;
break;
}
$tsearch_query = $this->tsearch_query;
/**
* Allow changing the query used to search for posts using fulltext_postgres
*
* @event core.search_postgres_keywords_main_query_before
* @var string tsearch_query The parsed keywords used for this search
* @var int result_count The previous result count for the format of the query.
* Set to 0 to force a re-count
* @var bool join_topic Weather or not TOPICS_TABLE should be CROSS JOIN'ED
* @var array author_ary Array of user_id containing the users to filter the results to
* @var string author_name An extra username to search on (!empty(author_ary) must be true, to be relevant)
* @var array ex_fid_ary Which forums not to search on
* @var int topic_id Limit the search to this topic_id only
* @var string sql_sort_table Extra tables to include in the SQL query.
* Used in conjunction with sql_sort_join
* @var string sql_sort_join SQL conditions to join all the tables used together.
* Used in conjunction with sql_sort_table
* @var int sort_days Time, in days, of the oldest possible post to list
* @var string sql_match Which columns to do the search on.
* @var string sql_match_where Extra conditions to use to properly filter the matching process
* @var string sort_by_sql The possible predefined sort types
* @var string sort_key The sort type used from the possible sort types
* @var string sort_dir "a" for ASC or "d" dor DESC for the sort order used
* @var string sql_sort The result SQL when processing sort_by_sql + sort_key + sort_dir
* @var int start How many posts to skip in the search results (used for pagination)
* @since 3.1.5-RC1
*/
* Allow changing the query used to search for posts using fulltext_postgres
*
* @event core.search_postgres_keywords_main_query_before
* @var string tsearch_query The parsed keywords used for this search
* @var int result_count The previous result count for the format of the query.
* Set to 0 to force a re-count
* @var bool join_topic Weather or not TOPICS_TABLE should be CROSS JOIN'ED
* @var array author_ary Array of user_id containing the users to filter the results to
* @var string author_name An extra username to search on (!empty(author_ary) must be true, to be relevant)
* @var array ex_fid_ary Which forums not to search on
* @var int topic_id Limit the search to this topic_id only
* @var string sql_sort_table Extra tables to include in the SQL query.
* Used in conjunction with sql_sort_join
* @var string sql_sort_join SQL conditions to join all the tables used together.
* Used in conjunction with sql_sort_table
* @var int sort_days Time, in days, of the oldest possible post to list
* @var string sql_match Which columns to do the search on.
* @var string sql_match_where Extra conditions to use to properly filter the matching process
* @var string sort_by_sql The possible predefined sort types
* @var string sort_key The sort type used from the possible sort types
* @var string sort_dir "a" for ASC or "d" dor DESC for the sort order used
* @var string sql_sort The result SQL when processing sort_by_sql + sort_key + sort_dir
* @var int start How many posts to skip in the search results (used for pagination)
* @since 3.1.5-RC1
*/
$vars = array(
'tsearch_query',
'result_count',
@@ -588,25 +520,9 @@ class fulltext_postgres extends \phpbb\search\base
}
/**
* Performs a search on an author's posts without caring about message contents. Depends on display specific params
*
* @param string $type contains either posts or topics depending on what should be searched for
* @param boolean $firstpost_only if true, only topic starting posts will be considered
* @param array $sort_by_sql contains SQL code for the ORDER BY part of a query
* @param string $sort_key is the key of $sort_by_sql for the selected sorting
* @param string $sort_dir is either a or d representing ASC and DESC
* @param string $sort_days specifies the maximum amount of days a post may be old
* @param array $ex_fid_ary specifies an array of forum ids which should not be searched
* @param string $post_visibility specifies which types of posts the user can view in which forums
* @param int $topic_id is set to 0 or a topic id, if it is not 0 then only posts in this topic should be searched
* @param array $author_ary an array of author ids
* @param string $author_name specifies the author match, when ANONYMOUS is also a search-match
* @param array &$id_ary passed by reference, to be filled with ids for the page specified by $start and $per_page, should be ordered
* @param int $start indicates the first index of the page
* @param int $per_page number of ids each page is supposed to contain
* @return boolean|int total number of results
*/
public function author_search($type, $firstpost_only, $sort_by_sql, $sort_key, $sort_dir, $sort_days, $ex_fid_ary, $post_visibility, $topic_id, $author_ary, $author_name, &$id_ary, &$start, $per_page)
* {@inheritdoc}
*/
public function author_search(string $type, bool $firstpost_only, array $sort_by_sql, string $sort_key, string $sort_dir, string $sort_days, array $ex_fid_ary, string $post_visibility, int $topic_id, array $author_ary, string $author_name, array &$id_ary, int &$start, int $per_page)
{
// No author? No posts
if (!count($author_ary))
@@ -669,7 +585,7 @@ class fulltext_postgres extends \phpbb\search\base
// 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;
}
@@ -872,16 +788,17 @@ class fulltext_postgres extends \phpbb\search\base
}
/**
* Destroys cached search results, that contained one of the new words in a post so the results won't be outdated
*
* @param string $mode contains the post mode: edit, post, reply, quote ...
* @param int $post_id contains the post id of the post to index
* @param string $message contains the post text of the post
* @param string $subject contains the subject of the post to index
* @param int $poster_id contains the user id of the poster
* @param int $forum_id contains the forum id of parent forum of the post
*/
public function index($mode, $post_id, &$message, &$subject, $poster_id, $forum_id)
* {@inheritdoc}
*/
public function supports_phrase_search(): bool
{
return $this->phrase_search;
}
/**
* {@inheritdoc}
*/
public function index(string $mode, int $post_id, string &$message, string &$subject, int $poster_id, int $forum_id)
{
// Split old and new post/subject to obtain array of words
$split_text = $this->split_message($message);
@@ -927,17 +844,17 @@ class fulltext_postgres extends \phpbb\search\base
}
/**
* Destroy cached results, that might be outdated after deleting a post
*/
public function index_remove($post_ids, $author_ids, $forum_ids)
* {@inheritdoc}
*/
public function index_remove(array $post_ids, array $author_ids, array $forum_ids): void
{
$this->destroy_cache(array(), $author_ids);
$this->destroy_cache([], $author_ids);
}
/**
* Destroy old cache entries
*/
public function tidy()
* {@inheritdoc}
*/
public function tidy(): void
{
// destroy too old cached search results
$this->destroy_cache(array());
@@ -946,16 +863,14 @@ class fulltext_postgres extends \phpbb\search\base
}
/**
* Create fulltext index
*
* @return string|bool error string is returned incase of errors otherwise false
*/
public function create_index($acp_module, $u_action)
* {@inheritdoc}
*/
public function create_index(int &$post_counter = 0): ?array
{
// Make sure we can actually use PostgreSQL with fulltext indexes
if ($error = $this->init())
{
return $error;
throw new RuntimeException($error);
}
if (empty($this->stats))
@@ -1003,20 +918,18 @@ class fulltext_postgres extends \phpbb\search\base
$this->db->sql_query('TRUNCATE TABLE ' . SEARCH_RESULTS_TABLE);
return false;
return null;
}
/**
* Drop fulltext index
*
* @return string|bool error string is returned incase of errors otherwise false
*/
public function delete_index($acp_module, $u_action)
* {@inheritdoc}
*/
public function delete_index(int &$post_counter = null): ?array
{
// Make sure we can actually use PostgreSQL with fulltext indexes
if ($error = $this->init())
{
return $error;
throw new RuntimeException($error);
}
if (empty($this->stats))
@@ -1064,13 +977,13 @@ class fulltext_postgres extends \phpbb\search\base
$this->db->sql_query('TRUNCATE TABLE ' . SEARCH_RESULTS_TABLE);
return false;
return null;
}
/**
* Returns true if both FULLTEXT indexes exist
* {@inheritdoc}
*/
public function index_created()
public function index_created(): bool
{
if (empty($this->stats))
{
@@ -1081,7 +994,7 @@ class fulltext_postgres extends \phpbb\search\base
}
/**
* Returns an associative array containing information about the indexes
* {@inheritdoc}
*/
public function index_stats()
{
@@ -1091,12 +1004,12 @@ class fulltext_postgres extends \phpbb\search\base
}
return array(
$this->user->lang['FULLTEXT_POSTGRES_TOTAL_POSTS'] => ($this->index_created()) ? $this->stats['total_posts'] : 0,
$this->language->lang('FULLTEXT_POSTGRES_TOTAL_POSTS') => ($this->index_created()) ? $this->stats['total_posts'] : 0,
);
}
/**
* Computes the stats and store them in the $this->stats associative array
* {@inheritdoc}
*/
protected function get_stats()
{
@@ -1139,19 +1052,44 @@ class fulltext_postgres extends \phpbb\search\base
}
/**
* Display various options that can be configured for the backend from the acp
*
* @return associative array containing template and config variables
*/
public function acp()
* Turns text into an array of words
* @param string $text contains post text/subject
* @return array
*/
protected function split_message($text)
{
// Split words
$text = preg_replace('#([^\p{L}\p{N}\'*])#u', '$1$1', str_replace('\'\'', '\' \'', trim($text)));
$matches = array();
preg_match_all('#(?:[^\p{L}\p{N}*]|^)([+\-|]?(?:[\p{L}\p{N}*]+\'?)*[\p{L}\p{N}*])(?:[^\p{L}\p{N}*]|$)#u', $text, $matches);
$text = $matches[1];
// remove too short or too long words
$text = array_values($text);
for ($i = 0, $n = count($text); $i < $n; $i++)
{
$text[$i] = trim($text[$i]);
if (utf8_strlen($text[$i]) < $this->config['fulltext_postgres_min_word_len'] || utf8_strlen($text[$i]) > $this->config['fulltext_postgres_max_word_len'])
{
unset($text[$i]);
}
}
return array_values($text);
}
/**
* {@inheritdoc}
*/
public function get_acp_options(): array
{
$tpl = '
<dl>
<dt><label>' . $this->user->lang['FULLTEXT_POSTGRES_VERSION_CHECK'] . '</label><br /><span>' . $this->user->lang['FULLTEXT_POSTGRES_VERSION_CHECK_EXPLAIN'] . '</span></dt>
<dd>' . (($this->db->get_sql_layer() == 'postgres') ? $this->user->lang['YES'] : $this->user->lang['NO']) . '</dd>
<dt><label>' . $this->language->lang('FULLTEXT_POSTGRES_VERSION_CHECK') . '</label><br /><span>' . $this->language->lang('FULLTEXT_POSTGRES_VERSION_CHECK_EXPLAIN') . '</span></dt>
<dd>' . (($this->db->get_sql_layer() == 'postgres') ? $this->language->lang('YES') : $this->language->lang('NO')) . '</dd>
</dl>
<dl>
<dt><label>' . $this->user->lang['FULLTEXT_POSTGRES_TS_NAME'] . '</label><br /><span>' . $this->user->lang['FULLTEXT_POSTGRES_TS_NAME_EXPLAIN'] . '</span></dt>
<dt><label>' . $this->language->lang('FULLTEXT_POSTGRES_TS_NAME') . '</label><br /><span>' . $this->language->lang('FULLTEXT_POSTGRES_TS_NAME_EXPLAIN') . '</span></dt>
<dd><select name="config[fulltext_postgres_ts_name]">';
if ($this->db->get_sql_layer() == 'postgres')
@@ -1174,11 +1112,11 @@ class fulltext_postgres extends \phpbb\search\base
$tpl .= '</select></dd>
</dl>
<dl>
<dt><label for="fulltext_postgres_min_word_len">' . $this->user->lang['FULLTEXT_POSTGRES_MIN_WORD_LEN'] . $this->user->lang['COLON'] . '</label><br /><span>' . $this->user->lang['FULLTEXT_POSTGRES_MIN_WORD_LEN_EXPLAIN'] . '</span></dt>
<dt><label for="fulltext_postgres_min_word_len">' . $this->language->lang('FULLTEXT_POSTGRES_MIN_WORD_LEN') . $this->language->lang('COLON') . '</label><br /><span>' . $this->language->lang('FULLTEXT_POSTGRES_MIN_WORD_LEN_EXPLAIN') . '</span></dt>
<dd><input id="fulltext_postgres_min_word_len" type="number" min="0" max="255" name="config[fulltext_postgres_min_word_len]" value="' . (int) $this->config['fulltext_postgres_min_word_len'] . '" /></dd>
</dl>
<dl>
<dt><label for="fulltext_postgres_max_word_len">' . $this->user->lang['FULLTEXT_POSTGRES_MAX_WORD_LEN'] . $this->user->lang['COLON'] . '</label><br /><span>' . $this->user->lang['FULLTEXT_POSTGRES_MAX_WORD_LEN_EXPLAIN'] . '</span></dt>
<dt><label for="fulltext_postgres_max_word_len">' . $this->language->lang('FULLTEXT_POSTGRES_MAX_WORD_LEN') . $this->language->lang('COLON') . '</label><br /><span>' . $this->language->lang('FULLTEXT_POSTGRES_MAX_WORD_LEN_EXPLAIN') . '</span></dt>
<dd><input id="fulltext_postgres_max_word_len" type="number" min="0" max="255" name="config[fulltext_postgres_max_word_len]" value="' . (int) $this->config['fulltext_postgres_max_word_len'] . '" /></dd>
</dl>
';

View File

@@ -0,0 +1,196 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @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;
interface search_backend_interface
{
/**
* Returns the name of this search backend to be displayed to administrators
*
* @return string Name
*/
public function get_name(): string;
/**
* Returns if the search engine is available
*
* @return bool
*/
public function is_available(): bool;
/**
* Method executed when a search backend is set from acp.
*
* Checks permissions and paths, if everything is correct it generates the config file
*
* @return string|false False if everything was ok or string with error message
*/
public function init();
/**
* Returns the search_query
*
* @return string search query
*/
public function get_search_query(): string;
/**
* Returns the common_words array
*
* @return array common words that are ignored by search backend
*/
public function get_common_words(): array;
/**
* Returns the word_length array
*
* @return array|false min and max word length for searching
*/
public function get_word_length();
/**
* Splits keywords entered by a user into an array of words stored in $this->split_words
* This function fills $this->search_query with the cleaned user search query
*
* If $terms is 'any' then the words will be extracted from the search query
* and combined with | inside brackets. They will afterwards be treated like
* an standard search query.
*
* Then it analyses the query and fills the internal arrays $must_not_contain_ids,
* $must_contain_ids and $must_exclude_one_ids which are later used by keyword_search()
*
* @param string $keywords contains the search query string as entered by the user
* @param string $terms is either 'all' (use search query as entered, default words to 'must be contained in post')
* or 'any' (find all posts containing at least one of the given words)
* @return boolean false if no valid keywords were found and otherwise true
*/
public function split_keywords(string &$keywords, string $terms): bool;
/**
* Performs a search on keywords depending on display specific params. You have to run split_keywords() first
*
* @param string $type contains either posts or topics depending on what should be searched for
* @param string $fields contains either titleonly (topic titles should be searched), msgonly (only message bodies should be searched), firstpost (only subject and body of the first post should be searched) or all (all post bodies and subjects should be searched)
* @param string $terms is either 'all' (use query as entered, words without prefix should default to "have to be in field") or 'any' (ignore search query parts and just return all posts that contain any of the specified words)
* @param array $sort_by_sql contains SQL code for the ORDER BY part of a query
* @param string $sort_key is the key of $sort_by_sql for the selected sorting
* @param string $sort_dir is either a or d representing ASC and DESC
* @param string $sort_days specifies the maximum amount of days a post may be old
* @param array $ex_fid_ary specifies an array of forum ids which should not be searched
* @param string $post_visibility specifies which types of posts the user can view in which forums
* @param int $topic_id is set to 0 or a topic id, if it is not 0 then only posts in this topic should be searched
* @param array $author_ary an array of author ids if the author should be ignored during the search the array is empty
* @param string $author_name specifies the author match, when ANONYMOUS is also a search-match
* @param array &$id_ary passed by reference, to be filled with ids for the page specified by $start and $per_page, should be ordered
* @param int $start indicates the first index of the page
* @param int $per_page number of ids each page is supposed to contain
* @return boolean|int total number of results
*/
public function keyword_search(string $type, string $fields, string $terms, array $sort_by_sql, string $sort_key, string $sort_dir, string $sort_days, array $ex_fid_ary, string $post_visibility, int $topic_id, array $author_ary, string $author_name, array &$id_ary, int &$start, int $per_page);
/**
* Performs a search on an author's posts without caring about message contents. Depends on display specific params
*
* @param string $type contains either posts or topics depending on what should be searched for
* @param boolean $firstpost_only if true, only topic starting posts will be considered
* @param array $sort_by_sql contains SQL code for the ORDER BY part of a query
* @param string $sort_key is the key of $sort_by_sql for the selected sorting
* @param string $sort_dir is either a or d representing ASC and DESC
* @param string $sort_days specifies the maximum amount of days a post may be old
* @param array $ex_fid_ary specifies an array of forum ids which should not be searched
* @param string $post_visibility specifies which types of posts the user can view in which forums
* @param int $topic_id is set to 0 or a topic id, if it is not 0 then only posts in this topic should be searched
* @param array $author_ary an array of author ids
* @param string $author_name specifies the author match, when ANONYMOUS is also a search-match
* @param array &$id_ary passed by reference, to be filled with ids for the page specified by $start and $per_page, should be ordered
* @param int $start indicates the first index of the page
* @param int $per_page number of ids each page is supposed to contain
* @return boolean|int total number of results
*/
public function author_search(string $type, bool $firstpost_only, array $sort_by_sql, string $sort_key, string $sort_dir, string $sort_days, array $ex_fid_ary, string $post_visibility, int $topic_id, array $author_ary, string $author_name, array &$id_ary, int &$start, int $per_page);
/**
* Returns if phrase search is supported or not
*
* @return bool
*/
public function supports_phrase_search(): bool;
/**
* Updates wordlist and wordmatch tables when a message is posted or changed
* Destroys cached search results, that contained one of the new words in a post so the results won't be outdated
*
* @param string $mode contains the post mode: edit, post, reply, quote ...
* @param int $post_id contains the post id of the post to index
* @param string $message contains the post text of the post
* @param string $subject contains the subject of the post to index
* @param int $poster_id contains the user id of the poster
* @param int $forum_id contains the forum id of parent forum of the post
*/
public function index(string $mode, int $post_id, string &$message, string &$subject, int $poster_id, int $forum_id);
/**
* Destroy cached results, that might be outdated after deleting a post
* @param array $post_ids
* @param array $author_ids
* @param array $forum_ids
*
* @return void
*/
public function index_remove(array $post_ids, array $author_ids, array $forum_ids): void;
/**
* Destroy old cache entries
*
* @return void
*/
public function tidy(): void;
/**
* Create fulltext index
*
* @param int $post_counter
* @return array|null array with current status or null if finished
*/
public function create_index(int &$post_counter = 0): ?array;
/**
* Drop fulltext index
*
* @param int $post_counter
* @return array|null array with current status or null if finished
*/
public function delete_index(int &$post_counter = 0): ?array;
/**
* Returns true if both FULLTEXT indexes exist
*
* @return bool
*/
public function index_created(): bool;
/**
* Returns an associative array containing information about the indexes
*
* @return array|false Language string of error false otherwise
*/
public function index_stats();
/**
* Display various options that can be configured for the backend from the acp
*
* @return array array containing template and config variables
*/
public function get_acp_options(): array;
}

View File

@@ -11,7 +11,7 @@
*
*/
namespace phpbb\search\sphinx;
namespace phpbb\search\backend\sphinx;
/**
* An object representing the sphinx configuration

View File

@@ -11,7 +11,7 @@
*
*/
namespace phpbb\search\sphinx;
namespace phpbb\search\backend\sphinx;
/**
* \phpbb\search\sphinx\config_comment

View File

@@ -11,7 +11,7 @@
*
*/
namespace phpbb\search\sphinx;
namespace phpbb\search\backend\sphinx;
/**
* \phpbb\search\sphinx\config_section

View File

@@ -11,7 +11,7 @@
*
*/
namespace phpbb\search\sphinx;
namespace phpbb\search\backend\sphinx;
/**
* \phpbb\search\sphinx\config_variable

View File

@@ -1,292 +0,0 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @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;
/**
* @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
*/
class base
{
var $ignore_words = array();
var $match_synonym = array();
var $replace_synonym = array();
function search_backend(&$error)
{
// This class cannot be used as a search plugin
$error = true;
}
/**
* 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)
{
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;
}
else
{
$result_count = $stored_ids[-1];
$reverse_ids = ($stored_ids[-2] != $sort_dir) ? true : false;
$complete = true;
// Change start parameter in case out of bounds
if ($result_count)
{
if ($start < 0)
{
$start = 0;
}
else if ($start >= $result_count)
{
$start = floor(($result_count - 1) / $per_page) * $per_page;
}
}
// 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
if ($reverse_ids)
{
$start = $result_count - $start - $per_page;
// the user requested a page past the last index
if ($start < 0)
{
return SEARCH_RESULT_NOT_IN_CACHE;
}
}
for ($i = $start, $n = $start + $per_page; ($i < $n) && ($i < $result_count); $i++)
{
if (!isset($stored_ids[$i]))
{
$complete = false;
}
else
{
$id_ary[] = $stored_ids[$i];
}
}
unset($stored_ids);
if ($reverse_ids)
{
$id_ary = array_reverse($id_ary);
}
if (!$complete)
{
return SEARCH_RESULT_INCOMPLETE;
}
return 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)
{
global $cache, $config, $db, $user;
$length = min(count($id_ary), $config['search_block_size']);
// nothing to cache so exit
if (!$length)
{
return;
}
$store_ids = array_slice($id_ary, 0, $length);
// create a new resultset if there is none for this search_key yet
// or add the ids to the existing resultset
if (!($store = $cache->get('_search_results_' . $search_key)))
{
// add the current keywords to the recent searches in the cache which are listed on the search page
if (!empty($keywords) || count($author_ary))
{
$sql = 'SELECT search_time
FROM ' . SEARCH_RESULTS_TABLE . '
WHERE search_key = \'' . $db->sql_escape($search_key) . '\'';
$result = $db->sql_query($sql);
if (!$db->sql_fetchrow($result))
{
$sql_ary = array(
'search_key' => $search_key,
'search_time' => time(),
'search_keywords' => $keywords,
'search_authors' => ' ' . implode(' ', $author_ary) . ' '
);
$sql = 'INSERT INTO ' . SEARCH_RESULTS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
$db->sql_query($sql);
}
$db->sql_freeresult($result);
}
$sql = 'UPDATE ' . USERS_TABLE . '
SET user_last_search = ' . time() . '
WHERE user_id = ' . $user->data['user_id'];
$db->sql_query($sql);
$store = array(-1 => $result_count, -2 => $sort_dir);
$id_range = range($start, $start + $length - 1);
}
else
{
// we use one set of results for both sort directions so we have to calculate the indizes
// for the reversed array and we also have to reverse the ids themselves
if ($store[-2] != $sort_dir)
{
$store_ids = array_reverse($store_ids);
$id_range = range($store[-1] - $start - $length, $store[-1] - $start - 1);
}
else
{
$id_range = range($start, $start + $length - 1);
}
}
$store_ids = array_combine($id_range, $store_ids);
// append the ids
if (is_array($store_ids))
{
$store += $store_ids;
// if the cache is too big
if (count($store) - 2 > 20 * $config['search_block_size'])
{
// remove everything in front of two blocks in front of the current start index
for ($i = 0, $n = $id_range[0] - 2 * $config['search_block_size']; $i < $n; $i++)
{
if (isset($store[$i]))
{
unset($store[$i]);
}
}
// remove everything after two blocks after the current stop index
end($id_range);
for ($i = $store[-1] - 1, $n = current($id_range) + 2 * $config['search_block_size']; $i > $n; $i--)
{
if (isset($store[$i]))
{
unset($store[$i]);
}
}
}
$cache->put('_search_results_' . $search_key, $store, $config['search_store_results']);
$sql = 'UPDATE ' . SEARCH_RESULTS_TABLE . '
SET search_time = ' . time() . '
WHERE search_key = \'' . $db->sql_escape($search_key) . '\'';
$db->sql_query($sql);
}
unset($store);
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.
*/
function destroy_cache($words, $authors = false)
{
global $db, $cache, $config;
// clear all searches that searched for the specified words
if (count($words))
{
$sql_where = '';
foreach ($words as $word)
{
$sql_where .= " OR search_keywords " . $db->sql_like_expression($db->get_any_char() . $word . $db->get_any_char());
}
$sql = 'SELECT search_key
FROM ' . SEARCH_RESULTS_TABLE . "
WHERE search_keywords LIKE '%*%' $sql_where";
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$cache->destroy('_search_results_' . $row['search_key']);
}
$db->sql_freeresult($result);
}
// clear all searches that searched for the specified authors
if (is_array($authors) && count($authors))
{
$sql_where = '';
foreach ($authors as $author)
{
$sql_where .= (($sql_where) ? ' OR ' : '') . 'search_authors ' . $db->sql_like_expression($db->get_any_char() . ' ' . (int) $author . ' ' . $db->get_any_char());
}
$sql = 'SELECT search_key
FROM ' . SEARCH_RESULTS_TABLE . "
WHERE $sql_where";
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$cache->destroy('_search_results_' . $row['search_key']);
}
$db->sql_freeresult($result);
}
$sql = 'DELETE
FROM ' . SEARCH_RESULTS_TABLE . '
WHERE search_time < ' . (time() - (int) $config['search_store_results']);
$db->sql_query($sql);
}
}

View File

@@ -1,10 +0,0 @@
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
</body>
</html>

View File

@@ -0,0 +1,65 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @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\di\service_collection;
use phpbb\search\backend\search_backend_interface;
class search_backend_factory
{
/**
* @var config
*/
protected $config;
/**
* @var service_collection
*/
protected $search_backends;
/**
* Constructor
*
* @param config $config
* @param service_collection $search_backends
*/
public function __construct(config $config, service_collection $search_backends)
{
$this->config = $config;
$this->search_backends = $search_backends;
}
/**
* Obtains a specified search backend
*
* @param string $class
*
* @return search_backend_interface
*/
public function get(string $class): search_backend_interface
{
return $this->search_backends->get_by_class($class);
}
/**
* Obtains active search backend
*
* @return search_backend_interface
*/
public function get_active(): search_backend_interface
{
return $this->get($this->config['search_type']);
}
}