Cache image API lookups in-process and, if available, in memcached for up to 1 hour.
[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 * );
16 *
17 * @ingroup FileRepo
18 */
19 class ForeignAPIRepo extends FileRepo {
20 protected $mQueryCache = array();
21
22 function __construct( $info ) {
23 parent::__construct( $info );
24 $this->mApiBase = $info['apibase']; // http://commons.wikimedia.org/w/api.php
25 if( !$this->scriptDirUrl ) {
26 // hack for description fetches
27 $this->scriptDirUrl = dirname( $this->mApiBase );
28 }
29 }
30
31 function storeBatch( $triplets, $flags = 0 ) {
32 return false;
33 }
34
35 function storeTemp( $originalName, $srcPath ) {
36 return false;
37 }
38 function publishBatch( $triplets, $flags = 0 ) {
39 return false;
40 }
41 function deleteBatch( $sourceDestPairs ) {
42 return false;
43 }
44 function getFileProps( $virtualUrl ) {
45 return false;
46 }
47 function newFile( $title, $time = false ) {
48 return false;
49 }
50
51 protected function queryImage( $query ) {
52 $data = $this->fetchImageQuery( $query );
53
54 if( isset( $data['query']['pages'] ) ) {
55 foreach( $data['query']['pages'] as $pageid => $info ) {
56 if( isset( $info['imageinfo'][0] ) ) {
57 return $info['imageinfo'][0];
58 }
59 }
60 }
61 return false;
62 }
63
64 protected function fetchImageQuery( $query ) {
65 global $wgMemc;
66
67 $url = $this->mApiBase .
68 '?' .
69 wfArrayToCgi(
70 array_merge( $query,
71 array(
72 'format' => 'json',
73 'action' => 'query',
74 'prop' => 'imageinfo' ) ) );
75
76 if( !isset( $this->mQueryCache[$url] ) ) {
77 $key = wfMemcKey( 'ForeignAPIRepo', $url );
78 $data = $wgMemc->get( $key );
79 if( !$data ) {
80 $data = Http::get( $url );
81 $wgMemc->set( $key, $data, 3600 );
82 }
83
84 if( count( $this->mQueryCache ) > 100 ) {
85 // Keep the cache from growing infinitely
86 $this->mQueryCache = array();
87 }
88 $this->mQueryCache[$url] = $data;
89 }
90 return json_decode( $this->mQueryCache[$url], true );
91 }
92
93 function findFile( $title, $time = false ) {
94 $info = $this->queryImage( array(
95 'titles' => 'Image:' . $title->getText(),
96 'iiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mimetype' ) );
97 if( $info ) {
98 return new ForeignAPIFile( $title, $this, $info );
99 } else {
100 return false;
101 }
102 }
103
104 function getThumbUrl( $name, $width=-1, $height=-1 ) {
105 $info = $this->queryImage( array(
106 'titles' => 'Image:' . $name,
107 'iiprop' => 'url',
108 'iiurlwidth' => $width,
109 'iiurlheight' => $height ) );
110 if( $info ) {
111 return $info['thumburl'];
112 } else {
113 return false;
114 }
115 }
116 }