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
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
{
36 protected $repoClass = 'ForeignApiRepo';
39 * @param Title|string|bool $title
40 * @param ForeignApiRepo $repo
44 function __construct( $title, $repo, $info, $exists = false ) {
45 parent
::__construct( $title, $repo );
48 $this->mExists
= $exists;
50 $this->assertRepoDefined();
55 * @param ForeignApiRepo $repo
56 * @return ForeignAPIFile|null
58 static function newFromTitle( Title
$title, $repo ) {
59 $data = $repo->fetchImageQuery( [
60 'titles' => 'File:' . $title->getDBkey(),
61 'iiprop' => self
::getProps(),
62 'prop' => 'imageinfo',
63 'iimetadataversion' => MediaHandler
::getMetadataVersion(),
64 // extmetadata is language-dependant, accessing the current language here
65 // would be problematic, so we just get them all
66 'iiextmetadatamultilang' => 1,
69 $info = $repo->getImageInfo( $data );
72 $lastRedirect = isset( $data['query']['redirects'] )
73 ?
count( $data['query']['redirects'] ) - 1
75 if ( $lastRedirect >= 0 ) {
76 $newtitle = Title
::newFromText( $data['query']['redirects'][$lastRedirect]['to'] );
77 $img = new self( $newtitle, $repo, $info, true );
79 $img->redirectedFrom( $title->getDBkey() );
82 $img = new self( $title, $repo, $info, true );
92 * Get the property string for iiprop and aiprop
95 static function getProps() {
96 return 'timestamp|user|comment|url|size|sha1|metadata|mime|mediatype|extmetadata';
104 public function exists() {
105 return $this->mExists
;
111 public function getPath() {
116 * @param array $params
118 * @return bool|MediaTransformOutput
120 function transform( $params, $flags = 0 ) {
121 if ( !$this->canRender() ) {
123 return parent
::transform( $params, $flags );
126 // Note, the this->canRender() check above implies
127 // that we have a handler, and it can do makeParamString.
128 $otherParams = $this->handler
->makeParamString( $params );
129 $width = isset( $params['width'] ) ?
$params['width'] : -1;
130 $height = isset( $params['height'] ) ?
$params['height'] : -1;
132 $thumbUrl = $this->repo
->getThumbUrlFromCache(
138 if ( $thumbUrl === false ) {
141 return $this->repo
->getThumbError(
150 return $this->handler
->getTransform( $this, 'bogus', $thumbUrl, $params );
153 // Info we can get from API...
159 public function getWidth( $page = 1 ) {
160 return isset( $this->mInfo
['width'] ) ?
intval( $this->mInfo
['width'] ) : 0;
167 public function getHeight( $page = 1 ) {
168 return isset( $this->mInfo
['height'] ) ?
intval( $this->mInfo
['height'] ) : 0;
172 * @return bool|null|string
174 public function getMetadata() {
175 if ( isset( $this->mInfo
['metadata'] ) ) {
176 return serialize( self
::parseMetadata( $this->mInfo
['metadata'] ) );
183 * @return array|null Extended metadata (see imageinfo API for format) or
186 public function getExtendedMetadata() {
187 if ( isset( $this->mInfo
['extmetadata'] ) ) {
188 return $this->mInfo
['extmetadata'];
195 * @param array $metadata
198 public static function parseMetadata( $metadata ) {
199 if ( !is_array( $metadata ) ) {
203 foreach ( $metadata as $meta ) {
204 $ret[$meta['name']] = self
::parseMetadata( $meta['value'] );
211 * @return bool|int|null
213 public function getSize() {
214 return isset( $this->mInfo
['size'] ) ?
intval( $this->mInfo
['size'] ) : null;
218 * @return null|string
220 public function getUrl() {
221 return isset( $this->mInfo
['url'] ) ?
strval( $this->mInfo
['url'] ) : null;
225 * Get short description URL for a file based on the foreign API response,
226 * or if unavailable, the short URL is constructed from the foreign page ID.
228 * @return null|string
231 public function getDescriptionShortUrl() {
232 if ( isset( $this->mInfo
['descriptionshorturl'] ) ) {
233 return $this->mInfo
['descriptionshorturl'];
234 } elseif ( isset( $this->mInfo
['pageid'] ) ) {
235 $url = $this->repo
->makeUrl( [ 'curid' => $this->mInfo
['pageid'] ] );
236 if ( $url !== false ) {
244 * @param string $type
245 * @return int|null|string
247 public function getUser( $type = 'text' ) {
248 if ( $type == 'text' ) {
249 return isset( $this->mInfo
['user'] ) ?
strval( $this->mInfo
['user'] ) : null;
251 return 0; // What makes sense here, for a remote user?
256 * @param int $audience
258 * @return null|string
260 public function getDescription( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
261 return isset( $this->mInfo
['comment'] ) ?
strval( $this->mInfo
['comment'] ) : null;
265 * @return null|string
268 return isset( $this->mInfo
['sha1'] )
269 ? Wikimedia\base_convert
( strval( $this->mInfo
['sha1'] ), 16, 36, 31 )
274 * @return bool|string
276 function getTimestamp() {
277 return wfTimestamp( TS_MW
,
278 isset( $this->mInfo
['timestamp'] )
279 ?
strval( $this->mInfo
['timestamp'] )
287 function getMimeType() {
288 if ( !isset( $this->mInfo
['mime'] ) ) {
289 $magic = MimeMagic
::singleton();
290 $this->mInfo
['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
293 return $this->mInfo
['mime'];
299 function getMediaType() {
300 if ( isset( $this->mInfo
['mediatype'] ) ) {
301 return $this->mInfo
['mediatype'];
303 $magic = MimeMagic
::singleton();
305 return $magic->getMediaType( null, $this->getMimeType() );
309 * @return bool|string
311 function getDescriptionUrl() {
312 return isset( $this->mInfo
['descriptionurl'] )
313 ?
$this->mInfo
['descriptionurl']
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 ] );
343 foreach ( $iter as $file ) {
350 function purgeCache( $options = [] ) {
351 $this->purgeThumbnails( $options );
352 $this->purgeDescriptionPage();
355 function purgeDescriptionPage() {
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();
375 $handler->filterThumbnailPurgeList( $files, $options );
378 $dir = $this->getThumbPath( $this->getName() );
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
395 public function isTransformedLocally() {