MediaWiki  REL1_31
SearchOracle.php
Go to the documentation of this file.
1 <?php
32  private $reservedWords = [
33  'ABOUT' => 1,
34  'ACCUM' => 1,
35  'AND' => 1,
36  'BT' => 1,
37  'BTG' => 1,
38  'BTI' => 1,
39  'BTP' => 1,
40  'FUZZY' => 1,
41  'HASPATH' => 1,
42  'INPATH' => 1,
43  'MINUS' => 1,
44  'NEAR' => 1,
45  'NOT' => 1,
46  'NT' => 1,
47  'NTG' => 1,
48  'NTI' => 1,
49  'NTP' => 1,
50  'OR' => 1,
51  'PT' => 1,
52  'RT' => 1,
53  'SQE' => 1,
54  'SYN' => 1,
55  'TR' => 1,
56  'TRSYN' => 1,
57  'TT' => 1,
58  'WITHIN' => 1,
59  ];
60 
67  function searchText( $term ) {
68  if ( $term == '' ) {
69  return new SqlSearchResultSet( false, '' );
70  }
71 
72  $resultSet = $this->db->query( $this->getQuery( $this->filter( $term ), true ) );
73  return new SqlSearchResultSet( $resultSet, $this->searchTerms );
74  }
75 
82  function searchTitle( $term ) {
83  if ( $term == '' ) {
84  return new SqlSearchResultSet( false, '' );
85  }
86 
87  $resultSet = $this->db->query( $this->getQuery( $this->filter( $term ), false ) );
88  return new SqlSearchResultSet( $resultSet, $this->searchTerms );
89  }
90 
95  function queryNamespaces() {
96  if ( is_null( $this->namespaces ) ) {
97  return '';
98  }
99  if ( !count( $this->namespaces ) ) {
100  $namespaces = '0';
101  } else {
102  $namespaces = $this->db->makeList( $this->namespaces );
103  }
104  return 'AND page_namespace IN (' . $namespaces . ')';
105  }
106 
114  function queryLimit( $sql ) {
115  return $this->db->limitResult( $sql, $this->limit, $this->offset );
116  }
117 
126  function queryRanking( $filteredTerm, $fulltext ) {
127  return ' ORDER BY score(1)';
128  }
129 
137  function getQuery( $filteredTerm, $fulltext ) {
138  return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
139  $this->queryNamespaces() . ' ' .
140  $this->queryRanking( $filteredTerm, $fulltext ) . ' ' );
141  }
142 
148  function getIndexField( $fulltext ) {
149  return $fulltext ? 'si_text' : 'si_title';
150  }
151 
159  function queryMain( $filteredTerm, $fulltext ) {
160  $match = $this->parseQuery( $filteredTerm, $fulltext );
161  $page = $this->db->tableName( 'page' );
162  $searchindex = $this->db->tableName( 'searchindex' );
163  return 'SELECT page_id, page_namespace, page_title ' .
164  "FROM $page,$searchindex " .
165  'WHERE page_id=si_page AND ' . $match;
166  }
167 
175  function parseQuery( $filteredText, $fulltext ) {
177  $lc = $this->legalSearchChars( self::CHARS_NO_SYNTAX );
178  $this->searchTerms = [];
179 
180  # @todo FIXME: This doesn't handle parenthetical expressions.
181  $m = [];
182  $searchon = '';
183  if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
184  $filteredText, $m, PREG_SET_ORDER ) ) {
185  foreach ( $m as $terms ) {
186  // Search terms in all variant forms, only
187  // apply on wiki with LanguageConverter
188  $temp_terms = $wgContLang->autoConvertToAllVariants( $terms[2] );
189  if ( is_array( $temp_terms ) ) {
190  $temp_terms = array_unique( array_values( $temp_terms ) );
191  foreach ( $temp_terms as $t ) {
192  $searchon .= ( $terms[1] == '-' ? ' ~' : ' & ' ) . $this->escapeTerm( $t );
193  }
194  } else {
195  $searchon .= ( $terms[1] == '-' ? ' ~' : ' & ' ) . $this->escapeTerm( $terms[2] );
196  }
197  if ( !empty( $terms[3] ) ) {
198  $regexp = preg_quote( $terms[3], '/' );
199  if ( $terms[4] ) {
200  $regexp .= "[0-9A-Za-z_]+";
201  }
202  } else {
203  $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
204  }
205  $this->searchTerms[] = $regexp;
206  }
207  }
208 
209  $searchon = $this->db->addQuotes( ltrim( $searchon, ' &' ) );
210  $field = $this->getIndexField( $fulltext );
211  return " CONTAINS($field, $searchon, 1) > 0 ";
212  }
213 
214  private function escapeTerm( $t ) {
216  $t = $wgContLang->normalizeForSearch( $t );
217  $t = isset( $this->reservedWords[strtoupper( $t )] ) ? '{' . $t . '}' : $t;
218  $t = preg_replace( '/^"(.*)"$/', '($1)', $t );
219  $t = preg_replace( '/([-&|])/', '\\\\$1', $t );
220  return $t;
221  }
222 
231  function update( $id, $title, $text ) {
232  $dbw = wfGetDB( DB_MASTER );
233  $dbw->replace( 'searchindex',
234  [ 'si_page' ],
235  [
236  'si_page' => $id,
237  'si_title' => $title,
238  'si_text' => $text
239  ], 'SearchOracle::update' );
240 
241  // Sync the index
242  // We need to specify the DB name (i.e. user/schema) here so that
243  // it can work from the installer, where
244  // ALTER SESSION SET CURRENT_SCHEMA = ...
245  // was used.
246  $dbw->query( "CALL ctx_ddl.sync_index(" .
247  $dbw->addQuotes( $dbw->getDBname() . '.' . $dbw->tableName( 'si_text_idx', 'raw' ) ) . ")" );
248  $dbw->query( "CALL ctx_ddl.sync_index(" .
249  $dbw->addQuotes( $dbw->getDBname() . '.' . $dbw->tableName( 'si_title_idx', 'raw' ) ) . ")" );
250  }
251 
259  function updateTitle( $id, $title ) {
260  $dbw = wfGetDB( DB_MASTER );
261 
262  $dbw->update( 'searchindex',
263  [ 'si_title' => $title ],
264  [ 'si_page' => $id ],
265  'SearchOracle::updateTitle',
266  [] );
267  }
268 
269  public static function legalSearchChars( $type = self::CHARS_ALL ) {
270  $searchChars = parent::legalSearchChars( $type );
271  if ( $type === self::CHARS_ALL ) {
272  $searchChars = "\"" . $searchChars;
273  }
274  return $searchChars;
275  }
276 }
SearchOracle\updateTitle
updateTitle( $id, $title)
Update a search index record's title only.
Definition: SearchOracle.php:259
SearchEngine\$namespaces
int[] null $namespaces
Definition: SearchEngine.php:39
SearchOracle
Search engine hook base class for Oracle (ConText).
Definition: SearchOracle.php:31
SearchOracle\parseQuery
parseQuery( $filteredText, $fulltext)
Parse a user input search string, and return an SQL fragment to be used as part of a WHERE clause.
Definition: SearchOracle.php:175
SearchOracle\getQuery
getQuery( $filteredTerm, $fulltext)
Construct the full SQL query to do the search.
Definition: SearchOracle.php:137
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:37
SearchDatabase
Base search engine base class for database-backed searches.
Definition: SearchDatabase.php:31
$term
For QUnit the mediawiki tests qunit testrunner dependency will be added to any module whereas SearchGetNearMatch runs after $term
Definition: hooks.txt:2845
namespaces
to move a page</td >< td > &*You are moving the page across namespaces
Definition: All_system_messages.txt:2670
SearchOracle\searchText
searchText( $term)
Perform a full text search query and return a result set.
Definition: SearchOracle.php:67
SearchOracle\update
update( $id, $title, $text)
Create or update the search index record for the given page.
Definition: SearchOracle.php:231
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2812
SearchOracle\queryRanking
queryRanking( $filteredTerm, $fulltext)
Does not do anything for generic search engine subclasses may define this though.
Definition: SearchOracle.php:126
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:964
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:95
DB_MASTER
const DB_MASTER
Definition: defines.php:26
SearchOracle\queryMain
queryMain( $filteredTerm, $fulltext)
Get the base part of the search query.
Definition: SearchOracle.php:159
SearchOracle\queryLimit
queryLimit( $sql)
Return a LIMIT clause to limit results on the query.
Definition: SearchOracle.php:114
SearchOracle\searchTitle
searchTitle( $term)
Perform a title-only search query and return a result set.
Definition: SearchOracle.php:82
SqlSearchResultSet
This class is used for different SQL-based search engines shipped with MediaWiki.
Definition: SqlSearchResultSet.php:9
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:22
SearchOracle\getIndexField
getIndexField( $fulltext)
Picks which field to index on, depending on what type of query.
Definition: SearchOracle.php:148
SearchOracle\escapeTerm
escapeTerm( $t)
Definition: SearchOracle.php:214
$t
$t
Definition: testCompression.php:69
SearchOracle\queryNamespaces
queryNamespaces()
Return a partial WHERE clause to limit the search to the given namespaces.
Definition: SearchOracle.php:95
SearchOracle\$reservedWords
$reservedWords
Definition: SearchOracle.php:32
SearchOracle\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: SearchOracle.php:269
SearchDatabase\filter
filter( $text)
Return a 'cleaned up' search string.
Definition: SearchDatabase.php:54
$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:57
$type
$type
Definition: testCompression.php:48