Debian Codegraph @

includes/Rest/Handler/EditHandler.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Rest\Handler;
4
5 use MediaWiki\Api\IApiMessage;
6 use MediaWiki\Config\Config;
7 use MediaWiki\Content\IContentHandlerFactory;
8 use MediaWiki\MainConfigNames;
9 use MediaWiki\Request\WebResponse;
10 use MediaWiki\Rest\LocalizedHttpException;
11 use MediaWiki\Rest\Response;
12 use MediaWiki\Rest\TokenAwareHandlerTrait;
13 use MediaWiki\Rest\Validator\Validator;
14 use MediaWiki\Revision\RevisionLookup;
15 use MediaWiki\Revision\SlotRecord;
16 use MediaWiki\Title\TitleFormatter;
17 use MediaWiki\Title\TitleParser;
18 use RuntimeException;
19 use Wikimedia\Message\MessageValue;
20
21 /**
22 * Base class for REST API handlers that perform page edits (main slot only).
23 */
24 abstract class EditHandler extends ActionModuleBasedHandler {
25 use TokenAwareHandlerTrait;
26
27 protected Config $config;
28 protected IContentHandlerFactory $contentHandlerFactory;
29 protected TitleParser $titleParser;
30 protected TitleFormatter $titleFormatter;
31 protected RevisionLookup $revisionLookup;
32
33 public function __construct(
34 Config $config,
35 IContentHandlerFactory $contentHandlerFactory,
36 TitleParser $titleParser,
37 TitleFormatter $titleFormatter,
38 RevisionLookup $revisionLookup
39 ) {
40 $this->config = $config;
41 $this->contentHandlerFactory = $contentHandlerFactory;
42 $this->titleParser = $titleParser;
43 $this->titleFormatter = $titleFormatter;
44 $this->revisionLookup = $revisionLookup;
45 }
46
47 public function needsWriteAccess() {
48 return true;
49 }
50
51 /**
52 * Returns the requested title.
53 *
54 * @return string
55 */
56 abstract protected function getTitleParameter();
57
58 /**
59 * @inheritDoc
60 */
61 public function validate( Validator $restValidator ) {
62 parent::validate( $restValidator );
63 $this->validateToken( true );
64 }
65
66 /**
67 * @inheritDoc
68 */
69 protected function mapActionModuleResult( array $data ) {
70 if ( isset( $data['error'] ) ) {
71 throw new LocalizedHttpException( new MessageValue( 'apierror-' . $data['error'] ), 400 );
72 }
73
74 if ( !isset( $data['edit'] ) || !$data['edit']['result'] ) {
75 throw new RuntimeException( 'Bad result structure received from ApiEditPage' );
76 }
77
78 if ( $data['edit']['result'] !== 'Success' ) {
79 // Probably an edit conflict
80 // TODO: which code for null edits?
81 throw new LocalizedHttpException(
82 new MessageValue( "rest-edit-conflict", [ $data['edit']['result'] ] ),
83 409
84 );
85 }
86
87 $title = $this->titleParser->parseTitle( $data['edit']['title'] );
88
89 // This seems wasteful. This is the downside of delegating to the action API module:
90 // if we need additional data in the response, we have to load it.
91 $revision = $this->revisionLookup->getRevisionById( (int)$data['edit']['newrevid'] );
92 $content = $revision->getContent( SlotRecord::MAIN );
93
94 return [
95 'id' => $data['edit']['pageid'],
96 'title' => $this->titleFormatter->getPrefixedText( $title ),
97 'key' => $this->titleFormatter->getPrefixedDBkey( $title ),
98 'latest' => [
99 'id' => $data['edit']['newrevid'],
100 'timestamp' => $data['edit']['newtimestamp'],
101 ],
102 'license' => [
103 'url' => $this->config->get( MainConfigNames::RightsUrl ),
104 'title' => $this->config->get( MainConfigNames::RightsText )
105 ],
106 'content_model' => $data['edit']['contentmodel'],
107 'source' => $content->serialize(),
108 ];
109 }
110
111 /**
112 * @inheritDoc
113 */
114 protected function throwHttpExceptionForActionModuleError( IApiMessage $msg, $statusCode = 400 ) {
115 $code = $msg->getApiCode();
116
117 if ( $code === 'protectedpage' ) {
118 throw new LocalizedHttpException( $this->makeMessageValue( $msg ), 403 );
119 }
120
121 if ( $code === 'badtoken' ) {
122 throw new LocalizedHttpException( $this->makeMessageValue( $msg ), 403 );
123 }
124
125 if ( $code === 'missingtitle' ) {
126 throw new LocalizedHttpException( $this->makeMessageValue( $msg ), 404 );
127 }
128
129 if ( $code === 'articleexists' ) {
130 throw new LocalizedHttpException( $this->makeMessageValue( $msg ), 409 );
131 }
132
133 if ( $code === 'editconflict' ) {
134 throw new LocalizedHttpException( $this->makeMessageValue( $msg ), 409 );
135 }
136
137 if ( $code === 'ratelimited' ) {
138 throw new LocalizedHttpException( $this->makeMessageValue( $msg ), 429 );
139 }
140
141 // Fall through to generic handling of the error (status 400).
142 parent::throwHttpExceptionForActionModuleError( $msg, $statusCode );
143 }
144
145 protected function mapActionModuleResponse(
146 WebResponse $actionModuleResponse,
147 array $actionModuleResult,
148 Response $response
149 ) {
150 parent::mapActionModuleResponse(
151 $actionModuleResponse,
152 $actionModuleResult,
153 $response
154 );
155
156 if ( $actionModuleResult['edit']['new'] ?? false ) {
157 $response->setStatus( 201 );
158 }
159 }
160
161 }