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