MediaWiki master
EditHandler.php
Go to the documentation of this file.
1<?php
2
4
13use MediaWiki\Rest\TokenAwareHandlerTrait;
18use RuntimeException;
20
24abstract class EditHandler extends ActionModuleBasedHandler {
26
28 protected $config;
29
34
38 protected $titleParser;
39
43 protected $titleFormatter;
44
48 protected $revisionLookup;
49
57 public function __construct(
63 ) {
64 $this->config = $config;
65 $this->contentHandlerFactory = $contentHandlerFactory;
66 $this->titleParser = $titleParser;
67 $this->titleFormatter = $titleFormatter;
68 $this->revisionLookup = $revisionLookup;
69 }
70
71 public function needsWriteAccess() {
72 return true;
73 }
74
80 abstract protected function getTitleParameter();
81
85 protected function mapActionModuleResult( array $data ) {
86 if ( isset( $data['error'] ) ) {
87 throw new LocalizedHttpException( new MessageValue( 'apierror-' . $data['error'] ), 400 );
88 }
89
90 if ( !isset( $data['edit'] ) || !$data['edit']['result'] ) {
91 throw new RuntimeException( 'Bad result structure received from ApiEditPage' );
92 }
93
94 if ( $data['edit']['result'] !== 'Success' ) {
95 // Probably an edit conflict
96 // TODO: which code for null edits?
97 throw new HttpException( $data['edit']['result'], 409 );
98 }
99
100 $title = $this->titleParser->parseTitle( $data['edit']['title'] );
101
102 // This seems wasteful. This is the downside of delegating to the action API module:
103 // if we need additional data in the response, we have to load it.
104 $revision = $this->revisionLookup->getRevisionById( (int)$data['edit']['newrevid'] );
105 $content = $revision->getContent( SlotRecord::MAIN );
106
107 return [
108 'id' => $data['edit']['pageid'],
109 'title' => $this->titleFormatter->getPrefixedText( $title ),
110 'key' => $this->titleFormatter->getPrefixedDBkey( $title ),
111 'latest' => [
112 'id' => $data['edit']['newrevid'],
113 'timestamp' => $data['edit']['newtimestamp'],
114 ],
115 'license' => [
116 'url' => $this->config->get( MainConfigNames::RightsUrl ),
117 'title' => $this->config->get( MainConfigNames::RightsText )
118 ],
119 'content_model' => $data['edit']['contentmodel'],
120 'source' => $content->serialize(),
121 ];
122 }
123
127 protected function throwHttpExceptionForActionModuleError( IApiMessage $msg, $statusCode = 400 ) {
128 $code = $msg->getApiCode();
129
130 if ( $code === 'protectedpage' ) {
131 throw new LocalizedHttpException( $this->makeMessageValue( $msg ), 403 );
132 }
133
134 if ( $code === 'badtoken' ) {
135 throw new LocalizedHttpException( $this->makeMessageValue( $msg ), 403 );
136 }
137
138 if ( $code === 'missingtitle' ) {
139 throw new LocalizedHttpException( $this->makeMessageValue( $msg ), 404 );
140 }
141
142 if ( $code === 'articleexists' ) {
143 throw new LocalizedHttpException( $this->makeMessageValue( $msg ), 409 );
144 }
145
146 if ( $code === 'editconflict' ) {
147 throw new LocalizedHttpException( $this->makeMessageValue( $msg ), 409 );
148 }
149
150 if ( $code === 'ratelimited' ) {
151 throw new LocalizedHttpException( $this->makeMessageValue( $msg ), 429 );
152 }
153
154 // Fall through to generic handling of the error (status 400).
155 parent::throwHttpExceptionForActionModuleError( $msg, $statusCode );
156 }
157
158 protected function mapActionModuleResponse(
159 WebResponse $actionModuleResponse,
160 array $actionModuleResult,
161 Response $response
162 ) {
163 parent::mapActionModuleResponse(
164 $actionModuleResponse,
165 $actionModuleResult,
166 $response
167 );
168
169 if ( $actionModuleResult['edit']['new'] ?? false ) {
170 $response->setStatus( 201 );
171 }
172 }
173
174}
A class containing constants representing the names of configuration variables.
const RightsText
Name constant for the RightsText setting, for use with Config::get()
const RightsUrl
Name constant for the RightsUrl setting, for use with Config::get()
Allow programs to request this object from WebRequest::response() and handle all outputting (or lack ...
Base class for REST handlers that are implemented by mapping to an existing ApiModule.
makeMessageValue(IApiMessage $msg)
Constructs a MessageValue from an IApiMessage.
Base class for REST API handlers that perform page edits (main slot only).
__construct(Config $config, IContentHandlerFactory $contentHandlerFactory, TitleParser $titleParser, TitleFormatter $titleFormatter, RevisionLookup $revisionLookup)
needsWriteAccess()
Indicates whether this route requires write access.
getTitleParameter()
Returns the requested title.
mapActionModuleResult(array $data)
Maps an action API result to a REST API result.mixed Data structure to be converted to JSON and wrapp...
throwHttpExceptionForActionModuleError(IApiMessage $msg, $statusCode=400)
Throws a HttpException for a given IApiMessage that represents an error.Never returns normally....
IContentHandlerFactory $contentHandlerFactory
mapActionModuleResponse(WebResponse $actionModuleResponse, array $actionModuleResult, Response $response)
Transfers relevant information, such as header values, from the WebResponse constructed by the action...
This is the base exception class for non-fatal exceptions thrown from REST handlers.
setStatus( $code, $reasonPhrase='')
Set the status code and, optionally, reason phrase.
Definition Response.php:44
Value object representing a content slot associated with a page revision.
Value object representing a message for i18n.
Interface for messages with machine-readable data for use by the API.
getApiCode()
Returns a machine-readable code for use by the API.
Interface for configuration instances.
Definition Config.php:32
Service for looking up page revisions.
A title formatter service for MediaWiki.
A title parser service for MediaWiki.
Copyright (C) 2011-2020 Wikimedia Foundation and others.