Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
91.44% |
363 / 397 |
|
70.00% |
7 / 10 |
CRAP | |
0.00% |
0 / 1 |
| ApiEditPage | |
91.67% |
363 / 396 |
|
70.00% |
7 / 10 |
128.33 | |
0.00% |
0 / 1 |
| persistGlobalSession | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __construct | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
1 | |||
| getUserForPermissions | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| execute | |
92.55% |
261 / 282 |
|
0.00% |
0 / 1 |
115.00 | |||
| mustBePosted | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isWriteMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getAllowedParams | |
100.00% |
78 / 78 |
|
100.00% |
1 / 1 |
1 | |||
| needsToken | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getExamplesMessages | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
2 | |||
| getHelpUrls | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Copyright © 2007 Iker Labarga "<Firstname><Lastname>@gmail.com" |
| 4 | * |
| 5 | * @license GPL-2.0-or-later |
| 6 | * @file |
| 7 | */ |
| 8 | |
| 9 | namespace MediaWiki\Api; |
| 10 | |
| 11 | use MediaWiki\ChangeTags\ChangeTags; |
| 12 | use MediaWiki\Content\ContentHandler; |
| 13 | use MediaWiki\Content\IContentHandlerFactory; |
| 14 | use MediaWiki\Content\TextContent; |
| 15 | use MediaWiki\Context\RequestContext; |
| 16 | use MediaWiki\EditPage\EditPage; |
| 17 | use MediaWiki\Exception\MWContentSerializationException; |
| 18 | use MediaWiki\MainConfigNames; |
| 19 | use MediaWiki\MediaWikiServices; |
| 20 | use MediaWiki\Message\Message; |
| 21 | use MediaWiki\Page\Article; |
| 22 | use MediaWiki\Page\RedirectLookup; |
| 23 | use MediaWiki\Page\WikiPageFactory; |
| 24 | use MediaWiki\Request\DerivativeRequest; |
| 25 | use MediaWiki\Revision\RevisionLookup; |
| 26 | use MediaWiki\Revision\RevisionRecord; |
| 27 | use MediaWiki\Revision\SlotRecord; |
| 28 | use MediaWiki\Title\Title; |
| 29 | use MediaWiki\User\Options\UserOptionsLookup; |
| 30 | use MediaWiki\User\TempUser\TempUserCreator; |
| 31 | use MediaWiki\User\User; |
| 32 | use MediaWiki\User\UserFactory; |
| 33 | use MediaWiki\Watchlist\WatchedItemStoreInterface; |
| 34 | use MediaWiki\Watchlist\WatchlistManager; |
| 35 | use Wikimedia\ParamValidator\ParamValidator; |
| 36 | use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
| 37 | use Wikimedia\Timestamp\TimestampFormat as TS; |
| 38 | |
| 39 | /** |
| 40 | * A module that allows for editing and creating pages. |
| 41 | * |
| 42 | * Currently, this wraps around the EditPage class in an ugly way, |
| 43 | * EditPage.php should be rewritten to provide a cleaner interface, |
| 44 | * see T20654 if you're inspired to fix this. |
| 45 | * |
| 46 | * WARNING: This class is //not// stable to extend. However, it is |
| 47 | * currently extended by the ApiThreadAction class in the LiquidThreads |
| 48 | * extension, which is deployed on WMF servers. Changes that would |
| 49 | * break LiquidThreads will likely be reverted. See T264200 for context |
| 50 | * and T264213 for removing LiquidThreads' unsupported extending of this |
| 51 | * class. |
| 52 | * |
| 53 | * @ingroup API |
| 54 | */ |
| 55 | class ApiEditPage extends ApiBase { |
| 56 | use ApiCreateTempUserTrait; |
| 57 | use ApiWatchlistTrait; |
| 58 | |
| 59 | private IContentHandlerFactory $contentHandlerFactory; |
| 60 | private RevisionLookup $revisionLookup; |
| 61 | private WatchedItemStoreInterface $watchedItemStore; |
| 62 | private WikiPageFactory $wikiPageFactory; |
| 63 | private RedirectLookup $redirectLookup; |
| 64 | private TempUserCreator $tempUserCreator; |
| 65 | private UserFactory $userFactory; |
| 66 | |
| 67 | /** |
| 68 | * Sends a cookie so anons get talk message notifications, mirroring SubmitAction (T295910) |
| 69 | */ |
| 70 | private function persistGlobalSession() { |
| 71 | $this->getRequest()->getSession()->persist(); |
| 72 | } |
| 73 | |
| 74 | public function __construct( |
| 75 | ApiMain $mainModule, |
| 76 | string $moduleName, |
| 77 | ?IContentHandlerFactory $contentHandlerFactory = null, |
| 78 | ?RevisionLookup $revisionLookup = null, |
| 79 | ?WatchedItemStoreInterface $watchedItemStore = null, |
| 80 | ?WikiPageFactory $wikiPageFactory = null, |
| 81 | ?WatchlistManager $watchlistManager = null, |
| 82 | ?UserOptionsLookup $userOptionsLookup = null, |
| 83 | ?RedirectLookup $redirectLookup = null, |
| 84 | ?TempUserCreator $tempUserCreator = null, |
| 85 | ?UserFactory $userFactory = null |
| 86 | ) { |
| 87 | parent::__construct( $mainModule, $moduleName ); |
| 88 | |
| 89 | // This class is extended and therefor fallback to global state - T264213 |
| 90 | $services = MediaWikiServices::getInstance(); |
| 91 | $this->contentHandlerFactory = $contentHandlerFactory ?? $services->getContentHandlerFactory(); |
| 92 | $this->revisionLookup = $revisionLookup ?? $services->getRevisionLookup(); |
| 93 | $this->watchedItemStore = $watchedItemStore ?? $services->getWatchedItemStore(); |
| 94 | $this->wikiPageFactory = $wikiPageFactory ?? $services->getWikiPageFactory(); |
| 95 | |
| 96 | // Variables needed in ApiWatchlistTrait trait |
| 97 | $this->watchlistExpiryEnabled = $this->getConfig()->get( MainConfigNames::WatchlistExpiry ); |
| 98 | $this->watchlistMaxDuration = |
| 99 | $this->getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration ); |
| 100 | $this->watchlistManager = $watchlistManager ?? $services->getWatchlistManager(); |
| 101 | $this->userOptionsLookup = $userOptionsLookup ?? $services->getUserOptionsLookup(); |
| 102 | $this->redirectLookup = $redirectLookup ?? $services->getRedirectLookup(); |
| 103 | $this->tempUserCreator = $tempUserCreator ?? $services->getTempUserCreator(); |
| 104 | $this->userFactory = $userFactory ?? $services->getUserFactory(); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @see EditPage::getUserForPermissions |
| 109 | * @return User |
| 110 | */ |
| 111 | private function getUserForPermissions() { |
| 112 | $user = $this->getUser(); |
| 113 | if ( $this->tempUserCreator->shouldAutoCreate( $user, 'edit' ) ) { |
| 114 | return $this->userFactory->newUnsavedTempUser( |
| 115 | $this->tempUserCreator->getStashedName( $this->getRequest()->getSession() ) |
| 116 | ); |
| 117 | } |
| 118 | return $user; |
| 119 | } |
| 120 | |
| 121 | public function execute() { |
| 122 | $this->useTransactionalTimeLimit(); |
| 123 | |
| 124 | $user = $this->getUser(); |
| 125 | $params = $this->extractRequestParams(); |
| 126 | |
| 127 | $this->requireAtLeastOneParameter( $params, 'text', 'appendtext', 'prependtext', 'undo' ); |
| 128 | |
| 129 | $pageObj = $this->getTitleOrPageId( $params ); |
| 130 | $titleObj = $pageObj->getTitle(); |
| 131 | $this->getErrorFormatter()->setContextTitle( $titleObj ); |
| 132 | $apiResult = $this->getResult(); |
| 133 | |
| 134 | if ( $params['redirect'] ) { |
| 135 | if ( $params['prependtext'] === null |
| 136 | && $params['appendtext'] === null |
| 137 | && $params['section'] !== 'new' |
| 138 | ) { |
| 139 | $this->dieWithError( 'apierror-redirect-appendonly' ); |
| 140 | } |
| 141 | if ( $titleObj->isRedirect() ) { |
| 142 | $oldTarget = $titleObj; |
| 143 | $redirTarget = $this->redirectLookup->getRedirectTarget( $oldTarget ); |
| 144 | $redirTarget = Title::castFromLinkTarget( $redirTarget ); |
| 145 | |
| 146 | $redirValues = [ |
| 147 | 'from' => $titleObj->getPrefixedText(), |
| 148 | 'to' => $redirTarget->getPrefixedText() |
| 149 | ]; |
| 150 | |
| 151 | // T239428: Check whether the new title is valid |
| 152 | if ( $redirTarget->isExternal() || !$redirTarget->canExist() ) { |
| 153 | $redirValues['to'] = $redirTarget->getFullText(); |
| 154 | $this->dieWithError( |
| 155 | [ |
| 156 | 'apierror-edit-invalidredirect', |
| 157 | Message::plaintextParam( $oldTarget->getPrefixedText() ), |
| 158 | Message::plaintextParam( $redirTarget->getFullText() ), |
| 159 | ], |
| 160 | 'edit-invalidredirect', |
| 161 | [ 'redirects' => $redirValues ] |
| 162 | ); |
| 163 | } |
| 164 | |
| 165 | ApiResult::setIndexedTagName( $redirValues, 'r' ); |
| 166 | $apiResult->addValue( null, 'redirects', $redirValues ); |
| 167 | |
| 168 | // Since the page changed, update $pageObj and $titleObj |
| 169 | $pageObj = $this->wikiPageFactory->newFromTitle( $redirTarget ); |
| 170 | $titleObj = $pageObj->getTitle(); |
| 171 | |
| 172 | $this->getErrorFormatter()->setContextTitle( $redirTarget ); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | if ( $params['contentmodel'] ) { |
| 177 | $contentHandler = $this->contentHandlerFactory->getContentHandler( $params['contentmodel'] ); |
| 178 | } else { |
| 179 | $contentHandler = $pageObj->getContentHandler(); |
| 180 | } |
| 181 | $contentModel = $contentHandler->getModelID(); |
| 182 | |
| 183 | $name = $titleObj->getPrefixedDBkey(); |
| 184 | |
| 185 | if ( $params['undo'] > 0 ) { |
| 186 | // allow undo via api |
| 187 | } elseif ( $contentHandler->supportsDirectApiEditing() === false ) { |
| 188 | $this->dieWithError( [ 'apierror-no-direct-editing', $contentModel, $name ] ); |
| 189 | } |
| 190 | |
| 191 | $contentFormat = $params['contentformat'] ?: $contentHandler->getDefaultFormat(); |
| 192 | |
| 193 | if ( !$contentHandler->isSupportedFormat( $contentFormat ) ) { |
| 194 | $this->dieWithError( [ 'apierror-badformat', $contentFormat, $contentModel, $name ] ); |
| 195 | } |
| 196 | |
| 197 | if ( $params['createonly'] && $titleObj->exists() ) { |
| 198 | $this->dieWithError( 'apierror-articleexists' ); |
| 199 | } |
| 200 | if ( $params['nocreate'] && !$titleObj->exists() ) { |
| 201 | $this->dieWithError( 'apierror-missingtitle' ); |
| 202 | } |
| 203 | |
| 204 | // Now let's check whether we're even allowed to do this |
| 205 | $this->checkTitleUserPermissions( |
| 206 | $titleObj, |
| 207 | 'edit', |
| 208 | [ 'autoblock' => true, 'user' => $this->getUserForPermissions() ] |
| 209 | ); |
| 210 | |
| 211 | $toMD5 = $params['text']; |
| 212 | if ( $params['appendtext'] !== null || $params['prependtext'] !== null ) { |
| 213 | $content = $pageObj->getContent(); |
| 214 | |
| 215 | if ( !$content ) { |
| 216 | if ( $titleObj->getNamespace() === NS_MEDIAWIKI ) { |
| 217 | # If this is a MediaWiki:x message, then load the messages |
| 218 | # and return the message value for x. |
| 219 | $text = $titleObj->getDefaultMessageText(); |
| 220 | if ( $text === false ) { |
| 221 | $text = ''; |
| 222 | } |
| 223 | |
| 224 | try { |
| 225 | $content = ContentHandler::makeContent( $text, $titleObj ); |
| 226 | } catch ( MWContentSerializationException $ex ) { |
| 227 | $this->dieWithException( $ex, [ |
| 228 | 'wrap' => ApiMessage::create( 'apierror-contentserializationexception', 'parseerror' ) |
| 229 | ] ); |
| 230 | } |
| 231 | } else { |
| 232 | # Otherwise, make a new empty content. |
| 233 | $content = $contentHandler->makeEmptyContent(); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | // @todo Add support for appending/prepending to the Content interface |
| 238 | |
| 239 | if ( !( $content instanceof TextContent ) ) { |
| 240 | $this->dieWithError( [ 'apierror-appendnotsupported', $contentModel ] ); |
| 241 | } |
| 242 | |
| 243 | if ( $params['section'] !== null ) { |
| 244 | if ( !$contentHandler->supportsSections() ) { |
| 245 | $this->dieWithError( [ 'apierror-sectionsnotsupported', $contentModel ] ); |
| 246 | } |
| 247 | |
| 248 | if ( $params['section'] == 'new' ) { |
| 249 | // DWIM if they're trying to prepend/append to a new section. |
| 250 | $content = null; |
| 251 | } else { |
| 252 | // Process the content for section edits |
| 253 | $section = $params['section']; |
| 254 | $content = $content->getSection( $section ); |
| 255 | |
| 256 | if ( !$content ) { |
| 257 | $this->dieWithError( [ 'apierror-nosuchsection', wfEscapeWikiText( $section ) ] ); |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | if ( !$content ) { |
| 263 | $text = ''; |
| 264 | } else { |
| 265 | $text = $content->serialize( $contentFormat ); |
| 266 | } |
| 267 | |
| 268 | $params['text'] = $params['prependtext'] . $text . $params['appendtext']; |
| 269 | $toMD5 = $params['prependtext'] . $params['appendtext']; |
| 270 | } |
| 271 | |
| 272 | if ( $params['undo'] > 0 ) { |
| 273 | $undoRev = $this->revisionLookup->getRevisionById( $params['undo'] ); |
| 274 | if ( $undoRev === null || $undoRev->isDeleted( RevisionRecord::DELETED_TEXT ) ) { |
| 275 | $this->dieWithError( [ 'apierror-nosuchrevid', $params['undo'] ] ); |
| 276 | } |
| 277 | |
| 278 | if ( $params['undoafter'] > 0 ) { |
| 279 | $undoafterRev = $this->revisionLookup->getRevisionById( $params['undoafter'] ); |
| 280 | } else { |
| 281 | // undoafter=0 or null |
| 282 | $undoafterRev = $this->revisionLookup->getPreviousRevision( $undoRev ); |
| 283 | } |
| 284 | if ( $undoafterRev === null || $undoafterRev->isDeleted( RevisionRecord::DELETED_TEXT ) ) { |
| 285 | $this->dieWithError( [ 'apierror-nosuchrevid', $params['undoafter'] ] ); |
| 286 | } |
| 287 | |
| 288 | if ( $undoRev->getPageId() != $pageObj->getId() ) { |
| 289 | $this->dieWithError( [ 'apierror-revwrongpage', $undoRev->getId(), |
| 290 | $titleObj->getPrefixedText() ] ); |
| 291 | } |
| 292 | if ( $undoafterRev->getPageId() != $pageObj->getId() ) { |
| 293 | $this->dieWithError( [ 'apierror-revwrongpage', $undoafterRev->getId(), |
| 294 | $titleObj->getPrefixedText() ] ); |
| 295 | } |
| 296 | |
| 297 | $newContent = $contentHandler->getUndoContent( |
| 298 | // @phan-suppress-next-line PhanTypeMismatchArgumentNullable Content is for public use here |
| 299 | $pageObj->getRevisionRecord()->getContent( SlotRecord::MAIN ), |
| 300 | // @phan-suppress-next-line PhanTypeMismatchArgumentNullable Content is for public use here |
| 301 | $undoRev->getContent( SlotRecord::MAIN ), |
| 302 | // @phan-suppress-next-line PhanTypeMismatchArgumentNullable Content is for public use here |
| 303 | $undoafterRev->getContent( SlotRecord::MAIN ), |
| 304 | $pageObj->getRevisionRecord()->getId() === $undoRev->getId() |
| 305 | ); |
| 306 | |
| 307 | if ( !$newContent ) { |
| 308 | $this->dieWithError( 'undo-failure', 'undofailure' ); |
| 309 | } |
| 310 | if ( !$params['contentmodel'] && !$params['contentformat'] ) { |
| 311 | // If we are reverting content model, the new content model |
| 312 | // might not support the current serialization format, in |
| 313 | // which case go back to the old serialization format, |
| 314 | // but only if the user hasn't specified a format/model |
| 315 | // parameter. |
| 316 | if ( !$newContent->isSupportedFormat( $contentFormat ) ) { |
| 317 | $undoafterRevMainSlot = $undoafterRev->getSlot( |
| 318 | SlotRecord::MAIN, |
| 319 | RevisionRecord::RAW |
| 320 | ); |
| 321 | $contentFormat = $undoafterRevMainSlot->getFormat(); |
| 322 | if ( !$contentFormat ) { |
| 323 | // fall back to default content format for the model |
| 324 | // of $undoafterRev |
| 325 | $contentFormat = $this->contentHandlerFactory |
| 326 | ->getContentHandler( $undoafterRevMainSlot->getModel() ) |
| 327 | ->getDefaultFormat(); |
| 328 | } |
| 329 | } |
| 330 | // Override content model with model of undid revision. |
| 331 | $contentModel = $newContent->getModel(); |
| 332 | $undoContentModel = true; |
| 333 | } |
| 334 | $params['text'] = $newContent->serialize( $contentFormat ); |
| 335 | // If no summary was given and we only undid one rev, |
| 336 | // use an autosummary |
| 337 | |
| 338 | if ( $params['summary'] === null ) { |
| 339 | $nextRev = $this->revisionLookup->getNextRevision( $undoafterRev ); |
| 340 | if ( $nextRev && $nextRev->getId() == $params['undo'] ) { |
| 341 | $undoRevUser = $undoRev->getUser(); |
| 342 | $params['summary'] = $this->msg( 'undo-summary' ) |
| 343 | ->params( $params['undo'], $undoRevUser ? $undoRevUser->getName() : '' ) |
| 344 | ->inContentLanguage()->text(); |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | // See if the MD5 hash checks out |
| 350 | if ( $params['md5'] !== null && md5( $toMD5 ) !== $params['md5'] ) { |
| 351 | $this->dieWithError( 'apierror-badmd5' ); |
| 352 | } |
| 353 | |
| 354 | // EditPage wants to parse its stuff from a WebRequest |
| 355 | // That interface kind of sucks, but it's workable |
| 356 | $requestArray = [ |
| 357 | // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive |
| 358 | 'wpTextbox1' => $params['text'], |
| 359 | 'format' => $contentFormat, |
| 360 | 'model' => $contentModel, |
| 361 | 'wpEditToken' => $params['token'], |
| 362 | 'wpIgnoreBlankSummary' => true, |
| 363 | 'wpIgnoreBlankArticle' => true, |
| 364 | 'wpIgnoreProblematicRedirects' => true, |
| 365 | 'bot' => $params['bot'], |
| 366 | 'wpUnicodeCheck' => EditPage::UNICODE_CHECK, |
| 367 | ]; |
| 368 | |
| 369 | // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive |
| 370 | if ( $params['summary'] !== null ) { |
| 371 | $requestArray['wpSummary'] = $params['summary']; |
| 372 | } |
| 373 | |
| 374 | if ( $params['sectiontitle'] !== null ) { |
| 375 | $requestArray['wpSectionTitle'] = $params['sectiontitle']; |
| 376 | } |
| 377 | |
| 378 | if ( $params['undo'] > 0 ) { |
| 379 | $requestArray['wpUndidRevision'] = $params['undo']; |
| 380 | } |
| 381 | if ( $params['undoafter'] > 0 ) { |
| 382 | $requestArray['wpUndoAfter'] = $params['undoafter']; |
| 383 | } |
| 384 | |
| 385 | // Skip for baserevid == null or '' or '0' or 0 |
| 386 | if ( !empty( $params['baserevid'] ) ) { |
| 387 | $requestArray['editRevId'] = $params['baserevid']; |
| 388 | } |
| 389 | |
| 390 | // Watch out for basetimestamp == '' or '0' |
| 391 | // It gets treated as NOW, almost certainly causing an edit conflict |
| 392 | if ( $params['basetimestamp'] !== null && (bool)$this->getMain()->getVal( 'basetimestamp' ) ) { |
| 393 | $requestArray['wpEdittime'] = $params['basetimestamp']; |
| 394 | } elseif ( empty( $params['baserevid'] ) ) { |
| 395 | // Only set if baserevid is not set. Otherwise, conflicts would be ignored, |
| 396 | // due to the way userWasLastToEdit() works. |
| 397 | $requestArray['wpEdittime'] = $pageObj->getTimestamp(); |
| 398 | } |
| 399 | |
| 400 | if ( $params['starttimestamp'] !== null ) { |
| 401 | $requestArray['wpStarttime'] = $params['starttimestamp']; |
| 402 | } else { |
| 403 | $requestArray['wpStarttime'] = wfTimestampNow(); // Fake wpStartime |
| 404 | } |
| 405 | |
| 406 | if ( $params['minor'] || ( !$params['notminor'] && |
| 407 | $this->userOptionsLookup->getOption( $user, 'minordefault' ) ) |
| 408 | ) { |
| 409 | $requestArray['wpMinoredit'] = ''; |
| 410 | } |
| 411 | |
| 412 | if ( $params['recreate'] ) { |
| 413 | $requestArray['wpRecreate'] = ''; |
| 414 | } |
| 415 | |
| 416 | if ( $params['section'] !== null ) { |
| 417 | $section = $params['section']; |
| 418 | if ( !preg_match( '/^((T-)?\d+|new)$/', $section ) ) { |
| 419 | $this->dieWithError( 'apierror-invalidsection' ); |
| 420 | } |
| 421 | $content = $pageObj->getContent(); |
| 422 | if ( $section !== '0' |
| 423 | && $section != 'new' |
| 424 | && ( !$content || !$content->getSection( $section ) ) |
| 425 | ) { |
| 426 | $this->dieWithError( [ 'apierror-nosuchsection', $section ] ); |
| 427 | } |
| 428 | $requestArray['wpSection'] = $params['section']; |
| 429 | } else { |
| 430 | $requestArray['wpSection'] = ''; |
| 431 | } |
| 432 | |
| 433 | $watch = $this->getWatchlistValue( $params['watchlist'], $titleObj, $user ); |
| 434 | |
| 435 | // Deprecated parameters |
| 436 | if ( $params['watch'] ) { |
| 437 | $watch = true; |
| 438 | } elseif ( $params['unwatch'] ) { |
| 439 | $watch = false; |
| 440 | } |
| 441 | |
| 442 | if ( $watch ) { |
| 443 | $requestArray['wpWatchthis'] = true; |
| 444 | $prefName = 'watchdefault-expiry'; |
| 445 | if ( !$pageObj->exists() ) { |
| 446 | $prefName = 'watchcreations-expiry'; |
| 447 | } |
| 448 | $watchlistExpiry = $this->getExpiryFromParams( $params, $titleObj, $user, $prefName ); |
| 449 | |
| 450 | if ( $watchlistExpiry ) { |
| 451 | $requestArray['wpWatchlistExpiry'] = $watchlistExpiry; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | // Apply change tags |
| 456 | if ( $params['tags'] ) { |
| 457 | $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() ); |
| 458 | if ( $tagStatus->isOK() ) { |
| 459 | $requestArray['wpChangeTags'] = implode( ',', $params['tags'] ); |
| 460 | } else { |
| 461 | $this->dieStatus( $tagStatus ); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | // Pass through anything else we might have been given, to support extensions |
| 466 | // This is kind of a hack but it's the best we can do to make extensions work |
| 467 | $requestArray += $this->getRequest()->getValues(); |
| 468 | |
| 469 | // phpcs:ignore MediaWiki.Usage.ExtendClassUsage.FunctionVarUsage,MediaWiki.Usage.DeprecatedGlobalVariables.Deprecated$wgTitle |
| 470 | global $wgTitle, $wgRequest; |
| 471 | |
| 472 | $req = new DerivativeRequest( $this->getRequest(), $requestArray, true ); |
| 473 | |
| 474 | // Some functions depend on $wgTitle == $ep->getTitle() |
| 475 | // TODO: Make them not or check if they still do |
| 476 | $wgTitle = $titleObj; |
| 477 | |
| 478 | $articleContext = new RequestContext; |
| 479 | $articleContext->setRequest( $req ); |
| 480 | $articleContext->setWikiPage( $pageObj ); |
| 481 | $articleContext->setUser( $this->getUser() ); |
| 482 | |
| 483 | /** @var Article $articleObject */ |
| 484 | $articleObject = Article::newFromWikiPage( $pageObj, $articleContext ); |
| 485 | |
| 486 | $ep = new EditPage( $articleObject ); |
| 487 | |
| 488 | $ep->setApiEditOverride( true ); |
| 489 | $ep->setContextTitle( $titleObj ); |
| 490 | $ep->importFormData( $req ); |
| 491 | $tempUserCreateStatus = $ep->maybeActivateTempUserCreate( true ); |
| 492 | if ( !$tempUserCreateStatus->isOK() ) { |
| 493 | $this->dieWithError( 'apierror-tempuseracquirefailed', 'tempuseracquirefailed' ); |
| 494 | } |
| 495 | |
| 496 | // T255700: Ensure content models of the base content |
| 497 | // and fetched revision remain the same before attempting to save. |
| 498 | $editRevId = $requestArray['editRevId'] ?? false; |
| 499 | $baseRev = $this->revisionLookup->getRevisionByTitle( $titleObj, $editRevId ); |
| 500 | $baseContentModel = null; |
| 501 | |
| 502 | if ( $baseRev ) { |
| 503 | $baseContent = $baseRev->getContent( SlotRecord::MAIN ); |
| 504 | $baseContentModel = $baseContent ? $baseContent->getModel() : null; |
| 505 | } |
| 506 | |
| 507 | $baseContentModel ??= $pageObj->getContentModel(); |
| 508 | |
| 509 | // However, allow the content models to possibly differ if we are intentionally |
| 510 | // changing them or we are doing an undo edit that is reverting content model change. |
| 511 | $contentModelsCanDiffer = $params['contentmodel'] || isset( $undoContentModel ); |
| 512 | |
| 513 | if ( !$contentModelsCanDiffer && $contentModel !== $baseContentModel ) { |
| 514 | $this->dieWithError( [ 'apierror-contentmodel-mismatch', $contentModel, $baseContentModel ] ); |
| 515 | } |
| 516 | |
| 517 | // Do the actual save |
| 518 | $oldRevId = $articleObject->getRevIdFetched(); |
| 519 | $result = null; |
| 520 | |
| 521 | // Fake $wgRequest for some hooks inside EditPage |
| 522 | // @todo FIXME: This interface SUCKS |
| 523 | // phpcs:disable MediaWiki.Usage.ExtendClassUsage.FunctionVarUsage |
| 524 | $oldRequest = $wgRequest; |
| 525 | $wgRequest = $req; |
| 526 | |
| 527 | $status = $ep->attemptSave( $result ); |
| 528 | $statusValue = is_int( $status->value ) ? $status->value : 0; |
| 529 | $wgRequest = $oldRequest; |
| 530 | // phpcs:enable |
| 531 | |
| 532 | $r = []; |
| 533 | switch ( $statusValue ) { |
| 534 | case EditPage::AS_HOOK_ERROR: |
| 535 | case EditPage::AS_HOOK_ERROR_EXPECTED: |
| 536 | if ( $status->statusData !== null ) { |
| 537 | $r = $status->statusData; |
| 538 | $r['result'] = 'Failure'; |
| 539 | $apiResult->addValue( null, $this->getModuleName(), $r ); |
| 540 | return; |
| 541 | } |
| 542 | if ( !$status->getMessages() ) { |
| 543 | // This appears to be unreachable right now, because all |
| 544 | // code paths will set an error. Could change, though. |
| 545 | $status->fatal( 'hookaborted' ); // @codeCoverageIgnore |
| 546 | } |
| 547 | $this->dieStatus( $status ); |
| 548 | |
| 549 | // These two cases will normally have been caught earlier, and will |
| 550 | // only occur if something blocks the user between the earlier |
| 551 | // check and the check in EditPage (presumably a hook). It's not |
| 552 | // obvious that this is even possible. |
| 553 | // @codeCoverageIgnoreStart |
| 554 | case EditPage::AS_BLOCKED_PAGE_FOR_USER: |
| 555 | // @phan-suppress-next-line PhanTypeMismatchArgumentNullable Block is checked and not null |
| 556 | $this->dieBlocked( $user->getBlock() ); |
| 557 | // dieBlocked prevents continuation |
| 558 | |
| 559 | case EditPage::AS_READ_ONLY_PAGE: |
| 560 | $this->dieReadOnly(); |
| 561 | // @codeCoverageIgnoreEnd |
| 562 | |
| 563 | case EditPage::AS_SUCCESS_NEW_ARTICLE: |
| 564 | $r['new'] = true; |
| 565 | // fall-through |
| 566 | |
| 567 | case EditPage::AS_SUCCESS_UPDATE: |
| 568 | $r['result'] = 'Success'; |
| 569 | $r['pageid'] = (int)$titleObj->getArticleID(); |
| 570 | $r['title'] = $titleObj->getPrefixedText(); |
| 571 | $r['contentmodel'] = $articleObject->getPage()->getContentModel(); |
| 572 | $newRevId = $articleObject->getPage()->getLatest(); |
| 573 | if ( $newRevId == $oldRevId ) { |
| 574 | $r['nochange'] = true; |
| 575 | } else { |
| 576 | $r['oldrevid'] = (int)$oldRevId; |
| 577 | $r['newrevid'] = (int)$newRevId; |
| 578 | $r['newtimestamp'] = wfTimestamp( TS::ISO_8601, |
| 579 | $pageObj->getTimestamp() ); |
| 580 | } |
| 581 | |
| 582 | if ( $watch ) { |
| 583 | $r['watched'] = true; |
| 584 | |
| 585 | $watchlistExpiry = $this->getWatchlistExpiry( |
| 586 | $this->watchedItemStore, |
| 587 | $titleObj, |
| 588 | $user |
| 589 | ); |
| 590 | |
| 591 | if ( $watchlistExpiry ) { |
| 592 | $r['watchlistexpiry'] = $watchlistExpiry; |
| 593 | } |
| 594 | } |
| 595 | $this->persistGlobalSession(); |
| 596 | |
| 597 | // If the temporary account was created in this request, |
| 598 | // or if the temporary account has zero edits (implying |
| 599 | // that the account was created during a failed edit |
| 600 | // attempt in a previous request), perform the top-level |
| 601 | // redirect to ensure the account is attached. |
| 602 | // Note that the temp user could already have performed |
| 603 | // the top-level redirect if this a first edit on |
| 604 | // a wiki that is not the user's home wiki. |
| 605 | $shouldRedirectForTempUser = isset( $result['savedTempUser'] ) || |
| 606 | ( $user->isTemp() && ( $user->getEditCount() === 0 ) ); |
| 607 | if ( $shouldRedirectForTempUser ) { |
| 608 | $r['tempusercreated'] = true; |
| 609 | $params['returnto'] ??= $titleObj->getPrefixedDBkey(); |
| 610 | $redirectUrl = $this->getTempUserRedirectUrl( |
| 611 | $params, |
| 612 | $result['savedTempUser'] ?? $user |
| 613 | ); |
| 614 | if ( $redirectUrl ) { |
| 615 | $r['tempusercreatedredirect'] = $redirectUrl; |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | break; |
| 620 | |
| 621 | default: |
| 622 | if ( !$status->getMessages() ) { |
| 623 | // EditPage sometimes only sets the status code without setting |
| 624 | // any actual error messages. Supply defaults for those cases. |
| 625 | switch ( $statusValue ) { |
| 626 | // Currently needed |
| 627 | case EditPage::AS_IMAGE_REDIRECT_ANON: |
| 628 | $status->fatal( 'apierror-noimageredirect-anon' ); |
| 629 | break; |
| 630 | case EditPage::AS_IMAGE_REDIRECT_LOGGED: |
| 631 | $status->fatal( 'apierror-noimageredirect' ); |
| 632 | break; |
| 633 | case EditPage::AS_READ_ONLY_PAGE_ANON: |
| 634 | $status->fatal( 'apierror-noedit-anon' ); |
| 635 | break; |
| 636 | case EditPage::AS_NO_CHANGE_CONTENT_MODEL: |
| 637 | $status->fatal( 'apierror-cantchangecontentmodel' ); |
| 638 | break; |
| 639 | case EditPage::AS_CONFLICT_DETECTED: |
| 640 | $status->fatal( 'edit-conflict' ); |
| 641 | break; |
| 642 | |
| 643 | // Currently shouldn't be needed, but here in case |
| 644 | // hooks use them without setting appropriate |
| 645 | // errors on the status. |
| 646 | // @codeCoverageIgnoreStart |
| 647 | case EditPage::AS_SPAM_ERROR: |
| 648 | // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset |
| 649 | $status->fatal( 'apierror-spamdetected', $result['spam'] ); |
| 650 | break; |
| 651 | case EditPage::AS_READ_ONLY_PAGE_LOGGED: |
| 652 | $status->fatal( 'apierror-noedit' ); |
| 653 | break; |
| 654 | case EditPage::AS_NO_CREATE_PERMISSION: |
| 655 | $status->fatal( 'nocreate-loggedin' ); |
| 656 | break; |
| 657 | case EditPage::AS_BLANK_ARTICLE: |
| 658 | $status->fatal( 'apierror-emptypage' ); |
| 659 | break; |
| 660 | case EditPage::AS_TEXTBOX_EMPTY: |
| 661 | $status->fatal( 'apierror-emptynewsection' ); |
| 662 | break; |
| 663 | case EditPage::AS_SUMMARY_NEEDED: |
| 664 | $status->fatal( 'apierror-summaryrequired' ); |
| 665 | break; |
| 666 | default: |
| 667 | wfWarn( __METHOD__ . ": Unknown EditPage code $statusValue with no message" ); |
| 668 | $status->fatal( 'apierror-unknownerror-editpage', $statusValue ); |
| 669 | break; |
| 670 | // @codeCoverageIgnoreEnd |
| 671 | } |
| 672 | } |
| 673 | $this->dieStatus( $status ); |
| 674 | } |
| 675 | $apiResult->addValue( null, $this->getModuleName(), $r ); |
| 676 | } |
| 677 | |
| 678 | /** @inheritDoc */ |
| 679 | public function mustBePosted() { |
| 680 | return true; |
| 681 | } |
| 682 | |
| 683 | /** @inheritDoc */ |
| 684 | public function isWriteMode() { |
| 685 | return true; |
| 686 | } |
| 687 | |
| 688 | /** @inheritDoc */ |
| 689 | public function getAllowedParams() { |
| 690 | $params = [ |
| 691 | 'title' => [ |
| 692 | ParamValidator::PARAM_TYPE => 'string', |
| 693 | ], |
| 694 | 'pageid' => [ |
| 695 | ParamValidator::PARAM_TYPE => 'integer', |
| 696 | ], |
| 697 | 'section' => null, |
| 698 | 'sectiontitle' => [ |
| 699 | ParamValidator::PARAM_TYPE => 'string', |
| 700 | ], |
| 701 | 'text' => [ |
| 702 | ParamValidator::PARAM_TYPE => 'text', |
| 703 | ], |
| 704 | 'summary' => null, |
| 705 | 'tags' => [ |
| 706 | ParamValidator::PARAM_TYPE => 'tags', |
| 707 | ParamValidator::PARAM_ISMULTI => true, |
| 708 | ], |
| 709 | 'minor' => false, |
| 710 | 'notminor' => false, |
| 711 | 'bot' => false, |
| 712 | 'baserevid' => [ |
| 713 | ParamValidator::PARAM_TYPE => 'integer', |
| 714 | ], |
| 715 | 'basetimestamp' => [ |
| 716 | ParamValidator::PARAM_TYPE => 'timestamp', |
| 717 | ], |
| 718 | 'starttimestamp' => [ |
| 719 | ParamValidator::PARAM_TYPE => 'timestamp', |
| 720 | ], |
| 721 | 'recreate' => false, |
| 722 | 'createonly' => false, |
| 723 | 'nocreate' => false, |
| 724 | 'watch' => [ |
| 725 | ParamValidator::PARAM_DEFAULT => false, |
| 726 | ParamValidator::PARAM_DEPRECATED => true, |
| 727 | ], |
| 728 | 'unwatch' => [ |
| 729 | ParamValidator::PARAM_DEFAULT => false, |
| 730 | ParamValidator::PARAM_DEPRECATED => true, |
| 731 | ], |
| 732 | ]; |
| 733 | |
| 734 | // Params appear in the docs in the order they are defined, |
| 735 | // which is why this is here and not at the bottom. |
| 736 | $params += $this->getWatchlistParams(); |
| 737 | |
| 738 | $params += [ |
| 739 | 'md5' => null, |
| 740 | 'prependtext' => [ |
| 741 | ParamValidator::PARAM_TYPE => 'text', |
| 742 | ], |
| 743 | 'appendtext' => [ |
| 744 | ParamValidator::PARAM_TYPE => 'text', |
| 745 | ], |
| 746 | 'undo' => [ |
| 747 | ParamValidator::PARAM_TYPE => 'integer', |
| 748 | IntegerDef::PARAM_MIN => 0, |
| 749 | ApiBase::PARAM_RANGE_ENFORCE => true, |
| 750 | ], |
| 751 | 'undoafter' => [ |
| 752 | ParamValidator::PARAM_TYPE => 'integer', |
| 753 | IntegerDef::PARAM_MIN => 0, |
| 754 | ApiBase::PARAM_RANGE_ENFORCE => true, |
| 755 | ], |
| 756 | 'redirect' => [ |
| 757 | ParamValidator::PARAM_TYPE => 'boolean', |
| 758 | ParamValidator::PARAM_DEFAULT => false, |
| 759 | ], |
| 760 | 'contentformat' => [ |
| 761 | ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(), |
| 762 | ], |
| 763 | 'contentmodel' => [ |
| 764 | ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getContentModels(), |
| 765 | ], |
| 766 | 'token' => [ |
| 767 | // Standard definition automatically inserted |
| 768 | ApiBase::PARAM_HELP_MSG_APPEND => [ 'apihelp-edit-param-token' ], |
| 769 | ], |
| 770 | ]; |
| 771 | |
| 772 | $params += $this->getCreateTempUserParams(); |
| 773 | |
| 774 | return $params; |
| 775 | } |
| 776 | |
| 777 | /** @inheritDoc */ |
| 778 | public function needsToken() { |
| 779 | return 'csrf'; |
| 780 | } |
| 781 | |
| 782 | /** @inheritDoc */ |
| 783 | protected function getExamplesMessages() { |
| 784 | return [ |
| 785 | 'action=edit&title=Test&summary=test%20summary&' . |
| 786 | 'text=article%20content&baserevid=1234567&token=123ABC' |
| 787 | => 'apihelp-edit-example-edit', |
| 788 | 'action=edit&title=Test&summary=NOTOC&minor=&' . |
| 789 | 'prependtext=__NOTOC__%0A&basetimestamp=2007-08-24T12:34:54Z&token=123ABC' |
| 790 | => 'apihelp-edit-example-prepend', |
| 791 | 'action=edit&title=Test&undo=13585&undoafter=13579&' . |
| 792 | 'basetimestamp=2007-08-24T12:34:54Z&token=123ABC' |
| 793 | => 'apihelp-edit-example-undo', |
| 794 | ]; |
| 795 | } |
| 796 | |
| 797 | /** @inheritDoc */ |
| 798 | public function getHelpUrls() { |
| 799 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Edit'; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | /** @deprecated class alias since 1.43 */ |
| 804 | class_alias( ApiEditPage::class, 'ApiEditPage' ); |