mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/PageImages
synced 2024-11-28 02:00:42 +00:00
c75b00d14d
PageImages::getPropNames can return either array or string, and PageProps::getProperties accepts both. But the latter's return value will be different depending on the type of input: with an array as input, the returned array's values will be an associative array where the index is the propname; with a string, it's just the propvalue. This difference matters because the code below assumes an array with propname keys. Bug: T323152 Change-Id: I422951ec0cd5c651b32c65e88a557d49f2f22712
89 lines
2.6 KiB
PHP
89 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace PageImages\Hooks;
|
|
|
|
use MediaWiki\Page\PageIdentity;
|
|
use MediaWiki\Search\Hook\SearchResultProvideThumbnailHook;
|
|
use MediaWiki\Search\SearchResultThumbnailProvider;
|
|
use PageImages\PageImages;
|
|
use PageProps;
|
|
use RepoGroup;
|
|
|
|
class SearchResultProvideThumbnailHookHandler implements SearchResultProvideThumbnailHook {
|
|
|
|
/** @var SearchResultThumbnailProvider */
|
|
private $thumbnailProvider;
|
|
|
|
/** @var PageProps */
|
|
private $pageProps;
|
|
|
|
/** @var RepoGroup */
|
|
private $repoGroup;
|
|
|
|
/**
|
|
* @param SearchResultThumbnailProvider $thumbnailProvider
|
|
* @param PageProps $pageProps
|
|
* @param RepoGroup $repoGroup
|
|
*/
|
|
public function __construct(
|
|
SearchResultThumbnailProvider $thumbnailProvider,
|
|
PageProps $pageProps,
|
|
RepoGroup $repoGroup
|
|
) {
|
|
$this->thumbnailProvider = $thumbnailProvider;
|
|
$this->pageProps = $pageProps;
|
|
$this->repoGroup = $repoGroup;
|
|
}
|
|
|
|
/**
|
|
* Returns a list of fileNames for a given list of PageIdentity objects (outside of NS_FILE)
|
|
*
|
|
* @param PageIdentity[] $identitiesByPageId key-value array of where key
|
|
* is pageId, value is PageIdentity
|
|
* @return array
|
|
*/
|
|
private function getFileNamesByPageId( array $identitiesByPageId ): array {
|
|
$nonFileIdentitiesByPageId = array_filter(
|
|
$identitiesByPageId,
|
|
static function ( PageIdentity $pageIdentity ) {
|
|
return $pageIdentity->getNamespace() !== NS_FILE;
|
|
}
|
|
);
|
|
|
|
$propValues = $this->pageProps->getProperties(
|
|
$nonFileIdentitiesByPageId,
|
|
// T320661: only provide free images for search purposes
|
|
(array)PageImages::getPropNames( PageImages::LICENSE_FREE )
|
|
);
|
|
$fileNames = array_map( static function ( $prop ) {
|
|
return $prop[ PageImages::getPropName( false ) ]
|
|
?? $prop[ PageImages::getPropName( true ) ]
|
|
?? null;
|
|
}, $propValues );
|
|
|
|
return array_filter( $fileNames, static function ( $fileName ) {
|
|
return $fileName !== null;
|
|
} );
|
|
}
|
|
|
|
/**
|
|
* @param array $pageIdentities array that contain $pageId => PageIdentity.
|
|
* @param array &$results Placeholder for result. $pageId => SearchResultThumbnail
|
|
* @param int|null $size size of thumbnail height and width in points
|
|
*/
|
|
public function onSearchResultProvideThumbnail( array $pageIdentities, &$results, int $size = null ): void {
|
|
$fileNamesByPageId = $this->getFileNamesByPageId( $pageIdentities );
|
|
$results = $results ?? [];
|
|
foreach ( $fileNamesByPageId as $pageId => $fileName ) {
|
|
$file = $this->repoGroup->findFile( $fileName );
|
|
if ( !$file ) {
|
|
continue;
|
|
}
|
|
$thumbnail = $this->thumbnailProvider->buildSearchResultThumbnailFromFile( $file, $size );
|
|
if ( $thumbnail ) {
|
|
$results[$pageId] = $thumbnail;
|
|
}
|
|
}
|
|
}
|
|
}
|