Expose $wgMaxArticleSize in siteinfo query api
[mediawiki.git] / includes / filerepo / file / ForeignAPIFile.php
blobf6752d8308ec9eeb614efba60d8eeba277b21b6c
1 <?php
2 /**
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
20 * @file
21 * @ingroup FileAbstraction
24 /**
25 * Foreign file accessible through api.php requests.
26 * Very hacky and inefficient, do not use :D
28 * @ingroup FileAbstraction
30 class ForeignAPIFile extends File {
31 private $mExists;
33 protected $repoClass = 'ForeignApiRepo';
35 /**
36 * @param Title|string|bool $title
37 * @param ForeignApiRepo $repo
38 * @param array $info
39 * @param bool $exists
41 function __construct( $title, $repo, $info, $exists = false ) {
42 parent::__construct( $title, $repo );
44 $this->mInfo = $info;
45 $this->mExists = $exists;
47 $this->assertRepoDefined();
50 /**
51 * @param Title $title
52 * @param ForeignApiRepo $repo
53 * @return ForeignAPIFile|null
55 static function newFromTitle( Title $title, $repo ) {
56 $data = $repo->fetchImageQuery( [
57 'titles' => 'File:' . $title->getDBkey(),
58 'iiprop' => self::getProps(),
59 'prop' => 'imageinfo',
60 'iimetadataversion' => MediaHandler::getMetadataVersion(),
61 // extmetadata is language-dependant, accessing the current language here
62 // would be problematic, so we just get them all
63 'iiextmetadatamultilang' => 1,
64 ] );
66 $info = $repo->getImageInfo( $data );
68 if ( $info ) {
69 $lastRedirect = isset( $data['query']['redirects'] )
70 ? count( $data['query']['redirects'] ) - 1
71 : -1;
72 if ( $lastRedirect >= 0 ) {
73 $newtitle = Title::newFromText( $data['query']['redirects'][$lastRedirect]['to'] );
74 $img = new self( $newtitle, $repo, $info, true );
75 if ( $img ) {
76 $img->redirectedFrom( $title->getDBkey() );
78 } else {
79 $img = new self( $title, $repo, $info, true );
82 return $img;
83 } else {
84 return null;
88 /**
89 * Get the property string for iiprop and aiprop
90 * @return string
92 static function getProps() {
93 return 'timestamp|user|comment|url|size|sha1|metadata|mime|mediatype|extmetadata';
96 // Dummy functions...
98 /**
99 * @return bool
101 public function exists() {
102 return $this->mExists;
106 * @return bool
108 public function getPath() {
109 return false;
113 * @param array $params
114 * @param int $flags
115 * @return bool|MediaTransformOutput
117 function transform( $params, $flags = 0 ) {
118 if ( !$this->canRender() ) {
119 // show icon
120 return parent::transform( $params, $flags );
123 // Note, the this->canRender() check above implies
124 // that we have a handler, and it can do makeParamString.
125 $otherParams = $this->handler->makeParamString( $params );
126 $width = isset( $params['width'] ) ? $params['width'] : -1;
127 $height = isset( $params['height'] ) ? $params['height'] : -1;
129 $thumbUrl = $this->repo->getThumbUrlFromCache(
130 $this->getName(),
131 $width,
132 $height,
133 $otherParams
135 if ( $thumbUrl === false ) {
136 global $wgLang;
138 return $this->repo->getThumbError(
139 $this->getName(),
140 $width,
141 $height,
142 $otherParams,
143 $wgLang->getCode()
147 return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );
150 // Info we can get from API...
153 * @param int $page
154 * @return int|number
156 public function getWidth( $page = 1 ) {
157 return isset( $this->mInfo['width'] ) ? intval( $this->mInfo['width'] ) : 0;
161 * @param int $page
162 * @return int
164 public function getHeight( $page = 1 ) {
165 return isset( $this->mInfo['height'] ) ? intval( $this->mInfo['height'] ) : 0;
169 * @return bool|null|string
171 public function getMetadata() {
172 if ( isset( $this->mInfo['metadata'] ) ) {
173 return serialize( self::parseMetadata( $this->mInfo['metadata'] ) );
176 return null;
180 * @return array|null Extended metadata (see imageinfo API for format) or
181 * null on error
183 public function getExtendedMetadata() {
184 if ( isset( $this->mInfo['extmetadata'] ) ) {
185 return $this->mInfo['extmetadata'];
188 return null;
192 * @param array $metadata
193 * @return array
195 public static function parseMetadata( $metadata ) {
196 if ( !is_array( $metadata ) ) {
197 return $metadata;
199 $ret = [];
200 foreach ( $metadata as $meta ) {
201 $ret[$meta['name']] = self::parseMetadata( $meta['value'] );
204 return $ret;
208 * @return bool|int|null
210 public function getSize() {
211 return isset( $this->mInfo['size'] ) ? intval( $this->mInfo['size'] ) : null;
215 * @return null|string
217 public function getUrl() {
218 return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null;
222 * Get short description URL for a file based on the foreign API response,
223 * or if unavailable, the short URL is constructed from the foreign page ID.
225 * @return null|string
226 * @since 1.27
228 public function getDescriptionShortUrl() {
229 if ( isset( $this->mInfo['descriptionshorturl'] ) ) {
230 return $this->mInfo['descriptionshorturl'];
231 } elseif ( isset( $this->mInfo['pageid'] ) ) {
232 $url = $this->repo->makeUrl( [ 'curid' => $this->mInfo['pageid'] ] );
233 if ( $url !== false ) {
234 return $url;
237 return null;
241 * @param string $type
242 * @return int|null|string
244 public function getUser( $type = 'text' ) {
245 if ( $type == 'text' ) {
246 return isset( $this->mInfo['user'] ) ? strval( $this->mInfo['user'] ) : null;
247 } elseif ( $type == 'id' ) {
248 return 0; // What makes sense here, for a remote user?
253 * @param int $audience
254 * @param User $user
255 * @return null|string
257 public function getDescription( $audience = self::FOR_PUBLIC, User $user = null ) {
258 return isset( $this->mInfo['comment'] ) ? strval( $this->mInfo['comment'] ) : null;
262 * @return null|string
264 function getSha1() {
265 return isset( $this->mInfo['sha1'] )
266 ? Wikimedia\base_convert( strval( $this->mInfo['sha1'] ), 16, 36, 31 )
267 : null;
271 * @return bool|string
273 function getTimestamp() {
274 return wfTimestamp( TS_MW,
275 isset( $this->mInfo['timestamp'] )
276 ? strval( $this->mInfo['timestamp'] )
277 : null
282 * @return string
284 function getMimeType() {
285 if ( !isset( $this->mInfo['mime'] ) ) {
286 $magic = MimeMagic::singleton();
287 $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
290 return $this->mInfo['mime'];
294 * @return int|string
296 function getMediaType() {
297 if ( isset( $this->mInfo['mediatype'] ) ) {
298 return $this->mInfo['mediatype'];
300 $magic = MimeMagic::singleton();
302 return $magic->getMediaType( null, $this->getMimeType() );
306 * @return bool|string
308 function getDescriptionUrl() {
309 return isset( $this->mInfo['descriptionurl'] )
310 ? $this->mInfo['descriptionurl']
311 : false;
315 * Only useful if we're locally caching thumbs anyway...
316 * @param string $suffix
317 * @return null|string
319 function getThumbPath( $suffix = '' ) {
320 if ( $this->repo->canCacheThumbs() ) {
321 $path = $this->repo->getZonePath( 'thumb' ) . '/' . $this->getHashPath( $this->getName() );
322 if ( $suffix ) {
323 $path = $path . $suffix . '/';
326 return $path;
327 } else {
328 return null;
333 * @return array
335 function getThumbnails() {
336 $dir = $this->getThumbPath( $this->getName() );
337 $iter = $this->repo->getBackend()->getFileList( [ 'dir' => $dir ] );
339 $files = [];
340 foreach ( $iter as $file ) {
341 $files[] = $file;
344 return $files;
348 * @see File::purgeCache()
350 function purgeCache( $options = [] ) {
351 $this->purgeThumbnails( $options );
352 $this->purgeDescriptionPage();
355 function purgeDescriptionPage() {
356 global $wgContLang;
358 $url = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
359 $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', md5( $url ) );
361 ObjectCache::getMainWANInstance()->delete( $key );
365 * @param array $options
367 function purgeThumbnails( $options = [] ) {
368 $key = $this->repo->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
369 ObjectCache::getMainWANInstance()->delete( $key );
371 $files = $this->getThumbnails();
372 // Give media handler a chance to filter the purge list
373 $handler = $this->getHandler();
374 if ( $handler ) {
375 $handler->filterThumbnailPurgeList( $files, $options );
378 $dir = $this->getThumbPath( $this->getName() );
379 $purgeList = [];
380 foreach ( $files as $file ) {
381 $purgeList[] = "{$dir}{$file}";
384 # Delete the thumbnails
385 $this->repo->quickPurgeBatch( $purgeList );
386 # Clear out the thumbnail directory if empty
387 $this->repo->quickCleanDir( $dir );
391 * The thumbnail is created on the foreign server and fetched over internet
392 * @since 1.25
393 * @return bool
395 public function isTransformedLocally() {
396 return false;