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