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
;
24 * Prepare an edit in shared cache so that it can be reused on edit
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.
36 class ApiStashEdit
extends ApiBase
{
37 public function execute() {
38 $user = $this->getUser();
39 $params = $this->extractRequestParams();
41 if ( $user->isBot() ) { // sanity
42 $this->dieWithError( 'apierror-botsnotsupported' );
45 $editStash = MediaWikiServices
::getInstance()->getPageEditStash();
46 $page = $this->getTitleOrPageId( $params );
47 $title = $page->getTitle();
49 if ( !ContentHandler
::getForModelID( $params['contentmodel'] )
50 ->isSupportedFormat( $params['contentformat'] )
53 [ 'apierror-badformat-generic', $params['contentformat'], $params['contentmodel'] ],
58 $this->requireOnlyOneParameter( $params, 'stashedtexthash', 'text' );
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' );
68 $text = $editStash->fetchInputText( $textHash );
69 if ( !is_string( $text ) ) {
70 $this->dieWithError( 'apierror-stashedit-missingtext', 'missingtext' );
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 );
79 $textContent = ContentHandler
::makeContent(
80 $text, $title, $params['contentmodel'], $params['contentformat'] );
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'] );
87 $this->dieWithError( [ 'apierror-nosuchrevid', $params['baserevid'] ] );
89 $currentRev = $page->getRevision();
91 $this->dieWithError( [ 'apierror-missingrev-pageid', $page->getId() ], 'missingrev' );
93 // Merge in the new version of the section to get the proposed version
94 $editContent = $page->replaceSectionAtRev(
97 $params['sectiontitle'],
100 if ( !$editContent ) {
101 $this->dieWithError( 'apierror-sectionreplacefailed', 'replacefailed' );
103 if ( $currentRev->getId() == $baseRev->getId() ) {
104 // Base revision was still the latest; nothing to merge
105 $content = $editContent;
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' );
113 $handler = ContentHandler
::getForModelID( $baseContent->getModel() );
114 $content = $handler->merge3( $baseContent, $editContent, $currentContent );
117 // New pages: use the user-provided content model
118 $content = $textContent;
121 if ( !$content ) { // merge3() failed
122 $this->getResult()->addValue( null,
123 $this->getModuleName(), [ 'status' => 'editconflict' ] );
127 // The user will abort the AJAX request by pressing "save", so ignore that
128 ignore_user_abort( true );
130 if ( $user->pingLimiter( 'stashedit' ) ) {
131 $status = 'ratelimited';
133 $status = $editStash->parseAndCache( $page, $content, $user, $params['summary'] );
134 $editStash->stashInputText( $text, $textHash );
137 $stats = MediaWikiServices
::getInstance()->getStatsdDataFactory();
138 $stats->increment( "editstash.cache_stores.$status" );
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;
146 $this->getResult()->addValue( null, $this->getModuleName(), $ret );
150 * @param WikiPage $page
151 * @param Content $content Edit content
153 * @param string $summary Edit summary
154 * @return string ApiStashEdit::ERROR_* constant
156 * @deprecated Since 1.34
158 public function parseAndStash( WikiPage
$page, Content
$content, User
$user, $summary ) {
159 $editStash = MediaWikiServices
::getInstance()->getPageEditStash();
161 return $editStash->parseAndCache( $page, $content, $user, $summary );
164 public function getAllowedParams() {
167 ApiBase
::PARAM_TYPE
=> 'string',
168 ApiBase
::PARAM_REQUIRED
=> true
171 ApiBase
::PARAM_TYPE
=> 'string',
174 ApiBase
::PARAM_TYPE
=> 'string'
177 ApiBase
::PARAM_TYPE
=> 'text',
178 ApiBase
::PARAM_DFLT
=> null
180 'stashedtexthash' => [
181 ApiBase
::PARAM_TYPE
=> 'string',
182 ApiBase
::PARAM_DFLT
=> null
185 ApiBase
::PARAM_TYPE
=> 'string',
188 ApiBase
::PARAM_TYPE
=> ContentHandler
::getContentModels(),
189 ApiBase
::PARAM_REQUIRED
=> true
192 ApiBase
::PARAM_TYPE
=> ContentHandler
::getAllContentFormats(),
193 ApiBase
::PARAM_REQUIRED
=> true
196 ApiBase
::PARAM_TYPE
=> 'integer',
197 ApiBase
::PARAM_REQUIRED
=> true
202 public function needsToken() {
206 public function mustBePosted() {
210 public function isWriteMode() {
214 public function isInternal() {