MediaWiki  master
MediaFileHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Rest\Handler;
4 
5 use File;
6 use MediaFileTrait;
12 use RepoGroup;
15 
20  use MediaFileTrait;
21 
23  private $repoGroup;
24 
26  private $pageLookup;
27 
31  private $page = false;
32 
36  private $file = false;
37 
42  public function __construct(
45  ) {
46  $this->repoGroup = $repoGroup;
47  $this->pageLookup = $pageLookup;
48  }
49 
53  private function getPage(): ?ExistingPageRecord {
54  if ( $this->page === false ) {
55  $this->page = $this->pageLookup->getExistingPageByText(
56  $this->getValidatedParams()['title'], NS_FILE
57  );
58  }
59  return $this->page;
60  }
61 
65  private function getFile(): ?File {
66  if ( $this->file === false ) {
67  $page = $this->getPage();
68  $this->file =
69  $this->repoGroup->findFile( $page, [ 'private' => $this->getAuthority() ] ) ?: null;
70  }
71  return $this->file;
72  }
73 
79  public function run( $title ) {
80  $page = $this->getPage();
81 
82  if ( !$page ) {
83  throw new LocalizedHttpException(
84  MessageValue::new( 'rest-nonexistent-title' )->plaintextParams( $title ),
85  404
86  );
87  }
88 
89  if ( !$this->getAuthority()->authorizeRead( 'read', $page ) ) {
90  throw new LocalizedHttpException(
91  MessageValue::new( 'rest-permission-denied-title' )->plaintextParams( $title ),
92  403
93  );
94  }
95 
96  $fileObj = $this->getFile();
97  if ( !$fileObj || !$fileObj->exists() ) {
98  throw new LocalizedHttpException(
99  MessageValue::new( 'rest-cannot-load-file' )->plaintextParams( $title ),
100  404
101  );
102  }
103 
104  $response = $this->getResponse( $fileObj );
105  return $this->getResponseFactory()->createJson( $response );
106  }
107 
112  private function getResponse( File $file ): array {
113  list( $maxWidth, $maxHeight ) = self::getImageLimitsFromOption(
114  $this->getAuthority()->getUser(), 'imagesize'
115  );
116  list( $maxThumbWidth, $maxThumbHeight ) = self::getImageLimitsFromOption(
117  $this->getAuthority()->getUser(), 'thumbsize'
118  );
119  $transforms = [
120  'preferred' => [
121  'maxWidth' => $maxWidth,
122  'maxHeight' => $maxHeight
123  ],
124  'thumbnail' => [
125  'maxWidth' => $maxThumbWidth,
126  'maxHeight' => $maxThumbHeight
127  ]
128  ];
129 
130  return $this->getFileInfo( $file, $this->getAuthority(), $transforms );
131  }
132 
133  public function needsWriteAccess() {
134  return false;
135  }
136 
137  public function getParamSettings() {
138  return [
139  'title' => [
140  self::PARAM_SOURCE => 'path',
141  ParamValidator::PARAM_TYPE => 'string',
142  ParamValidator::PARAM_REQUIRED => true,
143  ],
144  ];
145  }
146 
151  protected function getETag(): ?string {
152  $file = $this->getFile();
153  if ( !$file || !$file->exists() ) {
154  return null;
155  }
156 
157  return '"' . $file->getSha1() . '"';
158  }
159 
164  protected function getLastModified(): ?string {
165  $file = $this->getFile();
166  if ( !$file || !$file->exists() ) {
167  return null;
168  }
169 
170  return $file->getTimestamp();
171  }
172 
176  protected function hasRepresentation() {
177  $file = $this->getFile();
178  return $file ? $file->exists() : false;
179  }
180 }
MediaWiki\Rest\Handler
Definition: AbstractContributionHandler.php:3
MediaWiki\Rest\Handler\MediaFileHandler\$file
File false null $file
Definition: MediaFileHandler.php:36
MediaWiki\Rest\Handler\MediaFileHandler
Handler class for media meta-data.
Definition: MediaFileHandler.php:19
MediaWiki\Rest\Handler\getResponseFactory
getResponseFactory()
Get the ResponseFactory which can be used to generate Response objects.
Definition: Handler.php:170
MediaWiki\Rest\Handler\MediaFileHandler\hasRepresentation
hasRepresentation()
Definition: MediaFileHandler.php:176
MediaWiki\Rest\Handler\MediaFileHandler\getETag
getETag()
Definition: MediaFileHandler.php:151
MediaWiki\Rest\Handler\MediaFileHandler\$pageLookup
PageLookup $pageLookup
Definition: MediaFileHandler.php:26
File\getTimestamp
getTimestamp()
Get the 14-character timestamp of the file upload.
Definition: File.php:2311
File\getSha1
getSha1()
Get the SHA-1 base 36 hash of the file.
Definition: File.php:2335
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
Definition: MessageValue.php:16
File\exists
exists()
Returns true if file exists in the repository.
Definition: File.php:1028
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:66
MediaWiki\Rest\Response
Definition: Response.php:8
$title
$title
Definition: testCompression.php:38
MediaWiki\Rest\Handler\getValidatedParams
getValidatedParams()
Fetch the validated parameters.
Definition: Handler.php:282
MediaWiki\Rest\Handler\MediaFileHandler\needsWriteAccess
needsWriteAccess()
Indicates whether this route requires write access.
Definition: MediaFileHandler.php:133
MediaWiki\Rest\Handler\MediaFileHandler\$repoGroup
RepoGroup $repoGroup
Definition: MediaFileHandler.php:23
Page\ExistingPageRecord
Data record representing a page that currently exists as an editable page on a wiki.
Definition: ExistingPageRecord.php:15
MediaWiki\Rest\Handler\MediaFileHandler\getPage
getPage()
Definition: MediaFileHandler.php:53
Page\PageLookup
Service interface for looking up infermation about wiki pages.
Definition: PageLookup.php:14
MediaWiki\Rest\Handler\MediaFileHandler\__construct
__construct(RepoGroup $repoGroup, PageLookup $pageLookup)
Definition: MediaFileHandler.php:42
RepoGroup
Prioritized list of file repositories.
Definition: RepoGroup.php:32
Wikimedia\Message\MessageValue\new
static new( $key, $params=[])
Static constructor for easier chaining of ->params() methods.
Definition: MessageValue.php:42
MediaWiki\Rest\Handler\MediaFileHandler\getParamSettings
getParamSettings()
Fetch ParamValidator settings for parameters.
Definition: MediaFileHandler.php:137
MediaWiki\Rest\Handler\MediaFileHandler\getLastModified
getLastModified()
Definition: MediaFileHandler.php:164
MediaWiki\Rest\Handler\MediaFileHandler\getFile
getFile()
Definition: MediaFileHandler.php:65
NS_FILE
const NS_FILE
Definition: Defines.php:70
MediaWiki\Rest\Handler\getAuthority
getAuthority()
Get the current acting authority.
Definition: Handler.php:148
MediaWiki\Rest\Handler\MediaFileHandler\run
run( $title)
Definition: MediaFileHandler.php:79
MediaWiki\Rest\Handler\MediaFileHandler\getResponse
getResponse(File $file)
Definition: MediaFileHandler.php:112
MediaWiki\Rest\Handler\MediaFileHandler\$page
ExistingPageRecord false null $page
Definition: MediaFileHandler.php:31
Wikimedia\ParamValidator\ParamValidator
Service for formatting and validating API parameters.
Definition: ParamValidator.php:42
MediaWiki\Rest\LocalizedHttpException
@newable
Definition: LocalizedHttpException.php:10
MediaWiki\Rest\SimpleHandler
Definition: SimpleHandler.php:15