43 private static $mMinSearchLength;
54 private function parseQuery( $filteredText, $fulltext ) {
57 $this->searchTerms = [];
59 # @todo FIXME: This doesn't handle parenthetical expressions.
61 if ( preg_match_all(
'/([-+<>~]?)(([' . $lc .
']+)(\*?)|"[^"]*")/',
62 $filteredText, $m, PREG_SET_ORDER )
64 $services = MediaWikiServices::getInstance();
65 $contLang = $services->getContentLanguage();
66 $langConverter = $services->getLanguageConverterFactory()->getLanguageConverter( $contLang );
67 foreach ( $m as $bits ) {
68 AtEase::suppressWarnings();
69 [ , $modifier, $term, $nonQuoted, $wildcard ] = $bits;
70 AtEase::restoreWarnings();
72 if ( $nonQuoted !=
'' ) {
76 $term = str_replace(
'"',
'', $term );
80 if ( $searchon !==
'' ) {
83 if ( $this->strictMatching && ( $modifier ==
'' ) ) {
90 $convertedVariants = $langConverter->autoConvertToAllVariants( $term );
91 if ( is_array( $convertedVariants ) ) {
92 $variants = array_unique( array_values( $convertedVariants ) );
94 $variants = [ $term ];
101 $strippedVariants = array_map( [ $contLang,
'normalizeForSearch' ], $variants );
106 $strippedVariants = array_unique( $strippedVariants );
108 $searchon .= $modifier;
109 if ( count( $strippedVariants ) > 1 ) {
112 foreach ( $strippedVariants as $stripped ) {
114 if ( $nonQuoted && strpos( $stripped,
' ' ) !==
false ) {
118 $stripped =
'"' . trim( $stripped ) .
'"';
120 $searchon .=
"$quote$stripped$quote$wildcard ";
122 if ( count( $strippedVariants ) > 1 ) {
128 $regexp = $this->regexTerm( $term, $wildcard );
129 $this->searchTerms[] = $regexp;
131 wfDebug( __METHOD__ .
": Would search with '$searchon'" );
132 wfDebug( __METHOD__ .
': Match with /' . implode(
'|', $this->searchTerms ) .
"/" );
134 wfDebug( __METHOD__ .
": Can't understand search query '{$filteredText}'" );
137 $dbr = $this->dbProvider->getReplicaDatabase();
138 $searchon = $dbr->addQuotes( $searchon );
139 $field = $this->getIndexField( $fulltext );
141 " MATCH($field) AGAINST($searchon IN BOOLEAN MODE) ",
142 " MATCH($field) AGAINST($searchon IN NATURAL LANGUAGE MODE) DESC "
146 private function regexTerm( $string, $wildcard ) {
147 $regex = preg_quote( $string,
'/' );
148 if ( MediaWikiServices::getInstance()->getContentLanguage()->hasWordBreaks() ) {
153 $regex =
"\b$regex\b";
164 $searchChars = parent::legalSearchChars( $type );
165 if ( $type === self::CHARS_ALL ) {
167 $searchChars =
"\"*" . $searchChars;
194 if ( trim( $term ) ===
'' ) {
198 $filteredTerm = $this->
filter( $term );
199 $queryBuilder = $this->getQueryBuilder( $filteredTerm, $fulltext );
200 $resultSet = $queryBuilder->
caller( __METHOD__ )->fetchResultSet();
203 $queryBuilder = $this->getCountQueryBuilder( $filteredTerm, $fulltext );
204 $totalResult = $queryBuilder->
caller( __METHOD__ )->fetchResultSet();
206 $row = $totalResult->fetchObject();
208 $total = intval( $row->c );
210 $totalResult->free();
216 switch ( $feature ) {
217 case 'title-suffix-filter':
220 return parent::supports( $feature );
230 foreach ( $this->features as $feature => $value ) {
231 if ( $feature ===
'title-suffix-filter' && $value ) {
232 $dbr = $this->dbProvider->getReplicaDatabase();
234 $dbr->expr(
'page_title', IExpression::LIKE,
new LikeValue( $dbr->anyString(), $value ) )
245 private function queryNamespaces( $queryBuilder ) {
246 if ( is_array( $this->namespaces ) ) {
247 if ( count( $this->namespaces ) === 0 ) {
250 $queryBuilder->
andWhere( [
'page_namespace' => $this->namespaces ] );
262 $queryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder();
264 $this->queryMain( $queryBuilder, $filteredTerm, $fulltext );
266 $this->queryNamespaces( $queryBuilder );
267 $queryBuilder->
limit( $this->limit )
268 ->offset( $this->offset );
270 return $queryBuilder;
278 private function getIndexField( $fulltext ) {
279 return $fulltext ?
'si_text' :
'si_title';
290 private function queryMain(
SelectQueryBuilder $queryBuilder, $filteredTerm, $fulltext ) {
291 $match = $this->parseQuery( $filteredTerm, $fulltext );
292 $queryBuilder->
select( [
'page_id',
'page_namespace',
'page_title' ] )
294 ->join(
'searchindex',
null,
'page_id=si_page' )
296 ->orderBy( $match[1] );
305 private function getCountQueryBuilder( $filteredTerm, $fulltext ):
SelectQueryBuilder {
306 $match = $this->parseQuery( $filteredTerm, $fulltext );
307 $queryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder()
308 ->
select( [
'c' =>
'COUNT(*)' ] )
310 ->join(
'searchindex',
null,
'page_id=si_page' )
311 ->where( $match[0] );
313 $this->queryFeatures( $queryBuilder );
314 $this->queryNamespaces( $queryBuilder );
316 return $queryBuilder;
327 public function update( $id, $title, $text ) {
328 $this->dbProvider->getPrimaryDatabase()->newReplaceQueryBuilder()
329 ->replaceInto(
'searchindex' )
330 ->uniqueIndexFields( [
'si_page' ] )
333 'si_title' => $this->normalizeText( $title ),
334 'si_text' => $this->normalizeText( $text )
336 ->caller( __METHOD__ )->execute();
347 $this->dbProvider->getPrimaryDatabase()->newUpdateQueryBuilder()
348 ->update(
'searchindex' )
349 ->set( [
'si_title' => $this->normalizeText( $title ) ] )
350 ->where( [
'si_page' => $id ] )
351 ->caller( __METHOD__ )->execute();
361 public function delete( $id, $title ) {
362 $this->dbProvider->getPrimaryDatabase()->newDeleteQueryBuilder()
363 ->deleteFrom(
'searchindex' )
364 ->where( [
'si_page' => $id ] )
365 ->caller( __METHOD__ )->execute();
375 $out = parent::normalizeText( $string );
379 $out = preg_replace_callback(
380 "/([\\xc0-\\xff][\\x80-\\xbf]*)/",
381 [ $this,
'stripForSearchCallback' ],
382 MediaWikiServices::getInstance()->getContentLanguage()->lc( $out ) );
387 $minLength = $this->minSearchLength();
388 if ( $minLength > 1 ) {
416 return 'u8' . bin2hex(
$matches[1] );
426 if ( self::$mMinSearchLength ===
null ) {
427 $sql =
"SHOW GLOBAL VARIABLES LIKE 'ft\\_min\\_word\\_len'";
429 $dbr = $this->dbProvider->getReplicaDatabase();
431 '@phan-var IDatabase $dbr';
433 $result = $dbr->query( $sql, __METHOD__ );
434 $row = $result->fetchObject();
437 if ( $row && $row->Variable_name ==
'ft_min_word_len' ) {
438 self::$mMinSearchLength = intval( $row->Value );
440 self::$mMinSearchLength = 0;
443 return self::$mMinSearchLength;