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