Index: trunk/phase3/includes/filerepo/ForeignAPIRepo.php |
— | — | @@ -25,16 +25,20 @@ |
26 | 26 | */ |
27 | 27 | class ForeignAPIRepo extends FileRepo { |
28 | 28 | var $fileFactory = array( 'ForeignAPIFile', 'newFromTitle' ); |
| 29 | + /* Check back with Commons after a day */ |
29 | 30 | var $apiThumbCacheExpiry = 86400; |
| 31 | + /* Redownload thumbnail files after a month */ |
| 32 | + var $fileCacheExpiry = 2629743; |
| 33 | + |
30 | 34 | protected $mQueryCache = array(); |
31 | 35 | protected $mFileExists = array(); |
32 | 36 | |
33 | 37 | function __construct( $info ) { |
34 | 38 | parent::__construct( $info ); |
35 | | - |
36 | | - // http://commons.wikimedia.org/w/api.php |
37 | | - $this->mApiBase = isset( $info['apibase'] ) ? $info['apibase'] : null; |
38 | 39 | |
| 40 | + // http://commons.wikimedia.org/w/api.php |
| 41 | + $this->mApiBase = isset( $info['apibase'] ) ? $info['apibase'] : null; |
| 42 | + |
39 | 43 | if( isset( $info['apiThumbCacheExpiry'] ) ) { |
40 | 44 | $this->apiThumbCacheExpiry = $info['apiThumbCacheExpiry']; |
41 | 45 | } |
— | — | @@ -119,7 +123,7 @@ |
120 | 124 | array( |
121 | 125 | 'format' => 'json', |
122 | 126 | 'action' => 'query', |
123 | | - 'redirects' => 'true' |
| 127 | + 'redirects' => 'true' |
124 | 128 | ) ); |
125 | 129 | if ( $this->mApiBase ) { |
126 | 130 | $url = wfAppendQuery( $this->mApiBase, $query ); |
— | — | @@ -176,10 +180,10 @@ |
177 | 181 | return $ret; |
178 | 182 | } |
179 | 183 | |
180 | | - function getThumbUrl( $name, $width=-1, $height=-1 ) { |
| 184 | + function getThumbUrl( $name, $width=-1, $height=-1, &$result ) { |
181 | 185 | $data = $this->fetchImageQuery( array( |
182 | 186 | 'titles' => 'File:' . $name, |
183 | | - 'iiprop' => 'url', |
| 187 | + 'iiprop' => 'url|timestamp', |
184 | 188 | 'iiurlwidth' => $width, |
185 | 189 | 'iiurlheight' => $height, |
186 | 190 | 'prop' => 'imageinfo' ) ); |
— | — | @@ -187,12 +191,23 @@ |
188 | 192 | |
189 | 193 | if( $data && $info && isset( $info['thumburl'] ) ) { |
190 | 194 | wfDebug( __METHOD__ . " got remote thumb " . $info['thumburl'] . "\n" ); |
| 195 | + $result = $info; |
191 | 196 | return $info['thumburl']; |
192 | 197 | } else { |
193 | 198 | return false; |
194 | 199 | } |
195 | 200 | } |
196 | 201 | |
| 202 | + /* |
| 203 | + * Return the imageurl from cache if possible |
| 204 | + * |
| 205 | + * If the url has been requested today, get it from cache |
| 206 | + * Otherwise retrieve remote thumb url, check for local file. |
| 207 | + * |
| 208 | + * @param $name String is a dbkey form of a title |
| 209 | + * @param $width |
| 210 | + * @param $height |
| 211 | + */ |
197 | 212 | function getThumbUrlFromCache( $name, $width, $height ) { |
198 | 213 | global $wgMemc, $wgUploadPath, $wgServer, $wgUploadDirectory; |
199 | 214 | |
— | — | @@ -201,35 +216,53 @@ |
202 | 217 | } |
203 | 218 | |
204 | 219 | $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $name, $width ); |
| 220 | + |
205 | 221 | $thumbUrl = $wgMemc->get($key); |
206 | 222 | if ( $thumbUrl ) { |
207 | 223 | wfDebug("Got thumb from local cache. $thumbUrl \n"); |
208 | 224 | return $thumbUrl; |
209 | 225 | } |
210 | 226 | else { |
211 | | - $foreignUrl = $this->getThumbUrl( $name, $width, $height ); |
| 227 | + $metadata = null; |
| 228 | + $foreignUrl = $this->getThumbUrl( $name, $width, $height, $metadata ); |
| 229 | + // We need the same filename as the remote one :) |
| 230 | + $fileName = rawurldecode( pathinfo( $foreignUrl, PATHINFO_BASENAME ) ); |
| 231 | + $path = 'thumb/' . $this->getHashPath( $name ) . $name . "/"; |
| 232 | + $localFilename = $wgUploadDirectory . '/' . $path . $fileName; |
| 233 | + $localUrl = $wgServer . $wgUploadPath . '/' . $path . $fileName; |
| 234 | + |
212 | 235 | if( !$foreignUrl ) { |
213 | 236 | wfDebug( __METHOD__ . " Could not find thumburl\n" ); |
214 | 237 | return false; |
215 | 238 | } |
| 239 | + if( file_exists( $localFilename ) && isset( $metadata['timestamp'] ) ) { |
| 240 | + wfDebug( __METHOD__ . " Thumbnail was already downloaded before\n" ); |
| 241 | + $modified = filemtime( $localFilename ); |
| 242 | + $remoteModified = strtotime( $metadata['timestamp'] ); |
| 243 | + $diff = abs( $modified - $remoteModified ); |
| 244 | + if( $remoteModified < $modified && $diff < $this->fileCacheExpiry ) { |
| 245 | + /* Use our current already downloaded thumbnail */ |
| 246 | + $wgMemc->set( $key, $localUrl, $this->apiThumbCacheExpiry ); |
| 247 | + return $localUrl; |
| 248 | + } |
| 249 | + /* There is a new Commons file, or existing thumbnail older than a month */ |
| 250 | + |
| 251 | + } |
216 | 252 | $thumb = Http::get( $foreignUrl ); |
217 | 253 | if( !$thumb ) { |
218 | 254 | wfDebug( __METHOD__ . " Could not download thumb\n" ); |
219 | 255 | return false; |
220 | 256 | } |
221 | | - // We need the same filename as the remote one :) |
222 | | - $fileName = rawurldecode( pathinfo( $foreignUrl, PATHINFO_BASENAME ) ); |
223 | | - $path = 'thumb/' . $this->getHashPath( $name ) . $name . "/"; |
224 | 257 | if ( !is_dir($wgUploadDirectory . '/' . $path) ) { |
225 | 258 | if( !wfMkdirParents($wgUploadDirectory . '/' . $path) ) { |
226 | 259 | wfDebug( __METHOD__ . " could not create directory for thumb\n" ); |
227 | 260 | return $foreignUrl; |
228 | 261 | } |
229 | 262 | } |
230 | | - $localUrl = $wgServer . $wgUploadPath . '/' . $path . $fileName; |
| 263 | + |
231 | 264 | # FIXME: Delete old thumbs that aren't being used. Maintenance script? |
232 | 265 | wfSuppressWarnings(); |
233 | | - if( !file_put_contents($wgUploadDirectory . '/' . $path . $fileName, $thumb ) ) { |
| 266 | + if( !file_put_contents($localFilename, $thumb ) ) { |
234 | 267 | wfRestoreWarnings(); |
235 | 268 | wfDebug( __METHOD__ . " could not write to thumb path\n" ); |
236 | 269 | return $foreignUrl; |