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
23 private $repoGroup;
24
26 private $hookRunner;
27
32 public function __construct( RepoGroup $repoGroup, HookContainer $hookContainer ) {
33 $this->repoGroup = $repoGroup;
34 $this->hookRunner = new HookRunner( $hookContainer );
35 }
36
44 private function getFileNamesByPageId( array $identitiesByPageId ): array {
45 $fileIdentitiesByPageId = array_filter(
46 $identitiesByPageId,
47 static function ( PageIdentity $pageIdentity ) {
48 return $pageIdentity->getNamespace() === NS_FILE;
49 }
50 );
51
52 return array_map(
53 static function ( PageIdentity $pageIdentity ) {
54 return $pageIdentity->getDBkey();
55 },
56 $fileIdentitiesByPageId
57 );
58 }
59
67 public function buildSearchResultThumbnailFromFile( File $file, int $size = null ): ?SearchResultThumbnail {
68 $size ??= self::THUMBNAIL_SIZE;
69
70 $thumb = $file->transform( [ 'width' => $size ] );
71 if ( !$thumb || $thumb->isError() ) {
72 return null;
73 }
74
75 $urlUtils = MediaWikiServices::getInstance()->getUrlUtils();
76 return new SearchResultThumbnail(
77 $thumb->getFile()->getMimeType(),
78 null,
79 $thumb->getWidth(),
80 $thumb->getHeight(),
81 null,
82 $urlUtils->expand( $thumb->getUrl(), PROTO_RELATIVE ) ?? false,
83 $file->getName()
84 );
85 }
86
92 public function getThumbnails( array $pageIdentities, ?int $size = 60 ): array {
93 // add filenames for NS_FILE pages by default
94 $fileNamesByPageId = $this->getFileNamesByPageId( $pageIdentities );
95 $results = [];
96 foreach ( $fileNamesByPageId as $pageId => $fileName ) {
97 $file = $this->repoGroup->findFile( $fileName );
98 if ( !$file ) {
99 continue;
100 }
101 $thumbnail = $this->buildSearchResultThumbnailFromFile( $file, $size );
102 if ( $thumbnail ) {
103 $results[$pageId] = $thumbnail;
104 }
105 }
106
107 // allow extensions to inject additional thumbnails
108 $this->hookRunner->onSearchResultProvideThumbnail( $pageIdentities, $results, $size );
109
110 return $results;
111 }
112}
const NS_FILE
Definition Defines.php:71
const PROTO_RELATIVE
Definition Defines.php:206
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:76
getName()
Return the name of this file.
Definition File.php:344
transform( $params, $flags=0)
Transform a media file.
Definition File.php:1182
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.