d0add602b6a08af4b7d02f53d80eece3637fd107
[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 global $wgMemc;
35 $thumbUrl = $this->repo->getThumbUrl(
36 $this->getName(),
37 isset( $params['width'] ) ? $params['width'] : -1,
38 isset( $params['height'] ) ? $params['height'] : -1 );
39 if( $thumbUrl ) {
40 if ( $this->repo->useLocalCache ) {
41 wfDebug("Attempting to get the thumb from the cache...");
42 $key = md5($thumbUrl);
43 $obj = $wgMemc->get($key);
44 if ($obj) {
45 wfDebug("success!\n");
46 return $obj;
47 }
48 wfDebug("miss\n");
49 }
50 $res = $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );;
51 if ( $res && $this->repo->useLocalCache ) $wgMemc->set( $key, $res, $this->repo->localCacheExpiry );
52 wfDebug( __METHOD__ . " got remote thumb $thumbUrl\n" );
53 return $res;
54 }
55 return false;
56 }
57
58 // Info we can get from API...
59 public function getWidth( $page = 1 ) {
60 return intval( @$this->mInfo['width'] );
61 }
62
63 public function getHeight( $page = 1 ) {
64 return intval( @$this->mInfo['height'] );
65 }
66
67 public function getMetadata() {
68 return serialize( (array)@$this->mInfo['metadata'] );
69 }
70
71 public function getSize() {
72 return intval( @$this->mInfo['size'] );
73 }
74
75 public function getUrl() {
76 return strval( @$this->mInfo['url'] );
77 }
78
79 public function getUser( $method='text' ) {
80 return strval( @$this->mInfo['user'] );
81 }
82
83 public function getDescription() {
84 return strval( @$this->mInfo['comment'] );
85 }
86
87 function getSha1() {
88 return wfBaseConvert( strval( @$this->mInfo['sha1'] ), 16, 36, 31 );
89 }
90
91 function getTimestamp() {
92 return wfTimestamp( TS_MW, strval( @$this->mInfo['timestamp'] ) );
93 }
94
95 function getMimeType() {
96 if( empty( $info['mime'] ) ) {
97 $magic = MimeMagic::singleton();
98 $info['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
99 }
100 return $info['mime'];
101 }
102
103 /// @fixme May guess wrong on file types that can be eg audio or video
104 function getMediaType() {
105 $magic = MimeMagic::singleton();
106 return $magic->getMediaType( null, $this->getMimeType() );
107 }
108
109 function getDescriptionUrl() {
110 return isset( $this->mInfo['descriptionurl'] )
111 ? $this->mInfo['descriptionurl']
112 : false;
113 }
114 }