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 .
75 'action' => 'query' ) ) );
77 if( !isset( $this->mQueryCache[$url] ) ) {
78 $key = wfMemcKey( 'ForeignAPIRepo', 'Metadata', md5( $url ) );
79 $data = $wgMemc->get( $key );
81 $data = Http::get( $url );
85 $wgMemc->set( $key, $data, 3600 );
88 if( count( $this->mQueryCache ) > 100 ) {
89 // Keep the cache from growing infinitely
90 $this->mQueryCache = array();
92 $this->mQueryCache[$url] = $data;
94 return json_decode( $this->mQueryCache[$url], true );
97 function getImageInfo( $title, $time = false ) {
98 return $this->queryImage( array(
99 'titles' => 'Image:' . $title->getText(),
100 'iiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mime',
101 'prop' => 'imageinfo' ) );
104 function findBySha1( $hash ) {
105 $results = $this->fetchImageQuery( array(
106 'aisha1base36' => $hash,
107 'aiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mime',
108 'list' => 'allimages', ) );
110 if ( isset( $results['query']['allimages'] ) ) {
111 foreach ( $results['query']['allimages'] as $img ) {
112 $ret[] = new ForeignAPIFile( Title::makeTitle( NS_IMAGE, $img['name'] ), $this, $img );
118 function getThumbUrl( $name, $width=-1, $height=-1 ) {
119 $info = $this->queryImage( array(
120 'titles' => 'Image:' . $name,
122 'iiurlwidth' => $width,
123 'iiurlheight' => $height,
124 'prop' => 'imageinfo' ) );
126 wfDebug( __METHOD__ . " got remote thumb " . $info['thumburl'] . "\n" );
127 return $info['thumburl'];
133 function getThumbUrlFromCache( $name, $width, $height ) {
134 global $wgMemc, $wgUploadPath, $wgServer, $wgUploadDirectory;
136 if ( !$this->canCacheThumbs() ) {
137 return $this->getThumbUrl( $name, $width, $height );
140 $key = wfMemcKey( 'ForeignAPIRepo', 'ThumbUrl', $name );
141 if ( $thumbUrl = $wgMemc->get($key) ) {
142 wfDebug("Got thumb from local cache. $thumbUrl \n");
146 $foreignUrl = $this->getThumbUrl( $name, $width, $height );
148 // We need the same filename as the remote one :)
149 $fileName = ltrim( substr( $foreignUrl, strrpos( $foreignUrl, '/' ) ), '/' );
150 $path = 'thumb/' . $this->getHashPath( $name ) . $name . "/";
151 if ( !is_dir($wgUploadDirectory . '/' . $path) ) {
152 wfMkdirParents($wgUploadDirectory . '/' . $path);
154 if ( !is_writable( $wgUploadDirectory . '/' . $path . $fileName ) ) {
155 wfDebug( __METHOD__ . " could not write to thumb path\n" );
158 $localUrl = $wgServer . $wgUploadPath . '/' . $path . $fileName;
159 $thumb = Http::get( $foreignUrl );
160 # FIXME: Delete old thumbs that aren't being used. Maintenance script?
161 file_put_contents($wgUploadDirectory . '/' . $path . $fileName, $thumb );
162 $wgMemc->set( $key, $localUrl, $this->apiThumbCacheExpiry );
163 wfDebug( __METHOD__ . " got local thumb $localUrl, saving to cache \n" );
169 * Are we locally caching the thumbnails?
172 public function canCacheThumbs() {
173 return ( $this->apiThumbCacheExpiry > 0 );