41 private static $mMinSearchLength;
52 private function parseQuery( $filteredText, $fulltext ) {
55 $this->searchTerms = [];
57 # @todo FIXME: This doesn't handle parenthetical expressions.
59 if ( preg_match_all(
'/([-+<>~]?)(([' . $lc .
']+)(\*?)|"[^"]*")/',
60 $filteredText, $m, PREG_SET_ORDER )
62 $services = MediaWikiServices::getInstance();
63 $contLang = $services->getContentLanguage();
64 $langConverter = $services->getLanguageConverterFactory()->getLanguageConverter( $contLang );
65 foreach ( $m as $bits ) {
66 AtEase::suppressWarnings();
67 [ , $modifier, $term, $nonQuoted, $wildcard ] = $bits;
68 AtEase::restoreWarnings();
70 if ( $nonQuoted !=
'' ) {
74 $term = str_replace(
'"',
'', $term );
78 if ( $searchon !==
'' ) {
81 if ( $this->strictMatching && ( $modifier ==
'' ) ) {
88 $convertedVariants = $langConverter->autoConvertToAllVariants( $term );
89 if ( is_array( $convertedVariants ) ) {
90 $variants = array_unique( array_values( $convertedVariants ) );
92 $variants = [ $term ];
99 $strippedVariants = array_map( [ $contLang,
'normalizeForSearch' ], $variants );
104 $strippedVariants = array_unique( $strippedVariants );
106 $searchon .= $modifier;
107 if ( count( $strippedVariants ) > 1 ) {
110 foreach ( $strippedVariants as $stripped ) {
112 if ( $nonQuoted && strpos( $stripped,
' ' ) !==
false ) {
116 $stripped =
'"' . trim( $stripped ) .
'"';
118 $searchon .=
"$quote$stripped$quote$wildcard ";
120 if ( count( $strippedVariants ) > 1 ) {
126 $regexp = $this->regexTerm( $term, $wildcard );
127 $this->searchTerms[] = $regexp;
129 wfDebug( __METHOD__ .
": Would search with '$searchon'" );
130 wfDebug( __METHOD__ .
': Match with /' . implode(
'|', $this->searchTerms ) .
"/" );
132 wfDebug( __METHOD__ .
": Can't understand search query '{$filteredText}'" );
135 $dbr = $this->dbProvider->getReplicaDatabase();
136 $searchon = $dbr->addQuotes( $searchon );
137 $field = $this->getIndexField( $fulltext );
139 " MATCH($field) AGAINST($searchon IN BOOLEAN MODE) ",
140 " MATCH($field) AGAINST($searchon IN NATURAL LANGUAGE MODE) DESC "
144 private function regexTerm( $string, $wildcard ) {
145 $regex = preg_quote( $string,
'/' );
146 if ( MediaWikiServices::getInstance()->getContentLanguage()->hasWordBreaks() ) {
151 $regex =
"\b$regex\b";
162 $searchChars = parent::legalSearchChars( $type );
163 if ( $type === self::CHARS_ALL ) {
165 $searchChars =
"\"*" . $searchChars;
192 if ( trim( $term ) ===
'' ) {
196 $filteredTerm = $this->
filter( $term );
198 $resultSet = $queryBuilder->
caller( __METHOD__ )->fetchResultSet();
201 $queryBuilder = $this->getCountQueryBuilder( $filteredTerm, $fulltext );
202 $totalResult = $queryBuilder->
caller( __METHOD__ )->fetchResultSet();
204 $row = $totalResult->fetchObject();
206 $total = intval( $row->c );
208 $totalResult->free();
214 switch ( $feature ) {
215 case 'title-suffix-filter':
218 return parent::supports( $feature );
228 foreach ( $this->features as $feature => $value ) {
229 if ( $feature ===
'title-suffix-filter' && $value ) {
230 $dbr = $this->dbProvider->getReplicaDatabase();
232 $dbr->expr(
'page_title', IExpression::LIKE,
new LikeValue( $dbr->anyString(), $value ) )
243 private function queryNamespaces( $queryBuilder ) {
244 if ( is_array( $this->namespaces ) ) {
245 if ( count( $this->namespaces ) === 0 ) {
248 $queryBuilder->
andWhere( [
'page_namespace' => $this->namespaces ] );
260 $queryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder();
262 $this->queryMain( $queryBuilder, $filteredTerm, $fulltext );
264 $this->queryNamespaces( $queryBuilder );
265 $queryBuilder->
limit( $this->limit )
266 ->offset( $this->offset );
268 return $queryBuilder;
276 private function getIndexField( $fulltext ) {
277 return $fulltext ?
'si_text' :
'si_title';
288 private function queryMain(
SelectQueryBuilder $queryBuilder, $filteredTerm, $fulltext ) {
289 $match = $this->parseQuery( $filteredTerm, $fulltext );
290 $queryBuilder->
select( [
'page_id',
'page_namespace',
'page_title' ] )
292 ->join(
'searchindex',
null,
'page_id=si_page' )
294 ->orderBy( $match[1] );
303 private function getCountQueryBuilder( $filteredTerm, $fulltext ):
SelectQueryBuilder {
304 $match = $this->parseQuery( $filteredTerm, $fulltext );
305 $queryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder()
306 ->
select( [
'c' =>
'COUNT(*)' ] )
308 ->join(
'searchindex',
null,
'page_id=si_page' )
309 ->where( $match[0] );
311 $this->queryFeatures( $queryBuilder );
312 $this->queryNamespaces( $queryBuilder );
314 return $queryBuilder;
325 public function update( $id, $title, $text ) {
326 $this->dbProvider->getPrimaryDatabase()->newReplaceQueryBuilder()
327 ->replaceInto(
'searchindex' )
328 ->uniqueIndexFields( [
'si_page' ] )
331 'si_title' => $this->normalizeText( $title ),
332 'si_text' => $this->normalizeText( $text )
334 ->caller( __METHOD__ )->execute();
345 $this->dbProvider->getPrimaryDatabase()->newUpdateQueryBuilder()
346 ->update(
'searchindex' )
347 ->set( [
'si_title' => $this->normalizeText( $title ) ] )
348 ->where( [
'si_page' => $id ] )
349 ->caller( __METHOD__ )->execute();
359 public function delete( $id, $title ) {
360 $this->dbProvider->getPrimaryDatabase()->newDeleteQueryBuilder()
361 ->deleteFrom(
'searchindex' )
362 ->where( [
'si_page' => $id ] )
363 ->caller( __METHOD__ )->execute();
373 $out = parent::normalizeText( $string );
377 $out = preg_replace_callback(
378 "/([\\xc0-\\xff][\\x80-\\xbf]*)/",
379 [ $this,
'stripForSearchCallback' ],
380 MediaWikiServices::getInstance()->getContentLanguage()->lc( $out ) );
385 $minLength = $this->minSearchLength();
386 if ( $minLength > 1 ) {
414 return 'u8' . bin2hex(
$matches[1] );
424 if ( self::$mMinSearchLength ===
null ) {
425 $sql =
"SHOW GLOBAL VARIABLES LIKE 'ft\\_min\\_word\\_len'";
427 $dbr = $this->dbProvider->getReplicaDatabase();
429 '@phan-var IDatabase $dbr';
431 $result = $dbr->query( $sql, __METHOD__ );
432 $row = $result->fetchObject();
435 if ( $row && $row->Variable_name ==
'ft_min_word_len' ) {
436 self::$mMinSearchLength = intval( $row->Value );
438 self::$mMinSearchLength = 0;
441 return self::$mMinSearchLength;