9use InvalidArgumentException;
15use Psr\Log\LoggerInterface;
38 private readonly LoggerInterface $logger,
41 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
78 public function create(
$url, array $options = [], $caller = __METHOD__ ) {
79 if ( !isset( $options[
'logger'] ) ) {
80 $options[
'logger'] = $this->logger;
82 $options[
'timeout'] = $this->normalizeTimeout(
83 $options[
'timeout'] ??
null,
84 $options[
'maxTimeout'] ??
null,
88 $options[
'connectTimeout'] = $this->normalizeTimeout(
89 $options[
'connectTimeout'] ??
null,
90 $options[
'maxConnectTimeout'] ??
null,
95 if ( $this->telemetry ) {
96 $client->addTelemetry( $this->telemetry );
111 private function normalizeTimeout( $parameter, $maxParameter, $default, $maxConfigured ) {
112 if ( $parameter ===
'default' || $parameter ===
null ) {
113 if ( !is_numeric( $default ) ) {
114 throw new InvalidArgumentException(
115 '$wgHTTPTimeout and $wgHTTPConnectTimeout must be set to a number' );
121 $max = $maxParameter ?? $maxConfigured;
122 if ( $max && $value > $max ) {
135 return function_exists(
'curl_init' ) ||
wfIniGetBool(
'allow_url_fopen' );
149 public function request( $method,
$url, array $options = [], $caller = __METHOD__ ) {
150 $logger = LoggerFactory::getInstance(
'http' );
151 $logger->debug(
"$method: $url" );
153 $options[
'method'] = strtoupper( $method );
155 $req = $this->
create(
$url, $options, $caller );
156 $status = $req->execute();
158 if ( $status->isOK() ) {
159 return $req->getContent();
161 $errors = array_map(
static fn ( $msg ) => $msg->getKey(), $status->getMessages(
'error' ) );
162 $logger->warning( Status::wrap( $status )->getWikiText(
false,
false,
'en' ),
163 [
'error' => $errors,
'caller' => $caller,
'content' => $req->getContent() ] );
177 public function get(
$url, array $options = [], $caller = __METHOD__ ) {
178 return $this->
request(
'GET',
$url, $options, $caller );
190 public function post(
$url, array $options = [], $caller = __METHOD__ ) {
191 return $this->
request(
'POST',
$url, $options, $caller );
202 return 'MediaWiki/' .
MW_VERSION .
" ($contact)";
218 $options[
'reqTimeout'] = $this->normalizeTimeout(
219 $options[
'reqTimeout'] ?? $options[
'timeout'] ??
null,
220 $options[
'maxReqTimeout'] ?? $options[
'maxTimeout'] ??
null,
224 $options[
'connTimeout'] = $this->normalizeTimeout(
225 $options[
'connTimeout'] ?? $options[
'connectTimeout'] ??
null,
226 $options[
'maxConnTimeout'] ?? $options[
'maxConnectTimeout'] ??
null,
235 'logger' => $this->logger,
257 $config[
'timeout'] = $this->normalizeTimeout(
258 $config[
'timeout'] ?? null,
259 $config[
'maxTimeout'] ?? null,
264 $config[
'connect_timeout'] = $this->normalizeTimeout(
265 $config[
'connect_timeout'] ??
null,
266 $config[
'maxConnectTimeout'] ??
null,
271 if ( !isset( $config[
'headers'][
'User-Agent'] ) ) {
272 $config[
'headers'][
'User-Agent'] = $this->
getUserAgent();
274 if ( $this->telemetry ) {
275 $config[
'headers'] = array_merge(
276 $this->telemetry->getRequestHeaders(), $config[
'headers']
280 return new Client( $config );
const MW_VERSION
The running version of MediaWiki.
wfIniGetBool( $setting)
Safety wrapper around ini_get() for boolean settings.
A class containing constants representing the names of configuration variables.
const HTTPUserAgentContact
Name constant for the HTTPUserAgentContact setting, for use with Config::get()
const HTTPConnectTimeout
Name constant for the HTTPConnectTimeout setting, for use with Config::get()
const HTTPTimeout
Name constant for the HTTPTimeout setting, for use with Config::get()
const LocalVirtualHosts
Name constant for the LocalVirtualHosts setting, for use with Config::get()
const HTTPMaxConnectTimeout
Name constant for the HTTPMaxConnectTimeout setting, for use with Config::get()
const HTTPMaxTimeout
Name constant for the HTTPMaxTimeout setting, for use with Config::get()
const LocalHTTPProxy
Name constant for the LocalHTTPProxy setting, for use with Config::get()