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