37 private function fulltextSearchSupported() {
38 $dbr = $this->dbProvider->getReplicaDatabase();
39 $sql = (string)$dbr->newSelectQueryBuilder()
41 ->from( $dbr->addIdentifierQuotes(
'sqlite_master' ) )
42 ->where( [
'tbl_name' => $dbr->tableName(
'searchindex',
'raw' ) ] )
43 ->caller( __METHOD__ )->fetchField();
45 return ( stristr( $sql,
'fts' ) !== false );
56 private function parseQuery( $filteredText, $fulltext ) {
59 $this->searchTerms = [];
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();
69 if ( $nonQuoted !=
'' ) {
73 $term = str_replace(
'"',
'', $term );
77 if ( $searchon !==
'' ) {
84 $converter = MediaWikiServices::getInstance()->getLanguageConverterFactory()
85 ->getLanguageConverter();
86 $convertedVariants = $converter->autoConvertToAllVariants( $term );
87 if ( is_array( $convertedVariants ) ) {
88 $variants = array_unique( array_values( $convertedVariants ) );
90 $variants = [ $term ];
97 $strippedVariants = array_map(
98 [ MediaWikiServices::getInstance()->getContentLanguage(),
99 'normalizeForSearch' ],
105 $strippedVariants = array_unique( $strippedVariants );
107 $searchon .= $modifier;
108 if ( count( $strippedVariants ) > 1 ) {
112 foreach ( $strippedVariants as $stripped ) {
113 if ( $nonQuoted && strpos( $stripped,
' ' ) !==
false ) {
117 $stripped =
'"' . trim( $stripped ) .
'"';
122 $searchon .=
"$quote$stripped$quote$wildcard ";
125 if ( count( $strippedVariants ) > 1 ) {
131 $regexp = $this->regexTerm( $term, $wildcard );
132 $this->searchTerms[] = $regexp;
136 wfDebug( __METHOD__ .
": Can't understand search query '{$filteredText}'" );
139 $dbr = $this->dbProvider->getReplicaDatabase();
140 $searchon = $dbr->addQuotes( $searchon );
141 $field = $this->getIndexField( $fulltext );
143 return " $field MATCH $searchon ";
146 private function regexTerm( $string, $wildcard ) {
147 $regex = preg_quote( $string,
'/' );
148 if ( MediaWikiServices::getInstance()->getContentLanguage()->hasWordBreaks() ) {
153 $regex =
"\b$regex\b";
164 $searchChars = parent::legalSearchChars( $type );
165 if ( $type === self::CHARS_ALL ) {
167 $searchChars =
"\"*" . $searchChars;
193 if ( !$this->fulltextSearchSupported() ) {
198 $this->
filter( MediaWikiServices::getInstance()->getContentLanguage()->lc( $term ) );
199 $dbr = $this->dbProvider->getReplicaDatabase();
201 '@phan-var IDatabase $dbr';
203 $resultSet = $dbr->query( $this->getQuery( $filteredTerm, $fulltext ), __METHOD__ );
207 $totalResult = $dbr->query( $this->getCountQuery( $filteredTerm, $fulltext ), __METHOD__ );
208 $row = $totalResult->fetchObject();
210 $total = intval( $row->c );
212 $totalResult->free();
221 private function queryNamespaces() {
222 if ( $this->namespaces ===
null ) {
223 return ''; # search all
225 if ( $this->namespaces === [] ) {
228 $dbr = $this->dbProvider->getReplicaDatabase();
231 return 'AND page_namespace IN (' .
$namespaces .
')';
239 private function limitResult( $sql ) {
240 return $this->dbProvider->getReplicaDatabase()->limitResult( $sql, $this->limit, $this->offset );
250 private function getQuery( $filteredTerm, $fulltext ) {
251 return $this->limitResult(
252 $this->queryMain( $filteredTerm, $fulltext ) .
' ' .
253 $this->queryNamespaces()
262 private function getIndexField( $fulltext ) {
263 return $fulltext ?
'si_text' :
'si_title';
273 private function queryMain( $filteredTerm, $fulltext ) {
274 $match = $this->parseQuery( $filteredTerm, $fulltext );
275 $dbr = $this->dbProvider->getReplicaDatabase();
276 $page = $dbr->tableName(
'page' );
277 $searchindex = $dbr->tableName(
'searchindex' );
278 return "SELECT $searchindex.rowid, page_namespace, page_title " .
279 "FROM $searchindex CROSS JOIN $page " .
280 "WHERE page_id=$searchindex.rowid AND $match";
283 private function getCountQuery( $filteredTerm, $fulltext ) {
284 $match = $this->parseQuery( $filteredTerm, $fulltext );
285 $dbr = $this->dbProvider->getReplicaDatabase();
286 $page = $dbr->tableName(
'page' );
287 $searchindex = $dbr->tableName(
'searchindex' );
288 return "SELECT COUNT(*) AS c " .
289 "FROM $searchindex CROSS JOIN $page " .
290 "WHERE page_id=$searchindex.rowid AND $match " .
291 $this->queryNamespaces();
302 public function update( $id, $title, $text ) {
303 if ( !$this->fulltextSearchSupported() ) {
308 $dbw = $this->dbProvider->getPrimaryDatabase();
309 $dbw->newDeleteQueryBuilder()
310 ->deleteFrom(
'searchindex' )
311 ->where( [
'rowid' => $id ] )
312 ->caller( __METHOD__ )->execute();
313 $dbw->newInsertQueryBuilder()
314 ->insertInto(
'searchindex' )
315 ->row( [
'rowid' => $id,
'si_title' => $title,
'si_text' => $text ] )
316 ->caller( __METHOD__ )->execute();
327 if ( !$this->fulltextSearchSupported() ) {
331 $dbw = $this->dbProvider->getPrimaryDatabase();
332 $dbw->newUpdateQueryBuilder()
333 ->update(
'searchindex' )
334 ->set( [
'si_title' => $title ] )
335 ->where( [
'rowid' => $id ] )
336 ->caller( __METHOD__ )->execute();