2020-04-20 13:34:12 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PageImages\Hooks;
|
|
|
|
|
2021-05-14 23:16:41 +00:00
|
|
|
use MediaWiki\Page\PageIdentity;
|
2023-08-19 23:21:38 +00:00
|
|
|
use MediaWiki\Page\PageProps;
|
2022-08-23 12:24:38 +00:00
|
|
|
use MediaWiki\Search\Hook\SearchResultProvideThumbnailHook;
|
2022-09-01 12:38:32 +00:00
|
|
|
use MediaWiki\Search\SearchResultThumbnailProvider;
|
2020-04-20 13:34:12 +00:00
|
|
|
use PageImages\PageImages;
|
|
|
|
use RepoGroup;
|
|
|
|
|
2022-06-15 16:08:43 +00:00
|
|
|
class SearchResultProvideThumbnailHookHandler implements SearchResultProvideThumbnailHook {
|
2020-04-20 13:34:12 +00:00
|
|
|
|
2022-09-01 12:38:32 +00:00
|
|
|
/** @var SearchResultThumbnailProvider */
|
|
|
|
private $thumbnailProvider;
|
2020-04-20 13:34:12 +00:00
|
|
|
|
|
|
|
/** @var PageProps */
|
|
|
|
private $pageProps;
|
|
|
|
|
|
|
|
/** @var RepoGroup */
|
|
|
|
private $repoGroup;
|
|
|
|
|
|
|
|
/**
|
2022-09-01 12:38:32 +00:00
|
|
|
* @param SearchResultThumbnailProvider $thumbnailProvider
|
2020-04-20 13:34:12 +00:00
|
|
|
* @param PageProps $pageProps
|
|
|
|
* @param RepoGroup $repoGroup
|
|
|
|
*/
|
2022-09-01 12:38:32 +00:00
|
|
|
public function __construct(
|
|
|
|
SearchResultThumbnailProvider $thumbnailProvider,
|
|
|
|
PageProps $pageProps,
|
|
|
|
RepoGroup $repoGroup
|
|
|
|
) {
|
|
|
|
$this->thumbnailProvider = $thumbnailProvider;
|
2020-04-20 13:34:12 +00:00
|
|
|
$this->pageProps = $pageProps;
|
|
|
|
$this->repoGroup = $repoGroup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-09-01 12:38:32 +00:00
|
|
|
* Returns a list of fileNames for a given list of PageIdentity objects (outside of NS_FILE)
|
2020-04-20 13:34:12 +00:00
|
|
|
*
|
2022-09-01 12:38:32 +00:00
|
|
|
* @param PageIdentity[] $identitiesByPageId key-value array of where key
|
|
|
|
* is pageId, value is PageIdentity
|
2020-04-20 13:34:12 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2022-09-01 12:38:32 +00:00
|
|
|
private function getFileNamesByPageId( array $identitiesByPageId ): array {
|
|
|
|
$nonFileIdentitiesByPageId = array_filter(
|
|
|
|
$identitiesByPageId,
|
|
|
|
static function ( PageIdentity $pageIdentity ) {
|
|
|
|
return $pageIdentity->getNamespace() !== NS_FILE;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-04-20 13:34:12 +00:00
|
|
|
$propValues = $this->pageProps->getProperties(
|
2022-09-01 12:38:32 +00:00
|
|
|
$nonFileIdentitiesByPageId,
|
2022-11-07 14:49:55 +00:00
|
|
|
// T320661: only provide free images for search purposes
|
2022-11-16 08:14:13 +00:00
|
|
|
(array)PageImages::getPropNames( PageImages::LICENSE_FREE )
|
2020-04-20 13:34:12 +00:00
|
|
|
);
|
2021-05-12 09:23:38 +00:00
|
|
|
$fileNames = array_map( static function ( $prop ) {
|
2020-04-20 13:34:12 +00:00
|
|
|
return $prop[ PageImages::getPropName( false ) ]
|
|
|
|
?? $prop[ PageImages::getPropName( true ) ]
|
|
|
|
?? null;
|
|
|
|
}, $propValues );
|
|
|
|
|
2021-05-12 09:23:38 +00:00
|
|
|
return array_filter( $fileNames, static function ( $fileName ) {
|
2022-09-01 12:38:32 +00:00
|
|
|
return $fileName !== null;
|
2020-04-20 13:34:12 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-09-01 12:38:32 +00:00
|
|
|
* @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
|
2020-04-20 13:34:12 +00:00
|
|
|
*/
|
2024-10-26 13:09:15 +00:00
|
|
|
public function onSearchResultProvideThumbnail( array $pageIdentities, &$results, ?int $size = null ): void {
|
2022-09-01 12:38:32 +00:00
|
|
|
$fileNamesByPageId = $this->getFileNamesByPageId( $pageIdentities );
|
2023-10-13 22:55:37 +00:00
|
|
|
$results ??= [];
|
2022-09-01 12:38:32 +00:00
|
|
|
foreach ( $fileNamesByPageId as $pageId => $fileName ) {
|
2020-04-20 13:34:12 +00:00
|
|
|
$file = $this->repoGroup->findFile( $fileName );
|
|
|
|
if ( !$file ) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-09-01 12:38:32 +00:00
|
|
|
$thumbnail = $this->thumbnailProvider->buildSearchResultThumbnailFromFile( $file, $size );
|
|
|
|
if ( $thumbnail ) {
|
|
|
|
$results[$pageId] = $thumbnail;
|
2020-04-20 13:34:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|