29 private static $mMinSearchLength;
40 private function parseQuery( $filteredText, $fulltext ) {
43 $this->searchTerms = [];
45 # @todo FIXME: This doesn't handle parenthetical expressions.
47 if ( preg_match_all(
'/([-+<>~]?)(([' . $lc .
']+)(\*?)|"[^"]*")/',
48 $filteredText, $m, PREG_SET_ORDER )
50 $services = MediaWikiServices::getInstance();
51 $contLang = $services->getContentLanguage();
52 $langConverter = $services->getLanguageConverterFactory()->getLanguageConverter( $contLang );
53 foreach ( $m as $bits ) {
54 AtEase::suppressWarnings();
55 [ , $modifier, $term, $nonQuoted, $wildcard ] = $bits;
56 AtEase::restoreWarnings();
58 if ( $nonQuoted !=
'' ) {
62 $term = str_replace(
'"',
'', $term );
66 if ( $searchon !==
'' ) {
69 if ( $this->strictMatching && ( $modifier ==
'' ) ) {
76 $convertedVariants = $langConverter->autoConvertToAllVariants( $term );
77 if ( is_array( $convertedVariants ) ) {
78 $variants = array_unique( array_values( $convertedVariants ) );
80 $variants = [ $term ];
87 $strippedVariants = array_map( $contLang->normalizeForSearch( ... ), $variants );
92 $strippedVariants = array_unique( $strippedVariants );
94 $searchon .= $modifier;
95 if ( count( $strippedVariants ) > 1 ) {
98 foreach ( $strippedVariants as $stripped ) {
100 if ( $nonQuoted && str_contains( $stripped,
' ' ) ) {
104 $stripped =
'"' . trim( $stripped ) .
'"';
106 $searchon .=
"$quote$stripped$quote$wildcard ";
108 if ( count( $strippedVariants ) > 1 ) {
114 $regexp = $this->regexTerm( $term, $wildcard );
115 $this->searchTerms[] = $regexp;
117 wfDebug( __METHOD__ .
": Would search with '$searchon'" );
118 wfDebug( __METHOD__ .
': Match with /' . implode(
'|', $this->searchTerms ) .
"/" );
120 wfDebug( __METHOD__ .
": Can't understand search query '{$filteredText}'" );
123 $dbr = $this->dbProvider->getReplicaDatabase();
124 $searchon = $dbr->addQuotes( $searchon );
125 $field = $this->getIndexField( $fulltext );
127 " MATCH($field) AGAINST($searchon IN BOOLEAN MODE) ",
128 " MATCH($field) AGAINST($searchon IN NATURAL LANGUAGE MODE) DESC "
132 private function regexTerm(
string $string, ?
string $wildcard ): string {
133 $regex = preg_quote( $string,
'/' );
134 if ( MediaWikiServices::getInstance()->getContentLanguage()->hasWordBreaks() ) {
139 $regex =
"\b$regex\b";
151 $searchChars = parent::legalSearchChars( $type );
161 $searchChars = preg_replace(
'/\\\\-/',
'', $searchChars );
163 if ( $type === self::CHARS_ALL ) {
165 $searchChars =
"\"*" . $searchChars;
177 return $this->searchInternal( $term,
true );
187 return $this->searchInternal( $term,
false );
192 if ( trim( $term ) ===
'' ) {
196 $filteredTerm = $this->filter( $term );
197 $queryBuilder = $this->getQueryBuilder( $filteredTerm, $fulltext );
198 $resultSet = $queryBuilder->
caller( __METHOD__ )->fetchResultSet();
200 $queryBuilder = $this->getCountQueryBuilder( $filteredTerm, $fulltext );
201 $total = (int)$queryBuilder->
caller( __METHOD__ )->fetchField();
208 switch ( $feature ) {
209 case 'title-suffix-filter':
212 return parent::supports( $feature );
222 foreach ( $this->features as $feature => $value ) {
223 if ( $feature ===
'title-suffix-filter' && $value ) {
224 $dbr = $this->dbProvider->getReplicaDatabase();
226 $dbr->expr(
'page_title', IExpression::LIKE,
new LikeValue( $dbr->anyString(), $value ) )
237 private function queryNamespaces( $queryBuilder ) {
238 if ( is_array( $this->namespaces ) ) {
239 if ( count( $this->namespaces ) === 0 ) {
242 $queryBuilder->
andWhere( [
'page_namespace' => $this->namespaces ] );
254 $queryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder();
256 $this->queryMain( $queryBuilder, $filteredTerm, $fulltext );
257 $this->queryFeatures( $queryBuilder );
258 $this->queryNamespaces( $queryBuilder );
259 $queryBuilder->
limit( $this->limit )
260 ->offset( $this->offset );
262 return $queryBuilder;
270 private function getIndexField( $fulltext ) {
271 return $fulltext ?
'si_text' :
'si_title';
282 private function queryMain(
SelectQueryBuilder $queryBuilder, $filteredTerm, $fulltext ) {
283 $match = $this->parseQuery( $filteredTerm, $fulltext );
284 $queryBuilder->
select( [
'page_id',
'page_namespace',
'page_title' ] )
286 ->join(
'searchindex',
null,
'page_id=si_page' )
288 ->orderBy( $match[1] );
297 private function getCountQueryBuilder( $filteredTerm, $fulltext ):
SelectQueryBuilder {
298 $match = $this->parseQuery( $filteredTerm, $fulltext );
299 $queryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder()
302 ->join(
'searchindex',
null,
'page_id=si_page' )
303 ->where( $match[0] );
305 $this->queryFeatures( $queryBuilder );
306 $this->queryNamespaces( $queryBuilder );
308 return $queryBuilder;
319 public function update( $id, $title, $text ) {
320 $this->dbProvider->getPrimaryDatabase()->newReplaceQueryBuilder()
321 ->replaceInto(
'searchindex' )
322 ->uniqueIndexFields( [
'si_page' ] )
325 'si_title' => $this->normalizeText( $title ),
326 'si_text' => $this->normalizeText( $text )
328 ->caller( __METHOD__ )->execute();
339 $this->dbProvider->getPrimaryDatabase()->newUpdateQueryBuilder()
340 ->update(
'searchindex' )
341 ->set( [
'si_title' => $this->normalizeText( $title ) ] )
342 ->where( [
'si_page' => $id ] )
343 ->caller( __METHOD__ )->execute();
353 public function delete( $id, $title ) {
354 $this->dbProvider->getPrimaryDatabase()->newDeleteQueryBuilder()
355 ->deleteFrom(
'searchindex' )
356 ->where( [
'si_page' => $id ] )
357 ->caller( __METHOD__ )->execute();
367 $out = parent::normalizeText( $string );
371 $out = preg_replace_callback(
372 "/([\\xc0-\\xff][\\x80-\\xbf]*)/",
373 $this->stripForSearchCallback( ... ),
374 MediaWikiServices::getInstance()->getContentLanguage()->lc( $out ) );
379 $minLength = $this->minSearchLength();
380 if ( $minLength > 1 ) {
408 return 'u8' . bin2hex(
$matches[1] );
418 if ( self::$mMinSearchLength ===
null ) {
419 $sql =
"SHOW GLOBAL VARIABLES LIKE 'ft\\_min\\_word\\_len'";
421 $dbr = $this->dbProvider->getReplicaDatabase();
423 '@phan-var IDatabase $dbr';
425 $result = $dbr->query( $sql, __METHOD__ );
426 $row = $result->fetchObject();
429 if ( $row && $row->Variable_name ==
'ft_min_word_len' ) {
430 self::$mMinSearchLength = intval( $row->Value );
432 self::$mMinSearchLength = 0;
435 return self::$mMinSearchLength;