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 = 0;
23 protected $mQueryCache = array();
25 function __construct( $info ) {
26 parent
::__construct( $info );
27 $this->mApiBase
= $info['apibase']; // http://commons.wikimedia.org/w/api.php
28 if( !$this->scriptDirUrl
) {
29 // hack for description fetches
30 $this->scriptDirUrl
= dirname( $this->mApiBase
);
37 function storeBatch( $triplets, $flags = 0 ) {
40 function storeTemp( $originalName, $srcPath ) {
43 function publishBatch( $triplets, $flags = 0 ) {
46 function deleteBatch( $sourceDestPairs ) {
49 function getFileProps( $virtualUrl ) {
53 protected function queryImage( $query ) {
54 $data = $this->fetchImageQuery( $query );
56 if( isset( $data['query']['pages'] ) ) {
57 foreach( $data['query']['pages'] as $pageid => $info ) {
58 if( isset( $info['imageinfo'][0] ) ) {
59 return $info['imageinfo'][0];
66 protected function fetchImageQuery( $query ) {
69 $url = $this->mApiBase
.
76 'prop' => 'imageinfo' ) ) );
78 if( !isset( $this->mQueryCache
[$url] ) ) {
79 $key = wfMemcKey( 'ForeignAPIRepo', 'Metadata', md5( $url ) );
80 $data = $wgMemc->get( $key );
82 $data = Http
::get( $url );
83 $wgMemc->set( $key, $data, 3600 );
86 if( count( $this->mQueryCache
) > 100 ) {
87 // Keep the cache from growing infinitely
88 $this->mQueryCache
= array();
90 $this->mQueryCache
[$url] = $data;
92 return json_decode( $this->mQueryCache
[$url], true );
95 function getImageInfo( $title, $time = false ) {
96 return $this->queryImage( array(
97 'titles' => 'Image:' . $title->getText(),
98 'iiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mime' ) );
101 function getThumbUrl( $name, $width=-1, $height=-1 ) {
102 $info = $this->queryImage( array(
103 'titles' => 'Image:' . $name,
105 'iiurlwidth' => $width,
106 'iiurlheight' => $height ) );
108 wfDebug( __METHOD__
. " got remote thumb " . $info['thumburl'] . "\n" );
109 return $info['thumburl'];
115 function getThumbUrlFromCache( $name, $width, $height ) {
116 global $wgMemc, $wgUploadPath, $wgServer, $wgUploadDirectory;
118 if ( !$this->canCacheThumbs() ) {
119 return $this->getThumbUrl( $name, $width, $height );
122 $key = wfMemcKey( 'ForeignAPIRepo', 'ThumbUrl', $name );
123 if ( $thumbUrl = $wgMemc->get($key) ) {
124 wfDebug("Got thumb from local cache. $thumbUrl \n");
128 $foreignUrl = $this->getThumbUrl( $name, $width, $height );
130 // We need the same filename as the remote one :)
131 $fileName = ltrim( substr( $foreignUrl, strrpos( $foreignUrl, '/' ) ), '/' );
132 $path = 'thumb/' . $this->getHashPath( $name ) . $name . "/";
133 if ( !is_dir($wgUploadDirectory . '/' . $path) ) {
134 wfMkdirParents($wgUploadDirectory . '/' . $path);
137 $localUrl = $wgServer . $wgUploadPath . '/' . $path . $fileName;
138 $thumb = Http
::get( $foreignUrl );
139 # FIXME: Delete old thumbs that aren't being used. Maintenance script?
140 file_put_contents($wgUploadDirectory . '/' . $path . $fileName, $thumb );
141 $wgMemc->set( $key, $localUrl, $this->apiThumbCacheExpiry
);
142 wfDebug( __METHOD__
. " got local thumb $localUrl, saving to cache \n" );
148 * Are we locally caching the thumbnails?
151 public function canCacheThumbs() {
152 return ( $this->apiThumbCacheExpiry
> 0 );