MediaWiki master
ActionModuleBasedHandler.php
Go to the documentation of this file.
1<?php
2
4
14use MediaWiki\Rest\Handler\Helper\RestStatusTrait;
19
25abstract class ActionModuleBasedHandler extends Handler {
26 use RestStatusTrait;
27
31 private $apiMain = null;
32
33 protected function getUser() {
34 return $this->getApiMain()->getUser();
35 }
36
40 public function setApiMain( ApiMain $apiMain ) {
41 $this->apiMain = $apiMain;
42 }
43
47 public function getApiMain() {
48 if ( $this->apiMain ) {
49 return $this->apiMain;
50 }
51
52 $context = RequestContext::getMain();
53 $session = $context->getRequest()->getSession();
54
55 // NOTE: This being a MediaWiki\Request\FauxRequest instance triggers special case behavior
56 // in ApiMain, causing ApiMain::isInternalMode() to return true. Among other things,
57 // this causes ApiMain to throw errors rather than encode them in the result data.
58 $fauxRequest = new FauxRequest( [], true, $session );
59 $fauxRequest->setSessionId( $session->getSessionId() );
60
61 $fauxContext = new RequestContext();
62 $fauxContext->setRequest( $fauxRequest );
63 $fauxContext->setUser( $context->getUser() );
64 $fauxContext->setLanguage( $context->getLanguage() );
65
66 $this->apiMain = new ApiMain( $fauxContext, true );
67 return $this->apiMain;
68 }
69
77 public function overrideActionModule( string $name, string $group, ApiBase $module ) {
78 $this->getApiMain()->getModuleManager()->addModule(
79 $name,
80 $group,
81 [
82 'class' => get_class( $module ),
83 'factory' => static function () use ( $module ) {
84 return $module;
85 }
86 ]
87 );
88 }
89
99 public function execute() {
100 $apiMain = $this->getApiMain();
101
103 $request = $apiMain->getRequest();
104
105 foreach ( $params as $key => $value ) {
106 $request->setVal( $key, $value );
107 }
108
109 try {
110 // NOTE: ApiMain detects this to be an internal call, so it will throw
111 // ApiUsageException rather than putting error messages into the result.
112 $apiMain->execute();
113 } catch ( ApiUsageException $ex ) {
114 // use a fake loop to throw the first error
115 foreach ( $ex->getStatusValue()->getMessages( 'error' ) as $msg ) {
116 $msg = ApiMessage::create( $msg );
117 $this->throwHttpExceptionForActionModuleError( $msg, $ex->getCode() ?: 400 );
118 }
119
120 // This should never happen, since ApiUsageExceptions should always
121 // have errors in their Status object.
122 throw new LocalizedHttpException( new MessageValue( "rest-unmapped-action-error", [ $ex->getMessage() ] ),
123 $ex->getCode()
124 );
125 }
126
127 $actionModuleResult = $apiMain->getResult()->getResultData( null, [ 'Strip' => 'all' ] );
128
129 // construct result
130 $resultData = $this->mapActionModuleResult( $actionModuleResult );
131
132 $response = $this->getResponseFactory()->createFromReturnValue( $resultData );
133
135 $apiMain->getRequest()->response(),
136 $actionModuleResult,
137 $response
138 );
139
140 return $response;
141 }
142
152 abstract protected function getActionModuleParameters();
153
162 abstract protected function mapActionModuleResult( array $data );
163
180 protected function mapActionModuleResponse(
181 WebResponse $actionModuleResponse,
182 array $actionModuleResult,
183 Response $response
184 ) {
185 // TODO: map status, headers, cookies, etc
186 }
187
206 protected function throwHttpExceptionForActionModuleError( IApiMessage $msg, $statusCode = 400 ) {
207 // override to supply mappings
208
209 throw new LocalizedHttpException(
210 MessageValue::newFromSpecifier( $msg ),
211 $statusCode,
212 // Include the original error code in the response.
213 // This makes it easier to track down the original cause of the error,
214 // and allows more specific mappings to be added to
215 // implementations of throwHttpExceptionForActionModuleError() provided by
216 // subclasses
217 [ 'actionModuleErrorCode' => $msg->getApiCode() ]
218 );
219 }
220
221}
array $params
The job parameters.
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:75
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:78
getResult()
Get the ApiResult object associated with current request.
Definition ApiMain.php:718
execute()
Execute api request.
Definition ApiMain.php:914
Extension of Message implementing IApiMessage.
Exception used to abort API execution with an error.
getStatusValue()
Fetch the error status.
Group all the pieces relevant to the context of a request into one instance.
WebRequest clone which takes values from a provided array.
Allow programs to request this object from WebRequest::response() and handle all outputting (or lack ...
Base class for REST handlers that are implemented by mapping to an existing ApiModule.
setApiMain(ApiMain $apiMain)
Set main action API entry point for testing.
overrideActionModule(string $name, string $group, ApiBase $module)
Overrides an action API module.
throwHttpExceptionForActionModuleError(IApiMessage $msg, $statusCode=400)
Throws a HttpException for a given IApiMessage that represents an error.
mapActionModuleResponse(WebResponse $actionModuleResponse, array $actionModuleResult, Response $response)
Transfers relevant information, such as header values, from the WebResponse constructed by the action...
execute()
Main execution method, implemented to delegate execution to ApiMain.
mapActionModuleResult(array $data)
Maps an action API result to a REST API result.
getActionModuleParameters()
Maps a REST API request to an action API request.
Base class for REST route handlers.
Definition Handler.php:25
getResponseFactory()
Get the ResponseFactory which can be used to generate Response objects.
Definition Handler.php:354
This is the base exception class for non-fatal exceptions thrown from REST handlers.
Value object representing a message for i18n.
Interface for messages with machine-readable data for use by the API.
getApiCode()
Returns a machine-readable code for use by the API.
Copyright (C) 2011-2020 Wikimedia Foundation and others.