MediaWiki master
SearchResultThumbnailProvider.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Search;
4
5use File;
11use RepoGroup;
12
19
20 public const THUMBNAIL_SIZE = 60;
21
22 private RepoGroup $repoGroup;
23 private HookRunner $hookRunner;
24
25 public function __construct( RepoGroup $repoGroup, HookContainer $hookContainer ) {
26 $this->repoGroup = $repoGroup;
27 $this->hookRunner = new HookRunner( $hookContainer );
28 }
29
37 private function getFileNamesByPageId( array $identitiesByPageId ): array {
38 $fileIdentitiesByPageId = array_filter(
39 $identitiesByPageId,
40 static function ( PageIdentity $pageIdentity ) {
41 return $pageIdentity->getNamespace() === NS_FILE;
42 }
43 );
44
45 return array_map(
46 static function ( PageIdentity $pageIdentity ) {
47 return $pageIdentity->getDBkey();
48 },
49 $fileIdentitiesByPageId
50 );
51 }
52
60 public function buildSearchResultThumbnailFromFile( File $file, ?int $size = null ): ?SearchResultThumbnail {
61 $size ??= self::THUMBNAIL_SIZE;
62
63 $thumb = $file->transform( [ 'width' => $size ] );
64 if ( !$thumb || $thumb->isError() ) {
65 return null;
66 }
67
68 $urlUtils = MediaWikiServices::getInstance()->getUrlUtils();
69 return new SearchResultThumbnail(
70 $thumb->getFile()->getMimeType(),
71 null,
72 $thumb->getWidth(),
73 $thumb->getHeight(),
74 null,
75 $urlUtils->expand( $thumb->getUrl(), PROTO_RELATIVE ) ?? false,
76 $file->getName()
77 );
78 }
79
85 public function getThumbnails( array $pageIdentities, ?int $size = 60 ): array {
86 // add filenames for NS_FILE pages by default
87 $fileNamesByPageId = $this->getFileNamesByPageId( $pageIdentities );
88 $results = [];
89 foreach ( $fileNamesByPageId as $pageId => $fileName ) {
90 $file = $this->repoGroup->findFile( $fileName );
91 if ( !$file ) {
92 continue;
93 }
94 $thumbnail = $this->buildSearchResultThumbnailFromFile( $file, $size );
95 if ( $thumbnail ) {
96 $results[$pageId] = $thumbnail;
97 }
98 }
99
100 // allow extensions to inject additional thumbnails
101 $this->hookRunner->onSearchResultProvideThumbnail( $pageIdentities, $results, $size );
102
103 return $results;
104 }
105}
const NS_FILE
Definition Defines.php:71
const PROTO_RELATIVE
Definition Defines.php:212
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:79
getName()
Return the name of this file.
Definition File.php:347
transform( $params, $flags=0)
Transform a media file.
Definition File.php:1197
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Class that stores information about thumbnail, e.
__construct(RepoGroup $repoGroup, HookContainer $hookContainer)
buildSearchResultThumbnailFromFile(File $file, ?int $size=null)
Returns a SearchResultThumbnail instance for a given File/size combination.
Prioritized list of file repositories.
Definition RepoGroup.php:32
Interface for objects (potentially) representing an editable wiki page.