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