43b2ab105890aa8de290df275d114c98093b72ef
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 protected $mQueryCache = array();
22 function __construct( $info ) {
23 parent
::__construct( $info );
24 $this->mApiBase
= $info['apibase']; // http://commons.wikimedia.org/w/api.php
25 if( !$this->scriptDirUrl
) {
26 // hack for description fetches
27 $this->scriptDirUrl
= dirname( $this->mApiBase
);
31 function storeBatch( $triplets, $flags = 0 ) {
35 function storeTemp( $originalName, $srcPath ) {
38 function publishBatch( $triplets, $flags = 0 ) {
41 function deleteBatch( $sourceDestPairs ) {
44 function getFileProps( $virtualUrl ) {
47 function newFile( $title, $time = false ) {
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', $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 findFile( $title, $time = false ) {
94 $info = $this->queryImage( array(
95 'titles' => 'Image:' . $title->getText(),
96 'iiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mimetype' ) );
98 return new ForeignAPIFile( $title, $this, $info );
104 function getThumbUrl( $name, $width=-1, $height=-1 ) {
105 $info = $this->queryImage( array(
106 'titles' => 'Image:' . $name,
108 'iiurlwidth' => $width,
109 'iiurlheight' => $height ) );
111 return $info['thumburl'];