MediaWiki REL1_41
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(
43 RepoGroup $repoGroup,
44 PageLookup $pageLookup
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 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable
70 $this->repoGroup->findFile( $page, [ 'private' => $this->getAuthority() ] ) ?: null;
71 }
72 return $this->file;
73 }
74
80 public function run( $title ) {
81 $page = $this->getPage();
82
83 if ( !$page ) {
84 throw new LocalizedHttpException(
85 MessageValue::new( 'rest-nonexistent-title' )->plaintextParams( $title ),
86 404
87 );
88 }
89
90 if ( !$this->getAuthority()->authorizeRead( 'read', $page ) ) {
91 throw new LocalizedHttpException(
92 MessageValue::new( 'rest-permission-denied-title' )->plaintextParams( $title ),
93 403
94 );
95 }
96
97 $fileObj = $this->getFile();
98 if ( !$fileObj || !$fileObj->exists() ) {
99 throw new LocalizedHttpException(
100 MessageValue::new( 'rest-cannot-load-file' )->plaintextParams( $title ),
101 404
102 );
103 }
104
105 $response = $this->getResponse( $fileObj );
106 return $this->getResponseFactory()->createJson( $response );
107 }
108
113 private function getResponse( File $file ): array {
114 [ $maxWidth, $maxHeight ] = self::getImageLimitsFromOption(
115 $this->getAuthority()->getUser(), 'imagesize'
116 );
117 [ $maxThumbWidth, $maxThumbHeight ] = self::getImageLimitsFromOption(
118 $this->getAuthority()->getUser(), 'thumbsize'
119 );
120 $transforms = [
121 'preferred' => [
122 'maxWidth' => $maxWidth,
123 'maxHeight' => $maxHeight
124 ],
125 'thumbnail' => [
126 'maxWidth' => $maxThumbWidth,
127 'maxHeight' => $maxThumbHeight
128 ]
129 ];
130
131 return $this->getFileInfo( $file, $this->getAuthority(), $transforms );
132 }
133
134 public function needsWriteAccess() {
135 return false;
136 }
137
138 public function getParamSettings() {
139 return [
140 'title' => [
141 self::PARAM_SOURCE => 'path',
142 ParamValidator::PARAM_TYPE => 'string',
143 ParamValidator::PARAM_REQUIRED => true,
144 ],
145 ];
146 }
147
152 protected function getETag(): ?string {
153 $file = $this->getFile();
154 if ( !$file || !$file->exists() ) {
155 return null;
156 }
157
158 return '"' . $file->getSha1() . '"';
159 }
160
165 protected function getLastModified(): ?string {
166 $file = $this->getFile();
167 if ( !$file || !$file->exists() ) {
168 return null;
169 }
170
171 return $file->getTimestamp();
172 }
173
177 protected function hasRepresentation() {
178 $file = $this->getFile();
179 return $file && $file->exists();
180 }
181}
getUser()
getAuthority()
const NS_FILE
Definition Defines.php:70
getFile()
Get the file for this page, if one exists.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:88
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:70
exists()
Returns true if file exists in the repository.
Definition File.php:1012
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.
getValidatedParams()
Fetch the validated parameters.
Definition Handler.php:371
Prioritized list of file repositories.
Definition RepoGroup.php:30
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.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42