MediaWiki  1.23.0
SearchEngine.php
Go to the documentation of this file.
1 <?php
32 class SearchEngine {
33  var $limit = 10;
34  var $offset = 0;
35  var $prefix = '';
38  protected $showSuggestion = true;
39 
41  protected $features = array();
42 
51  function searchText( $term ) {
52  return null;
53  }
54 
63  function searchTitle( $term ) {
64  return null;
65  }
66 
72  public function supports( $feature ) {
73  switch ( $feature ) {
74  case 'search-update':
75  return true;
76  case 'title-suffix-filter':
77  default:
78  return false;
79  }
80  }
81 
89  public function setFeatureData( $feature, $data ) {
90  $this->features[$feature] = $data;
91  }
92 
101  public function normalizeText( $string ) {
103 
104  // Some languages such as Chinese require word segmentation
105  return $wgContLang->segmentByWord( $string );
106  }
107 
112  function transformSearchTerm( $term ) {
113  return $term;
114  }
115 
123  public static function getNearMatch( $searchterm ) {
124  $title = self::getNearMatchInternal( $searchterm );
125 
126  wfRunHooks( 'SearchGetNearMatchComplete', array( $searchterm, &$title ) );
127  return $title;
128  }
129 
137  public static function getNearMatchResultSet( $searchterm ) {
138  return new SearchNearMatchResultSet( self::getNearMatch( $searchterm ) );
139  }
140 
145  private static function getNearMatchInternal( $searchterm ) {
146  global $wgContLang, $wgEnableSearchContributorsByIP;
147 
148  $allSearchTerms = array( $searchterm );
149 
150  if ( $wgContLang->hasVariants() ) {
151  $allSearchTerms = array_merge( $allSearchTerms, $wgContLang->autoConvertToAllVariants( $searchterm ) );
152  }
153 
154  $titleResult = null;
155  if ( !wfRunHooks( 'SearchGetNearMatchBefore', array( $allSearchTerms, &$titleResult ) ) ) {
156  return $titleResult;
157  }
158 
159  foreach ( $allSearchTerms as $term ) {
160 
161  # Exact match? No need to look further.
163  if ( is_null( $title ) ) {
164  return null;
165  }
166 
167  # Try files if searching in the Media: namespace
168  if ( $title->getNamespace() == NS_MEDIA ) {
169  $title = Title::makeTitle( NS_FILE, $title->getText() );
170  }
171 
172  if ( $title->isSpecialPage() || $title->isExternal() || $title->exists() ) {
173  return $title;
174  }
175 
176  # See if it still otherwise has content is some sane sense
177  $page = WikiPage::factory( $title );
178  if ( $page->hasViewableContent() ) {
179  return $title;
180  }
181 
182  if ( !wfRunHooks( 'SearchAfterNoDirectMatch', array( $term, &$title ) ) ) {
183  return $title;
184  }
185 
186  # Now try all lower case (i.e. first letter capitalized)
188  if ( $title && $title->exists() ) {
189  return $title;
190  }
191 
192  # Now try capitalized string
193  $title = Title::newFromText( $wgContLang->ucwords( $term ) );
194  if ( $title && $title->exists() ) {
195  return $title;
196  }
197 
198  # Now try all upper case
200  if ( $title && $title->exists() ) {
201  return $title;
202  }
203 
204  # Now try Word-Caps-Breaking-At-Word-Breaks, for hyphenated names etc
205  $title = Title::newFromText( $wgContLang->ucwordbreaks( $term ) );
206  if ( $title && $title->exists() ) {
207  return $title;
208  }
209 
210  // Give hooks a chance at better match variants
211  $title = null;
212  if ( !wfRunHooks( 'SearchGetNearMatch', array( $term, &$title ) ) ) {
213  return $title;
214  }
215  }
216 
217  $title = Title::newFromText( $searchterm );
218 
219  # Entering an IP address goes to the contributions page
220  if ( $wgEnableSearchContributorsByIP ) {
221  if ( ( $title->getNamespace() == NS_USER && User::isIP( $title->getText() ) )
222  || User::isIP( trim( $searchterm ) ) ) {
223  return SpecialPage::getTitleFor( 'Contributions', $title->getDBkey() );
224  }
225  }
226 
227  # Entering a user goes to the user page whether it's there or not
228  if ( $title->getNamespace() == NS_USER ) {
229  return $title;
230  }
231 
232  # Go to images that exist even if there's no local page.
233  # There may have been a funny upload, or it may be on a shared
234  # file repository such as Wikimedia Commons.
235  if ( $title->getNamespace() == NS_FILE ) {
236  $image = wfFindFile( $title );
237  if ( $image ) {
238  return $title;
239  }
240  }
241 
242  # MediaWiki namespace? Page may be "implied" if not customized.
243  # Just return it, with caps forced as the message system likes it.
244  if ( $title->getNamespace() == NS_MEDIAWIKI ) {
245  return Title::makeTitle( NS_MEDIAWIKI, $wgContLang->ucfirst( $title->getText() ) );
246  }
247 
248  # Quoted term? Try without the quotes...
249  $matches = array();
250  if ( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) {
252  }
253 
254  return null;
255  }
256 
257  public static function legalSearchChars() {
258  return "A-Za-z_'.0-9\\x80-\\xFF\\-";
259  }
260 
268  function setLimitOffset( $limit, $offset = 0 ) {
269  $this->limit = intval( $limit );
270  $this->offset = intval( $offset );
271  }
272 
279  function setNamespaces( $namespaces ) {
280  $this->namespaces = $namespaces;
281  }
282 
290  function setShowSuggestion( $showSuggestion ) {
291  $this->showSuggestion = $showSuggestion;
292  }
293 
301  function replacePrefixes( $query ) {
303 
304  $parsed = $query;
305  if ( strpos( $query, ':' ) === false ) { // nothing to do
306  wfRunHooks( 'SearchEngineReplacePrefixesComplete', array( $this, $query, &$parsed ) );
307  return $parsed;
308  }
309 
310  $allkeyword = wfMessage( 'searchall' )->inContentLanguage()->text() . ":";
311  if ( strncmp( $query, $allkeyword, strlen( $allkeyword ) ) == 0 ) {
312  $this->namespaces = null;
313  $parsed = substr( $query, strlen( $allkeyword ) );
314  } elseif ( strpos( $query, ':' ) !== false ) {
315  $prefix = str_replace( ' ', '_', substr( $query, 0, strpos( $query, ':' ) ) );
316  $index = $wgContLang->getNsIndex( $prefix );
317  if ( $index !== false ) {
318  $this->namespaces = array( $index );
319  $parsed = substr( $query, strlen( $prefix ) + 1 );
320  }
321  }
322  if ( trim( $parsed ) == '' ) {
323  $parsed = $query; // prefix was the whole query
324  }
325 
326  wfRunHooks( 'SearchEngineReplacePrefixesComplete', array( $this, $query, &$parsed ) );
327 
328  return $parsed;
329  }
330 
335  public static function searchableNamespaces() {
337  $arr = array();
338  foreach ( $wgContLang->getNamespaces() as $ns => $name ) {
339  if ( $ns >= NS_MAIN ) {
340  $arr[$ns] = $name;
341  }
342  }
343 
344  wfRunHooks( 'SearchableNamespaces', array( &$arr ) );
345  return $arr;
346  }
347 
355  public static function userNamespaces( $user ) {
356  global $wgSearchEverythingOnlyLoggedIn;
357 
358  $searchableNamespaces = SearchEngine::searchableNamespaces();
359 
360  // get search everything preference, that can be set to be read for logged-in users
361  // it overrides other options
362  if ( !$wgSearchEverythingOnlyLoggedIn || $user->isLoggedIn() ) {
363  if ( $user->getOption( 'searcheverything' ) ) {
364  return array_keys( $searchableNamespaces );
365  }
366  }
367 
368  $arr = array();
369  foreach ( $searchableNamespaces as $ns => $name ) {
370  if ( $user->getOption( 'searchNs' . $ns ) ) {
371  $arr[] = $ns;
372  }
373  }
374 
375  return $arr;
376  }
377 
383  public static function userHighlightPrefs() {
384  $contextlines = 2; // Hardcode this. Old defaults sucked. :)
385  $contextchars = 75; // same as above.... :P
386  return array( $contextlines, $contextchars );
387  }
388 
394  public static function defaultNamespaces() {
395  global $wgNamespacesToBeSearchedDefault;
396 
397  return array_keys( $wgNamespacesToBeSearchedDefault, true );
398  }
399 
407  public static function namespacesAsText( $namespaces ) {
409 
410  $formatted = array_map( array( $wgContLang, 'getFormattedNsText' ), $namespaces );
411  foreach ( $formatted as $key => $ns ) {
412  if ( empty( $ns ) ) {
413  $formatted[$key] = wfMessage( 'blanknamespace' )->text();
414  }
415  }
416  return $formatted;
417  }
418 
424  public static function helpNamespaces() {
425  global $wgNamespacesToBeSearchedHelp;
426 
427  return array_keys( $wgNamespacesToBeSearchedHelp, true );
428  }
429 
436  function filter( $text ) {
437  $lc = $this->legalSearchChars();
438  return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
439  }
440 
448  public static function create( $type = null ) {
449  global $wgSearchType;
450  $dbr = null;
451 
452  $alternatives = self::getSearchTypes();
453 
454  if ( $type && in_array( $type, $alternatives ) ) {
455  $class = $type;
456  } elseif ( $wgSearchType !== null ) {
457  $class = $wgSearchType;
458  } else {
459  $dbr = wfGetDB( DB_SLAVE );
460  $class = $dbr->getSearchEngine();
461  }
462 
463  $search = new $class( $dbr );
464  return $search;
465  }
466 
473  public static function getSearchTypes() {
474  global $wgSearchType, $wgSearchTypeAlternatives;
475 
476  $alternatives = $wgSearchTypeAlternatives ?: array();
477  array_unshift( $alternatives, $wgSearchType );
478 
479  return $alternatives;
480  }
481 
491  function update( $id, $title, $text ) {
492  // no-op
493  }
494 
503  function updateTitle( $id, $title ) {
504  // no-op
505  }
506 
515  function delete( $id, $title ) {
516  // no-op
517  }
518 
524  public static function getOpenSearchTemplate() {
525  global $wgOpenSearchTemplate, $wgCanonicalServer;
526  if ( $wgOpenSearchTemplate ) {
527  return $wgOpenSearchTemplate;
528  } else {
529  $ns = implode( '|', SearchEngine::defaultNamespaces() );
530  if ( !$ns ) {
531  $ns = "0";
532  }
533  return $wgCanonicalServer . wfScript( 'api' ) . '?action=opensearch&search={searchTerms}&namespace=' . $ns;
534  }
535  }
536 
547  public function getTextFromContent( Title $t, Content $c = null ) {
548  return $c ? $c->getTextForSearchIndex() : '';
549  }
550 
558  public function textAlreadyUpdatedForIndex() {
559  return false;
560  }
561 }
562 
566 class SearchResultTooMany {
567  # # Some search engines may bail out if too many matches are found
568 }
569 
576 class SearchEngineDummy extends SearchEngine {
577  // no-op
578 }
Title\makeTitle
static & makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:398
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:189
SearchEngine\supports
supports( $feature)
Definition: SearchEngine.php:71
SearchEngine\setFeatureData
setFeatureData( $feature, $data)
Way to pass custom data for engines.
Definition: SearchEngine.php:88
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
SearchEngine\searchableNamespaces
static searchableNamespaces()
Make a list of searchable namespaces and their canonical names.
Definition: SearchEngine.php:334
SearchEngine\normalizeText
normalizeText( $string)
When overridden in derived class, performs database-specific conversions on text to be used for searc...
Definition: SearchEngine.php:100
SearchEngine\searchTitle
searchTitle( $term)
Perform a title-only search query and return a result set.
Definition: SearchEngine.php:62
SearchEngine\userHighlightPrefs
static userHighlightPrefs()
Find snippet highlight settings for all users.
Definition: SearchEngine.php:382
NS_FILE
const NS_FILE
Definition: Defines.php:85
SearchEngine\$prefix
$prefix
Definition: SearchEngine.php:35
SpecialPage\getTitleFor
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name.
Definition: SpecialPage.php:74
$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
SearchEngine\defaultNamespaces
static defaultNamespaces()
An array of namespaces indexes to be searched by default.
Definition: SearchEngine.php:393
SearchEngine\replacePrefixes
replacePrefixes( $query)
Parse some common prefixes: all (search everything) or namespace names.
Definition: SearchEngine.php:300
$dbr
$dbr
Definition: testCompression.php:48
NS_MAIN
const NS_MAIN
Definition: Defines.php:79
SearchEngine\textAlreadyUpdatedForIndex
textAlreadyUpdatedForIndex()
If an implementation of SearchEngine handles all of its own text processing in getTextFromContent() a...
Definition: SearchEngine.php:557
namespaces
to move a page</td >< td > &*You are moving the page across namespaces
Definition: All_system_messages.txt:2677
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:103
wfScript
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
Definition: GlobalFunctions.php:3730
SearchEngine\$features
Array $features
Feature values *.
Definition: SearchEngine.php:40
SearchEngine\getOpenSearchTemplate
static getOpenSearchTemplate()
Get OpenSearch suggestion template.
Definition: SearchEngine.php:523
wfMessage
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 an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing after in associative array form externallinks including delete and has completed for all link tables default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt
SearchEngine\$offset
$offset
Definition: SearchEngine.php:34
SearchEngine\searchText
searchText( $term)
Perform a full text search query and return a result set.
Definition: SearchEngine.php:50
wfRunHooks
wfRunHooks( $event, array $args=array(), $deprecatedVersion=null)
Call hook functions defined in $wgHooks.
Definition: GlobalFunctions.php:4001
User\isIP
static isIP( $name)
Does the string match an anonymous IPv4 address?
Definition: User.php:554
SearchNearMatchResultSet
A SearchResultSet wrapper for SearchEngine::getNearMatch.
Definition: SearchResultSet.php:186
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
SearchEngine\filter
filter( $text)
Return a 'cleaned up' search string.
Definition: SearchEngine.php:435
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
SearchEngine\helpNamespaces
static helpNamespaces()
Return the help namespaces to be shown on Special:Search.
Definition: SearchEngine.php:423
SearchEngine\updateTitle
updateTitle( $id, $title)
Update a search index record's title only.
Definition: SearchEngine.php:502
SearchEngine\setNamespaces
setNamespaces( $namespaces)
Set which namespaces the search should include.
Definition: SearchEngine.php:278
SearchEngine\$searchTerms
$searchTerms
Definition: SearchEngine.php:36
SearchEngine\update
update( $id, $title, $text)
Create or update the search index record for the given page.
Definition: SearchEngine.php:490
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
SearchEngine\getNearMatchResultSet
static getNearMatchResultSet( $searchterm)
Do a near match (see SearchEngine::getNearMatch) and wrap it into a SearchResultSet.
Definition: SearchEngine.php:136
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:336
SearchEngine\$limit
$limit
Definition: SearchEngine.php:33
$matches
if(!defined( 'MEDIAWIKI')) if(!isset( $wgVersion)) $matches
Definition: NoLocalSettings.php:33
SearchEngine\getNearMatchInternal
static getNearMatchInternal( $searchterm)
Really find the title match.
Definition: SearchEngine.php:144
SearchResultTooMany
Definition: SearchEngine.php:565
SearchEngine\getTextFromContent
getTextFromContent(Title $t, Content $c=null)
Get the raw text for updating the index from a content object Nicer search backends could possibly do...
Definition: SearchEngine.php:546
NS_MEDIA
const NS_MEDIA
Definition: Defines.php:67
SearchEngine\legalSearchChars
static legalSearchChars()
Definition: SearchEngine.php:256
SearchEngine\transformSearchTerm
transformSearchTerm( $term)
Transform search term in cases when parts of the query came as different GET params (when supported) ...
Definition: SearchEngine.php:111
SearchEngine\$namespaces
$namespaces
Definition: SearchEngine.php:37
SearchEngine\setShowSuggestion
setShowSuggestion( $showSuggestion)
Set whether the searcher should try to build a suggestion.
Definition: SearchEngine.php:289
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:237
SearchEngine
Contain a class for special pages.
Definition: SearchEngine.php:32
SearchEngineDummy
Dummy class to be used when non-supported Database engine is present.
Definition: SearchEngine.php:575
Content
Base interface for content objects.
Definition: Content.php:34
SearchEngine\$showSuggestion
$showSuggestion
Definition: SearchEngine.php:38
DB_SLAVE
const DB_SLAVE
Definition: Defines.php:55
Title
Represents a title within MediaWiki.
Definition: Title.php:35
SearchEngine\getNearMatch
static getNearMatch( $searchterm)
If an exact title match can be found, or a very slightly close match, return the title.
Definition: SearchEngine.php:122
$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
wfFindFile
wfFindFile( $title, $options=array())
Find a file.
Definition: GlobalFunctions.php:3693
NS_USER
const NS_USER
Definition: Defines.php:81
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition: Defines.php:87
$t
$t
Definition: testCompression.php:65
$query
return true to allow those checks to and false if checking is done use this to change the tables headers temp or archived zone change it to an object instance and return false override the list derivative used the name of the old file when set the default code will be skipped add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition: hooks.txt:1105
SearchEngine\create
static create( $type=null)
Load up the appropriate search engine class for the currently active database backend,...
Definition: SearchEngine.php:447
SearchEngine\setLimitOffset
setLimitOffset( $limit, $offset=0)
Set the maximum number of results to return and how many to skip before returning the first.
Definition: SearchEngine.php:267
SearchEngine\getSearchTypes
static getSearchTypes()
Return the search engines we support.
Definition: SearchEngine.php:472
SearchEngine\namespacesAsText
static namespacesAsText( $namespaces)
Get a list of namespace names useful for showing in tooltips and preferences.
Definition: SearchEngine.php:406
SearchEngine\userNamespaces
static userNamespaces( $user)
Extract default namespaces to search from the given user's settings, returning a list of index number...
Definition: SearchEngine.php:354
$type
$type
Definition: testCompression.php:46