Revert r43788 and r43788 (adding findBySha1 functionality). Something is breaking...
[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 if ( $this->repo->canCacheThumbs() ) {
106 global $wgUploadDirectory;
107 $path = $wgUploadDirectory . '/thumb/' . $this->getHashPath( $this->getName() );
108 if ( $suffix ) {
109 $path = $path . $suffix . '/';
110 }
111 return $path;
112 }
113 else {
114 return null;
115 }
116 }
117
118 function getThumbnails() {
119 $files = array();
120 $dir = $this->getThumbPath( $this->getName() );
121 if ( is_dir( $dir ) ) {
122 $handle = opendir( $dir );
123 if ( $handle ) {
124 while ( false !== ( $file = readdir($handle) ) ) {
125 if ( $file{0} != '.' ) {
126 $files[] = $file;
127 }
128 }
129 closedir( $handle );
130 }
131 }
132 return $files;
133 }
134
135 function purgeCache() {
136 $this->purgeThumbnails();
137 $this->purgeDescriptionPage();
138 }
139
140 function purgeDescriptionPage() {
141 global $wgMemc;
142 $url = $this->repo->getDescriptionRenderUrl( $this->getName() );
143 $key = wfMemcKey( 'RemoteFileDescription', 'url', md5($url) );
144 $wgMemc->delete( $key );
145 }
146
147 function purgeThumbnails() {
148 global $wgMemc;
149 $key = wfMemcKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
150 $wgMemc->delete( $key );
151 $files = $this->getThumbnails();
152 $dir = $this->getThumbPath( $this->getName() );
153 foreach ( $files as $file ) {
154 unlink( $dir . $file );
155 }
156 if ( is_dir( $dir ) ) {
157 rmdir( $dir ); // Might have already gone away, spews errors if we don't.
158 }
159 }
160 }