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
{
33 protected $repoClass = 'ForeignApiRepo';
36 * @param Title|string|bool $title
37 * @param ForeignApiRepo $repo
41 function __construct( $title, $repo, $info, $exists = false ) {
42 parent
::__construct( $title, $repo );
45 $this->mExists
= $exists;
47 $this->assertRepoDefined();
52 * @param ForeignApiRepo $repo
53 * @return ForeignAPIFile|null
55 static function newFromTitle( Title
$title, $repo ) {
56 $data = $repo->fetchImageQuery( array(
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,
66 $info = $repo->getImageInfo( $data );
69 $lastRedirect = isset( $data['query']['redirects'] )
70 ?
count( $data['query']['redirects'] ) - 1
72 if ( $lastRedirect >= 0 ) {
73 $newtitle = Title
::newFromText( $data['query']['redirects'][$lastRedirect]['to'] );
74 $img = new self( $newtitle, $repo, $info, true );
76 $img->redirectedFrom( $title->getDBkey() );
79 $img = new self( $title, $repo, $info, true );
89 * Get the property string for iiprop and aiprop
92 static function getProps() {
93 return 'timestamp|user|comment|url|size|sha1|metadata|mime|mediatype|extmetadata';
101 public function exists() {
102 return $this->mExists
;
108 public function getPath() {
113 * @param array $params
115 * @return bool|MediaTransformOutput
117 function transform( $params, $flags = 0 ) {
118 if ( !$this->canRender() ) {
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(
135 if ( $thumbUrl === false ) {
138 return $this->repo
->getThumbError(
147 return $this->handler
->getTransform( $this, 'bogus', $thumbUrl, $params );
150 // Info we can get from API...
156 public function getWidth( $page = 1 ) {
157 return isset( $this->mInfo
['width'] ) ?
intval( $this->mInfo
['width'] ) : 0;
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'] ) );
180 * @return array|null Extended metadata (see imageinfo API for format) or
183 public function getExtendedMetadata() {
184 if ( isset( $this->mInfo
['extmetadata'] ) ) {
185 return $this->mInfo
['extmetadata'];
192 * @param array $metadata
195 public static function parseMetadata( $metadata ) {
196 if ( !is_array( $metadata ) ) {
200 foreach ( $metadata as $meta ) {
201 $ret[$meta['name']] = self
::parseMetadata( $meta['value'] );
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 * @param string $method
223 * @return int|null|string
225 public function getUser( $method = 'text' ) {
226 return isset( $this->mInfo
['user'] ) ?
strval( $this->mInfo
['user'] ) : null;
230 * @param int $audience
232 * @return null|string
234 public function getDescription( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
235 return isset( $this->mInfo
['comment'] ) ?
strval( $this->mInfo
['comment'] ) : null;
239 * @return null|string
242 return isset( $this->mInfo
['sha1'] )
243 ?
wfBaseConvert( strval( $this->mInfo
['sha1'] ), 16, 36, 31 )
248 * @return bool|string
250 function getTimestamp() {
251 return wfTimestamp( TS_MW
,
252 isset( $this->mInfo
['timestamp'] )
253 ?
strval( $this->mInfo
['timestamp'] )
261 function getMimeType() {
262 if ( !isset( $this->mInfo
['mime'] ) ) {
263 $magic = MimeMagic
::singleton();
264 $this->mInfo
['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
267 return $this->mInfo
['mime'];
273 function getMediaType() {
274 if ( isset( $this->mInfo
['mediatype'] ) ) {
275 return $this->mInfo
['mediatype'];
277 $magic = MimeMagic
::singleton();
279 return $magic->getMediaType( null, $this->getMimeType() );
283 * @return bool|string
285 function getDescriptionUrl() {
286 return isset( $this->mInfo
['descriptionurl'] )
287 ?
$this->mInfo
['descriptionurl']
292 * Only useful if we're locally caching thumbs anyway...
293 * @param string $suffix
294 * @return null|string
296 function getThumbPath( $suffix = '' ) {
297 if ( $this->repo
->canCacheThumbs() ) {
298 $path = $this->repo
->getZonePath( 'thumb' ) . '/' . $this->getHashPath( $this->getName() );
300 $path = $path . $suffix . '/';
312 function getThumbnails() {
313 $dir = $this->getThumbPath( $this->getName() );
314 $iter = $this->repo
->getBackend()->getFileList( array( 'dir' => $dir ) );
317 foreach ( $iter as $file ) {
325 * @see File::purgeCache()
327 function purgeCache( $options = array() ) {
328 $this->purgeThumbnails( $options );
329 $this->purgeDescriptionPage();
332 function purgeDescriptionPage() {
333 global $wgMemc, $wgContLang;
335 $url = $this->repo
->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
336 $key = $this->repo
->getLocalCacheKey( 'RemoteFileDescription', 'url', md5( $url ) );
338 $wgMemc->delete( $key );
342 * @param array $options
344 function purgeThumbnails( $options = array() ) {
347 $key = $this->repo
->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
348 $wgMemc->delete( $key );
350 $files = $this->getThumbnails();
351 // Give media handler a chance to filter the purge list
352 $handler = $this->getHandler();
354 $handler->filterThumbnailPurgeList( $files, $options );
357 $dir = $this->getThumbPath( $this->getName() );
358 $purgeList = array();
359 foreach ( $files as $file ) {
360 $purgeList[] = "{$dir}{$file}";
363 # Delete the thumbnails
364 $this->repo
->quickPurgeBatch( $purgeList );
365 # Clear out the thumbnail directory if empty
366 $this->repo
->quickCleanDir( $dir );