MediaWiki  1.34.0
HttpRequestFactory.php
Go to the documentation of this file.
1 <?php
20 namespace MediaWiki\Http;
21 
22 use CurlHttpRequest;
24 use Http;
26 use MWHttpRequest;
27 use PhpHttpRequest;
28 use Profiler;
29 use RuntimeException;
30 use Status;
31 
69  public function create( $url, array $options = [], $caller = __METHOD__ ) {
70  if ( !Http::$httpEngine ) {
71  Http::$httpEngine = 'guzzle';
72  }
73 
74  if ( !isset( $options['logger'] ) ) {
75  $options['logger'] = LoggerFactory::getInstance( 'http' );
76  }
77 
78  switch ( Http::$httpEngine ) {
79  case 'guzzle':
80  return new GuzzleHttpRequest( $url, $options, $caller, Profiler::instance() );
81  case 'curl':
82  return new CurlHttpRequest( $url, $options, $caller, Profiler::instance() );
83  case 'php':
84  return new PhpHttpRequest( $url, $options, $caller, Profiler::instance() );
85  default:
86  throw new RuntimeException( __METHOD__ . ': The requested engine is not valid.' );
87  }
88  }
89 
95  public function canMakeRequests() {
96  return function_exists( 'curl_init' ) || wfIniGetBool( 'allow_url_fopen' );
97  }
98 
110  public function request( $method, $url, array $options = [], $caller = __METHOD__ ) {
111  $logger = LoggerFactory::getInstance( 'http' );
112  $logger->debug( "$method: $url" );
113 
114  $options['method'] = strtoupper( $method );
115 
116  if ( !isset( $options['timeout'] ) ) {
117  $options['timeout'] = 'default';
118  }
119  if ( !isset( $options['connectTimeout'] ) ) {
120  $options['connectTimeout'] = 'default';
121  }
122 
123  $req = $this->create( $url, $options, $caller );
124  $status = $req->execute();
125 
126  if ( $status->isOK() ) {
127  return $req->getContent();
128  } else {
129  $errors = $status->getErrorsByType( 'error' );
130  $logger->warning( Status::wrap( $status )->getWikiText( false, false, 'en' ),
131  [ 'error' => $errors, 'caller' => $caller, 'content' => $req->getContent() ] );
132  return null;
133  }
134  }
135 
145  public function get( $url, array $options = [], $caller = __METHOD__ ) {
146  return $this->request( 'GET', $url, $options, $caller );
147  }
148 
158  public function post( $url, array $options = [], $caller = __METHOD__ ) {
159  return $this->request( 'POST', $url, $options, $caller );
160  }
161 
165  public function getUserAgent() {
166  global $wgVersion;
167 
168  return "MediaWiki/$wgVersion";
169  }
170 }
Http\$httpEngine
static $httpEngine
Definition: Http.php:31
Profiler\instance
static instance()
Singleton.
Definition: Profiler.php:63
MediaWiki\Http\HttpRequestFactory\canMakeRequests
canMakeRequests()
Simple function to test if we can make any sort of requests at all, using cURL or fopen()
Definition: HttpRequestFactory.php:95
MediaWiki\Logger\LoggerFactory\getInstance
static getInstance( $channel)
Get a named logger instance from the currently configured logger factory.
Definition: LoggerFactory.php:92
MediaWiki\Http\HttpRequestFactory
Factory creating MWHttpRequest objects.
Definition: HttpRequestFactory.php:35
$wgVersion
$wgVersion
MediaWiki version number.
Definition: DefaultSettings.php:75
MediaWiki\Http
Definition: HttpRequestFactory.php:20
CurlHttpRequest
MWHttpRequest implemented using internal curl compiled into PHP.
Definition: CurlHttpRequest.php:24
MediaWiki\Http\HttpRequestFactory\getUserAgent
getUserAgent()
Definition: HttpRequestFactory.php:165
Status
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:40
MediaWiki\Http\HttpRequestFactory\request
request( $method, $url, array $options=[], $caller=__METHOD__)
Perform an HTTP request.
Definition: HttpRequestFactory.php:110
MediaWiki\Logger\LoggerFactory
PSR-3 logger instance factory.
Definition: LoggerFactory.php:45
Status\wrap
static wrap( $sv)
Succinct helper method to wrap a StatusValue.
Definition: Status.php:55
GuzzleHttpRequest
MWHttpRequest implemented using the Guzzle library.
Definition: GuzzleHttpRequest.php:39
Profiler
Profiler base class that defines the interface and some trivial functionality.
Definition: Profiler.php:33
MWHttpRequest
This wrapper class will call out to curl (if available) or fallback to regular PHP if necessary for h...
Definition: MWHttpRequest.php:32
wfIniGetBool
wfIniGetBool( $setting)
Safety wrapper around ini_get() for boolean settings.
Definition: GlobalFunctions.php:2062
$status
return $status
Definition: SyntaxHighlight.php:347
MediaWiki\Http\HttpRequestFactory\post
post( $url, array $options=[], $caller=__METHOD__)
Simple wrapper for request( 'POST' ), parameters have same meaning as for request()
Definition: HttpRequestFactory.php:158
PhpHttpRequest
Definition: PhpHttpRequest.php:21
MediaWiki\Http\HttpRequestFactory\create
create( $url, array $options=[], $caller=__METHOD__)
Generate a new MWHttpRequest object.
Definition: HttpRequestFactory.php:69
Http
Various HTTP related functions.
Definition: Http.php:29