MediaWiki master
CreationHandler.php
Go to the documentation of this file.
1<?php
2
4
10
15
19 protected function getTitleParameter() {
20 $body = $this->getValidatedBody();
21 '@phan-var array $body';
22 return $body['title'];
23 }
24
29 public function getParamSettings() {
30 return [
31 'source' => [
32 self::PARAM_SOURCE => 'body',
33 ParamValidator::PARAM_TYPE => 'string',
34 ParamValidator::PARAM_REQUIRED => true,
35 self::PARAM_DESCRIPTION => 'The intended content of the page',
36 ],
37 'title' => [
38 self::PARAM_SOURCE => 'body',
39 ParamValidator::PARAM_TYPE => 'string',
40 ParamValidator::PARAM_REQUIRED => true,
41 self::PARAM_DESCRIPTION => 'The title of the page to create',
42 ],
43 'comment' => [
44 self::PARAM_SOURCE => 'body',
45 ParamValidator::PARAM_TYPE => 'string',
46 ParamValidator::PARAM_REQUIRED => true,
47 self::PARAM_DESCRIPTION => 'A comment descripting the reason for creating the page',
48 ],
49 'content_model' => [
50 self::PARAM_SOURCE => 'body',
51 ParamValidator::PARAM_TYPE => 'string',
52 ParamValidator::PARAM_REQUIRED => false,
53 self::PARAM_DESCRIPTION => 'The content model to use to interpret the source',
54 ],
55 ]
57 + parent::getParamSettings();
58 }
59
63 protected function getActionModuleParameters() {
64 $body = $this->getValidatedBody();
65 '@phan-var array $body';
66
67 $title = $this->getTitleParameter();
68
69 $contentmodel = $body['content_model'] ?: null;
70
71 if ( $contentmodel !== null && !$this->contentHandlerFactory->isDefinedModel( $contentmodel ) ) {
72 throw new LocalizedHttpException(
73 new MessageValue( 'rest-bad-content-model', [ $body['content_model'] ] ), 400
74 );
75 }
76
77 // Use a known good CSRF token if a token is not needed because we are
78 // using a method of authentication that protects against CSRF, like OAuth.
79 $token = $this->needsToken() ? $this->getToken() : $this->getUser()->getEditToken();
80
81 $params = [
82 'action' => 'edit',
83 'title' => $title,
84 'text' => $body['source'],
85 'summary' => $body['comment'],
86 'token' => $token,
87 'createonly' => true,
88 ];
89
90 if ( $contentmodel !== null ) {
91 $params['contentmodel'] = $contentmodel;
92 }
93
94 return $params;
95 }
96
97 protected function mapActionModuleResponse(
98 WebResponse $actionModuleResponse,
99 array $actionModuleResult,
100 Response $response
101 ) {
102 parent::mapActionModuleResponse(
103 $actionModuleResponse,
104 $actionModuleResult,
105 $response
106 );
107
108 $title = $this->urlEncodeTitle( $actionModuleResult['edit']['title'] );
109
110 $url = $this->getRouter()->getRouteUrl( '/v1/page/' . $title );
111 $response->setHeader( 'Location', $url );
112 }
113
114}
array $params
The job parameters.
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)
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...
getParamSettings()
Fetch ParamValidator settings for parameters.Every setting must include self::PARAM_SOURCE to specify...
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:561
getRouter()
Get the Router.
Definition Handler.php:111
urlEncodeTitle( $title)
URL-encode titles in a "pretty" way.
Definition Handler.php:145
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.