* @ingroup FileRepo
*/
class ForeignAPIRepo extends FileRepo {
+ protected $mQueryCache = array();
+
function __construct( $info ) {
parent::__construct( $info );
$this->mApiBase = $info['apibase']; // http://commons.wikimedia.org/w/api.php
}
protected function queryImage( $query ) {
+ $data = $this->fetchImageQuery( $query );
+
+ if( isset( $data['query']['pages'] ) ) {
+ foreach( $data['query']['pages'] as $pageid => $info ) {
+ if( isset( $info['imageinfo'][0] ) ) {
+ return $info['imageinfo'][0];
+ }
+ }
+ }
+ return false;
+ }
+
+ protected function fetchImageQuery( $query ) {
+ global $wgMemc;
+
$url = $this->mApiBase .
'?' .
wfArrayToCgi(
'format' => 'json',
'action' => 'query',
'prop' => 'imageinfo' ) ) );
- $json = Http::get( $url );
- $data = json_decode( $json, true );
- if( isset( $data['query']['pages'] ) ) {
- foreach( $data['query']['pages'] as $pageid => $info ) {
- if( isset( $info['imageinfo'][0] ) ) {
- return $info['imageinfo'][0];
- }
+ if( !isset( $this->mQueryCache[$url] ) ) {
+ $key = wfMemcKey( 'ForeignAPIRepo', $url );
+ $data = $wgMemc->get( $key );
+ if( !$data ) {
+ $data = Http::get( $url );
+ $wgMemc->set( $key, $data, 3600 );
}
+
+ if( count( $this->mQueryCache ) > 100 ) {
+ // Keep the cache from growing infinitely
+ $this->mQueryCache = array();
+ }
+ $this->mQueryCache[$url] = $data;
}
- return false;
+ return json_decode( $this->mQueryCache[$url], true );
}
function findFile( $title, $time = false ) {
$info = $this->queryImage( array(
'titles' => 'Image:' . $title->getText(),
- 'prop' => 'imageinfo',
'iiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mimetype' ) );
if( $info ) {
return new ForeignAPIFile( $title, $this, $info );