Merge "Document SearchDatabase::doSearchTextInDB to return null"
[lhc/web/wiklou.git] / includes / search / SearchSqlite.php
1 <?php
2 /**
3 * SQLite search backend, based upon SearchMysql
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 MediaWiki\MediaWikiServices;
25 use Wikimedia\Rdbms\DatabaseSqlite;
26
27 /**
28 * Search engine hook for SQLite
29 * @ingroup Search
30 */
31 class SearchSqlite extends SearchDatabase {
32 /**
33 * Whether fulltext search is supported by current schema
34 * @return bool
35 */
36 private function fulltextSearchSupported() {
37 // Avoid getConnectionRef() in order to get DatabaseSqlite specifically
38 /** @var DatabaseSqlite $dbr */
39 $dbr = $this->lb->getConnection( DB_REPLICA );
40 try {
41 return $dbr->checkForEnabledSearch();
42 } finally {
43 $this->lb->reuseConnection( $dbr );
44 }
45 }
46
47 /**
48 * Parse the user's query and transform it into an SQL fragment which will
49 * become part of a WHERE clause
50 *
51 * @param string $filteredText
52 * @param bool $fulltext
53 * @return string
54 */
55 private function parseQuery( $filteredText, $fulltext ) {
56 $lc = $this->legalSearchChars( self::CHARS_NO_SYNTAX ); // Minus syntax chars (" and *)
57 $searchon = '';
58 $this->searchTerms = [];
59
60 $m = [];
61 if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
62 $filteredText, $m, PREG_SET_ORDER ) ) {
63 foreach ( $m as $bits ) {
64 Wikimedia\suppressWarnings();
65 list( /* all */, $modifier, $term, $nonQuoted, $wildcard ) = $bits;
66 Wikimedia\restoreWarnings();
67
68 if ( $nonQuoted != '' ) {
69 $term = $nonQuoted;
70 $quote = '';
71 } else {
72 $term = str_replace( '"', '', $term );
73 $quote = '"';
74 }
75
76 if ( $searchon !== '' ) {
77 $searchon .= ' ';
78 }
79
80 // Some languages such as Serbian store the input form in the search index,
81 // so we may need to search for matches in multiple writing system variants.
82 $convertedVariants = MediaWikiServices::getInstance()->getContentLanguage()->
83 autoConvertToAllVariants( $term );
84 if ( is_array( $convertedVariants ) ) {
85 $variants = array_unique( array_values( $convertedVariants ) );
86 } else {
87 $variants = [ $term ];
88 }
89
90 // The low-level search index does some processing on input to work
91 // around problems with minimum lengths and encoding in MySQL's
92 // fulltext engine.
93 // For Chinese this also inserts spaces between adjacent Han characters.
94 $strippedVariants = array_map(
95 [ MediaWikiServices::getInstance()->getContentLanguage(),
96 'normalizeForSearch' ],
97 $variants );
98
99 // Some languages such as Chinese force all variants to a canonical
100 // form when stripping to the low-level search index, so to be sure
101 // let's check our variants list for unique items after stripping.
102 $strippedVariants = array_unique( $strippedVariants );
103
104 $searchon .= $modifier;
105 if ( count( $strippedVariants ) > 1 ) {
106 $searchon .= '(';
107 }
108 foreach ( $strippedVariants as $stripped ) {
109 if ( $nonQuoted && strpos( $stripped, ' ' ) !== false ) {
110 // Hack for Chinese: we need to toss in quotes for
111 // multiple-character phrases since normalizeForSearch()
112 // added spaces between them to make word breaks.
113 $stripped = '"' . trim( $stripped ) . '"';
114 }
115 $searchon .= "$quote$stripped$quote$wildcard ";
116 }
117 if ( count( $strippedVariants ) > 1 ) {
118 $searchon .= ')';
119 }
120
121 // Match individual terms or quoted phrase in result highlighting...
122 // Note that variants will be introduced in a later stage for highlighting!
123 $regexp = $this->regexTerm( $term, $wildcard );
124 $this->searchTerms[] = $regexp;
125 }
126
127 } else {
128 wfDebug( __METHOD__ . ": Can't understand search query '{$filteredText}'\n" );
129 }
130
131 $dbr = $this->lb->getConnectionRef( DB_REPLICA );
132 $searchon = $dbr->addQuotes( $searchon );
133 $field = $this->getIndexField( $fulltext );
134
135 return " $field MATCH $searchon ";
136 }
137
138 private function regexTerm( $string, $wildcard ) {
139 $regex = preg_quote( $string, '/' );
140 if ( MediaWikiServices::getInstance()->getContentLanguage()->hasWordBreaks() ) {
141 if ( $wildcard ) {
142 // Don't cut off the final bit!
143 $regex = "\b$regex";
144 } else {
145 $regex = "\b$regex\b";
146 }
147 } else {
148 // For Chinese, words may legitimately abut other words in the text literal.
149 // Don't add \b boundary checks... note this could cause false positives
150 // for Latin chars.
151 }
152 return $regex;
153 }
154
155 public function legalSearchChars( $type = self::CHARS_ALL ) {
156 $searchChars = parent::legalSearchChars( $type );
157 if ( $type === self::CHARS_ALL ) {
158 // " for phrase, * for wildcard
159 $searchChars = "\"*" . $searchChars;
160 }
161 return $searchChars;
162 }
163
164 /**
165 * Perform a full text search query and return a result set.
166 *
167 * @param string $term Raw search term
168 * @return SqlSearchResultSet|null
169 */
170 protected function doSearchTextInDB( $term ) {
171 return $this->searchInternal( $term, true );
172 }
173
174 /**
175 * Perform a title-only search query and return a result set.
176 *
177 * @param string $term Raw search term
178 * @return SqlSearchResultSet|null
179 */
180 protected function doSearchTitleInDB( $term ) {
181 return $this->searchInternal( $term, false );
182 }
183
184 protected function searchInternal( $term, $fulltext ) {
185 if ( !$this->fulltextSearchSupported() ) {
186 return null;
187 }
188
189 $filteredTerm =
190 $this->filter( MediaWikiServices::getInstance()->getContentLanguage()->lc( $term ) );
191 $dbr = $this->lb->getConnectionRef( DB_REPLICA );
192 $resultSet = $dbr->query( $this->getQuery( $filteredTerm, $fulltext ) );
193
194 $total = null;
195 $totalResult = $dbr->query( $this->getCountQuery( $filteredTerm, $fulltext ) );
196 $row = $totalResult->fetchObject();
197 if ( $row ) {
198 $total = intval( $row->c );
199 }
200 $totalResult->free();
201
202 return new SqlSearchResultSet( $resultSet, $this->searchTerms, $total );
203 }
204
205 /**
206 * Return a partial WHERE clause to limit the search to the given namespaces
207 * @return string
208 */
209 private function queryNamespaces() {
210 if ( is_null( $this->namespaces ) ) {
211 return ''; # search all
212 }
213 if ( $this->namespaces === [] ) {
214 $namespaces = '0';
215 } else {
216 $dbr = $this->lb->getConnectionRef( DB_REPLICA );
217 $namespaces = $dbr->makeList( $this->namespaces );
218 }
219 return 'AND page_namespace IN (' . $namespaces . ')';
220 }
221
222 /**
223 * Returns a query with limit for number of results set.
224 * @param string $sql
225 * @return string
226 */
227 private function limitResult( $sql ) {
228 $dbr = $this->lb->getConnectionRef( DB_REPLICA );
229
230 return $dbr->limitResult( $sql, $this->limit, $this->offset );
231 }
232
233 /**
234 * Construct the full SQL query to do the search.
235 * The guts shoulds be constructed in queryMain()
236 * @param string $filteredTerm
237 * @param bool $fulltext
238 * @return string
239 */
240 private function getQuery( $filteredTerm, $fulltext ) {
241 return $this->limitResult(
242 $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
243 $this->queryNamespaces()
244 );
245 }
246
247 /**
248 * Picks which field to index on, depending on what type of query.
249 * @param bool $fulltext
250 * @return string
251 */
252 private function getIndexField( $fulltext ) {
253 return $fulltext ? 'si_text' : 'si_title';
254 }
255
256 /**
257 * Get the base part of the search query.
258 *
259 * @param string $filteredTerm
260 * @param bool $fulltext
261 * @return string
262 */
263 private function queryMain( $filteredTerm, $fulltext ) {
264 $match = $this->parseQuery( $filteredTerm, $fulltext );
265 $dbr = $this->lb->getMaintenanceConnectionRef( DB_REPLICA );
266 $page = $dbr->tableName( 'page' );
267 $searchindex = $dbr->tableName( 'searchindex' );
268 return "SELECT $searchindex.rowid, page_namespace, page_title " .
269 "FROM $page,$searchindex " .
270 "WHERE page_id=$searchindex.rowid AND $match";
271 }
272
273 private function getCountQuery( $filteredTerm, $fulltext ) {
274 $match = $this->parseQuery( $filteredTerm, $fulltext );
275 $dbr = $this->lb->getMaintenanceConnectionRef( DB_REPLICA );
276 $page = $dbr->tableName( 'page' );
277 $searchindex = $dbr->tableName( 'searchindex' );
278 return "SELECT COUNT(*) AS c " .
279 "FROM $page,$searchindex " .
280 "WHERE page_id=$searchindex.rowid AND $match " .
281 $this->queryNamespaces();
282 }
283
284 /**
285 * Create or update the search index record for the given page.
286 * Title and text should be pre-processed.
287 *
288 * @param int $id
289 * @param string $title
290 * @param string $text
291 */
292 public function update( $id, $title, $text ) {
293 if ( !$this->fulltextSearchSupported() ) {
294 return;
295 }
296 // @todo find a method to do it in a single request,
297 // couldn't do it so far due to typelessness of FTS3 tables.
298 $dbw = $this->lb->getConnectionRef( DB_MASTER );
299 $dbw->delete( 'searchindex', [ 'rowid' => $id ], __METHOD__ );
300 $dbw->insert( 'searchindex',
301 [
302 'rowid' => $id,
303 'si_title' => $title,
304 'si_text' => $text
305 ], __METHOD__ );
306 }
307
308 /**
309 * Update a search index record's title only.
310 * Title should be pre-processed.
311 *
312 * @param int $id
313 * @param string $title
314 */
315 public function updateTitle( $id, $title ) {
316 if ( !$this->fulltextSearchSupported() ) {
317 return;
318 }
319
320 $dbw = $this->lb->getConnectionRef( DB_MASTER );
321 $dbw->update( 'searchindex',
322 [ 'si_title' => $title ],
323 [ 'rowid' => $id ],
324 __METHOD__ );
325 }
326 }