Localisation updates German
[mediawiki.git] / includes / SearchEngine.php
blob6af2bb38a0f0b9941f089c34291f996195a1d847
1 <?php
2 /**
3 * Contain a class for special pages
4 * @addtogroup Search
5 */
6 class SearchEngine {
7 var $limit = 10;
8 var $offset = 0;
9 var $searchTerms = array();
10 var $namespaces = array( NS_MAIN );
11 var $showRedirects = false;
13 /**
14 * Perform a full text search query and return a result set.
15 * If title searches are not supported or disabled, return null.
17 * @param string $term - Raw search term
18 * @return SearchResultSet
19 * @access public
20 * @abstract
22 function searchText( $term ) {
23 return null;
26 /**
27 * Perform a title-only search query and return a result set.
28 * If title searches are not supported or disabled, return null.
30 * @param string $term - Raw search term
31 * @return SearchResultSet
32 * @access public
33 * @abstract
35 function searchTitle( $term ) {
36 return null;
39 /**
40 * If an exact title match can be find, or a very slightly close match,
41 * return the title. If no match, returns NULL.
43 * @param string $term
44 * @return Title
46 public static function getNearMatch( $searchterm ) {
47 global $wgContLang;
49 $allSearchTerms = array($searchterm);
51 if($wgContLang->hasVariants()){
52 $allSearchTerms = array_merge($allSearchTerms,$wgContLang->convertLinkToAllVariants($searchterm));
55 foreach($allSearchTerms as $term){
57 # Exact match? No need to look further.
58 $title = Title::newFromText( $term );
59 if (is_null($title))
60 return NULL;
62 if ( $title->getNamespace() == NS_SPECIAL || $title->exists() ) {
63 return $title;
66 # Now try all lower case (i.e. first letter capitalized)
68 $title = Title::newFromText( $wgContLang->lc( $term ) );
69 if ( $title->exists() ) {
70 return $title;
73 # Now try capitalized string
75 $title = Title::newFromText( $wgContLang->ucwords( $term ) );
76 if ( $title->exists() ) {
77 return $title;
80 # Now try all upper case
82 $title = Title::newFromText( $wgContLang->uc( $term ) );
83 if ( $title->exists() ) {
84 return $title;
87 # Now try Word-Caps-Breaking-At-Word-Breaks, for hyphenated names etc
88 $title = Title::newFromText( $wgContLang->ucwordbreaks($term) );
89 if ( $title->exists() ) {
90 return $title;
93 global $wgCapitalLinks, $wgContLang;
94 if( !$wgCapitalLinks ) {
95 // Catch differs-by-first-letter-case-only
96 $title = Title::newFromText( $wgContLang->ucfirst( $term ) );
97 if ( $title->exists() ) {
98 return $title;
100 $title = Title::newFromText( $wgContLang->lcfirst( $term ) );
101 if ( $title->exists() ) {
102 return $title;
106 // Give hooks a chance at better match variants
107 $title = null;
108 if( !wfRunHooks( 'SearchGetNearMatch', array( $term, &$title ) ) ) {
109 return $title;
113 $title = Title::newFromText( $searchterm );
115 # Entering an IP address goes to the contributions page
116 if ( ( $title->getNamespace() == NS_USER && User::isIP($title->getText() ) )
117 || User::isIP( trim( $searchterm ) ) ) {
118 return SpecialPage::getTitleFor( 'Contributions', $title->getDBkey() );
122 # Entering a user goes to the user page whether it's there or not
123 if ( $title->getNamespace() == NS_USER ) {
124 return $title;
127 # Go to images that exist even if there's no local page.
128 # There may have been a funny upload, or it may be on a shared
129 # file repository such as Wikimedia Commons.
130 if( $title->getNamespace() == NS_IMAGE ) {
131 $image = wfFindFile( $title );
132 if( $image ) {
133 return $title;
137 # MediaWiki namespace? Page may be "implied" if not customized.
138 # Just return it, with caps forced as the message system likes it.
139 if( $title->getNamespace() == NS_MEDIAWIKI ) {
140 return Title::makeTitle( NS_MEDIAWIKI, $wgContLang->ucfirst( $title->getText() ) );
143 # Quoted term? Try without the quotes...
144 $matches = array();
145 if( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) {
146 return SearchEngine::getNearMatch( $matches[1] );
149 return NULL;
152 public static function legalSearchChars() {
153 return "A-Za-z_'0-9\\x80-\\xFF\\-";
157 * Set the maximum number of results to return
158 * and how many to skip before returning the first.
160 * @param int $limit
161 * @param int $offset
162 * @access public
164 function setLimitOffset( $limit, $offset = 0 ) {
165 $this->limit = intval( $limit );
166 $this->offset = intval( $offset );
170 * Set which namespaces the search should include.
171 * Give an array of namespace index numbers.
173 * @param array $namespaces
174 * @access public
176 function setNamespaces( $namespaces ) {
177 $this->namespaces = $namespaces;
181 * Parse some common prefixes: all (search everything)
182 * or namespace names
184 * @param string $query
186 function replacePrefixes( $query ){
187 global $wgContLang;
189 if( strpos($query,':') === false )
190 return $query; // nothing to do
192 $parsed = $query;
193 $allkeyword = wfMsgForContent('searchall').":";
194 if( strncmp($query, $allkeyword, strlen($allkeyword)) == 0 ){
195 $this->namespaces = null;
196 $parsed = substr($query,strlen($allkeyword));
197 } else if( strpos($query,':') !== false ) {
198 $prefix = substr($query,0,strpos($query,':'));
199 $index = $wgContLang->getNsIndex($prefix);
200 if($index !== false){
201 $this->namespaces = array($index);
202 $parsed = substr($query,strlen($prefix)+1);
205 if(trim($parsed) == '')
206 return $query; // prefix was the whole query
208 return $parsed;
212 * Make a list of searchable namespaces and their canonical names.
213 * @return array
215 public static function searchableNamespaces() {
216 global $wgContLang;
217 $arr = array();
218 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
219 if( $ns >= NS_MAIN ) {
220 $arr[$ns] = $name;
223 return $arr;
227 * Extract default namespaces to search from the given user's
228 * settings, returning a list of index numbers.
230 * @param User $user
231 * @return array
232 * @static
234 public static function userNamespaces( &$user ) {
235 $arr = array();
236 foreach( SearchEngine::searchableNamespaces() as $ns => $name ) {
237 if( $user->getOption( 'searchNs' . $ns ) ) {
238 $arr[] = $ns;
241 return $arr;
245 * Find snippet highlight settings for a given user
247 * @param User $user
248 * @return array contextlines, contextchars
249 * @static
251 public static function userHighlightPrefs( &$user ){
252 //$contextlines = $user->getOption( 'contextlines', 5 );
253 $contextlines = 2; // Hardcode this. Old defaults sucked. :)
254 $contextchars = $user->getOption( 'contextchars', 50 );
255 return array($contextlines, $contextchars);
259 * An array of namespaces indexes to be searched by default
261 * @return array
262 * @static
264 public static function defaultNamespaces(){
265 global $wgNamespacesToBeSearchedDefault;
267 return array_keys($wgNamespacesToBeSearchedDefault, true);
271 * Return a 'cleaned up' search string
273 * @return string
274 * @access public
276 function filter( $text ) {
277 $lc = $this->legalSearchChars();
278 return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
281 * Load up the appropriate search engine class for the currently
282 * active database backend, and return a configured instance.
284 * @return SearchEngine
286 public static function create() {
287 global $wgDBtype, $wgSearchType;
288 if( $wgSearchType ) {
289 $class = $wgSearchType;
290 } elseif( $wgDBtype == 'mysql' ) {
291 $class = 'SearchMySQL';
292 } else if ( $wgDBtype == 'postgres' ) {
293 $class = 'SearchPostgres';
294 } else if ( $wgDBtype == 'oracle' ) {
295 $class = 'SearchOracle';
296 } else {
297 $class = 'SearchEngineDummy';
299 $search = new $class( wfGetDB( DB_SLAVE ) );
300 $search->setLimitOffset(0,0);
301 return $search;
305 * Create or update the search index record for the given page.
306 * Title and text should be pre-processed.
308 * @param int $id
309 * @param string $title
310 * @param string $text
311 * @abstract
313 function update( $id, $title, $text ) {
314 // no-op
318 * Update a search index record's title only.
319 * Title should be pre-processed.
321 * @param int $id
322 * @param string $title
323 * @abstract
325 function updateTitle( $id, $title ) {
326 // no-op
330 * Get OpenSearch suggestion template
332 * @return string
333 * @static
335 public static function getOpenSearchTemplate() {
336 global $wgOpenSearchTemplate, $wgServer, $wgScriptPath;
337 if($wgOpenSearchTemplate)
338 return $wgOpenSearchTemplate;
339 else{
340 $ns = implode(',',SearchEngine::defaultNamespaces());
341 if(!$ns) $ns = "0";
342 return $wgServer . $wgScriptPath . '/api.php?action=opensearch&search={searchTerms}&namespace='.$ns;
347 * Get internal MediaWiki Suggest template
349 * @return string
350 * @static
352 public static function getMWSuggestTemplate() {
353 global $wgMWSuggestTemplate, $wgServer, $wgScriptPath;
354 if($wgMWSuggestTemplate)
355 return $wgMWSuggestTemplate;
356 else
357 return $wgServer . $wgScriptPath . '/api.php?action=opensearch&search={searchTerms}&namespace={namespaces}';
363 * @addtogroup Search
365 class SearchResultSet {
367 * Fetch an array of regular expression fragments for matching
368 * the search terms as parsed by this engine in a text extract.
370 * @return array
371 * @access public
372 * @abstract
374 function termMatches() {
375 return array();
378 function numRows() {
379 return 0;
383 * Return true if results are included in this result set.
384 * @return bool
385 * @abstract
387 function hasResults() {
388 return false;
392 * Some search modes return a total hit count for the query
393 * in the entire article database. This may include pages
394 * in namespaces that would not be matched on the given
395 * settings.
397 * Return null if no total hits number is supported.
399 * @return int
400 * @access public
402 function getTotalHits() {
403 return null;
407 * Some search modes return a suggested alternate term if there are
408 * no exact hits. Returns true if there is one on this set.
410 * @return bool
411 * @access public
413 function hasSuggestion() {
414 return false;
418 * @return string suggested query, null if none
420 function getSuggestionQuery(){
421 return null;
425 * @return string highlighted suggested query, '' if none
427 function getSuggestionSnippet(){
428 return '';
432 * Return information about how and from where the results were fetched,
433 * should be useful for diagnostics and debugging
435 * @return string
437 function getInfo() {
438 return null;
442 * Return a result set of hits on other (multiple) wikis associated with this one
444 * @return SearchResultSet
446 function getInterwikiResults() {
447 return null;
451 * Check if there are results on other wikis
453 * @return boolean
455 function hasInterwikiResults() {
456 return $this->getInterwikiResults() != null;
461 * Fetches next search result, or false.
462 * @return SearchResult
463 * @access public
464 * @abstract
466 function next() {
467 return false;
471 * Frees the result set, if applicable.
472 * @ access public
474 function free() {
475 // ...
481 * @addtogroup Search
483 class SearchResultTooMany {
484 ## Some search engines may bail out if too many matches are found
489 * @addtogroup Search
491 class SearchResult {
493 function SearchResult( $row ) {
494 $this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
495 if( !is_null($this->mTitle) )
496 $this->mRevision = Revision::newFromTitle( $this->mTitle );
500 * Check if this is result points to an invalid title
502 * @return boolean
503 * @access public
505 function isBrokenTitle(){
506 if( is_null($this->mTitle) )
507 return true;
508 return false;
512 * Check if target page is missing, happens when index is out of date
514 * @return boolean
515 * @access public
517 function isMissingRevision(){
518 if( !$this->mRevision )
519 return true;
520 return false;
524 * @return Title
525 * @access public
527 function getTitle() {
528 return $this->mTitle;
532 * @return double or null if not supported
534 function getScore() {
535 return null;
539 * Lazy initialization of article text from DB
541 protected function initText(){
542 if( !isset($this->mText) ){
543 $this->mText = $this->mRevision->getText();
548 * @param array $terms Terms to highlight (unescaped)
549 * @return string highlighted text snippet, null (and not '') if not supported
551 function getTextSnippet($terms){
552 global $wgUser;
553 $this->initText();
554 list($contextlines,$contextchars) = SearchEngine::userHighlightPrefs($wgUser);
555 return $this->extractText( $this->mText, $terms, $contextlines, $contextchars);
559 * Default implementation of snippet extraction
561 * @param string $text
562 * @param array $terms Terms to highlight (unescaped)
563 * @param int $contextlines
564 * @param int $contextchars
565 * @return string
567 protected function extractText( $text, $terms, $contextlines, $contextchars ) {
568 global $wgLang, $wgContLang;
569 $fname = __METHOD__;
571 $lines = explode( "\n", $text );
573 foreach( $terms as $index => $term ) {
574 $terms[$index] = preg_quote( $term, '/' );
576 $terms = implode( '|', $terms );
577 $max = intval( $contextchars ) + 1;
578 $pat1 = "/(.*)($terms)(.{0,$max})/i";
580 $lineno = 0;
582 $extract = "";
583 wfProfileIn( "$fname-extract" );
584 foreach ( $lines as $line ) {
585 if ( 0 == $contextlines ) {
586 break;
588 ++$lineno;
589 $m = array();
590 if ( ! preg_match( $pat1, $line, $m ) ) {
591 continue;
593 --$contextlines;
594 $pre = $wgContLang->truncate( $m[1], -$contextchars, ' ... ' );
596 if ( count( $m ) < 3 ) {
597 $post = '';
598 } else {
599 $post = $wgContLang->truncate( $m[3], $contextchars, ' ... ' );
602 $found = $m[2];
604 $line = htmlspecialchars( $pre . $found . $post );
605 $pat2 = '/(' . $terms . ")/i";
606 $line = preg_replace( $pat2,
607 "<span class='searchmatch'>\\1</span>", $line );
609 $extract .= "${line}\n";
611 wfProfileOut( "$fname-extract" );
613 return $extract;
617 * @param array $terms terms to highlight
618 * @return string highlighted title, '' if not supported
620 function getTitleSnippet($terms){
621 return '';
625 * @param array $terms terms to highlight
626 * @return string highlighted redirect name (redirect to this page), '' if none or not supported
628 function getRedirectSnippet($terms){
629 return '';
633 * @return Title object for the redirect to this page, null if none or not supported
635 function getRedirectTitle(){
636 return null;
640 * @return string highlighted relevant section name, null if none or not supported
642 function getSectionSnippet(){
643 return '';
647 * @return Title object (pagename+fragment) for the section, null if none or not supported
649 function getSectionTitle(){
650 return null;
654 * @return string timestamp
656 function getTimestamp(){
657 return $this->mRevision->getTimestamp();
661 * @return int number of words
663 function getWordCount(){
664 $this->initText();
665 return str_word_count( $this->mText );
669 * @return int size in bytes
671 function getByteSize(){
672 $this->initText();
673 return strlen( $this->mText );
677 * @return boolean if hit has related articles
679 function hasRelated(){
680 return false;
684 * @return interwiki prefix of the title (return iw even if title is broken)
686 function getInterwikiPrefix(){
687 return '';
692 * @addtogroup Search
694 class SearchEngineDummy {
695 function search( $term ) {
696 return null;
698 function setLimitOffset($l, $o) {}
699 function legalSearchChars() {}
700 function update() {}
701 function setnamespaces() {}
702 function searchtitle() {}
703 function searchtext() {}