moving SpecialUploadMogile.php from phase3/includes/specials to extensions/MogileClie...
[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 $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 if ( isset( $this->mInfo['metadata'] ) ) {
63 return serialize( self::parseMetadata( $this->mInfo['metadata'] ) );
64 }
65 return null;
66 }
67
68 public static function parseMetadata( $metadata ) {
69 if( !is_array( $metadata ) ) {
70 return $metadata;
71 }
72 $ret = array();
73 foreach( $metadata as $meta ) {
74 $ret[ $meta['name'] ] = self::parseMetadata( $meta['value'] );
75 }
76 return $ret;
77 }
78
79 public function getSize() {
80 return intval( @$this->mInfo['size'] );
81 }
82
83 public function getUrl() {
84 return strval( @$this->mInfo['url'] );
85 }
86
87 public function getUser( $method='text' ) {
88 return strval( @$this->mInfo['user'] );
89 }
90
91 public function getDescription() {
92 return strval( @$this->mInfo['comment'] );
93 }
94
95 function getSha1() {
96 return wfBaseConvert( strval( @$this->mInfo['sha1'] ), 16, 36, 31 );
97 }
98
99 function getTimestamp() {
100 return wfTimestamp( TS_MW, strval( @$this->mInfo['timestamp'] ) );
101 }
102
103 function getMimeType() {
104 if( !isset( $this->mInfo['mime'] ) ) {
105 $magic = MimeMagic::singleton();
106 $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
107 }
108 return $this->mInfo['mime'];
109 }
110
111 /// @fixme May guess wrong on file types that can be eg audio or video
112 function getMediaType() {
113 $magic = MimeMagic::singleton();
114 return $magic->getMediaType( null, $this->getMimeType() );
115 }
116
117 function getDescriptionUrl() {
118 return isset( $this->mInfo['descriptionurl'] )
119 ? $this->mInfo['descriptionurl']
120 : false;
121 }
122
123 /**
124 * Only useful if we're locally caching thumbs anyway...
125 */
126 function getThumbPath( $suffix = '' ) {
127 if ( $this->repo->canCacheThumbs() ) {
128 global $wgUploadDirectory;
129 $path = $wgUploadDirectory . '/thumb/' . $this->getHashPath( $this->getName() );
130 if ( $suffix ) {
131 $path = $path . $suffix . '/';
132 }
133 return $path;
134 }
135 else {
136 return null;
137 }
138 }
139
140 function getThumbnails() {
141 $files = array();
142 $dir = $this->getThumbPath( $this->getName() );
143 if ( is_dir( $dir ) ) {
144 $handle = opendir( $dir );
145 if ( $handle ) {
146 while ( false !== ( $file = readdir($handle) ) ) {
147 if ( $file{0} != '.' ) {
148 $files[] = $file;
149 }
150 }
151 closedir( $handle );
152 }
153 }
154 return $files;
155 }
156
157 function purgeCache() {
158 $this->purgeThumbnails();
159 $this->purgeDescriptionPage();
160 }
161
162 function purgeDescriptionPage() {
163 global $wgMemc, $wgContLang;
164 $url = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
165 $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', md5($url) );
166 $wgMemc->delete( $key );
167 }
168
169 function purgeThumbnails() {
170 global $wgMemc;
171 $key = $this->repo->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
172 $wgMemc->delete( $key );
173 $files = $this->getThumbnails();
174 $dir = $this->getThumbPath( $this->getName() );
175 foreach ( $files as $file ) {
176 unlink( $dir . $file );
177 }
178 if ( is_dir( $dir ) ) {
179 rmdir( $dir ); // Might have already gone away, spews errors if we don't.
180 }
181 }
182 }