264cb920f884966ada9bac30e8810c7f88d566a7
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 protected function queryImage( $query ) {
107 $data = $this->fetchImageQuery( $query );
109 if( isset( $data['query']['pages'] ) ) {
110 foreach( $data['query']['pages'] as $pageid => $info ) {
111 if( isset( $info['imageinfo'][0] ) ) {
112 return $info['imageinfo'][0];
119 protected function fetchImageQuery( $query ) {
122 $url = $this->mApiBase
.
128 'action' => 'query' ) ) );
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( $title, $time = false ) {
151 return $this->queryImage( array(
152 'titles' => 'Image:' . $title->getText(),
153 'iiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mime',
154 'prop' => 'imageinfo' ) );
157 function findBySha1( $hash ) {
158 $results = $this->fetchImageQuery( array(
159 'aisha1base36' => $hash,
160 'aiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mime',
161 'list' => 'allimages', ) );
163 if ( isset( $results['query']['allimages'] ) ) {
164 foreach ( $results['query']['allimages'] as $img ) {
165 $ret[] = new ForeignAPIFile( Title
::makeTitle( NS_FILE
, $img['name'] ), $this, $img );
171 function getThumbUrl( $name, $width=-1, $height=-1 ) {
172 $info = $this->queryImage( array(
173 'titles' => 'Image:' . $name,
175 'iiurlwidth' => $width,
176 'iiurlheight' => $height,
177 'prop' => 'imageinfo' ) );
178 if( $info && $info['thumburl'] ) {
179 wfDebug( __METHOD__
. " got remote thumb " . $info['thumburl'] . "\n" );
180 return $info['thumburl'];
186 function getThumbUrlFromCache( $name, $width, $height ) {
187 global $wgMemc, $wgUploadPath, $wgServer, $wgUploadDirectory;
189 if ( !$this->canCacheThumbs() ) {
190 return $this->getThumbUrl( $name, $width, $height );
193 $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $name );
194 if ( $thumbUrl = $wgMemc->get($key) ) {
195 wfDebug("Got thumb from local cache. $thumbUrl \n");
199 $foreignUrl = $this->getThumbUrl( $name, $width, $height );
201 wfDebug( __METHOD__
. " Could not find thumburl\n" );
204 $thumb = Http
::get( $foreignUrl );
206 wfDebug( __METHOD__
. " Could not download thumb\n" );
209 // We need the same filename as the remote one :)
210 $fileName = rawurldecode( pathinfo( $foreignUrl, PATHINFO_BASENAME
) );
211 $path = 'thumb/' . $this->getHashPath( $name ) . $name . "/";
212 if ( !is_dir($wgUploadDirectory . '/' . $path) ) {
213 wfMkdirParents($wgUploadDirectory . '/' . $path);
215 $localUrl = $wgServer . $wgUploadPath . '/' . $path . $fileName;
216 # FIXME: Delete old thumbs that aren't being used. Maintenance script?
217 if( !file_put_contents($wgUploadDirectory . '/' . $path . $fileName, $thumb ) ) {
218 wfDebug( __METHOD__
. " could not write to thumb path\n" );
221 $wgMemc->set( $key, $localUrl, $this->apiThumbCacheExpiry
);
222 wfDebug( __METHOD__
. " got local thumb $localUrl, saving to cache \n" );
228 * @see FileRepo::getZoneUrl()
230 function getZoneUrl( $zone ) {
235 return $this->thumbUrl
;
237 return parent
::getZoneUrl( $zone );
242 * Are we locally caching the thumbnails?
245 public function canCacheThumbs() {
246 return ( $this->apiThumbCacheExpiry
> 0 );