MediaWiki  1.29.1
SearchSqlite.php
Go to the documentation of this file.
1 <?php
34  return $this->db->checkForEnabledSearch();
35  }
36 
45  function parseQuery( $filteredText, $fulltext ) {
47  $lc = $this->legalSearchChars( self::CHARS_NO_SYNTAX ); // Minus syntax chars (" and *)
48  $searchon = '';
49  $this->searchTerms = [];
50 
51  $m = [];
52  if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
53  $filteredText, $m, PREG_SET_ORDER ) ) {
54  foreach ( $m as $bits ) {
55  MediaWiki\suppressWarnings();
56  list( /* all */, $modifier, $term, $nonQuoted, $wildcard ) = $bits;
57  MediaWiki\restoreWarnings();
58 
59  if ( $nonQuoted != '' ) {
60  $term = $nonQuoted;
61  $quote = '';
62  } else {
63  $term = str_replace( '"', '', $term );
64  $quote = '"';
65  }
66 
67  if ( $searchon !== '' ) {
68  $searchon .= ' ';
69  }
70 
71  // Some languages such as Serbian store the input form in the search index,
72  // so we may need to search for matches in multiple writing system variants.
73  $convertedVariants = $wgContLang->autoConvertToAllVariants( $term );
74  if ( is_array( $convertedVariants ) ) {
75  $variants = array_unique( array_values( $convertedVariants ) );
76  } else {
77  $variants = [ $term ];
78  }
79 
80  // The low-level search index does some processing on input to work
81  // around problems with minimum lengths and encoding in MySQL's
82  // fulltext engine.
83  // For Chinese this also inserts spaces between adjacent Han characters.
84  $strippedVariants = array_map(
85  [ $wgContLang, 'normalizeForSearch' ],
86  $variants );
87 
88  // Some languages such as Chinese force all variants to a canonical
89  // form when stripping to the low-level search index, so to be sure
90  // let's check our variants list for unique items after stripping.
91  $strippedVariants = array_unique( $strippedVariants );
92 
93  $searchon .= $modifier;
94  if ( count( $strippedVariants ) > 1 ) {
95  $searchon .= '(';
96  }
97  foreach ( $strippedVariants as $stripped ) {
98  if ( $nonQuoted && strpos( $stripped, ' ' ) !== false ) {
99  // Hack for Chinese: we need to toss in quotes for
100  // multiple-character phrases since normalizeForSearch()
101  // added spaces between them to make word breaks.
102  $stripped = '"' . trim( $stripped ) . '"';
103  }
104  $searchon .= "$quote$stripped$quote$wildcard ";
105  }
106  if ( count( $strippedVariants ) > 1 ) {
107  $searchon .= ')';
108  }
109 
110  // Match individual terms or quoted phrase in result highlighting...
111  // Note that variants will be introduced in a later stage for highlighting!
112  $regexp = $this->regexTerm( $term, $wildcard );
113  $this->searchTerms[] = $regexp;
114  }
115 
116  } else {
117  wfDebug( __METHOD__ . ": Can't understand search query '{$filteredText}'\n" );
118  }
119 
120  $searchon = $this->db->addQuotes( $searchon );
121  $field = $this->getIndexField( $fulltext );
122  return " $field MATCH $searchon ";
123  }
124 
125  function regexTerm( $string, $wildcard ) {
127 
128  $regex = preg_quote( $string, '/' );
129  if ( $wgContLang->hasWordBreaks() ) {
130  if ( $wildcard ) {
131  // Don't cut off the final bit!
132  $regex = "\b$regex";
133  } else {
134  $regex = "\b$regex\b";
135  }
136  } else {
137  // For Chinese, words may legitimately abut other words in the text literal.
138  // Don't add \b boundary checks... note this could cause false positives
139  // for latin chars.
140  }
141  return $regex;
142  }
143 
144  public static function legalSearchChars( $type = self::CHARS_ALL ) {
145  $searchChars = parent::legalSearchChars( $type );
146  if ( $type === self::CHARS_ALL ) {
147  // " for phrase, * for wildcard
148  $searchChars = "\"*" . $searchChars;
149  }
150  return $searchChars;
151  }
152 
159  function searchText( $term ) {
160  return $this->searchInternal( $term, true );
161  }
162 
169  function searchTitle( $term ) {
170  return $this->searchInternal( $term, false );
171  }
172 
173  protected function searchInternal( $term, $fulltext ) {
175 
176  if ( !$this->fulltextSearchSupported() ) {
177  return null;
178  }
179 
180  $filteredTerm = $this->filter( $wgContLang->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  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  function limitResult( $sql ) {
216  return $this->db->limitResult( $sql, $this->limit, $this->offset );
217  }
218 
226  function getQuery( $filteredTerm, $fulltext ) {
227  return $this->limitResult(
228  $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
229  $this->queryNamespaces()
230  );
231  }
232 
238  function getIndexField( $fulltext ) {
239  return $fulltext ? 'si_text' : 'si_title';
240  }
241 
249  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  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 }
SearchSqlite\searchTitle
searchTitle( $term)
Perform a title-only search query and return a result set.
Definition: SearchSqlite.php:169
captcha-old.count
count
Definition: captcha-old.py:225
SearchSqlite\searchText
searchText( $term)
Perform a full text search query and return a result set.
Definition: SearchSqlite.php:159
SearchEngine\$namespaces
int[] null $namespaces
Definition: SearchEngine.php:39
SearchSqlite\limitResult
limitResult( $sql)
Returns a query with limit for number of results set.
Definition: SearchSqlite.php:215
SearchSqlite\legalSearchChars
static legalSearchChars( $type=self::CHARS_ALL)
Get chars legal for search NOTE: usage as static is deprecated and preserved only as BC measure.
Definition: SearchSqlite.php:144
$term
external whereas SearchGetNearMatch runs after $term
Definition: hooks.txt:2759
$type
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition: hooks.txt:2536
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
SearchSqlite\fulltextSearchSupported
fulltextSearchSupported()
Whether fulltext search is supported by current schema.
Definition: SearchSqlite.php:33
SearchSqlite\queryMain
queryMain( $filteredTerm, $fulltext)
Get the base part of the search query.
Definition: SearchSqlite.php:249
SearchDatabase
Base search engine base class for database-backed searches.
Definition: SearchDatabase.php:31
namespaces
to move a page</td >< td > &*You are moving the page across namespaces
Definition: All_system_messages.txt:2670
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:934
SearchSqlite\regexTerm
regexTerm( $string, $wildcard)
Definition: SearchSqlite.php:125
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3060
$page
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached $page
Definition: hooks.txt:2536
SearchSqlite\getCountQuery
getCountQuery( $filteredTerm, $fulltext)
Definition: SearchSqlite.php:258
SearchSqlite\queryNamespaces
queryNamespaces()
Return a partial WHERE clause to limit the search to the given namespaces.
Definition: SearchSqlite.php:198
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
DB_MASTER
const DB_MASTER
Definition: defines.php:26
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:999
list
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
SqlSearchResultSet
This class is used for different SQL-based search engines shipped with MediaWiki.
Definition: SqlSearchResultSet.php:9
SearchSqlite\getIndexField
getIndexField( $fulltext)
Picks which field to index on, depending on what type of query.
Definition: SearchSqlite.php:238
SearchSqlite
Search engine hook for SQLite.
Definition: SearchSqlite.php:28
SearchSqlite\getQuery
getQuery( $filteredTerm, $fulltext)
Construct the full SQL query to do the search.
Definition: SearchSqlite.php:226
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
SearchSqlite\update
update( $id, $title, $text)
Create or update the search index record for the given page.
Definition: SearchSqlite.php:276
SearchSqlite\updateTitle
updateTitle( $id, $title)
Update a search index record's title only.
Definition: SearchSqlite.php:301
SearchSqlite\parseQuery
parseQuery( $filteredText, $fulltext)
Parse the user's query and transform it into an SQL fragment which will become part of a WHERE clause...
Definition: SearchSqlite.php:45
SearchDatabase\filter
filter( $text)
Return a 'cleaned up' search string.
Definition: SearchDatabase.php:55
SearchSqlite\searchInternal
searchInternal( $term, $fulltext)
Definition: SearchSqlite.php:173
$wgContLang
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the content language as $wgContLang
Definition: design.txt:56