143 $titleObj = $pageObj->getTitle();
147 if ( $params[
'redirect'] ) {
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();
198 if ( $params[
'undo'] > 0 ) {
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() ]
224 $toMD5 = $params[
'text'];
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' ) {
266 $section = $params[
'section'];
267 $content = $content->getSection( $section );
278 $text = $content->serialize( $contentFormat );
281 $params[
'text'] = $params[
'prependtext'] . $text . $params[
'appendtext'];
282 $toMD5 = $params[
'prependtext'] . $params[
'appendtext'];
285 if ( $params[
'undo'] > 0 ) {
286 $undoRev = $this->revisionLookup->getRevisionById( $params[
'undo'] );
287 if ( $undoRev ===
null || $undoRev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
288 $this->
dieWithError( [
'apierror-nosuchrevid', $params[
'undo'] ] );
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 ) ) {
298 $this->
dieWithError( [
'apierror-nosuchrevid', $params[
'undoafter'] ] );
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 ) {
323 if ( !$params[
'contentmodel'] && !$params[
'contentformat'] ) {
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,
379 'wpIgnoreDoubleRedirects' =>
true,
380 'bot' => $params[
'bot'],
381 'wpUnicodeCheck' => EditPage::UNICODE_CHECK,
385 if ( $params[
'summary'] !==
null ) {
386 $requestArray[
'wpSummary'] = $params[
'summary'];
389 if ( $params[
'sectiontitle'] !==
null ) {
390 $requestArray[
'wpSectionTitle'] = $params[
'sectiontitle'];
393 if ( $params[
'undo'] > 0 ) {
394 $requestArray[
'wpUndidRevision'] = $params[
'undo'];
396 if ( $params[
'undoafter'] > 0 ) {
397 $requestArray[
'wpUndoAfter'] = $params[
'undoafter'];
401 if ( !empty( $params[
'baserevid'] ) ) {
402 $requestArray[
'editRevId'] = $params[
'baserevid'];
407 if ( $params[
'basetimestamp'] !==
null && (
bool)$this->
getMain()->getVal(
'basetimestamp' ) ) {
408 $requestArray[
'wpEdittime'] = $params[
'basetimestamp'];
409 } elseif ( empty( $params[
'baserevid'] ) ) {
412 $requestArray[
'wpEdittime'] = $pageObj->getTimestamp();
415 if ( $params[
'starttimestamp'] !==
null ) {
416 $requestArray[
'wpStarttime'] = $params[
'starttimestamp'];
421 if ( $params[
'minor'] || ( !$params[
'notminor'] &&
422 $this->userOptionsLookup->getOption( $user,
'minordefault' ) )
424 $requestArray[
'wpMinoredit'] =
'';
427 if ( $params[
'recreate'] ) {
428 $requestArray[
'wpRecreate'] =
'';
431 if ( $params[
'section'] !==
null ) {
432 $section = $params[
'section'];
433 if ( !preg_match(
'/^((T-)?\d+|new)$/', $section ) ) {
436 $content = $pageObj->getContent();
437 if ( $section !==
'0'
439 && ( !$content || !$content->getSection( $section ) )
441 $this->
dieWithError( [
'apierror-nosuchsection', $section ] );
443 $requestArray[
'wpSection'] = $params[
'section'];
445 $requestArray[
'wpSection'] =
'';
451 if ( $params[
'watch'] ) {
453 } elseif ( $params[
'unwatch'] ) {
458 $requestArray[
'wpWatchthis'] =
true;
461 if ( $watchlistExpiry ) {
462 $requestArray[
'wpWatchlistExpiry'] = $watchlistExpiry;
467 if ( $params[
'tags'] ) {
468 $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params[
'tags'], $this->
getAuthority() );
469 if ( $tagStatus->isOK() ) {
470 $requestArray[
'wpChangeTags'] = implode(
',', $params[
'tags'] );
478 $requestArray += $this->
getRequest()->getValues();
491 $articleContext->setWikiPage( $pageObj );
492 $articleContext->setUser( $this->
getUser() );
495 $articleObject = Article::newFromWikiPage( $pageObj, $articleContext );
497 $ep =
new EditPage( $articleObject );
499 $ep->setApiEditOverride(
true );
500 $ep->setContextTitle( $titleObj );
501 $ep->importFormData( $req );
502 $tempUserCreateStatus = $ep->maybeActivateTempUserCreate(
true );
503 if ( !$tempUserCreateStatus->isOK() ) {
504 $this->
dieWithError(
'apierror-tempuseracquirefailed',
'tempuseracquirefailed' );
509 $editRevId = $requestArray[
'editRevId'] ??
false;
510 $baseRev = $this->revisionLookup->getRevisionByTitle( $titleObj, $editRevId );
511 $baseContentModel =
null;
514 $baseContent = $baseRev->getContent( SlotRecord::MAIN );
515 $baseContentModel = $baseContent ? $baseContent->getModel() :
null;
518 $baseContentModel ??= $pageObj->getContentModel();
522 $contentModelsCanDiffer = $params[
'contentmodel'] || isset( $undoContentModel );
524 if ( !$contentModelsCanDiffer && $contentModel !== $baseContentModel ) {
525 $this->
dieWithError( [
'apierror-contentmodel-mismatch', $contentModel, $baseContentModel ] );
529 $oldRevId = $articleObject->getRevIdFetched();
538 $status = $ep->attemptSave( $result );
539 $statusValue = is_int( $status->value ) ? $status->value : 0;
544 switch ( $statusValue ) {
545 case EditPage::AS_HOOK_ERROR:
546 case EditPage::AS_HOOK_ERROR_EXPECTED:
547 if ( $status->statusData !==
null ) {
548 $r = $status->statusData;
549 $r[
'result'] =
'Failure';
553 if ( !$status->getMessages() ) {
556 $status->fatal(
'hookaborted' );
565 case EditPage::AS_BLOCKED_PAGE_FOR_USER:
570 case EditPage::AS_READ_ONLY_PAGE:
574 case EditPage::AS_SUCCESS_NEW_ARTICLE:
578 case EditPage::AS_SUCCESS_UPDATE:
579 $r[
'result'] =
'Success';
580 $r[
'pageid'] = (int)$titleObj->getArticleID();
581 $r[
'title'] = $titleObj->getPrefixedText();
582 $r[
'contentmodel'] = $articleObject->getPage()->getContentModel();
583 $newRevId = $articleObject->getPage()->getLatest();
584 if ( $newRevId == $oldRevId ) {
585 $r[
'nochange'] =
true;
587 $r[
'oldrevid'] = (int)$oldRevId;
588 $r[
'newrevid'] = (int)$newRevId;
590 $pageObj->getTimestamp() );
594 $r[
'watched'] =
true;
597 $this->watchedItemStore,
602 if ( $watchlistExpiry ) {
603 $r[
'watchlistexpiry'] = $watchlistExpiry;
606 $this->persistGlobalSession();
616 $shouldRedirectForTempUser = isset( $result[
'savedTempUser'] ) ||
617 ( $user->isTemp() && ( $user->getEditCount() === 0 ) );
618 if ( $shouldRedirectForTempUser ) {
619 $r[
'tempusercreated'] =
true;
620 $params[
'returnto'] ??= $titleObj->getPrefixedDBkey();
621 $redirectUrl = $this->getTempUserRedirectUrl(
623 $result[
'savedTempUser'] ?? $user
625 if ( $redirectUrl ) {
626 $r[
'tempusercreatedredirect'] = $redirectUrl;
633 if ( !$status->getMessages() ) {
636 switch ( $statusValue ) {
638 case EditPage::AS_IMAGE_REDIRECT_ANON:
639 $status->fatal(
'apierror-noimageredirect-anon' );
641 case EditPage::AS_IMAGE_REDIRECT_LOGGED:
642 $status->fatal(
'apierror-noimageredirect' );
644 case EditPage::AS_READ_ONLY_PAGE_ANON:
645 $status->fatal(
'apierror-noedit-anon' );
647 case EditPage::AS_NO_CHANGE_CONTENT_MODEL:
648 $status->fatal(
'apierror-cantchangecontentmodel' );
650 case EditPage::AS_ARTICLE_WAS_DELETED:
651 $status->fatal(
'apierror-pagedeleted' );
653 case EditPage::AS_CONFLICT_DETECTED:
654 $status->fatal(
'edit-conflict' );
661 case EditPage::AS_SPAM_ERROR:
663 $status->fatal(
'apierror-spamdetected', $result[
'spam'] );
665 case EditPage::AS_READ_ONLY_PAGE_LOGGED:
666 $status->fatal(
'apierror-noedit' );
668 case EditPage::AS_RATE_LIMITED:
669 $status->fatal(
'apierror-ratelimited' );
671 case EditPage::AS_NO_CREATE_PERMISSION:
672 $status->fatal(
'nocreate-loggedin' );
674 case EditPage::AS_BLANK_ARTICLE:
675 $status->fatal(
'apierror-emptypage' );
677 case EditPage::AS_TEXTBOX_EMPTY:
678 $status->fatal(
'apierror-emptynewsection' );
680 case EditPage::AS_SUMMARY_NEEDED:
681 $status->fatal(
'apierror-summaryrequired' );
684 wfWarn( __METHOD__ .
": Unknown EditPage code $statusValue with no message" );
685 $status->fatal(
'apierror-unknownerror-editpage', $statusValue );
706 ParamValidator::PARAM_TYPE =>
'string',
709 ParamValidator::PARAM_TYPE =>
'integer',
713 ParamValidator::PARAM_TYPE =>
'string',
716 ParamValidator::PARAM_TYPE =>
'text',
720 ParamValidator::PARAM_TYPE =>
'tags',
721 ParamValidator::PARAM_ISMULTI =>
true,
727 ParamValidator::PARAM_TYPE =>
'integer',
730 ParamValidator::PARAM_TYPE =>
'timestamp',
732 'starttimestamp' => [
733 ParamValidator::PARAM_TYPE =>
'timestamp',
736 'createonly' =>
false,
739 ParamValidator::PARAM_DEFAULT =>
false,
740 ParamValidator::PARAM_DEPRECATED =>
true,
743 ParamValidator::PARAM_DEFAULT =>
false,
744 ParamValidator::PARAM_DEPRECATED =>
true,
755 ParamValidator::PARAM_TYPE =>
'text',
758 ParamValidator::PARAM_TYPE =>
'text',
761 ParamValidator::PARAM_TYPE =>
'integer',
762 IntegerDef::PARAM_MIN => 0,
766 ParamValidator::PARAM_TYPE =>
'integer',
767 IntegerDef::PARAM_MIN => 0,
771 ParamValidator::PARAM_TYPE =>
'boolean',
772 ParamValidator::PARAM_DEFAULT =>
false,
775 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
778 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getContentModels(),
786 $params += $this->getCreateTempUserParams();