MediaWiki REL1_37
MediaFileHandler.php
Go to the documentation of this file.
1<?php
2
4
5use File;
6use MediaFileTrait;
12use RepoGroup;
15
20 use MediaFileTrait;
21
23 private $repoGroup;
24
26 private $pageLookup;
27
31 private $page = false;
32
36 private $file = false;
37
42 public function __construct(
45 ) {
46 $this->repoGroup = $repoGroup;
47 $this->pageLookup = $pageLookup;
48 }
49
53 private function getPage(): ?ExistingPageRecord {
54 if ( $this->page === false ) {
55 $this->page = $this->pageLookup->getExistingPageByText(
56 $this->getValidatedParams()['title'], NS_FILE
57 );
58 }
59 return $this->page;
60 }
61
65 private function getFile(): ?File {
66 if ( $this->file === false ) {
67 $page = $this->getPage();
68 $this->file =
69 $this->repoGroup->findFile( $page, [ 'private' => $this->getAuthority() ] ) ?: null;
70 }
71 return $this->file;
72 }
73
79 public function run( $title ) {
80 $page = $this->getPage();
81
82 if ( !$page ) {
83 throw new LocalizedHttpException(
84 MessageValue::new( 'rest-nonexistent-title' )->plaintextParams( $title ),
85 404
86 );
87 }
88
89 if ( !$this->getAuthority()->authorizeRead( 'read', $page ) ) {
90 throw new LocalizedHttpException(
91 MessageValue::new( 'rest-permission-denied-title' )->plaintextParams( $title ),
92 403
93 );
94 }
95
96 $fileObj = $this->getFile();
97 if ( !$fileObj || !$fileObj->exists() ) {
98 throw new LocalizedHttpException(
99 MessageValue::new( 'rest-cannot-load-file' )->plaintextParams( $title ),
100 404
101 );
102 }
103
104 $response = $this->getResponse( $fileObj );
105 return $this->getResponseFactory()->createJson( $response );
106 }
107
112 private function getResponse( File $file ): array {
113 list( $maxWidth, $maxHeight ) = self::getImageLimitsFromOption(
114 $this->getAuthority()->getUser(), 'imagesize'
115 );
116 list( $maxThumbWidth, $maxThumbHeight ) = self::getImageLimitsFromOption(
117 $this->getAuthority()->getUser(), 'thumbsize'
118 );
119 $transforms = [
120 'preferred' => [
121 'maxWidth' => $maxWidth,
122 'maxHeight' => $maxHeight
123 ],
124 'thumbnail' => [
125 'maxWidth' => $maxThumbWidth,
126 'maxHeight' => $maxThumbHeight
127 ]
128 ];
129
130 return $this->getFileInfo( $file, $this->getAuthority(), $transforms );
131 }
132
133 public function needsWriteAccess() {
134 return false;
135 }
136
137 public function getParamSettings() {
138 return [
139 'title' => [
140 self::PARAM_SOURCE => 'path',
141 ParamValidator::PARAM_TYPE => 'string',
142 ParamValidator::PARAM_REQUIRED => true,
143 ],
144 ];
145 }
146
151 protected function getETag(): ?string {
152 $file = $this->getFile();
153 if ( !$file || !$file->exists() ) {
154 return null;
155 }
156
157 return '"' . $file->getSha1() . '"';
158 }
159
164 protected function getLastModified(): ?string {
165 $file = $this->getFile();
166 if ( !$file || !$file->exists() ) {
167 return null;
168 }
169
170 return $file->getTimestamp();
171 }
172
176 protected function hasRepresentation() {
177 $file = $this->getFile();
178 return $file ? $file->exists() : false;
179 }
180}
getAuthority()
const NS_FILE
Definition Defines.php:70
getFile()
Get the file for this page, if one exists.
if(ini_get('mbstring.func_overload')) if(!defined('MW_ENTRY_POINT'))
Pre-config setup: Before loading LocalSettings.php.
Definition Setup.php:88
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:66
exists()
Returns true if file exists in the repository.
Definition File.php:1028
Handler class for media meta-data.
getParamSettings()
Fetch ParamValidator settings for parameters.
__construct(RepoGroup $repoGroup, PageLookup $pageLookup)
ExistingPageRecord false null $page
needsWriteAccess()
Indicates whether this route requires write access.
getValidatedParams()
Fetch the validated parameters.
Definition Handler.php:282
Prioritized list of file repositories.
Definition RepoGroup.php:33
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 interface for looking up infermation about wiki pages.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42