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
);
35 * Per docs in FileRepo, this needs to return false if we don't support versioned
36 * files. Well, we don't.
38 function newFile( $title, $time = false ) {
42 return parent
::newFile( $title, $time );
48 function storeBatch( $triplets, $flags = 0 ) {
51 function storeTemp( $originalName, $srcPath ) {
54 function publishBatch( $triplets, $flags = 0 ) {
57 function deleteBatch( $sourceDestPairs ) {
60 function getFileProps( $virtualUrl ) {
64 protected function queryImage( $query ) {
65 $data = $this->fetchImageQuery( $query );
67 if( isset( $data['query']['pages'] ) ) {
68 foreach( $data['query']['pages'] as $pageid => $info ) {
69 if( isset( $info['imageinfo'][0] ) ) {
70 return $info['imageinfo'][0];
77 protected function fetchImageQuery( $query ) {
80 $url = $this->mApiBase
.
86 'action' => 'query' ) ) );
88 if( !isset( $this->mQueryCache
[$url] ) ) {
89 $key = wfMemcKey( 'ForeignAPIRepo', 'Metadata', md5( $url ) );
90 $data = $wgMemc->get( $key );
92 $data = Http
::get( $url );
96 $wgMemc->set( $key, $data, 3600 );
99 if( count( $this->mQueryCache
) > 100 ) {
100 // Keep the cache from growing infinitely
101 $this->mQueryCache
= array();
103 $this->mQueryCache
[$url] = $data;
105 return json_decode( $this->mQueryCache
[$url], true );
108 function getImageInfo( $title, $time = false ) {
109 return $this->queryImage( array(
110 'titles' => 'Image:' . $title->getText(),
111 'iiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mime',
112 'prop' => 'imageinfo' ) );
115 function findBySha1( $hash ) {
116 $results = $this->fetchImageQuery( array(
117 'aisha1base36' => $hash,
118 'aiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mime',
119 'list' => 'allimages', ) );
121 if ( isset( $results['query']['allimages'] ) ) {
122 foreach ( $results['query']['allimages'] as $img ) {
123 $ret[] = new ForeignAPIFile( Title
::makeTitle( NS_FILE
, $img['name'] ), $this, $img );
129 function getThumbUrl( $name, $width=-1, $height=-1 ) {
130 $info = $this->queryImage( array(
131 'titles' => 'Image:' . $name,
133 'iiurlwidth' => $width,
134 'iiurlheight' => $height,
135 'prop' => 'imageinfo' ) );
137 wfDebug( __METHOD__
. " got remote thumb " . $info['thumburl'] . "\n" );
138 return $info['thumburl'];
144 function getThumbUrlFromCache( $name, $width, $height ) {
145 global $wgMemc, $wgUploadPath, $wgServer, $wgUploadDirectory;
147 if ( !$this->canCacheThumbs() ) {
148 return $this->getThumbUrl( $name, $width, $height );
151 $key = wfMemcKey( 'ForeignAPIRepo', 'ThumbUrl', $name );
152 if ( $thumbUrl = $wgMemc->get($key) ) {
153 wfDebug("Got thumb from local cache. $thumbUrl \n");
157 $foreignUrl = $this->getThumbUrl( $name, $width, $height );
159 // We need the same filename as the remote one :)
160 $fileName = ltrim( substr( $foreignUrl, strrpos( $foreignUrl, '/' ) ), '/' );
161 $path = 'thumb/' . $this->getHashPath( $name ) . $name . "/";
162 if ( !is_dir($wgUploadDirectory . '/' . $path) ) {
163 wfMkdirParents($wgUploadDirectory . '/' . $path);
165 if ( !is_writable( $wgUploadDirectory . '/' . $path . $fileName ) ) {
166 wfDebug( __METHOD__
. " could not write to thumb path\n" );
169 $localUrl = $wgServer . $wgUploadPath . '/' . $path . $fileName;
170 $thumb = Http
::get( $foreignUrl );
171 # FIXME: Delete old thumbs that aren't being used. Maintenance script?
172 file_put_contents($wgUploadDirectory . '/' . $path . $fileName, $thumb );
173 $wgMemc->set( $key, $localUrl, $this->apiThumbCacheExpiry
);
174 wfDebug( __METHOD__
. " got local thumb $localUrl, saving to cache \n" );
180 * Are we locally caching the thumbnails?
183 public function canCacheThumbs() {
184 return ( $this->apiThumbCacheExpiry
> 0 );