143 $titleObj = $pageObj->getTitle();
148 if (
$params[
'prependtext'] ===
null
149 &&
$params[
'appendtext'] ===
null
150 &&
$params[
'section'] !==
'new'
154 if ( $titleObj->isRedirect() ) {
155 $oldTarget = $titleObj;
156 $redirTarget = $this->redirectLookup->getRedirectTarget( $oldTarget );
157 $redirTarget = Title::castFromLinkTarget( $redirTarget );
160 'from' => $titleObj->getPrefixedText(),
161 'to' => $redirTarget->getPrefixedText()
165 if ( $redirTarget->isExternal() || !$redirTarget->canExist() ) {
166 $redirValues[
'to'] = $redirTarget->getFullText();
169 'apierror-edit-invalidredirect',
173 'edit-invalidredirect',
174 [
'redirects' => $redirValues ]
179 $apiResult->addValue(
null,
'redirects', $redirValues );
182 $pageObj = $this->wikiPageFactory->newFromTitle( $redirTarget );
183 $titleObj = $pageObj->getTitle();
189 if (
$params[
'contentmodel'] ) {
190 $contentHandler = $this->contentHandlerFactory->getContentHandler(
$params[
'contentmodel'] );
192 $contentHandler = $pageObj->getContentHandler();
194 $contentModel = $contentHandler->getModelID();
196 $name = $titleObj->getPrefixedDBkey();
200 } elseif ( $contentHandler->supportsDirectApiEditing() ===
false ) {
201 $this->
dieWithError( [
'apierror-no-direct-editing', $contentModel, $name ] );
204 $contentFormat =
$params[
'contentformat'] ?: $contentHandler->getDefaultFormat();
206 if ( !$contentHandler->isSupportedFormat( $contentFormat ) ) {
207 $this->
dieWithError( [
'apierror-badformat', $contentFormat, $contentModel, $name ] );
210 if (
$params[
'createonly'] && $titleObj->exists() ) {
213 if (
$params[
'nocreate'] && !$titleObj->exists() ) {
221 [
'autoblock' =>
true,
'user' => $this->getUserForPermissions() ]
225 if (
$params[
'appendtext'] !==
null ||
$params[
'prependtext'] !==
null ) {
226 $content = $pageObj->getContent();
230 # If this is a MediaWiki:x message, then load the messages
231 # and return the message value for x.
232 $text = $titleObj->getDefaultMessageText();
233 if ( $text ===
false ) {
238 $content = ContentHandler::makeContent( $text, $titleObj );
241 'wrap' =>
ApiMessage::create(
'apierror-contentserializationexception',
'parseerror' )
245 # Otherwise, make a new empty content.
246 $content = $contentHandler->makeEmptyContent();
253 $this->
dieWithError( [
'apierror-appendnotsupported', $contentModel ] );
256 if (
$params[
'section'] !==
null ) {
257 if ( !$contentHandler->supportsSections() ) {
258 $this->
dieWithError( [
'apierror-sectionsnotsupported', $contentModel ] );
261 if (
$params[
'section'] ==
'new' ) {
267 $content = $content->getSection( $section );
278 $text = $content->serialize( $contentFormat );
286 $undoRev = $this->revisionLookup->getRevisionById(
$params[
'undo'] );
287 if ( $undoRev ===
null || $undoRev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
291 if (
$params[
'undoafter'] > 0 ) {
292 $undoafterRev = $this->revisionLookup->getRevisionById(
$params[
'undoafter'] );
295 $undoafterRev = $this->revisionLookup->getPreviousRevision( $undoRev );
297 if ( $undoafterRev ===
null || $undoafterRev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
301 if ( $undoRev->getPageId() != $pageObj->getId() ) {
302 $this->
dieWithError( [
'apierror-revwrongpage', $undoRev->getId(),
303 $titleObj->getPrefixedText() ] );
305 if ( $undoafterRev->getPageId() != $pageObj->getId() ) {
306 $this->
dieWithError( [
'apierror-revwrongpage', $undoafterRev->getId(),
307 $titleObj->getPrefixedText() ] );
310 $newContent = $contentHandler->getUndoContent(
312 $pageObj->getRevisionRecord()->getContent( SlotRecord::MAIN ),
314 $undoRev->getContent( SlotRecord::MAIN ),
316 $undoafterRev->getContent( SlotRecord::MAIN ),
317 $pageObj->getRevisionRecord()->getId() === $undoRev->getId()
320 if ( !$newContent ) {
329 if ( !$newContent->isSupportedFormat( $contentFormat ) ) {
330 $undoafterRevMainSlot = $undoafterRev->getSlot(
334 $contentFormat = $undoafterRevMainSlot->getFormat();
335 if ( !$contentFormat ) {
338 $contentFormat = $this->contentHandlerFactory
339 ->getContentHandler( $undoafterRevMainSlot->getModel() )
340 ->getDefaultFormat();
344 $contentModel = $newContent->getModel();
345 $undoContentModel =
true;
347 $params[
'text'] = $newContent->serialize( $contentFormat );
351 if (
$params[
'summary'] ===
null ) {
352 $nextRev = $this->revisionLookup->getNextRevision( $undoafterRev );
353 if ( $nextRev && $nextRev->getId() ==
$params[
'undo'] ) {
354 $undoRevUser = $undoRev->getUser();
355 $params[
'summary'] = $this->
msg(
'undo-summary' )
356 ->params(
$params[
'undo'], $undoRevUser ? $undoRevUser->getName() :
'' )
357 ->inContentLanguage()->text();
363 if (
$params[
'md5'] !==
null && md5( $toMD5 ) !==
$params[
'md5'] ) {
371 'wpTextbox1' =>
$params[
'text'],
372 'format' => $contentFormat,
373 'model' => $contentModel,
374 'wpEditToken' =>
$params[
'token'],
375 'wpIgnoreBlankSummary' =>
true,
376 'wpIgnoreBlankArticle' =>
true,
377 'wpIgnoreSelfRedirect' =>
true,
379 'wpUnicodeCheck' => EditPage::UNICODE_CHECK,
383 if (
$params[
'summary'] !==
null ) {
384 $requestArray[
'wpSummary'] =
$params[
'summary'];
387 if (
$params[
'sectiontitle'] !==
null ) {
388 $requestArray[
'wpSectionTitle'] =
$params[
'sectiontitle'];
392 $requestArray[
'wpUndidRevision'] =
$params[
'undo'];
394 if (
$params[
'undoafter'] > 0 ) {
395 $requestArray[
'wpUndoAfter'] =
$params[
'undoafter'];
399 if ( !empty(
$params[
'baserevid'] ) ) {
400 $requestArray[
'editRevId'] =
$params[
'baserevid'];
405 if (
$params[
'basetimestamp'] !==
null && (
bool)$this->
getMain()->getVal(
'basetimestamp' ) ) {
406 $requestArray[
'wpEdittime'] =
$params[
'basetimestamp'];
407 } elseif ( empty(
$params[
'baserevid'] ) ) {
410 $requestArray[
'wpEdittime'] = $pageObj->getTimestamp();
413 if (
$params[
'starttimestamp'] !==
null ) {
414 $requestArray[
'wpStarttime'] =
$params[
'starttimestamp'];
420 $this->userOptionsLookup->getOption( $user,
'minordefault' ) )
422 $requestArray[
'wpMinoredit'] =
'';
426 $requestArray[
'wpRecreate'] =
'';
429 if (
$params[
'section'] !==
null ) {
431 if ( !preg_match(
'/^((T-)?\d+|new)$/', $section ) ) {
434 $content = $pageObj->getContent();
435 if ( $section !==
'0'
437 && ( !$content || !$content->getSection( $section ) )
439 $this->
dieWithError( [
'apierror-nosuchsection', $section ] );
441 $requestArray[
'wpSection'] =
$params[
'section'];
443 $requestArray[
'wpSection'] =
'';
451 } elseif (
$params[
'unwatch'] ) {
456 $requestArray[
'wpWatchthis'] =
true;
459 if ( $watchlistExpiry ) {
460 $requestArray[
'wpWatchlistExpiry'] = $watchlistExpiry;
467 if ( $tagStatus->isOK() ) {
468 $requestArray[
'wpChangeTags'] = implode(
',',
$params[
'tags'] );
476 $requestArray += $this->
getRequest()->getValues();
489 $articleContext->setWikiPage( $pageObj );
490 $articleContext->setUser( $this->
getUser() );
493 $articleObject = Article::newFromWikiPage( $pageObj, $articleContext );
495 $ep =
new EditPage( $articleObject );
497 $ep->setApiEditOverride(
true );
498 $ep->setContextTitle( $titleObj );
499 $ep->importFormData( $req );
500 $tempUserCreateStatus = $ep->maybeActivateTempUserCreate(
true );
501 if ( !$tempUserCreateStatus->isOK() ) {
502 $this->
dieWithError(
'apierror-tempuseracquirefailed',
'tempuseracquirefailed' );
507 $editRevId = $requestArray[
'editRevId'] ??
false;
508 $baseRev = $this->revisionLookup->getRevisionByTitle( $titleObj, $editRevId );
509 $baseContentModel =
null;
512 $baseContent = $baseRev->getContent( SlotRecord::MAIN );
513 $baseContentModel = $baseContent ? $baseContent->getModel() :
null;
516 $baseContentModel ??= $pageObj->getContentModel();
520 $contentModelsCanDiffer =
$params[
'contentmodel'] || isset( $undoContentModel );
522 if ( !$contentModelsCanDiffer && $contentModel !== $baseContentModel ) {
523 $this->
dieWithError( [
'apierror-contentmodel-mismatch', $contentModel, $baseContentModel ] );
527 $oldRevId = $articleObject->getRevIdFetched();
536 $status = $ep->attemptSave( $result );
537 $statusValue = is_int( $status->value ) ? $status->value : 0;
542 switch ( $statusValue ) {
543 case EditPage::AS_HOOK_ERROR:
544 case EditPage::AS_HOOK_ERROR_EXPECTED:
545 if ( $status->statusData !==
null ) {
546 $r = $status->statusData;
547 $r[
'result'] =
'Failure';
551 if ( !$status->getMessages() ) {
554 $status->fatal(
'hookaborted' );
563 case EditPage::AS_BLOCKED_PAGE_FOR_USER:
568 case EditPage::AS_READ_ONLY_PAGE:
572 case EditPage::AS_SUCCESS_NEW_ARTICLE:
576 case EditPage::AS_SUCCESS_UPDATE:
577 $r[
'result'] =
'Success';
578 $r[
'pageid'] = (int)$titleObj->getArticleID();
579 $r[
'title'] = $titleObj->getPrefixedText();
580 $r[
'contentmodel'] = $articleObject->getPage()->getContentModel();
581 $newRevId = $articleObject->getPage()->getLatest();
582 if ( $newRevId == $oldRevId ) {
583 $r[
'nochange'] =
true;
585 $r[
'oldrevid'] = (int)$oldRevId;
586 $r[
'newrevid'] = (int)$newRevId;
588 $pageObj->getTimestamp() );
592 $r[
'watched'] =
true;
595 $this->watchedItemStore,
600 if ( $watchlistExpiry ) {
601 $r[
'watchlistexpiry'] = $watchlistExpiry;
604 $this->persistGlobalSession();
614 $shouldRedirectForTempUser = isset( $result[
'savedTempUser'] ) ||
615 ( $user->isTemp() && ( $user->getEditCount() === 0 ) );
616 if ( $shouldRedirectForTempUser ) {
617 $r[
'tempusercreated'] =
true;
618 $params[
'returnto'] ??= $titleObj->getPrefixedDBkey();
619 $redirectUrl = $this->getTempUserRedirectUrl(
621 $result[
'savedTempUser'] ?? $user
623 if ( $redirectUrl ) {
624 $r[
'tempusercreatedredirect'] = $redirectUrl;
631 if ( !$status->getMessages() ) {
634 switch ( $statusValue ) {
636 case EditPage::AS_IMAGE_REDIRECT_ANON:
637 $status->fatal(
'apierror-noimageredirect-anon' );
639 case EditPage::AS_IMAGE_REDIRECT_LOGGED:
640 $status->fatal(
'apierror-noimageredirect' );
642 case EditPage::AS_CONTENT_TOO_BIG:
643 case EditPage::AS_MAX_ARTICLE_SIZE_EXCEEDED:
644 $status->fatal(
'apierror-contenttoobig',
647 case EditPage::AS_READ_ONLY_PAGE_ANON:
648 $status->fatal(
'apierror-noedit-anon' );
650 case EditPage::AS_NO_CHANGE_CONTENT_MODEL:
651 $status->fatal(
'apierror-cantchangecontentmodel' );
653 case EditPage::AS_ARTICLE_WAS_DELETED:
654 $status->fatal(
'apierror-pagedeleted' );
656 case EditPage::AS_CONFLICT_DETECTED:
657 $status->fatal(
'edit-conflict' );
664 case EditPage::AS_SPAM_ERROR:
666 $status->fatal(
'apierror-spamdetected', $result[
'spam'] );
668 case EditPage::AS_READ_ONLY_PAGE_LOGGED:
669 $status->fatal(
'apierror-noedit' );
671 case EditPage::AS_RATE_LIMITED:
672 $status->fatal(
'apierror-ratelimited' );
674 case EditPage::AS_NO_CREATE_PERMISSION:
675 $status->fatal(
'nocreate-loggedin' );
677 case EditPage::AS_BLANK_ARTICLE:
678 $status->fatal(
'apierror-emptypage' );
680 case EditPage::AS_TEXTBOX_EMPTY:
681 $status->fatal(
'apierror-emptynewsection' );
683 case EditPage::AS_SUMMARY_NEEDED:
684 $status->fatal(
'apierror-summaryrequired' );
687 wfWarn( __METHOD__ .
": Unknown EditPage code $statusValue with no message" );
688 $status->fatal(
'apierror-unknownerror-editpage', $statusValue );
709 ParamValidator::PARAM_TYPE =>
'string',
712 ParamValidator::PARAM_TYPE =>
'integer',
716 ParamValidator::PARAM_TYPE =>
'string',
719 ParamValidator::PARAM_TYPE =>
'text',
723 ParamValidator::PARAM_TYPE =>
'tags',
724 ParamValidator::PARAM_ISMULTI =>
true,
730 ParamValidator::PARAM_TYPE =>
'integer',
733 ParamValidator::PARAM_TYPE =>
'timestamp',
735 'starttimestamp' => [
736 ParamValidator::PARAM_TYPE =>
'timestamp',
739 'createonly' =>
false,
742 ParamValidator::PARAM_DEFAULT =>
false,
743 ParamValidator::PARAM_DEPRECATED =>
true,
746 ParamValidator::PARAM_DEFAULT =>
false,
747 ParamValidator::PARAM_DEPRECATED =>
true,
758 ParamValidator::PARAM_TYPE =>
'text',
761 ParamValidator::PARAM_TYPE =>
'text',
764 ParamValidator::PARAM_TYPE =>
'integer',
765 IntegerDef::PARAM_MIN => 0,
769 ParamValidator::PARAM_TYPE =>
'integer',
770 IntegerDef::PARAM_MIN => 0,
774 ParamValidator::PARAM_TYPE =>
'boolean',
775 ParamValidator::PARAM_DEFAULT =>
false,
778 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
781 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getContentModels(),
789 $params += $this->getCreateTempUserParams();