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;
459 $prefName =
'watchdefault-expiry';
460 if ( !$pageObj->exists() ) {
461 $prefName =
'watchcreations-expiry';
465 if ( $watchlistExpiry ) {
466 $requestArray[
'wpWatchlistExpiry'] = $watchlistExpiry;
471 if ( $params[
'tags'] ) {
472 $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params[
'tags'], $this->
getAuthority() );
473 if ( $tagStatus->isOK() ) {
474 $requestArray[
'wpChangeTags'] = implode(
',', $params[
'tags'] );
482 $requestArray += $this->
getRequest()->getValues();
495 $articleContext->setWikiPage( $pageObj );
496 $articleContext->setUser( $this->
getUser() );
499 $articleObject = Article::newFromWikiPage( $pageObj, $articleContext );
501 $ep =
new EditPage( $articleObject );
503 $ep->setApiEditOverride(
true );
504 $ep->setContextTitle( $titleObj );
505 $ep->importFormData( $req );
506 $tempUserCreateStatus = $ep->maybeActivateTempUserCreate(
true );
507 if ( !$tempUserCreateStatus->isOK() ) {
508 $this->
dieWithError(
'apierror-tempuseracquirefailed',
'tempuseracquirefailed' );
513 $editRevId = $requestArray[
'editRevId'] ??
false;
514 $baseRev = $this->revisionLookup->getRevisionByTitle( $titleObj, $editRevId );
515 $baseContentModel =
null;
518 $baseContent = $baseRev->getContent( SlotRecord::MAIN );
519 $baseContentModel = $baseContent ? $baseContent->getModel() :
null;
522 $baseContentModel ??= $pageObj->getContentModel();
526 $contentModelsCanDiffer = $params[
'contentmodel'] || isset( $undoContentModel );
528 if ( !$contentModelsCanDiffer && $contentModel !== $baseContentModel ) {
529 $this->
dieWithError( [
'apierror-contentmodel-mismatch', $contentModel, $baseContentModel ] );
533 $oldRevId = $articleObject->getRevIdFetched();
542 $status = $ep->attemptSave( $result );
543 $statusValue = is_int( $status->value ) ? $status->value : 0;
548 switch ( $statusValue ) {
549 case EditPage::AS_HOOK_ERROR:
550 case EditPage::AS_HOOK_ERROR_EXPECTED:
551 if ( $status->statusData !==
null ) {
552 $r = $status->statusData;
553 $r[
'result'] =
'Failure';
557 if ( !$status->getMessages() ) {
560 $status->fatal(
'hookaborted' );
569 case EditPage::AS_BLOCKED_PAGE_FOR_USER:
574 case EditPage::AS_READ_ONLY_PAGE:
578 case EditPage::AS_SUCCESS_NEW_ARTICLE:
582 case EditPage::AS_SUCCESS_UPDATE:
583 $r[
'result'] =
'Success';
584 $r[
'pageid'] = (int)$titleObj->getArticleID();
585 $r[
'title'] = $titleObj->getPrefixedText();
586 $r[
'contentmodel'] = $articleObject->getPage()->getContentModel();
587 $newRevId = $articleObject->getPage()->getLatest();
588 if ( $newRevId == $oldRevId ) {
589 $r[
'nochange'] =
true;
591 $r[
'oldrevid'] = (int)$oldRevId;
592 $r[
'newrevid'] = (int)$newRevId;
594 $pageObj->getTimestamp() );
598 $r[
'watched'] =
true;
601 $this->watchedItemStore,
606 if ( $watchlistExpiry ) {
607 $r[
'watchlistexpiry'] = $watchlistExpiry;
610 $this->persistGlobalSession();
620 $shouldRedirectForTempUser = isset( $result[
'savedTempUser'] ) ||
621 ( $user->isTemp() && ( $user->getEditCount() === 0 ) );
622 if ( $shouldRedirectForTempUser ) {
623 $r[
'tempusercreated'] =
true;
624 $params[
'returnto'] ??= $titleObj->getPrefixedDBkey();
625 $redirectUrl = $this->getTempUserRedirectUrl(
627 $result[
'savedTempUser'] ?? $user
629 if ( $redirectUrl ) {
630 $r[
'tempusercreatedredirect'] = $redirectUrl;
637 if ( !$status->getMessages() ) {
640 switch ( $statusValue ) {
642 case EditPage::AS_IMAGE_REDIRECT_ANON:
643 $status->fatal(
'apierror-noimageredirect-anon' );
645 case EditPage::AS_IMAGE_REDIRECT_LOGGED:
646 $status->fatal(
'apierror-noimageredirect' );
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();