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',
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Base search engine base class for database-backed searches.
filter( $text)
Return a 'cleaned up' search string.
Search engine hook for SQLite.
getIndexField( $fulltext)
Picks which field to index on, depending on what type of query.
searchInternal( $term, $fulltext)
parseQuery( $filteredText, $fulltext)
Parse the user's query and transform it into an SQL fragment which will become part of a WHERE clause...
fulltextSearchSupported()
Whether fulltext search is supported by current schema.
limitResult( $sql)
Returns a query with limit for number of results set.
update( $id, $title, $text)
Create or update the search index record for the given page.
updateTitle( $id, $title)
Update a search index record's title only.
getCountQuery( $filteredTerm, $fulltext)
doSearchTitleInDB( $term)
Perform a title-only search query and return a result set.
doSearchTextInDB( $term)
Perform a full text search query and return a result set.
queryNamespaces()
Return a partial WHERE clause to limit the search to the given namespaces.
regexTerm( $string, $wildcard)
queryMain( $filteredTerm, $fulltext)
Get the base part of the search query.
getQuery( $filteredTerm, $fulltext)
Construct the full SQL query to do the search.
legalSearchChars( $type=self::CHARS_ALL)
Get chars legal for search.
This class is used for different SQL-based search engines shipped with MediaWiki.