3 * Foreign repository accessible through api.php requests.
10 * A foreign repository with a remote MediaWiki with an API thingy
11 * Very hacky and inefficient
12 * do not use except for testing :D
16 * $wgForeignFileRepos[] = array(
17 * 'class' => 'ForeignAPIRepo',
19 * 'apibase' => 'http://en.wikipedia.org/w/api.php',
20 * 'fetchDescription' => true, // Optional
21 * 'descriptionCacheExpiry' => 3600,
26 class ForeignAPIRepo
extends FileRepo
{
27 var $fileFactory = array( 'ForeignAPIFile', 'newFromTitle' );
28 var $apiThumbCacheExpiry = 86400;
29 protected $mQueryCache = array();
30 protected $mFileExists = array();
32 function __construct( $info ) {
33 parent
::__construct( $info );
35 // http://commons.wikimedia.org/w/api.php
36 $this->mApiBase
= isset( $info['apibase'] ) ?
$info['apibase'] : null;
38 if( isset( $info['apiThumbCacheExpiry'] ) ) {
39 $this->apiThumbCacheExpiry
= $info['apiThumbCacheExpiry'];
41 if( !$this->scriptDirUrl
) {
42 // hack for description fetches
43 $this->scriptDirUrl
= dirname( $this->mApiBase
);
45 // If we can cache thumbs we can guess sane defaults for these
46 if( $this->canCacheThumbs() && !$this->url
) {
47 global $wgLocalFileRepo;
48 $this->url
= $wgLocalFileRepo['url'];
50 if( $this->canCacheThumbs() && !$this->thumbUrl
) {
51 $this->thumbUrl
= $this->url
. '/thumb';
56 * Per docs in FileRepo, this needs to return false if we don't support versioned
57 * files. Well, we don't.
59 function newFile( $title, $time = false ) {
63 return parent
::newFile( $title, $time );
69 function storeBatch( $triplets, $flags = 0 ) {
72 function storeTemp( $originalName, $srcPath ) {
75 function append( $srcPath, $toAppendPath, $flags = 0 ){
78 function publishBatch( $triplets, $flags = 0 ) {
81 function deleteBatch( $sourceDestPairs ) {
86 function fileExistsBatch( $files, $flags = 0 ) {
88 foreach ( $files as $k => $f ) {
89 if ( isset( $this->mFileExists
[$k] ) ) {
92 } elseif( self
::isVirtualUrl( $f ) ) {
93 # TODO! FIXME! We need to be able to handle virtual
94 # URLs better, at least when we know they refer to the
101 $results = $this->fetchImageQuery( array( 'titles' => implode( $files, '|' ),
102 'prop' => 'imageinfo' ) );
103 if( isset( $data['query']['pages'] ) ) {
105 foreach( $files as $key => $file ) {
106 $results[$key] = $this->mFileExists
[$key] = !isset( $data['query']['pages'][$i]['missing'] );
111 function getFileProps( $virtualUrl ) {
115 function fetchImageQuery( $query ) {
118 $query = array_merge( $query,
122 'redirects' => 'true'
124 if ( $this->mApiBase
) {
125 $url = wfAppendQuery( $this->mApiBase
, $query );
127 $url = $this->makeUrl( $query, 'api' );
130 if( !isset( $this->mQueryCache
[$url] ) ) {
131 $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'Metadata', md5( $url ) );
132 $data = $wgMemc->get( $key );
134 $data = Http
::get( $url );
138 $wgMemc->set( $key, $data, 3600 );
141 if( count( $this->mQueryCache
) > 100 ) {
142 // Keep the cache from growing infinitely
143 $this->mQueryCache
= array();
145 $this->mQueryCache
[$url] = $data;
147 return FormatJson
::decode( $this->mQueryCache
[$url], true );
150 function getImageInfo( $data ) {
151 if( $data && isset( $data['query']['pages'] ) ) {
152 foreach( $data['query']['pages'] as $pageid => $info ) {
153 if( isset( $info['imageinfo'][0] ) ) {
154 return $info['imageinfo'][0];
161 function findBySha1( $hash ) {
162 $results = $this->fetchImageQuery( array(
163 'aisha1base36' => $hash,
164 'aiprop' => ForeignAPIFile
::getProps(),
165 'list' => 'allimages', ) );
167 if ( isset( $results['query']['allimages'] ) ) {
168 foreach ( $results['query']['allimages'] as $img ) {
169 // 1.14 was broken, doesn't return name attribute
170 if( !isset( $img['name'] ) ) {
173 $ret[] = new ForeignAPIFile( Title
::makeTitle( NS_FILE
, $img['name'] ), $this, $img );
179 function getThumbUrl( $name, $width=-1, $height=-1 ) {
180 $data = $this->fetchImageQuery( array(
181 'titles' => 'File:' . $name,
183 'iiurlwidth' => $width,
184 'iiurlheight' => $height,
185 'prop' => 'imageinfo' ) );
186 $info = $this->getImageInfo( $data );
188 if( $data && $info && $info['thumburl'] ) {
189 wfDebug( __METHOD__
. " got remote thumb " . $info['thumburl'] . "\n" );
190 return $info['thumburl'];
196 function getThumbUrlFromCache( $name, $width, $height ) {
197 global $wgMemc, $wgUploadPath, $wgServer, $wgUploadDirectory;
199 if ( !$this->canCacheThumbs() ) {
200 return $this->getThumbUrl( $name, $width, $height );
203 $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $name );
204 if ( $thumbUrl = $wgMemc->get($key) ) {
205 wfDebug("Got thumb from local cache. $thumbUrl \n");
209 $foreignUrl = $this->getThumbUrl( $name, $width, $height );
211 wfDebug( __METHOD__
. " Could not find thumburl\n" );
214 $thumb = Http
::get( $foreignUrl );
216 wfDebug( __METHOD__
. " Could not download thumb\n" );
219 // We need the same filename as the remote one :)
220 $fileName = rawurldecode( pathinfo( $foreignUrl, PATHINFO_BASENAME
) );
221 $path = 'thumb/' . $this->getHashPath( $name ) . $name . "/";
222 if ( !is_dir($wgUploadDirectory . '/' . $path) ) {
223 if( !wfMkdirParents($wgUploadDirectory . '/' . $path) ) {
224 wfDebug( __METHOD__
. " could not create directory for thumb\n" );
228 $localUrl = $wgServer . $wgUploadPath . '/' . $path . $fileName;
229 # FIXME: Delete old thumbs that aren't being used. Maintenance script?
230 wfSuppressWarnings();
231 if( !file_put_contents($wgUploadDirectory . '/' . $path . $fileName, $thumb ) ) {
233 wfDebug( __METHOD__
. " could not write to thumb path\n" );
237 $wgMemc->set( $key, $localUrl, $this->apiThumbCacheExpiry
);
238 wfDebug( __METHOD__
. " got local thumb $localUrl, saving to cache \n" );
244 * @see FileRepo::getZoneUrl()
246 function getZoneUrl( $zone ) {
251 return $this->thumbUrl
;
253 return parent
::getZoneUrl( $zone );
258 * Are we locally caching the thumbnails?
261 public function canCacheThumbs() {
262 return ( $this->apiThumbCacheExpiry
> 0 );