| 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 |
use MediaWiki\MediaWikiServices; |
| 25 |
use Wikimedia\AtEase\AtEase; |
| 26 |
use Wikimedia\Rdbms\IDatabase; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class SearchSqlite extends SearchDatabase { |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
private function fulltextSearchSupported() { |
| 38 |
$dbr = $this->dbProvider->getReplicaDatabase(); |
| 39 |
$sql = (string)$dbr->newSelectQueryBuilder() |
| 40 |
->select( 'sql' ) |
| 41 |
->from( 'sqlite_master' ) |
| 42 |
->where( [ 'tbl_name' => $dbr->tableName( 'searchindex', 'raw' ) ] ) |
| 43 |
->caller( __METHOD__ )->fetchField(); |
| 44 |
|
| 45 |
return ( stristr( $sql, 'fts' ) !== false ); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
private function parseQuery( $filteredText, $fulltext ) { |
| 57 |
$lc = $this->legalSearchChars( self::CHARS_NO_SYNTAX ); |
| 58 |
$searchon = ''; |
| 59 |
$this->searchTerms = []; |
| 60 |
|
| 61 |
$m = []; |
| 62 |
if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/', |
| 63 |
$filteredText, $m, PREG_SET_ORDER ) ) { |
| 64 |
foreach ( $m as $bits ) { |
| 65 |
AtEase::suppressWarnings(); |
| 66 |
[ , $modifier, $term, $nonQuoted, $wildcard ] = $bits; |
| 67 |
AtEase::restoreWarnings(); |
| 68 |
|
| 69 |
if ( $nonQuoted != '' ) { |
| 70 |
$term = $nonQuoted; |
| 71 |
$quote = ''; |
| 72 |
} else { |
| 73 |
$term = str_replace( '"', '', $term ); |
| 74 |
$quote = '"'; |
| 75 |
} |
| 76 |
|
| 77 |
if ( $searchon !== '' ) { |
| 78 |
$searchon .= ' '; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
$converter = MediaWikiServices::getInstance()->getLanguageConverterFactory() |
| 85 |
->getLanguageConverter(); |
| 86 |
$convertedVariants = $converter->autoConvertToAllVariants( $term ); |
| 87 |
if ( is_array( $convertedVariants ) ) { |
| 88 |
$variants = array_unique( array_values( $convertedVariants ) ); |
| 89 |
} else { |
| 90 |
$variants = [ $term ]; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
$strippedVariants = array_map( |
| 98 |
[ MediaWikiServices::getInstance()->getContentLanguage(), |
| 99 |
'normalizeForSearch' ], |
| 100 |
$variants ); |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
$strippedVariants = array_unique( $strippedVariants ); |
| 106 |
|
| 107 |
$searchon .= $modifier; |
| 108 |
if ( count( $strippedVariants ) > 1 ) { |
| 109 |
$searchon .= '('; |
| 110 |
} |
| 111 |
foreach ( $strippedVariants as $stripped ) { |
| 112 |
if ( $nonQuoted && strpos( $stripped, ' ' ) !== false ) { |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
$stripped = '"' . trim( $stripped ) . '"'; |
| 117 |
} |
| 118 |
$searchon .= "$quote$stripped$quote$wildcard "; |
| 119 |
} |
| 120 |
if ( count( $strippedVariants ) > 1 ) { |
| 121 |
$searchon .= ')'; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
$regexp = $this->regexTerm( $term, $wildcard ); |
| 127 |
$this->searchTerms[] = $regexp; |
| 128 |
} |
| 129 |
|
| 130 |
} else { |
| 131 |
wfDebug( __METHOD__ . ": Can't understand search query '{$filteredText}'" ); |
| 132 |
} |
| 133 |
|
| 134 |
$dbr = $this->dbProvider->getReplicaDatabase(); |
| 135 |
$searchon = $dbr->addQuotes( $searchon ); |
| 136 |
$field = $this->getIndexField( $fulltext ); |
| 137 |
|
| 138 |
return " $field MATCH $searchon "; |
| 139 |
} |
| 140 |
|
| 141 |
private function regexTerm( $string, $wildcard ) { |
| 142 |
$regex = preg_quote( $string, '/' ); |
| 143 |
if ( MediaWikiServices::getInstance()->getContentLanguage()->hasWordBreaks() ) { |
| 144 |
if ( $wildcard ) { |
| 145 |
|
| 146 |
$regex = "\b$regex"; |
| 147 |
} else { |
| 148 |
$regex = "\b$regex\b"; |
| 149 |
} |
| 150 |
} else { |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
} |
| 155 |
return $regex; |
| 156 |
} |
| 157 |
|
| 158 |
public function legalSearchChars( $type = self::CHARS_ALL ) { |
| 159 |
$searchChars = parent::legalSearchChars( $type ); |
| 160 |
if ( $type === self::CHARS_ALL ) { |
| 161 |
|
| 162 |
$searchChars = "\"*" . $searchChars; |
| 163 |
} |
| 164 |
return $searchChars; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
protected function doSearchTextInDB( $term ) { |
| 174 |
return $this->searchInternal( $term, true ); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
protected function doSearchTitleInDB( $term ) { |
| 184 |
return $this->searchInternal( $term, false ); |
| 185 |
} |
| 186 |
|
| 187 |
protected function searchInternal( $term, $fulltext ) { |
| 188 |
if ( !$this->fulltextSearchSupported() ) { |
| 189 |
return null; |
| 190 |
} |
| 191 |
|
| 192 |
$filteredTerm = |
| 193 |
$this->filter( MediaWikiServices::getInstance()->getContentLanguage()->lc( $term ) ); |
| 194 |
$dbr = $this->dbProvider->getReplicaDatabase(); |
| 195 |
|
| 196 |
'@phan-var IDatabase $dbr'; |
| 197 |
|
| 198 |
$resultSet = $dbr->query( $this->getQuery( $filteredTerm, $fulltext ), __METHOD__ ); |
| 199 |
|
| 200 |
$total = null; |
| 201 |
|
| 202 |
$totalResult = $dbr->query( $this->getCountQuery( $filteredTerm, $fulltext ), __METHOD__ ); |
| 203 |
$row = $totalResult->fetchObject(); |
| 204 |
if ( $row ) { |
| 205 |
$total = intval( $row->c ); |
| 206 |
} |
| 207 |
$totalResult->free(); |
| 208 |
|
| 209 |
return new SqlSearchResultSet( $resultSet, $this->searchTerms, $total ); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
private function queryNamespaces() { |
| 217 |
if ( $this->namespaces === null ) { |
| 218 |
return ''; |
| 219 |
} |
| 220 |
if ( $this->namespaces === [] ) { |
| 221 |
$namespaces = NS_MAIN; |
| 222 |
} else { |
| 223 |
$dbr = $this->dbProvider->getReplicaDatabase(); |
| 224 |
$namespaces = $dbr->makeList( $this->namespaces ); |
| 225 |
} |
| 226 |
return 'AND page_namespace IN (' . $namespaces . ')'; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
private function limitResult( $sql ) { |
| 235 |
return $this->dbProvider->getReplicaDatabase()->limitResult( $sql, $this->limit, $this->offset ); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
private function getQuery( $filteredTerm, $fulltext ) { |
| 246 |
return $this->limitResult( |
| 247 |
$this->queryMain( $filteredTerm, $fulltext ) . ' ' . |
| 248 |
$this->queryNamespaces() |
| 249 |
); |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
private function getIndexField( $fulltext ) { |
| 258 |
return $fulltext ? 'si_text' : 'si_title'; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
private function queryMain( $filteredTerm, $fulltext ) { |
| 269 |
$match = $this->parseQuery( $filteredTerm, $fulltext ); |
| 270 |
$dbr = $this->dbProvider->getReplicaDatabase(); |
| 271 |
$page = $dbr->tableName( 'page' ); |
| 272 |
$searchindex = $dbr->tableName( 'searchindex' ); |
| 273 |
return "SELECT $searchindex.rowid, page_namespace, page_title " . |
| 274 |
"FROM $searchindex CROSS JOIN $page " . |
| 275 |
"WHERE page_id=$searchindex.rowid AND $match"; |
| 276 |
} |
| 277 |
|
| 278 |
private function getCountQuery( $filteredTerm, $fulltext ) { |
| 279 |
$match = $this->parseQuery( $filteredTerm, $fulltext ); |
| 280 |
$dbr = $this->dbProvider->getReplicaDatabase(); |
| 281 |
$page = $dbr->tableName( 'page' ); |
| 282 |
$searchindex = $dbr->tableName( 'searchindex' ); |
| 283 |
return "SELECT COUNT(*) AS c " . |
| 284 |
"FROM $searchindex CROSS JOIN $page " . |
| 285 |
"WHERE page_id=$searchindex.rowid AND $match " . |
| 286 |
$this->queryNamespaces(); |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
public function update( $id, $title, $text ) { |
| 298 |
if ( !$this->fulltextSearchSupported() ) { |
| 299 |
return; |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
$dbw = $this->dbProvider->getPrimaryDatabase(); |
| 304 |
$dbw->newDeleteQueryBuilder() |
| 305 |
->deleteFrom( 'searchindex' ) |
| 306 |
->where( [ 'rowid' => $id ] ) |
| 307 |
->caller( __METHOD__ )->execute(); |
| 308 |
$dbw->newInsertQueryBuilder() |
| 309 |
->insertInto( 'searchindex' ) |
| 310 |
->row( [ 'rowid' => $id, 'si_title' => $title, 'si_text' => $text ] ) |
| 311 |
->caller( __METHOD__ )->execute(); |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
public function updateTitle( $id, $title ) { |
| 322 |
if ( !$this->fulltextSearchSupported() ) { |
| 323 |
return; |
| 324 |
} |
| 325 |
|
| 326 |
$dbw = $this->dbProvider->getPrimaryDatabase(); |
| 327 |
$dbw->newUpdateQueryBuilder() |
| 328 |
->update( 'searchindex' ) |
| 329 |
->set( [ 'si_title' => $title ] ) |
| 330 |
->where( [ 'rowid' => $id ] ) |
| 331 |
->caller( __METHOD__ )->execute(); |
| 332 |
} |
| 333 |
} |