MediaWiki master
EditHandler.php
Go to the documentation of this file.
1<?php
2
4
12use MediaWiki\Rest\TokenAwareHandlerTrait;
17use RuntimeException;
19
23abstract class EditHandler extends ActionModuleBasedHandler {
25
26 protected Config $config;
31
32 public function __construct(
38 ) {
39 $this->config = $config;
40 $this->contentHandlerFactory = $contentHandlerFactory;
41 $this->titleParser = $titleParser;
42 $this->titleFormatter = $titleFormatter;
43 $this->revisionLookup = $revisionLookup;
44 }
45
46 public function needsWriteAccess() {
47 return true;
48 }
49
55 abstract protected function getTitleParameter();
56
60 protected function mapActionModuleResult( array $data ) {
61 if ( isset( $data['error'] ) ) {
62 throw new LocalizedHttpException( new MessageValue( 'apierror-' . $data['error'] ), 400 );
63 }
64
65 if ( !isset( $data['edit'] ) || !$data['edit']['result'] ) {
66 throw new RuntimeException( 'Bad result structure received from ApiEditPage' );
67 }
68
69 if ( $data['edit']['result'] !== 'Success' ) {
70 // Probably an edit conflict
71 // TODO: which code for null edits?
72 throw new LocalizedHttpException(
73 new MessageValue( "rest-edit-conflict", [ $data['edit']['result'] ] ),
74 409
75 );
76 }
77
78 $title = $this->titleParser->parseTitle( $data['edit']['title'] );
79
80 // This seems wasteful. This is the downside of delegating to the action API module:
81 // if we need additional data in the response, we have to load it.
82 $revision = $this->revisionLookup->getRevisionById( (int)$data['edit']['newrevid'] );
83 $content = $revision->getContent( SlotRecord::MAIN );
84
85 return [
86 'id' => $data['edit']['pageid'],
87 'title' => $this->titleFormatter->getPrefixedText( $title ),
88 'key' => $this->titleFormatter->getPrefixedDBkey( $title ),
89 'latest' => [
90 'id' => $data['edit']['newrevid'],
91 'timestamp' => $data['edit']['newtimestamp'],
92 ],
93 'license' => [
94 'url' => $this->config->get( MainConfigNames::RightsUrl ),
95 'title' => $this->config->get( MainConfigNames::RightsText )
96 ],
97 'content_model' => $data['edit']['contentmodel'],
98 'source' => $content->serialize(),
99 ];
100 }
101
105 protected function throwHttpExceptionForActionModuleError( IApiMessage $msg, $statusCode = 400 ) {
106 $code = $msg->getApiCode();
107
108 if ( $code === 'protectedpage' ) {
109 throw new LocalizedHttpException( $this->makeMessageValue( $msg ), 403 );
110 }
111
112 if ( $code === 'badtoken' ) {
113 throw new LocalizedHttpException( $this->makeMessageValue( $msg ), 403 );
114 }
115
116 if ( $code === 'missingtitle' ) {
117 throw new LocalizedHttpException( $this->makeMessageValue( $msg ), 404 );
118 }
119
120 if ( $code === 'articleexists' ) {
121 throw new LocalizedHttpException( $this->makeMessageValue( $msg ), 409 );
122 }
123
124 if ( $code === 'editconflict' ) {
125 throw new LocalizedHttpException( $this->makeMessageValue( $msg ), 409 );
126 }
127
128 if ( $code === 'ratelimited' ) {
129 throw new LocalizedHttpException( $this->makeMessageValue( $msg ), 429 );
130 }
131
132 // Fall through to generic handling of the error (status 400).
133 parent::throwHttpExceptionForActionModuleError( $msg, $statusCode );
134 }
135
136 protected function mapActionModuleResponse(
137 WebResponse $actionModuleResponse,
138 array $actionModuleResult,
139 Response $response
140 ) {
141 parent::mapActionModuleResponse(
142 $actionModuleResponse,
143 $actionModuleResult,
144 $response
145 );
146
147 if ( $actionModuleResult['edit']['new'] ?? false ) {
148 $response->setStatus( 201 );
149 }
150 }
151
152}
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...
setStatus( $code, $reasonPhrase='')
Set the status code and, optionally, reason phrase.
Definition Response.php:58
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.