MediaWiki master
MediaFileHandler.php
Go to the documentation of this file.
1<?php
2
4
5use File;
6use MediaFileTrait;
12use RepoGroup;
15
20 use MediaFileTrait;
21
22 private RepoGroup $repoGroup;
23 private PageLookup $pageLookup;
24
28 private $page = false;
29
33 private $file = false;
34
35 public function __construct(
36 RepoGroup $repoGroup,
37 PageLookup $pageLookup
38 ) {
39 $this->repoGroup = $repoGroup;
40 $this->pageLookup = $pageLookup;
41 }
42
46 private function getPage(): ?ExistingPageRecord {
47 if ( $this->page === false ) {
48 $this->page = $this->pageLookup->getExistingPageByText(
49 $this->getValidatedParams()['title'], NS_FILE
50 );
51 }
52 return $this->page;
53 }
54
58 private function getFile(): ?File {
59 if ( $this->file === false ) {
60 $page = $this->getPage();
61 $this->file =
62 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable
63 $this->repoGroup->findFile( $page, [ 'private' => $this->getAuthority() ] ) ?: null;
64 }
65 return $this->file;
66 }
67
73 public function run( $title ) {
74 $page = $this->getPage();
75
76 if ( !$page ) {
77 throw new LocalizedHttpException(
78 MessageValue::new( 'rest-nonexistent-title' )->plaintextParams( $title ),
79 404
80 );
81 }
82
83 if ( !$this->getAuthority()->authorizeRead( 'read', $page ) ) {
84 throw new LocalizedHttpException(
85 MessageValue::new( 'rest-permission-denied-title' )->plaintextParams( $title ),
86 403
87 );
88 }
89
90 $fileObj = $this->getFile();
91 if ( !$fileObj || !$fileObj->exists() ) {
92 throw new LocalizedHttpException(
93 MessageValue::new( 'rest-cannot-load-file' )->plaintextParams( $title ),
94 404
95 );
96 }
97
98 $response = $this->getResponse( $fileObj );
99 return $this->getResponseFactory()->createJson( $response );
100 }
101
106 private function getResponse( File $file ): array {
107 [ $maxWidth, $maxHeight ] = self::getImageLimitsFromOption(
108 $this->getAuthority()->getUser(), 'imagesize'
109 );
110 [ $maxThumbWidth, $maxThumbHeight ] = self::getImageLimitsFromOption(
111 $this->getAuthority()->getUser(), 'thumbsize'
112 );
113 $transforms = [
114 'preferred' => [
115 'maxWidth' => $maxWidth,
116 'maxHeight' => $maxHeight
117 ],
118 'thumbnail' => [
119 'maxWidth' => $maxThumbWidth,
120 'maxHeight' => $maxThumbHeight
121 ]
122 ];
123
124 return $this->getFileInfo( $file, $this->getAuthority(), $transforms );
125 }
126
127 public function needsWriteAccess() {
128 return false;
129 }
130
131 public function getParamSettings() {
132 return [
133 'title' => [
134 self::PARAM_SOURCE => 'path',
135 ParamValidator::PARAM_TYPE => 'string',
136 ParamValidator::PARAM_REQUIRED => true,
137 ],
138 ];
139 }
140
145 protected function getETag(): ?string {
146 $file = $this->getFile();
147 if ( !$file || !$file->exists() ) {
148 return null;
149 }
150
151 return '"' . $file->getSha1() . '"';
152 }
153
158 protected function getLastModified(): ?string {
159 $file = $this->getFile();
160 if ( !$file || !$file->exists() ) {
161 return null;
162 }
163
164 return $file->getTimestamp();
165 }
166
170 protected function hasRepresentation() {
171 $file = $this->getFile();
172 return $file && $file->exists();
173 }
174}
const NS_FILE
Definition Defines.php:71
getFile()
Get the file for this page, if one exists.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:76
getTimestamp()
Get the 14-character timestamp of the file upload.
Definition File.php:2312
exists()
Returns true if file exists in the repository.
Definition File.php:1020
getSha1()
Get the SHA-1 base 36 hash of the file.
Definition File.php:2336
Handler class for media meta-data.
getParamSettings()
Fetch ParamValidator settings for parameters.
__construct(RepoGroup $repoGroup, PageLookup $pageLookup)
needsWriteAccess()
Indicates whether this route requires write access to the wiki.
getValidatedParams()
Fetch the validated parameters.
Definition Handler.php:832
Prioritized list of file repositories.
Definition RepoGroup.php:32
Value object representing a message for i18n.
Service for formatting and validating API parameters.
Data record representing a page that currently exists as an editable page on a wiki.
Service for looking up information about wiki pages.
Copyright (C) 2011-2020 Wikimedia Foundation and others.