3 * Foreign file accessible through api.php requests.
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.
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.
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
21 * @ingroup FileAbstraction
24 use MediaWiki\MediaWikiServices
;
27 * Foreign file accessible through api.php requests.
28 * Very hacky and inefficient, do not use :D
30 * @ingroup FileAbstraction
32 class ForeignAPIFile
extends File
{
38 protected $repoClass = ForeignAPIRepo
::class;
41 * @param Title|string|bool $title
42 * @param ForeignApiRepo $repo
46 function __construct( $title, $repo, $info, $exists = false ) {
47 parent
::__construct( $title, $repo );
50 $this->mExists
= $exists;
52 $this->assertRepoDefined();
57 * @param ForeignApiRepo $repo
58 * @return ForeignAPIFile|null
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,
71 $info = $repo->getImageInfo( $data );
74 $lastRedirect = isset( $data['query']['redirects'] )
75 ?
count( $data['query']['redirects'] ) - 1
77 if ( $lastRedirect >= 0 ) {
78 $newtitle = Title
::newFromText( $data['query']['redirects'][$lastRedirect]['to'] );
79 $img = new self( $newtitle, $repo, $info, true );
81 $img->redirectedFrom( $title->getDBkey() );
84 $img = new self( $title, $repo, $info, true );
94 * Get the property string for iiprop and aiprop
97 static function getProps() {
98 return 'timestamp|user|comment|url|size|sha1|metadata|mime|mediatype|extmetadata';
101 // Dummy functions...
106 public function exists() {
107 return $this->mExists
;
113 public function getPath() {
118 * @param array $params
120 * @return bool|MediaTransformOutput
122 function transform( $params, $flags = 0 ) {
123 if ( !$this->canRender() ) {
125 return parent
::transform( $params, $flags );
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;
134 $thumbUrl = $this->repo
->getThumbUrlFromCache(
140 if ( $thumbUrl === false ) {
143 return $this->repo
->getThumbError(
152 return $this->handler
->getTransform( $this, 'bogus', $thumbUrl, $params );
155 // Info we can get from API...
161 public function getWidth( $page = 1 ) {
162 return isset( $this->mInfo
['width'] ) ?
intval( $this->mInfo
['width'] ) : 0;
169 public function getHeight( $page = 1 ) {
170 return isset( $this->mInfo
['height'] ) ?
intval( $this->mInfo
['height'] ) : 0;
174 * @return bool|null|string
176 public function getMetadata() {
177 if ( isset( $this->mInfo
['metadata'] ) ) {
178 return serialize( self
::parseMetadata( $this->mInfo
['metadata'] ) );
185 * @return array|null Extended metadata (see imageinfo API for format) or
188 public function getExtendedMetadata() {
189 if ( isset( $this->mInfo
['extmetadata'] ) ) {
190 return $this->mInfo
['extmetadata'];
197 * @param mixed $metadata
200 public static function parseMetadata( $metadata ) {
201 if ( !is_array( $metadata ) ) {
205 foreach ( $metadata as $meta ) {
206 $ret[$meta['name']] = self
::parseMetadata( $meta['value'] );
213 * @return bool|int|null
215 public function getSize() {
216 return isset( $this->mInfo
['size'] ) ?
intval( $this->mInfo
['size'] ) : null;
220 * @return null|string
222 public function getUrl() {
223 return isset( $this->mInfo
['url'] ) ?
strval( $this->mInfo
['url'] ) : null;
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.
230 * @return null|string
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 ) {
246 * @param string $type
247 * @return int|null|string
249 public function getUser( $type = 'text' ) {
250 if ( $type == 'text' ) {
251 return isset( $this->mInfo
['user'] ) ?
strval( $this->mInfo
['user'] ) : null;
253 return 0; // What makes sense here, for a remote user?
258 * @param int $audience
259 * @param User|null $user
260 * @return null|string
262 public function getDescription( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
263 return isset( $this->mInfo
['comment'] ) ?
strval( $this->mInfo
['comment'] ) : null;
267 * @return null|string
270 return isset( $this->mInfo
['sha1'] )
271 ? Wikimedia\base_convert
( strval( $this->mInfo
['sha1'] ), 16, 36, 31 )
276 * @return bool|string
278 function getTimestamp() {
279 return wfTimestamp( TS_MW
,
280 isset( $this->mInfo
['timestamp'] )
281 ?
strval( $this->mInfo
['timestamp'] )
289 function getMimeType() {
290 if ( !isset( $this->mInfo
['mime'] ) ) {
291 $magic = MediaWiki\MediaWikiServices
::getInstance()->getMimeAnalyzer();
292 $this->mInfo
['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
295 return $this->mInfo
['mime'];
301 function getMediaType() {
302 if ( isset( $this->mInfo
['mediatype'] ) ) {
303 return $this->mInfo
['mediatype'];
305 $magic = MediaWiki\MediaWikiServices
::getInstance()->getMimeAnalyzer();
307 return $magic->getMediaType( null, $this->getMimeType() );
311 * @return bool|string
313 function getDescriptionUrl() {
314 return $this->mInfo
['descriptionurl'] ??
false;
318 * Only useful if we're locally caching thumbs anyway...
319 * @param string $suffix
320 * @return null|string
322 function getThumbPath( $suffix = '' ) {
323 if ( $this->repo
->canCacheThumbs() ) {
324 $path = $this->repo
->getZonePath( 'thumb' ) . '/' . $this->getHashPath( $this->getName() );
326 $path = $path . $suffix . '/';
338 function getThumbnails() {
339 $dir = $this->getThumbPath( $this->getName() );
340 $iter = $this->repo
->getBackend()->getFileList( [ 'dir' => $dir ] );
344 foreach ( $iter as $file ) {
352 function purgeCache( $options = [] ) {
353 $this->purgeThumbnails( $options );
354 $this->purgeDescriptionPage();
357 function purgeDescriptionPage() {
360 $url = $this->repo
->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
361 $key = $this->repo
->getLocalCacheKey( 'RemoteFileDescription', 'url', md5( $url ) );
363 MediaWikiServices
::getInstance()->getMainWANObjectCache()->delete( $key );
367 * @param array $options
369 function purgeThumbnails( $options = [] ) {
370 $key = $this->repo
->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
371 MediaWikiServices
::getInstance()->getMainWANObjectCache()->delete( $key );
373 $files = $this->getThumbnails();
374 // Give media handler a chance to filter the purge list
375 $handler = $this->getHandler();
377 $handler->filterThumbnailPurgeList( $files, $options );
380 $dir = $this->getThumbPath( $this->getName() );
382 foreach ( $files as $file ) {
383 $purgeList[] = "{$dir}{$file}";
386 # Delete the thumbnails
387 $this->repo
->quickPurgeBatch( $purgeList );
388 # Clear out the thumbnail directory if empty
389 $this->repo
->quickCleanDir( $dir );
393 * The thumbnail is created on the foreign server and fetched over internet
397 public function isTransformedLocally() {