/** * Show single interwiki link * * @param SearchResult $result * @param string $lastInterwiki * @param array $terms * @param string $query * @param array $customCaptions iw prefix -> caption */ protected function showInterwikiHit($result, $lastInterwiki, $terms, $query, $customCaptions) { wfProfileIn(__METHOD__); global $wgContLang, $wgLang; if ($result->isBrokenTitle()) { wfProfileOut(__METHOD__); return "<!-- Broken link in search result -->\n"; } $t = $result->getTitle(); $titleSnippet = $result->getTitleSnippet($terms); if ($titleSnippet == '') { $titleSnippet = null; } $link = $this->sk->linkKnown($t, $titleSnippet); // format redirect if any $redirectTitle = $result->getRedirectTitle(); $redirectText = $result->getRedirectSnippet($terms); $redirect = ''; if (!is_null($redirectTitle)) { if ($redirectText == '') { $redirectText = null; } $redirect = "<span class='searchalttitle'>" . wfMsg('search-redirect', $this->sk->linkKnown($redirectTitle, $redirectText)) . "</span>"; } $out = ""; // display project name if (is_null($lastInterwiki) || $lastInterwiki != $t->getInterwiki()) { if (key_exists($t->getInterwiki(), $customCaptions)) { // captions from 'search-interwiki-custom' $caption = $customCaptions[$t->getInterwiki()]; } else { // default is to show the hostname of the other wiki which might suck // if there are many wikis on one hostname $parsed = parse_url($t->getFullURL()); $caption = wfMsg('search-interwiki-default', $parsed['host']); } // "more results" link (special page stuff could be localized, but we might not know target lang) $searchTitle = Title::newFromText($t->getInterwiki() . ":Special:Search"); $searchLink = $this->sk->linkKnown($searchTitle, wfMsg('search-interwiki-more'), array(), array('search' => $query, 'fulltext' => 'Search')); $out .= "</ul><div class='mw-search-interwiki-project'><span class='mw-search-interwiki-more'>\n\t\t\t\t{$searchLink}</span>{$caption}</div>\n<ul>"; } $out .= "<li>{$link} {$redirect}</li>\n"; wfProfileOut(__METHOD__); return $out; }
/** * Returns extra data for specific result and store it in SearchResult object. * @param SearchResult $result * @return array|null List of data as name => value or null if none present. */ public function augmentResult(SearchResult $result) { $id = $result->getTitle()->getArticleID(); if (!$id || !isset($this->extraData[$id])) { return null; } $result->setExtensionData($this->extraData[$id]); return $this->extraData[$id]; }
/** * Format a single hit result * @param SearchResult $result * @param string $terms partial regexp for highlighting terms */ function showHit($result, $terms) { $fname = 'SpecialSearch::showHit'; wfProfileIn($fname); global $wgUser, $wgContLang; $t = $result->getTitle(); if (is_null($t)) { wfProfileOut($fname); return "<!-- Broken link in search result -->\n"; } $sk =& $wgUser->getSkin(); $contextlines = $wgUser->getOption('contextlines'); if ('' == $contextlines) { $contextlines = 5; } $contextchars = $wgUser->getOption('contextchars'); if ('' == $contextchars) { $contextchars = 50; } $link = $sk->makeKnownLinkObj($t); $revision = Revision::newFromTitle($t); $text = $revision->getText(); $size = wfMsg('nbytes', strlen($text)); $lines = explode("\n", $text); $max = IntVal($contextchars) + 1; $pat1 = "/(.*)({$terms})(.{0,{$max}})/i"; $lineno = 0; $extract = ''; wfProfileIn("{$fname}-extract"); foreach ($lines as $line) { if (0 == $contextlines) { break; } ++$lineno; if (!preg_match($pat1, $line, $m)) { continue; } --$contextlines; $pre = $wgContLang->truncate($m[1], -$contextchars, '...'); if (count($m) < 3) { $post = ''; } else { $post = $wgContLang->truncate($m[3], $contextchars, '...'); } $found = $m[2]; $line = htmlspecialchars($pre . $found . $post); $pat2 = '/(' . $terms . ")/i"; $line = preg_replace($pat2, "<span class='searchmatch'>\\1</span>", $line); $extract .= "<br /><small>{$lineno}: {$line}</small>\n"; } wfProfileOut("{$fname}-extract"); wfProfileOut($fname); return "<li>{$link} ({$size}){$extract}</li>\n"; }
/** * Show single interwiki link * * @param SearchResult $result * @param string $lastInterwiki * @param string $query * @param array $customCaptions Interwiki prefix -> caption * * @return string */ protected function showInterwikiHit($result, $lastInterwiki, $query, $customCaptions) { if ($result->isBrokenTitle()) { return ''; } $title = $result->getTitle(); $titleSnippet = $result->getTitleSnippet(); if ($titleSnippet == '') { $titleSnippet = null; } $link = Linker::linkKnown($title, $titleSnippet); // format redirect if any $redirectTitle = $result->getRedirectTitle(); $redirectText = $result->getRedirectSnippet(); $redirect = ''; if (!is_null($redirectTitle)) { if ($redirectText == '') { $redirectText = null; } $redirect = "<span class='searchalttitle'>" . $this->msg('search-redirect')->rawParams(Linker::linkKnown($redirectTitle, $redirectText))->text() . "</span>"; } $out = ""; // display project name if (is_null($lastInterwiki) || $lastInterwiki != $title->getInterwiki()) { if (array_key_exists($title->getInterwiki(), $customCaptions)) { // captions from 'search-interwiki-custom' $caption = $customCaptions[$title->getInterwiki()]; } else { // default is to show the hostname of the other wiki which might suck // if there are many wikis on one hostname $parsed = wfParseUrl($title->getFullURL()); $caption = $this->msg('search-interwiki-default', $parsed['host'])->text(); } // "more results" link (special page stuff could be localized, but we might not know target lang) $searchTitle = Title::newFromText($title->getInterwiki() . ":Special:Search"); $searchLink = Linker::linkKnown($searchTitle, $this->msg('search-interwiki-more')->text(), array(), array('search' => $query, 'fulltext' => 'Search')); $out .= "</ul><div class='mw-search-interwiki-project'><span class='mw-search-interwiki-more'>\n\t\t\t\t{$searchLink}</span>{$caption}</div>\n<ul>"; } $out .= "<li>{$link} {$redirect}</li>\n"; return $out; }
/** * Format a single hit result * @param SearchResult $result * @param string $terms partial regexp for highlighting terms */ function showHit($result, $terms) { $fname = 'SpecialSearch::showHit'; wfProfileIn($fname); global $wgUser, $wgContLang, $wgLang; $t = $result->getTitle(); if (is_null($t)) { wfProfileOut($fname); return "<!-- Broken link in search result -->\n"; } $sk = $wgUser->getSkin(); $contextlines = $wgUser->getOption('contextlines', 5); $contextchars = $wgUser->getOption('contextchars', 50); $link = $sk->makeKnownLinkObj($t); //If page content is not readable, just return the title. //This is not quite safe, but better than showing excerpts from non-readable pages //Note that hiding the entry entirely would screw up paging. if (!$t->userCanRead()) { return "<li>{$link}</li>\n"; } $revision = Revision::newFromTitle($t); $text = $revision->getText(); $size = wfMsgExt('nbytes', array('parsemag', 'escape'), $wgLang->formatNum(strlen($text))); $lines = explode("\n", $text); $max = intval($contextchars) + 1; $pat1 = "/(.*)({$terms})(.{0,{$max}})/i"; $lineno = 0; $extract = ''; wfProfileIn("{$fname}-extract"); foreach ($lines as $line) { if (0 == $contextlines) { break; } ++$lineno; $m = array(); if (!preg_match($pat1, $line, $m)) { continue; } --$contextlines; $pre = $wgContLang->truncate($m[1], -$contextchars, '...'); if (count($m) < 3) { $post = ''; } else { $post = $wgContLang->truncate($m[3], $contextchars, '...'); } $found = $m[2]; $line = htmlspecialchars($pre . $found . $post); $pat2 = '/(' . $terms . ")/i"; $line = preg_replace($pat2, "<span class='searchmatch'>\\1</span>", $line); $extract .= "<br /><small>{$lineno}: {$line}</small>\n"; } wfProfileOut("{$fname}-extract"); wfProfileOut($fname); return "<li>{$link} ({$size}){$extract}</li>\n"; }