36 private function fulltextSearchSupported() {
37 $dbr = $this->dbProvider->getReplicaDatabase();
38 $sql = (string)$dbr->newSelectQueryBuilder()
40 ->from( $dbr->addIdentifierQuotes(
'sqlite_master' ) )
41 ->where( [
'tbl_name' => $dbr->tableName(
'searchindex',
'raw' ) ] )
42 ->caller( __METHOD__ )->fetchField();
44 return ( stristr( $sql,
'fts' ) !== false );
55 private function parseQuery( $filteredText, $fulltext ) {
58 $this->searchTerms = [];
61 if ( preg_match_all(
'/([-+<>~]?)(([' . $lc .
']+)(\*?)|"[^"]*")/',
62 $filteredText, $m, PREG_SET_ORDER ) ) {
63 foreach ( $m as $bits ) {
64 AtEase::suppressWarnings();
65 [ , $modifier, $term, $nonQuoted, $wildcard ] = $bits;
66 AtEase::restoreWarnings();
68 if ( $nonQuoted !=
'' ) {
72 $term = str_replace(
'"',
'', $term );
76 if ( $searchon !==
'' ) {
83 $converter = MediaWikiServices::getInstance()->getLanguageConverterFactory()
84 ->getLanguageConverter();
85 $convertedVariants = $converter->autoConvertToAllVariants( $term );
86 if ( is_array( $convertedVariants ) ) {
87 $variants = array_unique( array_values( $convertedVariants ) );
89 $variants = [ $term ];
96 $strippedVariants = array_map(
97 [ MediaWikiServices::getInstance()->getContentLanguage(),
98 'normalizeForSearch' ],
104 $strippedVariants = array_unique( $strippedVariants );
106 $searchon .= $modifier;
107 if ( count( $strippedVariants ) > 1 ) {
110 foreach ( $strippedVariants as $stripped ) {
111 if ( $nonQuoted && strpos( $stripped,
' ' ) !==
false ) {
115 $stripped =
'"' . trim( $stripped ) .
'"';
117 $searchon .=
"$quote$stripped$quote$wildcard ";
119 if ( count( $strippedVariants ) > 1 ) {
125 $regexp = $this->regexTerm( $term, $wildcard );
126 $this->searchTerms[] = $regexp;
130 wfDebug( __METHOD__ .
": Can't understand search query '{$filteredText}'" );
133 $dbr = $this->dbProvider->getReplicaDatabase();
134 $searchon = $dbr->addQuotes( $searchon );
135 $field = $this->getIndexField( $fulltext );
137 return " $field MATCH $searchon ";
140 private function regexTerm( $string, $wildcard ) {
141 $regex = preg_quote( $string,
'/' );
142 if ( MediaWikiServices::getInstance()->getContentLanguage()->hasWordBreaks() ) {
147 $regex =
"\b$regex\b";
158 $searchChars = parent::legalSearchChars( $type );
159 if ( $type === self::CHARS_ALL ) {
161 $searchChars =
"\"*" . $searchChars;
187 if ( !$this->fulltextSearchSupported() ) {
192 $this->
filter( MediaWikiServices::getInstance()->getContentLanguage()->lc( $term ) );
193 $dbr = $this->dbProvider->getReplicaDatabase();
195 $resultSet = $dbr->query( $this->getQuery( $filteredTerm, $fulltext ), __METHOD__ );
199 $totalResult = $dbr->query( $this->getCountQuery( $filteredTerm, $fulltext ), __METHOD__ );
200 $row = $totalResult->fetchObject();
202 $total = intval( $row->c );
204 $totalResult->free();
213 private function queryNamespaces() {
214 if ( $this->namespaces ===
null ) {
215 return ''; # search all
217 if ( $this->namespaces === [] ) {
220 $dbr = $this->dbProvider->getReplicaDatabase();
223 return 'AND page_namespace IN (' .
$namespaces .
')';
231 private function limitResult( $sql ) {
232 return $this->dbProvider->getReplicaDatabase()->limitResult( $sql, $this->limit, $this->offset );
242 private function getQuery( $filteredTerm, $fulltext ) {
243 return $this->limitResult(
244 $this->queryMain( $filteredTerm, $fulltext ) .
' ' .
245 $this->queryNamespaces()
254 private function getIndexField( $fulltext ) {
255 return $fulltext ?
'si_text' :
'si_title';
265 private function queryMain( $filteredTerm, $fulltext ) {
266 $match = $this->parseQuery( $filteredTerm, $fulltext );
267 $dbr = $this->dbProvider->getReplicaDatabase();
268 $page = $dbr->tableName(
'page' );
269 $searchindex = $dbr->tableName(
'searchindex' );
270 return "SELECT $searchindex.rowid, page_namespace, page_title " .
271 "FROM $page,$searchindex " .
272 "WHERE page_id=$searchindex.rowid AND $match";
275 private function getCountQuery( $filteredTerm, $fulltext ) {
276 $match = $this->parseQuery( $filteredTerm, $fulltext );
277 $dbr = $this->dbProvider->getReplicaDatabase();
278 $page = $dbr->tableName(
'page' );
279 $searchindex = $dbr->tableName(
'searchindex' );
280 return "SELECT COUNT(*) AS c " .
281 "FROM $page,$searchindex " .
282 "WHERE page_id=$searchindex.rowid AND $match " .
283 $this->queryNamespaces();
294 public function update( $id, $title, $text ) {
295 if ( !$this->fulltextSearchSupported() ) {
300 $dbw = $this->dbProvider->getPrimaryDatabase();
301 $dbw->newDeleteQueryBuilder()
302 ->deleteFrom(
'searchindex' )
303 ->where( [
'rowid' => $id ] )
304 ->caller( __METHOD__ )->execute();
305 $dbw->newInsertQueryBuilder()
306 ->insertInto(
'searchindex' )
307 ->row( [
'rowid' => $id,
'si_title' => $title,
'si_text' => $text ] )
308 ->caller( __METHOD__ )->execute();
319 if ( !$this->fulltextSearchSupported() ) {
323 $dbw = $this->dbProvider->getPrimaryDatabase();
324 $dbw->newUpdateQueryBuilder()
325 ->update(
'searchindex' )
326 ->set( [
'si_title' => $title ] )
327 ->where( [
'rowid' => $id ] )
328 ->caller( __METHOD__ )->execute();