Simplify some API PHP code where possible

Change-Id: I663d9f2a25c70d8d265a3217cd1d0ad6fbb1bb3e
This commit is contained in:
Thiemo Kreuz 2021-08-26 11:37:13 +02:00
parent 6c33cc4a35
commit a1464bd617
2 changed files with 9 additions and 24 deletions

View file

@ -118,17 +118,8 @@ trait ApiParsoidTrait {
protected function requestRestbase(
Title $title, string $method, string $path, array $params, array $reqheaders = []
): array {
$request = [
'method' => $method,
'url' => '/restbase/local/v1/' . $path
];
if ( $method === 'GET' ) {
$request['query'] = $params;
} else {
$request['body'] = $params;
}
// Should be synchronised with modules/ve-mw/init/ve.init.mw.ArticleTargetLoader.js
$defaultReqHeaders = [
$reqheaders += [
'Accept' =>
'text/html; charset=utf-8; profile="https://www.mediawiki.org/wiki/Specs/HTML/2.0.0"',
'Accept-Language' => self::getPageLanguage( $title )->getCode(),
@ -136,20 +127,19 @@ trait ApiParsoidTrait {
'Api-User-Agent' => 'VisualEditor-MediaWiki/' . MW_VERSION,
'Promise-Non-Write-API-Action' => 'true',
];
// $reqheaders take precedence over $defaultReqHeaders
$request['headers'] = $reqheaders + $defaultReqHeaders;
$request = [
'method' => $method,
'url' => '/restbase/local/v1/' . $path,
( $method === 'GET' ? 'query' : 'body' ) => $params,
'headers' => $reqheaders,
];
$response = $this->getVRSClient()->run( $request );
if ( $response['code'] === 200 && $response['error'] === "" ) {
// If response was served directly from Varnish, use the response
// (RP) header to declare the cache hit and pass the data to the client.
$headers = $response['headers'];
$rp = null;
if ( isset( $headers['x-cache'] ) && strpos( $headers['x-cache'], 'hit' ) !== false ) {
$rp = 'cached-response=true';
}
if ( $rp !== null ) {
$resp = $this->getRequest()->response();
$resp->header( 'X-Cache: ' . $rp );
$this->getRequest()->response()->header( 'X-Cache: cached-response=true' );
}
} elseif ( $response['error'] !== '' ) {
$this->dieWithError(

View file

@ -81,14 +81,9 @@ class ApiVisualEditorEdit extends ApiBase {
'captchaid' => $params['captchaid'],
'captchaword' => $params['captchaword'],
'errorformat' => 'html',
( $params['minor'] !== null ? 'minor' : 'notminor' ) => true,
];
if ( $params['minor'] !== null ) {
$apiParams['minor'] = true;
} else {
$apiParams['notminor'] = true;
}
// Pass any unrecognized query parameters to the internal action=edit API request. This is
// necessary to support extensions that add extra stuff to the edit form (e.g. FlaggedRevs)
// and allows passing any other query parameters to be used for edit tagging (e.g. T209132).