Merge "Simplify HTMLTitleTextField::validate"
[lhc/web/wiklou.git] / includes / filerepo / file / ForeignAPIFile.php
1 <?php
2 /**
3 * Foreign file accessible through api.php requests.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup FileAbstraction
22 */
23
24 use MediaWiki\MediaWikiServices;
25
26 /**
27 * Foreign file accessible through api.php requests.
28 * Very hacky and inefficient, do not use :D
29 *
30 * @ingroup FileAbstraction
31 */
32 class ForeignAPIFile extends File {
33 /** @var bool */
34 private $mExists;
35 /** @var array */
36 private $mInfo = [];
37
38 protected $repoClass = ForeignAPIRepo::class;
39
40 /**
41 * @param Title|string|bool $title
42 * @param ForeignApiRepo $repo
43 * @param array $info
44 * @param bool $exists
45 */
46 function __construct( $title, $repo, $info, $exists = false ) {
47 parent::__construct( $title, $repo );
48
49 $this->mInfo = $info;
50 $this->mExists = $exists;
51
52 $this->assertRepoDefined();
53 }
54
55 /**
56 * @param Title $title
57 * @param ForeignApiRepo $repo
58 * @return ForeignAPIFile|null
59 */
60 static function newFromTitle( Title $title, $repo ) {
61 $data = $repo->fetchImageQuery( [
62 'titles' => 'File:' . $title->getDBkey(),
63 'iiprop' => self::getProps(),
64 'prop' => 'imageinfo',
65 'iimetadataversion' => MediaHandler::getMetadataVersion(),
66 // extmetadata is language-dependant, accessing the current language here
67 // would be problematic, so we just get them all
68 'iiextmetadatamultilang' => 1,
69 ] );
70
71 $info = $repo->getImageInfo( $data );
72
73 if ( $info ) {
74 $lastRedirect = isset( $data['query']['redirects'] )
75 ? count( $data['query']['redirects'] ) - 1
76 : -1;
77 if ( $lastRedirect >= 0 ) {
78 $newtitle = Title::newFromText( $data['query']['redirects'][$lastRedirect]['to'] );
79 $img = new self( $newtitle, $repo, $info, true );
80 if ( $img ) {
81 $img->redirectedFrom( $title->getDBkey() );
82 }
83 } else {
84 $img = new self( $title, $repo, $info, true );
85 }
86
87 return $img;
88 } else {
89 return null;
90 }
91 }
92
93 /**
94 * Get the property string for iiprop and aiprop
95 * @return string
96 */
97 static function getProps() {
98 return 'timestamp|user|comment|url|size|sha1|metadata|mime|mediatype|extmetadata';
99 }
100
101 // Dummy functions...
102
103 /**
104 * @return bool
105 */
106 public function exists() {
107 return $this->mExists;
108 }
109
110 /**
111 * @return bool
112 */
113 public function getPath() {
114 return false;
115 }
116
117 /**
118 * @param array $params
119 * @param int $flags
120 * @return bool|MediaTransformOutput
121 */
122 function transform( $params, $flags = 0 ) {
123 if ( !$this->canRender() ) {
124 // show icon
125 return parent::transform( $params, $flags );
126 }
127
128 // Note, the this->canRender() check above implies
129 // that we have a handler, and it can do makeParamString.
130 $otherParams = $this->handler->makeParamString( $params );
131 $width = $params['width'] ?? -1;
132 $height = $params['height'] ?? -1;
133
134 $thumbUrl = $this->repo->getThumbUrlFromCache(
135 $this->getName(),
136 $width,
137 $height,
138 $otherParams
139 );
140 if ( $thumbUrl === false ) {
141 global $wgLang;
142
143 return $this->repo->getThumbError(
144 $this->getName(),
145 $width,
146 $height,
147 $otherParams,
148 $wgLang->getCode()
149 );
150 }
151
152 return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );
153 }
154
155 // Info we can get from API...
156
157 /**
158 * @param int $page
159 * @return int|number
160 */
161 public function getWidth( $page = 1 ) {
162 return isset( $this->mInfo['width'] ) ? intval( $this->mInfo['width'] ) : 0;
163 }
164
165 /**
166 * @param int $page
167 * @return int
168 */
169 public function getHeight( $page = 1 ) {
170 return isset( $this->mInfo['height'] ) ? intval( $this->mInfo['height'] ) : 0;
171 }
172
173 /**
174 * @return bool|null|string
175 */
176 public function getMetadata() {
177 if ( isset( $this->mInfo['metadata'] ) ) {
178 return serialize( self::parseMetadata( $this->mInfo['metadata'] ) );
179 }
180
181 return null;
182 }
183
184 /**
185 * @return array|null Extended metadata (see imageinfo API for format) or
186 * null on error
187 */
188 public function getExtendedMetadata() {
189 if ( isset( $this->mInfo['extmetadata'] ) ) {
190 return $this->mInfo['extmetadata'];
191 }
192
193 return null;
194 }
195
196 /**
197 * @param mixed $metadata
198 * @return mixed
199 */
200 public static function parseMetadata( $metadata ) {
201 if ( !is_array( $metadata ) ) {
202 return $metadata;
203 }
204 $ret = [];
205 foreach ( $metadata as $meta ) {
206 $ret[$meta['name']] = self::parseMetadata( $meta['value'] );
207 }
208
209 return $ret;
210 }
211
212 /**
213 * @return bool|int|null
214 */
215 public function getSize() {
216 return isset( $this->mInfo['size'] ) ? intval( $this->mInfo['size'] ) : null;
217 }
218
219 /**
220 * @return null|string
221 */
222 public function getUrl() {
223 return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null;
224 }
225
226 /**
227 * Get short description URL for a file based on the foreign API response,
228 * or if unavailable, the short URL is constructed from the foreign page ID.
229 *
230 * @return null|string
231 * @since 1.27
232 */
233 public function getDescriptionShortUrl() {
234 if ( isset( $this->mInfo['descriptionshorturl'] ) ) {
235 return $this->mInfo['descriptionshorturl'];
236 } elseif ( isset( $this->mInfo['pageid'] ) ) {
237 $url = $this->repo->makeUrl( [ 'curid' => $this->mInfo['pageid'] ] );
238 if ( $url !== false ) {
239 return $url;
240 }
241 }
242 return null;
243 }
244
245 /**
246 * @param string $type
247 * @return int|null|string
248 */
249 public function getUser( $type = 'text' ) {
250 if ( $type == 'text' ) {
251 return isset( $this->mInfo['user'] ) ? strval( $this->mInfo['user'] ) : null;
252 } else {
253 return 0; // What makes sense here, for a remote user?
254 }
255 }
256
257 /**
258 * @param int $audience
259 * @param User|null $user
260 * @return null|string
261 */
262 public function getDescription( $audience = self::FOR_PUBLIC, User $user = null ) {
263 return isset( $this->mInfo['comment'] ) ? strval( $this->mInfo['comment'] ) : null;
264 }
265
266 /**
267 * @return null|string
268 */
269 function getSha1() {
270 return isset( $this->mInfo['sha1'] )
271 ? Wikimedia\base_convert( strval( $this->mInfo['sha1'] ), 16, 36, 31 )
272 : null;
273 }
274
275 /**
276 * @return bool|string
277 */
278 function getTimestamp() {
279 return wfTimestamp( TS_MW,
280 isset( $this->mInfo['timestamp'] )
281 ? strval( $this->mInfo['timestamp'] )
282 : null
283 );
284 }
285
286 /**
287 * @return string
288 */
289 function getMimeType() {
290 if ( !isset( $this->mInfo['mime'] ) ) {
291 $magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
292 $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
293 }
294
295 return $this->mInfo['mime'];
296 }
297
298 /**
299 * @return int|string
300 */
301 function getMediaType() {
302 if ( isset( $this->mInfo['mediatype'] ) ) {
303 return $this->mInfo['mediatype'];
304 }
305 $magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
306
307 return $magic->getMediaType( null, $this->getMimeType() );
308 }
309
310 /**
311 * @return bool|string
312 */
313 function getDescriptionUrl() {
314 return $this->mInfo['descriptionurl'] ?? false;
315 }
316
317 /**
318 * Only useful if we're locally caching thumbs anyway...
319 * @param string $suffix
320 * @return null|string
321 */
322 function getThumbPath( $suffix = '' ) {
323 if ( $this->repo->canCacheThumbs() ) {
324 $path = $this->repo->getZonePath( 'thumb' ) . '/' . $this->getHashPath( $this->getName() );
325 if ( $suffix ) {
326 $path = $path . $suffix . '/';
327 }
328
329 return $path;
330 } else {
331 return null;
332 }
333 }
334
335 /**
336 * @return string[]
337 */
338 function getThumbnails() {
339 $dir = $this->getThumbPath( $this->getName() );
340 $iter = $this->repo->getBackend()->getFileList( [ 'dir' => $dir ] );
341
342 $files = [];
343 if ( $iter ) {
344 foreach ( $iter as $file ) {
345 $files[] = $file;
346 }
347 }
348
349 return $files;
350 }
351
352 function purgeCache( $options = [] ) {
353 $this->purgeThumbnails( $options );
354 $this->purgeDescriptionPage();
355 }
356
357 function purgeDescriptionPage() {
358 $services = MediaWikiServices::getInstance();
359 $url = $this->repo->getDescriptionRenderUrl(
360 $this->getName(), $services->getContentLanguage()->getCode() );
361 $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', md5( $url ) );
362
363 $services->getMainWANObjectCache()->delete( $key );
364 }
365
366 /**
367 * @param array $options
368 */
369 function purgeThumbnails( $options = [] ) {
370 $key = $this->repo->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
371 MediaWikiServices::getInstance()->getMainWANObjectCache()->delete( $key );
372
373 $files = $this->getThumbnails();
374 // Give media handler a chance to filter the purge list
375 $handler = $this->getHandler();
376 if ( $handler ) {
377 $handler->filterThumbnailPurgeList( $files, $options );
378 }
379
380 $dir = $this->getThumbPath( $this->getName() );
381 $purgeList = [];
382 foreach ( $files as $file ) {
383 $purgeList[] = "{$dir}{$file}";
384 }
385
386 # Delete the thumbnails
387 $this->repo->quickPurgeBatch( $purgeList );
388 # Clear out the thumbnail directory if empty
389 $this->repo->quickCleanDir( $dir );
390 }
391
392 /**
393 * The thumbnail is created on the foreign server and fetched over internet
394 * @since 1.25
395 * @return bool
396 */
397 public function isTransformedLocally() {
398 return false;
399 }
400 }