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 ) {
111 foreach ( $strippedVariants as $stripped ) {
112 if ( $nonQuoted && strpos( $stripped,
' ' ) !==
false ) {
116 $stripped =
'"' . trim( $stripped ) .
'"';
118 $searchon .=
"$quote$stripped$quote$wildcard ";
120 if ( count( $strippedVariants ) > 1 ) {
126 $regexp = $this->regexTerm( $term, $wildcard );
127 $this->searchTerms[] = $regexp;
131 wfDebug( __METHOD__ .
": Can't understand search query '{$filteredText}'" );
134 $dbr = $this->dbProvider->getReplicaDatabase();
135 $searchon = $dbr->addQuotes( $searchon );
136 $field = $this->getIndexField( $fulltext );
138 return " $field MATCH $searchon ";
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;
188 if ( !$this->fulltextSearchSupported() ) {
193 $this->
filter( MediaWikiServices::getInstance()->getContentLanguage()->lc( $term ) );
194 $dbr = $this->dbProvider->getReplicaDatabase();
196 '@phan-var IDatabase $dbr';
198 $resultSet = $dbr->query( $this->getQuery( $filteredTerm, $fulltext ), __METHOD__ );
202 $totalResult = $dbr->query( $this->getCountQuery( $filteredTerm, $fulltext ), __METHOD__ );
203 $row = $totalResult->fetchObject();
205 $total = intval( $row->c );
207 $totalResult->free();
216 private function queryNamespaces() {
217 if ( $this->namespaces ===
null ) {
218 return ''; # search all
220 if ( $this->namespaces === [] ) {
223 $dbr = $this->dbProvider->getReplicaDatabase();
226 return 'AND page_namespace IN (' .
$namespaces .
')';
234 private function limitResult( $sql ) {
235 return $this->dbProvider->getReplicaDatabase()->limitResult( $sql, $this->limit, $this->offset );
245 private function getQuery( $filteredTerm, $fulltext ) {
246 return $this->limitResult(
247 $this->queryMain( $filteredTerm, $fulltext ) .
' ' .
248 $this->queryNamespaces()
257 private function getIndexField( $fulltext ) {
258 return $fulltext ?
'si_text' :
'si_title';
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";
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();
297 public function update( $id, $title, $text ) {
298 if ( !$this->fulltextSearchSupported() ) {
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();
322 if ( !$this->fulltextSearchSupported() ) {
326 $dbw = $this->dbProvider->getPrimaryDatabase();
327 $dbw->newUpdateQueryBuilder()
328 ->update(
'searchindex' )
329 ->set( [
'si_title' => $title ] )
330 ->where( [
'rowid' => $id ] )
331 ->caller( __METHOD__ )->execute();