summaryrefslogtreecommitdiff
path: root/includes/filerepo/ForeignAPIFile.php
blob: d9fb85d069ea1e5e6afea0a62b6529afefc598e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php

/** 
 * Very hacky and inefficient
 * do not use :D
 *
 * @ingroup FileRepo
 */
class ForeignAPIFile extends File {
	
	private $mExists;
	
	function __construct( $title, $repo, $info, $exists = false ) {
		parent::__construct( $title, $repo );
		$this->mInfo = $info;
		$this->mExists = $exists;
	}
	
	static function newFromTitle( $title, $repo ) {
		$info = $repo->getImageInfo( $title );
		if( $info ) {
			return new ForeignAPIFile( $title, $repo, $info, true );
		} else {
			return null;
		}
	}
	
	// Dummy functions...
	public function exists() {
		return $this->mExists;
	}
	
	public function getPath() {
		return false;
	}

	function transform( $params, $flags = 0 ) {
		if( !$this->canRender() ) {
			// show icon
			return parent::transform( $params, $flags );
		}
		$thumbUrl = $this->repo->getThumbUrlFromCache(
				$this->getName(),
				isset( $params['width'] ) ? $params['width'] : -1,
				isset( $params['height'] ) ? $params['height'] : -1 );
		if( $thumbUrl ) {
			return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );;
		}
		return false;
	}

	// Info we can get from API...
	public function getWidth( $page = 1 ) {
		return intval( @$this->mInfo['width'] );
	}
	
	public function getHeight( $page = 1 ) {
		return intval( @$this->mInfo['height'] );
	}
	
	public function getMetadata() {
		return serialize( (array)@$this->mInfo['metadata'] );
	}
	
	public function getSize() {
		return intval( @$this->mInfo['size'] );
	}
	
	public function getUrl() {
		return strval( @$this->mInfo['url'] );
	}

	public function getUser( $method='text' ) {
		return strval( @$this->mInfo['user'] );
	}
	
	public function getDescription() {
		return strval( @$this->mInfo['comment'] );
	}

	function getSha1() {
		return wfBaseConvert( strval( @$this->mInfo['sha1'] ), 16, 36, 31 );
	}
	
	function getTimestamp() {
		return wfTimestamp( TS_MW, strval( @$this->mInfo['timestamp'] ) );
	}
	
	function getMimeType() {
		if( empty( $info['mime'] ) ) {
			$magic = MimeMagic::singleton();
			$info['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
		}
		return $info['mime'];
	}
	
	/// @fixme May guess wrong on file types that can be eg audio or video
	function getMediaType() {
		$magic = MimeMagic::singleton();
		return $magic->getMediaType( null, $this->getMimeType() );
	}
	
	function getDescriptionUrl() {
		return isset( $this->mInfo['descriptionurl'] )
			? $this->mInfo['descriptionurl']
			: false;
	}
	
	/**
	 * Only useful if we're locally caching thumbs anyway...
	 */
	function getThumbPath( $suffix = '' ) {
		if ( $this->repo->canCacheThumbs() ) {
			global $wgUploadDirectory;
			$path = $wgUploadDirectory . '/thumb/' . $this->getHashPath( $this->getName() );
			if ( $suffix ) {
				$path = $path . $suffix . '/';
			}
			return $path;
		}
		else {
			return null;	
		}
	}
	
	function getThumbnails() {
		$files = array();
		$dir = $this->getThumbPath( $this->getName() );
		if ( is_dir( $dir ) ) {
			$handle = opendir( $dir );
			if ( $handle ) {
				while ( false !== ( $file = readdir($handle) ) ) {
					if ( $file{0} != '.'  ) {
						$files[] = $file;
					}
				}
				closedir( $handle );
			}
		}
		return $files;
	}
	
	function purgeCache() {
		$this->purgeThumbnails();
		$this->purgeDescriptionPage();
	}
	
	function purgeDescriptionPage() {
		global $wgMemc;
		$url = $this->repo->getDescriptionRenderUrl( $this->getName() );
		$key = wfMemcKey( 'RemoteFileDescription', 'url', md5($url) );
		$wgMemc->delete( $key );
	}
	
	function purgeThumbnails() {
		global $wgMemc;
		$key = wfMemcKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
		$wgMemc->delete( $key );
		$files = $this->getThumbnails();
		$dir = $this->getThumbPath( $this->getName() );
		foreach ( $files as $file ) {
			unlink( $dir . $file );
		}
		if ( is_dir( $dir ) ) {
			rmdir( $dir ); // Might have already gone away, spews errors if we don't.
		}
	}
}