Revert r43785 (switching hardcoded Image: to loading the canonical name). This causes...
[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 * No-ops
36 */
37 function storeBatch( $triplets, $flags = 0 ) {
38 return false;
39 }
40 function storeTemp( $originalName, $srcPath ) {
41 return false;
42 }
43 function publishBatch( $triplets, $flags = 0 ) {
44 return false;
45 }
46 function deleteBatch( $sourceDestPairs ) {
47 return false;
48 }
49 function getFileProps( $virtualUrl ) {
50 return false;
51 }
52
53 protected function queryImage( $query ) {
54 $data = $this->fetchImageQuery( $query );
55
56 if( isset( $data['query']['pages'] ) ) {
57 foreach( $data['query']['pages'] as $pageid => $info ) {
58 if( isset( $info['imageinfo'][0] ) ) {
59 return $info['imageinfo'][0];
60 }
61 }
62 }
63 return false;
64 }
65
66 protected function fetchImageQuery( $query ) {
67 global $wgMemc;
68
69 $url = $this->mApiBase .
70 '?' .
71 wfArrayToCgi(
72 array_merge( $query,
73 array(
74 'format' => 'json',
75 'action' => 'query' ) ) );
76
77 if( !isset( $this->mQueryCache[$url] ) ) {
78 $key = wfMemcKey( 'ForeignAPIRepo', 'Metadata', md5( $url ) );
79 $data = $wgMemc->get( $key );
80 if( !$data ) {
81 $data = Http::get( $url );
82 $wgMemc->set( $key, $data, 3600 );
83 }
84
85 if( count( $this->mQueryCache ) > 100 ) {
86 // Keep the cache from growing infinitely
87 $this->mQueryCache = array();
88 }
89 $this->mQueryCache[$url] = $data;
90 }
91 return json_decode( $this->mQueryCache[$url], true );
92 }
93
94 function getImageInfo( $title, $time = false ) {
95 return $this->queryImage( array(
96 'titles' => 'Image:' . $title->getText(),
97 'iiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mime',
98 'prop' => 'imageinfo' ) );
99 }
100
101 function findBySha1( $hash ) {
102 $results = $this->fetchImageQuery( array(
103 'aisha1base36' => $hash,
104 'aiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mime',
105 'list' => 'allimages', ) );
106 $ret = array();
107 foreach ( $results['query']['allimages'] as $img ) {
108 $ret[] = new ForeignAPIFile( Title::makeTitle( NS_IMAGE, $img['name'] ), $this, $img );
109 }
110 return $ret;
111 }
112
113
114
115 function getThumbUrl( $name, $width=-1, $height=-1 ) {
116 $info = $this->queryImage( array(
117 'titles' => 'Image:' . $name,
118 'iiprop' => 'url',
119 'iiurlwidth' => $width,
120 'iiurlheight' => $height ) );
121 if( $info ) {
122 wfDebug( __METHOD__ . " got remote thumb " . $info['thumburl'] . "\n" );
123 return $info['thumburl'];
124 } else {
125 return false;
126 }
127 }
128
129 function getThumbUrlFromCache( $name, $width, $height ) {
130 global $wgMemc, $wgUploadPath, $wgServer, $wgUploadDirectory;
131
132 if ( !$this->canCacheThumbs() ) {
133 return $this->getThumbUrl( $name, $width, $height );
134 }
135
136 $key = wfMemcKey( 'ForeignAPIRepo', 'ThumbUrl', $name );
137 if ( $thumbUrl = $wgMemc->get($key) ) {
138 wfDebug("Got thumb from local cache. $thumbUrl \n");
139 return $thumbUrl;
140 }
141 else {
142 $foreignUrl = $this->getThumbUrl( $name, $width, $height );
143
144 // We need the same filename as the remote one :)
145 $fileName = ltrim( substr( $foreignUrl, strrpos( $foreignUrl, '/' ) ), '/' );
146 $path = 'thumb/' . $this->getHashPath( $name ) . $name . "/";
147 if ( !is_dir($wgUploadDirectory . '/' . $path) ) {
148 wfMkdirParents($wgUploadDirectory . '/' . $path);
149 }
150
151 $localUrl = $wgServer . $wgUploadPath . '/' . $path . $fileName;
152 $thumb = Http::get( $foreignUrl );
153 # FIXME: Delete old thumbs that aren't being used. Maintenance script?
154 file_put_contents($wgUploadDirectory . '/' . $path . $fileName, $thumb );
155 $wgMemc->set( $key, $localUrl, $this->apiThumbCacheExpiry );
156 wfDebug( __METHOD__ . " got local thumb $localUrl, saving to cache \n" );
157 return $localUrl;
158 }
159 }
160
161 /**
162 * Are we locally caching the thumbnails?
163 * @return bool
164 */
165 public function canCacheThumbs() {
166 return ( $this->apiThumbCacheExpiry > 0 );
167 }
168 }