Debian Codegraph @

includes/Rest/Handler/MediaFileHandler.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Rest\Handler;
4
5 use File;
6 use MediaFileTrait;
7 use MediaWiki\Page\ExistingPageRecord;
8 use MediaWiki\Page\PageLookup;
9 use MediaWiki\Rest\LocalizedHttpException;
10 use MediaWiki\Rest\Response;
11 use MediaWiki\Rest\SimpleHandler;
12 use RepoGroup;
13 use Wikimedia\Message\MessageValue;
14 use Wikimedia\ParamValidator\ParamValidator;
15
16 /**
17 * Handler class for media meta-data
18 */
19 class MediaFileHandler extends SimpleHandler {
20 use MediaFileTrait;
21
22 private RepoGroup $repoGroup;
23 private PageLookup $pageLookup;
24
25 /**
26 * @var ExistingPageRecord|false|null
27 */
28 private $page = false;
29
30 /**
31 * @var File|false|null
32 */
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
43 /**
44 * @return ExistingPageRecord|null
45 */
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
55 /**
56 * @return File|null
57 */
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
68 /**
69 * @param string $title
70 * @return Response
71 * @throws LocalizedHttpException
72 */
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
102 /**
103 * @param File $file the file to load media links for
104 * @return array response data
105 */
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
141 /**
142 * @return string|null
143 * @throws LocalizedHttpException
144 */
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
154 /**
155 * @return string|null
156 * @throws LocalizedHttpException
157 */
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
167 /**
168 * @return bool
169 */
170 protected function hasRepresentation() {
171 $file = $this->getFile();
172 return $file && $file->exists();
173 }
174
175 public function getResponseBodySchemaFileName( string $method ): ?string {
176 return 'includes/Rest/Handler/Schema/MediaFile.json';
177 }
178 }