// Printer may not be initialized if the extractRequestParams() fails for the main module
$this->createErrorPrinter();
+ // Get desired HTTP code from an ApiUsageException. Don't use codes from other
+ // exception types, as they are unlikely to be intended as an HTTP code.
+ $httpCode = $e instanceof ApiUsageException ? $e->getCode() : 0;
+
$failed = false;
try {
- $this->printResult( $e->getCode() );
+ $this->printResult( $httpCode );
} catch ( ApiUsageException $ex ) {
// The error printer itself is failing. Try suppressing its request
// parameters and redo.
$this->mPrinter = null;
$this->createErrorPrinter();
$this->mPrinter->forceDefaultParams();
- if ( $e->getCode() ) {
+ if ( $httpCode ) {
$response->statusHeader( 200 ); // Reset in case the fallback doesn't want a non-200
}
- $this->printResult( $e->getCode() );
+ $this->printResult( $httpCode );
}
}
return;
}
- $useKeyHeader = $config->get( 'UseKeyHeader' );
if ( $this->mCacheMode == 'anon-public-user-private' ) {
$out->addVaryHeader( 'Cookie' );
$response->header( $out->getVaryHeader() );
- if ( $useKeyHeader ) {
- $response->header( $out->getKeyHeader() );
- if ( $out->haveCacheVaryCookies() ) {
- // Logged in, mark this request private
- $response->header( "Cache-Control: $privateCache" );
- return;
- }
- // Logged out, send normal public headers below
- } elseif ( MediaWiki\Session\SessionManager::getGlobalSession()->isPersistent() ) {
+ if ( MediaWiki\Session\SessionManager::getGlobalSession()->isPersistent() ) {
// Logged in or otherwise has session (e.g. anonymous users who have edited)
// Mark request private
$response->header( "Cache-Control: $privateCache" );
return;
- } // else no Key and anonymous, send public headers below
+ } // else anonymous, send public headers below
}
// Send public headers
$response->header( $out->getVaryHeader() );
- if ( $useKeyHeader ) {
- $response->header( $out->getKeyHeader() );
- }
// If nobody called setCacheMaxAge(), use the (s)maxage parameters
if ( !isset( $this->mCacheControl['s-maxage'] ) ) {
*/
protected function logRequest( $time, $e = null ) {
$request = $this->getRequest();
- $legacyLogCtx = [
- 'ts' => time(),
- 'ip' => $request->getIP(),
- 'userAgent' => $this->getUserAgent(),
- 'wiki' => WikiMap::getCurrentWikiDbDomain()->getId(),
- 'timeSpentBackend' => (int)round( $time * 1000 ),
- 'hadError' => $e !== null,
- 'errorCodes' => [],
- 'params' => [],
- ];
$logCtx = [
+ // https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/event-schemas/+/master/jsonschema/mediawiki/api/request
'$schema' => '/mediawiki/api/request/0.0.1',
'meta' => [
'request_id' => WebRequest::getRequestId(),
- 'id' => UIDGenerator::newUUIDv1(),
+ 'id' => UIDGenerator::newUUIDv4(),
'dt' => wfTimestamp( TS_ISO_8601 ),
'domain' => $this->getConfig()->get( 'ServerName' ),
+ // If using the EventBus extension (as intended) with this log channel,
+ // this stream name will map to a Kafka topic.
'stream' => 'mediawiki.api-request'
],
'http' => [
if ( $e ) {
$logCtx['api_error_codes'] = [];
foreach ( $this->errorMessagesFromException( $e ) as $msg ) {
- $legacyLogCtx['errorCodes'][] = $msg->getApiCode();
$logCtx['api_error_codes'][] = $msg->getApiCode();
}
}
// Construct space separated message for 'api' log channel
$msg = "API {$request->getMethod()} " .
wfUrlencode( str_replace( ' ', '_', $this->getUser()->getName() ) ) .
- " {$legacyLogCtx['ip']} " .
- "T={$legacyLogCtx['timeSpentBackend']}ms";
+ " {$logCtx['http']['client_ip']} " .
+ "T={$logCtx['backend_time_ms']}ms";
$sensitive = array_flip( $this->getSensitiveParams() );
foreach ( $this->getParamsUsed() as $name ) {
$encValue = $this->encodeRequestLogValue( $value );
}
- $legacyLogCtx['params'][$name] = $value;
$logCtx['params'][$name] = $value;
$msg .= " {$name}={$encValue}";
}
+ // Log an unstructured message to the api channel.
wfDebugLog( 'api', $msg, 'private' );
- // ApiAction channel is for structured data consumers.
- // The ApiAction was using logging channel is deprecated and is replaced
- // by the api-request channel.
- wfDebugLog( 'ApiAction', '', 'private', $legacyLogCtx );
+
+ // The api-request channel a structured data log channel.
wfDebugLog( 'api-request', '', 'private', $logCtx );
}