| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
use MediaWiki\MediaWikiServices; |
| 28 |
use Wikimedia\AtEase\AtEase; |
| 29 |
use Wikimedia\Rdbms\IDatabase; |
| 30 |
use Wikimedia\Rdbms\IExpression; |
| 31 |
use Wikimedia\Rdbms\LikeValue; |
| 32 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class SearchMySQL extends SearchDatabase { |
| 39 |
|
| 40 |
protected $strictMatching = true; |
| 41 |
|
| 42 |
|
| 43 |
private static $mMinSearchLength; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
private function parseQuery( $filteredText, $fulltext ) { |
| 55 |
$lc = $this->legalSearchChars( self::CHARS_NO_SYNTAX ); |
| 56 |
$searchon = ''; |
| 57 |
$this->searchTerms = []; |
| 58 |
|
| 59 |
|
| 60 |
$m = []; |
| 61 |
if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/', |
| 62 |
$filteredText, $m, PREG_SET_ORDER ) |
| 63 |
) { |
| 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(); |
| 71 |
|
| 72 |
if ( $nonQuoted != '' ) { |
| 73 |
$term = $nonQuoted; |
| 74 |
$quote = ''; |
| 75 |
} else { |
| 76 |
$term = str_replace( '"', '', $term ); |
| 77 |
$quote = '"'; |
| 78 |
} |
| 79 |
|
| 80 |
if ( $searchon !== '' ) { |
| 81 |
$searchon .= ' '; |
| 82 |
} |
| 83 |
if ( $this->strictMatching && ( $modifier == '' ) ) { |
| 84 |
|
| 85 |
$modifier = '+'; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
$convertedVariants = $langConverter->autoConvertToAllVariants( $term ); |
| 91 |
if ( is_array( $convertedVariants ) ) { |
| 92 |
$variants = array_unique( array_values( $convertedVariants ) ); |
| 93 |
} else { |
| 94 |
$variants = [ $term ]; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
$strippedVariants = array_map( [ $contLang, 'normalizeForSearch' ], $variants ); |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
$strippedVariants = array_unique( $strippedVariants ); |
| 107 |
|
| 108 |
$searchon .= $modifier; |
| 109 |
if ( count( $strippedVariants ) > 1 ) { |
| 110 |
$searchon .= '('; |
| 111 |
} |
| 112 |
foreach ( $strippedVariants as $stripped ) { |
| 113 |
$stripped = $this->normalizeText( $stripped ); |
| 114 |
if ( $nonQuoted && strpos( $stripped, ' ' ) !== false ) { |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
$stripped = '"' . trim( $stripped ) . '"'; |
| 119 |
} |
| 120 |
$searchon .= "$quote$stripped$quote$wildcard "; |
| 121 |
} |
| 122 |
if ( count( $strippedVariants ) > 1 ) { |
| 123 |
$searchon .= ')'; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
$regexp = $this->regexTerm( $term, $wildcard ); |
| 129 |
$this->searchTerms[] = $regexp; |
| 130 |
} |
| 131 |
wfDebug( __METHOD__ . ": Would search with '$searchon'" ); |
| 132 |
wfDebug( __METHOD__ . ': Match with /' . implode( '|', $this->searchTerms ) . "/" ); |
| 133 |
} else { |
| 134 |
wfDebug( __METHOD__ . ": Can't understand search query '{$filteredText}'" ); |
| 135 |
} |
| 136 |
|
| 137 |
$dbr = $this->dbProvider->getReplicaDatabase(); |
| 138 |
$searchon = $dbr->addQuotes( $searchon ); |
| 139 |
$field = $this->getIndexField( $fulltext ); |
| 140 |
return [ |
| 141 |
" MATCH($field) AGAINST($searchon IN BOOLEAN MODE) ", |
| 142 |
" MATCH($field) AGAINST($searchon IN NATURAL LANGUAGE MODE) DESC " |
| 143 |
]; |
| 144 |
} |
| 145 |
|
| 146 |
private function regexTerm( $string, $wildcard ) { |
| 147 |
$regex = preg_quote( $string, '/' ); |
| 148 |
if ( MediaWikiServices::getInstance()->getContentLanguage()->hasWordBreaks() ) { |
| 149 |
if ( $wildcard ) { |
| 150 |
|
| 151 |
$regex = "\b$regex"; |
| 152 |
} else { |
| 153 |
$regex = "\b$regex\b"; |
| 154 |
} |
| 155 |
} else { |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
} |
| 160 |
return $regex; |
| 161 |
} |
| 162 |
|
| 163 |
public function legalSearchChars( $type = self::CHARS_ALL ) { |
| 164 |
$searchChars = parent::legalSearchChars( $type ); |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
$searchChars = preg_replace( '/\\\\-/', '', $searchChars ); |
| 175 |
|
| 176 |
if ( $type === self::CHARS_ALL ) { |
| 177 |
|
| 178 |
$searchChars = "\"*" . $searchChars; |
| 179 |
} |
| 180 |
return $searchChars; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
protected function doSearchTextInDB( $term ) { |
| 190 |
return $this->searchInternal( $term, true ); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
protected function doSearchTitleInDB( $term ) { |
| 200 |
return $this->searchInternal( $term, false ); |
| 201 |
} |
| 202 |
|
| 203 |
protected function searchInternal( $term, $fulltext ) { |
| 204 |
|
| 205 |
if ( trim( $term ) === '' ) { |
| 206 |
return null; |
| 207 |
} |
| 208 |
|
| 209 |
$filteredTerm = $this->filter( $term ); |
| 210 |
$queryBuilder = $this->getQueryBuilder( $filteredTerm, $fulltext ); |
| 211 |
$resultSet = $queryBuilder->caller( __METHOD__ )->fetchResultSet(); |
| 212 |
|
| 213 |
$total = null; |
| 214 |
$queryBuilder = $this->getCountQueryBuilder( $filteredTerm, $fulltext ); |
| 215 |
$totalResult = $queryBuilder->caller( __METHOD__ )->fetchResultSet(); |
| 216 |
|
| 217 |
$row = $totalResult->fetchObject(); |
| 218 |
if ( $row ) { |
| 219 |
$total = intval( $row->c ); |
| 220 |
} |
| 221 |
$totalResult->free(); |
| 222 |
|
| 223 |
return new SqlSearchResultSet( $resultSet, $this->searchTerms, $total ); |
| 224 |
} |
| 225 |
|
| 226 |
public function supports( $feature ) { |
| 227 |
switch ( $feature ) { |
| 228 |
case 'title-suffix-filter': |
| 229 |
return true; |
| 230 |
default: |
| 231 |
return parent::supports( $feature ); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
protected function queryFeatures( SelectQueryBuilder $queryBuilder ) { |
| 241 |
foreach ( $this->features as $feature => $value ) { |
| 242 |
if ( $feature === 'title-suffix-filter' && $value ) { |
| 243 |
$dbr = $this->dbProvider->getReplicaDatabase(); |
| 244 |
$queryBuilder->andWhere( |
| 245 |
$dbr->expr( 'page_title', IExpression::LIKE, new LikeValue( $dbr->anyString(), $value ) ) |
| 246 |
); |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
private function queryNamespaces( $queryBuilder ) { |
| 257 |
if ( is_array( $this->namespaces ) ) { |
| 258 |
if ( count( $this->namespaces ) === 0 ) { |
| 259 |
$this->namespaces[] = NS_MAIN; |
| 260 |
} |
| 261 |
$queryBuilder->andWhere( [ 'page_namespace' => $this->namespaces ] ); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
private function getQueryBuilder( $filteredTerm, $fulltext ): SelectQueryBuilder { |
| 273 |
$queryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder(); |
| 274 |
|
| 275 |
$this->queryMain( $queryBuilder, $filteredTerm, $fulltext ); |
| 276 |
$this->queryFeatures( $queryBuilder ); |
| 277 |
$this->queryNamespaces( $queryBuilder ); |
| 278 |
$queryBuilder->limit( $this->limit ) |
| 279 |
->offset( $this->offset ); |
| 280 |
|
| 281 |
return $queryBuilder; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
private function getIndexField( $fulltext ) { |
| 290 |
return $fulltext ? 'si_text' : 'si_title'; |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
private function queryMain( SelectQueryBuilder $queryBuilder, $filteredTerm, $fulltext ) { |
| 302 |
$match = $this->parseQuery( $filteredTerm, $fulltext ); |
| 303 |
$queryBuilder->select( [ 'page_id', 'page_namespace', 'page_title' ] ) |
| 304 |
->from( 'page' ) |
| 305 |
->join( 'searchindex', null, 'page_id=si_page' ) |
| 306 |
->where( $match[0] ) |
| 307 |
->orderBy( $match[1] ); |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
private function getCountQueryBuilder( $filteredTerm, $fulltext ): SelectQueryBuilder { |
| 317 |
$match = $this->parseQuery( $filteredTerm, $fulltext ); |
| 318 |
$queryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder() |
| 319 |
->select( [ 'c' => 'COUNT(*)' ] ) |
| 320 |
->from( 'page' ) |
| 321 |
->join( 'searchindex', null, 'page_id=si_page' ) |
| 322 |
->where( $match[0] ); |
| 323 |
|
| 324 |
$this->queryFeatures( $queryBuilder ); |
| 325 |
$this->queryNamespaces( $queryBuilder ); |
| 326 |
|
| 327 |
return $queryBuilder; |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
public function update( $id, $title, $text ) { |
| 339 |
$this->dbProvider->getPrimaryDatabase()->newReplaceQueryBuilder() |
| 340 |
->replaceInto( 'searchindex' ) |
| 341 |
->uniqueIndexFields( [ 'si_page' ] ) |
| 342 |
->row( [ |
| 343 |
'si_page' => $id, |
| 344 |
'si_title' => $this->normalizeText( $title ), |
| 345 |
'si_text' => $this->normalizeText( $text ) |
| 346 |
] ) |
| 347 |
->caller( __METHOD__ )->execute(); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
public function updateTitle( $id, $title ) { |
| 358 |
$this->dbProvider->getPrimaryDatabase()->newUpdateQueryBuilder() |
| 359 |
->update( 'searchindex' ) |
| 360 |
->set( [ 'si_title' => $this->normalizeText( $title ) ] ) |
| 361 |
->where( [ 'si_page' => $id ] ) |
| 362 |
->caller( __METHOD__ )->execute(); |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
public function delete( $id, $title ) { |
| 373 |
$this->dbProvider->getPrimaryDatabase()->newDeleteQueryBuilder() |
| 374 |
->deleteFrom( 'searchindex' ) |
| 375 |
->where( [ 'si_page' => $id ] ) |
| 376 |
->caller( __METHOD__ )->execute(); |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
public function normalizeText( $string ) { |
| 386 |
$out = parent::normalizeText( $string ); |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
$out = preg_replace_callback( |
| 391 |
"/([\\xc0-\\xff][\\x80-\\xbf]*)/", |
| 392 |
[ $this, 'stripForSearchCallback' ], |
| 393 |
MediaWikiServices::getInstance()->getContentLanguage()->lc( $out ) ); |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
$minLength = $this->minSearchLength(); |
| 399 |
if ( $minLength > 1 ) { |
| 400 |
$n = $minLength - 1; |
| 401 |
$out = preg_replace( |
| 402 |
"/\b(\w{1,$n})\b/", |
| 403 |
"$1u800", |
| 404 |
$out ); |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
return preg_replace( |
| 413 |
"/(\w)\.(\w|\*)/u", |
| 414 |
"$1u82e$2", |
| 415 |
$out |
| 416 |
); |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
protected function stripForSearchCallback( $matches ) { |
| 427 |
return 'u8' . bin2hex( $matches[1] ); |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
protected function minSearchLength() { |
| 437 |
if ( self::$mMinSearchLength === null ) { |
| 438 |
$sql = "SHOW GLOBAL VARIABLES LIKE 'ft\\_min\\_word\\_len'"; |
| 439 |
|
| 440 |
$dbr = $this->dbProvider->getReplicaDatabase(); |
| 441 |
|
| 442 |
'@phan-var IDatabase $dbr'; |
| 443 |
|
| 444 |
$result = $dbr->query( $sql, __METHOD__ ); |
| 445 |
$row = $result->fetchObject(); |
| 446 |
$result->free(); |
| 447 |
|
| 448 |
if ( $row && $row->Variable_name == 'ft_min_word_len' ) { |
| 449 |
self::$mMinSearchLength = intval( $row->Value ); |
| 450 |
} else { |
| 451 |
self::$mMinSearchLength = 0; |
| 452 |
} |
| 453 |
} |
| 454 |
return self::$mMinSearchLength; |
| 455 |
} |
| 456 |
} |