37 $sql = (string)
$dbr->selectField(
38 $dbr->addIdentifierQuotes(
'sqlite_master' ),
40 [
'tbl_name' =>
$dbr->tableName(
'searchindex',
'raw' ) ],
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 Wikimedia\suppressWarnings();
65 list( , $modifier, $term, $nonQuoted, $wildcard ) = $bits;
66 Wikimedia\restoreWarnings();
68 if ( $nonQuoted !=
'' ) {
72 $term = str_replace(
'"',
'', $term );
76 if ( $searchon !==
'' ) {
82 $convertedVariants = MediaWikiServices::getInstance()->getContentLanguage()->
83 autoConvertToAllVariants( $term );
84 if ( is_array( $convertedVariants ) ) {
85 $variants = array_unique( array_values( $convertedVariants ) );
87 $variants = [ $term ];
94 $strippedVariants = array_map(
95 [ MediaWikiServices::getInstance()->getContentLanguage(),
96 'normalizeForSearch' ],
102 $strippedVariants = array_unique( $strippedVariants );
104 $searchon .= $modifier;
105 if ( count( $strippedVariants ) > 1 ) {
108 foreach ( $strippedVariants as $stripped ) {
109 if ( $nonQuoted && strpos( $stripped,
' ' ) !==
false ) {
113 $stripped =
'"' . trim( $stripped ) .
'"';
115 $searchon .=
"$quote$stripped$quote$wildcard ";
117 if ( count( $strippedVariants ) > 1 ) {
123 $regexp = $this->
regexTerm( $term, $wildcard );
124 $this->searchTerms[] = $regexp;
128 wfDebug( __METHOD__ .
": Can't understand search query '{$filteredText}'\n" );
132 $searchon =
$dbr->addQuotes( $searchon );
135 return " $field MATCH $searchon ";
139 $regex = preg_quote( $string,
'/' );
140 if ( MediaWikiServices::getInstance()->getContentLanguage()->hasWordBreaks() ) {
145 $regex =
"\b$regex\b";
156 $searchChars = parent::legalSearchChars(
$type );
157 if (
$type === self::CHARS_ALL ) {
159 $searchChars =
"\"*" . $searchChars;
190 $this->
filter( MediaWikiServices::getInstance()->getContentLanguage()->lc( $term ) );
192 $resultSet =
$dbr->query( $this->
getQuery( $filteredTerm, $fulltext ) );
196 $row = $totalResult->fetchObject();
198 $total = intval( $row->c );
200 $totalResult->free();
210 if ( is_null( $this->namespaces ) ) {
211 return ''; # search all
213 if ( $this->namespaces === [] ) {
219 return 'AND page_namespace IN (' .
$namespaces .
')';
230 return $dbr->limitResult( $sql, $this->limit, $this->offset );
240 private function getQuery( $filteredTerm, $fulltext ) {
242 $this->
queryMain( $filteredTerm, $fulltext ) .
' ' .
253 return $fulltext ?
'si_text' :
'si_title';
263 private function queryMain( $filteredTerm, $fulltext ) {
264 $match = $this->
parseQuery( $filteredTerm, $fulltext );
266 $page =
$dbr->tableName(
'page' );
267 $searchindex =
$dbr->tableName(
'searchindex' );
268 return "SELECT $searchindex.rowid, page_namespace, page_title " .
269 "FROM $page,$searchindex " .
270 "WHERE page_id=$searchindex.rowid AND $match";
274 $match = $this->
parseQuery( $filteredTerm, $fulltext );
276 $page =
$dbr->tableName(
'page' );
277 $searchindex =
$dbr->tableName(
'searchindex' );
278 return "SELECT COUNT(*) AS c " .
279 "FROM $page,$searchindex " .
280 "WHERE page_id=$searchindex.rowid AND $match " .
298 $dbw = $this->lb->getConnectionRef(
DB_MASTER );
299 $dbw->delete(
'searchindex', [
'rowid' => $id ], __METHOD__ );
300 $dbw->insert(
'searchindex',
320 $dbw = $this->lb->getConnectionRef(
DB_MASTER );
321 $dbw->update(
'searchindex',