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 );
29 // http://commons.wikimedia.org/w/api.php
30 $this->mApiBase
= isset( $info['apibase'] ) ?
$info['apibase'] : null;
32 if( isset( $info['apiThumbCacheExpiry'] ) ) {
33 $this->apiThumbCacheExpiry
= $info['apiThumbCacheExpiry'];
35 if( !$this->scriptDirUrl
) {
36 // hack for description fetches
37 $this->scriptDirUrl
= dirname( $this->mApiBase
);
39 // If we can cache thumbs we can guess sane defaults for these
40 if( $this->canCacheThumbs() && !$this->url
) {
41 global $wgLocalFileRepo;
42 $this->url
= $wgLocalFileRepo['url'];
44 if( $this->canCacheThumbs() && !$this->thumbUrl
) {
45 $this->thumbUrl
= $this->url
. '/thumb';
50 * Per docs in FileRepo, this needs to return false if we don't support versioned
51 * files. Well, we don't.
53 function newFile( $title, $time = false ) {
57 return parent
::newFile( $title, $time );
63 function storeBatch( $triplets, $flags = 0 ) {
66 function storeTemp( $originalName, $srcPath ) {
69 function append( $srcPath, $toAppendPath, $flags = 0 ){
72 function publishBatch( $triplets, $flags = 0 ) {
75 function deleteBatch( $sourceDestPairs ) {
80 function fileExistsBatch( $files, $flags = 0 ) {
82 foreach ( $files as $k => $f ) {
83 if ( isset( $this->mFileExists
[$k] ) ) {
86 } elseif( self
::isVirtualUrl( $f ) ) {
87 # TODO! FIXME! We need to be able to handle virtual
88 # URLs better, at least when we know they refer to the
95 $results = $this->fetchImageQuery( array( 'titles' => implode( $files, '|' ),
96 'prop' => 'imageinfo' ) );
97 if( isset( $data['query']['pages'] ) ) {
99 foreach( $files as $key => $file ) {
100 $results[$key] = $this->mFileExists
[$key] = !isset( $data['query']['pages'][$i]['missing'] );
105 function getFileProps( $virtualUrl ) {
109 function fetchImageQuery( $query ) {
112 $query = array_merge( $query,
116 'redirects' => 'true'
118 if ( $this->mApiBase
) {
119 $url = wfAppendQuery( $this->mApiBase
, $query );
121 $url = $this->makeUrl( $query, 'api' );
124 if( !isset( $this->mQueryCache
[$url] ) ) {
125 $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'Metadata', md5( $url ) );
126 $data = $wgMemc->get( $key );
128 $data = Http
::get( $url );
132 $wgMemc->set( $key, $data, 3600 );
135 if( count( $this->mQueryCache
) > 100 ) {
136 // Keep the cache from growing infinitely
137 $this->mQueryCache
= array();
139 $this->mQueryCache
[$url] = $data;
141 return FormatJson
::decode( $this->mQueryCache
[$url], true );
144 function getImageInfo( $data ) {
145 if( $data && isset( $data['query']['pages'] ) ) {
146 foreach( $data['query']['pages'] as $pageid => $info ) {
147 if( isset( $info['imageinfo'][0] ) ) {
148 return $info['imageinfo'][0];
155 function findBySha1( $hash ) {
156 $results = $this->fetchImageQuery( array(
157 'aisha1base36' => $hash,
158 'aiprop' => ForeignAPIFile
::getProps(),
159 'list' => 'allimages', ) );
161 if ( isset( $results['query']['allimages'] ) ) {
162 foreach ( $results['query']['allimages'] as $img ) {
163 // 1.14 was broken, doesn't return name attribute
164 if( !isset( $img['name'] ) ) {
167 $ret[] = new ForeignAPIFile( Title
::makeTitle( NS_FILE
, $img['name'] ), $this, $img );
173 function getThumbUrl( $name, $width=-1, $height=-1 ) {
174 $data = $this->fetchImageQuery( array(
175 'titles' => 'File:' . $name,
177 'iiurlwidth' => $width,
178 'iiurlheight' => $height,
179 'prop' => 'imageinfo' ) );
180 $info = $this->getImageInfo( $data );
182 if( $data && $info && $info['thumburl'] ) {
183 wfDebug( __METHOD__
. " got remote thumb " . $info['thumburl'] . "\n" );
184 return $info['thumburl'];
190 function getThumbUrlFromCache( $name, $width, $height ) {
191 global $wgMemc, $wgUploadPath, $wgServer, $wgUploadDirectory;
193 if ( !$this->canCacheThumbs() ) {
194 return $this->getThumbUrl( $name, $width, $height );
197 $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $name );
198 if ( $thumbUrl = $wgMemc->get($key) ) {
199 wfDebug("Got thumb from local cache. $thumbUrl \n");
203 $foreignUrl = $this->getThumbUrl( $name, $width, $height );
205 wfDebug( __METHOD__
. " Could not find thumburl\n" );
208 $thumb = Http
::get( $foreignUrl );
210 wfDebug( __METHOD__
. " Could not download thumb\n" );
213 // We need the same filename as the remote one :)
214 $fileName = rawurldecode( pathinfo( $foreignUrl, PATHINFO_BASENAME
) );
215 $path = 'thumb/' . $this->getHashPath( $name ) . $name . "/";
216 if ( !is_dir($wgUploadDirectory . '/' . $path) ) {
217 if( !wfMkdirParents($wgUploadDirectory . '/' . $path) ) {
218 wfDebug( __METHOD__
. " could not create directory for thumb\n" );
222 $localUrl = $wgServer . $wgUploadPath . '/' . $path . $fileName;
223 # FIXME: Delete old thumbs that aren't being used. Maintenance script?
224 wfSuppressWarnings();
225 if( !file_put_contents($wgUploadDirectory . '/' . $path . $fileName, $thumb ) ) {
227 wfDebug( __METHOD__
. " could not write to thumb path\n" );
231 $wgMemc->set( $key, $localUrl, $this->apiThumbCacheExpiry
);
232 wfDebug( __METHOD__
. " got local thumb $localUrl, saving to cache \n" );
238 * @see FileRepo::getZoneUrl()
240 function getZoneUrl( $zone ) {
245 return $this->thumbUrl
;
247 return parent
::getZoneUrl( $zone );
252 * Are we locally caching the thumbnails?
255 public function canCacheThumbs() {
256 return ( $this->apiThumbCacheExpiry
> 0 );