Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
57.89% |
22 / 38 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
SearchResultThumbnailProvider | |
57.89% |
22 / 38 |
|
0.00% |
0 / 4 |
15.05 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getFileNamesByPageId | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
2 | |||
buildSearchResultThumbnailFromFile | |
92.31% |
12 / 13 |
|
0.00% |
0 / 1 |
3.00 | |||
getThumbnails | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
4.01 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Search; |
4 | |
5 | use File; |
6 | use MediaWiki\HookContainer\HookContainer; |
7 | use MediaWiki\HookContainer\HookRunner; |
8 | use MediaWiki\Page\PageIdentity; |
9 | use MediaWiki\Search\Entity\SearchResultThumbnail; |
10 | use RepoGroup; |
11 | |
12 | /** |
13 | * Find thumbnails for search results |
14 | * |
15 | * @since 1.40 |
16 | */ |
17 | class SearchResultThumbnailProvider { |
18 | |
19 | public const THUMBNAIL_SIZE = 60; |
20 | |
21 | /** @var RepoGroup */ |
22 | private $repoGroup; |
23 | |
24 | /** @var HookRunner */ |
25 | private $hookRunner; |
26 | |
27 | /** |
28 | * @param RepoGroup $repoGroup |
29 | * @param HookContainer $hookContainer |
30 | */ |
31 | public function __construct( RepoGroup $repoGroup, HookContainer $hookContainer ) { |
32 | $this->repoGroup = $repoGroup; |
33 | $this->hookRunner = new HookRunner( $hookContainer ); |
34 | } |
35 | |
36 | /** |
37 | * Returns a list of fileNames for a given list of PageIdentity objects (within NS_FILE) |
38 | * |
39 | * @param PageIdentity[] $identitiesByPageId key-value array of where key |
40 | * is pageId, value is PageIdentity |
41 | * @return array |
42 | */ |
43 | private function getFileNamesByPageId( array $identitiesByPageId ): array { |
44 | $fileIdentitiesByPageId = array_filter( |
45 | $identitiesByPageId, |
46 | static function ( PageIdentity $pageIdentity ) { |
47 | return $pageIdentity->getNamespace() === NS_FILE; |
48 | } |
49 | ); |
50 | |
51 | return array_map( |
52 | static function ( PageIdentity $pageIdentity ) { |
53 | return $pageIdentity->getDBkey(); |
54 | }, |
55 | $fileIdentitiesByPageId |
56 | ); |
57 | } |
58 | |
59 | /** |
60 | * Returns a SearchResultThumbnail instance for a given File/size combination. |
61 | * |
62 | * @param File $file |
63 | * @param int|null $size |
64 | * @return SearchResultThumbnail|null |
65 | */ |
66 | public function buildSearchResultThumbnailFromFile( File $file, int $size = null ): ?SearchResultThumbnail { |
67 | $size ??= self::THUMBNAIL_SIZE; |
68 | |
69 | $thumb = $file->transform( [ 'width' => $size ] ); |
70 | if ( !$thumb || $thumb->isError() ) { |
71 | return null; |
72 | } |
73 | |
74 | return new SearchResultThumbnail( |
75 | $thumb->getFile()->getMimeType(), |
76 | null, |
77 | $thumb->getWidth(), |
78 | $thumb->getHeight(), |
79 | null, |
80 | wfExpandUrl( $thumb->getUrl(), PROTO_RELATIVE ), |
81 | $file->getName() |
82 | ); |
83 | } |
84 | |
85 | /** |
86 | * @param PageIdentity[] $pageIdentities array that contains $pageId => PageIdentity. |
87 | * @param int|null $size size of thumbnail height and width in points |
88 | * @return SearchResultThumbnail[] array of $pageId => SearchResultThumbnail |
89 | */ |
90 | public function getThumbnails( array $pageIdentities, ?int $size = 60 ): array { |
91 | // add filenames for NS_FILE pages by default |
92 | $fileNamesByPageId = $this->getFileNamesByPageId( $pageIdentities ); |
93 | $results = []; |
94 | foreach ( $fileNamesByPageId as $pageId => $fileName ) { |
95 | $file = $this->repoGroup->findFile( $fileName ); |
96 | if ( !$file ) { |
97 | continue; |
98 | } |
99 | $thumbnail = $this->buildSearchResultThumbnailFromFile( $file, $size ); |
100 | if ( $thumbnail ) { |
101 | $results[$pageId] = $thumbnail; |
102 | } |
103 | } |
104 | |
105 | // allow extensions to inject additional thumbnails |
106 | $this->hookRunner->onSearchResultProvideThumbnail( $pageIdentities, $results, $size ); |
107 | |
108 | return $results; |
109 | } |
110 | } |