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