Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F4137330
MediaFileHandler.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
3 KB
Referenced Files
None
Subscribers
None
MediaFileHandler.php
View Options
<?php
namespace
MediaWiki\Rest\Handler
;
use
File
;
use
MediaFileTrait
;
use
MediaWiki\Page\ExistingPageRecord
;
use
MediaWiki\Page\PageLookup
;
use
MediaWiki\Rest\LocalizedHttpException
;
use
MediaWiki\Rest\Response
;
use
MediaWiki\Rest\SimpleHandler
;
use
RepoGroup
;
use
Wikimedia\Message\MessageValue
;
use
Wikimedia\ParamValidator\ParamValidator
;
/**
* Handler class for media meta-data
*/
class
MediaFileHandler
extends
SimpleHandler
{
use
MediaFileTrait
;
private
RepoGroup
$repoGroup
;
private
PageLookup
$pageLookup
;
/**
* @var ExistingPageRecord|false|null
*/
private
$page
=
false
;
/**
* @var File|false|null
*/
private
$file
=
false
;
public
function
__construct
(
RepoGroup
$repoGroup
,
PageLookup
$pageLookup
)
{
$this
->
repoGroup
=
$repoGroup
;
$this
->
pageLookup
=
$pageLookup
;
}
/**
* @return ExistingPageRecord|null
*/
private
function
getPage
():
?
ExistingPageRecord
{
if
(
$this
->
page
===
false
)
{
$this
->
page
=
$this
->
pageLookup
->
getExistingPageByText
(
$this
->
getValidatedParams
()[
'title'
],
NS_FILE
);
}
return
$this
->
page
;
}
/**
* @return File|null
*/
private
function
getFile
():
?
File
{
if
(
$this
->
file
===
false
)
{
$page
=
$this
->
getPage
();
$this
->
file
=
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable
$this
->
repoGroup
->
findFile
(
$page
,
[
'private'
=>
$this
->
getAuthority
()
]
)
?:
null
;
}
return
$this
->
file
;
}
/**
* @param string $title
* @return Response
* @throws LocalizedHttpException
*/
public
function
run
(
$title
)
{
$page
=
$this
->
getPage
();
if
(
!
$page
)
{
throw
new
LocalizedHttpException
(
MessageValue
::
new
(
'rest-nonexistent-title'
)->
plaintextParams
(
$title
),
404
);
}
if
(
!
$this
->
getAuthority
()->
authorizeRead
(
'read'
,
$page
)
)
{
throw
new
LocalizedHttpException
(
MessageValue
::
new
(
'rest-permission-denied-title'
)->
plaintextParams
(
$title
),
403
);
}
$fileObj
=
$this
->
getFile
();
if
(
!
$fileObj
||
!
$fileObj
->
exists
()
)
{
throw
new
LocalizedHttpException
(
MessageValue
::
new
(
'rest-cannot-load-file'
)->
plaintextParams
(
$title
),
404
);
}
$response
=
$this
->
getResponse
(
$fileObj
);
return
$this
->
getResponseFactory
()->
createJson
(
$response
);
}
/**
* @param File $file the file to load media links for
* @return array response data
*/
private
function
getResponse
(
File
$file
):
array
{
[
$maxWidth
,
$maxHeight
]
=
self
::
getImageLimitsFromOption
(
$this
->
getAuthority
()->
getUser
(),
'imagesize'
);
[
$maxThumbWidth
,
$maxThumbHeight
]
=
self
::
getImageLimitsFromOption
(
$this
->
getAuthority
()->
getUser
(),
'thumbsize'
);
$transforms
=
[
'preferred'
=>
[
'maxWidth'
=>
$maxWidth
,
'maxHeight'
=>
$maxHeight
],
'thumbnail'
=>
[
'maxWidth'
=>
$maxThumbWidth
,
'maxHeight'
=>
$maxThumbHeight
]
];
return
$this
->
getFileInfo
(
$file
,
$this
->
getAuthority
(),
$transforms
);
}
public
function
needsWriteAccess
()
{
return
false
;
}
public
function
getParamSettings
()
{
return
[
'title'
=>
[
self
::
PARAM_SOURCE
=>
'path'
,
ParamValidator
::
PARAM_TYPE
=>
'string'
,
ParamValidator
::
PARAM_REQUIRED
=>
true
,
],
];
}
/**
* @return string|null
* @throws LocalizedHttpException
*/
protected
function
getETag
():
?
string
{
$file
=
$this
->
getFile
();
if
(
!
$file
||
!
$file
->
exists
()
)
{
return
null
;
}
return
'"'
.
$file
->
getSha1
()
.
'"'
;
}
/**
* @return string|null
* @throws LocalizedHttpException
*/
protected
function
getLastModified
():
?
string
{
$file
=
$this
->
getFile
();
if
(
!
$file
||
!
$file
->
exists
()
)
{
return
null
;
}
return
$file
->
getTimestamp
();
}
/**
* @return bool
*/
protected
function
hasRepresentation
()
{
$file
=
$this
->
getFile
();
return
$file
&&
$file
->
exists
();
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Wed, Aug 19, 14:06 (3 w, 3 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
82/3e/37d6b47ac9e690cac3d896b6d00b
Default Alt Text
MediaFileHandler.php (3 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment