MediaWiki
REL1_35
MediaFileHandler.php
Go to the documentation of this file.
1
<?php
2
3
namespace
MediaWiki\Rest\Handler
;
4
5
use
File
;
6
use MediaFileTrait;
7
use
MediaWiki\Permissions\PermissionManager
;
8
use
MediaWiki\Rest\LocalizedHttpException
;
9
use
MediaWiki\Rest\Response
;
10
use
MediaWiki\Rest\SimpleHandler
;
11
use
RepoGroup
;
12
use
RequestContext
;
13
use
Title
;
14
use
User
;
15
use
Wikimedia\Message\MessageValue
;
16
use
Wikimedia\ParamValidator\ParamValidator
;
17
21
class
MediaFileHandler
extends
SimpleHandler
{
22
use MediaFileTrait;
23
25
private
$permissionManager
;
26
28
private
$repoGroup
;
29
31
private
$user
;
32
36
private
$title
=
null
;
37
41
private
$file
=
null
;
42
47
public
function
__construct
(
48
PermissionManager
$permissionManager
,
49
RepoGroup
$repoGroup
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
getFile()
Get the file for this page, if one exists.
Definition
RevisionSearchResultTrait.php:89
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition
File.php:63
MediaWiki\Permissions\PermissionManager
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
Definition
PermissionManager.php:49
MediaWiki\Rest\Handler\MediaFileHandler
Handler class for media meta-data.
Definition
MediaFileHandler.php:21
MediaWiki\Rest\Handler\MediaFileHandler\getParamSettings
getParamSettings()
Fetch ParamValidator settings for parameters.
Definition
MediaFileHandler.php:150
MediaWiki\Rest\Handler\MediaFileHandler\hasRepresentation
hasRepresentation()
Definition
MediaFileHandler.php:189
MediaWiki\Rest\Handler\MediaFileHandler\getETag
getETag()
Definition
MediaFileHandler.php:164
MediaWiki\Rest\Handler\MediaFileHandler\$repoGroup
RepoGroup $repoGroup
Definition
MediaFileHandler.php:28
MediaWiki\Rest\Handler\MediaFileHandler\$permissionManager
PermissionManager $permissionManager
Definition
MediaFileHandler.php:25
MediaWiki\Rest\Handler\MediaFileHandler\$file
File bool null $file
Definition
MediaFileHandler.php:41
MediaWiki\Rest\Handler\MediaFileHandler\$user
User $user
Definition
MediaFileHandler.php:31
MediaWiki\Rest\Handler\MediaFileHandler\getTitle
getTitle()
Definition
MediaFileHandler.php:61
MediaWiki\Rest\Handler\MediaFileHandler\getLastModified
getLastModified()
Definition
MediaFileHandler.php:177
MediaWiki\Rest\Handler\MediaFileHandler\$title
Title bool null $title
Definition
MediaFileHandler.php:36
MediaWiki\Rest\Handler\MediaFileHandler\getFile
getFile()
Definition
MediaFileHandler.php:72
MediaWiki\Rest\Handler\MediaFileHandler\getResponse
getResponse(File $file)
Definition
MediaFileHandler.php:125
MediaWiki\Rest\Handler\MediaFileHandler\needsWriteAccess
needsWriteAccess()
Indicates whether this route requires write access.
Definition
MediaFileHandler.php:146
MediaWiki\Rest\Handler\MediaFileHandler\run
run( $title)
Definition
MediaFileHandler.php:86
MediaWiki\Rest\Handler\MediaFileHandler\__construct
__construct(PermissionManager $permissionManager, RepoGroup $repoGroup)
Definition
MediaFileHandler.php:47
MediaWiki\Rest\Handler\getValidatedParams
getValidatedParams()
Fetch the validated parameters.
Definition
Handler.php:257
MediaWiki\Rest\Handler\getResponseFactory
getResponseFactory()
Get the ResponseFactory which can be used to generate Response objects.
Definition
Handler.php:151
MediaWiki\Rest\LocalizedHttpException
@newable
Definition
LocalizedHttpException.php:10
MediaWiki\Rest\Response
Definition
Response.php:8
MediaWiki\Rest\SimpleHandler
Definition
SimpleHandler.php:15
RepoGroup
Prioritized list of file repositories.
Definition
RepoGroup.php:31
RequestContext
Group all the pieces relevant to the context of a request into one instance @newable.
Definition
RequestContext.php:38
Title
Represents a title within MediaWiki.
Definition
Title.php:42
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition
User.php:60
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
Definition
MessageValue.php:16
Wikimedia\ParamValidator\ParamValidator
Service for formatting and validating API parameters.
Definition
ParamValidator.php:42
NS_FILE
const NS_FILE
Definition
Defines.php:76
MediaWiki\Rest\Handler
Definition
ActionModuleBasedHandler.php:3
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition
router.php:42
includes
Rest
Handler
MediaFileHandler.php
Generated on Sat Apr 6 2024 00:07:43 for MediaWiki by
1.9.8