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.
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.
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
21 use MediaWiki\MediaWikiServices
;
22 use MediaWiki\Storage\PageEditStash
;
25 * Prepare an edit in shared cache so that it can be reused on edit
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.
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
44 public function execute() {
45 $user = $this->getUser();
46 $params = $this->extractRequestParams();
48 if ( $user->isBot() ) { // sanity
49 $this->dieWithError( 'apierror-botsnotsupported' );
52 $editStash = MediaWikiServices
::getInstance()->getPageEditStash();
53 $page = $this->getTitleOrPageId( $params );
54 $title = $page->getTitle();
56 if ( !ContentHandler
::getForModelID( $params['contentmodel'] )
57 ->isSupportedFormat( $params['contentformat'] )
60 [ 'apierror-badformat-generic', $params['contentformat'], $params['contentmodel'] ],
65 $this->requireOnlyOneParameter( $params, 'stashedtexthash', 'text' );
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' );
75 $text = $editStash->fetchInputText( $textHash );
76 if ( !is_string( $text ) ) {
77 $this->dieWithError( 'apierror-stashedit-missingtext', 'missingtext' );
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 );
86 $textContent = ContentHandler
::makeContent(
87 $text, $title, $params['contentmodel'], $params['contentformat'] );
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'] );
94 $this->dieWithError( [ 'apierror-nosuchrevid', $params['baserevid'] ] );
96 $currentRev = $page->getRevision();
98 $this->dieWithError( [ 'apierror-missingrev-pageid', $page->getId() ], 'missingrev' );
100 // Merge in the new version of the section to get the proposed version
101 $editContent = $page->replaceSectionAtRev(
104 $params['sectiontitle'],
107 if ( !$editContent ) {
108 $this->dieWithError( 'apierror-sectionreplacefailed', 'replacefailed' );
110 if ( $currentRev->getId() == $baseRev->getId() ) {
111 // Base revision was still the latest; nothing to merge
112 $content = $editContent;
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' );
120 $handler = ContentHandler
::getForModelID( $baseContent->getModel() );
121 $content = $handler->merge3( $baseContent, $editContent, $currentContent );
124 // New pages: use the user-provided content model
125 $content = $textContent;
128 if ( !$content ) { // merge3() failed
129 $this->getResult()->addValue( null,
130 $this->getModuleName(), [ 'status' => 'editconflict' ] );
134 // The user will abort the AJAX request by pressing "save", so ignore that
135 ignore_user_abort( true );
137 if ( $user->pingLimiter( 'stashedit' ) ) {
138 $status = 'ratelimited';
140 $status = $editStash->parseAndCache( $page, $content, $user, $params['summary'] );
141 $editStash->stashInputText( $text, $textHash );
144 $stats = MediaWikiServices
::getInstance()->getStatsdDataFactory();
145 $stats->increment( "editstash.cache_stores.$status" );
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;
153 $this->getResult()->addValue( null, $this->getModuleName(), $ret );
157 * @param WikiPage $page
158 * @param Content $content Edit content
160 * @param string $summary Edit summary
161 * @return string ApiStashEdit::ERROR_* constant
163 * @deprecated Since 1.34
165 public function parseAndStash( WikiPage
$page, Content
$content, User
$user, $summary ) {
166 $editStash = MediaWikiServices
::getInstance()->getPageEditStash();
168 return $editStash->parseAndCache( $page, $content, $user, $summary );
171 public function getAllowedParams() {
174 ApiBase
::PARAM_TYPE
=> 'string',
175 ApiBase
::PARAM_REQUIRED
=> true
178 ApiBase
::PARAM_TYPE
=> 'string',
181 ApiBase
::PARAM_TYPE
=> 'string'
184 ApiBase
::PARAM_TYPE
=> 'text',
185 ApiBase
::PARAM_DFLT
=> null
187 'stashedtexthash' => [
188 ApiBase
::PARAM_TYPE
=> 'string',
189 ApiBase
::PARAM_DFLT
=> null
192 ApiBase
::PARAM_TYPE
=> 'string',
195 ApiBase
::PARAM_TYPE
=> ContentHandler
::getContentModels(),
196 ApiBase
::PARAM_REQUIRED
=> true
199 ApiBase
::PARAM_TYPE
=> ContentHandler
::getAllContentFormats(),
200 ApiBase
::PARAM_REQUIRED
=> true
203 ApiBase
::PARAM_TYPE
=> 'integer',
204 ApiBase
::PARAM_REQUIRED
=> true
209 public function needsToken() {
213 public function mustBePosted() {
217 public function isWriteMode() {
221 public function isInternal() {