| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types = 1 ); |
| 4 |
|
| 5 |
namespace Wikimedia\Parsoid\Config\Api; |
| 6 |
|
| 7 |
class ApiHelper { |
| 8 |
|
| 9 |
/** @var string */ |
| 10 |
private $endpoint; |
| 11 |
|
| 12 |
/** @var array */ |
| 13 |
private $curlopt; |
| 14 |
|
| 15 |
/** @var string */ |
| 16 |
private $cacheDir; |
| 17 |
|
| 18 |
/** @var bool|string */ |
| 19 |
private $writeToCache; |
| 20 |
|
| 21 |
/** |
| 22 |
* @param array $opts |
| 23 |
* - apiEndpoint: (string) URL for api.php. Required. |
| 24 |
* - apiTimeout: (int) Timeout, in sections. Default 60. |
| 25 |
* - userAgent: (string) User agent prefix. |
| 26 |
* - cacheDir: (string) If present, looks aside to the specified directory |
| 27 |
* for a cached response before making a network request. |
| 28 |
* - writeToCache: (bool|string) If present and truthy, writes successful |
| 29 |
* network requests to `cacheDir` so they can be reused. If set to |
| 30 |
* the string 'pretty', prettifies the JSON returned before writing it. |
| 31 |
*/ |
| 32 |
public function __construct( array $opts ) { |
| 33 |
if ( !isset( $opts['apiEndpoint'] ) ) { |
| 34 |
throw new \InvalidArgumentException( '$opts[\'apiEndpoint\'] must be set' ); |
| 35 |
} |
| 36 |
$this->endpoint = $opts['apiEndpoint']; |
| 37 |
|
| 38 |
$this->cacheDir = $opts['cacheDir'] ?? null; |
| 39 |
$this->writeToCache = $opts['writeToCache'] ?? false; |
| 40 |
|
| 41 |
$this->curlopt = [ |
| 42 |
CURLOPT_USERAGENT => trim( ( $opts['userAgent'] ?? '' ) . ' ApiEnv/1.0 Parsoid-PHP/0.1' ), |
| 43 |
CURLOPT_CONNECTTIMEOUT => $opts['apiTimeout'] ?? 60, |
| 44 |
CURLOPT_TIMEOUT => $opts['apiTimeout'] ?? 60, |
| 45 |
CURLOPT_FOLLOWLOCATION => false, |
| 46 |
CURLOPT_ENCODING => '', // Enable compression |
| 47 |
CURLOPT_SAFE_UPLOAD => true, |
| 48 |
CURLOPT_RETURNTRANSFER => true, |
| 49 |
]; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Make an API request |
| 54 |
* @param array $params API parameters |
| 55 |
* @return array API response data |
| 56 |
*/ |
| 57 |
public function makeRequest( array $params ): array { |
| 58 |
$filename = null; |
| 59 |
$params += [ 'formatversion' => 2 ]; |
| 60 |
if ( $this->cacheDir !== null ) { |
| 61 |
# sort the parameters for a repeatable filename |
| 62 |
ksort( $params ); |
| 63 |
$query = $this->endpoint . "?" . http_build_query( $params ); |
| 64 |
$queryHash = hash( 'sha256', $query ); |
| 65 |
$filename = $this->cacheDir . DIRECTORY_SEPARATOR . |
| 66 |
parse_url( $query, PHP_URL_HOST ) . '-' . |
| 67 |
substr( $queryHash, 0, 8 ); |
| 68 |
if ( file_exists( $filename ) ) { |
| 69 |
$res = file_get_contents( $filename ); |
| 70 |
$filename = null; // We don't need to write this back |
| 71 |
} else { |
| 72 |
$res = $this->makeCurlRequest( $params ); |
| 73 |
} |
| 74 |
} else { |
| 75 |
$res = $this->makeCurlRequest( $params ); |
| 76 |
} |
| 77 |
|
| 78 |
$data = json_decode( $res, true ); |
| 79 |
if ( !is_array( $data ) ) { |
| 80 |
throw new \RuntimeException( "HTTP request failed: Response was not a JSON array" ); |
| 81 |
} |
| 82 |
|
| 83 |
if ( isset( $data['error'] ) ) { |
| 84 |
$e = $data['error']; |
| 85 |
throw new \RuntimeException( "MediaWiki API error: [{$e['code']}] {$e['info']}" ); |
| 86 |
} |
| 87 |
|
| 88 |
if ( $filename && $this->writeToCache ) { |
| 89 |
if ( $this->writeToCache === 'pretty' ) { |
| 90 |
/* Prettify the results */ |
| 91 |
$dataPretty = [ |
| 92 |
'__endpoint__' => $this->endpoint, |
| 93 |
'__params__' => $params, |
| 94 |
] + $data; |
| 95 |
$res = json_encode( |
| 96 |
$dataPretty, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE |
| 97 |
); |
| 98 |
} |
| 99 |
file_put_contents( $filename, $res ); |
| 100 |
} |
| 101 |
|
| 102 |
return $data; |
| 103 |
} |
| 104 |
|
| 105 |
private function makeCurlRequest( array $params ): string { |
| 106 |
$ch = curl_init( $this->endpoint ); |
| 107 |
if ( !$ch ) { |
| 108 |
throw new \RuntimeException( "Failed to open curl handle to $this->endpoint" ); |
| 109 |
} |
| 110 |
|
| 111 |
$params['format'] = 'json'; |
| 112 |
$params['formatversion'] ??= '2'; |
| 113 |
|
| 114 |
$opts = [ |
| 115 |
CURLOPT_POST => true, |
| 116 |
CURLOPT_POSTFIELDS => $params, |
| 117 |
] + $this->curlopt; |
| 118 |
if ( !curl_setopt_array( $ch, $opts ) ) { |
| 119 |
throw new \RuntimeException( "Error setting curl options: " . curl_error( $ch ) ); |
| 120 |
} |
| 121 |
|
| 122 |
$res = curl_exec( $ch ); |
| 123 |
|
| 124 |
if ( curl_errno( $ch ) !== 0 ) { |
| 125 |
throw new \RuntimeException( "HTTP request failed: " . curl_error( $ch ) ); |
| 126 |
} |
| 127 |
|
| 128 |
$code = curl_getinfo( $ch, CURLINFO_RESPONSE_CODE ); |
| 129 |
if ( $code !== 200 ) { |
| 130 |
throw new \RuntimeException( "HTTP request failed: HTTP code $code" ); |
| 131 |
} |
| 132 |
|
| 133 |
if ( !$res ) { |
| 134 |
throw new \RuntimeException( "HTTP request failed: Empty response" ); |
| 135 |
} |
| 136 |
|
| 137 |
return $res; |
| 138 |
} |
| 139 |
} |