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 protected $mQueryCache = array();
24 function __construct( $info ) {
25 parent
::__construct( $info );
26 $this->mApiBase
= $info['apibase']; // http://commons.wikimedia.org/w/api.php
27 if( !$this->scriptDirUrl
) {
28 // hack for description fetches
29 $this->scriptDirUrl
= dirname( $this->mApiBase
);
33 function storeBatch( $triplets, $flags = 0 ) {
37 function storeTemp( $originalName, $srcPath ) {
40 function publishBatch( $triplets, $flags = 0 ) {
43 function deleteBatch( $sourceDestPairs ) {
46 function getFileProps( $virtualUrl ) {
50 protected function queryImage( $query ) {
51 $data = $this->fetchImageQuery( $query );
53 if( isset( $data['query']['pages'] ) ) {
54 foreach( $data['query']['pages'] as $pageid => $info ) {
55 if( isset( $info['imageinfo'][0] ) ) {
56 return $info['imageinfo'][0];
63 protected function fetchImageQuery( $query ) {
66 $url = $this->mApiBase
.
73 'prop' => 'imageinfo' ) ) );
75 if( !isset( $this->mQueryCache
[$url] ) ) {
76 $key = wfMemcKey( 'ForeignAPIRepo', $url );
77 $data = $wgMemc->get( $key );
79 $data = Http
::get( $url );
80 $wgMemc->set( $key, $data, 3600 );
83 if( count( $this->mQueryCache
) > 100 ) {
84 // Keep the cache from growing infinitely
85 $this->mQueryCache
= array();
87 $this->mQueryCache
[$url] = $data;
89 return json_decode( $this->mQueryCache
[$url], true );
92 function getImageInfo( $title, $time = false ) {
93 return $this->queryImage( array(
94 'titles' => 'Image:' . $title->getText(),
95 'iiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mime' ) );
98 function getThumbUrl( $name, $width=-1, $height=-1 ) {
99 $info = $this->queryImage( array(
100 'titles' => 'Image:' . $name,
102 'iiurlwidth' => $width,
103 'iiurlheight' => $height ) );
105 return $info['thumburl'];