r76303 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r76302‎ | r76303 | r76304 >
Date:14:39, 8 November 2010
Author:hartman
Status:ok (Comments)
Tags:
Comment:
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?
Modified paths:
  • /trunk/phase3/includes/filerepo/ForeignAPIRepo.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/filerepo/ForeignAPIRepo.php
@@ -22,6 +22,11 @@
2323 * @ingroup FileRepo
2424 */
2525 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+
2631 var $fileFactory = array( 'ForeignAPIFile', 'newFromTitle' );
2732 /* Check back with Commons after a day */
2833 var $apiThumbCacheExpiry = 86400;
@@ -146,7 +151,7 @@
147152 $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'Metadata', md5( $url ) );
148153 $data = $wgMemc->get( $key );
149154 if( !$data ) {
150 - $data = Http::get( $url );
 155+ $data = self::httpGet( $url );
151156 if ( !$data ) {
152157 return null;
153158 }
@@ -264,7 +269,7 @@
265270 }
266271 /* There is a new Commons file, or existing thumbnail older than a month */
267272 }
268 - $thumb = Http::get( $foreignUrl );
 273+ $thumb = self::httpGet( $foreignUrl );
269274 if( !$thumb ) {
270275 wfDebug( __METHOD__ . " Could not download thumb\n" );
271276 return false;
@@ -325,4 +330,37 @@
326331 public function canCacheThumbs() {
327332 return ( $this->apiThumbCacheExpiry > 0 );
328333 }
 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+ }
329367 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r76307Followup r76303. Use a less verbose const VERSION.hartman15:14, 8 November 2010

Comments

#Comment by 😂 (talk | contribs)   15:01, 8 November 2010

ForeignAPIRepo::$foreignAPIRepoVersion is a little verbose. Why not make it a const: ForeignAPIRepo::VERSION?

#Comment by 😂 (talk | contribs)   15:17, 8 November 2010

Looks good, thanks. With these user agents we can actually start getting some sort of usage numbers on this \o/

Status & tagging log