1<?php
2/**
3 * Database search engine
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Search
22 */
23
24use Wikimedia\Rdbms\IDatabase;
25use Wikimedia\Rdbms\ILoadBalancer;
26
27/**
28 * Base search engine base class for database-backed searches
29 * @stable to extend
30 * @ingroup Search
31 * @since 1.23
32 */
33abstract class SearchDatabase extends SearchEngine {
34	/** @var ILoadBalancer */
35	protected $lb;
36	/** @var IDatabase (backwards compatibility) */
37	protected $db;
38
39	/**
40	 * @var string[] search terms
41	 */
42	protected $searchTerms = [];
43
44	/**
45	 * @param ILoadBalancer $lb The load balancer for the DB cluster to search on
46	 */
47	public function __construct( ILoadBalancer $lb ) {
48		$this->lb = $lb;
49		// @TODO: remove this deprecated field in 1.35
50		$this->db = $lb->getLazyConnectionRef( DB_REPLICA ); // b/c
51	}
52
53	/**
54	 * @param string $term
55	 * @return ISearchResultSet|Status|null
56	 */
57	final public function doSearchText( $term ) {
58		return $this->doSearchTextInDB( $this->extractNamespacePrefix( $term ) );
59	}
60
61	/**
62	 * Perform a full text search query and return a result set.
63	 *
64	 * @param string $term Raw search term
65	 * @return SqlSearchResultSet|null
66	 */
67	abstract protected function doSearchTextInDB( $term );
68
69	/**
70	 * @param string $term
71	 * @return ISearchResultSet|null
72	 */
73	final public function doSearchTitle( $term ) {
74		return $this->doSearchTitleInDB( $this->extractNamespacePrefix( $term ) );
75	}
76
77	/**
78	 * Perform a title-only search query and return a result set.
79	 *
80	 * @param string $term Raw search term
81	 * @return SqlSearchResultSet|null
82	 */
83	abstract protected function doSearchTitleInDB( $term );
84
85	/**
86	 * Return a 'cleaned up' search string
87	 *
88	 * @param string $text
89	 * @return string
90	 */
91	protected function filter( $text ) {
92		// List of chars allowed in the search query.
93		// This must include chars used in the search syntax.
94		// Usually " (phrase) or * (wildcards) if supported by the engine
95		$lc = $this->legalSearchChars( self::CHARS_ALL );
96		return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
97	}
98
99	/**
100	 * Extract the optional namespace prefix and set self::namespaces
101	 * accordingly and return the query string
102	 * @param string $term
103	 * @return string the query string without any namespace prefix
104	 */
105	final protected function extractNamespacePrefix( $term ) {
106		$queryAndNs = self::parseNamespacePrefixes( $term );
107		if ( $queryAndNs === false ) {
108			return $term;
109		}
110		$this->namespaces = $queryAndNs[1];
111		return $queryAndNs[0];
112	}
113}
114