MediaWiki  master
CreationHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Rest\Handler;
4 
9 use WebResponse;
12 
17 
21  protected function getTitleParameter() {
22  return $this->getValidatedBody()['title'];
23  }
24 
28  public function getBodyValidator( $contentType ) {
29  if ( $contentType !== 'application/json' ) {
30  throw new HttpException( "Unsupported Content-Type",
31  415,
32  [ 'content_type' => $contentType ]
33  );
34  }
35 
36  return new JsonBodyValidator( [
37  'source' => [
38  self::PARAM_SOURCE => 'body',
39  ParamValidator::PARAM_TYPE => 'string',
40  ParamValidator::PARAM_REQUIRED => true,
41  ],
42  'title' => [
43  self::PARAM_SOURCE => 'body',
44  ParamValidator::PARAM_TYPE => 'string',
45  ParamValidator::PARAM_REQUIRED => true,
46  ],
47  'comment' => [
48  self::PARAM_SOURCE => 'body',
49  ParamValidator::PARAM_TYPE => 'string',
50  ParamValidator::PARAM_REQUIRED => true,
51  ],
52  'content_model' => [
53  self::PARAM_SOURCE => 'body',
54  ParamValidator::PARAM_TYPE => 'string',
55  ParamValidator::PARAM_REQUIRED => false,
56  ],
57  'token' => [
58  self::PARAM_SOURCE => 'body',
59  ParamValidator::PARAM_TYPE => 'string',
60  ParamValidator::PARAM_REQUIRED => false,
61  ParamValidator::PARAM_DEFAULT => '',
62  ],
63  ] );
64  }
65 
69  protected function getActionModuleParameters() {
70  $body = $this->getValidatedBody();
71 
72  $title = $this->getTitleParameter();
73 
74  $contentmodel = $body['content_model'] ?: null;
75 
76  if ( $contentmodel !== null && !$this->contentHandlerFactory->isDefinedModel( $contentmodel ) ) {
77  throw new LocalizedHttpException(
78  new MessageValue( 'rest-bad-content-model', [ $body['content_model'] ] ), 400
79  );
80  }
81 
82  $token = $this->getActionModuleToken();
83 
84  $params = [
85  'action' => 'edit',
86  'title' => $title,
87  'text' => $body['source'],
88  'summary' => $body['comment'],
89  'token' => $token,
90  'createonly' => true,
91  ];
92 
93  if ( $contentmodel !== null ) {
94  $params['contentmodel'] = $contentmodel;
95  }
96 
97  return $params;
98  }
99 
100  protected function mapActionModuleResponse(
101  WebResponse $actionModuleResponse,
102  array $actionModuleResult,
104  ) {
105  parent::mapActionModuleResponse(
106  $actionModuleResponse,
107  $actionModuleResult,
108  $response
109  );
110 
111  $title = urlencode( $this->getTitleParameter() );
112  $url = $this->getRouter()->getRouteUrl( '/coredev/v0/page/' . $title );
113  $response->setHeader( 'Location', $url );
114  }
115 
116 }
MediaWiki\Rest\Handler
Definition: ActionModuleBasedHandler.php:3
$response
$response
Definition: opensearch_desc.php:44
MediaWiki\Rest\Validator\JsonBodyValidator
Do-nothing body validator.
Definition: JsonBodyValidator.php:14
MediaWiki\Rest\Handler\EditHandler\getActionModuleToken
getActionModuleToken()
Determines the CSRF token to be passed to the action module.
Definition: EditHandler.php:166
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
Definition: MessageValue.php:14
MediaWiki\Rest\Handler\CreationHandler\getBodyValidator
getBodyValidator( $contentType)
Fetch the BodyValidator.Content type of the request. BodyValidator
Definition: CreationHandler.php:28
MediaWiki\Rest\Response\setHeader
setHeader( $name, $value)
Set or replace the specified header.
Definition: Response.php:80
MediaWiki\Rest\Handler\getValidatedBody
getValidatedBody()
Fetch the validated body.
Definition: Handler.php:212
MediaWiki\Rest\Response
Definition: Response.php:8
$title
$title
Definition: testCompression.php:38
MediaWiki\Rest\Handler\EditHandler
Base class for REST API handlers that perform page edits (main slot only).
Definition: EditHandler.php:21
MediaWiki\Rest\Handler\CreationHandler\mapActionModuleResponse
mapActionModuleResponse(WebResponse $actionModuleResponse, array $actionModuleResult, Response $response)
Transfers relevant information, such as header values, from the WebResponse constructed by the action...
Definition: CreationHandler.php:100
MediaWiki\Rest\HttpException
This is the base exception class for non-fatal exceptions thrown from REST handlers.
Definition: HttpException.php:10
MediaWiki\Rest\Handler\CreationHandler
Core REST API endpoint that handles page creation (main slot only)
Definition: CreationHandler.php:16
MediaWiki\Rest\Handler\CreationHandler\getTitleParameter
getTitleParameter()
Returns the requested title.string
Definition: CreationHandler.php:21
MediaWiki\Rest\Handler\CreationHandler\getActionModuleParameters
getActionModuleParameters()
Maps a REST API request to an action API request.Implementations typically use information returned b...
Definition: CreationHandler.php:69
WebResponse
Allow programs to request this object from WebRequest::response() and handle all outputting (or lack ...
Definition: WebResponse.php:28
MediaWiki\Rest\Handler\getRouter
getRouter()
Get the Router.
Definition: Handler.php:72
Wikimedia\ParamValidator\ParamValidator
Service for formatting and validating API parameters.
Definition: ParamValidator.php:42
MediaWiki\Rest\LocalizedHttpException
Definition: LocalizedHttpException.php:7