MediaWiki REL1_35
CreationHandler.php
Go to the documentation of this file.
1<?php
2
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',
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,
103 Response $response
104 ) {
105 parent::mapActionModuleResponse(
106 $actionModuleResponse,
107 $actionModuleResult,
108 $response
109 );
110
111 $title = $this->urlEncodeTitle( $actionModuleResult['edit']['title'] );
112
113 $url = $this->getRouter()->getRouteUrl( '/v1/page/' . $title );
114 $response->setHeader( 'Location', $url );
115 }
116
117}
Core REST API endpoint that handles page creation (main slot only)
getBodyValidator( $contentType)
Fetch the BodyValidator.Stable to overrideBodyValidator
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).
getActionModuleToken()
Determines the CSRF token to be passed to the action module.
getValidatedBody()
Fetch the validated body.
Definition Handler.php:269
getRouter()
Get the Router.
Definition Handler.php:77
urlEncodeTitle( $title)
URL-encode titles in a "pretty" way.
Definition Handler.php:111
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
Allow programs to request this object from WebRequest::response() and handle all outputting (or lack ...
Value object representing a message for i18n.
Service for formatting and validating API parameters.