3 * Database search engine
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 use Wikimedia\Rdbms\IDatabase
;
25 use Wikimedia\Rdbms\ILoadBalancer
;
28 * Base search engine base class for database-backed searches
32 abstract class SearchDatabase
extends SearchEngine
{
33 /** @var ILoadBalancer */
35 /** @var IDatabase (backwards compatibility) */
39 * @var string[] search terms
41 protected $searchTerms = [];
44 * @param ILoadBalancer $lb The load balancer for the DB cluster to search on
46 public function __construct( ILoadBalancer
$lb ) {
48 // @TODO: remove this deprecated field in 1.35
49 $this->db
= $lb->getLazyConnectionRef( DB_REPLICA
); // b/c
54 * @return ISearchResultSet|Status|null
56 final public function doSearchText( $term ) {
57 return $this->doSearchTextInDB( $this->extractNamespacePrefix( $term ) );
61 * Perform a full text search query and return a result set.
63 * @param string $term Raw search term
64 * @return SqlSearchResultSet|null
66 abstract protected function doSearchTextInDB( $term );
70 * @return ISearchResultSet|null
72 final public function doSearchTitle( $term ) {
73 return $this->doSearchTitleInDB( $this->extractNamespacePrefix( $term ) );
77 * Perform a title-only search query and return a result set.
79 * @param string $term Raw search term
80 * @return SqlSearchResultSet|null
82 abstract protected function doSearchTitleInDB( $term );
85 * Return a 'cleaned up' search string
90 protected function filter( $text ) {
91 // List of chars allowed in the search query.
92 // This must include chars used in the search syntax.
93 // Usually " (phrase) or * (wildcards) if supported by the engine
94 $lc = $this->legalSearchChars( self
::CHARS_ALL
);
95 return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
99 * Extract the optional namespace prefix and set self::namespaces
100 * accordingly and return the query string
101 * @param string $term
102 * @return string the query string without any namespace prefix
104 final protected function extractNamespacePrefix( $term ) {
105 $queryAndNs = self
::parseNamespacePrefixes( $term );
106 if ( $queryAndNs === false ) {
109 $this->namespaces
= $queryAndNs[1];
110 return $queryAndNs[0];