Merge "Add block type filter to Special:BlockList"
[lhc/web/wiklou.git] / includes / api / ApiStashEdit.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 use MediaWiki\MediaWikiServices;
22
23 /**
24 * Prepare an edit in shared cache so that it can be reused on edit
25 *
26 * This endpoint can be called via AJAX as the user focuses on the edit
27 * summary box. By the time of submission, the parse may have already
28 * finished, and can be immediately used on page save. Certain parser
29 * functions like {{REVISIONID}} or {{CURRENTTIME}} may cause the cache
30 * to not be used on edit. Template and files used are check for changes
31 * since the output was generated. The cache TTL is also kept low for sanity.
32 *
33 * @ingroup API
34 * @since 1.25
35 */
36 class ApiStashEdit extends ApiBase {
37 public function execute() {
38 $user = $this->getUser();
39 $params = $this->extractRequestParams();
40
41 if ( $user->isBot() ) { // sanity
42 $this->dieWithError( 'apierror-botsnotsupported' );
43 }
44
45 $editStash = MediaWikiServices::getInstance()->getPageEditStash();
46 $page = $this->getTitleOrPageId( $params );
47 $title = $page->getTitle();
48
49 if ( !ContentHandler::getForModelID( $params['contentmodel'] )
50 ->isSupportedFormat( $params['contentformat'] )
51 ) {
52 $this->dieWithError(
53 [ 'apierror-badformat-generic', $params['contentformat'], $params['contentmodel'] ],
54 'badmodelformat'
55 );
56 }
57
58 $this->requireOnlyOneParameter( $params, 'stashedtexthash', 'text' );
59
60 $text = null;
61 $textHash = null;
62 if ( $params['stashedtexthash'] !== null ) {
63 // Load from cache since the client indicates the text is the same as last stash
64 $textHash = $params['stashedtexthash'];
65 if ( !preg_match( '/^[0-9a-f]{40}$/', $textHash ) ) {
66 $this->dieWithError( 'apierror-stashedit-missingtext', 'missingtext' );
67 }
68 $text = $editStash->fetchInputText( $textHash );
69 if ( !is_string( $text ) ) {
70 $this->dieWithError( 'apierror-stashedit-missingtext', 'missingtext' );
71 }
72 } else {
73 // 'text' was passed. Trim and fix newlines so the key SHA1's
74 // match (see WebRequest::getText())
75 $text = rtrim( str_replace( "\r\n", "\n", $params['text'] ) );
76 $textHash = sha1( $text );
77 }
78
79 $textContent = ContentHandler::makeContent(
80 $text, $title, $params['contentmodel'], $params['contentformat'] );
81
82 $page = WikiPage::factory( $title );
83 if ( $page->exists() ) {
84 // Page exists: get the merged content with the proposed change
85 $baseRev = Revision::newFromPageId( $page->getId(), $params['baserevid'] );
86 if ( !$baseRev ) {
87 $this->dieWithError( [ 'apierror-nosuchrevid', $params['baserevid'] ] );
88 }
89 $currentRev = $page->getRevision();
90 if ( !$currentRev ) {
91 $this->dieWithError( [ 'apierror-missingrev-pageid', $page->getId() ], 'missingrev' );
92 }
93 // Merge in the new version of the section to get the proposed version
94 $editContent = $page->replaceSectionAtRev(
95 $params['section'],
96 $textContent,
97 $params['sectiontitle'],
98 $baseRev->getId()
99 );
100 if ( !$editContent ) {
101 $this->dieWithError( 'apierror-sectionreplacefailed', 'replacefailed' );
102 }
103 if ( $currentRev->getId() == $baseRev->getId() ) {
104 // Base revision was still the latest; nothing to merge
105 $content = $editContent;
106 } else {
107 // Merge the edit into the current version
108 $baseContent = $baseRev->getContent();
109 $currentContent = $currentRev->getContent();
110 if ( !$baseContent || !$currentContent ) {
111 $this->dieWithError( [ 'apierror-missingcontent-pageid', $page->getId() ], 'missingrev' );
112 }
113 $handler = ContentHandler::getForModelID( $baseContent->getModel() );
114 $content = $handler->merge3( $baseContent, $editContent, $currentContent );
115 }
116 } else {
117 // New pages: use the user-provided content model
118 $content = $textContent;
119 }
120
121 if ( !$content ) { // merge3() failed
122 $this->getResult()->addValue( null,
123 $this->getModuleName(), [ 'status' => 'editconflict' ] );
124 return;
125 }
126
127 // The user will abort the AJAX request by pressing "save", so ignore that
128 ignore_user_abort( true );
129
130 if ( $user->pingLimiter( 'stashedit' ) ) {
131 $status = 'ratelimited';
132 } else {
133 $status = $editStash->parseAndCache( $page, $content, $user, $params['summary'] );
134 $editStash->stashInputText( $text, $textHash );
135 }
136
137 $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
138 $stats->increment( "editstash.cache_stores.$status" );
139
140 $ret = [ 'status' => $status ];
141 // If we were rate-limited, we still return the pre-existing valid hash if one was passed
142 if ( $status !== 'ratelimited' || $params['stashedtexthash'] !== null ) {
143 $ret['texthash'] = $textHash;
144 }
145
146 $this->getResult()->addValue( null, $this->getModuleName(), $ret );
147 }
148
149 /**
150 * @param WikiPage $page
151 * @param Content $content Edit content
152 * @param User $user
153 * @param string $summary Edit summary
154 * @return string ApiStashEdit::ERROR_* constant
155 * @since 1.25
156 * @deprecated Since 1.34
157 */
158 public function parseAndStash( WikiPage $page, Content $content, User $user, $summary ) {
159 $editStash = MediaWikiServices::getInstance()->getPageEditStash();
160
161 return $editStash->parseAndCache( $page, $content, $user, $summary );
162 }
163
164 public function getAllowedParams() {
165 return [
166 'title' => [
167 ApiBase::PARAM_TYPE => 'string',
168 ApiBase::PARAM_REQUIRED => true
169 ],
170 'section' => [
171 ApiBase::PARAM_TYPE => 'string',
172 ],
173 'sectiontitle' => [
174 ApiBase::PARAM_TYPE => 'string'
175 ],
176 'text' => [
177 ApiBase::PARAM_TYPE => 'text',
178 ApiBase::PARAM_DFLT => null
179 ],
180 'stashedtexthash' => [
181 ApiBase::PARAM_TYPE => 'string',
182 ApiBase::PARAM_DFLT => null
183 ],
184 'summary' => [
185 ApiBase::PARAM_TYPE => 'string',
186 ],
187 'contentmodel' => [
188 ApiBase::PARAM_TYPE => ContentHandler::getContentModels(),
189 ApiBase::PARAM_REQUIRED => true
190 ],
191 'contentformat' => [
192 ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
193 ApiBase::PARAM_REQUIRED => true
194 ],
195 'baserevid' => [
196 ApiBase::PARAM_TYPE => 'integer',
197 ApiBase::PARAM_REQUIRED => true
198 ]
199 ];
200 }
201
202 public function needsToken() {
203 return 'csrf';
204 }
205
206 public function mustBePosted() {
207 return true;
208 }
209
210 public function isWriteMode() {
211 return true;
212 }
213
214 public function isInternal() {
215 return true;
216 }
217 }