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 $string, ?
string $wildcard ): string {
147 $regex = preg_quote( $string,
'/' );
148 if ( MediaWikiServices::getInstance()->getContentLanguage()->hasWordBreaks() ) {
153 $regex =
"\b$regex\b";
164 $searchChars = parent::legalSearchChars( $type );
174 $searchChars = preg_replace(
'/\\\\-/',
'', $searchChars );
176 if ( $type === self::CHARS_ALL ) {
178 $searchChars =
"\"*" . $searchChars;
190 return $this->searchInternal( $term,
true );
200 return $this->searchInternal( $term,
false );
205 if ( trim( $term ) ===
'' ) {
209 $filteredTerm = $this->filter( $term );
210 $queryBuilder = $this->getQueryBuilder( $filteredTerm, $fulltext );
211 $resultSet = $queryBuilder->
caller( __METHOD__ )->fetchResultSet();
214 $queryBuilder = $this->getCountQueryBuilder( $filteredTerm, $fulltext );
215 $totalResult = $queryBuilder->
caller( __METHOD__ )->fetchResultSet();
217 $row = $totalResult->fetchObject();
219 $total = intval( $row->c );
221 $totalResult->free();
227 switch ( $feature ) {
228 case 'title-suffix-filter':
231 return parent::supports( $feature );
241 foreach ( $this->features as $feature => $value ) {
242 if ( $feature ===
'title-suffix-filter' && $value ) {
243 $dbr = $this->dbProvider->getReplicaDatabase();
245 $dbr->expr(
'page_title', IExpression::LIKE,
new LikeValue( $dbr->anyString(), $value ) )
256 private function queryNamespaces( $queryBuilder ) {
257 if ( is_array( $this->namespaces ) ) {
258 if ( count( $this->namespaces ) === 0 ) {
261 $queryBuilder->
andWhere( [
'page_namespace' => $this->namespaces ] );
273 $queryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder();
275 $this->queryMain( $queryBuilder, $filteredTerm, $fulltext );
276 $this->queryFeatures( $queryBuilder );
277 $this->queryNamespaces( $queryBuilder );
278 $queryBuilder->
limit( $this->limit )
279 ->offset( $this->offset );
281 return $queryBuilder;
289 private function getIndexField( $fulltext ) {
290 return $fulltext ?
'si_text' :
'si_title';
301 private function queryMain(
SelectQueryBuilder $queryBuilder, $filteredTerm, $fulltext ) {
302 $match = $this->parseQuery( $filteredTerm, $fulltext );
303 $queryBuilder->
select( [
'page_id',
'page_namespace',
'page_title' ] )
305 ->join(
'searchindex',
null,
'page_id=si_page' )
307 ->orderBy( $match[1] );
316 private function getCountQueryBuilder( $filteredTerm, $fulltext ):
SelectQueryBuilder {
317 $match = $this->parseQuery( $filteredTerm, $fulltext );
318 $queryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder()
319 ->
select( [
'c' =>
'COUNT(*)' ] )
321 ->join(
'searchindex',
null,
'page_id=si_page' )
322 ->where( $match[0] );
324 $this->queryFeatures( $queryBuilder );
325 $this->queryNamespaces( $queryBuilder );
327 return $queryBuilder;
338 public function update( $id, $title, $text ) {
339 $this->dbProvider->getPrimaryDatabase()->newReplaceQueryBuilder()
340 ->replaceInto(
'searchindex' )
341 ->uniqueIndexFields( [
'si_page' ] )
344 'si_title' => $this->normalizeText( $title ),
345 'si_text' => $this->normalizeText( $text )
347 ->caller( __METHOD__ )->execute();
358 $this->dbProvider->getPrimaryDatabase()->newUpdateQueryBuilder()
359 ->update(
'searchindex' )
360 ->set( [
'si_title' => $this->normalizeText( $title ) ] )
361 ->where( [
'si_page' => $id ] )
362 ->caller( __METHOD__ )->execute();
372 public function delete( $id, $title ) {
373 $this->dbProvider->getPrimaryDatabase()->newDeleteQueryBuilder()
374 ->deleteFrom(
'searchindex' )
375 ->where( [
'si_page' => $id ] )
376 ->caller( __METHOD__ )->execute();
386 $out = parent::normalizeText( $string );
390 $out = preg_replace_callback(
391 "/([\\xc0-\\xff][\\x80-\\xbf]*)/",
392 [ $this,
'stripForSearchCallback' ],
393 MediaWikiServices::getInstance()->getContentLanguage()->lc( $out ) );
398 $minLength = $this->minSearchLength();
399 if ( $minLength > 1 ) {
427 return 'u8' . bin2hex(
$matches[1] );
437 if ( self::$mMinSearchLength ===
null ) {
438 $sql =
"SHOW GLOBAL VARIABLES LIKE 'ft\\_min\\_word\\_len'";
440 $dbr = $this->dbProvider->getReplicaDatabase();
442 '@phan-var IDatabase $dbr';
444 $result = $dbr->query( $sql, __METHOD__ );
445 $row = $result->fetchObject();
448 if ( $row && $row->Variable_name ==
'ft_min_word_len' ) {
449 self::$mMinSearchLength = intval( $row->Value );
451 self::$mMinSearchLength = 0;
454 return self::$mMinSearchLength;