From: Derk-Jan Hartman Date: Mon, 8 Nov 2010 14:39:08 +0000 (+0000) Subject: Add a version number and user-agent string to ForeignAPIRepo. X-Git-Tag: 1.31.0-rc.0~33993 X-Git-Url: http://git.cyclocoop.org/data/%7B%24admin_url%7Dconfig?a=commitdiff_plain;h=d2e2764bb1216fb8ed5f2f271a30963102ed814e;p=lhc%2Fweb%2Fwiklou.git Add a version number and user-agent string to ForeignAPIRepo. Will identify as "MediaWiki/version ForeignAPIRepo/version" Current version is 2.0 We could consider adding the known cache times in a UA comment section. Might help isolating abusive usage? --- diff --git a/includes/filerepo/ForeignAPIRepo.php b/includes/filerepo/ForeignAPIRepo.php index d850bdb15d..512c6cb11a 100644 --- a/includes/filerepo/ForeignAPIRepo.php +++ b/includes/filerepo/ForeignAPIRepo.php @@ -22,6 +22,11 @@ * @ingroup FileRepo */ class ForeignAPIRepo extends FileRepo { + /* This version string is used in the user agent for requests and will help + * server maintainers in identify ForeignAPI usage. + * Update the version every time you make breaking or significant changes. */ + public static $foreignAPIRepoVersion = "2.0"; + var $fileFactory = array( 'ForeignAPIFile', 'newFromTitle' ); /* Check back with Commons after a day */ var $apiThumbCacheExpiry = 86400; @@ -146,7 +151,7 @@ class ForeignAPIRepo extends FileRepo { $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'Metadata', md5( $url ) ); $data = $wgMemc->get( $key ); if( !$data ) { - $data = Http::get( $url ); + $data = self::httpGet( $url ); if ( !$data ) { return null; } @@ -264,7 +269,7 @@ class ForeignAPIRepo extends FileRepo { } /* There is a new Commons file, or existing thumbnail older than a month */ } - $thumb = Http::get( $foreignUrl ); + $thumb = self::httpGet( $foreignUrl ); if( !$thumb ) { wfDebug( __METHOD__ . " Could not download thumb\n" ); return false; @@ -325,4 +330,37 @@ class ForeignAPIRepo extends FileRepo { public function canCacheThumbs() { return ( $this->apiThumbCacheExpiry > 0 ); } + + /** + * The user agent the ForeignAPIRepo will use. + */ + public static function getUserAgent() { + return Http::userAgent() . " ForeignAPIRepo/" . self::$foreignAPIRepoVersion; + } + + /** + * Like a Http:get request, but with custom User-Agent. + * @see Http:get + */ + public static function httpGet( $url, $timeout = 'default', $options = array() ) { + $options['timeout'] = $timeout; + /* Http::get */ + $url = wfExpandUrl( $url ); + wfDebug( "ForeignAPIRepo: HTTP GET: $url\n" ); + $options['method'] = "GET"; + + if ( !isset( $options['timeout'] ) ) { + $options['timeout'] = 'default'; + } + + $req = HttpRequest::factory( $url, $options ); + $req->setUserAgent( ForeignAPIRepo::getUserAgent() ); + $status = $req->execute(); + + if ( $status->isOK() ) { + return $req->getContent(); + } else { + return false; + } + } }