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