54 private $guessedTitle =
false;
77 parent::__construct( $mainModule, $moduleName );
78 $this->revisionStore = $revisionStore;
79 $this->archivedRevisionLookup = $archivedRevisionLookup;
80 $this->slotRoleRegistry = $slotRoleRegistry;
81 $this->contentHandlerFactory = $contentHandlerFactory;
82 $this->contentTransformer = $contentTransformer;
83 $this->commentFormatter = $commentFormatter;
84 $this->tempUserCreator = $tempUserCreator;
85 $this->userFactory = $userFactory;
94 $params,
'fromtitle',
'fromid',
'fromrev',
'fromtext',
'fromslots'
97 $params,
'totitle',
'toid',
'torev',
'totext',
'torelative',
'toslots'
100 $this->props = array_fill_keys(
$params[
'prop'],
true );
103 $this->
getMain()->setCacheMode(
'public' );
106 [ $fromRev, $fromRelRev, $fromValsRev ] = $this->getDiffRevision(
'from',
$params );
109 if (
$params[
'torelative'] !==
null ) {
110 if ( !$fromRelRev ) {
111 $this->
dieWithError(
'apierror-compare-relative-to-nothing' );
116 $this->
dieWithError( [
'apierror-compare-relative-to-deleted', $params[
'torelative'] ] );
118 switch (
$params[
'torelative'] ) {
121 [ $toRev, $toRelRev, $toValsRev ] = [ $fromRev, $fromRelRev, $fromValsRev ];
122 $fromRev = $this->revisionStore->getPreviousRevision( $toRelRev );
123 $fromRelRev = $fromRev;
124 $fromValsRev = $fromRev;
126 $title = Title::newFromLinkTarget( $toRelRev->getPageAsLinkTarget() );
128 'apiwarn-compare-no-prev',
136 $title ?: $toRev->getPage()
140 $toRelRev->getMainContentRaw()
141 ->getContentHandler()
148 $toRev = $this->revisionStore->getNextRevision( $fromRelRev );
152 $title = Title::newFromLinkTarget( $fromRelRev->getPageAsLinkTarget() );
154 'apiwarn-compare-no-next',
161 $toRev = MutableRevisionRecord::newFromParentRevision( $fromRelRev );
166 $title = $fromRelRev->getPageAsLinkTarget();
167 $toRev = $this->revisionStore->getRevisionByTitle( $title );
169 $title = Title::newFromLinkTarget( $title );
171 [
'apierror-missingrev-title',
wfEscapeWikiText( $title->getPrefixedText() ) ],
180 [ $toRev, $toRelRev, $toValsRev ] = $this->getDiffRevision(
'to',
$params );
186 if ( !$fromRev || !$toRev ) {
192 if ( !$fromRev->userCan( RevisionRecord::DELETED_TEXT, $this->getAuthority() ) ) {
193 $this->
dieWithError( [
'apierror-missingcontent-revid', $fromRev->getId() ],
'missingcontent' );
195 if ( !$toRev->userCan( RevisionRecord::DELETED_TEXT, $this->getAuthority() ) ) {
196 $this->
dieWithError( [
'apierror-missingcontent-revid', $toRev->getId() ],
'missingcontent' );
201 if ( $fromRelRev && $fromRelRev->getPageAsLinkTarget() ) {
202 $context->setTitle( Title::newFromLinkTarget( $fromRelRev->getPageAsLinkTarget() ) );
204 } elseif ( $toRelRev && $toRelRev->getPageAsLinkTarget() ) {
205 $context->setTitle( Title::newFromLinkTarget( $toRelRev->getPageAsLinkTarget() ) );
207 $guessedTitle = $this->guessTitle();
208 if ( $guessedTitle ) {
209 $context->setTitle( $guessedTitle );
212 $this->differenceEngine->setContext( $context );
213 $this->differenceEngine->setSlotDiffOptions( [
'diff-type' =>
$params[
'difftype'] ] );
214 $this->differenceEngine->setRevisions( $fromRev, $toRev );
215 if (
$params[
'slots'] ===
null ) {
216 $difftext = $this->differenceEngine->getDiffBody();
217 if ( $difftext ===
false ) {
222 foreach (
$params[
'slots'] as $role ) {
223 $difftext[$role] = $this->differenceEngine->getDiffBodyForRole( $role );
226 foreach ( $this->differenceEngine->getRevisionLoadErrors() as $msg ) {
232 $this->setVals( $vals,
'from', $fromValsRev );
234 $this->setVals( $vals,
'to', $toValsRev );
236 if ( isset( $this->props[
'rel'] ) ) {
238 $rev = $this->revisionStore->getPreviousRevision( $fromRev );
240 $vals[
'prev'] = $rev->getId();
244 $rev = $this->revisionStore->getNextRevision( $toRev );
246 $vals[
'next'] = $rev->getId();
251 if ( isset( $this->props[
'diffsize'] ) ) {
252 $vals[
'diffsize'] = 0;
253 foreach ( (array)$difftext as $text ) {
254 $vals[
'diffsize'] += strlen( $text );
257 if ( isset( $this->props[
'diff'] ) ) {
258 if ( is_array( $difftext ) ) {
260 $vals[
'bodies'] = $difftext;
280 private function getRevisionById( $id ) {
281 $rev = $this->revisionStore->getRevisionById( $id );
282 if ( !$rev && $this->
getAuthority()->isAllowedAny(
'deletedtext',
'undelete' ) ) {
284 $rev = $this->archivedRevisionLookup->getArchivedRevisionRecord(
null, $id );
294 private function guessTitle() {
295 if ( $this->guessedTitle !==
false ) {
296 return $this->guessedTitle;
299 $this->guessedTitle =
null;
302 foreach ( [
'from',
'to' ] as $prefix ) {
303 if (
$params[
"{$prefix}rev"] !==
null ) {
304 $rev = $this->getRevisionById(
$params[
"{$prefix}rev"] );
306 $this->guessedTitle = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
311 if (
$params[
"{$prefix}title"] !==
null ) {
312 $title = Title::newFromText(
$params[
"{$prefix}title"] );
313 if ( $title && !$title->isExternal() ) {
314 $this->guessedTitle = $title;
319 if (
$params[
"{$prefix}id"] !==
null ) {
320 $title = Title::newFromID(
$params[
"{$prefix}id"] );
322 $this->guessedTitle = $title;
328 return $this->guessedTitle;
336 private function guessModel( $role ) {
339 foreach ( [
'from',
'to' ] as $prefix ) {
340 if (
$params[
"{$prefix}rev"] !==
null ) {
341 $rev = $this->getRevisionById(
$params[
"{$prefix}rev"] );
342 if ( $rev && $rev->hasSlot( $role ) ) {
343 return $rev->getSlot( $role, RevisionRecord::RAW )->getModel();
348 $guessedTitle = $this->guessTitle();
349 if ( $guessedTitle ) {
350 return $this->slotRoleRegistry->getRoleHandler( $role )->getDefaultModel( $guessedTitle );
353 if ( isset(
$params[
"fromcontentmodel-$role"] ) ) {
354 return $params[
"fromcontentmodel-$role"];
356 if ( isset(
$params[
"tocontentmodel-$role"] ) ) {
357 return $params[
"tocontentmodel-$role"];
360 if ( $role === SlotRecord::MAIN ) {
361 if ( isset(
$params[
'fromcontentmodel'] ) ) {
362 return $params[
'fromcontentmodel'];
364 if ( isset(
$params[
'tocontentmodel'] ) ) {
365 return $params[
'tocontentmodel'];
387 private function getDiffRevision( $prefix, array
$params ) {
391 if (
$params[
"{$prefix}text"] !==
null ) {
392 $params[
"{$prefix}slots"] = [ SlotRecord::MAIN ];
394 $params[
"{$prefix}section-main"] =
null;
395 $params[
"{$prefix}contentmodel-main"] =
$params[
"{$prefix}contentmodel"];
396 $params[
"{$prefix}contentformat-main"] =
$params[
"{$prefix}contentformat"];
401 $suppliedContent =
$params[
"{$prefix}slots"] !==
null;
405 if (
$params[
"{$prefix}rev"] !==
null ) {
406 $revId =
$params[
"{$prefix}rev"];
407 } elseif (
$params[
"{$prefix}title"] !==
null ||
$params[
"{$prefix}id"] !==
null ) {
408 if (
$params[
"{$prefix}title"] !==
null ) {
409 $title = Title::newFromText(
$params[
"{$prefix}title"] );
410 if ( !$title || $title->isExternal() ) {
416 $title = Title::newFromID(
$params[
"{$prefix}id"] );
418 $this->
dieWithError( [
'apierror-nosuchpageid', $params[
"{$prefix}id"] ] );
421 $revId = $title->getLatestRevID();
425 if ( !$suppliedContent ) {
426 if ( $title->exists() ) {
428 [
'apierror-missingrev-title',
wfEscapeWikiText( $title->getPrefixedText() ) ],
433 [
'apierror-missingtitle-byname',
wfEscapeWikiText( $title->getPrefixedText() ) ],
440 if ( $revId !==
null ) {
441 $rev = $this->getRevisionById( $revId );
443 $this->
dieWithError( [
'apierror-nosuchrevid', $revId ] );
445 $title = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
449 if ( !$suppliedContent ) {
453 if ( isset(
$params[
"{$prefix}section"] ) ) {
454 $section =
$params[
"{$prefix}section"];
456 $newRev = MutableRevisionRecord::newFromParentRevision( $rev );
457 $content = $rev->getContent( SlotRecord::MAIN, RevisionRecord::FOR_THIS_USER,
461 [
'apierror-missingcontent-revid-role', $rev->getId(), SlotRecord::MAIN ],
'missingcontent'
464 $content = $content->getSection( $section );
467 [
"apierror-compare-nosuch{$prefix}section",
wfEscapeWikiText( $section ) ],
468 "nosuch{$prefix}section"
472 $newRev->setContent( SlotRecord::MAIN, $content );
475 return [ $newRev, $rev, $rev ];
481 $title = $this->guessTitle();
484 $newRev = MutableRevisionRecord::newFromParentRevision( $rev );
486 $newRev =
new MutableRevisionRecord( $title ?: Title::newMainPage() );
488 foreach (
$params[
"{$prefix}slots"] as $role ) {
489 $text =
$params[
"{$prefix}text-{$role}"];
490 if ( $text ===
null ) {
492 if ( $role === SlotRecord::MAIN ) {
493 $this->
dieWithError( [
'apierror-compare-maintextrequired', $prefix ] );
498 foreach ( [
'section',
'contentmodel',
'contentformat' ] as $param ) {
499 if ( isset(
$params[
"{$prefix}{$param}-{$role}"] ) ) {
501 'apierror-compare-notext',
508 $newRev->removeSlot( $role );
512 $model =
$params[
"{$prefix}contentmodel-{$role}"];
513 $format =
$params[
"{$prefix}contentformat-{$role}"];
515 if ( !$model && $rev && $rev->hasSlot( $role ) ) {
516 $model = $rev->getSlot( $role, RevisionRecord::RAW )->getModel();
518 if ( !$model && $title && $role === SlotRecord::MAIN ) {
520 $model = $title->getContentModel();
523 $model = $this->guessModel( $role );
527 $this->
addWarning( [
'apiwarn-compare-nocontentmodel', $model ] );
531 $content = $this->contentHandlerFactory
532 ->getContentHandler( $model )
533 ->unserializeContent( $text, $format );
536 'wrap' =>
ApiMessage::create(
'apierror-contentserializationexception',
'parseerror' )
540 if (
$params[
"{$prefix}pst"] ) {
544 $popts = ParserOptions::newFromContext( $this->
getContext() );
545 $content = $this->contentTransformer->preSaveTransform(
549 $this->getUserForPreview(),
554 $section =
$params[
"{$prefix}section-{$role}"];
555 if ( $section !==
null && $section !==
'' ) {
557 $this->
dieWithError(
"apierror-compare-no{$prefix}revision" );
559 $oldContent = $rev->getContent( $role, RevisionRecord::FOR_THIS_USER, $this->
getAuthority() );
560 if ( !$oldContent ) {
562 [
'apierror-missingcontent-revid-role', $rev->getId(),
wfEscapeWikiText( $role ) ],
566 if ( !$oldContent->getContentHandler()->supportsSections() ) {
567 $this->
dieWithError( [
'apierror-sectionsnotsupported', $content->getModel() ] );
571 $content = $oldContent->replaceSection( $section, $content,
'' );
572 }
catch ( TimeoutException $e ) {
574 }
catch ( Exception $ex ) {
579 $this->
dieWithError( [
'apierror-sectionreplacefailed' ] );
584 if ( $role === SlotRecord::MAIN && isset(
$params[
"{$prefix}section"] ) ) {
585 $section =
$params[
"{$prefix}section"];
586 $content = $content->getSection( $section );
589 [
"apierror-compare-nosuch{$prefix}section",
wfEscapeWikiText( $section ) ],
590 "nosuch{$prefix}section"
596 $newRev->setContent( $role, $content );
598 return [ $newRev, $rev, null ];
608 private function setVals( &$vals, $prefix, $rev ) {
610 $title = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
611 if ( isset( $this->props[
'ids'] ) ) {
612 $vals[
"{$prefix}id"] = $title->getArticleID();
613 $vals[
"{$prefix}revid"] = $rev->getId();
615 if ( isset( $this->props[
'title'] ) ) {
618 if ( isset( $this->props[
'size'] ) ) {
619 $vals[
"{$prefix}size"] = $rev->getSize();
621 if ( isset( $this->props[
'timestamp'] ) ) {
622 $revTimestamp = $rev->getTimestamp();
623 if ( $revTimestamp ) {
624 $vals[
"{$prefix}timestamp"] =
wfTimestamp( TS_ISO_8601, $revTimestamp );
629 if ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
630 $vals[
"{$prefix}texthidden"] =
true;
634 if ( $rev->isDeleted( RevisionRecord::DELETED_USER ) ) {
635 $vals[
"{$prefix}userhidden"] =
true;
638 if ( isset( $this->props[
'user'] ) ) {
639 $user = $rev->getUser( RevisionRecord::FOR_THIS_USER, $this->
getAuthority() );
641 $vals[
"{$prefix}user"] = $user->getName();
642 $vals[
"{$prefix}userid"] = $user->getId();
646 if ( $rev->isDeleted( RevisionRecord::DELETED_COMMENT ) ) {
647 $vals[
"{$prefix}commenthidden"] =
true;
650 if ( isset( $this->props[
'comment'] ) || isset( $this->props[
'parsedcomment'] ) ) {
651 $comment = $rev->getComment( RevisionRecord::FOR_THIS_USER, $this->
getAuthority() );
652 if ( $comment !==
null ) {
653 if ( isset( $this->props[
'comment'] ) ) {
654 $vals[
"{$prefix}comment"] = $comment->text;
656 $vals[
"{$prefix}parsedcomment"] = $this->commentFormatter->format(
657 $comment->text, $title
663 $this->
getMain()->setCacheMode(
'private' );
664 if ( $rev->isDeleted( RevisionRecord::DELETED_RESTRICTED ) ) {
665 $vals[
"{$prefix}suppressed"] =
true;
669 if ( $rev instanceof RevisionArchiveRecord ) {
670 $this->
getMain()->setCacheMode(
'private' );
671 $vals[
"{$prefix}archive"] =
true;
676 private function getUserForPreview() {
678 if ( $this->tempUserCreator->shouldAutoCreate( $user,
'edit' ) ) {
679 return $this->userFactory->newUnsavedTempUser(
680 $this->tempUserCreator->getStashedName( $this->getRequest()->getSession() )
687 $slotRoles = $this->slotRoleRegistry->getKnownRoles();
688 sort( $slotRoles, SORT_STRING );
694 ParamValidator::PARAM_TYPE =>
'integer'
697 ParamValidator::PARAM_TYPE =>
'integer'
701 ParamValidator::PARAM_TYPE => $slotRoles,
702 ParamValidator::PARAM_ISMULTI =>
true,
706 ParamValidator::PARAM_TYPE =>
'text',
708 'section-{slot}' => [
710 ParamValidator::PARAM_TYPE =>
'string',
712 'contentformat-{slot}' => [
714 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
716 'contentmodel-{slot}' => [
718 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getContentModels(),
723 ParamValidator::PARAM_TYPE =>
'text',
724 ParamValidator::PARAM_DEPRECATED =>
true,
727 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
728 ParamValidator::PARAM_DEPRECATED =>
true,
731 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getContentModels(),
732 ParamValidator::PARAM_DEPRECATED =>
true,
735 ParamValidator::PARAM_DEFAULT =>
null,
736 ParamValidator::PARAM_DEPRECATED =>
true,
741 foreach ( $fromToParams as $k => $v ) {
747 foreach ( $fromToParams as $k => $v ) {
756 [
'torelative' => [ ParamValidator::PARAM_TYPE => [
'prev',
'next',
'cur' ], ] ],
761 ParamValidator::PARAM_DEFAULT =>
'diff|ids|title',
762 ParamValidator::PARAM_TYPE => [
774 ParamValidator::PARAM_ISMULTI =>
true,
779 ParamValidator::PARAM_TYPE => $slotRoles,
780 ParamValidator::PARAM_ISMULTI =>
true,
781 ParamValidator::PARAM_ALL =>
true,
785 ParamValidator::PARAM_TYPE => $this->differenceEngine->getSupportedFormats(),
786 ParamValidator::PARAM_DEFAULT =>
'table',
794 'action=compare&fromrev=1&torev=2'
795 =>
'apihelp-compare-example-1',
800 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Compare';