36 private function fulltextSearchSupported() {
38 $sql = (string)
$dbr->selectField(
39 $dbr->addIdentifierQuotes(
'sqlite_master' ),
41 [
'tbl_name' =>
$dbr->tableName(
'searchindex',
'raw' ) ],
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 list( , $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}'" );
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 ) );
195 $resultSet =
$dbr->query( $this->getQuery( $filteredTerm, $fulltext ), __METHOD__ );
198 $totalResult =
$dbr->query( $this->getCountQuery( $filteredTerm, $fulltext ), __METHOD__ );
199 $row = $totalResult->fetchObject();
201 $total = intval( $row->c );
203 $totalResult->free();
212 private function queryNamespaces() {
213 if ( $this->namespaces ===
null ) {
214 return ''; # search all
216 if ( $this->namespaces === [] ) {
222 return 'AND page_namespace IN (' .
$namespaces .
')';
230 private function limitResult( $sql ) {
233 return $dbr->limitResult( $sql, $this->limit, $this->offset );
243 private function getQuery( $filteredTerm, $fulltext ) {
244 return $this->limitResult(
245 $this->queryMain( $filteredTerm, $fulltext ) .
' ' .
246 $this->queryNamespaces()
255 private function getIndexField( $fulltext ) {
256 return $fulltext ?
'si_text' :
'si_title';
266 private function queryMain( $filteredTerm, $fulltext ) {
267 $match = $this->parseQuery( $filteredTerm, $fulltext );
269 $page =
$dbr->tableName(
'page' );
270 $searchindex =
$dbr->tableName(
'searchindex' );
271 return "SELECT $searchindex.rowid, page_namespace, page_title " .
272 "FROM $page,$searchindex " .
273 "WHERE page_id=$searchindex.rowid AND $match";
276 private function getCountQuery( $filteredTerm, $fulltext ) {
277 $match = $this->parseQuery( $filteredTerm, $fulltext );
279 $page =
$dbr->tableName(
'page' );
280 $searchindex =
$dbr->tableName(
'searchindex' );
281 return "SELECT COUNT(*) AS c " .
282 "FROM $page,$searchindex " .
283 "WHERE page_id=$searchindex.rowid AND $match " .
284 $this->queryNamespaces();
296 if ( !$this->fulltextSearchSupported() ) {
301 $dbw = $this->lb->getConnectionRef(
DB_PRIMARY );
302 $dbw->delete(
'searchindex', [
'rowid' => $id ], __METHOD__ );
303 $dbw->insert(
'searchindex',
319 if ( !$this->fulltextSearchSupported() ) {
323 $dbw = $this->lb->getConnectionRef(
DB_PRIMARY );
324 $dbw->update(
'searchindex',