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
);
34 function storeBatch( $triplets, $flags = 0 ) {
38 function storeTemp( $originalName, $srcPath ) {
41 function publishBatch( $triplets, $flags = 0 ) {
44 function deleteBatch( $sourceDestPairs ) {
47 function getFileProps( $virtualUrl ) {
51 protected function queryImage( $query ) {
52 $data = $this->fetchImageQuery( $query );
54 if( isset( $data['query']['pages'] ) ) {
55 foreach( $data['query']['pages'] as $pageid => $info ) {
56 if( isset( $info['imageinfo'][0] ) ) {
57 return $info['imageinfo'][0];
64 protected function fetchImageQuery( $query ) {
67 $url = $this->mApiBase
.
74 'prop' => 'imageinfo' ) ) );
76 if( !isset( $this->mQueryCache
[$url] ) ) {
77 $key = wfMemcKey( 'ForeignAPIRepo', 'Metadata', md5( $url ) );
78 $data = $wgMemc->get( $key );
80 $data = Http
::get( $url );
81 $wgMemc->set( $key, $data, 3600 );
84 if( count( $this->mQueryCache
) > 100 ) {
85 // Keep the cache from growing infinitely
86 $this->mQueryCache
= array();
88 $this->mQueryCache
[$url] = $data;
90 return json_decode( $this->mQueryCache
[$url], true );
93 function getImageInfo( $title, $time = false ) {
94 return $this->queryImage( array(
95 'titles' => 'Image:' . $title->getText(),
96 'iiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mime' ) );
99 function getThumbUrl( $name, $width=-1, $height=-1 ) {
100 $info = $this->queryImage( array(
101 'titles' => 'Image:' . $name,
103 'iiurlwidth' => $width,
104 'iiurlheight' => $height ) );
106 wfDebug( __METHOD__
. " got remote thumb " . $info['thumburl'] . "\n" );
107 return $info['thumburl'];
113 function getThumbUrlFromCache( $name, $width, $height ) {
114 global $wgMemc, $wgUploadPath, $wgServer, $wgUploadDirectory;
116 if ( !$this->canCacheThumbs() ) {
117 return $this->getThumbUrl( $name, $width, $height );
120 $key = wfMemcKey( 'ForeignAPIRepo', 'ThumbUrl', $name );
121 if ( $thumbUrl = $wgMemc->get($key) ) {
122 wfDebug("Got thumb from local cache. $thumbUrl \n");
126 $foreignUrl = $this->getThumbUrl( $name, $width, $height );
127 $path = $this->apiThumbCacheDir
. '/' . $this->name
. '/' .
129 if ( !is_dir($wgUploadDirectory . '/' . $path) ) {
130 wfMkdirParents($wgUploadDirectory . '/' . $path);
132 $localUrl = $wgServer . $wgUploadPath . '/' . $path . $width . 'px-' . $name;
133 $thumb = Http
::get( $foreignUrl );
134 # FIXME: Delete old thumbs that aren't being used. Maintenance script?
135 file_put_contents($wgUploadDirectory . '/' . $path . $width . 'px-' . $name, $thumb );
136 $wgMemc->set( $key, $localUrl, $this->apiThumbCacheExpiry
);
137 wfDebug( __METHOD__
. " got local thumb $localUrl, saving to cache \n" );
143 * Are we locally caching the thumbnails?
146 public function canCacheThumbs() {
147 return ( $this->apiThumbCacheExpiry
> 0 && $this->apiThumbCacheDir
);