MediaWiki REL1_35
MediaFileHandler.php
Go to the documentation of this file.
1<?php
2
4
5use File;
6use MediaFileTrait;
11use RepoGroup;
13use Title;
14use 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}
getFile()
Get the file for this page, if one exists.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:63
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
Handler class for media meta-data.
getParamSettings()
Fetch ParamValidator settings for parameters.
needsWriteAccess()
Indicates whether this route requires write access.
__construct(PermissionManager $permissionManager, RepoGroup $repoGroup)
getValidatedParams()
Fetch the validated parameters.
Definition Handler.php:257
getResponseFactory()
Get the ResponseFactory which can be used to generate Response objects.
Definition Handler.php:151
Prioritized list of file repositories.
Definition RepoGroup.php:31
Group all the pieces relevant to the context of a request into one instance @newable.
Represents a title within MediaWiki.
Definition Title.php:42
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:60
Value object representing a message for i18n.
Service for formatting and validating API parameters.
const NS_FILE
Definition Defines.php:76
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42