MediaWiki REL1_32
SearchSqlite.php
Go to the documentation of this file.
1<?php
25
36 return $this->db->checkForEnabledSearch();
37 }
38
47 private function parseQuery( $filteredText, $fulltext ) {
48 $lc = $this->legalSearchChars( self::CHARS_NO_SYNTAX ); // Minus syntax chars (" and *)
49 $searchon = '';
50 $this->searchTerms = [];
51
52 $m = [];
53 if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
54 $filteredText, $m, PREG_SET_ORDER ) ) {
55 foreach ( $m as $bits ) {
56 Wikimedia\suppressWarnings();
57 list( /* all */, $modifier, $term, $nonQuoted, $wildcard ) = $bits;
58 Wikimedia\restoreWarnings();
59
60 if ( $nonQuoted != '' ) {
61 $term = $nonQuoted;
62 $quote = '';
63 } else {
64 $term = str_replace( '"', '', $term );
65 $quote = '"';
66 }
67
68 if ( $searchon !== '' ) {
69 $searchon .= ' ';
70 }
71
72 // Some languages such as Serbian store the input form in the search index,
73 // so we may need to search for matches in multiple writing system variants.
74 $convertedVariants = MediaWikiServices::getInstance()->getContentLanguage()->
75 autoConvertToAllVariants( $term );
76 if ( is_array( $convertedVariants ) ) {
77 $variants = array_unique( array_values( $convertedVariants ) );
78 } else {
79 $variants = [ $term ];
80 }
81
82 // The low-level search index does some processing on input to work
83 // around problems with minimum lengths and encoding in MySQL's
84 // fulltext engine.
85 // For Chinese this also inserts spaces between adjacent Han characters.
86 $strippedVariants = array_map(
87 [ MediaWikiServices::getInstance()->getContentLanguage(),
88 'normalizeForSearch' ],
89 $variants );
90
91 // Some languages such as Chinese force all variants to a canonical
92 // form when stripping to the low-level search index, so to be sure
93 // let's check our variants list for unique items after stripping.
94 $strippedVariants = array_unique( $strippedVariants );
95
96 $searchon .= $modifier;
97 if ( count( $strippedVariants ) > 1 ) {
98 $searchon .= '(';
99 }
100 foreach ( $strippedVariants as $stripped ) {
101 if ( $nonQuoted && strpos( $stripped, ' ' ) !== false ) {
102 // Hack for Chinese: we need to toss in quotes for
103 // multiple-character phrases since normalizeForSearch()
104 // added spaces between them to make word breaks.
105 $stripped = '"' . trim( $stripped ) . '"';
106 }
107 $searchon .= "$quote$stripped$quote$wildcard ";
108 }
109 if ( count( $strippedVariants ) > 1 ) {
110 $searchon .= ')';
111 }
112
113 // Match individual terms or quoted phrase in result highlighting...
114 // Note that variants will be introduced in a later stage for highlighting!
115 $regexp = $this->regexTerm( $term, $wildcard );
116 $this->searchTerms[] = $regexp;
117 }
118
119 } else {
120 wfDebug( __METHOD__ . ": Can't understand search query '{$filteredText}'\n" );
121 }
122
123 $searchon = $this->db->addQuotes( $searchon );
124 $field = $this->getIndexField( $fulltext );
125 return " $field MATCH $searchon ";
126 }
127
128 private function regexTerm( $string, $wildcard ) {
129 $regex = preg_quote( $string, '/' );
130 if ( MediaWikiServices::getInstance()->getContentLanguage()->hasWordBreaks() ) {
131 if ( $wildcard ) {
132 // Don't cut off the final bit!
133 $regex = "\b$regex";
134 } else {
135 $regex = "\b$regex\b";
136 }
137 } else {
138 // For Chinese, words may legitimately abut other words in the text literal.
139 // Don't add \b boundary checks... note this could cause false positives
140 // for Latin chars.
141 }
142 return $regex;
143 }
144
145 public static function legalSearchChars( $type = self::CHARS_ALL ) {
146 $searchChars = parent::legalSearchChars( $type );
147 if ( $type === self::CHARS_ALL ) {
148 // " for phrase, * for wildcard
149 $searchChars = "\"*" . $searchChars;
150 }
151 return $searchChars;
152 }
153
160 protected function doSearchTextInDB( $term ) {
161 return $this->searchInternal( $term, true );
162 }
163
170 protected function doSearchTitleInDB( $term ) {
171 return $this->searchInternal( $term, false );
172 }
173
174 protected function searchInternal( $term, $fulltext ) {
175 if ( !$this->fulltextSearchSupported() ) {
176 return null;
177 }
178
179 $filteredTerm =
180 $this->filter( MediaWikiServices::getInstance()->getContentLanguage()->lc( $term ) );
181 $resultSet = $this->db->query( $this->getQuery( $filteredTerm, $fulltext ) );
182
183 $total = null;
184 $totalResult = $this->db->query( $this->getCountQuery( $filteredTerm, $fulltext ) );
185 $row = $totalResult->fetchObject();
186 if ( $row ) {
187 $total = intval( $row->c );
188 }
189 $totalResult->free();
190
191 return new SqlSearchResultSet( $resultSet, $this->searchTerms, $total );
192 }
193
198 private function queryNamespaces() {
199 if ( is_null( $this->namespaces ) ) {
200 return ''; # search all
201 }
202 if ( !count( $this->namespaces ) ) {
203 $namespaces = '0';
204 } else {
205 $namespaces = $this->db->makeList( $this->namespaces );
206 }
207 return 'AND page_namespace IN (' . $namespaces . ')';
208 }
209
215 private function limitResult( $sql ) {
216 return $this->db->limitResult( $sql, $this->limit, $this->offset );
217 }
218
226 private function getQuery( $filteredTerm, $fulltext ) {
227 return $this->limitResult(
228 $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
229 $this->queryNamespaces()
230 );
231 }
232
238 private function getIndexField( $fulltext ) {
239 return $fulltext ? 'si_text' : 'si_title';
240 }
241
249 private function queryMain( $filteredTerm, $fulltext ) {
250 $match = $this->parseQuery( $filteredTerm, $fulltext );
251 $page = $this->db->tableName( 'page' );
252 $searchindex = $this->db->tableName( 'searchindex' );
253 return "SELECT $searchindex.rowid, page_namespace, page_title " .
254 "FROM $page,$searchindex " .
255 "WHERE page_id=$searchindex.rowid AND $match";
256 }
257
258 private function getCountQuery( $filteredTerm, $fulltext ) {
259 $match = $this->parseQuery( $filteredTerm, $fulltext );
260 $page = $this->db->tableName( 'page' );
261 $searchindex = $this->db->tableName( 'searchindex' );
262 return "SELECT COUNT(*) AS c " .
263 "FROM $page,$searchindex " .
264 "WHERE page_id=$searchindex.rowid AND $match " .
265 $this->queryNamespaces();
266 }
267
276 function update( $id, $title, $text ) {
277 if ( !$this->fulltextSearchSupported() ) {
278 return;
279 }
280 // @todo find a method to do it in a single request,
281 // couldn't do it so far due to typelessness of FTS3 tables.
282 $dbw = wfGetDB( DB_MASTER );
283
284 $dbw->delete( 'searchindex', [ 'rowid' => $id ], __METHOD__ );
285
286 $dbw->insert( 'searchindex',
287 [
288 'rowid' => $id,
289 'si_title' => $title,
290 'si_text' => $text
291 ], __METHOD__ );
292 }
293
301 function updateTitle( $id, $title ) {
302 if ( !$this->fulltextSearchSupported() ) {
303 return;
304 }
305 $dbw = wfGetDB( DB_MASTER );
306
307 $dbw->update( 'searchindex',
308 [ 'si_title' => $title ],
309 [ 'rowid' => $id ],
310 __METHOD__ );
311 }
312}
to move a page</td >< td > &*You are moving the page across namespaces
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Base search engine base class for database-backed searches.
filter( $text)
Return a 'cleaned up' search string.
int[] null $namespaces
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.
static legalSearchChars( $type=self::CHARS_ALL)
Get chars legal for search NOTE: usage as static is deprecated and preserved only as BC measure.
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.
This class is used for different SQL-based search engines shipped with MediaWiki.
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
For QUnit the mediawiki tests qunit testrunner dependency will be added to any module whereas SearchGetNearMatch runs after $term
Definition hooks.txt:2926
const DB_MASTER
Definition defines.php:26