From 73ec3a331e2a73d6bb191b14d999e43d994000f7 Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Tue, 3 May 2016 17:46:59 -0700 Subject: [PATCH] Use MultiHttpClient for server-side cross-wiki requests Change-Id: I6d647f250941a671b7b63890ef115380a22d38cc --- includes/api/ApiEchoNotifications.php | 50 +++++++++++---------------- 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/includes/api/ApiEchoNotifications.php b/includes/api/ApiEchoNotifications.php index 982f478a5..1217105f1 100644 --- a/includes/api/ApiEchoNotifications.php +++ b/includes/api/ApiEchoNotifications.php @@ -358,44 +358,34 @@ class ApiEchoNotifications extends ApiQueryBase { return array(); } - $multi = curl_multi_init(); - $curls = array(); + // Don't request cross-wiki notifications + unset( $params['notcrosswikisummary'] ); + $params['format'] = 'php'; + $reqs = array(); foreach ( $apis as $wiki => $api ) { - // only request data from that specific wiki, or they'd all spawn - // cross-wiki api requests... - $params['notwikis'] = $wiki; - unset( $params['notcrosswikisummary'] ); - - // get data in an easy-to-process format here - $params['format'] = 'php'; - - $curl = curl_init( $api['url'] ); - curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true ); - curl_setopt( $curl, CURLOPT_POSTFIELDS, $params ); - - curl_multi_add_handle( $multi, $curl ); - $curls[$wiki] = $curl; + $reqs[$wiki] = array( + 'method' => 'GET', + 'url' => $api['url'], + // Only request data from that specific wiki, or they'd all spawn + // cross-wiki api requests... + 'query' => array_merge( $params, array( 'notwikis' => $wiki ) ), + ); } - do { - curl_multi_exec( $multi, $running ); - curl_multi_select( $multi ); - } while ( $running > 0 ); + $http = new MultiHttpClient( array() ); + $responses = $http->runMulti( $reqs ); - $results = array(); - foreach ( $curls as $wiki => $curl ) { - $content = curl_multi_getcontent( $curl ); - curl_multi_remove_handle( $multi, $curl ); - - if ( $content !== null ) { - $results[$wiki] = unserialize( $content ); - $results[$wiki] = $results[$wiki]['query']['notifications']; + foreach ( $responses as $wiki => $response ) { + $statusCode = $response['response']['code']; + if ( $statusCode >= 200 && $statusCode <= 299 ) { + $parsed = unserialize( $response['response']['body'] ); + if ( $parsed ) { + $results[$wiki] = $parsed['query']['notifications']; + } } } - curl_multi_close( $multi ); - return $results; }