Merge "move page_restrictions.pr_id to top in tables.sql"
[mediawiki.git] / includes / filerepo / file / ForeignAPIFile.php
blob0b3d5dfdb06f4d0ed1b47f988c4df15183451012
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
37 * @param $repo ForeignApiRepo
38 * @param $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 $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,
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 );
81 return $img;
82 } else {
83 return null;
87 /**
88 * Get the property string for iiprop and aiprop
89 * @return string
91 static function getProps() {
92 return 'timestamp|user|comment|url|size|sha1|metadata|mime|mediatype|extmetadata';
95 // Dummy functions...
97 /**
98 * @return bool
100 public function exists() {
101 return $this->mExists;
105 * @return bool
107 public function getPath() {
108 return false;
112 * @param array $params
113 * @param int $flags
114 * @return bool|MediaTransformOutput
116 function transform( $params, $flags = 0 ) {
117 if ( !$this->canRender() ) {
118 // show icon
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(
129 $this->getName(),
130 $width,
131 $height,
132 $otherParams
134 if ( $thumbUrl === false ) {
135 global $wgLang;
136 return $this->repo->getThumbError(
137 $this->getName(),
138 $width,
139 $height,
140 $otherParams,
141 $wgLang->getCode()
144 return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );
147 // Info we can get from API...
150 * @param $page int
151 * @return int|number
153 public function getWidth( $page = 1 ) {
154 return isset( $this->mInfo['width'] ) ? intval( $this->mInfo['width'] ) : 0;
158 * @param $page int
159 * @return int
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'] ) );
172 return null;
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'];
182 return null;
186 * @param $metadata array
187 * @return array
189 public static function parseMetadata( $metadata ) {
190 if ( !is_array( $metadata ) ) {
191 return $metadata;
193 $ret = array();
194 foreach ( $metadata as $meta ) {
195 $ret[$meta['name']] = self::parseMetadata( $meta['value'] );
197 return $ret;
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
232 function getSha1() {
233 return isset( $this->mInfo['sha1'] )
234 ? wfBaseConvert( strval( $this->mInfo['sha1'] ), 16, 36, 31 )
235 : null;
239 * @return bool|Mixed|string
241 function getTimestamp() {
242 return wfTimestamp( TS_MW,
243 isset( $this->mInfo['timestamp'] )
244 ? strval( $this->mInfo['timestamp'] )
245 : null
250 * @return string
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'];
261 * @return int|string
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']
277 : false;
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() );
288 if ( $suffix ) {
289 $path = $path . $suffix . '/';
291 return $path;
292 } else {
293 return null;
298 * @return array
300 function getThumbnails() {
301 $dir = $this->getThumbPath( $this->getName() );
302 $iter = $this->repo->getBackend()->getFileList( array( 'dir' => $dir ) );
304 $files = array();
305 foreach ( $iter as $file ) {
306 $files[] = $file;
309 return $files;
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() ) {
333 global $wgMemc;
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();
341 if ( $handler ) {
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 );