]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/filerepo/ForeignAPIFile.php
MediaWiki 1.14.0
[autoinstallsdev/mediawiki.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                 $info = $repo->getImageInfo( $title );
21                 if( $info ) {
22                         return new ForeignAPIFile( $title, $repo, $info, true );
23                 } else {
24                         return null;
25                 }
26         }
27         
28         // Dummy functions...
29         public function exists() {
30                 return $this->mExists;
31         }
32         
33         public function getPath() {
34                 return false;
35         }
36
37         function transform( $params, $flags = 0 ) {
38                 if( !$this->canRender() ) {
39                         // show icon
40                         return parent::transform( $params, $flags );
41                 }
42                 $thumbUrl = $this->repo->getThumbUrlFromCache(
43                                 $this->getName(),
44                                 isset( $params['width'] ) ? $params['width'] : -1,
45                                 isset( $params['height'] ) ? $params['height'] : -1 );
46                 if( $thumbUrl ) {
47                         return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );;
48                 }
49                 return false;
50         }
51
52         // Info we can get from API...
53         public function getWidth( $page = 1 ) {
54                 return intval( @$this->mInfo['width'] );
55         }
56         
57         public function getHeight( $page = 1 ) {
58                 return intval( @$this->mInfo['height'] );
59         }
60         
61         public function getMetadata() {
62                 return serialize( (array)@$this->mInfo['metadata'] );
63         }
64         
65         public function getSize() {
66                 return intval( @$this->mInfo['size'] );
67         }
68         
69         public function getUrl() {
70                 return strval( @$this->mInfo['url'] );
71         }
72
73         public function getUser( $method='text' ) {
74                 return strval( @$this->mInfo['user'] );
75         }
76         
77         public function getDescription() {
78                 return strval( @$this->mInfo['comment'] );
79         }
80
81         function getSha1() {
82                 return wfBaseConvert( strval( @$this->mInfo['sha1'] ), 16, 36, 31 );
83         }
84         
85         function getTimestamp() {
86                 return wfTimestamp( TS_MW, strval( @$this->mInfo['timestamp'] ) );
87         }
88         
89         function getMimeType() {
90                 if( empty( $info['mime'] ) ) {
91                         $magic = MimeMagic::singleton();
92                         $info['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
93                 }
94                 return $info['mime'];
95         }
96         
97         /// @fixme May guess wrong on file types that can be eg audio or video
98         function getMediaType() {
99                 $magic = MimeMagic::singleton();
100                 return $magic->getMediaType( null, $this->getMimeType() );
101         }
102         
103         function getDescriptionUrl() {
104                 return isset( $this->mInfo['descriptionurl'] )
105                         ? $this->mInfo['descriptionurl']
106                         : false;
107         }
108         
109         /**
110          * Only useful if we're locally caching thumbs anyway...
111          */
112         function getThumbPath( $suffix = '' ) {
113                 if ( $this->repo->canCacheThumbs() ) {
114                         global $wgUploadDirectory;
115                         $path = $wgUploadDirectory . '/thumb/' . $this->getHashPath( $this->getName() );
116                         if ( $suffix ) {
117                                 $path = $path . $suffix . '/';
118                         }
119                         return $path;
120                 }
121                 else {
122                         return null;    
123                 }
124         }
125         
126         function getThumbnails() {
127                 $files = array();
128                 $dir = $this->getThumbPath( $this->getName() );
129                 if ( is_dir( $dir ) ) {
130                         $handle = opendir( $dir );
131                         if ( $handle ) {
132                                 while ( false !== ( $file = readdir($handle) ) ) {
133                                         if ( $file{0} != '.'  ) {
134                                                 $files[] = $file;
135                                         }
136                                 }
137                                 closedir( $handle );
138                         }
139                 }
140                 return $files;
141         }
142         
143         function purgeCache() {
144                 $this->purgeThumbnails();
145                 $this->purgeDescriptionPage();
146         }
147         
148         function purgeDescriptionPage() {
149                 global $wgMemc;
150                 $url = $this->repo->getDescriptionRenderUrl( $this->getName() );
151                 $key = wfMemcKey( 'RemoteFileDescription', 'url', md5($url) );
152                 $wgMemc->delete( $key );
153         }
154         
155         function purgeThumbnails() {
156                 global $wgMemc;
157                 $key = wfMemcKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
158                 $wgMemc->delete( $key );
159                 $files = $this->getThumbnails();
160                 $dir = $this->getThumbPath( $this->getName() );
161                 foreach ( $files as $file ) {
162                         unlink( $dir . $file );
163                 }
164                 if ( is_dir( $dir ) ) {
165                         rmdir( $dir ); // Might have already gone away, spews errors if we don't.
166                 }
167         }
168 }