Merge "Make DBAccessBase use DBConnRef, rename $wiki, and hide getLoadBalancer()"
[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 if ( $user->pingLimiter( 'stashedit' ) ) {
135 $status = 'ratelimited';
136 } else {
137 $status = $editStash->parseAndCache( $page, $content, $user, $params['summary'] );
138 $editStash->stashInputText( $text, $textHash );
139 }
140
141 $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
142 $stats->increment( "editstash.cache_stores.$status" );
143
144 $ret = [ 'status' => $status ];
145 // If we were rate-limited, we still return the pre-existing valid hash if one was passed
146 if ( $status !== 'ratelimited' || $params['stashedtexthash'] !== null ) {
147 $ret['texthash'] = $textHash;
148 }
149
150 $this->getResult()->addValue( null, $this->getModuleName(), $ret );
151 }
152
153 /**
154 * @param WikiPage $page
155 * @param Content $content Edit content
156 * @param User $user
157 * @param string $summary Edit summary
158 * @return string ApiStashEdit::ERROR_* constant
159 * @since 1.25
160 * @deprecated Since 1.34
161 */
162 public function parseAndStash( WikiPage $page, Content $content, User $user, $summary ) {
163 $editStash = MediaWikiServices::getInstance()->getPageEditStash();
164
165 return $editStash->parseAndCache( $page, $content, $user, $summary );
166 }
167
168 public function getAllowedParams() {
169 return [
170 'title' => [
171 ApiBase::PARAM_TYPE => 'string',
172 ApiBase::PARAM_REQUIRED => true
173 ],
174 'section' => [
175 ApiBase::PARAM_TYPE => 'string',
176 ],
177 'sectiontitle' => [
178 ApiBase::PARAM_TYPE => 'string'
179 ],
180 'text' => [
181 ApiBase::PARAM_TYPE => 'text',
182 ApiBase::PARAM_DFLT => null
183 ],
184 'stashedtexthash' => [
185 ApiBase::PARAM_TYPE => 'string',
186 ApiBase::PARAM_DFLT => null
187 ],
188 'summary' => [
189 ApiBase::PARAM_TYPE => 'string',
190 ],
191 'contentmodel' => [
192 ApiBase::PARAM_TYPE => ContentHandler::getContentModels(),
193 ApiBase::PARAM_REQUIRED => true
194 ],
195 'contentformat' => [
196 ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
197 ApiBase::PARAM_REQUIRED => true
198 ],
199 'baserevid' => [
200 ApiBase::PARAM_TYPE => 'integer',
201 ApiBase::PARAM_REQUIRED => true
202 ]
203 ];
204 }
205
206 public function needsToken() {
207 return 'csrf';
208 }
209
210 public function mustBePosted() {
211 return true;
212 }
213
214 public function isWriteMode() {
215 return true;
216 }
217
218 public function isInternal() {
219 return true;
220 }
221 }