MediaWiki  master
SearchSqlite.php
Go to the documentation of this file.
1 <?php
25 use Wikimedia\AtEase\AtEase;
26 
36  private function fulltextSearchSupported() {
37  $dbr = $this->dbProvider->getReplicaDatabase();
38  $sql = (string)$dbr->newSelectQueryBuilder()
39  ->select( 'sql' )
40  ->from( $dbr->addIdentifierQuotes( 'sqlite_master' ) )
41  ->where( [ 'tbl_name' => $dbr->tableName( 'searchindex', 'raw' ) ] )
42  ->caller( __METHOD__ )->fetchField();
43 
44  return ( stristr( $sql, 'fts' ) !== false );
45  }
46 
55  private function parseQuery( $filteredText, $fulltext ) {
56  $lc = $this->legalSearchChars( self::CHARS_NO_SYNTAX ); // Minus syntax chars (" and *)
57  $searchon = '';
58  $this->searchTerms = [];
59 
60  $m = [];
61  if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
62  $filteredText, $m, PREG_SET_ORDER ) ) {
63  foreach ( $m as $bits ) {
64  AtEase::suppressWarnings();
65  [ /* all */, $modifier, $term, $nonQuoted, $wildcard ] = $bits;
66  AtEase::restoreWarnings();
67 
68  if ( $nonQuoted != '' ) {
69  $term = $nonQuoted;
70  $quote = '';
71  } else {
72  $term = str_replace( '"', '', $term );
73  $quote = '"';
74  }
75 
76  if ( $searchon !== '' ) {
77  $searchon .= ' ';
78  }
79 
80  // Some languages such as Serbian store the input form in the search index,
81  // so we may need to search for matches in multiple writing system variants.
82 
83  $converter = MediaWikiServices::getInstance()->getLanguageConverterFactory()
84  ->getLanguageConverter();
85  $convertedVariants = $converter->autoConvertToAllVariants( $term );
86  if ( is_array( $convertedVariants ) ) {
87  $variants = array_unique( array_values( $convertedVariants ) );
88  } else {
89  $variants = [ $term ];
90  }
91 
92  // The low-level search index does some processing on input to work
93  // around problems with minimum lengths and encoding in MySQL's
94  // fulltext engine.
95  // For Chinese this also inserts spaces between adjacent Han characters.
96  $strippedVariants = array_map(
97  [ MediaWikiServices::getInstance()->getContentLanguage(),
98  'normalizeForSearch' ],
99  $variants );
100 
101  // Some languages such as Chinese force all variants to a canonical
102  // form when stripping to the low-level search index, so to be sure
103  // let's check our variants list for unique items after stripping.
104  $strippedVariants = array_unique( $strippedVariants );
105 
106  $searchon .= $modifier;
107  if ( count( $strippedVariants ) > 1 ) {
108  $searchon .= '(';
109  }
110  foreach ( $strippedVariants as $stripped ) {
111  if ( $nonQuoted && strpos( $stripped, ' ' ) !== false ) {
112  // Hack for Chinese: we need to toss in quotes for
113  // multiple-character phrases since normalizeForSearch()
114  // added spaces between them to make word breaks.
115  $stripped = '"' . trim( $stripped ) . '"';
116  }
117  $searchon .= "$quote$stripped$quote$wildcard ";
118  }
119  if ( count( $strippedVariants ) > 1 ) {
120  $searchon .= ')';
121  }
122 
123  // Match individual terms or quoted phrase in result highlighting...
124  // Note that variants will be introduced in a later stage for highlighting!
125  $regexp = $this->regexTerm( $term, $wildcard );
126  $this->searchTerms[] = $regexp;
127  }
128 
129  } else {
130  wfDebug( __METHOD__ . ": Can't understand search query '{$filteredText}'" );
131  }
132 
133  $dbr = $this->dbProvider->getReplicaDatabase();
134  $searchon = $dbr->addQuotes( $searchon );
135  $field = $this->getIndexField( $fulltext );
136 
137  return " $field MATCH $searchon ";
138  }
139 
140  private function regexTerm( $string, $wildcard ) {
141  $regex = preg_quote( $string, '/' );
142  if ( MediaWikiServices::getInstance()->getContentLanguage()->hasWordBreaks() ) {
143  if ( $wildcard ) {
144  // Don't cut off the final bit!
145  $regex = "\b$regex";
146  } else {
147  $regex = "\b$regex\b";
148  }
149  } else {
150  // For Chinese, words may legitimately abut other words in the text literal.
151  // Don't add \b boundary checks... note this could cause false positives
152  // for Latin chars.
153  }
154  return $regex;
155  }
156 
157  public function legalSearchChars( $type = self::CHARS_ALL ) {
158  $searchChars = parent::legalSearchChars( $type );
159  if ( $type === self::CHARS_ALL ) {
160  // " for phrase, * for wildcard
161  $searchChars = "\"*" . $searchChars;
162  }
163  return $searchChars;
164  }
165 
172  protected function doSearchTextInDB( $term ) {
173  return $this->searchInternal( $term, true );
174  }
175 
182  protected function doSearchTitleInDB( $term ) {
183  return $this->searchInternal( $term, false );
184  }
185 
186  protected function searchInternal( $term, $fulltext ) {
187  if ( !$this->fulltextSearchSupported() ) {
188  return null;
189  }
190 
191  $filteredTerm =
192  $this->filter( MediaWikiServices::getInstance()->getContentLanguage()->lc( $term ) );
193  $dbr = $this->dbProvider->getReplicaDatabase();
194  // phpcs:ignore MediaWiki.Usage.DbrQueryUsage.DbrQueryFound
195  $resultSet = $dbr->query( $this->getQuery( $filteredTerm, $fulltext ), __METHOD__ );
196 
197  $total = null;
198  // phpcs:ignore MediaWiki.Usage.DbrQueryUsage.DbrQueryFound
199  $totalResult = $dbr->query( $this->getCountQuery( $filteredTerm, $fulltext ), __METHOD__ );
200  $row = $totalResult->fetchObject();
201  if ( $row ) {
202  $total = intval( $row->c );
203  }
204  $totalResult->free();
205 
206  return new SqlSearchResultSet( $resultSet, $this->searchTerms, $total );
207  }
208 
213  private function queryNamespaces() {
214  if ( $this->namespaces === null ) {
215  return ''; # search all
216  }
217  if ( $this->namespaces === [] ) {
219  } else {
220  $dbr = $this->dbProvider->getReplicaDatabase();
221  $namespaces = $dbr->makeList( $this->namespaces );
222  }
223  return 'AND page_namespace IN (' . $namespaces . ')';
224  }
225 
231  private function limitResult( $sql ) {
232  return $this->dbProvider->getReplicaDatabase()->limitResult( $sql, $this->limit, $this->offset );
233  }
234 
242  private function getQuery( $filteredTerm, $fulltext ) {
243  return $this->limitResult(
244  $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
245  $this->queryNamespaces()
246  );
247  }
248 
254  private function getIndexField( $fulltext ) {
255  return $fulltext ? 'si_text' : 'si_title';
256  }
257 
265  private function queryMain( $filteredTerm, $fulltext ) {
266  $match = $this->parseQuery( $filteredTerm, $fulltext );
267  $dbr = $this->dbProvider->getReplicaDatabase();
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";
273  }
274 
275  private function getCountQuery( $filteredTerm, $fulltext ) {
276  $match = $this->parseQuery( $filteredTerm, $fulltext );
277  $dbr = $this->dbProvider->getReplicaDatabase();
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 " .
283  $this->queryNamespaces();
284  }
285 
294  public function update( $id, $title, $text ) {
295  if ( !$this->fulltextSearchSupported() ) {
296  return;
297  }
298  // @todo find a method to do it in a single request,
299  // couldn't do it so far due to typelessness of FTS3 tables.
300  $dbw = $this->dbProvider->getPrimaryDatabase();
301  $dbw->newDeleteQueryBuilder()
302  ->deleteFrom( 'searchindex' )
303  ->where( [ 'rowid' => $id ] )
304  ->caller( __METHOD__ )->execute();
305  $dbw->newInsertQueryBuilder()
306  ->insertInto( 'searchindex' )
307  ->row( [ 'rowid' => $id, 'si_title' => $title, 'si_text' => $text ] )
308  ->caller( __METHOD__ )->execute();
309  }
310 
318  public function updateTitle( $id, $title ) {
319  if ( !$this->fulltextSearchSupported() ) {
320  return;
321  }
322 
323  $dbw = $this->dbProvider->getPrimaryDatabase();
324  $dbw->newUpdateQueryBuilder()
325  ->update( 'searchindex' )
326  ->set( [ 'si_title' => $title ] )
327  ->where( [ 'rowid' => $id ] )
328  ->caller( __METHOD__ )->execute();
329  }
330 }
const NS_MAIN
Definition: Defines.php:64
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Service locator for MediaWiki core services.
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.
searchInternal( $term, $fulltext)
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.
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.
legalSearchChars( $type=self::CHARS_ALL)
Get chars legal for search.
This class is used for different SQL-based search engines shipped with MediaWiki.