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 !==
'' ) {
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}'" );
134 $searchon =
$dbr->addQuotes( $searchon );
137 return " $field MATCH $searchon ";
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;
192 $this->
filter( MediaWikiServices::getInstance()->getContentLanguage()->lc( $term ) );
194 $resultSet =
$dbr->query( $this->
getQuery( $filteredTerm, $fulltext ), __METHOD__ );
197 $totalResult =
$dbr->query( $this->
getCountQuery( $filteredTerm, $fulltext ), __METHOD__ );
198 $row = $totalResult->fetchObject();
200 $total = intval( $row->c );
202 $totalResult->free();
212 if ( $this->namespaces ===
null ) {
213 return ''; # search all
215 if ( $this->namespaces === [] ) {
221 return 'AND page_namespace IN (' .
$namespaces .
')';
232 return $dbr->limitResult( $sql, $this->limit, $this->offset );
242 private function getQuery( $filteredTerm, $fulltext ) {
244 $this->
queryMain( $filteredTerm, $fulltext ) .
' ' .
255 return $fulltext ?
'si_text' :
'si_title';
265 private function queryMain( $filteredTerm, $fulltext ) {
266 $match = $this->
parseQuery( $filteredTerm, $fulltext );
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";
276 $match = $this->
parseQuery( $filteredTerm, $fulltext );
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 " .
300 $dbw = $this->lb->getConnectionRef(
DB_PRIMARY );
301 $dbw->delete(
'searchindex', [
'rowid' => $id ], __METHOD__ );
302 $dbw->insert(
'searchindex',
322 $dbw = $this->lb->getConnectionRef(
DB_PRIMARY );
323 $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.