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