Add more error handling
[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 if ( $this->repo->apiThumbCacheExpiry > 0 && $this->repo->apiThumbCacheDir ) {
35 $thumbUrl = $this->repo->getThumbUrlFromCache(
36 $this->getName(),
37 isset( $params['width'] ) ? $params['width'] : -1,
38 isset( $params['height'] ) ? $params['height'] : -1 );
39 } else {
40 $thumbUrl = $this->repo->getThumbUrl(
41 $this->getName(),
42 isset( $params['width'] ) ? $params['width'] : -1,
43 isset( $params['height'] ) ? $params['height'] : -1 );
44 }
45 if( $thumbUrl ) {
46 return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );;
47 }
48 return false;
49 }
50
51 // Info we can get from API...
52 public function getWidth( $page = 1 ) {
53 return intval( @$this->mInfo['width'] );
54 }
55
56 public function getHeight( $page = 1 ) {
57 return intval( @$this->mInfo['height'] );
58 }
59
60 public function getMetadata() {
61 return serialize( (array)@$this->mInfo['metadata'] );
62 }
63
64 public function getSize() {
65 return intval( @$this->mInfo['size'] );
66 }
67
68 public function getUrl() {
69 return strval( @$this->mInfo['url'] );
70 }
71
72 public function getUser( $method='text' ) {
73 return strval( @$this->mInfo['user'] );
74 }
75
76 public function getDescription() {
77 return strval( @$this->mInfo['comment'] );
78 }
79
80 function getSha1() {
81 return wfBaseConvert( strval( @$this->mInfo['sha1'] ), 16, 36, 31 );
82 }
83
84 function getTimestamp() {
85 return wfTimestamp( TS_MW, strval( @$this->mInfo['timestamp'] ) );
86 }
87
88 function getMimeType() {
89 if( empty( $info['mime'] ) ) {
90 $magic = MimeMagic::singleton();
91 $info['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
92 }
93 return $info['mime'];
94 }
95
96 /// @fixme May guess wrong on file types that can be eg audio or video
97 function getMediaType() {
98 $magic = MimeMagic::singleton();
99 return $magic->getMediaType( null, $this->getMimeType() );
100 }
101
102 function getDescriptionUrl() {
103 return isset( $this->mInfo['descriptionurl'] )
104 ? $this->mInfo['descriptionurl']
105 : false;
106 }
107
108 /**
109 * Only useful if we're locally caching thumbs anyway...
110 */
111 function getThumbPath( $suffix = '' ) {
112 $ret = null;
113 if ( $this->repo->apiThumbCacheExpiry > 0 && $this->repo->apiThumbCacheDir ) {
114 global $wgUploadDirectory;
115 $path = $wgUploadDirectory . '/' . $this->repo->apiThumbCacheDir . '/' . $this->repo->name . '/';
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 }