Merge "Print chained exceptions when maintenance script fails."
[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 use MediaWiki\Storage\PageEditStash;
23
24 /**
25 * Prepare an edit in shared cache so that it can be reused on edit
26 *
27 * This endpoint can be called via AJAX as the user focuses on the edit
28 * summary box. By the time of submission, the parse may have already
29 * finished, and can be immediately used on page save. Certain parser
30 * functions like {{REVISIONID}} or {{CURRENTTIME}} may cause the cache
31 * to not be used on edit. Template and files used are check for changes
32 * since the output was generated. The cache TTL is also kept low for sanity.
33 *
34 * @ingroup API
35 * @since 1.25
36 */
37 class ApiStashEdit extends ApiBase {
38 const ERROR_NONE = PageEditStash::ERROR_NONE; // b/c
39 const ERROR_PARSE = PageEditStash::ERROR_PARSE; // b/c
40 const ERROR_CACHE = PageEditStash::ERROR_CACHE; // b/c
41 const ERROR_UNCACHEABLE = PageEditStash::ERROR_UNCACHEABLE; // b/c
42 const ERROR_BUSY = PageEditStash::ERROR_BUSY; // b/c
43
44 public function execute() {
45 $user = $this->getUser();
46 $params = $this->extractRequestParams();
47
48 if ( $user->isBot() ) { // sanity
49 $this->dieWithError( 'apierror-botsnotsupported' );
50 }
51
52 $editStash = MediaWikiServices::getInstance()->getPageEditStash();
53 $page = $this->getTitleOrPageId( $params );
54 $title = $page->getTitle();
55
56 if ( !ContentHandler::getForModelID( $params['contentmodel'] )
57 ->isSupportedFormat( $params['contentformat'] )
58 ) {
59 $this->dieWithError(
60 [ 'apierror-badformat-generic', $params['contentformat'], $params['contentmodel'] ],
61 'badmodelformat'
62 );
63 }
64
65 $this->requireOnlyOneParameter( $params, 'stashedtexthash', 'text' );
66
67 $text = null;
68 $textHash = null;
69 if ( $params['stashedtexthash'] !== null ) {
70 // Load from cache since the client indicates the text is the same as last stash
71 $textHash = $params['stashedtexthash'];
72 if ( !preg_match( '/^[0-9a-f]{40}$/', $textHash ) ) {
73 $this->dieWithError( 'apierror-stashedit-missingtext', 'missingtext' );
74 }
75 $text = $editStash->fetchInputText( $textHash );
76 if ( !is_string( $text ) ) {
77 $this->dieWithError( 'apierror-stashedit-missingtext', 'missingtext' );
78 }
79 } else {
80 // 'text' was passed. Trim and fix newlines so the key SHA1's
81 // match (see WebRequest::getText())
82 $text = rtrim( str_replace( "\r\n", "\n", $params['text'] ) );
83 $textHash = sha1( $text );
84 }
85
86 $textContent = ContentHandler::makeContent(
87 $text, $title, $params['contentmodel'], $params['contentformat'] );
88
89 $page = WikiPage::factory( $title );
90 if ( $page->exists() ) {
91 // Page exists: get the merged content with the proposed change
92 $baseRev = Revision::newFromPageId( $page->getId(), $params['baserevid'] );
93 if ( !$baseRev ) {
94 $this->dieWithError( [ 'apierror-nosuchrevid', $params['baserevid'] ] );
95 }
96 $currentRev = $page->getRevision();
97 if ( !$currentRev ) {
98 $this->dieWithError( [ 'apierror-missingrev-pageid', $page->getId() ], 'missingrev' );
99 }
100 // Merge in the new version of the section to get the proposed version
101 $editContent = $page->replaceSectionAtRev(
102 $params['section'],
103 $textContent,
104 $params['sectiontitle'],
105 $baseRev->getId()
106 );
107 if ( !$editContent ) {
108 $this->dieWithError( 'apierror-sectionreplacefailed', 'replacefailed' );
109 }
110 if ( $currentRev->getId() == $baseRev->getId() ) {
111 // Base revision was still the latest; nothing to merge
112 $content = $editContent;
113 } else {
114 // Merge the edit into the current version
115 $baseContent = $baseRev->getContent();
116 $currentContent = $currentRev->getContent();
117 if ( !$baseContent || !$currentContent ) {
118 $this->dieWithError( [ 'apierror-missingcontent-pageid', $page->getId() ], 'missingrev' );
119 }
120 $handler = ContentHandler::getForModelID( $baseContent->getModel() );
121 $content = $handler->merge3( $baseContent, $editContent, $currentContent );
122 }
123 } else {
124 // New pages: use the user-provided content model
125 $content = $textContent;
126 }
127
128 if ( !$content ) { // merge3() failed
129 $this->getResult()->addValue( null,
130 $this->getModuleName(), [ 'status' => 'editconflict' ] );
131 return;
132 }
133
134 // The user will abort the AJAX request by pressing "save", so ignore that
135 ignore_user_abort( true );
136
137 if ( $user->pingLimiter( 'stashedit' ) ) {
138 $status = 'ratelimited';
139 } else {
140 $status = $editStash->parseAndCache( $page, $content, $user, $params['summary'] );
141 $editStash->stashInputText( $text, $textHash );
142 }
143
144 $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
145 $stats->increment( "editstash.cache_stores.$status" );
146
147 $ret = [ 'status' => $status ];
148 // If we were rate-limited, we still return the pre-existing valid hash if one was passed
149 if ( $status !== 'ratelimited' || $params['stashedtexthash'] !== null ) {
150 $ret['texthash'] = $textHash;
151 }
152
153 $this->getResult()->addValue( null, $this->getModuleName(), $ret );
154 }
155
156 /**
157 * @param WikiPage $page
158 * @param Content $content Edit content
159 * @param User $user
160 * @param string $summary Edit summary
161 * @return string ApiStashEdit::ERROR_* constant
162 * @since 1.25
163 * @deprecated Since 1.34
164 */
165 public function parseAndStash( WikiPage $page, Content $content, User $user, $summary ) {
166 $editStash = MediaWikiServices::getInstance()->getPageEditStash();
167
168 return $editStash->parseAndCache( $page, $content, $user, $summary );
169 }
170
171 public function getAllowedParams() {
172 return [
173 'title' => [
174 ApiBase::PARAM_TYPE => 'string',
175 ApiBase::PARAM_REQUIRED => true
176 ],
177 'section' => [
178 ApiBase::PARAM_TYPE => 'string',
179 ],
180 'sectiontitle' => [
181 ApiBase::PARAM_TYPE => 'string'
182 ],
183 'text' => [
184 ApiBase::PARAM_TYPE => 'text',
185 ApiBase::PARAM_DFLT => null
186 ],
187 'stashedtexthash' => [
188 ApiBase::PARAM_TYPE => 'string',
189 ApiBase::PARAM_DFLT => null
190 ],
191 'summary' => [
192 ApiBase::PARAM_TYPE => 'string',
193 ],
194 'contentmodel' => [
195 ApiBase::PARAM_TYPE => ContentHandler::getContentModels(),
196 ApiBase::PARAM_REQUIRED => true
197 ],
198 'contentformat' => [
199 ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
200 ApiBase::PARAM_REQUIRED => true
201 ],
202 'baserevid' => [
203 ApiBase::PARAM_TYPE => 'integer',
204 ApiBase::PARAM_REQUIRED => true
205 ]
206 ];
207 }
208
209 public function needsToken() {
210 return 'csrf';
211 }
212
213 public function mustBePosted() {
214 return true;
215 }
216
217 public function isWriteMode() {
218 return true;
219 }
220
221 public function isInternal() {
222 return true;
223 }
224 }