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( !$this->scriptDirUrl
) {
30 // hack for description fetches
31 $this->scriptDirUrl
= dirname( $this->mApiBase
);
36 * Per docs in FileRepo, this needs to return false if we don't support versioned
37 * files. Well, we don't.
39 function newFile( $title, $time = false ) {
43 return parent
::newFile( $title, $time );
49 function storeBatch( $triplets, $flags = 0 ) {
52 function storeTemp( $originalName, $srcPath ) {
55 function append( $srcPath, $toAppendPath ){
58 function publishBatch( $triplets, $flags = 0 ) {
61 function deleteBatch( $sourceDestPairs ) {
66 function fileExistsBatch( $files, $flags = 0 ) {
68 foreach ( $files as $k => $f ) {
69 if ( isset( $this->mFileExists
[$k] ) ) {
72 } elseif( self
::isVirtualUrl( $f ) ) {
73 # TODO! FIXME! We need to be able to handle virtual
74 # URLs better, at least when we know they refer to the
81 $results = $this->fetchImageQuery( array( 'titles' => implode( $files, '|' ),
82 'prop' => 'imageinfo' ) );
83 if( isset( $data['query']['pages'] ) ) {
85 foreach( $files as $key => $file ) {
86 $results[$key] = $this->mFileExists
[$key] = !isset( $data['query']['pages'][$i]['missing'] );
91 function getFileProps( $virtualUrl ) {
95 protected function queryImage( $query ) {
96 $data = $this->fetchImageQuery( $query );
98 if( isset( $data['query']['pages'] ) ) {
99 foreach( $data['query']['pages'] as $pageid => $info ) {
100 if( isset( $info['imageinfo'][0] ) ) {
101 return $info['imageinfo'][0];
108 protected function fetchImageQuery( $query ) {
111 $url = $this->mApiBase
.
117 'action' => 'query' ) ) );
119 if( !isset( $this->mQueryCache
[$url] ) ) {
120 $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'Metadata', md5( $url ) );
121 $data = $wgMemc->get( $key );
123 $data = Http
::get( $url );
127 $wgMemc->set( $key, $data, 3600 );
130 if( count( $this->mQueryCache
) > 100 ) {
131 // Keep the cache from growing infinitely
132 $this->mQueryCache
= array();
134 $this->mQueryCache
[$url] = $data;
136 return FormatJson
::decode( $this->mQueryCache
[$url], true );
139 function getImageInfo( $title, $time = false ) {
140 return $this->queryImage( array(
141 'titles' => 'Image:' . $title->getText(),
142 'iiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mime',
143 'prop' => 'imageinfo' ) );
146 function findBySha1( $hash ) {
147 $results = $this->fetchImageQuery( array(
148 'aisha1base36' => $hash,
149 'aiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mime',
150 'list' => 'allimages', ) );
152 if ( isset( $results['query']['allimages'] ) ) {
153 foreach ( $results['query']['allimages'] as $img ) {
154 $ret[] = new ForeignAPIFile( Title
::makeTitle( NS_FILE
, $img['name'] ), $this, $img );
160 function getThumbUrl( $name, $width=-1, $height=-1 ) {
161 $info = $this->queryImage( array(
162 'titles' => 'Image:' . $name,
164 'iiurlwidth' => $width,
165 'iiurlheight' => $height,
166 'prop' => 'imageinfo' ) );
168 wfDebug( __METHOD__
. " got remote thumb " . $info['thumburl'] . "\n" );
169 return $info['thumburl'];
175 function getThumbUrlFromCache( $name, $width, $height ) {
176 global $wgMemc, $wgUploadPath, $wgServer, $wgUploadDirectory;
178 if ( !$this->canCacheThumbs() ) {
179 return $this->getThumbUrl( $name, $width, $height );
182 $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $name );
183 if ( $thumbUrl = $wgMemc->get($key) ) {
184 wfDebug("Got thumb from local cache. $thumbUrl \n");
188 $foreignUrl = $this->getThumbUrl( $name, $width, $height );
190 // We need the same filename as the remote one :)
191 $fileName = rawurldecode( pathinfo( $foreignUrl, PATHINFO_BASENAME
) );
192 $path = 'thumb/' . $this->getHashPath( $name ) . $name . "/";
193 if ( !is_dir($wgUploadDirectory . '/' . $path) ) {
194 wfMkdirParents($wgUploadDirectory . '/' . $path);
196 $localUrl = $wgServer . $wgUploadPath . '/' . $path . $fileName;
197 $thumb = Http
::get( $foreignUrl );
198 # FIXME: Delete old thumbs that aren't being used. Maintenance script?
199 if( !file_put_contents($wgUploadDirectory . '/' . $path . $fileName, $thumb ) ) {
200 wfDebug( __METHOD__
. " could not write to thumb path\n" );
203 $wgMemc->set( $key, $localUrl, $this->apiThumbCacheExpiry
);
204 wfDebug( __METHOD__
. " got local thumb $localUrl, saving to cache \n" );
210 * Are we locally caching the thumbnails?
213 public function canCacheThumbs() {
214 return ( $this->apiThumbCacheExpiry
> 0 );