md5() the URL in cache keys so they're not excessively long. Thanks to Nikerabbit...
[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 function storeBatch( $triplets, $flags = 0 ) {
35 return false;
36 }
37
38 function storeTemp( $originalName, $srcPath ) {
39 return false;
40 }
41 function publishBatch( $triplets, $flags = 0 ) {
42 return false;
43 }
44 function deleteBatch( $sourceDestPairs ) {
45 return false;
46 }
47 function getFileProps( $virtualUrl ) {
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', 'Metadata', md5( $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 getImageInfo( $title, $time = false ) {
94 return $this->queryImage( array(
95 'titles' => 'Image:' . $title->getText(),
96 'iiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mime' ) );
97 }
98
99 function getThumbUrl( $name, $width=-1, $height=-1 ) {
100 $info = $this->queryImage( array(
101 'titles' => 'Image:' . $name,
102 'iiprop' => 'url',
103 'iiurlwidth' => $width,
104 'iiurlheight' => $height ) );
105 if( $info ) {
106 wfDebug( __METHOD__ . " got remote thumb " . $info['thumburl'] . "\n" );
107 return $info['thumburl'];
108 } else {
109 return false;
110 }
111 }
112
113 function getThumbUrlFromCache( $name, $width, $height ) {
114 global $wgMemc, $wgUploadPath, $wgServer, $wgUploadDirectory;
115 ;
116
117 $key = wfMemcKey( 'ForeignAPIRepo', 'ThumbUrl', $name );
118 if ( $thumbUrl = $wgMemc->get($key) ) {
119 wfDebug("Got thumb from local cache. $thumbUrl \n");
120 return $thumbUrl;
121 }
122 else {
123 $foreignUrl = $this->getThumbUrl( $name, $width, $height );
124 $path = $this->apiThumbCacheDir . '/' . $this->name . '/' .
125 $name . '/';
126 if ( !is_dir($wgUploadDirectory . '/' . $path) ) {
127 wfMkdirParents($wgUploadDirectory . '/' . $path);
128 }
129 $localUrl = $wgServer . $wgUploadPath . '/' . $path . $width . 'px-' . $name;
130 $thumb = Http::get( $foreignUrl );
131 # FIXME: Delete old thumbs that aren't being used. Maintenance script?
132 file_put_contents($wgUploadDirectory . '/' . $path . $width . 'px-' . $name, $thumb );
133 $wgMemc->set( $key, $localUrl, $this->apiThumbCacheExpiry );
134 wfDebug( __METHOD__ . " got local thumb $localUrl, saving to cache \n" );
135 return $localUrl;
136 }
137 }
138 }