MediaWiki  master
SearchResultThumbnailProvider.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Search;
4 
5 use File;
10 use RepoGroup;
11 
18 
19  public const THUMBNAIL_SIZE = 60;
20 
22  private $repoGroup;
23 
25  private $hookRunner;
26 
31  public function __construct( RepoGroup $repoGroup, HookContainer $hookContainer ) {
32  $this->repoGroup = $repoGroup;
33  $this->hookRunner = new HookRunner( $hookContainer );
34  }
35 
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 
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 
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 }
const NS_FILE
Definition: Defines.php:70
const PROTO_RELATIVE
Definition: Defines.php:193
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL using $wgServer (or one of its alternatives).
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:70
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Definition: HookRunner.php:568
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:30
Interface for objects (potentially) representing an editable wiki page.
getNamespace()
Returns the page's namespace number.
getDBkey()
Get the page title in DB key form.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42