147 $titleObj = $pageObj->getTitle();
152 if (
$params[
'prependtext'] ===
null
153 &&
$params[
'appendtext'] ===
null
154 &&
$params[
'section'] !==
'new'
158 if ( $titleObj->isRedirect() ) {
159 $oldTarget = $titleObj;
160 $redirTarget = $this->redirectLookup->getRedirectTarget( $oldTarget );
161 $redirTarget = Title::castFromLinkTarget( $redirTarget );
164 'from' => $titleObj->getPrefixedText(),
165 'to' => $redirTarget->getPrefixedText()
169 if ( $redirTarget->isExternal() || !$redirTarget->canExist() ) {
170 $redirValues[
'to'] = $redirTarget->getFullText();
173 'apierror-edit-invalidredirect',
174 Message::plaintextParam( $oldTarget->getPrefixedText() ),
175 Message::plaintextParam( $redirTarget->getFullText() ),
177 'edit-invalidredirect',
178 [
'redirects' => $redirValues ]
182 ApiResult::setIndexedTagName( $redirValues,
'r' );
183 $apiResult->addValue(
null,
'redirects', $redirValues );
186 $pageObj = $this->wikiPageFactory->newFromTitle( $redirTarget );
187 $titleObj = $pageObj->getTitle();
193 if (
$params[
'contentmodel'] ) {
194 $contentHandler = $this->contentHandlerFactory->getContentHandler(
$params[
'contentmodel'] );
196 $contentHandler = $pageObj->getContentHandler();
198 $contentModel = $contentHandler->getModelID();
200 $name = $titleObj->getPrefixedDBkey();
204 } elseif ( $contentHandler->supportsDirectApiEditing() ===
false ) {
205 $this->
dieWithError( [
'apierror-no-direct-editing', $contentModel, $name ] );
208 $contentFormat =
$params[
'contentformat'] ?: $contentHandler->getDefaultFormat();
210 if ( !$contentHandler->isSupportedFormat( $contentFormat ) ) {
211 $this->
dieWithError( [
'apierror-badformat', $contentFormat, $contentModel, $name ] );
214 if (
$params[
'createonly'] && $titleObj->exists() ) {
217 if (
$params[
'nocreate'] && !$titleObj->exists() ) {
225 [
'autoblock' =>
true,
'user' => $this->getUserForPermissions() ]
229 if (
$params[
'appendtext'] !==
null ||
$params[
'prependtext'] !==
null ) {
230 $content = $pageObj->getContent();
234 # If this is a MediaWiki:x message, then load the messages
235 # and return the message value for x.
236 $text = $titleObj->getDefaultMessageText();
237 if ( $text ===
false ) {
242 $content = ContentHandler::makeContent( $text, $titleObj );
245 'wrap' => ApiMessage::create(
'apierror-contentserializationexception',
'parseerror' )
249 # Otherwise, make a new empty content.
250 $content = $contentHandler->makeEmptyContent();
257 $this->
dieWithError( [
'apierror-appendnotsupported', $contentModel ] );
260 if (
$params[
'section'] !==
null ) {
261 if ( !$contentHandler->supportsSections() ) {
262 $this->
dieWithError( [
'apierror-sectionsnotsupported', $contentModel ] );
265 if (
$params[
'section'] ==
'new' ) {
271 $content = $content->getSection( $section );
282 $text = $content->serialize( $contentFormat );
290 $undoRev = $this->revisionLookup->getRevisionById(
$params[
'undo'] );
291 if ( $undoRev ===
null || $undoRev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
295 if (
$params[
'undoafter'] > 0 ) {
296 $undoafterRev = $this->revisionLookup->getRevisionById(
$params[
'undoafter'] );
299 $undoafterRev = $this->revisionLookup->getPreviousRevision( $undoRev );
301 if ( $undoafterRev ===
null || $undoafterRev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
305 if ( $undoRev->getPageId() != $pageObj->getId() ) {
306 $this->
dieWithError( [
'apierror-revwrongpage', $undoRev->getId(),
307 $titleObj->getPrefixedText() ] );
309 if ( $undoafterRev->getPageId() != $pageObj->getId() ) {
310 $this->
dieWithError( [
'apierror-revwrongpage', $undoafterRev->getId(),
311 $titleObj->getPrefixedText() ] );
314 $newContent = $contentHandler->getUndoContent(
316 $pageObj->getRevisionRecord()->getContent( SlotRecord::MAIN ),
318 $undoRev->getContent( SlotRecord::MAIN ),
320 $undoafterRev->getContent( SlotRecord::MAIN ),
321 $pageObj->getRevisionRecord()->getId() === $undoRev->getId()
324 if ( !$newContent ) {
333 if ( !$newContent->isSupportedFormat( $contentFormat ) ) {
334 $undoafterRevMainSlot = $undoafterRev->getSlot(
338 $contentFormat = $undoafterRevMainSlot->getFormat();
339 if ( !$contentFormat ) {
342 $contentFormat = $this->contentHandlerFactory
343 ->getContentHandler( $undoafterRevMainSlot->getModel() )
344 ->getDefaultFormat();
348 $contentModel = $newContent->getModel();
349 $undoContentModel =
true;
351 $params[
'text'] = $newContent->serialize( $contentFormat );
355 if (
$params[
'summary'] ===
null ) {
356 $nextRev = $this->revisionLookup->getNextRevision( $undoafterRev );
357 if ( $nextRev && $nextRev->getId() ==
$params[
'undo'] ) {
358 $undoRevUser = $undoRev->getUser();
359 $params[
'summary'] = $this->
msg(
'undo-summary' )
360 ->params(
$params[
'undo'], $undoRevUser ? $undoRevUser->getName() :
'' )
361 ->inContentLanguage()->text();
367 if (
$params[
'md5'] !==
null && md5( $toMD5 ) !==
$params[
'md5'] ) {
375 'wpTextbox1' =>
$params[
'text'],
376 'format' => $contentFormat,
377 'model' => $contentModel,
378 'wpEditToken' =>
$params[
'token'],
379 'wpIgnoreBlankSummary' =>
true,
380 'wpIgnoreBlankArticle' =>
true,
381 'wpIgnoreSelfRedirect' =>
true,
383 'wpUnicodeCheck' => EditPage::UNICODE_CHECK,
387 if (
$params[
'summary'] !==
null ) {
388 $requestArray[
'wpSummary'] =
$params[
'summary'];
391 if (
$params[
'sectiontitle'] !==
null ) {
392 $requestArray[
'wpSectionTitle'] =
$params[
'sectiontitle'];
396 $requestArray[
'wpUndidRevision'] =
$params[
'undo'];
398 if (
$params[
'undoafter'] > 0 ) {
399 $requestArray[
'wpUndoAfter'] =
$params[
'undoafter'];
403 if ( !empty(
$params[
'baserevid'] ) ) {
404 $requestArray[
'editRevId'] =
$params[
'baserevid'];
409 if (
$params[
'basetimestamp'] !==
null && (
bool)$this->
getMain()->getVal(
'basetimestamp' ) ) {
410 $requestArray[
'wpEdittime'] =
$params[
'basetimestamp'];
411 } elseif ( empty(
$params[
'baserevid'] ) ) {
414 $requestArray[
'wpEdittime'] = $pageObj->getTimestamp();
417 if (
$params[
'starttimestamp'] !==
null ) {
418 $requestArray[
'wpStarttime'] =
$params[
'starttimestamp'];
424 $this->userOptionsLookup->getOption( $user,
'minordefault' ) )
426 $requestArray[
'wpMinoredit'] =
'';
430 $requestArray[
'wpRecreate'] =
'';
433 if (
$params[
'section'] !==
null ) {
435 if ( !preg_match(
'/^((T-)?\d+|new)$/', $section ) ) {
438 $content = $pageObj->getContent();
439 if ( $section !==
'0'
441 && ( !$content || !$content->getSection( $section ) )
443 $this->
dieWithError( [
'apierror-nosuchsection', $section ] );
445 $requestArray[
'wpSection'] =
$params[
'section'];
447 $requestArray[
'wpSection'] =
'';
455 } elseif (
$params[
'unwatch'] ) {
460 $requestArray[
'wpWatchthis'] =
true;
463 if ( $watchlistExpiry ) {
464 $requestArray[
'wpWatchlistExpiry'] = $watchlistExpiry;
471 if ( $tagStatus->isOK() ) {
472 $requestArray[
'wpChangeTags'] = implode(
',',
$params[
'tags'] );
480 $requestArray += $this->
getRequest()->getValues();
492 $articleContext->setWikiPage( $pageObj );
493 $articleContext->setUser( $this->
getUser() );
496 $articleObject = Article::newFromWikiPage( $pageObj, $articleContext );
498 $ep =
new EditPage( $articleObject );
500 $ep->setApiEditOverride(
true );
501 $ep->setContextTitle( $titleObj );
502 $ep->importFormData( $req );
503 $tempUserCreateStatus = $ep->maybeActivateTempUserCreate(
true );
504 if ( !$tempUserCreateStatus->isOK() ) {
505 $this->
dieWithError(
'apierror-tempuseracquirefailed',
'tempuseracquirefailed' );
510 $editRevId = $requestArray[
'editRevId'] ??
false;
511 $baseRev = $this->revisionLookup->getRevisionByTitle( $titleObj, $editRevId );
512 $baseContentModel =
null;
515 $baseContent = $baseRev->getContent( SlotRecord::MAIN );
516 $baseContentModel = $baseContent ? $baseContent->getModel() :
null;
519 $baseContentModel ??= $pageObj->getContentModel();
523 $contentModelsCanDiffer =
$params[
'contentmodel'] || isset( $undoContentModel );
525 if ( !$contentModelsCanDiffer && $contentModel !== $baseContentModel ) {
526 $this->
dieWithError( [
'apierror-contentmodel-mismatch', $contentModel, $baseContentModel ] );
530 $oldRevId = $articleObject->getRevIdFetched();
538 $status = $ep->attemptSave( $result );
539 $statusValue = is_int( $status->value ) ? $status->value : 0;
543 switch ( $statusValue ) {
544 case EditPage::AS_HOOK_ERROR:
545 case EditPage::AS_HOOK_ERROR_EXPECTED:
546 if ( $status->statusData !==
null ) {
547 $r = $status->statusData;
548 $r[
'result'] =
'Failure';
552 if ( !$status->getErrors() ) {
555 $status->fatal(
'hookaborted' );
564 case EditPage::AS_BLOCKED_PAGE_FOR_USER:
569 case EditPage::AS_READ_ONLY_PAGE:
573 case EditPage::AS_SUCCESS_NEW_ARTICLE:
577 case EditPage::AS_SUCCESS_UPDATE:
578 $r[
'result'] =
'Success';
579 $r[
'pageid'] = (int)$titleObj->getArticleID();
580 $r[
'title'] = $titleObj->getPrefixedText();
581 $r[
'contentmodel'] = $articleObject->getPage()->getContentModel();
582 $newRevId = $articleObject->getPage()->getLatest();
583 if ( $newRevId == $oldRevId ) {
584 $r[
'nochange'] =
true;
586 $r[
'oldrevid'] = (int)$oldRevId;
587 $r[
'newrevid'] = (int)$newRevId;
589 $pageObj->getTimestamp() );
593 $r[
'watched'] =
true;
596 $this->watchedItemStore,
601 if ( $watchlistExpiry ) {
602 $r[
'watchlistexpiry'] = $watchlistExpiry;
605 $this->persistGlobalSession();
607 if ( isset( $result[
'savedTempUser'] ) ) {
608 $r[
'tempusercreated'] =
true;
609 $params[
'returnto'] ??= $titleObj->getPrefixedDBkey();
610 $redirectUrl = $this->getTempUserRedirectUrl(
612 $result[
'savedTempUser']
614 if ( $redirectUrl ) {
615 $r[
'tempusercreatedredirect'] = $redirectUrl;
622 if ( !$status->getErrors() ) {
625 switch ( $statusValue ) {
627 case EditPage::AS_IMAGE_REDIRECT_ANON:
628 $status->fatal(
'apierror-noimageredirect-anon' );
630 case EditPage::AS_IMAGE_REDIRECT_LOGGED:
631 $status->fatal(
'apierror-noimageredirect' );
633 case EditPage::AS_CONTENT_TOO_BIG:
634 case EditPage::AS_MAX_ARTICLE_SIZE_EXCEEDED:
635 $status->fatal(
'apierror-contenttoobig',
636 $this->
getConfig()->
get( MainConfigNames::MaxArticleSize ) );
638 case EditPage::AS_READ_ONLY_PAGE_ANON:
639 $status->fatal(
'apierror-noedit-anon' );
641 case EditPage::AS_NO_CHANGE_CONTENT_MODEL:
642 $status->fatal(
'apierror-cantchangecontentmodel' );
644 case EditPage::AS_ARTICLE_WAS_DELETED:
645 $status->fatal(
'apierror-pagedeleted' );
647 case EditPage::AS_CONFLICT_DETECTED:
648 $status->fatal(
'edit-conflict' );
655 case EditPage::AS_SPAM_ERROR:
657 $status->fatal(
'apierror-spamdetected', $result[
'spam'] );
659 case EditPage::AS_READ_ONLY_PAGE_LOGGED:
660 $status->fatal(
'apierror-noedit' );
662 case EditPage::AS_RATE_LIMITED:
663 $status->fatal(
'apierror-ratelimited' );
665 case EditPage::AS_NO_CREATE_PERMISSION:
666 $status->fatal(
'nocreate-loggedin' );
668 case EditPage::AS_BLANK_ARTICLE:
669 $status->fatal(
'apierror-emptypage' );
671 case EditPage::AS_TEXTBOX_EMPTY:
672 $status->fatal(
'apierror-emptynewsection' );
674 case EditPage::AS_SUMMARY_NEEDED:
675 $status->fatal(
'apierror-summaryrequired' );
678 wfWarn( __METHOD__ .
": Unknown EditPage code $statusValue with no message" );
679 $status->fatal(
'apierror-unknownerror-editpage', $statusValue );
700 ParamValidator::PARAM_TYPE =>
'string',
703 ParamValidator::PARAM_TYPE =>
'integer',
707 ParamValidator::PARAM_TYPE =>
'string',
710 ParamValidator::PARAM_TYPE =>
'text',
714 ParamValidator::PARAM_TYPE =>
'tags',
715 ParamValidator::PARAM_ISMULTI =>
true,
721 ParamValidator::PARAM_TYPE =>
'integer',
724 ParamValidator::PARAM_TYPE =>
'timestamp',
726 'starttimestamp' => [
727 ParamValidator::PARAM_TYPE =>
'timestamp',
730 'createonly' =>
false,
733 ParamValidator::PARAM_DEFAULT =>
false,
734 ParamValidator::PARAM_DEPRECATED =>
true,
737 ParamValidator::PARAM_DEFAULT =>
false,
738 ParamValidator::PARAM_DEPRECATED =>
true,
749 ParamValidator::PARAM_TYPE =>
'text',
752 ParamValidator::PARAM_TYPE =>
'text',
755 ParamValidator::PARAM_TYPE =>
'integer',
756 IntegerDef::PARAM_MIN => 0,
760 ParamValidator::PARAM_TYPE =>
'integer',
761 IntegerDef::PARAM_MIN => 0,
765 ParamValidator::PARAM_TYPE =>
'boolean',
766 ParamValidator::PARAM_DEFAULT =>
false,
769 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
772 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getContentModels(),
780 $params += $this->getCreateTempUserParams();