MediaWiki master
FullSearchResultWidget.php
Go to the documentation of this file.
1<?php
2
4
23
32
33 public const THUMBNAIL_SIZE = 90;
34
37 private HookRunner $hookRunner;
38 private RepoGroup $repoGroup;
39 private SearchResultThumbnailProvider $thumbnailProvider;
40 private UserOptionsManager $userOptionsManager;
41
43 private $thumbnailPlaceholderHtml;
44
45 public function __construct(
48 HookContainer $hookContainer,
49 RepoGroup $repoGroup,
50 SearchResultThumbnailProvider $thumbnailProvider,
51 UserOptionsManager $userOptionsManager
52 ) {
53 $this->specialPage = $specialPage;
54 $this->linkRenderer = $linkRenderer;
55 $this->hookRunner = new HookRunner( $hookContainer );
56 $this->repoGroup = $repoGroup;
57 $this->thumbnailProvider = $thumbnailProvider;
58 $this->userOptionsManager = $userOptionsManager;
59 }
60
66 public function render( SearchResult $result, $position ) {
67 // If the page doesn't *exist*... our search index is out of date.
68 // The least confusing at this point is to drop the result.
69 // You may get less results, but... on well. :P
70 if ( $result->isBrokenTitle() || $result->isMissingRevision() ) {
71 return '';
72 }
73
74 $link = $this->generateMainLinkHtml( $result, $position );
75 // If page content is not readable, just return ths title.
76 // This is not quite safe, but better than showing excerpts from
77 // non-readable pages. Note that hiding the entry entirely would
78 // screw up paging (really?).
79 if ( !$this->specialPage->getAuthority()->definitelyCan( 'read', $result->getTitle() ) ) {
80 return Html::rawElement( 'li', [], $link );
81 }
82
83 $redirect = $this->generateRedirectHtml( $result );
84 $section = $this->generateSectionHtml( $result );
85 $category = $this->generateCategoryHtml( $result );
86 $date = htmlspecialchars(
87 $this->specialPage->getLanguage()->userTimeAndDate(
88 $result->getTimestamp(),
89 $this->specialPage->getUser()
90 )
91 );
92 [ $file, $desc, $thumb ] = $this->generateFileHtml( $result );
93 $snippet = $result->getTextSnippet();
94 if ( $snippet ) {
95 $snippetWithEllipsis = $snippet . $this->specialPage->msg( 'ellipsis' );
96 $extract = Html::rawElement( 'div', [ 'class' => 'searchresult' ], $snippetWithEllipsis );
97 } else {
98 $extract = '';
99 }
100
101 if ( $result->getTitle() && $result->getTitle()->getNamespace() !== NS_FILE ) {
102 // If no file, then the description is about size
103 $desc = $this->generateSizeHtml( $result );
104
105 // Let hooks do their own final construction if desired.
106 // FIXME: Not sure why this is only for results without thumbnails,
107 // but keeping it as-is for now to prevent breaking hook consumers.
108 $html = null;
109 $score = '';
110 $related = '';
111 // TODO: remove this instanceof and always pass [], let implementors do the cast if
112 // they want to be SearchDatabase specific
113 $terms = $result instanceof \MediaWiki\Search\SqlSearchResult ? $result->getTermMatches() : [];
114 if ( !$this->hookRunner->onShowSearchHit( $this->specialPage, $result,
115 $terms, $link, $redirect, $section, $extract, $score,
116 // @phan-suppress-next-line PhanTypeMismatchArgument Type mismatch on pass-by-ref args
117 $desc, $date, $related, $html )
118 ) {
119 return $html;
120 }
121 }
122
123 // All the pieces have been collected. Now generate the final HTML
124 $joined = "{$link} {$redirect} {$category} {$section} {$file}";
125 $meta = $this->buildMeta( $desc, $date );
126
127 // Text portion of the search result
128 $html = Html::rawElement(
129 'div',
130 [ 'class' => 'mw-search-result-heading' ],
131 $joined
132 );
133 $html .= $extract . ' ' . $meta;
134
135 // If the result has a thumbnail, place it next to the text block
136 if ( $thumb ) {
137 $gridCells = Html::rawElement(
138 'div',
139 [ 'class' => 'searchResultImage-thumbnail' ],
140 $thumb
141 ) . Html::rawElement(
142 'div',
143 [ 'class' => 'searchResultImage-text' ],
144 $html
145 );
146 $html = Html::rawElement(
147 'div',
148 [ 'class' => 'searchResultImage' ],
149 $gridCells
150 );
151 }
152
153 return Html::rawElement(
154 'li',
155 [ 'class' => [ 'mw-search-result', 'mw-search-result-ns-' . $result->getTitle()->getNamespace() ] ],
156 $html
157 );
158 }
159
170 protected function generateMainLinkHtml( SearchResult $result, $position ) {
171 $snippet = $result->getTitleSnippet();
172 if ( $snippet === '' ) {
173 $snippet = null;
174 } else {
175 $snippet = new HtmlArmor( $snippet );
176 }
177
178 // clone to prevent hook from changing the title stored inside $result
179 $title = clone $result->getTitle();
180 $query = [];
181
182 $attributes = [ 'data-serp-pos' => $position ];
183 $this->hookRunner->onShowSearchHitTitle( $title, $snippet, $result,
184 $result instanceof \MediaWiki\Search\SqlSearchResult ? $result->getTermMatches() : [],
185 // @phan-suppress-next-line PhanTypeMismatchArgument Type mismatch on pass-by-ref args
186 $this->specialPage, $query, $attributes );
187
188 $link = $this->linkRenderer->makeLink(
189 $title,
190 $snippet,
191 $attributes,
192 $query
193 );
194
195 return $link;
196 }
197
208 protected function generateAltTitleHtml( $msgKey, ?Title $title, $text ) {
209 $inner = $title === null
210 ? $text
211 : $this->linkRenderer->makeLink( $title, $text ? new HtmlArmor( $text ) : null );
212
213 return "<span class='searchalttitle'>" .
214 $this->specialPage->msg( $msgKey )->rawParams( $inner )->parse()
215 . "</span>";
216 }
217
222 protected function generateRedirectHtml( SearchResult $result ) {
223 $title = $result->getRedirectTitle();
224 return $title === null
225 ? ''
226 : $this->generateAltTitleHtml( 'search-redirect', $title, $result->getRedirectSnippet() );
227 }
228
233 protected function generateSectionHtml( SearchResult $result ) {
234 $title = $result->getSectionTitle();
235 return $title === null
236 ? ''
237 : $this->generateAltTitleHtml( 'search-section', $title, $result->getSectionSnippet() );
238 }
239
244 protected function generateCategoryHtml( SearchResult $result ) {
245 $snippet = $result->getCategorySnippet();
246 return $snippet
247 ? $this->generateAltTitleHtml( 'search-category', null, $snippet )
248 : '';
249 }
250
255 protected function generateSizeHtml( SearchResult $result ) {
256 $title = $result->getTitle();
257 if ( $title->getNamespace() === NS_CATEGORY ) {
258 $cat = Category::newFromTitle( $title );
259 return $this->specialPage->msg( 'search-result-category-size' )
260 ->numParams( $cat->getMemberCount(), $cat->getSubcatCount(), $cat->getFileCount() )
261 ->escaped();
262 // TODO: This is a bit odd...but requires changing the i18n message to fix
263 } elseif ( $result->getByteSize() !== null || $result->getWordCount() > 0 ) {
264 return $this->specialPage->msg( 'search-result-size' )
265 ->sizeParams( $result->getByteSize() )
266 ->numParams( $result->getWordCount() )
267 ->escaped();
268 }
269
270 return '';
271 }
272
279 protected function generateFileHtml( SearchResult $result ) {
280 $title = $result->getTitle();
281 // don't assume that result is a valid title; e.g. could be an interwiki link target
282 if ( $title === null || !$title->canExist() ) {
283 return [ '', null, null ];
284 }
285
286 $html = '';
287 if ( $result->isFileMatch() ) {
288 $html = Html::element( 'span',
289 [ 'class' => 'searchalttitle' ],
290 $this->specialPage->msg( 'search-file-match' )->text()
291 );
292 }
293
294 $allowExtraThumbsFromRequest = $this->specialPage->getRequest()->getVal( 'search-thumbnail-extra-namespaces' );
295 $allowExtraThumbsFromPreference = $this->userOptionsManager->getOption(
296 $this->specialPage->getUser(),
297 'search-thumbnail-extra-namespaces'
298 );
299 $allowExtraThumbs = (bool)( $allowExtraThumbsFromRequest ?? $allowExtraThumbsFromPreference );
300 if ( !$allowExtraThumbs && $title->getNamespace() !== NS_FILE ) {
301 return [ $html, null, null ];
302 }
303
304 $thumbnail = $this->getThumbnail( $result, self::THUMBNAIL_SIZE );
305 $thumbnailName = $thumbnail ? $thumbnail->getName() : null;
306 if ( !$thumbnailName ) {
307 return [ $html, null, $this->generateThumbnailHtml( $result ) ];
308 }
309
310 $img = $this->repoGroup->findFile( $thumbnailName );
311 if ( !$img ) {
312 return [ $html, null, $this->generateThumbnailHtml( $result ) ];
313 }
314
315 // File::getShortDesc() is documented to return HTML, but many handlers used to incorrectly
316 // return plain text (T395834), so sanitize it in case the same bug is present in extensions.
317 $unsafeShortDesc = $img->getShortDesc();
318 $shortDesc = Sanitizer::removeSomeTags( $unsafeShortDesc );
319
320 return [
321 $html,
322 $this->specialPage->msg( 'parentheses' )->rawParams( $shortDesc )->escaped(),
323 $this->generateThumbnailHtml( $result, $thumbnail )
324 ];
325 }
326
332 private function getThumbnail( SearchResult $result, int $size ): ?SearchResultThumbnail {
333 $title = $result->getTitle();
334 // don't assume that result is a valid title; e.g. could be an interwiki link target
335 if ( $title === null || !$title->canExist() ) {
336 return null;
337 }
338
339 $thumbnails = $this->thumbnailProvider->getThumbnails( [ $title->getArticleID() => $title ], $size );
340
341 return $thumbnails[ $title->getArticleID() ] ?? null;
342 }
343
349 private function generateThumbnailHtml( SearchResult $result, ?SearchResultThumbnail $thumbnail = null ): ?string {
350 $title = $result->getTitle();
351 // don't assume that result is a valid title; e.g. could be an interwiki link target
352 if ( $title === null || !$title->canExist() ) {
353 return null;
354 }
355
356 $namespacesWithThumbnails = $this->specialPage->getConfig()->get( MainConfigNames::ThumbnailNamespaces );
357 $showThumbnail = in_array( $title->getNamespace(), $namespacesWithThumbnails );
358 if ( !$showThumbnail ) {
359 return null;
360 }
361
362 $thumbnailName = $thumbnail ? $thumbnail->getName() : null;
363 if ( !$thumbnail || !$thumbnailName ) {
364 return $this->generateThumbnailPlaceholderHtml();
365 }
366
367 $img = $this->repoGroup->findFile( $thumbnailName );
368 if ( !$img ) {
369 return $this->generateThumbnailPlaceholderHtml();
370 }
371
372 $thumb = $this->transformThumbnail( $img, $thumbnail );
373 if ( $thumb ) {
374 if ( $title->getNamespace() === NS_FILE ) {
375 // don't use a custom link, just use traditional thumbnail HTML
376 // @phan-suppress-next-line SecurityCheck-DoubleEscaped
377 return $thumb->toHtml( [
378 'desc-link' => true,
379 'loading' => 'lazy',
380 'alt' => $this->specialPage->msg( 'search-thumbnail-alt', $title->getPrefixedText() ),
381 ] );
382 }
383
384 // thumbnails for non-file results should link to the relevant title
385 // @phan-suppress-next-line SecurityCheck-DoubleEscaped
386 return $thumb->toHtml( [
387 'desc-link' => true,
388 'custom-title-link' => $title,
389 'loading' => 'lazy',
390 'alt' => $this->specialPage->msg( 'search-thumbnail-alt', $title->getPrefixedText() ),
391 ] );
392 }
393
394 return $this->generateThumbnailPlaceholderHtml();
395 }
396
402 private function transformThumbnail( File $img, SearchResultThumbnail $thumbnail ) {
403 $optimalThumbnailWidth = $thumbnail->getWidth();
404
405 // $thumb will have rescaled to fit within a <$size>x<$size> bounding
406 // box, but we want it to cover a full square (at the cost of losing
407 // some of the edges)
408 // instead of the largest side matching up with $size, we want the
409 // smallest size to match (or exceed) $size
410 $thumbnailMaxDimension = max( $thumbnail->getWidth(), $thumbnail->getHeight() );
411 $thumbnailMinDimension = min( $thumbnail->getWidth(), $thumbnail->getHeight() );
412 $rescaleCoefficient = $thumbnailMinDimension
413 ? $thumbnailMaxDimension / $thumbnailMinDimension : 1;
414
415 // we'll only deal with width from now on since conventions for
416 // standard sizes have formed around width; height will simply
417 // follow according to aspect ratio
418 $rescaledWidth = (int)round( $rescaleCoefficient * $thumbnail->getWidth() );
419
420 // we'll also be looking at $wgThumbLimits to ensure that we pick
421 // from within the predefined list of sizes
422 // NOTE: only do this when there is a difference in the rescaled
423 // size vs the original thumbnail size - some media types are
424 // different and thumb limits don't matter (e.g. for audio, the
425 // player must remain at the size we want, regardless of whether or
426 // not it fits the thumb limits, which in this case are irrelevant)
427 if ( $rescaledWidth !== $thumbnail->getWidth() ) {
428 $thumbLimits = $this->specialPage->getConfig()->get( MainConfigNames::ThumbLimits );
429 $largerThumbLimits = array_filter(
430 $thumbLimits,
431 static function ( $limit ) use ( $rescaledWidth ) {
432 return $limit >= $rescaledWidth;
433 }
434 );
435 $optimalThumbnailWidth = $largerThumbLimits ? min( $largerThumbLimits ) : max( $thumbLimits );
436 }
437
438 return $img->transform( [ 'width' => $optimalThumbnailWidth ] );
439 }
440
441 private function generateThumbnailPlaceholderHtml(): string {
442 if ( $this->thumbnailPlaceholderHtml ) {
443 return $this->thumbnailPlaceholderHtml;
444 }
445
446 $path = MW_INSTALL_PATH . '/resources/lib/ooui/themes/wikimediaui/images/icons/imageLayoutFrameless.svg';
447 $this->thumbnailPlaceholderHtml = Html::rawElement(
448 'div',
449 [
450 'class' => 'searchResultImage-thumbnail-placeholder',
451 'aria-hidden' => 'true',
452 ],
453 file_get_contents( $path )
454 );
455 return $this->thumbnailPlaceholderHtml;
456 }
457
465 protected function buildMeta( $desc, $date ) {
466 if ( $desc && $date ) {
467 $meta = "{$desc} - {$date}";
468 } elseif ( $desc ) {
469 $meta = $desc;
470 } elseif ( $date ) {
471 $meta = $date;
472 } else {
473 return '';
474 }
475
476 return "<div class='mw-search-result-data'>{$meta}</div>";
477 }
478}
const NS_FILE
Definition Defines.php:57
const NS_CATEGORY
Definition Defines.php:65
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:68
Category objects are immutable, strictly speaking.
Definition Category.php:29
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:79
Prioritized list of file repositories.
Definition RepoGroup.php:30
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
This class is a collection of static functions that serve two purposes:
Definition Html.php:43
Class that generates HTML for internal links.
A class containing constants representing the names of configuration variables.
const ThumbnailNamespaces
Name constant for the ThumbnailNamespaces setting, for use with Config::get()
Base class for the output of MediaHandler::doTransform() and File::transform().
Media transform output for images.
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:32
Class that stores information about thumbnail, e.
An abstract base class representing a search engine result.
isMissingRevision()
Check if target page is missing, happens when index is out of date.
isFileMatch()
Did this match file contents (eg: PDF/DJVU)?
isBrokenTitle()
Check if this is result points to an invalid title.
Renders a 'full' multi-line search result with metadata.
generateAltTitleHtml( $msgKey, ?Title $title, $text)
Generates an alternate title link, such as (redirect from Foo).
generateMainLinkHtml(SearchResult $result, $position)
Generates HTML for the primary call to action.
__construct(SpecialSearch $specialPage, LinkRenderer $linkRenderer, HookContainer $hookContainer, RepoGroup $repoGroup, SearchResultThumbnailProvider $thumbnailProvider, UserOptionsManager $userOptionsManager)
Run text & title search and display the output.
Represents a title within MediaWiki.
Definition Title.php:69
A service class to control user options.
Marks HTML that shouldn't be escaped.
Definition HtmlArmor.php:18
Renders a single search result to HTML.
element(SerializerNode $parent, SerializerNode $node, $contents)
Helper trait for implementations \DAO.