Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
92.98% |
53 / 57 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
CreationHandler | |
92.98% |
53 / 57 |
|
60.00% |
3 / 5 |
10.03 | |
0.00% |
0 / 1 |
getTitleParameter | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getBodyParamSettings | |
100.00% |
27 / 27 |
|
100.00% |
1 / 1 |
1 | |||
getActionModuleParameters | |
84.21% |
16 / 19 |
|
0.00% |
0 / 1 |
6.14 | |||
mapActionModuleResponse | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
getResponseBodySchemaFileName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Rest\Handler; |
4 | |
5 | use MediaWiki\Request\WebResponse; |
6 | use MediaWiki\Rest\LocalizedHttpException; |
7 | use MediaWiki\Rest\Response; |
8 | use Wikimedia\Message\MessageValue; |
9 | use Wikimedia\ParamValidator\ParamValidator; |
10 | |
11 | /** |
12 | * Core REST API endpoint that handles page creation (main slot only) |
13 | */ |
14 | class CreationHandler extends EditHandler { |
15 | |
16 | /** |
17 | * @inheritDoc |
18 | */ |
19 | protected function getTitleParameter() { |
20 | $body = $this->getValidatedBody(); |
21 | '@phan-var array $body'; |
22 | return $body['title']; |
23 | } |
24 | |
25 | /** |
26 | * @inheritDoc |
27 | * @return array |
28 | */ |
29 | public function getBodyParamSettings(): array { |
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 describing 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 | ] |
56 | + $this->getTokenParamDefinition(); |
57 | } |
58 | |
59 | /** |
60 | * @inheritDoc |
61 | */ |
62 | protected function getActionModuleParameters() { |
63 | $body = $this->getValidatedBody(); |
64 | '@phan-var array $body'; |
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 | // Use a known good CSRF token if a token is not needed because we are |
77 | // using a method of authentication that protects against CSRF, like OAuth. |
78 | $token = $this->needsToken() ? $this->getToken() : $this->getUser()->getEditToken(); |
79 | |
80 | $params = [ |
81 | 'action' => 'edit', |
82 | 'title' => $title, |
83 | 'text' => $body['source'], |
84 | 'summary' => $body['comment'], |
85 | 'token' => $token, |
86 | 'createonly' => true, |
87 | ]; |
88 | |
89 | if ( $contentmodel !== null ) { |
90 | $params['contentmodel'] = $contentmodel; |
91 | } |
92 | |
93 | return $params; |
94 | } |
95 | |
96 | protected function mapActionModuleResponse( |
97 | WebResponse $actionModuleResponse, |
98 | array $actionModuleResult, |
99 | Response $response |
100 | ) { |
101 | parent::mapActionModuleResponse( |
102 | $actionModuleResponse, |
103 | $actionModuleResult, |
104 | $response |
105 | ); |
106 | |
107 | $title = $this->urlEncodeTitle( $actionModuleResult['edit']['title'] ); |
108 | |
109 | $url = $this->getRouter()->getRouteUrl( '/v1/page/' . $title ); |
110 | $response->setHeader( 'Location', $url ); |
111 | } |
112 | |
113 | /** |
114 | * This method specifies the JSON schema file for the response |
115 | * body when creating a new page. |
116 | * |
117 | * @return ?string The file path to the NewPage JSON schema. |
118 | */ |
119 | public function getResponseBodySchemaFileName( string $method ): ?string { |
120 | return 'includes/Rest/Handler/Schema/NewPage.json'; |
121 | } |
122 | } |