Override FileRepo::newFile() when $time is set. See docs on parent.
[lhc/web/wiklou.git] / includes / filerepo / ForeignAPIRepo.php
1 <?php
2
3 /**
4 * A foreign repository with a remote MediaWiki with an API thingy
5 * Very hacky and inefficient
6 * do not use except for testing :D
7 *
8 * Example config:
9 *
10 * $wgForeignFileRepos[] = array(
11 * 'class' => 'ForeignAPIRepo',
12 * 'name' => 'shared',
13 * 'apibase' => 'http://en.wikipedia.org/w/api.php',
14 * 'fetchDescription' => true, // Optional
15 * 'descriptionCacheExpiry' => 3600,
16 * );
17 *
18 * @ingroup FileRepo
19 */
20 class ForeignAPIRepo extends FileRepo {
21 var $fileFactory = array( 'ForeignAPIFile', 'newFromTitle' );
22 var $apiThumbCacheExpiry = 0;
23 protected $mQueryCache = array();
24
25 function __construct( $info ) {
26 parent::__construct( $info );
27 $this->mApiBase = $info['apibase']; // http://commons.wikimedia.org/w/api.php
28 if( !$this->scriptDirUrl ) {
29 // hack for description fetches
30 $this->scriptDirUrl = dirname( $this->mApiBase );
31 }
32 }
33
34 /**
35 * Per docs in FileRepo, this needs to return false if we don't support versioned
36 * files. Well, we don't.
37 */
38 function newFile( $title, $time = false ) {
39 if ( $time ) {
40 return false;
41 }
42 return parent::newFile( $title, $time );
43 }
44
45 /**
46 * No-ops
47 */
48 function storeBatch( $triplets, $flags = 0 ) {
49 return false;
50 }
51 function storeTemp( $originalName, $srcPath ) {
52 return false;
53 }
54 function publishBatch( $triplets, $flags = 0 ) {
55 return false;
56 }
57 function deleteBatch( $sourceDestPairs ) {
58 return false;
59 }
60 function getFileProps( $virtualUrl ) {
61 return false;
62 }
63
64 protected function queryImage( $query ) {
65 $data = $this->fetchImageQuery( $query );
66
67 if( isset( $data['query']['pages'] ) ) {
68 foreach( $data['query']['pages'] as $pageid => $info ) {
69 if( isset( $info['imageinfo'][0] ) ) {
70 return $info['imageinfo'][0];
71 }
72 }
73 }
74 return false;
75 }
76
77 protected function fetchImageQuery( $query ) {
78 global $wgMemc;
79
80 $url = $this->mApiBase .
81 '?' .
82 wfArrayToCgi(
83 array_merge( $query,
84 array(
85 'format' => 'json',
86 'action' => 'query' ) ) );
87
88 if( !isset( $this->mQueryCache[$url] ) ) {
89 $key = wfMemcKey( 'ForeignAPIRepo', 'Metadata', md5( $url ) );
90 $data = $wgMemc->get( $key );
91 if( !$data ) {
92 $data = Http::get( $url );
93 if ( !$data ) {
94 return null;
95 }
96 $wgMemc->set( $key, $data, 3600 );
97 }
98
99 if( count( $this->mQueryCache ) > 100 ) {
100 // Keep the cache from growing infinitely
101 $this->mQueryCache = array();
102 }
103 $this->mQueryCache[$url] = $data;
104 }
105 return json_decode( $this->mQueryCache[$url], true );
106 }
107
108 function getImageInfo( $title, $time = false ) {
109 return $this->queryImage( array(
110 'titles' => 'Image:' . $title->getText(),
111 'iiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mime',
112 'prop' => 'imageinfo' ) );
113 }
114
115 function findBySha1( $hash ) {
116 $results = $this->fetchImageQuery( array(
117 'aisha1base36' => $hash,
118 'aiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mime',
119 'list' => 'allimages', ) );
120 $ret = array();
121 if ( isset( $results['query']['allimages'] ) ) {
122 foreach ( $results['query']['allimages'] as $img ) {
123 $ret[] = new ForeignAPIFile( Title::makeTitle( NS_IMAGE, $img['name'] ), $this, $img );
124 }
125 }
126 return $ret;
127 }
128
129 function getThumbUrl( $name, $width=-1, $height=-1 ) {
130 $info = $this->queryImage( array(
131 'titles' => 'Image:' . $name,
132 'iiprop' => 'url',
133 'iiurlwidth' => $width,
134 'iiurlheight' => $height,
135 'prop' => 'imageinfo' ) );
136 if( $info ) {
137 wfDebug( __METHOD__ . " got remote thumb " . $info['thumburl'] . "\n" );
138 return $info['thumburl'];
139 } else {
140 return false;
141 }
142 }
143
144 function getThumbUrlFromCache( $name, $width, $height ) {
145 global $wgMemc, $wgUploadPath, $wgServer, $wgUploadDirectory;
146
147 if ( !$this->canCacheThumbs() ) {
148 return $this->getThumbUrl( $name, $width, $height );
149 }
150
151 $key = wfMemcKey( 'ForeignAPIRepo', 'ThumbUrl', $name );
152 if ( $thumbUrl = $wgMemc->get($key) ) {
153 wfDebug("Got thumb from local cache. $thumbUrl \n");
154 return $thumbUrl;
155 }
156 else {
157 $foreignUrl = $this->getThumbUrl( $name, $width, $height );
158
159 // We need the same filename as the remote one :)
160 $fileName = ltrim( substr( $foreignUrl, strrpos( $foreignUrl, '/' ) ), '/' );
161 $path = 'thumb/' . $this->getHashPath( $name ) . $name . "/";
162 if ( !is_dir($wgUploadDirectory . '/' . $path) ) {
163 wfMkdirParents($wgUploadDirectory . '/' . $path);
164 }
165 if ( !is_writable( $wgUploadDirectory . '/' . $path . $fileName ) ) {
166 wfDebug( __METHOD__ . " could not write to thumb path\n" );
167 return $foreignUrl;
168 }
169 $localUrl = $wgServer . $wgUploadPath . '/' . $path . $fileName;
170 $thumb = Http::get( $foreignUrl );
171 # FIXME: Delete old thumbs that aren't being used. Maintenance script?
172 file_put_contents($wgUploadDirectory . '/' . $path . $fileName, $thumb );
173 $wgMemc->set( $key, $localUrl, $this->apiThumbCacheExpiry );
174 wfDebug( __METHOD__ . " got local thumb $localUrl, saving to cache \n" );
175 return $localUrl;
176 }
177 }
178
179 /**
180 * Are we locally caching the thumbnails?
181 * @return bool
182 */
183 public function canCacheThumbs() {
184 return ( $this->apiThumbCacheExpiry > 0 );
185 }
186 }