| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Api; |
| 22 |
|
| 23 |
use Exception; |
| 24 |
use MediaWiki\Content\IContentHandlerFactory; |
| 25 |
use MediaWiki\Page\WikiPageFactory; |
| 26 |
use MediaWiki\Revision\RevisionLookup; |
| 27 |
use MediaWiki\Revision\SlotRecord; |
| 28 |
use MediaWiki\Storage\PageEditStash; |
| 29 |
use MediaWiki\User\TempUser\TempUserCreator; |
| 30 |
use MediaWiki\User\UserFactory; |
| 31 |
use Wikimedia\ParamValidator\ParamValidator; |
| 32 |
use Wikimedia\Stats\StatsFactory; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
class ApiStashEdit extends ApiBase { |
| 48 |
|
| 49 |
private IContentHandlerFactory $contentHandlerFactory; |
| 50 |
private PageEditStash $pageEditStash; |
| 51 |
private RevisionLookup $revisionLookup; |
| 52 |
private StatsFactory $stats; |
| 53 |
private WikiPageFactory $wikiPageFactory; |
| 54 |
private TempUserCreator $tempUserCreator; |
| 55 |
private UserFactory $userFactory; |
| 56 |
|
| 57 |
public function __construct( |
| 58 |
ApiMain $main, |
| 59 |
string $action, |
| 60 |
IContentHandlerFactory $contentHandlerFactory, |
| 61 |
PageEditStash $pageEditStash, |
| 62 |
RevisionLookup $revisionLookup, |
| 63 |
StatsFactory $statsFactory, |
| 64 |
WikiPageFactory $wikiPageFactory, |
| 65 |
TempUserCreator $tempUserCreator, |
| 66 |
UserFactory $userFactory |
| 67 |
) { |
| 68 |
parent::__construct( $main, $action ); |
| 69 |
|
| 70 |
$this->contentHandlerFactory = $contentHandlerFactory; |
| 71 |
$this->pageEditStash = $pageEditStash; |
| 72 |
$this->revisionLookup = $revisionLookup; |
| 73 |
$this->stats = $statsFactory; |
| 74 |
$this->wikiPageFactory = $wikiPageFactory; |
| 75 |
$this->tempUserCreator = $tempUserCreator; |
| 76 |
$this->userFactory = $userFactory; |
| 77 |
} |
| 78 |
|
| 79 |
public function execute() { |
| 80 |
$user = $this->getUser(); |
| 81 |
$params = $this->extractRequestParams(); |
| 82 |
|
| 83 |
if ( $user->isBot() ) { |
| 84 |
$this->dieWithError( 'apierror-botsnotsupported' ); |
| 85 |
} |
| 86 |
|
| 87 |
$page = $this->getTitleOrPageId( $params ); |
| 88 |
$title = $page->getTitle(); |
| 89 |
$this->getErrorFormatter()->setContextTitle( $title ); |
| 90 |
|
| 91 |
if ( !$this->contentHandlerFactory |
| 92 |
->getContentHandler( $params['contentmodel'] ) |
| 93 |
->isSupportedFormat( $params['contentformat'] ) |
| 94 |
) { |
| 95 |
$this->dieWithError( |
| 96 |
[ 'apierror-badformat-generic', $params['contentformat'], $params['contentmodel'] ], |
| 97 |
'badmodelformat' |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
$this->requireOnlyOneParameter( $params, 'stashedtexthash', 'text' ); |
| 102 |
|
| 103 |
if ( $params['stashedtexthash'] !== null ) { |
| 104 |
|
| 105 |
$textHash = $params['stashedtexthash']; |
| 106 |
if ( !preg_match( '/^[0-9a-f]{40}$/', $textHash ) ) { |
| 107 |
$this->dieWithError( 'apierror-stashedit-missingtext', 'missingtext' ); |
| 108 |
} |
| 109 |
$text = $this->pageEditStash->fetchInputText( $textHash ); |
| 110 |
if ( !is_string( $text ) ) { |
| 111 |
$this->dieWithError( 'apierror-stashedit-missingtext', 'missingtext' ); |
| 112 |
} |
| 113 |
} else { |
| 114 |
|
| 115 |
|
| 116 |
$text = rtrim( str_replace( "\r\n", "\n", $params['text'] ) ); |
| 117 |
$textHash = sha1( $text ); |
| 118 |
} |
| 119 |
|
| 120 |
$textContent = $this->contentHandlerFactory |
| 121 |
->getContentHandler( $params['contentmodel'] ) |
| 122 |
->unserializeContent( $text, $params['contentformat'] ); |
| 123 |
|
| 124 |
$page = $this->wikiPageFactory->newFromTitle( $title ); |
| 125 |
if ( $page->exists() ) { |
| 126 |
|
| 127 |
$baseRev = $this->revisionLookup->getRevisionByPageId( |
| 128 |
$page->getId(), |
| 129 |
$params['baserevid'] |
| 130 |
); |
| 131 |
if ( !$baseRev ) { |
| 132 |
$this->dieWithError( [ 'apierror-nosuchrevid', $params['baserevid'] ] ); |
| 133 |
} |
| 134 |
$currentRev = $page->getRevisionRecord(); |
| 135 |
if ( !$currentRev ) { |
| 136 |
$this->dieWithError( [ 'apierror-missingrev-pageid', $page->getId() ], 'missingrev' ); |
| 137 |
} |
| 138 |
|
| 139 |
$editContent = $page->replaceSectionAtRev( |
| 140 |
$params['section'], |
| 141 |
$textContent, |
| 142 |
$params['sectiontitle'], |
| 143 |
$baseRev->getId() |
| 144 |
); |
| 145 |
if ( !$editContent ) { |
| 146 |
$this->dieWithError( 'apierror-sectionreplacefailed', 'replacefailed' ); |
| 147 |
} |
| 148 |
if ( $currentRev->getId() == $baseRev->getId() ) { |
| 149 |
|
| 150 |
$content = $editContent; |
| 151 |
} else { |
| 152 |
|
| 153 |
$baseContent = $baseRev->getContent( SlotRecord::MAIN ); |
| 154 |
$currentContent = $currentRev->getContent( SlotRecord::MAIN ); |
| 155 |
if ( !$baseContent || !$currentContent ) { |
| 156 |
$this->dieWithError( [ 'apierror-missingcontent-pageid', $page->getId() ], 'missingrev' ); |
| 157 |
} |
| 158 |
|
| 159 |
$baseModel = $baseContent->getModel(); |
| 160 |
$currentModel = $currentContent->getModel(); |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
try { |
| 165 |
$content = $this->contentHandlerFactory |
| 166 |
->getContentHandler( $baseModel ) |
| 167 |
->merge3( $baseContent, $editContent, $currentContent ); |
| 168 |
} catch ( Exception $e ) { |
| 169 |
$this->dieWithException( $e, [ |
| 170 |
'wrap' => ApiMessage::create( |
| 171 |
[ 'apierror-contentmodel-mismatch', $currentModel, $baseModel ] |
| 172 |
) |
| 173 |
] ); |
| 174 |
} |
| 175 |
|
| 176 |
} |
| 177 |
} else { |
| 178 |
|
| 179 |
$content = $textContent; |
| 180 |
} |
| 181 |
|
| 182 |
if ( !$content ) { |
| 183 |
$this->getResult()->addValue( null, |
| 184 |
$this->getModuleName(), [ 'status' => 'editconflict' ] ); |
| 185 |
return; |
| 186 |
} |
| 187 |
|
| 188 |
if ( !$user->authorizeWrite( 'stashedit', $title ) ) { |
| 189 |
$status = 'ratelimited'; |
| 190 |
} else { |
| 191 |
$user = $this->getUserForPreview(); |
| 192 |
$updater = $page->newPageUpdater( $user ); |
| 193 |
$status = $this->pageEditStash->parseAndCache( $updater, $content, $user, $params['summary'] ); |
| 194 |
$this->pageEditStash->stashInputText( $text, $textHash ); |
| 195 |
} |
| 196 |
|
| 197 |
$this->stats->getCounter( 'editstash_cache_stores_total' ) |
| 198 |
->setLabel( 'status', $status ) |
| 199 |
->copyToStatsdAt( "editstash.cache_stores.$status" ) |
| 200 |
->increment(); |
| 201 |
|
| 202 |
$ret = [ 'status' => $status ]; |
| 203 |
|
| 204 |
if ( $status !== 'ratelimited' || $params['stashedtexthash'] !== null ) { |
| 205 |
$ret['texthash'] = $textHash; |
| 206 |
} |
| 207 |
|
| 208 |
$this->getResult()->addValue( null, $this->getModuleName(), $ret ); |
| 209 |
} |
| 210 |
|
| 211 |
private function getUserForPreview() { |
| 212 |
$user = $this->getUser(); |
| 213 |
if ( $this->tempUserCreator->shouldAutoCreate( $user, 'edit' ) ) { |
| 214 |
return $this->userFactory->newUnsavedTempUser( |
| 215 |
$this->tempUserCreator->getStashedName( $this->getRequest()->getSession() ) |
| 216 |
); |
| 217 |
} |
| 218 |
return $user; |
| 219 |
} |
| 220 |
|
| 221 |
public function getAllowedParams() { |
| 222 |
return [ |
| 223 |
'title' => [ |
| 224 |
ParamValidator::PARAM_TYPE => 'string', |
| 225 |
ParamValidator::PARAM_REQUIRED => true |
| 226 |
], |
| 227 |
'section' => [ |
| 228 |
ParamValidator::PARAM_TYPE => 'string', |
| 229 |
], |
| 230 |
'sectiontitle' => [ |
| 231 |
ParamValidator::PARAM_TYPE => 'string' |
| 232 |
], |
| 233 |
'text' => [ |
| 234 |
ParamValidator::PARAM_TYPE => 'text', |
| 235 |
ParamValidator::PARAM_DEFAULT => null |
| 236 |
], |
| 237 |
'stashedtexthash' => [ |
| 238 |
ParamValidator::PARAM_TYPE => 'string', |
| 239 |
ParamValidator::PARAM_DEFAULT => null |
| 240 |
], |
| 241 |
'summary' => [ |
| 242 |
ParamValidator::PARAM_TYPE => 'string', |
| 243 |
ParamValidator::PARAM_DEFAULT => '' |
| 244 |
], |
| 245 |
'contentmodel' => [ |
| 246 |
ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getContentModels(), |
| 247 |
ParamValidator::PARAM_REQUIRED => true |
| 248 |
], |
| 249 |
'contentformat' => [ |
| 250 |
ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(), |
| 251 |
ParamValidator::PARAM_REQUIRED => true |
| 252 |
], |
| 253 |
'baserevid' => [ |
| 254 |
ParamValidator::PARAM_TYPE => 'integer', |
| 255 |
ParamValidator::PARAM_REQUIRED => true |
| 256 |
] |
| 257 |
]; |
| 258 |
} |
| 259 |
|
| 260 |
public function needsToken() { |
| 261 |
return 'csrf'; |
| 262 |
} |
| 263 |
|
| 264 |
public function mustBePosted() { |
| 265 |
return true; |
| 266 |
} |
| 267 |
|
| 268 |
public function isWriteMode() { |
| 269 |
return true; |
| 270 |
} |
| 271 |
|
| 272 |
public function isInternal() { |
| 273 |
return true; |
| 274 |
} |
| 275 |
|
| 276 |
public function getHelpUrls() { |
| 277 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Stashedit'; |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
class_alias( ApiStashEdit::class, 'ApiStashEdit' ); |