129 $titleObj = $pageObj->getTitle();
133 if ( $params[
'redirect'] ) {
134 if ( $params[
'prependtext'] ===
null
135 && $params[
'appendtext'] ===
null
136 && $params[
'section'] !==
'new'
140 if ( $titleObj->isRedirect() ) {
141 $oldTarget = $titleObj;
142 $redirTarget = $this->redirectLookup->getRedirectTarget( $oldTarget );
143 $redirTarget = Title::castFromLinkTarget( $redirTarget );
146 'from' => $titleObj->getPrefixedText(),
147 'to' => $redirTarget->getPrefixedText()
151 if ( $redirTarget->isExternal() || !$redirTarget->canExist() ) {
152 $redirValues[
'to'] = $redirTarget->getFullText();
155 'apierror-edit-invalidredirect',
159 'edit-invalidredirect',
160 [
'redirects' => $redirValues ]
165 $apiResult->addValue(
null,
'redirects', $redirValues );
168 $pageObj = $this->wikiPageFactory->newFromTitle( $redirTarget );
169 $titleObj = $pageObj->getTitle();
175 if ( $params[
'contentmodel'] ) {
176 $contentHandler = $this->contentHandlerFactory->getContentHandler( $params[
'contentmodel'] );
178 $contentHandler = $pageObj->getContentHandler();
180 $contentModel = $contentHandler->getModelID();
182 $name = $titleObj->getPrefixedDBkey();
184 if ( $params[
'undo'] > 0 ) {
186 } elseif ( $contentHandler->supportsDirectApiEditing() ===
false ) {
187 $this->
dieWithError( [
'apierror-no-direct-editing', $contentModel, $name ] );
190 $contentFormat = $params[
'contentformat'] ?: $contentHandler->getDefaultFormat();
192 if ( !$contentHandler->isSupportedFormat( $contentFormat ) ) {
193 $this->
dieWithError( [
'apierror-badformat', $contentFormat, $contentModel, $name ] );
196 if ( $params[
'createonly'] && $titleObj->exists() ) {
199 if ( $params[
'nocreate'] && !$titleObj->exists() ) {
207 [
'autoblock' =>
true,
'user' => $this->getUserForPermissions() ]
210 $toMD5 = $params[
'text'];
211 if ( $params[
'appendtext'] !==
null || $params[
'prependtext'] !==
null ) {
212 $content = $pageObj->getContent();
216 # If this is a MediaWiki:x message, then load the messages
217 # and return the message value for x.
218 $text = $titleObj->getDefaultMessageText();
219 if ( $text ===
false ) {
224 $content = ContentHandler::makeContent( $text, $titleObj );
227 'wrap' =>
ApiMessage::create(
'apierror-contentserializationexception',
'parseerror' )
231 # Otherwise, make a new empty content.
232 $content = $contentHandler->makeEmptyContent();
239 $this->
dieWithError( [
'apierror-appendnotsupported', $contentModel ] );
242 if ( $params[
'section'] !==
null ) {
243 if ( !$contentHandler->supportsSections() ) {
244 $this->
dieWithError( [
'apierror-sectionsnotsupported', $contentModel ] );
247 if ( $params[
'section'] ==
'new' ) {
252 $section = $params[
'section'];
253 $content = $content->getSection( $section );
264 $text = $content->serialize( $contentFormat );
267 $params[
'text'] = $params[
'prependtext'] . $text . $params[
'appendtext'];
268 $toMD5 = $params[
'prependtext'] . $params[
'appendtext'];
271 if ( $params[
'undo'] > 0 ) {
272 $undoRev = $this->revisionLookup->getRevisionById( $params[
'undo'] );
273 if ( $undoRev ===
null || $undoRev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
274 $this->
dieWithError( [
'apierror-nosuchrevid', $params[
'undo'] ] );
277 if ( $params[
'undoafter'] > 0 ) {
278 $undoafterRev = $this->revisionLookup->getRevisionById( $params[
'undoafter'] );
281 $undoafterRev = $this->revisionLookup->getPreviousRevision( $undoRev );
283 if ( $undoafterRev ===
null || $undoafterRev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
284 $this->
dieWithError( [
'apierror-nosuchrevid', $params[
'undoafter'] ] );
287 if ( $undoRev->getPageId() != $pageObj->getId() ) {
288 $this->
dieWithError( [
'apierror-revwrongpage', $undoRev->getId(),
289 $titleObj->getPrefixedText() ] );
291 if ( $undoafterRev->getPageId() != $pageObj->getId() ) {
292 $this->
dieWithError( [
'apierror-revwrongpage', $undoafterRev->getId(),
293 $titleObj->getPrefixedText() ] );
296 $newContent = $contentHandler->getUndoContent(
298 $pageObj->getRevisionRecord()->getContent( SlotRecord::MAIN ),
300 $undoRev->getContent( SlotRecord::MAIN ),
302 $undoafterRev->getContent( SlotRecord::MAIN ),
303 $pageObj->getRevisionRecord()->getId() === $undoRev->getId()
306 if ( !$newContent ) {
309 if ( !$params[
'contentmodel'] && !$params[
'contentformat'] ) {
315 if ( !$newContent->isSupportedFormat( $contentFormat ) ) {
316 $undoafterRevMainSlot = $undoafterRev->getSlot(
320 $contentFormat = $undoafterRevMainSlot->getFormat();
321 if ( !$contentFormat ) {
324 $contentFormat = $this->contentHandlerFactory
325 ->getContentHandler( $undoafterRevMainSlot->getModel() )
326 ->getDefaultFormat();
330 $contentModel = $newContent->getModel();
331 $undoContentModel =
true;
333 $params[
'text'] = $newContent->serialize( $contentFormat );
337 if ( $params[
'summary'] ===
null ) {
338 $nextRev = $this->revisionLookup->getNextRevision( $undoafterRev );
339 if ( $nextRev && $nextRev->getId() == $params[
'undo'] ) {
340 $undoRevUser = $undoRev->getUser();
341 $params[
'summary'] = $this->
msg(
'undo-summary' )
342 ->params( $params[
'undo'], $undoRevUser ? $undoRevUser->getName() :
'' )
343 ->inContentLanguage()->text();
349 if ( $params[
'md5'] !==
null && md5( $toMD5 ) !== $params[
'md5'] ) {
356 'wpTextbox1' => $params[
'text'],
357 'format' => $contentFormat,
358 'model' => $contentModel,
359 'wpEditToken' => $params[
'token'],
360 'wpIgnoreBlankSummary' =>
true,
361 'wpIgnoreBlankArticle' =>
true,
362 'wpIgnoreProblematicRedirects' =>
true,
363 'bot' => $params[
'bot'],
364 'wpUnicodeCheck' => EditPage::UNICODE_CHECK,
367 if ( $params[
'summary'] !==
null ) {
368 $requestArray[
'wpSummary'] = $params[
'summary'];
371 if ( $params[
'sectiontitle'] !==
null ) {
372 $requestArray[
'wpSectionTitle'] = $params[
'sectiontitle'];
375 if ( $params[
'undo'] > 0 ) {
376 $requestArray[
'wpUndidRevision'] = $params[
'undo'];
378 if ( $params[
'undoafter'] > 0 ) {
379 $requestArray[
'wpUndoAfter'] = $params[
'undoafter'];
383 if ( !empty( $params[
'baserevid'] ) ) {
384 $requestArray[
'editRevId'] = $params[
'baserevid'];
389 if ( $params[
'basetimestamp'] !==
null && (
bool)$this->
getMain()->getVal(
'basetimestamp' ) ) {
390 $requestArray[
'wpEdittime'] = $params[
'basetimestamp'];
391 } elseif ( empty( $params[
'baserevid'] ) ) {
394 $requestArray[
'wpEdittime'] = $pageObj->getTimestamp();
397 if ( $params[
'starttimestamp'] !==
null ) {
398 $requestArray[
'wpStarttime'] = $params[
'starttimestamp'];
403 if ( $params[
'minor'] || ( !$params[
'notminor'] &&
404 $this->userOptionsLookup->getOption( $user,
'minordefault' ) )
406 $requestArray[
'wpMinoredit'] =
'';
409 if ( $params[
'recreate'] ) {
410 $requestArray[
'wpRecreate'] =
'';
413 if ( $params[
'section'] !==
null ) {
414 $section = $params[
'section'];
415 if ( !preg_match(
'/^((T-)?\d+|new)$/', $section ) ) {
418 $content = $pageObj->getContent();
419 if ( $section !==
'0'
421 && ( !$content || !$content->getSection( $section ) )
423 $this->
dieWithError( [
'apierror-nosuchsection', $section ] );
425 $requestArray[
'wpSection'] = $params[
'section'];
427 $requestArray[
'wpSection'] =
'';
433 if ( $params[
'watch'] ) {
435 } elseif ( $params[
'unwatch'] ) {
440 $requestArray[
'wpWatchthis'] =
true;
441 $prefName =
'watchdefault-expiry';
442 if ( !$pageObj->exists() ) {
443 $prefName =
'watchcreations-expiry';
447 if ( $watchlistExpiry ) {
448 $requestArray[
'wpWatchlistExpiry'] = $watchlistExpiry;
453 if ( $params[
'tags'] ) {
454 $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params[
'tags'], $this->
getAuthority() );
455 if ( $tagStatus->isOK() ) {
456 $requestArray[
'wpChangeTags'] = implode(
',', $params[
'tags'] );
464 $requestArray += $this->
getRequest()->getValues();
477 $articleContext->setWikiPage( $pageObj );
478 $articleContext->setUser( $this->
getUser() );
481 $articleObject = Article::newFromWikiPage( $pageObj, $articleContext );
483 $ep =
new EditPage( $articleObject );
485 $ep->setApiEditOverride(
true );
486 $ep->setContextTitle( $titleObj );
487 $ep->importFormData( $req );
488 $tempUserCreateStatus = $ep->maybeActivateTempUserCreate(
true );
489 if ( !$tempUserCreateStatus->isOK() ) {
490 $this->
dieWithError(
'apierror-tempuseracquirefailed',
'tempuseracquirefailed' );
495 $editRevId = $requestArray[
'editRevId'] ??
false;
496 $baseRev = $this->revisionLookup->getRevisionByTitle( $titleObj, $editRevId );
497 $baseContentModel =
null;
500 $baseContent = $baseRev->getContent( SlotRecord::MAIN );
501 $baseContentModel = $baseContent ? $baseContent->getModel() :
null;
504 $baseContentModel ??= $pageObj->getContentModel();
508 $contentModelsCanDiffer = $params[
'contentmodel'] || isset( $undoContentModel );
510 if ( !$contentModelsCanDiffer && $contentModel !== $baseContentModel ) {
511 $this->
dieWithError( [
'apierror-contentmodel-mismatch', $contentModel, $baseContentModel ] );
515 $oldRevId = $articleObject->getRevIdFetched();
524 $status = $ep->attemptSave( $result );
525 $statusValue = is_int( $status->value ) ? $status->value : 0;
530 switch ( $statusValue ) {
531 case EditPage::AS_HOOK_ERROR:
532 case EditPage::AS_HOOK_ERROR_EXPECTED:
533 if ( $status->statusData !==
null ) {
534 $r = $status->statusData;
535 $r[
'result'] =
'Failure';
539 if ( !$status->getMessages() ) {
542 $status->fatal(
'hookaborted' );
551 case EditPage::AS_BLOCKED_PAGE_FOR_USER:
556 case EditPage::AS_READ_ONLY_PAGE:
560 case EditPage::AS_SUCCESS_NEW_ARTICLE:
564 case EditPage::AS_SUCCESS_UPDATE:
565 $r[
'result'] =
'Success';
566 $r[
'pageid'] = (int)$titleObj->getArticleID();
567 $r[
'title'] = $titleObj->getPrefixedText();
568 $r[
'contentmodel'] = $articleObject->getPage()->getContentModel();
569 $newRevId = $articleObject->getPage()->getLatest();
570 if ( $newRevId == $oldRevId ) {
571 $r[
'nochange'] =
true;
573 $r[
'oldrevid'] = (int)$oldRevId;
574 $r[
'newrevid'] = (int)$newRevId;
576 $pageObj->getTimestamp() );
580 $r[
'watched'] =
true;
583 $this->watchedItemStore,
588 if ( $watchlistExpiry ) {
589 $r[
'watchlistexpiry'] = $watchlistExpiry;
592 $this->persistGlobalSession();
602 $shouldRedirectForTempUser = isset( $result[
'savedTempUser'] ) ||
603 ( $user->isTemp() && ( $user->getEditCount() === 0 ) );
604 if ( $shouldRedirectForTempUser ) {
605 $r[
'tempusercreated'] =
true;
606 $params[
'returnto'] ??= $titleObj->getPrefixedDBkey();
607 $redirectUrl = $this->getTempUserRedirectUrl(
609 $result[
'savedTempUser'] ?? $user
611 if ( $redirectUrl ) {
612 $r[
'tempusercreatedredirect'] = $redirectUrl;
619 if ( !$status->getMessages() ) {
622 switch ( $statusValue ) {
624 case EditPage::AS_IMAGE_REDIRECT_ANON:
625 $status->fatal(
'apierror-noimageredirect-anon' );
627 case EditPage::AS_IMAGE_REDIRECT_LOGGED:
628 $status->fatal(
'apierror-noimageredirect' );
630 case EditPage::AS_READ_ONLY_PAGE_ANON:
631 $status->fatal(
'apierror-noedit-anon' );
633 case EditPage::AS_NO_CHANGE_CONTENT_MODEL:
634 $status->fatal(
'apierror-cantchangecontentmodel' );
636 case EditPage::AS_CONFLICT_DETECTED:
637 $status->fatal(
'edit-conflict' );
644 case EditPage::AS_SPAM_ERROR:
645 $status->fatal(
'apierror-spamdetected', $result[
'spam'] );
647 case EditPage::AS_READ_ONLY_PAGE_LOGGED:
648 $status->fatal(
'apierror-noedit' );
650 case EditPage::AS_NO_CREATE_PERMISSION:
651 $status->fatal(
'nocreate-loggedin' );
653 case EditPage::AS_BLANK_ARTICLE:
654 $status->fatal(
'apierror-emptypage' );
656 case EditPage::AS_TEXTBOX_EMPTY:
657 $status->fatal(
'apierror-emptynewsection' );
659 case EditPage::AS_SUMMARY_NEEDED:
660 $status->fatal(
'apierror-summaryrequired' );
663 wfWarn( __METHOD__ .
": Unknown EditPage code $statusValue with no message" );
664 $status->fatal(
'apierror-unknownerror-editpage', $statusValue );
688 ParamValidator::PARAM_TYPE =>
'string',
691 ParamValidator::PARAM_TYPE =>
'integer',
695 ParamValidator::PARAM_TYPE =>
'string',
698 ParamValidator::PARAM_TYPE =>
'text',
702 ParamValidator::PARAM_TYPE =>
'tags',
703 ParamValidator::PARAM_ISMULTI =>
true,
709 ParamValidator::PARAM_TYPE =>
'integer',
712 ParamValidator::PARAM_TYPE =>
'timestamp',
714 'starttimestamp' => [
715 ParamValidator::PARAM_TYPE =>
'timestamp',
718 'createonly' =>
false,
721 ParamValidator::PARAM_DEFAULT =>
false,
722 ParamValidator::PARAM_DEPRECATED =>
true,
725 ParamValidator::PARAM_DEFAULT =>
false,
726 ParamValidator::PARAM_DEPRECATED =>
true,
737 ParamValidator::PARAM_TYPE =>
'text',
740 ParamValidator::PARAM_TYPE =>
'text',
743 ParamValidator::PARAM_TYPE =>
'integer',
744 IntegerDef::PARAM_MIN => 0,
748 ParamValidator::PARAM_TYPE =>
'integer',
749 IntegerDef::PARAM_MIN => 0,
753 ParamValidator::PARAM_TYPE =>
'boolean',
754 ParamValidator::PARAM_DEFAULT =>
false,
757 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
760 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getContentModels(),
768 $params += $this->getCreateTempUserParams();