Merge "Make line breaks in <blockquote> behave like <div> (bug 6200)."
[mediawiki.git] / includes / filerepo / file / ForeignAPIFile.php
blob8829cd92e49ce2f85b5b82e79bab3d3103e58a82
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 ) );
63 $info = $repo->getImageInfo( $data );
65 if ( $info ) {
66 $lastRedirect = isset( $data['query']['redirects'] )
67 ? count( $data['query']['redirects'] ) - 1
68 : -1;
69 if ( $lastRedirect >= 0 ) {
70 $newtitle = Title::newFromText( $data['query']['redirects'][$lastRedirect]['to'] );
71 $img = new self( $newtitle, $repo, $info, true );
72 if ( $img ) {
73 $img->redirectedFrom( $title->getDBkey() );
75 } else {
76 $img = new self( $title, $repo, $info, true );
78 return $img;
79 } else {
80 return null;
84 /**
85 * Get the property string for iiprop and aiprop
86 * @return string
88 static function getProps() {
89 return 'timestamp|user|comment|url|size|sha1|metadata|mime';
92 // Dummy functions...
94 /**
95 * @return bool
97 public function exists() {
98 return $this->mExists;
102 * @return bool
104 public function getPath() {
105 return false;
109 * @param array $params
110 * @param int $flags
111 * @return bool|MediaTransformOutput
113 function transform( $params, $flags = 0 ) {
114 if ( !$this->canRender() ) {
115 // show icon
116 return parent::transform( $params, $flags );
119 // Note, the this->canRender() check above implies
120 // that we have a handler, and it can do makeParamString.
121 $otherParams = $this->handler->makeParamString( $params );
122 $width = isset( $params['width'] ) ? $params['width'] : -1;
123 $height = isset( $params['height'] ) ? $params['height'] : -1;
125 $thumbUrl = $this->repo->getThumbUrlFromCache(
126 $this->getName(),
127 $width,
128 $height,
129 $otherParams
131 if ( $thumbUrl === false ) {
132 global $wgLang;
133 return $this->repo->getThumbError(
134 $this->getName(),
135 $width,
136 $height,
137 $otherParams,
138 $wgLang->getCode()
141 return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );
144 // Info we can get from API...
147 * @param $page int
148 * @return int|number
150 public function getWidth( $page = 1 ) {
151 return isset( $this->mInfo['width'] ) ? intval( $this->mInfo['width'] ) : 0;
155 * @param $page int
156 * @return int
158 public function getHeight( $page = 1 ) {
159 return isset( $this->mInfo['height'] ) ? intval( $this->mInfo['height'] ) : 0;
163 * @return bool|null|string
165 public function getMetadata() {
166 if ( isset( $this->mInfo['metadata'] ) ) {
167 return serialize( self::parseMetadata( $this->mInfo['metadata'] ) );
169 return null;
173 * @param $metadata array
174 * @return array
176 public static function parseMetadata( $metadata ) {
177 if ( !is_array( $metadata ) ) {
178 return $metadata;
180 $ret = array();
181 foreach ( $metadata as $meta ) {
182 $ret[$meta['name']] = self::parseMetadata( $meta['value'] );
184 return $ret;
188 * @return bool|int|null
190 public function getSize() {
191 return isset( $this->mInfo['size'] ) ? intval( $this->mInfo['size'] ) : null;
195 * @return null|string
197 public function getUrl() {
198 return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null;
202 * @param string $method
203 * @return int|null|string
205 public function getUser( $method = 'text' ) {
206 return isset( $this->mInfo['user'] ) ? strval( $this->mInfo['user'] ) : null;
210 * @return null|string
212 public function getDescription( $audience = self::FOR_PUBLIC, User $user = null ) {
213 return isset( $this->mInfo['comment'] ) ? strval( $this->mInfo['comment'] ) : null;
217 * @return null|String
219 function getSha1() {
220 return isset( $this->mInfo['sha1'] )
221 ? wfBaseConvert( strval( $this->mInfo['sha1'] ), 16, 36, 31 )
222 : null;
226 * @return bool|Mixed|string
228 function getTimestamp() {
229 return wfTimestamp( TS_MW,
230 isset( $this->mInfo['timestamp'] )
231 ? strval( $this->mInfo['timestamp'] )
232 : null
237 * @return string
239 function getMimeType() {
240 if ( !isset( $this->mInfo['mime'] ) ) {
241 $magic = MimeMagic::singleton();
242 $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
244 return $this->mInfo['mime'];
248 * @todo FIXME: May guess wrong on file types that can be eg audio or video
249 * @return int|string
251 function getMediaType() {
252 $magic = MimeMagic::singleton();
253 return $magic->getMediaType( null, $this->getMimeType() );
257 * @return bool|string
259 function getDescriptionUrl() {
260 return isset( $this->mInfo['descriptionurl'] )
261 ? $this->mInfo['descriptionurl']
262 : false;
266 * Only useful if we're locally caching thumbs anyway...
267 * @param $suffix string
268 * @return null|string
270 function getThumbPath( $suffix = '' ) {
271 if ( $this->repo->canCacheThumbs() ) {
272 $path = $this->repo->getZonePath( 'thumb' ) . '/' . $this->getHashPath( $this->getName() );
273 if ( $suffix ) {
274 $path = $path . $suffix . '/';
276 return $path;
277 } else {
278 return null;
283 * @return array
285 function getThumbnails() {
286 $dir = $this->getThumbPath( $this->getName() );
287 $iter = $this->repo->getBackend()->getFileList( array( 'dir' => $dir ) );
289 $files = array();
290 foreach ( $iter as $file ) {
291 $files[] = $file;
294 return $files;
298 * @see File::purgeCache()
300 function purgeCache( $options = array() ) {
301 $this->purgeThumbnails( $options );
302 $this->purgeDescriptionPage();
305 function purgeDescriptionPage() {
306 global $wgMemc, $wgContLang;
308 $url = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
309 $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', md5( $url ) );
311 $wgMemc->delete( $key );
315 * @param $options array
317 function purgeThumbnails( $options = array() ) {
318 global $wgMemc;
320 $key = $this->repo->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
321 $wgMemc->delete( $key );
323 $files = $this->getThumbnails();
324 // Give media handler a chance to filter the purge list
325 $handler = $this->getHandler();
326 if ( $handler ) {
327 $handler->filterThumbnailPurgeList( $files, $options );
330 $dir = $this->getThumbPath( $this->getName() );
331 $purgeList = array();
332 foreach ( $files as $file ) {
333 $purgeList[] = "{$dir}{$file}";
336 # Delete the thumbnails
337 $this->repo->quickPurgeBatch( $purgeList );
338 # Clear out the thumbnail directory if empty
339 $this->repo->quickCleanDir( $dir );