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