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
10 * $wgForeignFileRepos[] = array(
11 * 'class' => 'ForeignAPIRepo',
13 * 'apibase' => 'http://en.wikipedia.org/w/api.php',
14 * 'fetchDescription' => true, // Optional
15 * 'descriptionCacheExpiry' => 3600,
20 class ForeignAPIRepo
extends FileRepo
{
21 var $fileFactory = array( 'ForeignAPIFile', 'newFromTitle' );
22 var $apiThumbCacheExpiry = 86400;
23 protected $mQueryCache = array();
24 protected $mFileExists = array();
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'];
32 if( !$this->scriptDirUrl
) {
33 // hack for description fetches
34 $this->scriptDirUrl
= dirname( $this->mApiBase
);
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'];
41 if( $this->canCacheThumbs() && !$this->thumbUrl
) {
42 $this->thumbUrl
= $this->url
. '/thumb';
47 * Per docs in FileRepo, this needs to return false if we don't support versioned
48 * files. Well, we don't.
50 function newFile( $title, $time = false ) {
54 return parent
::newFile( $title, $time );
60 function storeBatch( $triplets, $flags = 0 ) {
63 function storeTemp( $originalName, $srcPath ) {
66 function append( $srcPath, $toAppendPath, $flags = 0 ){
69 function publishBatch( $triplets, $flags = 0 ) {
72 function deleteBatch( $sourceDestPairs ) {
77 function fileExistsBatch( $files, $flags = 0 ) {
79 foreach ( $files as $k => $f ) {
80 if ( isset( $this->mFileExists
[$k] ) ) {
83 } elseif( self
::isVirtualUrl( $f ) ) {
84 # TODO! FIXME! We need to be able to handle virtual
85 # URLs better, at least when we know they refer to the
92 $results = $this->fetchImageQuery( array( 'titles' => implode( $files, '|' ),
93 'prop' => 'imageinfo' ) );
94 if( isset( $data['query']['pages'] ) ) {
96 foreach( $files as $key => $file ) {
97 $results[$key] = $this->mFileExists
[$key] = !isset( $data['query']['pages'][$i]['missing'] );
102 function getFileProps( $virtualUrl ) {
106 function fetchImageQuery( $query ) {
109 $url = $this->mApiBase
.
116 'redirects' => 'true' ) ) );
118 if( !isset( $this->mQueryCache
[$url] ) ) {
119 $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'Metadata', md5( $url ) );
120 $data = $wgMemc->get( $key );
122 $data = Http
::get( $url );
126 $wgMemc->set( $key, $data, 3600 );
129 if( count( $this->mQueryCache
) > 100 ) {
130 // Keep the cache from growing infinitely
131 $this->mQueryCache
= array();
133 $this->mQueryCache
[$url] = $data;
135 return FormatJson
::decode( $this->mQueryCache
[$url], true );
138 function getImageInfo( $data ) {
139 if( $data && isset( $data['query']['pages'] ) ) {
140 foreach( $data['query']['pages'] as $pageid => $info ) {
141 if( isset( $info['imageinfo'][0] ) ) {
142 return $info['imageinfo'][0];
149 function findBySha1( $hash ) {
150 $results = $this->fetchImageQuery( array(
151 'aisha1base36' => $hash,
152 'aiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mime',
153 'list' => 'allimages', ) );
155 if ( isset( $results['query']['allimages'] ) ) {
156 foreach ( $results['query']['allimages'] as $img ) {
157 $ret[] = new ForeignAPIFile( Title
::makeTitle( NS_FILE
, $img['name'] ), $this, $img );
163 function getThumbUrl( $name, $width=-1, $height=-1 ) {
164 $data = $this->fetchImageQuery( array(
165 'titles' => 'File:' . $name,
167 'iiurlwidth' => $width,
168 'iiurlheight' => $height,
169 'prop' => 'imageinfo' ) );
170 $info = $this->getImageInfo( $data );
172 if( $data && $info && $info['thumburl'] ) {
173 wfDebug( __METHOD__
. " got remote thumb " . $info['thumburl'] . "\n" );
174 return $info['thumburl'];
180 function getThumbUrlFromCache( $name, $width, $height ) {
181 global $wgMemc, $wgUploadPath, $wgServer, $wgUploadDirectory;
183 if ( !$this->canCacheThumbs() ) {
184 return $this->getThumbUrl( $name, $width, $height );
187 $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $name );
188 if ( $thumbUrl = $wgMemc->get($key) ) {
189 wfDebug("Got thumb from local cache. $thumbUrl \n");
193 $foreignUrl = $this->getThumbUrl( $name, $width, $height );
195 wfDebug( __METHOD__
. " Could not find thumburl\n" );
198 $thumb = Http
::get( $foreignUrl );
200 wfDebug( __METHOD__
. " Could not download thumb\n" );
203 // We need the same filename as the remote one :)
204 $fileName = rawurldecode( pathinfo( $foreignUrl, PATHINFO_BASENAME
) );
205 $path = 'thumb/' . $this->getHashPath( $name ) . $name . "/";
206 if ( !is_dir($wgUploadDirectory . '/' . $path) ) {
207 if( !wfMkdirParents($wgUploadDirectory . '/' . $path) ) {
208 wfDebug( __METHOD__
. " could not create directory for thumb\n" );
212 $localUrl = $wgServer . $wgUploadPath . '/' . $path . $fileName;
213 # FIXME: Delete old thumbs that aren't being used. Maintenance script?
214 if( !file_put_contents($wgUploadDirectory . '/' . $path . $fileName, $thumb ) ) {
215 wfDebug( __METHOD__
. " could not write to thumb path\n" );
218 $wgMemc->set( $key, $localUrl, $this->apiThumbCacheExpiry
);
219 wfDebug( __METHOD__
. " got local thumb $localUrl, saving to cache \n" );
225 * @see FileRepo::getZoneUrl()
227 function getZoneUrl( $zone ) {
232 return $this->thumbUrl
;
234 return parent
::getZoneUrl( $zone );
239 * Are we locally caching the thumbnails?
242 public function canCacheThumbs() {
243 return ( $this->apiThumbCacheExpiry
> 0 );