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
19 class ForeignAPIRepo
extends FileRepo
{
20 var $fileFactory = array( 'ForeignAPIFile', 'newFromTitle' );
21 protected $mQueryCache = array();
23 function __construct( $info ) {
24 parent
::__construct( $info );
25 $this->mApiBase
= $info['apibase']; // http://commons.wikimedia.org/w/api.php
26 if( !$this->scriptDirUrl
) {
27 // hack for description fetches
28 $this->scriptDirUrl
= dirname( $this->mApiBase
);
32 function storeBatch( $triplets, $flags = 0 ) {
36 function storeTemp( $originalName, $srcPath ) {
39 function publishBatch( $triplets, $flags = 0 ) {
42 function deleteBatch( $sourceDestPairs ) {
45 function getFileProps( $virtualUrl ) {
49 protected function queryImage( $query ) {
50 $data = $this->fetchImageQuery( $query );
52 if( isset( $data['query']['pages'] ) ) {
53 foreach( $data['query']['pages'] as $pageid => $info ) {
54 if( isset( $info['imageinfo'][0] ) ) {
55 return $info['imageinfo'][0];
62 protected function fetchImageQuery( $query ) {
65 $url = $this->mApiBase
.
72 'prop' => 'imageinfo' ) ) );
74 if( !isset( $this->mQueryCache
[$url] ) ) {
75 $key = wfMemcKey( 'ForeignAPIRepo', $url );
76 $data = $wgMemc->get( $key );
78 $data = Http
::get( $url );
79 $wgMemc->set( $key, $data, 3600 );
82 if( count( $this->mQueryCache
) > 100 ) {
83 // Keep the cache from growing infinitely
84 $this->mQueryCache
= array();
86 $this->mQueryCache
[$url] = $data;
88 return json_decode( $this->mQueryCache
[$url], true );
91 function getImageInfo( $title, $time = false ) {
92 return $this->queryImage( array(
93 'titles' => 'Image:' . $title->getText(),
94 'iiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mime' ) );
97 function getThumbUrl( $name, $width=-1, $height=-1 ) {
98 $info = $this->queryImage( array(
99 'titles' => 'Image:' . $name,
101 'iiurlwidth' => $width,
102 'iiurlheight' => $height ) );
104 return $info['thumburl'];