X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Flibs%2FMultiHttpClient.php;h=e9922b9edf88e3c475a043690e57ebacab2117ca;hb=3cb14f56bdf3271769a5866f9dcaad56bf873aea;hp=a87020496343fb23a406d9b6e0e970f0140b83e9;hpb=1d95c68977ef49738eb0fc9c3701afe06467fbeb;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/libs/MultiHttpClient.php b/includes/libs/MultiHttpClient.php index a870204963..e9922b9edf 100644 --- a/includes/libs/MultiHttpClient.php +++ b/includes/libs/MultiHttpClient.php @@ -20,6 +20,10 @@ * @file */ +use Psr\Log\LoggerAwareInterface; +use Psr\Log\LoggerInterface; +use Psr\Log\NullLogger; + /** * Class to handle concurrent HTTP requests * @@ -42,7 +46,7 @@ * @author Aaron Schulz * @since 1.23 */ -class MultiHttpClient { +class MultiHttpClient implements LoggerAwareInterface { /** @var resource */ protected $multiHandle = null; // curl_multi handle /** @var string|null SSL certificates path */ @@ -59,6 +63,8 @@ class MultiHttpClient { protected $proxy; /** @var string */ protected $userAgent = 'wikimedia/multi-http-client v1.0'; + /** @var LoggerInterface */ + protected $logger; /** * @param array $options @@ -78,13 +84,17 @@ class MultiHttpClient { } } static $opts = [ - 'connTimeout', 'reqTimeout', 'usePipelining', 'maxConnsPerHost', 'proxy', 'userAgent' + 'connTimeout', 'reqTimeout', 'usePipelining', 'maxConnsPerHost', + 'proxy', 'userAgent', 'logger' ]; foreach ( $opts as $key ) { if ( isset( $options[$key] ) ) { $this->$key = $options[$key]; } } + if ( $this->logger === null ) { + $this->logger = new NullLogger; + } } /** @@ -162,6 +172,7 @@ class MultiHttpClient { } elseif ( !isset( $req['url'] ) ) { throw new Exception( "Request has no 'url' field set." ); } + $this->logger->debug( "{$req['method']}: {$req['url']}" ); $req['query'] = isset( $req['query'] ) ? $req['query'] : []; $headers = []; // normalized headers if ( isset( $req['headers'] ) ) { @@ -215,7 +226,7 @@ class MultiHttpClient { // Wait (if possible) for available work... if ( $active > 0 && $mrc == CURLM_OK ) { if ( curl_multi_select( $chm, 10 ) == -1 ) { - // PHP bug 63411; http://curl.haxx.se/libcurl/c/curl_multi_fdset.html + // PHP bug 63411; https://curl.haxx.se/libcurl/c/curl_multi_fdset.html usleep( 5000 ); // 5ms } } @@ -235,6 +246,8 @@ class MultiHttpClient { if ( function_exists( 'curl_strerror' ) ) { $req['response']['error'] .= " " . curl_strerror( $errno ); } + $this->logger->warning( "Error fetching URL \"{$req['url']}\": " . + $req['response']['error'] ); } } else { $req['response']['error'] = "(curl error: no status set)"; @@ -420,6 +433,15 @@ class MultiHttpClient { return $this->multiHandle; } + /** + * Register a logger + * + * @param LoggerInterface + */ + public function setLogger( LoggerInterface $logger ) { + $this->logger = $logger; + } + function __destruct() { if ( $this->multiHandle ) { curl_multi_close( $this->multiHandle );