(bug 15814) Filter log chunk at Special:RevisionDelete to selected items
[mediawiki.git] / includes / filerepo / ForeignAPIRepo.php
blobe63e4a6ba8ecb65fb3f8b3b49e7bcb5d9ebb9c2c
1 <?php
3 /**
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
8 * Example config:
10 * $wgForeignFileRepos[] = array(
11 * 'class' => 'ForeignAPIRepo',
12 * 'name' => 'shared',
13 * 'apibase' => 'http://en.wikipedia.org/w/api.php',
14 * 'fetchDescription' => true, // Optional
15 * 'descriptionCacheExpiry' => 3600,
16 * );
18 * @ingroup FileRepo
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 );
34 /**
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 ) {
39 if ( $time ) {
40 return false;
42 return parent::newFile( $title, $time );
45 /**
46 * No-ops
48 function storeBatch( $triplets, $flags = 0 ) {
49 return false;
51 function storeTemp( $originalName, $srcPath ) {
52 return false;
54 function publishBatch( $triplets, $flags = 0 ) {
55 return false;
57 function deleteBatch( $sourceDestPairs ) {
58 return false;
60 function getFileProps( $virtualUrl ) {
61 return false;
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];
74 return false;
77 protected function fetchImageQuery( $query ) {
78 global $wgMemc;
80 $url = $this->mApiBase .
81 '?' .
82 wfArrayToCgi(
83 array_merge( $query,
84 array(
85 'format' => 'json',
86 'action' => 'query' ) ) );
88 if( !isset( $this->mQueryCache[$url] ) ) {
89 $key = wfMemcKey( 'ForeignAPIRepo', 'Metadata', md5( $url ) );
90 $data = $wgMemc->get( $key );
91 if( !$data ) {
92 $data = Http::get( $url );
93 if ( !$data ) {
94 return null;
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', ) );
120 $ret = array();
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 );
126 return $ret;
129 function getThumbUrl( $name, $width=-1, $height=-1 ) {
130 $info = $this->queryImage( array(
131 'titles' => 'Image:' . $name,
132 'iiprop' => 'url',
133 'iiurlwidth' => $width,
134 'iiurlheight' => $height,
135 'prop' => 'imageinfo' ) );
136 if( $info ) {
137 wfDebug( __METHOD__ . " got remote thumb " . $info['thumburl'] . "\n" );
138 return $info['thumburl'];
139 } else {
140 return false;
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");
154 return $thumbUrl;
156 else {
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" );
167 return $foreignUrl;
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" );
175 return $localUrl;
180 * Are we locally caching the thumbnails?
181 * @return bool
183 public function canCacheThumbs() {
184 return ( $this->apiThumbCacheExpiry > 0 );