MediaWiki  master
MediaFileHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Rest\Handler;
4 
5 use MediaFileTrait;
10 use RepoGroup;
11 use RequestContext;
12 use Title;
13 use User;
16 
21  use MediaFileTrait;
22 
25 
27  private $repoGroup;
28 
30  private $user;
31 
36  public function __construct(
39  ) {
40  $this->permissionManager = $permissionManager;
41  $this->repoGroup = $repoGroup;
42 
43  // @todo Inject this, when there is a good way to do that
44  $this->user = RequestContext::getMain()->getUser();
45  }
46 
52  public function run( $title ) {
53  $titleObj = Title::newFromText( $title );
54  if ( !$titleObj || !$titleObj->getArticleID() ) {
55  throw new LocalizedHttpException(
56  MessageValue::new( 'rest-nonexistent-title' )->plaintextParams( $title ),
57  404
58  );
59  }
60 
61  if ( !$this->permissionManager->userCan( 'read', $this->user, $titleObj ) ) {
62  throw new LocalizedHttpException(
63  MessageValue::new( 'rest-permission-denied-title' )->plaintextParams( $title ),
64  403
65  );
66  }
67 
68  $response = $this->getResponse( $titleObj );
69  return $this->getResponseFactory()->createJson( $response );
70  }
71 
76  private function getResponse( Title $titleObj ) : array {
77  $file = $this->repoGroup->findFile( $titleObj, [ 'private' => $this->user ] );
78  list( $maxWidth, $maxHeight ) = self::getImageLimitsFromOption(
79  $this->user, 'imagesize'
80  );
81  list( $maxThumbWidth, $maxThumbHeight ) = self::getImageLimitsFromOption(
82  $this->user, 'thumbsize'
83  );
84  $transforms = [
85  'preferred' => [
86  'maxWidth' => $maxWidth,
87  'maxHeight' => $maxHeight
88  ],
89  'thumbnail' => [
90  'maxWidth' => $maxThumbWidth,
91  'maxHeight' => $maxThumbHeight
92  ]
93  ];
94 
95  return $this->getFileInfo( $file, $this->user, $transforms );
96  }
97 
98  public function needsWriteAccess() {
99  return false;
100  }
101 
102  public function getParamSettings() {
103  return [
104  'title' => [
105  self::PARAM_SOURCE => 'path',
106  ParamValidator::PARAM_TYPE => 'string',
107  ParamValidator::PARAM_REQUIRED => true,
108  ],
109  ];
110  }
111 }
MediaWiki\Rest\Handler
Definition: CompareHandler.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:335
MediaWiki\Rest\Handler\MediaFileHandler
Handler class for Core REST API endpoints that perform operations on revisions.
Definition: MediaFileHandler.php:20
MediaWiki\Rest\Handler\getResponseFactory
getResponseFactory()
Get the ResponseFactory which can be used to generate Response objects.
Definition: Handler.php:92
$response
$response
Definition: opensearch_desc.php:44
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
Definition: MessageValue.php:14
MediaWiki\Rest\Handler\MediaFileHandler\$permissionManager
PermissionManager $permissionManager
Definition: MediaFileHandler.php:24
MediaWiki\Rest\Handler\MediaFileHandler\getResponse
getResponse(Title $titleObj)
Definition: MediaFileHandler.php:76
MediaWiki\Rest\Response
Definition: Response.php:8
$title
$title
Definition: testCompression.php:36
MediaWiki\Rest\Handler\MediaFileHandler\$user
User $user
Definition: MediaFileHandler.php:30
RequestContext
Group all the pieces relevant to the context of a request into one instance.
Definition: RequestContext.php:34
MediaWiki\Rest\Handler\MediaFileHandler\needsWriteAccess
needsWriteAccess()
Indicates whether this route requires write access.
Definition: MediaFileHandler.php:98
MediaWiki\Rest\Handler\MediaFileHandler\$repoGroup
RepoGroup $repoGroup
Definition: MediaFileHandler.php:27
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:36
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:102
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:52
MediaWiki\Rest\Handler\MediaFileHandler\run
run( $title)
Definition: MediaFileHandler.php:52
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