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,
378 'wpIgnoreBrokenRedirects' =>
true,
380 'wpUnicodeCheck' => EditPage::UNICODE_CHECK,
384 if (
$params[
'summary'] !==
null ) {
385 $requestArray[
'wpSummary'] =
$params[
'summary'];
388 if (
$params[
'sectiontitle'] !==
null ) {
389 $requestArray[
'wpSectionTitle'] =
$params[
'sectiontitle'];
393 $requestArray[
'wpUndidRevision'] =
$params[
'undo'];
395 if (
$params[
'undoafter'] > 0 ) {
396 $requestArray[
'wpUndoAfter'] =
$params[
'undoafter'];
400 if ( !empty(
$params[
'baserevid'] ) ) {
401 $requestArray[
'editRevId'] =
$params[
'baserevid'];
406 if (
$params[
'basetimestamp'] !==
null && (
bool)$this->
getMain()->getVal(
'basetimestamp' ) ) {
407 $requestArray[
'wpEdittime'] =
$params[
'basetimestamp'];
408 } elseif ( empty(
$params[
'baserevid'] ) ) {
411 $requestArray[
'wpEdittime'] = $pageObj->getTimestamp();
414 if (
$params[
'starttimestamp'] !==
null ) {
415 $requestArray[
'wpStarttime'] =
$params[
'starttimestamp'];
421 $this->userOptionsLookup->getOption( $user,
'minordefault' ) )
423 $requestArray[
'wpMinoredit'] =
'';
427 $requestArray[
'wpRecreate'] =
'';
430 if (
$params[
'section'] !==
null ) {
432 if ( !preg_match(
'/^((T-)?\d+|new)$/', $section ) ) {
435 $content = $pageObj->getContent();
436 if ( $section !==
'0'
438 && ( !$content || !$content->getSection( $section ) )
440 $this->
dieWithError( [
'apierror-nosuchsection', $section ] );
442 $requestArray[
'wpSection'] =
$params[
'section'];
444 $requestArray[
'wpSection'] =
'';
452 } elseif (
$params[
'unwatch'] ) {
457 $requestArray[
'wpWatchthis'] =
true;
460 if ( $watchlistExpiry ) {
461 $requestArray[
'wpWatchlistExpiry'] = $watchlistExpiry;
468 if ( $tagStatus->isOK() ) {
469 $requestArray[
'wpChangeTags'] = implode(
',',
$params[
'tags'] );
477 $requestArray += $this->
getRequest()->getValues();
490 $articleContext->setWikiPage( $pageObj );
491 $articleContext->setUser( $this->
getUser() );
494 $articleObject = Article::newFromWikiPage( $pageObj, $articleContext );
496 $ep =
new EditPage( $articleObject );
498 $ep->setApiEditOverride(
true );
499 $ep->setContextTitle( $titleObj );
500 $ep->importFormData( $req );
501 $tempUserCreateStatus = $ep->maybeActivateTempUserCreate(
true );
502 if ( !$tempUserCreateStatus->isOK() ) {
503 $this->
dieWithError(
'apierror-tempuseracquirefailed',
'tempuseracquirefailed' );
508 $editRevId = $requestArray[
'editRevId'] ??
false;
509 $baseRev = $this->revisionLookup->getRevisionByTitle( $titleObj, $editRevId );
510 $baseContentModel =
null;
513 $baseContent = $baseRev->getContent( SlotRecord::MAIN );
514 $baseContentModel = $baseContent ? $baseContent->getModel() :
null;
517 $baseContentModel ??= $pageObj->getContentModel();
521 $contentModelsCanDiffer =
$params[
'contentmodel'] || isset( $undoContentModel );
523 if ( !$contentModelsCanDiffer && $contentModel !== $baseContentModel ) {
524 $this->
dieWithError( [
'apierror-contentmodel-mismatch', $contentModel, $baseContentModel ] );
528 $oldRevId = $articleObject->getRevIdFetched();
537 $status = $ep->attemptSave( $result );
538 $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->getMessages() ) {
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();
615 $shouldRedirectForTempUser = isset( $result[
'savedTempUser'] ) ||
616 ( $user->isTemp() && ( $user->getEditCount() === 0 ) );
617 if ( $shouldRedirectForTempUser ) {
618 $r[
'tempusercreated'] =
true;
619 $params[
'returnto'] ??= $titleObj->getPrefixedDBkey();
620 $redirectUrl = $this->getTempUserRedirectUrl(
622 $result[
'savedTempUser'] ?? $user
624 if ( $redirectUrl ) {
625 $r[
'tempusercreatedredirect'] = $redirectUrl;
632 if ( !$status->getMessages() ) {
635 switch ( $statusValue ) {
637 case EditPage::AS_IMAGE_REDIRECT_ANON:
638 $status->fatal(
'apierror-noimageredirect-anon' );
640 case EditPage::AS_IMAGE_REDIRECT_LOGGED:
641 $status->fatal(
'apierror-noimageredirect' );
643 case EditPage::AS_CONTENT_TOO_BIG:
644 case EditPage::AS_MAX_ARTICLE_SIZE_EXCEEDED:
645 $status->fatal(
'apierror-contenttoobig',
648 case EditPage::AS_READ_ONLY_PAGE_ANON:
649 $status->fatal(
'apierror-noedit-anon' );
651 case EditPage::AS_NO_CHANGE_CONTENT_MODEL:
652 $status->fatal(
'apierror-cantchangecontentmodel' );
654 case EditPage::AS_ARTICLE_WAS_DELETED:
655 $status->fatal(
'apierror-pagedeleted' );
657 case EditPage::AS_CONFLICT_DETECTED:
658 $status->fatal(
'edit-conflict' );
665 case EditPage::AS_SPAM_ERROR:
667 $status->fatal(
'apierror-spamdetected', $result[
'spam'] );
669 case EditPage::AS_READ_ONLY_PAGE_LOGGED:
670 $status->fatal(
'apierror-noedit' );
672 case EditPage::AS_RATE_LIMITED:
673 $status->fatal(
'apierror-ratelimited' );
675 case EditPage::AS_NO_CREATE_PERMISSION:
676 $status->fatal(
'nocreate-loggedin' );
678 case EditPage::AS_BLANK_ARTICLE:
679 $status->fatal(
'apierror-emptypage' );
681 case EditPage::AS_TEXTBOX_EMPTY:
682 $status->fatal(
'apierror-emptynewsection' );
684 case EditPage::AS_SUMMARY_NEEDED:
685 $status->fatal(
'apierror-summaryrequired' );
688 wfWarn( __METHOD__ .
": Unknown EditPage code $statusValue with no message" );
689 $status->fatal(
'apierror-unknownerror-editpage', $statusValue );
710 ParamValidator::PARAM_TYPE =>
'string',
713 ParamValidator::PARAM_TYPE =>
'integer',
717 ParamValidator::PARAM_TYPE =>
'string',
720 ParamValidator::PARAM_TYPE =>
'text',
724 ParamValidator::PARAM_TYPE =>
'tags',
725 ParamValidator::PARAM_ISMULTI =>
true,
731 ParamValidator::PARAM_TYPE =>
'integer',
734 ParamValidator::PARAM_TYPE =>
'timestamp',
736 'starttimestamp' => [
737 ParamValidator::PARAM_TYPE =>
'timestamp',
740 'createonly' =>
false,
743 ParamValidator::PARAM_DEFAULT =>
false,
744 ParamValidator::PARAM_DEPRECATED =>
true,
747 ParamValidator::PARAM_DEFAULT =>
false,
748 ParamValidator::PARAM_DEPRECATED =>
true,
759 ParamValidator::PARAM_TYPE =>
'text',
762 ParamValidator::PARAM_TYPE =>
'text',
765 ParamValidator::PARAM_TYPE =>
'integer',
766 IntegerDef::PARAM_MIN => 0,
770 ParamValidator::PARAM_TYPE =>
'integer',
771 IntegerDef::PARAM_MIN => 0,
775 ParamValidator::PARAM_TYPE =>
'boolean',
776 ParamValidator::PARAM_DEFAULT =>
false,
779 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
782 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getContentModels(),
790 $params += $this->getCreateTempUserParams();