No jquery.ui for now.
[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 = 86400;
23 protected $mQueryCache = array();
24 protected $mFileExists = array();
25
26 function __construct( $info ) {
27 parent::__construct( $info );
28 $this->mApiBase = $info['apibase']; // http://commons.wikimedia.org/w/api.php
29 if( isset( $info['apiThumbCacheExpiry'] ) ) {
30 $this->apiThumbCacheExpiry = $info['apiThumbCacheExpiry'];
31 }
32 if( !$this->scriptDirUrl ) {
33 // hack for description fetches
34 $this->scriptDirUrl = dirname( $this->mApiBase );
35 }
36 // If we can cache thumbs we can guess sane defaults for these
37 if( $this->canCacheThumbs() && !$this->url ) {
38 global $wgLocalFileRepo;
39 $this->url = $wgLocalFileRepo['url'];
40 }
41 if( $this->canCacheThumbs() && !$this->thumbUrl ) {
42 $this->thumbUrl = $this->url . '/thumb';
43 }
44 }
45
46 /**
47 * Per docs in FileRepo, this needs to return false if we don't support versioned
48 * files. Well, we don't.
49 */
50 function newFile( $title, $time = false ) {
51 if ( $time ) {
52 return false;
53 }
54 return parent::newFile( $title, $time );
55 }
56
57 /**
58 * No-ops
59 */
60 function storeBatch( $triplets, $flags = 0 ) {
61 return false;
62 }
63 function storeTemp( $originalName, $srcPath ) {
64 return false;
65 }
66 function publishBatch( $triplets, $flags = 0 ) {
67 return false;
68 }
69 function deleteBatch( $sourceDestPairs ) {
70 return false;
71 }
72
73
74 function fileExistsBatch( $files, $flags = 0 ) {
75 $results = array();
76 foreach ( $files as $k => $f ) {
77 if ( isset( $this->mFileExists[$k] ) ) {
78 $results[$k] = true;
79 unset( $files[$k] );
80 } elseif( self::isVirtualUrl( $f ) ) {
81 # TODO! FIXME! We need to be able to handle virtual
82 # URLs better, at least when we know they refer to the
83 # same repo.
84 $results[$k] = false;
85 unset( $files[$k] );
86 }
87 }
88
89 $results = $this->fetchImageQuery( array( 'titles' => implode( $files, '|' ),
90 'prop' => 'imageinfo' ) );
91 if( isset( $data['query']['pages'] ) ) {
92 $i = 0;
93 foreach( $files as $key => $file ) {
94 $results[$key] = $this->mFileExists[$key] = !isset( $data['query']['pages'][$i]['missing'] );
95 $i++;
96 }
97 }
98 }
99 function getFileProps( $virtualUrl ) {
100 return false;
101 }
102
103 protected function queryImage( $query ) {
104 $data = $this->fetchImageQuery( $query );
105
106 if( isset( $data['query']['pages'] ) ) {
107 foreach( $data['query']['pages'] as $pageid => $info ) {
108 if( isset( $info['imageinfo'][0] ) ) {
109 return $info['imageinfo'][0];
110 }
111 }
112 }
113 return false;
114 }
115
116 protected function fetchImageQuery( $query ) {
117 global $wgMemc;
118
119 $url = $this->mApiBase .
120 '?' .
121 wfArrayToCgi(
122 array_merge( $query,
123 array(
124 'format' => 'json',
125 'action' => 'query' ) ) );
126
127 if( !isset( $this->mQueryCache[$url] ) ) {
128 $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'Metadata', md5( $url ) );
129 $data = $wgMemc->get( $key );
130 if( !$data ) {
131 $data = Http::get( $url );
132 if ( !$data ) {
133 return null;
134 }
135 $wgMemc->set( $key, $data, 3600 );
136 }
137
138 if( count( $this->mQueryCache ) > 100 ) {
139 // Keep the cache from growing infinitely
140 $this->mQueryCache = array();
141 }
142 $this->mQueryCache[$url] = $data;
143 }
144 return FormatJson::decode( $this->mQueryCache[$url], true );
145 }
146
147 function getImageInfo( $title, $time = false ) {
148 return $this->queryImage( array(
149 'titles' => 'Image:' . $title->getText(),
150 'iiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mime',
151 'prop' => 'imageinfo' ) );
152 }
153
154 function findBySha1( $hash ) {
155 $results = $this->fetchImageQuery( array(
156 'aisha1base36' => $hash,
157 'aiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mime',
158 'list' => 'allimages', ) );
159 $ret = array();
160 if ( isset( $results['query']['allimages'] ) ) {
161 foreach ( $results['query']['allimages'] as $img ) {
162 $ret[] = new ForeignAPIFile( Title::makeTitle( NS_FILE, $img['name'] ), $this, $img );
163 }
164 }
165 return $ret;
166 }
167
168 function getThumbUrl( $name, $width=-1, $height=-1 ) {
169 $info = $this->queryImage( array(
170 'titles' => 'Image:' . $name,
171 'iiprop' => 'url',
172 'iiurlwidth' => $width,
173 'iiurlheight' => $height,
174 'prop' => 'imageinfo' ) );
175 if( $info ) {
176 wfDebug( __METHOD__ . " got remote thumb " . $info['thumburl'] . "\n" );
177 return $info['thumburl'];
178 } else {
179 return false;
180 }
181 }
182
183 function getThumbUrlFromCache( $name, $width, $height ) {
184 global $wgMemc, $wgUploadPath, $wgServer, $wgUploadDirectory;
185
186 if ( !$this->canCacheThumbs() ) {
187 return $this->getThumbUrl( $name, $width, $height );
188 }
189
190 $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $name );
191 if ( $thumbUrl = $wgMemc->get($key) ) {
192 wfDebug("Got thumb from local cache. $thumbUrl \n");
193 return $thumbUrl;
194 }
195 else {
196 $foreignUrl = $this->getThumbUrl( $name, $width, $height );
197
198 // We need the same filename as the remote one :)
199 $fileName = rawurldecode( pathinfo( $foreignUrl, PATHINFO_BASENAME ) );
200 $path = 'thumb/' . $this->getHashPath( $name ) . $name . "/";
201 if ( !is_dir($wgUploadDirectory . '/' . $path) ) {
202 wfMkdirParents($wgUploadDirectory . '/' . $path);
203 }
204 $localUrl = $wgServer . $wgUploadPath . '/' . $path . $fileName;
205 $thumb = Http::get( $foreignUrl );
206 # FIXME: Delete old thumbs that aren't being used. Maintenance script?
207 if( !file_put_contents($wgUploadDirectory . '/' . $path . $fileName, $thumb ) ) {
208 wfDebug( __METHOD__ . " could not write to thumb path\n" );
209 return $foreignUrl;
210 }
211 $wgMemc->set( $key, $localUrl, $this->apiThumbCacheExpiry );
212 wfDebug( __METHOD__ . " got local thumb $localUrl, saving to cache \n" );
213 return $localUrl;
214 }
215 }
216
217 /**
218 * @see FileRepo::getZoneUrl()
219 */
220 function getZoneUrl( $zone ) {
221 switch ( $zone ) {
222 case 'public':
223 return $this->url;
224 case 'thumb':
225 return $this->thumbUrl;
226 default:
227 return parent::getZoneUrl( $zone );
228 }
229 }
230
231 /**
232 * Are we locally caching the thumbnails?
233 * @return bool
234 */
235 public function canCacheThumbs() {
236 return ( $this->apiThumbCacheExpiry > 0 );
237 }
238 }