Index: trunk/phase3/includes/filerepo/ForeignAPIRepo.php |
— | — | @@ -22,6 +22,11 @@ |
23 | 23 | * @ingroup FileRepo |
24 | 24 | */ |
25 | 25 | class ForeignAPIRepo extends FileRepo { |
| 26 | + /* This version string is used in the user agent for requests and will help |
| 27 | + * server maintainers in identify ForeignAPI usage. |
| 28 | + * Update the version every time you make breaking or significant changes. */ |
| 29 | + public static $foreignAPIRepoVersion = "2.0"; |
| 30 | + |
26 | 31 | var $fileFactory = array( 'ForeignAPIFile', 'newFromTitle' ); |
27 | 32 | /* Check back with Commons after a day */ |
28 | 33 | var $apiThumbCacheExpiry = 86400; |
— | — | @@ -146,7 +151,7 @@ |
147 | 152 | $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'Metadata', md5( $url ) ); |
148 | 153 | $data = $wgMemc->get( $key ); |
149 | 154 | if( !$data ) { |
150 | | - $data = Http::get( $url ); |
| 155 | + $data = self::httpGet( $url ); |
151 | 156 | if ( !$data ) { |
152 | 157 | return null; |
153 | 158 | } |
— | — | @@ -264,7 +269,7 @@ |
265 | 270 | } |
266 | 271 | /* There is a new Commons file, or existing thumbnail older than a month */ |
267 | 272 | } |
268 | | - $thumb = Http::get( $foreignUrl ); |
| 273 | + $thumb = self::httpGet( $foreignUrl ); |
269 | 274 | if( !$thumb ) { |
270 | 275 | wfDebug( __METHOD__ . " Could not download thumb\n" ); |
271 | 276 | return false; |
— | — | @@ -325,4 +330,37 @@ |
326 | 331 | public function canCacheThumbs() { |
327 | 332 | return ( $this->apiThumbCacheExpiry > 0 ); |
328 | 333 | } |
| 334 | + |
| 335 | + /** |
| 336 | + * The user agent the ForeignAPIRepo will use. |
| 337 | + */ |
| 338 | + public static function getUserAgent() { |
| 339 | + return Http::userAgent() . " ForeignAPIRepo/" . self::$foreignAPIRepoVersion; |
| 340 | + } |
| 341 | + |
| 342 | + /** |
| 343 | + * Like a Http:get request, but with custom User-Agent. |
| 344 | + * @see Http:get |
| 345 | + */ |
| 346 | + public static function httpGet( $url, $timeout = 'default', $options = array() ) { |
| 347 | + $options['timeout'] = $timeout; |
| 348 | + /* Http::get */ |
| 349 | + $url = wfExpandUrl( $url ); |
| 350 | + wfDebug( "ForeignAPIRepo: HTTP GET: $url\n" ); |
| 351 | + $options['method'] = "GET"; |
| 352 | + |
| 353 | + if ( !isset( $options['timeout'] ) ) { |
| 354 | + $options['timeout'] = 'default'; |
| 355 | + } |
| 356 | + |
| 357 | + $req = HttpRequest::factory( $url, $options ); |
| 358 | + $req->setUserAgent( ForeignAPIRepo::getUserAgent() ); |
| 359 | + $status = $req->execute(); |
| 360 | + |
| 361 | + if ( $status->isOK() ) { |
| 362 | + return $req->getContent(); |
| 363 | + } else { |
| 364 | + return false; |
| 365 | + } |
| 366 | + } |
329 | 367 | } |