6f43719c3c2555480095af960f0064476cfdbbed
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 publishBatch( $triplets, $flags = 0 ) {
69 function deleteBatch( $sourceDestPairs ) {
74 function fileExistsBatch( $files, $flags = 0 ) {
76 foreach ( $files as $k => $f ) {
77 if ( isset( $this->mFileExists
[$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
89 $results = $this->fetchImageQuery( array( 'titles' => implode( $files, '|' ),
90 'prop' => 'imageinfo' ) );
91 if( isset( $data['query']['pages'] ) ) {
93 foreach( $files as $key => $file ) {
94 $results[$key] = $this->mFileExists
[$key] = !isset( $data['query']['pages'][$i]['missing'] );
99 function getFileProps( $virtualUrl ) {
103 protected function queryImage( $query ) {
104 $data = $this->fetchImageQuery( $query );
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];
116 protected function fetchImageQuery( $query ) {
119 $url = $this->mApiBase
.
125 'action' => 'query' ) ) );
127 if( !isset( $this->mQueryCache
[$url] ) ) {
128 $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'Metadata', md5( $url ) );
129 $data = $wgMemc->get( $key );
131 $data = Http
::get( $url );
135 $wgMemc->set( $key, $data, 3600 );
138 if( count( $this->mQueryCache
) > 100 ) {
139 // Keep the cache from growing infinitely
140 $this->mQueryCache
= array();
142 $this->mQueryCache
[$url] = $data;
144 return FormatJson
::decode( $this->mQueryCache
[$url], true );
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' ) );
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', ) );
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 );
168 function getThumbUrl( $name, $width=-1, $height=-1 ) {
169 $info = $this->queryImage( array(
170 'titles' => 'Image:' . $name,
172 'iiurlwidth' => $width,
173 'iiurlheight' => $height,
174 'prop' => 'imageinfo' ) );
176 wfDebug( __METHOD__
. " got remote thumb " . $info['thumburl'] . "\n" );
177 return $info['thumburl'];
183 function getThumbUrlFromCache( $name, $width, $height ) {
184 global $wgMemc, $wgUploadPath, $wgServer, $wgUploadDirectory;
186 if ( !$this->canCacheThumbs() ) {
187 return $this->getThumbUrl( $name, $width, $height );
190 $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $name );
191 if ( $thumbUrl = $wgMemc->get($key) ) {
192 wfDebug("Got thumb from local cache. $thumbUrl \n");
196 $foreignUrl = $this->getThumbUrl( $name, $width, $height );
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);
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" );
211 $wgMemc->set( $key, $localUrl, $this->apiThumbCacheExpiry
);
212 wfDebug( __METHOD__
. " got local thumb $localUrl, saving to cache \n" );
218 * @see FileRepo::getZoneUrl()
220 function getZoneUrl( $zone ) {
225 return $this->thumbUrl
;
227 return parent
::getZoneUrl( $zone );
232 * Are we locally caching the thumbnails?
235 public function canCacheThumbs() {
236 return ( $this->apiThumbCacheExpiry
> 0 );