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 )
51 $contLang = $services->getContentLanguage();
52 $langConverter = $services->getLanguageConverterFactory()->getLanguageConverter( $contLang );
53 foreach ( $m as $bits ) {
55 @[ , $modifier, $term, $nonQuoted, $wildcard ] = $bits;
57 if ( $nonQuoted !=
'' ) {
61 $term = str_replace(
'"',
'', $term );
65 if ( $searchon !==
'' ) {
68 if ( $this->strictMatching && ( $modifier ==
'' ) ) {
75 $convertedVariants = $langConverter->autoConvertToAllVariants( $term );
76 if ( is_array( $convertedVariants ) ) {
77 $variants = array_unique( array_values( $convertedVariants ) );
79 $variants = [ $term ];
86 $strippedVariants = array_map( $contLang->normalizeForSearch( ... ), $variants );
91 $strippedVariants = array_unique( $strippedVariants );
93 $searchon .= $modifier;
94 if ( count( $strippedVariants ) > 1 ) {
97 foreach ( $strippedVariants as $stripped ) {
99 if ( $nonQuoted && str_contains( $stripped,
' ' ) ) {
103 $stripped =
'"' . trim( $stripped ) .
'"';
105 $searchon .=
"$quote$stripped$quote$wildcard ";
107 if ( count( $strippedVariants ) > 1 ) {
113 $regexp = $this->
regexTerm( $term, $wildcard );
114 $this->searchTerms[] = $regexp;
116 wfDebug( __METHOD__ .
": Would search with '$searchon'" );
117 wfDebug( __METHOD__ .
': Match with /' . implode(
'|', $this->searchTerms ) .
"/" );
119 wfDebug( __METHOD__ .
": Can't understand search query '{$filteredText}'" );
122 $dbr = $this->dbProvider->getReplicaDatabase();
123 $searchon = $dbr->addQuotes( $searchon );
124 $field = $this->getIndexField( $fulltext );
126 " MATCH($field) AGAINST($searchon IN BOOLEAN MODE) ",
127 " MATCH($field) AGAINST($searchon IN NATURAL LANGUAGE MODE) DESC "
133 $searchChars = parent::legalSearchChars( $type );
143 $searchChars = preg_replace(
'/\\\\-/',
'', $searchChars );
145 if ( $type === self::CHARS_ALL ) {
147 $searchChars =
"\"*" . $searchChars;
174 if ( trim( $term ) ===
'' ) {
178 $filteredTerm = $this->
filter( $term );
179 $queryBuilder = $this->getQueryBuilder( $filteredTerm, $fulltext );
180 $resultSet = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
182 $queryBuilder = $this->getCountQueryBuilder( $filteredTerm, $fulltext );
183 $total = (int)$queryBuilder->caller( __METHOD__ )->fetchField();
190 switch ( $feature ) {
191 case 'title-suffix-filter':
194 return parent::supports( $feature );
204 foreach ( $this->features as $feature => $value ) {
205 if ( $feature ===
'title-suffix-filter' && $value ) {
206 $dbr = $this->dbProvider->getReplicaDatabase();
208 $dbr->expr(
'page_title', IExpression::LIKE,
new LikeValue( $dbr->anyString(), $value ) )
219 private function queryNamespaces( $queryBuilder ) {
220 if ( is_array( $this->namespaces ) ) {
221 if ( count( $this->namespaces ) === 0 ) {
224 $queryBuilder->andWhere( [
'page_namespace' => $this->namespaces ] );
235 private function getQueryBuilder( $filteredTerm, $fulltext ): SelectQueryBuilder {
236 $queryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder();
238 $this->queryMain( $queryBuilder, $filteredTerm, $fulltext );
239 $this->queryFeatures( $queryBuilder );
240 $this->queryNamespaces( $queryBuilder );
241 $queryBuilder->limit( $this->limit )
242 ->offset( $this->offset );
244 return $queryBuilder;
252 private function getIndexField( $fulltext ) {
253 return $fulltext ?
'si_text' :
'si_title';
264 private function queryMain( SelectQueryBuilder $queryBuilder, $filteredTerm, $fulltext ) {
265 $match = $this->parseQuery( $filteredTerm, $fulltext );
266 $queryBuilder->select( [
'page_id',
'page_namespace',
'page_title' ] )
268 ->join(
'searchindex',
null,
'page_id=si_page' )
270 ->orderBy( $match[1] );
279 private function getCountQueryBuilder( $filteredTerm, $fulltext ): SelectQueryBuilder {
280 $match = $this->parseQuery( $filteredTerm, $fulltext );
281 $queryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder()
282 ->select(
'COUNT(*)' )
284 ->join(
'searchindex',
null,
'page_id=si_page' )
285 ->where( $match[0] );
287 $this->queryFeatures( $queryBuilder );
288 $this->queryNamespaces( $queryBuilder );
290 return $queryBuilder;
301 public function update( $id, $title, $text ) {
302 $this->dbProvider->getPrimaryDatabase()->newReplaceQueryBuilder()
303 ->replaceInto(
'searchindex' )
304 ->uniqueIndexFields( [
'si_page' ] )
307 'si_title' => $this->normalizeText( $title ),
308 'si_text' => $this->normalizeText( $text )
310 ->caller( __METHOD__ )->execute();
321 $this->dbProvider->getPrimaryDatabase()->newUpdateQueryBuilder()
322 ->update(
'searchindex' )
323 ->set( [
'si_title' => $this->normalizeText( $title ) ] )
324 ->where( [
'si_page' => $id ] )
325 ->caller( __METHOD__ )->execute();
335 public function delete( $id, $title ) {
336 $this->dbProvider->getPrimaryDatabase()->newDeleteQueryBuilder()
337 ->deleteFrom(
'searchindex' )
338 ->where( [
'si_page' => $id ] )
339 ->caller( __METHOD__ )->execute();
349 $out = parent::normalizeText( $string );
353 $out = preg_replace_callback(
354 "/([\\xc0-\\xff][\\x80-\\xbf]*)/",
355 $this->stripForSearchCallback( ... ),
356 MediaWikiServices::getInstance()->getContentLanguage()->lc( $out ) );
361 $minLength = $this->minSearchLength();
362 if ( $minLength > 1 ) {
390 return 'u8' . bin2hex(
$matches[1] );
400 if ( self::$mMinSearchLength ===
null ) {
401 $sql =
"SHOW GLOBAL VARIABLES LIKE 'ft\\_min\\_word\\_len'";
403 $dbr = $this->dbProvider->getReplicaDatabase();
405 '@phan-var \Wikimedia\Rdbms\IDatabase $dbr';
407 $result = $dbr->query( $sql, __METHOD__ );
408 $row = $result->fetchObject();
411 if ( $row && $row->Variable_name ==
'ft_min_word_len' ) {
412 self::$mMinSearchLength = intval( $row->Value );
414 self::$mMinSearchLength = 0;
417 return self::$mMinSearchLength;