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