38 private static $mMinSearchLength;
49 private function parseQuery( $filteredText, $fulltext ) {
52 $this->searchTerms = [];
54 # @todo FIXME: This doesn't handle parenthetical expressions.
56 if ( preg_match_all(
'/([-+<>~]?)(([' . $lc .
']+)(\*?)|"[^"]*")/',
57 $filteredText, $m, PREG_SET_ORDER )
59 $services = MediaWikiServices::getInstance();
60 $contLang = $services->getContentLanguage();
61 $langConverter = $services->getLanguageConverterFactory()->getLanguageConverter( $contLang );
62 foreach ( $m as $bits ) {
63 AtEase::suppressWarnings();
64 [ , $modifier, $term, $nonQuoted, $wildcard ] = $bits;
65 AtEase::restoreWarnings();
67 if ( $nonQuoted !=
'' ) {
71 $term = str_replace(
'"',
'', $term );
75 if ( $searchon !==
'' ) {
78 if ( $this->strictMatching && ( $modifier ==
'' ) ) {
85 $convertedVariants = $langConverter->autoConvertToAllVariants( $term );
86 if ( is_array( $convertedVariants ) ) {
87 $variants = array_unique( array_values( $convertedVariants ) );
89 $variants = [ $term ];
96 $strippedVariants = array_map( [ $contLang,
'normalizeForSearch' ], $variants );
101 $strippedVariants = array_unique( $strippedVariants );
103 $searchon .= $modifier;
104 if ( count( $strippedVariants ) > 1 ) {
107 foreach ( $strippedVariants as $stripped ) {
109 if ( $nonQuoted && strpos( $stripped,
' ' ) !==
false ) {
113 $stripped =
'"' . trim( $stripped ) .
'"';
115 $searchon .=
"$quote$stripped$quote$wildcard ";
117 if ( count( $strippedVariants ) > 1 ) {
123 $regexp = $this->regexTerm( $term, $wildcard );
124 $this->searchTerms[] = $regexp;
126 wfDebug( __METHOD__ .
": Would search with '$searchon'" );
127 wfDebug( __METHOD__ .
': Match with /' . implode(
'|', $this->searchTerms ) .
"/" );
129 wfDebug( __METHOD__ .
": Can't understand search query '{$filteredText}'" );
132 $dbr = $this->dbProvider->getReplicaDatabase();
133 $searchon = $dbr->addQuotes( $searchon );
134 $field = $this->getIndexField( $fulltext );
136 " MATCH($field) AGAINST($searchon IN BOOLEAN MODE) ",
137 " MATCH($field) AGAINST($searchon IN NATURAL LANGUAGE MODE) DESC "
141 private function regexTerm( $string, $wildcard ) {
142 $regex = preg_quote( $string,
'/' );
143 if ( MediaWikiServices::getInstance()->getContentLanguage()->hasWordBreaks() ) {
148 $regex =
"\b$regex\b";
159 $searchChars = parent::legalSearchChars( $type );
160 if ( $type === self::CHARS_ALL ) {
162 $searchChars =
"\"*" . $searchChars;
189 if ( trim( $term ) ===
'' ) {
193 $filteredTerm = $this->
filter( $term );
194 $queryBuilder = $this->getQueryBuilder( $filteredTerm, $fulltext );
195 $resultSet = $queryBuilder->
caller( __METHOD__ )->fetchResultSet();
198 $queryBuilder = $this->getCountQueryBuilder( $filteredTerm, $fulltext );
199 $totalResult = $queryBuilder->
caller( __METHOD__ )->fetchResultSet();
201 $row = $totalResult->fetchObject();
203 $total = intval( $row->c );
205 $totalResult->free();
211 switch ( $feature ) {
212 case 'title-suffix-filter':
215 return parent::supports( $feature );
225 foreach ( $this->features as $feature => $value ) {
226 if ( $feature ===
'title-suffix-filter' && $value ) {
227 $dbr = $this->dbProvider->getReplicaDatabase();
228 $queryBuilder->
andWhere(
'page_title' . $dbr->buildLike( $dbr->anyString(), $value ) );
238 private function queryNamespaces( $queryBuilder ) {
239 if ( is_array( $this->namespaces ) ) {
240 if ( count( $this->namespaces ) === 0 ) {
243 $queryBuilder->
andWhere( [
'page_namespace' => $this->namespaces ] );
255 $queryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder();
257 $this->queryMain( $queryBuilder, $filteredTerm, $fulltext );
259 $this->queryNamespaces( $queryBuilder );
260 $queryBuilder->
limit( $this->limit )
261 ->offset( $this->offset );
263 return $queryBuilder;
271 private function getIndexField( $fulltext ) {
272 return $fulltext ?
'si_text' :
'si_title';
283 private function queryMain(
SelectQueryBuilder $queryBuilder, $filteredTerm, $fulltext ) {
284 $match = $this->parseQuery( $filteredTerm, $fulltext );
285 $queryBuilder->
select( [
'page_id',
'page_namespace',
'page_title' ] )
287 ->join(
'searchindex',
null,
'page_id=si_page' )
289 ->orderBy( $match[1] );
298 private function getCountQueryBuilder( $filteredTerm, $fulltext ):
SelectQueryBuilder {
299 $match = $this->parseQuery( $filteredTerm, $fulltext );
300 $queryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder()
301 ->
select( [
'c' =>
'COUNT(*)' ] )
303 ->join(
'searchindex',
null,
'page_id=si_page' )
304 ->where( $match[0] );
306 $this->queryFeatures( $queryBuilder );
307 $this->queryNamespaces( $queryBuilder );
309 return $queryBuilder;
320 public function update( $id, $title, $text ) {
321 $this->dbProvider->getPrimaryDatabase()->newReplaceQueryBuilder()
322 ->replaceInto(
'searchindex' )
323 ->uniqueIndexFields( [
'si_page' ] )
326 'si_title' => $this->normalizeText( $title ),
327 'si_text' => $this->normalizeText( $text )
329 ->caller( __METHOD__ )->execute();
340 $this->dbProvider->getPrimaryDatabase()->newUpdateQueryBuilder()
341 ->update(
'searchindex' )
342 ->set( [
'si_title' => $this->normalizeText( $title ) ] )
343 ->where( [
'si_page' => $id ] )
344 ->caller( __METHOD__ )->execute();
354 public function delete( $id, $title ) {
355 $this->dbProvider->getPrimaryDatabase()->newDeleteQueryBuilder()
356 ->deleteFrom(
'searchindex' )
357 ->where( [
'si_page' => $id ] )
358 ->caller( __METHOD__ )->execute();
368 $out = parent::normalizeText( $string );
372 $out = preg_replace_callback(
373 "/([\\xc0-\\xff][\\x80-\\xbf]*)/",
374 [ $this,
'stripForSearchCallback' ],
375 MediaWikiServices::getInstance()->getContentLanguage()->lc( $out ) );
380 $minLength = $this->minSearchLength();
381 if ( $minLength > 1 ) {
409 return 'u8' . bin2hex(
$matches[1] );
419 if ( self::$mMinSearchLength ===
null ) {
420 $sql =
"SHOW GLOBAL VARIABLES LIKE 'ft\\_min\\_word\\_len'";
422 $dbr = $this->dbProvider->getReplicaDatabase();
424 $result = $dbr->query( $sql, __METHOD__ );
425 $row = $result->fetchObject();
428 if ( $row && $row->Variable_name ==
'ft_min_word_len' ) {
429 self::$mMinSearchLength = intval( $row->Value );
431 self::$mMinSearchLength = 0;
434 return self::$mMinSearchLength;