MediaWiki  1.23.2
SearchOracle.php
Go to the documentation of this file.
1 <?php
32 
33  private $reservedWords = array(
34  'ABOUT' => 1,
35  'ACCUM' => 1,
36  'AND' => 1,
37  'BT' => 1,
38  'BTG' => 1,
39  'BTI' => 1,
40  'BTP' => 1,
41  'FUZZY' => 1,
42  'HASPATH' => 1,
43  'INPATH' => 1,
44  'MINUS' => 1,
45  'NEAR' => 1,
46  'NOT' => 1,
47  'NT' => 1,
48  'NTG' => 1,
49  'NTI' => 1,
50  'NTP' => 1,
51  'OR' => 1,
52  'PT' => 1,
53  'RT' => 1,
54  'SQE' => 1,
55  'SYN' => 1,
56  'TR' => 1,
57  'TRSYN' => 1,
58  'TT' => 1,
59  'WITHIN' => 1,
60  );
61 
68  function searchText( $term ) {
69  if ( $term == '' ) {
70  return new SqlSearchResultSet( false, '' );
71  }
72 
73  $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), true ) ) );
74  return new SqlSearchResultSet( $resultSet, $this->searchTerms );
75  }
76 
83  function searchTitle( $term ) {
84  if ( $term == '' ) {
85  return new SqlSearchResultSet( false, '' );
86  }
87 
88  $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), false ) ) );
89  return new MySQLSearchResultSet( $resultSet, $this->searchTerms );
90  }
91 
96  function queryNamespaces() {
97  if ( is_null( $this->namespaces ) ) {
98  return '';
99  }
100  if ( !count( $this->namespaces ) ) {
101  $namespaces = '0';
102  } else {
103  $namespaces = $this->db->makeList( $this->namespaces );
104  }
105  return 'AND page_namespace IN (' . $namespaces . ')';
106  }
107 
115  function queryLimit( $sql ) {
116  return $this->db->limitResult( $sql, $this->limit, $this->offset );
117  }
118 
125  function queryRanking( $filteredTerm, $fulltext ) {
126  return ' ORDER BY score(1)';
127  }
128 
136  function getQuery( $filteredTerm, $fulltext ) {
137  return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
138  $this->queryNamespaces() . ' ' .
139  $this->queryRanking( $filteredTerm, $fulltext ) . ' ' );
140  }
141 
147  function getIndexField( $fulltext ) {
148  return $fulltext ? 'si_text' : 'si_title';
149  }
150 
158  function queryMain( $filteredTerm, $fulltext ) {
159  $match = $this->parseQuery( $filteredTerm, $fulltext );
160  $page = $this->db->tableName( 'page' );
161  $searchindex = $this->db->tableName( 'searchindex' );
162  return 'SELECT page_id, page_namespace, page_title ' .
163  "FROM $page,$searchindex " .
164  'WHERE page_id=si_page AND ' . $match;
165  }
166 
172  function parseQuery( $filteredText, $fulltext ) {
175  $this->searchTerms = array();
176 
177  # @todo FIXME: This doesn't handle parenthetical expressions.
178  $m = array();
179  $searchon = '';
180  if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
181  $filteredText, $m, PREG_SET_ORDER ) ) {
182  foreach ( $m as $terms ) {
183  // Search terms in all variant forms, only
184  // apply on wiki with LanguageConverter
185  $temp_terms = $wgContLang->autoConvertToAllVariants( $terms[2] );
186  if ( is_array( $temp_terms ) ) {
187  $temp_terms = array_unique( array_values( $temp_terms ) );
188  foreach ( $temp_terms as $t ) {
189  $searchon .= ( $terms[1] == '-' ? ' ~' : ' & ' ) . $this->escapeTerm( $t );
190  }
191  }
192  else {
193  $searchon .= ( $terms[1] == '-' ? ' ~' : ' & ' ) . $this->escapeTerm( $terms[2] );
194  }
195  if ( !empty( $terms[3] ) ) {
196  $regexp = preg_quote( $terms[3], '/' );
197  if ( $terms[4] ) {
198  $regexp .= "[0-9A-Za-z_]+";
199  }
200  } else {
201  $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
202  }
203  $this->searchTerms[] = $regexp;
204  }
205  }
206 
207  $searchon = $this->db->addQuotes( ltrim( $searchon, ' &' ) );
208  $field = $this->getIndexField( $fulltext );
209  return " CONTAINS($field, $searchon, 1) > 0 ";
210  }
211 
212  private function escapeTerm( $t ) {
214  $t = $wgContLang->normalizeForSearch( $t );
215  $t = isset( $this->reservedWords[strtoupper( $t )] ) ? '{' . $t . '}' : $t;
216  $t = preg_replace( '/^"(.*)"$/', '($1)', $t );
217  $t = preg_replace( '/([-&|])/', '\\\\$1', $t );
218  return $t;
219  }
220 
229  function update( $id, $title, $text ) {
230  $dbw = wfGetDB( DB_MASTER );
231  $dbw->replace( 'searchindex',
232  array( 'si_page' ),
233  array(
234  'si_page' => $id,
235  'si_title' => $title,
236  'si_text' => $text
237  ), 'SearchOracle::update' );
238 
239  // Sync the index
240  // We need to specify the DB name (i.e. user/schema) here so that
241  // it can work from the installer, where
242  // ALTER SESSION SET CURRENT_SCHEMA = ...
243  // was used.
244  $dbw->query( "CALL ctx_ddl.sync_index(" .
245  $dbw->addQuotes( $dbw->getDBname() . '.' . $dbw->tableName( 'si_text_idx', 'raw' ) ) . ")" );
246  $dbw->query( "CALL ctx_ddl.sync_index(" .
247  $dbw->addQuotes( $dbw->getDBname() . '.' . $dbw->tableName( 'si_title_idx', 'raw' ) ) . ")" );
248  }
249 
257  function updateTitle( $id, $title ) {
258  $dbw = wfGetDB( DB_MASTER );
259 
260  $dbw->update( 'searchindex',
261  array( 'si_title' => $title ),
262  array( 'si_page' => $id ),
263  'SearchOracle::updateTitle',
264  array() );
265  }
266 
267  public static function legalSearchChars() {
268  return "\"" . parent::legalSearchChars();
269  }
270 }
SearchOracle\updateTitle
updateTitle( $id, $title)
Update a search index record's title only.
Definition: SearchOracle.php:257
DB_MASTER
const DB_MASTER
Definition: Defines.php:56
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3650
SearchOracle
Search engine hook base class for Oracle (ConText).
Definition: SearchOracle.php:31
$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
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:172
SearchOracle\getQuery
getQuery( $filteredTerm, $fulltext)
Construct the full SQL query to do the search.
Definition: SearchOracle.php:136
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1530
SearchDatabase
Base search engine base class for database-backed searches.
Definition: SearchDatabase.php:29
namespaces
to move a page</td >< td > &*You are moving the page across namespaces
Definition: All_system_messages.txt:2677
SearchOracle\searchText
searchText( $term)
Perform a full text search query and return a result set.
Definition: SearchOracle.php:68
SearchOracle\update
update( $id, $title, $text)
Create or update the search index record for the given page.
Definition: SearchOracle.php:229
SearchOracle\queryRanking
queryRanking( $filteredTerm, $fulltext)
Does not do anything for generic search engine subclasses may define this though.
Definition: SearchOracle.php:125
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
$regexp
$regexp
Definition: mwdoc-filter.php:19
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:188
SearchOracle\queryMain
queryMain( $filteredTerm, $fulltext)
Get the base part of the search query.
Definition: SearchOracle.php:158
SearchOracle\queryLimit
queryLimit( $sql)
Return a LIMIT clause to limit results on the query.
Definition: SearchOracle.php:115
SearchOracle\searchTitle
searchTitle( $term)
Perform a title-only search query and return a result set.
Definition: SearchOracle.php:83
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
SqlSearchResultSet
This class is used for different SQL-based search engines shipped with MediaWiki.
Definition: SearchResultSet.php:140
SearchEngine\legalSearchChars
static legalSearchChars()
Definition: SearchEngine.php:256
SearchEngine\$namespaces
$namespaces
Definition: SearchEngine.php:37
$term
the value to return A Title object or null whereas SearchGetNearMatch runs after $term
Definition: hooks.txt:2125
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
SearchOracle\legalSearchChars
static legalSearchChars()
Definition: SearchOracle.php:267
MySQLSearchResultSet
Definition: SearchMySQL.php:459
SearchOracle\getIndexField
getIndexField( $fulltext)
Picks which field to index on, depending on what type of query.
Definition: SearchOracle.php:147
SearchOracle\escapeTerm
escapeTerm( $t)
Definition: SearchOracle.php:212
$t
$t
Definition: testCompression.php:65
SearchOracle\queryNamespaces
queryNamespaces()
Return a partial WHERE clause to limit the search to the given namespaces.
Definition: SearchOracle.php:96
SearchOracle\$reservedWords
$reservedWords
Definition: SearchOracle.php:33