http://www.mediawiki.org/wiki/Special:Code/MediaWiki/76303

Revision: 76303
Author:   hartman
Date:     2010-11-08 14:39:08 +0000 (Mon, 08 Nov 2010)
Log Message:
-----------
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: trunk/phase3/includes/filerepo/ForeignAPIRepo.php
===================================================================
--- trunk/phase3/includes/filerepo/ForeignAPIRepo.php   2010-11-08 14:16:22 UTC 
(rev 76302)
+++ trunk/phase3/includes/filerepo/ForeignAPIRepo.php   2010-11-08 14:39:08 UTC 
(rev 76303)
@@ -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 @@
                        $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 @@
                                }
                                /* 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 @@
        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;
+               }
+       }
 }


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to