MediaWiki REL1_39
EditHandler.php
Go to the documentation of this file.
1<?php
2
4
5use Config;
12use MediaWiki\Rest\TokenAwareHandlerTrait;
16use TitleParser;
17use WebResponse;
19
23abstract class EditHandler extends ActionModuleBasedHandler {
24 use TokenAwareHandlerTrait;
25
27 protected $config;
28
33
37 protected $titleParser;
38
42 protected $titleFormatter;
43
47 protected $revisionLookup;
48
56 public function __construct(
62 ) {
63 $this->config = $config;
64 $this->contentHandlerFactory = $contentHandlerFactory;
65 $this->titleParser = $titleParser;
66 $this->titleFormatter = $titleFormatter;
67 $this->revisionLookup = $revisionLookup;
68 }
69
70 public function needsWriteAccess() {
71 return true;
72 }
73
79 abstract protected function getTitleParameter();
80
84 protected function mapActionModuleResult( array $data ) {
85 if ( isset( $data['error'] ) ) {
86 throw new LocalizedHttpException( new MessageValue( 'apierror-' . $data['error'] ), 400 );
87 }
88
89 if ( !isset( $data['edit'] ) || !$data['edit']['result'] ) {
90 throw new HttpException( 'Bad result structure received from ApiEditPage' );
91 }
92
93 if ( $data['edit']['result'] !== 'Success' ) {
94 // Probably an edit conflict
95 // TODO: which code for null edits?
96 throw new HttpException( $data['edit']['result'], 409 );
97 }
98
99 $title = $this->titleParser->parseTitle( $data['edit']['title'] );
100
101 // This seems wasteful. This is the downside of delegating to the action API module:
102 // if we need additional data in the response, we have to load it.
103 $revision = $this->revisionLookup->getRevisionById( (int)$data['edit']['newrevid'] );
104 $content = $revision->getContent( SlotRecord::MAIN );
105
106 return [
107 'id' => $data['edit']['pageid'],
108 'title' => $this->titleFormatter->getPrefixedText( $title ),
109 'key' => $this->titleFormatter->getPrefixedDBkey( $title ),
110 'latest' => [
111 'id' => $data['edit']['newrevid'],
112 'timestamp' => $data['edit']['newtimestamp'],
113 ],
114 'license' => [
115 'url' => $this->config->get( MainConfigNames::RightsUrl ),
116 'title' => $this->config->get( MainConfigNames::RightsText )
117 ],
118 'content_model' => $data['edit']['contentmodel'],
119 'source' => $content->serialize(),
120 ];
121 }
122
126 protected function throwHttpExceptionForActionModuleError( IApiMessage $msg, $statusCode = 400 ) {
127 $code = $msg->getApiCode();
128
129 if ( $code === 'protectedpage' ) {
130 throw new LocalizedHttpException( $this->makeMessageValue( $msg ), 403 );
131 }
132
133 if ( $code === 'badtoken' ) {
134 throw new LocalizedHttpException( $this->makeMessageValue( $msg ), 403 );
135 }
136
137 if ( $code === 'missingtitle' ) {
138 throw new LocalizedHttpException( $this->makeMessageValue( $msg ), 404 );
139 }
140
141 if ( $code === 'articleexists' ) {
142 throw new LocalizedHttpException( $this->makeMessageValue( $msg ), 409 );
143 }
144
145 if ( $code === 'editconflict' ) {
146 throw new LocalizedHttpException( $this->makeMessageValue( $msg ), 409 );
147 }
148
149 if ( $code === 'ratelimited' ) {
150 throw new LocalizedHttpException( $this->makeMessageValue( $msg ), 429 );
151 }
152
153 // Fall through to generic handling of the error (status 400).
154 parent::throwHttpExceptionForActionModuleError( $msg, $statusCode );
155 }
156
157 protected function mapActionModuleResponse(
158 WebResponse $actionModuleResponse,
159 array $actionModuleResult,
160 Response $response
161 ) {
162 parent::mapActionModuleResponse(
163 $actionModuleResponse,
164 $actionModuleResult,
165 $response
166 );
167
168 if ( $actionModuleResult['edit']['new'] ?? false ) {
169 $response->setStatus( 201 );
170 }
171 }
172
173}
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()
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.
Allow programs to request this object from WebRequest::response() and handle all outputting (or lack ...
Value object representing a message for i18n.
Interface for configuration instances.
Definition Config.php:30
Interface for messages with machine-readable data for use by the API.
getApiCode()
Returns a machine-readable code for use by the API.
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.
$content
Definition router.php:76