69223e1997347530beed314bdde09e860ccf7e2a
[lhc/web/wiklou.git] / includes / filerepo / ForeignAPIFile.php
1 <?php
2 /**
3 * Foreign file accessible through api.php requests.
4 *
5 * @file
6 * @ingroup FileRepo
7 */
8
9 /**
10 * Foreign file accessible through api.php requests.
11 * Very hacky and inefficient, do not use :D
12 *
13 * @ingroup FileRepo
14 */
15 class ForeignAPIFile extends File {
16
17 private $mExists;
18
19 /**
20 * @param $title
21 * @param $repo ForeignApiRepo
22 * @param $info
23 * @param bool $exists
24 */
25 function __construct( $title, $repo, $info, $exists = false ) {
26 parent::__construct( $title, $repo );
27 $this->mInfo = $info;
28 $this->mExists = $exists;
29 }
30
31 /**
32 * @param $title Title
33 * @param $repo ForeignApiRepo
34 * @return ForeignAPIFile|null
35 */
36 static function newFromTitle( $title, $repo ) {
37 $data = $repo->fetchImageQuery( array(
38 'titles' => 'File:' . $title->getDBKey(),
39 'iiprop' => self::getProps(),
40 'prop' => 'imageinfo',
41 'iimetadataversion' => mediaHandler::getMetadataVersion()
42 ) );
43
44 $info = $repo->getImageInfo( $data );
45
46 if( $info ) {
47 $lastRedirect = isset( $data['query']['redirects'] )
48 ? count( $data['query']['redirects'] ) - 1
49 : -1;
50 if( $lastRedirect >= 0 ) {
51 $newtitle = Title::newFromText( $data['query']['redirects'][$lastRedirect]['to']);
52 $img = new ForeignAPIFile( $newtitle, $repo, $info, true );
53 if( $img ) {
54 $img->redirectedFrom( $title->getDBkey() );
55 }
56 } else {
57 $img = new ForeignAPIFile( $title, $repo, $info, true );
58 }
59 return $img;
60 } else {
61 return null;
62 }
63 }
64
65 /**
66 * Get the property string for iiprop and aiprop
67 */
68 static function getProps() {
69 return 'timestamp|user|comment|url|size|sha1|metadata|mime';
70 }
71
72 // Dummy functions...
73 public function exists() {
74 return $this->mExists;
75 }
76
77 public function getPath() {
78 return false;
79 }
80
81 function transform( $params, $flags = 0 ) {
82 if( !$this->canRender() ) {
83 // show icon
84 return parent::transform( $params, $flags );
85 }
86
87 // Note, the this->canRender() check above implies
88 // that we have a handler, and it can do makeParamString.
89 $otherParams = $this->handler->makeParamString( $params );
90
91 $thumbUrl = $this->repo->getThumbUrlFromCache(
92 $this->getName(),
93 isset( $params['width'] ) ? $params['width'] : -1,
94 isset( $params['height'] ) ? $params['height'] : -1,
95 $otherParams );
96 return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );
97 }
98
99 // Info we can get from API...
100 public function getWidth( $page = 1 ) {
101 return intval( @$this->mInfo['width'] );
102 }
103
104 public function getHeight( $page = 1 ) {
105 return intval( @$this->mInfo['height'] );
106 }
107
108 public function getMetadata() {
109 if ( isset( $this->mInfo['metadata'] ) ) {
110 return serialize( self::parseMetadata( $this->mInfo['metadata'] ) );
111 }
112 return null;
113 }
114
115 public static function parseMetadata( $metadata ) {
116 if( !is_array( $metadata ) ) {
117 return $metadata;
118 }
119 $ret = array();
120 foreach( $metadata as $meta ) {
121 $ret[ $meta['name'] ] = self::parseMetadata( $meta['value'] );
122 }
123 return $ret;
124 }
125
126 public function getSize() {
127 return isset( $this->mInfo['size'] ) ? intval( $this->mInfo['size'] ) : null;
128 }
129
130 public function getUrl() {
131 return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null;
132 }
133
134 public function getUser( $method='text' ) {
135 return isset( $this->mInfo['user'] ) ? strval( $this->mInfo['user'] ) : null;
136 }
137
138 public function getDescription() {
139 return isset( $this->mInfo['comment'] ) ? strval( $this->mInfo['comment'] ) : null;
140 }
141
142 function getSha1() {
143 return isset( $this->mInfo['sha1'] ) ?
144 wfBaseConvert( strval( $this->mInfo['sha1'] ), 16, 36, 31 ) :
145 null;
146 }
147
148 function getTimestamp() {
149 return wfTimestamp( TS_MW,
150 isset( $this->mInfo['timestamp'] ) ?
151 strval( $this->mInfo['timestamp'] ) :
152 null
153 );
154 }
155
156 function getMimeType() {
157 if( !isset( $this->mInfo['mime'] ) ) {
158 $magic = MimeMagic::singleton();
159 $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
160 }
161 return $this->mInfo['mime'];
162 }
163
164 /// @todo Fixme: may guess wrong on file types that can be eg audio or video
165 function getMediaType() {
166 $magic = MimeMagic::singleton();
167 return $magic->getMediaType( null, $this->getMimeType() );
168 }
169
170 function getDescriptionUrl() {
171 return isset( $this->mInfo['descriptionurl'] )
172 ? $this->mInfo['descriptionurl']
173 : false;
174 }
175
176 /**
177 * Only useful if we're locally caching thumbs anyway...
178 */
179 function getThumbPath( $suffix = '' ) {
180 if ( $this->repo->canCacheThumbs() ) {
181 $path = $this->repo->getZonePath('thumb') . '/' . $this->getHashPath( $this->getName() );
182 if ( $suffix ) {
183 $path = $path . $suffix . '/';
184 }
185 return $path;
186 } else {
187 return null;
188 }
189 }
190
191 function getThumbnails() {
192 $files = array();
193 $dir = $this->getThumbPath( $this->getName() );
194 if ( is_dir( $dir ) ) {
195 $handle = opendir( $dir );
196 if ( $handle ) {
197 while ( false !== ( $file = readdir($handle) ) ) {
198 if ( $file[0] != '.' ) {
199 $files[] = $file;
200 }
201 }
202 closedir( $handle );
203 }
204 }
205 return $files;
206 }
207
208 function purgeCache() {
209 $this->purgeThumbnails();
210 $this->purgeDescriptionPage();
211 }
212
213 function purgeDescriptionPage() {
214 global $wgMemc, $wgContLang;
215 $url = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
216 $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', md5($url) );
217 $wgMemc->delete( $key );
218 }
219
220 function purgeThumbnails() {
221 global $wgMemc;
222 $key = $this->repo->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
223 $wgMemc->delete( $key );
224 $files = $this->getThumbnails();
225 $dir = $this->getThumbPath( $this->getName() );
226 foreach ( $files as $file ) {
227 unlink( $dir . $file );
228 }
229 if ( is_dir( $dir ) ) {
230 rmdir( $dir ); // Might have already gone away, spews errors if we don't.
231 }
232 }
233 }