| 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 |
|
| 13 |
|
| 14 |
class CreationHandler extends EditHandler { |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
protected function getTitleParameter() { |
| 20 |
$body = $this->getValidatedBody(); |
| 21 |
'@phan-var array $body'; |
| 22 |
return $body['title']; |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 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 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 |
] |
| 56 |
+ $this->getTokenParamDefinition(); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 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 |
|
| 77 |
|
| 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 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function getResponseBodySchemaFileName( string $method ): ?string { |
| 120 |
return 'includes/Rest/Handler/Schema/NewPage.json'; |
| 121 |
} |
| 122 |
} |