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;
11 use RepoGroup;
12 use RequestContext;
13 use Title;
14 use User;
17 
22  use MediaFileTrait;
23 
26 
28  private $repoGroup;
29 
31  private $user;
32 
36  private $title = null;
37 
41  private $file = null;
42 
47  public function __construct(
50  ) {
51  $this->permissionManager = $permissionManager;
52  $this->repoGroup = $repoGroup;
53 
54  // @todo Inject this, when there is a good way to do that
55  $this->user = RequestContext::getMain()->getUser();
56  }
57 
61  private function getTitle() {
62  if ( $this->title === null ) {
63  $this->title =
64  Title::newFromText( $this->getValidatedParams()['title'], NS_FILE ) ?? false;
65  }
66  return $this->title;
67  }
68 
72  private function getFile() {
73  if ( $this->file === null ) {
74  $title = $this->getTitle();
75  $this->file =
76  $this->repoGroup->findFile( $title, [ 'private' => $this->user ] ) ?? false;
77  }
78  return $this->file;
79  }
80 
86  public function run( $title ) {
87  $titleObj = $this->getTitle();
88  $fileObj = $this->getFile();
89 
90  if ( !$titleObj || !$titleObj->exists() ) {
91  throw new LocalizedHttpException(
92  MessageValue::new( 'rest-nonexistent-title' )->plaintextParams(
93  $titleObj->getPrefixedDBkey()
94  ),
95  404
96  );
97  }
98 
99  if ( !$this->permissionManager->userCan( 'read', $this->user, $titleObj ) ) {
100  throw new LocalizedHttpException(
101  MessageValue::new( 'rest-permission-denied-title' )->plaintextParams(
102  $titleObj->getPrefixedDBkey()
103  ),
104  403
105  );
106  }
107 
108  if ( !$fileObj || !$fileObj->exists() ) {
109  throw new LocalizedHttpException(
110  MessageValue::new( 'rest-cannot-load-file' )->plaintextParams(
111  $titleObj->getPrefixedDBkey()
112  ),
113  404
114  );
115  }
116 
117  $response = $this->getResponse( $fileObj );
118  return $this->getResponseFactory()->createJson( $response );
119  }
120 
125  private function getResponse( File $file ) : array {
126  list( $maxWidth, $maxHeight ) = self::getImageLimitsFromOption(
127  $this->user, 'imagesize'
128  );
129  list( $maxThumbWidth, $maxThumbHeight ) = self::getImageLimitsFromOption(
130  $this->user, 'thumbsize'
131  );
132  $transforms = [
133  'preferred' => [
134  'maxWidth' => $maxWidth,
135  'maxHeight' => $maxHeight
136  ],
137  'thumbnail' => [
138  'maxWidth' => $maxThumbWidth,
139  'maxHeight' => $maxThumbHeight
140  ]
141  ];
142 
143  return $this->getFileInfo( $file, $this->user, $transforms );
144  }
145 
146  public function needsWriteAccess() {
147  return false;
148  }
149 
150  public function getParamSettings() {
151  return [
152  'title' => [
153  self::PARAM_SOURCE => 'path',
154  ParamValidator::PARAM_TYPE => 'string',
155  ParamValidator::PARAM_REQUIRED => true,
156  ],
157  ];
158  }
159 
164  protected function getETag(): ?string {
165  $file = $this->getFile();
166  if ( !$file || !$file->exists() ) {
167  return null;
168  }
169 
170  return '"' . $file->getSha1() . '"';
171  }
172 
177  protected function getLastModified(): ?string {
178  $file = $this->getFile();
179  if ( !$file || !$file->exists() ) {
180  return null;
181  }
182 
183  return $file->getTimestamp();
184  }
185 
189  protected function hasRepresentation() {
190  $file = $this->getFile();
191  return $file ? $file->exists() : false;
192  }
193 }
MediaWiki\Rest\Handler
Definition: ActionModuleBasedHandler.php:3
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:332
MediaWiki\Rest\Handler\MediaFileHandler
Handler class for media meta-data.
Definition: MediaFileHandler.php:21
MediaWiki\Rest\Handler\getResponseFactory
getResponseFactory()
Get the ResponseFactory which can be used to generate Response objects.
Definition: Handler.php:93
MediaWiki\Rest\Handler\MediaFileHandler\hasRepresentation
hasRepresentation()
Definition: MediaFileHandler.php:189
$response
$response
Definition: opensearch_desc.php:44
MediaWiki\Rest\Handler\MediaFileHandler\getETag
getETag()
Definition: MediaFileHandler.php:164
MediaWiki\Rest\Handler\MediaFileHandler\$title
Title bool null $title
Definition: MediaFileHandler.php:36
File\getTimestamp
getTimestamp()
Get the 14-character timestamp of the file upload.
Definition: File.php:2184
File\getSha1
getSha1()
Get the SHA-1 base 36 hash of the file.
Definition: File.php:2206
MediaWiki\Rest\Handler\MediaFileHandler\getTitle
getTitle()
Definition: MediaFileHandler.php:61
NS_FILE
const NS_FILE
Definition: Defines.php:75
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
Definition: MessageValue.php:14
File\exists
exists()
Returns true if file exists in the repository.
Definition: File.php:941
MediaWiki\Rest\Handler\MediaFileHandler\$permissionManager
PermissionManager $permissionManager
Definition: MediaFileHandler.php:25
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:61
MediaWiki\Rest\Handler\MediaFileHandler\$file
File bool null $file
Definition: MediaFileHandler.php:41
MediaWiki\Rest\Response
Definition: Response.php:8
MediaWiki\Rest\Handler\MediaFileHandler\$user
User $user
Definition: MediaFileHandler.php:31
RequestContext
Group all the pieces relevant to the context of a request into one instance.
Definition: RequestContext.php:34
MediaWiki\Rest\Handler\getValidatedParams
getValidatedParams()
Fetch the validated parameters.
Definition: Handler.php:189
MediaWiki\Rest\Handler\MediaFileHandler\needsWriteAccess
needsWriteAccess()
Indicates whether this route requires write access.
Definition: MediaFileHandler.php:146
MediaWiki\Rest\Handler\MediaFileHandler\$repoGroup
RepoGroup $repoGroup
Definition: MediaFileHandler.php:28
MediaWiki\Permissions\PermissionManager
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
Definition: PermissionManager.php:48
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:451
Title
Represents a title within MediaWiki.
Definition: Title.php:42
MediaWiki\Rest\Handler\MediaFileHandler\__construct
__construct(PermissionManager $permissionManager, RepoGroup $repoGroup)
Definition: MediaFileHandler.php:47
RepoGroup
Prioritized list of file repositories.
Definition: RepoGroup.php:31
Wikimedia\Message\MessageValue\new
static new( $key, $params=[])
Static constructor for easier chaining of ->params() methods.
Definition: MessageValue.php:38
MediaWiki\Rest\Handler\MediaFileHandler\getParamSettings
getParamSettings()
Fetch ParamValidator settings for parameters.
Definition: MediaFileHandler.php:150
MediaWiki\Rest\Handler\MediaFileHandler\getLastModified
getLastModified()
Definition: MediaFileHandler.php:177
MediaWiki\Rest\Handler\MediaFileHandler\getFile
getFile()
Definition: MediaFileHandler.php:72
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:54
MediaWiki\Rest\Handler\MediaFileHandler\run
run( $title)
Definition: MediaFileHandler.php:86
MediaWiki\Rest\Handler\MediaFileHandler\getResponse
getResponse(File $file)
Definition: MediaFileHandler.php:125
Wikimedia\ParamValidator\ParamValidator
Service for formatting and validating API parameters.
Definition: ParamValidator.php:42
MediaWiki\Rest\LocalizedHttpException
Definition: LocalizedHttpException.php:7
MediaWiki\Rest\SimpleHandler
Definition: SimpleHandler.php:14