Merge "Document SearchDatabase::doSearchTextInDB to return null"
[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 * @var string[] search terms
40 */
41 protected $searchTerms = [];
42
43 /**
44 * @param ILoadBalancer $lb The load balancer for the DB cluster to search on
45 */
46 public function __construct( ILoadBalancer $lb ) {
47 $this->lb = $lb;
48 // @TODO: remove this deprecated field in 1.35
49 $this->db = $lb->getLazyConnectionRef( DB_REPLICA ); // b/c
50 }
51
52 /**
53 * @param string $term
54 * @return ISearchResultSet|Status|null
55 */
56 final public function doSearchText( $term ) {
57 return $this->doSearchTextInDB( $this->extractNamespacePrefix( $term ) );
58 }
59
60 /**
61 * Perform a full text search query and return a result set.
62 *
63 * @param string $term Raw search term
64 * @return SqlSearchResultSet|null
65 */
66 abstract protected function doSearchTextInDB( $term );
67
68 /**
69 * @param string $term
70 * @return ISearchResultSet|null
71 */
72 final public function doSearchTitle( $term ) {
73 return $this->doSearchTitleInDB( $this->extractNamespacePrefix( $term ) );
74 }
75
76 /**
77 * Perform a title-only search query and return a result set.
78 *
79 * @param string $term Raw search term
80 * @return SqlSearchResultSet|null
81 */
82 abstract protected function doSearchTitleInDB( $term );
83
84 /**
85 * Return a 'cleaned up' search string
86 *
87 * @param string $text
88 * @return string
89 */
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 ) );
96 }
97
98 /**
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
103 */
104 final protected function extractNamespacePrefix( $term ) {
105 $queryAndNs = self::parseNamespacePrefixes( $term );
106 if ( $queryAndNs === false ) {
107 return $term;
108 }
109 $this->namespaces = $queryAndNs[1];
110 return $queryAndNs[0];
111 }
112 }