[feature/extension-manager] Make search backends loadable from extensions

Search backends are now required to be autoloadable. The database updater to
3.1 tries to guess the class name as phpbb_search_<oldname> which works for
the default backends we ship.

PHPBB3-10323
This commit is contained in:
Nils Adermann 2011-08-15 20:00:47 -04:00
parent fb943d4d6b
commit dcc5ca5377
8 changed files with 44 additions and 46 deletions

View file

@ -77,7 +77,8 @@ class acp_search
continue; continue;
} }
$name = ucfirst(strtolower(str_replace('_', ' ', $type))); $name = $search->get_name();
$selected = ($config['search_type'] == $type) ? ' selected="selected"' : ''; $selected = ($config['search_type'] == $type) ? ' selected="selected"' : '';
$search_options .= '<option value="' . $type . '"' . $selected . '>' . $name . '</option>'; $search_options .= '<option value="' . $type . '"' . $selected . '>' . $name . '</option>';
@ -275,7 +276,7 @@ class acp_search
{ {
trigger_error($error . adm_back_link($this->u_action), E_USER_WARNING); trigger_error($error . adm_back_link($this->u_action), E_USER_WARNING);
} }
$name = ucfirst(strtolower(str_replace('_', ' ', $this->state[0]))); $name = $this->search->get_name();
$action = &$this->state[1]; $action = &$this->state[1];
@ -454,7 +455,7 @@ class acp_search
continue; continue;
} }
$name = ucfirst(strtolower(str_replace('_', ' ', $type))); $name = $search->get_name();
$data = array(); $data = array();
if (method_exists($search, 'index_stats')) if (method_exists($search, 'index_stats'))
@ -553,8 +554,19 @@ class acp_search
function get_search_types() function get_search_types()
{ {
global $phpbb_root_path, $phpEx; global $phpbb_root_path, $phpEx, $phpbb_extension_manager;
$finder = $phpbb_extension_manager->get_finder();
return $finder
->suffix('_backend')
->directory('/search')
->default_path('includes/search/')
->default_suffix('')
->default_directory('')
->get_classes();
/*
$search_types = array(); $search_types = array();
$dp = @opendir($phpbb_root_path . 'includes/search'); $dp = @opendir($phpbb_root_path . 'includes/search');
@ -574,6 +586,7 @@ class acp_search
} }
return $search_types; return $search_types;
*/
} }
function get_max_post_id() function get_max_post_id()
@ -610,14 +623,6 @@ class acp_search
{ {
global $phpbb_root_path, $phpEx, $user; global $phpbb_root_path, $phpEx, $user;
if (!preg_match('#^\w+$#', $type) || !file_exists("{$phpbb_root_path}includes/search/$type.$phpEx"))
{
$error = $user->lang['NO_SUCH_SEARCH_MODULE'];
return $error;
}
include_once("{$phpbb_root_path}includes/search/$type.$phpEx");
if (!class_exists($type)) if (!class_exists($type))
{ {
$error = $user->lang['NO_SUCH_SEARCH_MODULE']; $error = $user->lang['NO_SUCH_SEARCH_MODULE'];

View file

@ -2350,16 +2350,11 @@ function submit_post($mode, $subject, $username, $topic_type, &$poll, &$data, $u
if ($update_search_index && $data['enable_indexing']) if ($update_search_index && $data['enable_indexing'])
{ {
// Select the search method and do some additional checks to ensure it can actually be utilised // Select the search method and do some additional checks to ensure it can actually be utilised
$search_type = basename($config['search_type']); $search_type = $config['search_type'];
if (!file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx))
{
trigger_error('NO_SUCH_SEARCH_MODULE');
}
if (!class_exists($search_type)) if (!class_exists($search_type))
{ {
include("{$phpbb_root_path}includes/search/$search_type.$phpEx"); trigger_error('NO_SUCH_SEARCH_MODULE');
} }
$error = false; $error = false;

View file

@ -24,12 +24,12 @@ define('SEARCH_RESULT_IN_CACHE', 1);
define('SEARCH_RESULT_INCOMPLETE', 2); define('SEARCH_RESULT_INCOMPLETE', 2);
/** /**
* search_backend * phpbb_search_base
* optional base class for search plugins providing simple caching based on ACM * optional base class for search plugins providing simple caching based on ACM
* and functions to retrieve ignore_words and synonyms * and functions to retrieve ignore_words and synonyms
* @package search * @package search
*/ */
class search_backend class phpbb_search_base
{ {
var $ignore_words = array(); var $ignore_words = array();
var $match_synonym = array(); var $match_synonym = array();

View file

@ -16,17 +16,12 @@ if (!defined('IN_PHPBB'))
exit; exit;
} }
/**
* @ignore
*/
include_once($phpbb_root_path . 'includes/search/search.' . $phpEx);
/** /**
* fulltext_mysql * fulltext_mysql
* Fulltext search for MySQL * Fulltext search for MySQL
* @package search * @package search
*/ */
class fulltext_mysql extends search_backend class phpbb_search_fulltext_mysql extends phpbb_search_base
{ {
var $stats = array(); var $stats = array();
var $word_length = array(); var $word_length = array();
@ -36,7 +31,7 @@ class fulltext_mysql extends search_backend
var $pcre_properties = false; var $pcre_properties = false;
var $mbstring_regex = false; var $mbstring_regex = false;
function fulltext_mysql(&$error) public function __construct(&$error)
{ {
global $config; global $config;
@ -57,6 +52,11 @@ class fulltext_mysql extends search_backend
$error = false; $error = false;
} }
function get_name()
{
return 'MySQL Fulltext';
}
/** /**
* Checks for correct MySQL version and stores min/max word length in the config * Checks for correct MySQL version and stores min/max word length in the config
*/ */

View file

@ -16,17 +16,12 @@ if (!defined('IN_PHPBB'))
exit; exit;
} }
/**
* @ignore
*/
include_once($phpbb_root_path . 'includes/search/search.' . $phpEx);
/** /**
* fulltext_native * fulltext_native
* phpBB's own db driven fulltext search, version 2 * phpBB's own db driven fulltext search, version 2
* @package search * @package search
*/ */
class fulltext_native extends search_backend class phpbb_search_fulltext_native extends phpbb_search_base
{ {
var $stats = array(); var $stats = array();
var $word_length = array(); var $word_length = array();
@ -41,10 +36,8 @@ class fulltext_native extends search_backend
* Initialises the fulltext_native search backend with min/max word length and makes sure the UTF-8 normalizer is loaded. * Initialises the fulltext_native search backend with min/max word length and makes sure the UTF-8 normalizer is loaded.
* *
* @param boolean|string &$error is passed by reference and should either be set to false on success or an error message on failure. * @param boolean|string &$error is passed by reference and should either be set to false on success or an error message on failure.
*
* @access public
*/ */
function fulltext_native(&$error) public function __construct(&$error)
{ {
global $phpbb_root_path, $phpEx, $config; global $phpbb_root_path, $phpEx, $config;
@ -58,10 +51,14 @@ class fulltext_native extends search_backend
include($phpbb_root_path . 'includes/utf/utf_normalizer.' . $phpEx); include($phpbb_root_path . 'includes/utf/utf_normalizer.' . $phpEx);
} }
$error = false; $error = false;
} }
function get_name()
{
return 'phpBB Native Fulltext';
}
/** /**
* This function fills $this->search_query with the cleaned user search query. * This function fills $this->search_query with the cleaned user search query.
* *

View file

@ -2106,6 +2106,10 @@ function change_database_data(&$no_updates, $version)
// Changes from 3.1.0-dev to 3.1.0-A1 // Changes from 3.1.0-dev to 3.1.0-A1
case '3.1.0-dev': case '3.1.0-dev':
// try to guess the new auto loaded search class name
// works for native and mysql fulltext
set_config('search_type', 'phpbb_search_' . $config['search_type']);
set_config('use_system_cron', 0); set_config('use_system_cron', 0);
$sql = 'UPDATE ' . GROUPS_TABLE . ' $sql = 'UPDATE ' . GROUPS_TABLE . '

View file

@ -223,7 +223,7 @@ INSERT INTO phpbb_config (config_name, config_value) VALUES ('search_block_size'
INSERT INTO phpbb_config (config_name, config_value) VALUES ('search_gc', '7200'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('search_gc', '7200');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('search_interval', '0'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('search_interval', '0');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('search_anonymous_interval', '0'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('search_anonymous_interval', '0');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('search_type', 'fulltext_native'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('search_type', 'phpbb_search_fulltext_native');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('search_store_results', '1800'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('search_store_results', '1800');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('secure_allow_deny', '1'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('secure_allow_deny', '1');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('secure_allow_empty_referer', '1'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('secure_allow_empty_referer', '1');

View file

@ -273,15 +273,12 @@ if ($keywords || $author || $author_id || $search_id || $submit)
} }
// Select which method we'll use to obtain the post_id or topic_id information // Select which method we'll use to obtain the post_id or topic_id information
$search_type = basename($config['search_type']); $search_type = $config['search_type'];
if (!file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx)) if (!class_exists($search_type))
{ {
trigger_error('NO_SUCH_SEARCH_MODULE'); trigger_error('NO_SUCH_SEARCH_MODULE');
} }
require("{$phpbb_root_path}includes/search/$search_type.$phpEx");
// We do some additional checks in the module to ensure it can actually be utilised // We do some additional checks in the module to ensure it can actually be utilised
$error = false; $error = false;
$search = new $search_type($error); $search = new $search_type($error);