diff options
Diffstat (limited to 'includes/search/SearchEngine.php')
-rw-r--r-- | includes/search/SearchEngine.php | 83 |
1 files changed, 45 insertions, 38 deletions
diff --git a/includes/search/SearchEngine.php b/includes/search/SearchEngine.php index 27a321ac..6b3e62b1 100644 --- a/includes/search/SearchEngine.php +++ b/includes/search/SearchEngine.php @@ -45,7 +45,7 @@ class SearchEngine { */ protected $db; - function __construct($db = null) { + function __construct( $db = null ) { if ( $db ) { $this->db = $db; } else { @@ -58,7 +58,7 @@ class SearchEngine { * If title searches are not supported or disabled, return null. * STUB * - * @param $term String: raw search term + * @param string $term raw search term * @return SearchResultSet */ function searchText( $term ) { @@ -70,7 +70,7 @@ class SearchEngine { * If title searches are not supported or disabled, return null. * STUB * - * @param $term String: raw search term + * @param string $term raw search term * @return SearchResultSet */ function searchTitle( $term ) { @@ -118,7 +118,7 @@ class SearchEngine { * on text to be used for searching or updating search index. * Default implementation does nothing (simply returns $string). * - * @param $string string: String to process + * @param string $string String to process * @return string */ public function normalizeText( $string ) { @@ -163,7 +163,7 @@ class SearchEngine { /** * Really find the title match. - * @return null|\Title + * @return null|Title */ private static function getNearMatchInternal( $searchterm ) { global $wgContLang, $wgEnableSearchContributorsByIP; @@ -183,10 +183,15 @@ class SearchEngine { # Exact match? No need to look further. $title = Title::newFromText( $term ); - if ( is_null( $title ) ){ + if ( is_null( $title ) ) { return null; } + # Try files if searching in the Media: namespace + if ( $title->getNamespace() == NS_MEDIA ) { + $title = Title::makeTitle( NS_FILE, $title->getText() ); + } + if ( $title->isSpecialPage() || $title->isExternal() || $title->exists() ) { return $title; } @@ -197,22 +202,23 @@ class SearchEngine { return $title; } + if ( !wfRunHooks( 'SearchAfterNoDirectMatch', array( $term, &$title ) ) ) { + return $title; + } + # Now try all lower case (i.e. first letter capitalized) - # $title = Title::newFromText( $wgContLang->lc( $term ) ); if ( $title && $title->exists() ) { return $title; } # Now try capitalized string - # $title = Title::newFromText( $wgContLang->ucwords( $term ) ); if ( $title && $title->exists() ) { return $title; } # Now try all upper case - # $title = Title::newFromText( $wgContLang->uc( $term ) ); if ( $title && $title->exists() ) { return $title; @@ -233,7 +239,6 @@ class SearchEngine { $title = Title::newFromText( $searchterm ); - # Entering an IP address goes to the contributions page if ( $wgEnableSearchContributorsByIP ) { if ( ( $title->getNamespace() == NS_USER && User::isIP( $title->getText() ) ) @@ -242,7 +247,6 @@ class SearchEngine { } } - # Entering a user goes to the user page whether it's there or not if ( $title->getNamespace() == NS_USER ) { return $title; @@ -669,7 +673,6 @@ class SearchResultTooMany { # # Some search engines may bail out if too many matches are found } - /** * @todo FIXME: This class is horribly factored. It would probably be better to * have a useful base class to which you pass some standard information, then @@ -791,21 +794,26 @@ class SearchResult { */ protected function initText() { if ( !isset( $this->mText ) ) { - if ( $this->mRevision != null ) - $this->mText = $this->mRevision->getText(); - else // TODO: can we fetch raw wikitext for commons images? + if ( $this->mRevision != null ) { + //TODO: if we could plug in some code that knows about special content models *and* about + // special features of the search engine, the search could benefit. + $content = $this->mRevision->getContent(); + $this->mText = $content ? $content->getTextForSearchIndex() : ''; + } else { // TODO: can we fetch raw wikitext for commons images? $this->mText = ''; - + } } } /** - * @param $terms Array: terms to highlight + * @param array $terms terms to highlight * @return String: highlighted text snippet, null (and not '') if not supported */ function getTextSnippet( $terms ) { global $wgUser, $wgAdvancedSearchHighlighting; $this->initText(); + + // TODO: make highliter take a content object. Make ContentHandler a factory for SearchHighliter. list( $contextlines, $contextchars ) = SearchEngine::userHighlightPrefs( $wgUser ); $h = new SearchHighlighter(); if ( $wgAdvancedSearchHighlighting ) @@ -815,7 +823,7 @@ class SearchResult { } /** - * @param $terms Array: terms to highlight + * @param array $terms terms to highlight * @return String: highlighted title, '' if not supported */ function getTitleSnippet( $terms ) { @@ -823,7 +831,7 @@ class SearchResult { } /** - * @param $terms Array: terms to highlight + * @param array $terms terms to highlight * @return String: highlighted redirect name (redirect to this page), '' if none or not supported */ function getRedirectSnippet( $terms ) { @@ -934,7 +942,7 @@ class SearchHighlighter { * Default implementation of wikitext highlighting * * @param $text String - * @param $terms Array: terms to highlight (unescaped) + * @param array $terms terms to highlight (unescaped) * @param $contextlines Integer * @param $contextchars Integer * @return String @@ -962,7 +970,7 @@ class SearchHighlighter { } $spat .= '/'; $textExt = array(); // text extracts - $otherExt = array(); // other extracts + $otherExt = array(); // other extracts wfProfileIn( "$fname-split" ); $start = 0; $textLen = strlen( $text ); @@ -1131,8 +1139,8 @@ class SearchHighlighter { // add more lines $add = $index + 1; while ( $len < $targetchars - 20 - && array_key_exists( $add, $all ) - && !array_key_exists( $add, $snippets ) ) { + && array_key_exists( $add, $all ) + && !array_key_exists( $add, $snippets ) ) { $offsets[$add] = 0; $tt = "\n" . $this->extract( $all[$add], 0, $targetchars - $len, $offsets[$add] ); $extended[$add] = $tt; @@ -1142,7 +1150,7 @@ class SearchHighlighter { } } - // $snippets = array_map('htmlspecialchars', $extended); + // $snippets = array_map( 'htmlspecialchars', $extended ); $snippets = $extended; $last = - 1; $extract = ''; @@ -1177,7 +1185,7 @@ class SearchHighlighter { /** * Split text into lines and add it to extracts array * - * @param $extracts Array: index -> $line + * @param array $extracts index -> $line * @param $count Integer * @param $text String */ @@ -1232,7 +1240,7 @@ class SearchHighlighter { $posEnd = $end; } - if ( $end > $start ) { + if ( $end > $start ) { return substr( $text, $start, $end - $start ); } else { return ''; @@ -1272,12 +1280,12 @@ class SearchHighlighter { /** * Search extracts for a pattern, and return snippets * - * @param $pattern String: regexp for matching lines - * @param $extracts Array: extracts to search + * @param string $pattern regexp for matching lines + * @param array $extracts extracts to search * @param $linesleft Integer: number of extracts to make * @param $contextchars Integer: length of snippet - * @param $out Array: map for highlighted snippets - * @param $offsets Array: map of starting points of snippets + * @param array $out map for highlighted snippets + * @param array $offsets map of starting points of snippets * @protected */ function process( $pattern, $extracts, &$linesleft, &$contextchars, &$out, &$offsets ) { @@ -1321,12 +1329,12 @@ class SearchHighlighter { $fname = __METHOD__; wfProfileIn( $fname ); - // $text = preg_replace("/'{2,5}/", "", $text); - // $text = preg_replace("/\[[a-z]+:\/\/[^ ]+ ([^]]+)\]/", "\\2", $text); - // $text = preg_replace("/\[\[([^]|]+)\]\]/", "\\1", $text); - // $text = preg_replace("/\[\[([^]]+\|)?([^|]]+)\]\]/", "\\2", $text); - // $text = preg_replace("/\\{\\|(.*?)\\|\\}/", "", $text); - // $text = preg_replace("/\\[\\[[A-Za-z_-]+:([^|]+?)\\]\\]/", "", $text); + // $text = preg_replace( "/'{2,5}/", "", $text ); + // $text = preg_replace( "/\[[a-z]+:\/\/[^ ]+ ([^]]+)\]/", "\\2", $text ); + // $text = preg_replace( "/\[\[([^]|]+)\]\]/", "\\1", $text ); + // $text = preg_replace( "/\[\[([^]]+\|)?([^|]]+)\]\]/", "\\2", $text ); + // $text = preg_replace( "/\\{\\|(.*?)\\|\\}/", "", $text ); + // $text = preg_replace( "/\\[\\[[A-Za-z_-]+:([^|]+?)\\]\\]/", "", $text ); $text = preg_replace( "/\\{\\{([^|]+?)\\}\\}/", "", $text ); $text = preg_replace( "/\\{\\{([^|]+\\|)(.*?)\\}\\}/", "\\2", $text ); $text = preg_replace( "/\\[\\[([^|]+?)\\]\\]/", "\\1", $text ); @@ -1408,8 +1416,7 @@ class SearchHighlighter { $line = htmlspecialchars( $pre . $found . $post ); $pat2 = '/(' . $terms . ")/i"; - $line = preg_replace( $pat2, - "<span class='searchmatch'>\\1</span>", $line ); + $line = preg_replace( $pat2, "<span class='searchmatch'>\\1</span>", $line ); $extract .= "${line}\n"; } |