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';
37 * @param $repo ForeignApiRepo
41 function __construct( $title, $repo, $info, $exists = false ) {
42 parent
::__construct( $title, $repo );
45 $this->mExists
= $exists;
47 $this->assertRepoDefined();
52 * @param $repo ForeignApiRepo
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 );
88 * Get the property string for iiprop and aiprop
91 static function getProps() {
92 return 'timestamp|user|comment|url|size|sha1|metadata|mime|mediatype|extmetadata';
100 public function exists() {
101 return $this->mExists
;
107 public function getPath() {
112 * @param array $params
114 * @return bool|MediaTransformOutput
116 function transform( $params, $flags = 0 ) {
117 if ( !$this->canRender() ) {
119 return parent
::transform( $params, $flags );
122 // Note, the this->canRender() check above implies
123 // that we have a handler, and it can do makeParamString.
124 $otherParams = $this->handler
->makeParamString( $params );
125 $width = isset( $params['width'] ) ?
$params['width'] : -1;
126 $height = isset( $params['height'] ) ?
$params['height'] : -1;
128 $thumbUrl = $this->repo
->getThumbUrlFromCache(
134 if ( $thumbUrl === false ) {
136 return $this->repo
->getThumbError(
144 return $this->handler
->getTransform( $this, 'bogus', $thumbUrl, $params );
147 // Info we can get from API...
153 public function getWidth( $page = 1 ) {
154 return isset( $this->mInfo
['width'] ) ?
intval( $this->mInfo
['width'] ) : 0;
161 public function getHeight( $page = 1 ) {
162 return isset( $this->mInfo
['height'] ) ?
intval( $this->mInfo
['height'] ) : 0;
166 * @return bool|null|string
168 public function getMetadata() {
169 if ( isset( $this->mInfo
['metadata'] ) ) {
170 return serialize( self
::parseMetadata( $this->mInfo
['metadata'] ) );
176 * @return array|null extended metadata (see imageinfo API for format) or null on error
178 public function getExtendedMetadata() {
179 if ( isset( $this->mInfo
['extmetadata'] ) ) {
180 return $this->mInfo
['extmetadata'];
186 * @param $metadata array
189 public static function parseMetadata( $metadata ) {
190 if ( !is_array( $metadata ) ) {
194 foreach ( $metadata as $meta ) {
195 $ret[$meta['name']] = self
::parseMetadata( $meta['value'] );
201 * @return bool|int|null
203 public function getSize() {
204 return isset( $this->mInfo
['size'] ) ?
intval( $this->mInfo
['size'] ) : null;
208 * @return null|string
210 public function getUrl() {
211 return isset( $this->mInfo
['url'] ) ?
strval( $this->mInfo
['url'] ) : null;
215 * @param string $method
216 * @return int|null|string
218 public function getUser( $method = 'text' ) {
219 return isset( $this->mInfo
['user'] ) ?
strval( $this->mInfo
['user'] ) : null;
223 * @return null|string
225 public function getDescription( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
226 return isset( $this->mInfo
['comment'] ) ?
strval( $this->mInfo
['comment'] ) : null;
230 * @return null|String
233 return isset( $this->mInfo
['sha1'] )
234 ?
wfBaseConvert( strval( $this->mInfo
['sha1'] ), 16, 36, 31 )
239 * @return bool|Mixed|string
241 function getTimestamp() {
242 return wfTimestamp( TS_MW
,
243 isset( $this->mInfo
['timestamp'] )
244 ?
strval( $this->mInfo
['timestamp'] )
252 function getMimeType() {
253 if ( !isset( $this->mInfo
['mime'] ) ) {
254 $magic = MimeMagic
::singleton();
255 $this->mInfo
['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
257 return $this->mInfo
['mime'];
263 function getMediaType() {
264 if ( isset( $this->mInfo
['mediatype'] ) ) {
265 return $this->mInfo
['mediatype'];
267 $magic = MimeMagic
::singleton();
268 return $magic->getMediaType( null, $this->getMimeType() );
272 * @return bool|string
274 function getDescriptionUrl() {
275 return isset( $this->mInfo
['descriptionurl'] )
276 ?
$this->mInfo
['descriptionurl']
281 * Only useful if we're locally caching thumbs anyway...
282 * @param $suffix string
283 * @return null|string
285 function getThumbPath( $suffix = '' ) {
286 if ( $this->repo
->canCacheThumbs() ) {
287 $path = $this->repo
->getZonePath( 'thumb' ) . '/' . $this->getHashPath( $this->getName() );
289 $path = $path . $suffix . '/';
300 function getThumbnails() {
301 $dir = $this->getThumbPath( $this->getName() );
302 $iter = $this->repo
->getBackend()->getFileList( array( 'dir' => $dir ) );
305 foreach ( $iter as $file ) {
313 * @see File::purgeCache()
315 function purgeCache( $options = array() ) {
316 $this->purgeThumbnails( $options );
317 $this->purgeDescriptionPage();
320 function purgeDescriptionPage() {
321 global $wgMemc, $wgContLang;
323 $url = $this->repo
->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
324 $key = $this->repo
->getLocalCacheKey( 'RemoteFileDescription', 'url', md5( $url ) );
326 $wgMemc->delete( $key );
330 * @param $options array
332 function purgeThumbnails( $options = array() ) {
335 $key = $this->repo
->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
336 $wgMemc->delete( $key );
338 $files = $this->getThumbnails();
339 // Give media handler a chance to filter the purge list
340 $handler = $this->getHandler();
342 $handler->filterThumbnailPurgeList( $files, $options );
345 $dir = $this->getThumbPath( $this->getName() );
346 $purgeList = array();
347 foreach ( $files as $file ) {
348 $purgeList[] = "{$dir}{$file}";
351 # Delete the thumbnails
352 $this->repo
->quickPurgeBatch( $purgeList );
353 # Clear out the thumbnail directory if empty
354 $this->repo
->quickCleanDir( $dir );