(bug 16265) Enabling apiThumbCacheExpiry produced invalid thumbs for things like...
[lhc/web/wiklou.git] / includes / filerepo / ForeignAPIRepo.php
1 <?php
2
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
7 *
8 * Example config:
9 *
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 * );
17 *
18 * @ingroup FileRepo
19 */
20 class ForeignAPIRepo extends FileRepo {
21 var $fileFactory = array( 'ForeignAPIFile', 'newFromTitle' );
22 var $apiThumbCacheExpiry = 0;
23 protected $mQueryCache = array();
24
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 );
31 }
32 }
33
34 function storeBatch( $triplets, $flags = 0 ) {
35 return false;
36 }
37
38 function storeTemp( $originalName, $srcPath ) {
39 return false;
40 }
41 function publishBatch( $triplets, $flags = 0 ) {
42 return false;
43 }
44 function deleteBatch( $sourceDestPairs ) {
45 return false;
46 }
47 function getFileProps( $virtualUrl ) {
48 return false;
49 }
50
51 protected function queryImage( $query ) {
52 $data = $this->fetchImageQuery( $query );
53
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];
58 }
59 }
60 }
61 return false;
62 }
63
64 protected function fetchImageQuery( $query ) {
65 global $wgMemc;
66
67 $url = $this->mApiBase .
68 '?' .
69 wfArrayToCgi(
70 array_merge( $query,
71 array(
72 'format' => 'json',
73 'action' => 'query',
74 'prop' => 'imageinfo' ) ) );
75
76 if( !isset( $this->mQueryCache[$url] ) ) {
77 $key = wfMemcKey( 'ForeignAPIRepo', 'Metadata', md5( $url ) );
78 $data = $wgMemc->get( $key );
79 if( !$data ) {
80 $data = Http::get( $url );
81 $wgMemc->set( $key, $data, 3600 );
82 }
83
84 if( count( $this->mQueryCache ) > 100 ) {
85 // Keep the cache from growing infinitely
86 $this->mQueryCache = array();
87 }
88 $this->mQueryCache[$url] = $data;
89 }
90 return json_decode( $this->mQueryCache[$url], true );
91 }
92
93 function getImageInfo( $title, $time = false ) {
94 return $this->queryImage( array(
95 'titles' => 'Image:' . $title->getText(),
96 'iiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mime' ) );
97 }
98
99 function getThumbUrl( $name, $width=-1, $height=-1 ) {
100 $info = $this->queryImage( array(
101 'titles' => 'Image:' . $name,
102 'iiprop' => 'url',
103 'iiurlwidth' => $width,
104 'iiurlheight' => $height ) );
105 if( $info ) {
106 wfDebug( __METHOD__ . " got remote thumb " . $info['thumburl'] . "\n" );
107 return $info['thumburl'];
108 } else {
109 return false;
110 }
111 }
112
113 function getThumbUrlFromCache( $name, $width, $height ) {
114 global $wgMemc, $wgUploadPath, $wgServer, $wgUploadDirectory;
115
116 if ( !$this->canCacheThumbs() ) {
117 return $this->getThumbUrl( $name, $width, $height );
118 }
119
120 $key = wfMemcKey( 'ForeignAPIRepo', 'ThumbUrl', $name );
121 if ( $thumbUrl = $wgMemc->get($key) ) {
122 wfDebug("Got thumb from local cache. $thumbUrl \n");
123 return $thumbUrl;
124 }
125 else {
126 $foreignUrl = $this->getThumbUrl( $name, $width, $height );
127
128 // We need the same filename as the remote one :)
129 $fileName = ltrim( substr( $foreignUrl, strrpos( $foreignUrl, '/' ),
130 strlen ( $foreignUrl ) ), '/' );
131 //die( var_dump( $fileName ) );
132 $path = $this->apiThumbCacheDir . '/' .
133 $name . '/';
134 if ( !is_dir($wgUploadDirectory . '/' . $path) ) {
135 wfMkdirParents($wgUploadDirectory . '/' . $path);
136 }
137
138 $localUrl = $wgServer . $wgUploadPath . '/' . $path . $fileName;
139 $thumb = Http::get( $foreignUrl );
140 # FIXME: Delete old thumbs that aren't being used. Maintenance script?
141 file_put_contents($wgUploadDirectory . '/' . $path . $fileName, $thumb );
142 $wgMemc->set( $key, $localUrl, $this->apiThumbCacheExpiry );
143 wfDebug( __METHOD__ . " got local thumb $localUrl, saving to cache \n" );
144 return $localUrl;
145 }
146 }
147
148 /**
149 * Are we locally caching the thumbnails?
150 * @return bool
151 */
152 public function canCacheThumbs() {
153 return ( $this->apiThumbCacheExpiry > 0 && $this->apiThumbCacheDir );
154 }
155 }