MediaWiki master
CreationHandler.php
Go to the documentation of this file.
1<?php
2
4
12
17
21 protected function getTitleParameter() {
22 $body = $this->getValidatedBody();
23 '@phan-var array $body';
24 return $body['title'];
25 }
26
31 public function getBodyValidator( $contentType ) {
32 if ( $contentType !== 'application/json' ) {
33 throw new HttpException( "Unsupported Content-Type",
34 415,
35 [ 'content_type' => $contentType ]
36 );
37 }
38
39 return new JsonBodyValidator( [
40 'source' => [
41 self::PARAM_SOURCE => 'body',
42 ParamValidator::PARAM_TYPE => 'string',
43 ParamValidator::PARAM_REQUIRED => true,
44 self::PARAM_DESCRIPTION => 'The intended content of the page',
45 ],
46 'title' => [
47 self::PARAM_SOURCE => 'body',
48 ParamValidator::PARAM_TYPE => 'string',
49 ParamValidator::PARAM_REQUIRED => true,
50 self::PARAM_DESCRIPTION => 'The title of the page to create',
51 ],
52 'comment' => [
53 self::PARAM_SOURCE => 'body',
54 ParamValidator::PARAM_TYPE => 'string',
55 ParamValidator::PARAM_REQUIRED => true,
56 self::PARAM_DESCRIPTION => 'A comment descripting the reason for creating the page',
57 ],
58 'content_model' => [
59 self::PARAM_SOURCE => 'body',
60 ParamValidator::PARAM_TYPE => 'string',
61 ParamValidator::PARAM_REQUIRED => false,
62 self::PARAM_DESCRIPTION => 'The content model to use to interpret the source',
63 ],
64 ] + $this->getTokenParamDefinition() );
65 }
66
70 protected function getActionModuleParameters() {
71 $body = $this->getValidatedBody();
72 '@phan-var array $body';
73
74 $title = $this->getTitleParameter();
75
76 $contentmodel = $body['content_model'] ?: null;
77
78 if ( $contentmodel !== null && !$this->contentHandlerFactory->isDefinedModel( $contentmodel ) ) {
79 throw new LocalizedHttpException(
80 new MessageValue( 'rest-bad-content-model', [ $body['content_model'] ] ), 400
81 );
82 }
83
84 // Use a known good CSRF token if a token is not needed because we are
85 // using a method of authentication that protects against CSRF, like OAuth.
86 $token = $this->needsToken() ? $this->getToken() : $this->getUser()->getEditToken();
87
88 $params = [
89 'action' => 'edit',
90 'title' => $title,
91 'text' => $body['source'],
92 'summary' => $body['comment'],
93 'token' => $token,
94 'createonly' => true,
95 ];
96
97 if ( $contentmodel !== null ) {
98 $params['contentmodel'] = $contentmodel;
99 }
100
101 return $params;
102 }
103
104 protected function mapActionModuleResponse(
105 WebResponse $actionModuleResponse,
106 array $actionModuleResult,
107 Response $response
108 ) {
109 parent::mapActionModuleResponse(
110 $actionModuleResponse,
111 $actionModuleResult,
112 $response
113 );
114
115 $title = $this->urlEncodeTitle( $actionModuleResult['edit']['title'] );
116
117 $url = $this->getRouter()->getRouteUrl( '/v1/page/' . $title );
118 $response->setHeader( 'Location', $url );
119 }
120
121}
Allow programs to request this object from WebRequest::response() and handle all outputting (or lack ...
Core REST API endpoint that handles page creation (main slot only)
getBodyValidator( $contentType)
Fetch the BodyValidator.to overrideBodyValidator A {NullBodyValidator} in this default implementation...
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).
getValidatedBody()
Fetch the validated body.
Definition Handler.php:517
getRouter()
Get the Router.
Definition Handler.php:106
urlEncodeTitle( $title)
URL-encode titles in a "pretty" way.
Definition Handler.php:140
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.
Copyright (C) 2011-2020 Wikimedia Foundation and others.
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.
needsToken()
Determines whether a CSRF token is needed.