MediaWiki  master
CreationHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Rest\Handler;
4 
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',
41  ],
42  'title' => [
43  self::PARAM_SOURCE => 'body',
44  ParamValidator::PARAM_TYPE => 'string',
46  ],
47  'comment' => [
48  self::PARAM_SOURCE => 'body',
49  ParamValidator::PARAM_TYPE => 'string',
51  ],
52  'content_model' => [
53  self::PARAM_SOURCE => 'body',
54  ParamValidator::PARAM_TYPE => 'string',
56  ],
57  ] + $this->getTokenParamDefinition() );
58  }
59 
63  protected function getActionModuleParameters() {
64  $body = $this->getValidatedBody();
65 
66  $title = $this->getTitleParameter();
67 
68  $contentmodel = $body['content_model'] ?: null;
69 
70  if ( $contentmodel !== null && !$this->contentHandlerFactory->isDefinedModel( $contentmodel ) ) {
71  throw new LocalizedHttpException(
72  new MessageValue( 'rest-bad-content-model', [ $body['content_model'] ] ), 400
73  );
74  }
75 
76  $token = $this->getToken() ?? $this->getUser()->getEditToken();
77 
78  $params = [
79  'action' => 'edit',
80  'title' => $title,
81  'text' => $body['source'],
82  'summary' => $body['comment'],
83  'token' => $token,
84  'createonly' => true,
85  ];
86 
87  if ( $contentmodel !== null ) {
88  $params['contentmodel'] = $contentmodel;
89  }
90 
91  return $params;
92  }
93 
94  protected function mapActionModuleResponse(
95  WebResponse $actionModuleResponse,
96  array $actionModuleResult,
97  Response $response
98  ) {
99  parent::mapActionModuleResponse(
100  $actionModuleResponse,
101  $actionModuleResult,
102  $response
103  );
104 
105  $title = $this->urlEncodeTitle( $actionModuleResult['edit']['title'] );
106 
107  $url = $this->getRouter()->getRouteUrl( '/v1/page/' . $title );
108  $response->setHeader( 'Location', $url );
109  }
110 
111 }
getTokenParamDefinition()
Returns the definition for the token parameter, to be used in getBodyValidator().
getToken()
Determines the CSRF token to be used, possibly taking it from a request parameter.
Allow programs to request this object from WebRequest::response() and handle all outputting (or lack ...
Definition: WebResponse.php:36
Core REST API endpoint that handles page creation (main slot only)
getBodyValidator( $contentType)
Fetch the BodyValidator.Stability: stableto overrideContent type of the request. BodyValidator
getTitleParameter()
Returns the requested title.string
mapActionModuleResponse(WebResponse $actionModuleResponse, array $actionModuleResult, Response $response)
Transfers relevant information, such as header values, from the WebResponse constructed by the action...
getActionModuleParameters()
Maps a REST API request to an action API request.Implementations typically use information returned b...
Base class for REST API handlers that perform page edits (main slot only).
Definition: EditHandler.php:23
getValidatedBody()
Fetch the validated body.
Definition: Handler.php:383
getRouter()
Get the Router.
Definition: Handler.php:104
urlEncodeTitle( $title)
URL-encode titles in a "pretty" way.
Definition: Handler.php:138
This is the base exception class for non-fatal exceptions thrown from REST handlers.
setHeader( $name, $value)
Set or replace the specified header.
Definition: Response.php:80
Value object representing a message for i18n.
Service for formatting and validating API parameters.
const PARAM_TYPE
(string|array) Type of the parameter.
const PARAM_REQUIRED
(bool) Indicate that the parameter is required.
Copyright (C) 2011-2020 Wikimedia Foundation and others.