3 namespace MediaWiki\Widget\Search
;
8 use MediaWiki\Linker\LinkRenderer
;
9 use MediaWiki\MediaWikiServices
;
15 * Renders a 'full' multi-line search result with metadata.
18 * some *highlighted* *text* about the search result
19 * 5KB (651 words) - 12:40, 6 Aug 2016
21 class FullSearchResultWidget
implements SearchResultWidget
{
22 /** @var SpecialSearch */
23 protected $specialPage;
24 /** @var LinkRenderer */
25 protected $linkRenderer;
27 public function __construct( SpecialSearch
$specialPage, LinkRenderer
$linkRenderer ) {
28 $this->specialPage
= $specialPage;
29 $this->linkRenderer
= $linkRenderer;
33 * @param SearchResult $result The result to render
34 * @param string $terms Terms to be highlighted (@see SearchResult::getTextSnippet)
35 * @param int $position The result position, including offset
38 public function render( SearchResult
$result, $terms, $position ) {
39 // If the page doesn't *exist*... our search index is out of date.
40 // The least confusing at this point is to drop the result.
41 // You may get less results, but... on well. :P
42 if ( $result->isBrokenTitle() ||
$result->isMissingRevision() ) {
46 $link = $this->generateMainLinkHtml( $result, $terms, $position );
47 // If page content is not readable, just return ths title.
48 // This is not quite safe, but better than showing excerpts from
49 // non-readable pages. Note that hiding the entry entirely would
50 // screw up paging (really?).
51 $permissionManager = MediaWikiServices
::getInstance()->getPermissionManager();
52 if ( !$permissionManager->userCan(
53 'read', $this->specialPage
->getUser(), $result->getTitle()
55 return "<li>{$link}</li>";
58 $redirect = $this->generateRedirectHtml( $result );
59 $section = $this->generateSectionHtml( $result );
60 $category = $this->generateCategoryHtml( $result );
61 $date = $this->specialPage
->getLanguage()->userTimeAndDate(
62 $result->getTimestamp(),
63 $this->specialPage
->getUser()
65 list( $file, $desc, $thumb ) = $this->generateFileHtml( $result );
66 $snippet = $result->getTextSnippet( $terms );
68 $extract = "<div class='searchresult'>$snippet</div>";
73 if ( $thumb === null ) {
74 // If no thumb, then the description is about size
75 $desc = $this->generateSizeHtml( $result );
77 // Let hooks do their own final construction if desired.
78 // FIXME: Not sure why this is only for results without thumbnails,
79 // but keeping it as-is for now to prevent breaking hook consumers.
83 if ( !Hooks
::run( 'ShowSearchHit', [
84 $this->specialPage
, $result, $terms,
85 &$link, &$redirect, &$section, &$extract,
86 &$score, &$desc, &$date, &$related, &$html
92 // All the pieces have been collected. Now generate the final HTML
93 $joined = "{$link} {$redirect} {$category} {$section} {$file}";
94 $meta = $this->buildMeta( $desc, $date );
96 if ( $thumb === null ) {
98 "<div class='mw-search-result-heading'>{$joined}</div>" .
102 "<table class='searchResultImage'>" .
104 "<td style='width: 120px; text-align: center; vertical-align: top'>" .
107 "<td style='vertical-align: top'>" .
108 "{$joined} {$extract} {$meta}" .
114 return "<li class='mw-search-result'>{$html}</li>";
118 * Generates HTML for the primary call to action. It is
119 * typically the article title, but the search engine can
120 * return an exact snippet to use (typically the article
121 * title with highlighted words).
123 * @param SearchResult $result
124 * @param string $terms
125 * @param int $position
126 * @return string HTML
128 protected function generateMainLinkHtml( SearchResult
$result, $terms, $position ) {
129 $snippet = $result->getTitleSnippet();
130 if ( $snippet === '' ) {
133 $snippet = new HtmlArmor( $snippet );
136 // clone to prevent hook from changing the title stored inside $result
137 $title = clone $result->getTitle();
140 $attributes = [ 'data-serp-pos' => $position ];
141 Hooks
::run( 'ShowSearchHitTitle',
142 [ &$title, &$snippet, $result, $terms, $this->specialPage
, &$query, &$attributes ] );
144 $link = $this->linkRenderer
->makeLink(
155 * Generates an alternate title link, such as (redirect from <a>Foo</a>).
157 * @param string $msgKey i18n message used to wrap title
158 * @param Title|null $title The title to link to, or null to generate
159 * the message without a link. In that case $text must be non-null.
160 * @param string|null $text The text snippet to display, or null
162 * @return string HTML
164 protected function generateAltTitleHtml( $msgKey, Title
$title = null, $text ) {
165 $inner = $title === null
167 : $this->linkRenderer
->makeLink( $title, $text ?
new HtmlArmor( $text ) : null );
169 return "<span class='searchalttitle'>" .
170 $this->specialPage
->msg( $msgKey )->rawParams( $inner )->parse()
175 * @param SearchResult $result
176 * @return string HTML
178 protected function generateRedirectHtml( SearchResult
$result ) {
179 $title = $result->getRedirectTitle();
180 return $title === null
182 : $this->generateAltTitleHtml( 'search-redirect', $title, $result->getRedirectSnippet() );
186 * @param SearchResult $result
187 * @return string HTML
189 protected function generateSectionHtml( SearchResult
$result ) {
190 $title = $result->getSectionTitle();
191 return $title === null
193 : $this->generateAltTitleHtml( 'search-section', $title, $result->getSectionSnippet() );
197 * @param SearchResult $result
198 * @return string HTML
200 protected function generateCategoryHtml( SearchResult
$result ) {
201 $snippet = $result->getCategorySnippet();
203 ?
$this->generateAltTitleHtml( 'search-category', null, $snippet )
208 * @param SearchResult $result
209 * @return string HTML
211 protected function generateSizeHtml( SearchResult
$result ) {
212 $title = $result->getTitle();
213 if ( $title->getNamespace() === NS_CATEGORY
) {
214 $cat = Category
::newFromTitle( $title );
215 return $this->specialPage
->msg( 'search-result-category-size' )
216 ->numParams( $cat->getPageCount(), $cat->getSubcatCount(), $cat->getFileCount() )
218 // TODO: This is a bit odd...but requires changing the i18n message to fix
219 } elseif ( $result->getByteSize() !== null ||
$result->getWordCount() > 0 ) {
220 $lang = $this->specialPage
->getLanguage();
221 $bytes = $lang->formatSize( $result->getByteSize() );
222 $words = $result->getWordCount();
224 return $this->specialPage
->msg( 'search-result-size', $bytes )
225 ->numParams( $words )
233 * @param SearchResult $result
234 * @return array Three element array containing the main file html,
235 * a text description of the file, and finally the thumbnail html.
236 * If no thumbnail is available the second and third will be null.
238 protected function generateFileHtml( SearchResult
$result ) {
239 $title = $result->getTitle();
240 if ( $title->getNamespace() !== NS_FILE
) {
241 return [ '', null, null ];
244 if ( $result->isFileMatch() ) {
245 $html = "<span class='searchalttitle'>" .
246 $this->specialPage
->msg( 'search-file-match' )->escaped() .
255 $img = $result->getFile() ?
: MediaWikiServices
::getInstance()->getRepoGroup()
256 ->findFile( $title );
258 $thumb = $img->transform( [ 'width' => 120, 'height' => 120 ] );
260 $descHtml = $this->specialPage
->msg( 'parentheses' )
261 ->rawParams( $img->getShortDesc() )
263 $thumbHtml = $thumb->toHtml( [ 'desc-link' => true ] );
267 return [ $html, $descHtml, $thumbHtml ];
271 * @param string $desc HTML description of result, ex: size in bytes, or empty string
272 * @param string $date HTML representation of last edit date, or empty string
273 * @return string HTML A div combining $desc and $date with a separator in a <div>.
274 * If either is missing only one will be represented. If both are missing an empty
275 * string will be returned.
277 protected function buildMeta( $desc, $date ) {
278 if ( $desc && $date ) {
279 $meta = "{$desc} - {$date}";
288 return "<div class='mw-search-result-data'>{$meta}</div>";