55 private $guessedTitle =
false;
78 parent::__construct( $mainModule, $moduleName );
79 $this->revisionStore = $revisionStore;
80 $this->archivedRevisionLookup = $archivedRevisionLookup;
81 $this->slotRoleRegistry = $slotRoleRegistry;
82 $this->contentHandlerFactory = $contentHandlerFactory;
83 $this->contentTransformer = $contentTransformer;
84 $this->commentFormatter = $commentFormatter;
85 $this->tempUserCreator = $tempUserCreator;
86 $this->userFactory = $userFactory;
95 $params,
'fromtitle',
'fromid',
'fromrev',
'fromtext',
'fromslots'
98 $params,
'totitle',
'toid',
'torev',
'totext',
'torelative',
'toslots'
101 $this->props = array_fill_keys( $params[
'prop'],
true );
104 $this->
getMain()->setCacheMode(
'public' );
107 [ $fromRev, $fromRelRev, $fromValsRev ] = $this->getDiffRevision(
'from', $params );
110 if ( $params[
'torelative'] !==
null ) {
111 if ( !$fromRelRev ) {
112 $this->
dieWithError(
'apierror-compare-relative-to-nothing' );
117 $this->
dieWithError( [
'apierror-compare-relative-to-deleted', $params[
'torelative'] ] );
119 switch ( $params[
'torelative'] ) {
122 [ $toRev, $toRelRev, $toValsRev ] = [ $fromRev, $fromRelRev, $fromValsRev ];
123 $fromRev = $this->revisionStore->getPreviousRevision( $toRelRev );
124 $fromRelRev = $fromRev;
125 $fromValsRev = $fromRev;
127 $title = Title::newFromPageIdentity( $toRelRev->getPage() );
129 'apiwarn-compare-no-prev',
137 $fromRev->setContent(
139 $toRelRev->getMainContentRaw()
140 ->getContentHandler()
147 $toRev = $this->revisionStore->getNextRevision( $fromRelRev );
151 $title = Title::newFromPageIdentity( $fromRelRev->getPage() );
153 'apiwarn-compare-no-next',
160 $toRev = MutableRevisionRecord::newFromParentRevision( $fromRelRev );
165 $title = $fromRelRev->getPage();
166 $toRev = $this->revisionStore->getRevisionByTitle( $title );
168 $title = Title::newFromPageIdentity( $title );
170 [
'apierror-missingrev-title',
wfEscapeWikiText( $title->getPrefixedText() ) ],
179 [ $toRev, $toRelRev, $toValsRev ] = $this->getDiffRevision(
'to', $params );
185 if ( !$fromRev || !$toRev ) {
191 if ( !$fromRev->userCan( RevisionRecord::DELETED_TEXT, $this->getAuthority() ) ) {
192 $this->
dieWithError( [
'apierror-missingcontent-revid', $fromRev->getId() ],
'missingcontent' );
194 if ( !$toRev->userCan( RevisionRecord::DELETED_TEXT, $this->getAuthority() ) ) {
195 $this->
dieWithError( [
'apierror-missingcontent-revid', $toRev->getId() ],
'missingcontent' );
201 $context->setTitle( Title::newFromPageIdentity( $fromRelRev->getPage() ) );
203 } elseif ( $toRelRev ) {
204 $context->setTitle( Title::newFromPageIdentity( $toRelRev->getPage() ) );
206 $guessedTitle = $this->guessTitle();
207 if ( $guessedTitle ) {
208 $context->setTitle( $guessedTitle );
211 $this->differenceEngine->setContext( $context );
212 $this->differenceEngine->setSlotDiffOptions( [
'diff-type' => $params[
'difftype'] ] );
213 $this->differenceEngine->setRevisions( $fromRev, $toRev );
214 if ( $params[
'slots'] ===
null ) {
215 $difftext = $this->differenceEngine->getDiffBody();
216 if ( $difftext ===
false ) {
221 foreach ( $params[
'slots'] as $role ) {
222 $difftext[$role] = $this->differenceEngine->getDiffBodyForRole( $role );
225 foreach ( $this->differenceEngine->getRevisionLoadErrors() as $msg ) {
231 $this->setVals( $vals,
'from', $fromValsRev );
233 $this->setVals( $vals,
'to', $toValsRev );
235 if ( isset( $this->props[
'rel'] ) ) {
237 $rev = $this->revisionStore->getPreviousRevision( $fromRev );
239 $vals[
'prev'] = $rev->getId();
243 $rev = $this->revisionStore->getNextRevision( $toRev );
245 $vals[
'next'] = $rev->getId();
250 if ( isset( $this->props[
'diffsize'] ) ) {
251 $vals[
'diffsize'] = 0;
252 foreach ( (array)$difftext as $text ) {
253 $vals[
'diffsize'] += strlen( $text );
256 if ( isset( $this->props[
'diff'] ) ) {
257 if ( is_array( $difftext ) ) {
259 $vals[
'bodies'] = $difftext;
279 private function getRevisionById( $id ) {
280 $rev = $this->revisionStore->getRevisionById( $id );
286 if ( !$rev && $this->
getAuthority()->isAllowedAny(
'deletedtext',
'undelete' ) ) {
288 $rev = $this->archivedRevisionLookup->getArchivedRevisionRecord(
null, $id );
302 private function guessTitle() {
303 if ( $this->guessedTitle !==
false ) {
304 return $this->guessedTitle;
307 $this->guessedTitle =
null;
310 foreach ( [
'from',
'to' ] as $prefix ) {
311 if ( $params[
"{$prefix}rev"] !==
null ) {
312 $rev = $this->getRevisionById( $params[
"{$prefix}rev"] );
314 $this->guessedTitle = Title::newFromPageIdentity( $rev->getPage() );
319 if ( $params[
"{$prefix}title"] !==
null ) {
320 $title = Title::newFromText( $params[
"{$prefix}title"] );
321 if ( $title && !$title->isExternal() ) {
322 $this->guessedTitle = $title;
327 if ( $params[
"{$prefix}id"] !==
null ) {
328 $title = Title::newFromID( $params[
"{$prefix}id"] );
330 $this->guessedTitle = $title;
336 return $this->guessedTitle;
344 private function guessModel( $role ) {
347 foreach ( [
'from',
'to' ] as $prefix ) {
348 if ( $params[
"{$prefix}rev"] !==
null ) {
349 $rev = $this->getRevisionById( $params[
"{$prefix}rev"] );
350 if ( $rev && $rev->hasSlot( $role ) ) {
351 return $rev->getSlot( $role, RevisionRecord::RAW )->getModel();
356 $guessedTitle = $this->guessTitle();
357 if ( $guessedTitle ) {
358 return $this->slotRoleRegistry->getRoleHandler( $role )->getDefaultModel( $guessedTitle );
361 if ( isset( $params[
"fromcontentmodel-$role"] ) ) {
362 return $params[
"fromcontentmodel-$role"];
364 if ( isset( $params[
"tocontentmodel-$role"] ) ) {
365 return $params[
"tocontentmodel-$role"];
368 if ( $role === SlotRecord::MAIN ) {
369 if ( isset( $params[
'fromcontentmodel'] ) ) {
370 return $params[
'fromcontentmodel'];
372 if ( isset( $params[
'tocontentmodel'] ) ) {
373 return $params[
'tocontentmodel'];
395 private function getDiffRevision( $prefix, array $params ) {
399 if ( $params[
"{$prefix}text"] !==
null ) {
400 $params[
"{$prefix}slots"] = [ SlotRecord::MAIN ];
402 $params[
"{$prefix}section-main"] =
null;
403 $params[
"{$prefix}contentmodel-main"] =
$params[
"{$prefix}contentmodel"];
404 $params[
"{$prefix}contentformat-main"] =
$params[
"{$prefix}contentformat"];
409 $suppliedContent =
$params[
"{$prefix}slots"] !==
null;
413 if ( $params[
"{$prefix}rev"] !==
null ) {
414 $revId =
$params[
"{$prefix}rev"];
415 } elseif ( $params[
"{$prefix}title"] !==
null || $params[
"{$prefix}id"] !==
null ) {
416 if ( $params[
"{$prefix}title"] !==
null ) {
417 $title = Title::newFromText( $params[
"{$prefix}title"] );
418 if ( !$title || $title->isExternal() ) {
424 $title = Title::newFromID( $params[
"{$prefix}id"] );
426 $this->
dieWithError( [
'apierror-nosuchpageid', $params[
"{$prefix}id"] ] );
429 $revId = $title->getLatestRevID();
433 if ( !$suppliedContent ) {
434 if ( $title->exists() ) {
436 [
'apierror-missingrev-title',
wfEscapeWikiText( $title->getPrefixedText() ) ],
441 [
'apierror-missingtitle-byname',
wfEscapeWikiText( $title->getPrefixedText() ) ],
448 if ( $revId !==
null ) {
449 $rev = $this->getRevisionById( $revId );
451 $this->
dieWithError( [
'apierror-nosuchrevid', $revId ] );
453 $title = Title::newFromPageIdentity( $rev->getPage() );
457 if ( !$suppliedContent ) {
461 if ( isset( $params[
"{$prefix}section"] ) ) {
462 $section =
$params[
"{$prefix}section"];
464 $newRev = MutableRevisionRecord::newFromParentRevision( $rev );
465 $content = $rev->getContent( SlotRecord::MAIN, RevisionRecord::FOR_THIS_USER,
469 [
'apierror-missingcontent-revid-role', $rev->getId(), SlotRecord::MAIN ],
'missingcontent'
472 $content = $content->getSection( $section );
475 [
"apierror-compare-nosuch{$prefix}section",
wfEscapeWikiText( $section ) ],
476 "nosuch{$prefix}section"
480 $newRev->setContent( SlotRecord::MAIN, $content );
483 return [ $newRev, $rev, $rev ];
489 $title = $this->guessTitle();
492 $newRev = MutableRevisionRecord::newFromParentRevision( $rev );
494 $newRev =
new MutableRevisionRecord( $title ?: Title::newMainPage() );
496 foreach ( $params[
"{$prefix}slots"] as $role ) {
497 $text =
$params[
"{$prefix}text-{$role}"];
498 if ( $text ===
null ) {
500 if ( $role === SlotRecord::MAIN ) {
501 $this->
dieWithError( [
'apierror-compare-maintextrequired', $prefix ] );
506 foreach ( [
'section',
'contentmodel',
'contentformat' ] as $param ) {
507 if ( isset( $params[
"{$prefix}{$param}-{$role}"] ) ) {
509 'apierror-compare-notext',
516 $newRev->removeSlot( $role );
520 $model =
$params[
"{$prefix}contentmodel-{$role}"];
521 $format =
$params[
"{$prefix}contentformat-{$role}"];
523 if ( !$model && $rev && $rev->hasSlot( $role ) ) {
524 $model = $rev->getSlot( $role, RevisionRecord::RAW )->getModel();
526 if ( !$model && $title && $role === SlotRecord::MAIN ) {
528 $model = $title->getContentModel();
531 $model = $this->guessModel( $role );
535 $this->
addWarning( [
'apiwarn-compare-nocontentmodel', $model ] );
539 $content = $this->contentHandlerFactory
540 ->getContentHandler( $model )
541 ->unserializeContent( $text, $format );
542 }
catch ( MWContentSerializationException $ex ) {
544 'wrap' =>
ApiMessage::create(
'apierror-contentserializationexception',
'parseerror' )
548 if ( $params[
"{$prefix}pst"] ) {
552 $popts = ParserOptions::newFromContext( $this->
getContext() );
553 $content = $this->contentTransformer->preSaveTransform(
557 $this->getUserForPreview(),
562 $section =
$params[
"{$prefix}section-{$role}"];
563 if ( $section !==
null && $section !==
'' ) {
565 $this->
dieWithError(
"apierror-compare-no{$prefix}revision" );
567 $oldContent = $rev->getContent( $role, RevisionRecord::FOR_THIS_USER, $this->
getAuthority() );
568 if ( !$oldContent ) {
570 [
'apierror-missingcontent-revid-role', $rev->getId(),
wfEscapeWikiText( $role ) ],
574 if ( !$oldContent->getContentHandler()->supportsSections() ) {
575 $this->
dieWithError( [
'apierror-sectionsnotsupported', $content->getModel() ] );
579 $content = $oldContent->replaceSection( $section, $content,
'' );
580 }
catch ( TimeoutException $e ) {
582 }
catch ( Exception ) {
587 $this->
dieWithError( [
'apierror-sectionreplacefailed' ] );
592 if ( $role === SlotRecord::MAIN && isset( $params[
"{$prefix}section"] ) ) {
593 $section =
$params[
"{$prefix}section"];
594 $content = $content->getSection( $section );
597 [
"apierror-compare-nosuch{$prefix}section",
wfEscapeWikiText( $section ) ],
598 "nosuch{$prefix}section"
604 $newRev->setContent( $role, $content );
606 return [ $newRev, $rev, null ];
616 private function setVals( &$vals, $prefix, $rev ) {
618 $title = Title::newFromPageIdentity( $rev->getPage() );
619 if ( isset( $this->props[
'ids'] ) ) {
620 $vals[
"{$prefix}id"] = $title->getArticleID();
621 $vals[
"{$prefix}revid"] = $rev->getId();
623 if ( isset( $this->props[
'title'] ) ) {
626 if ( isset( $this->props[
'size'] ) ) {
627 $vals[
"{$prefix}size"] = $rev->getSize();
629 if ( isset( $this->props[
'timestamp'] ) ) {
630 $revTimestamp = $rev->getTimestamp();
631 if ( $revTimestamp ) {
632 $vals[
"{$prefix}timestamp"] =
wfTimestamp( TS_ISO_8601, $revTimestamp );
637 if ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
638 $vals[
"{$prefix}texthidden"] =
true;
642 if ( $rev->isDeleted( RevisionRecord::DELETED_USER ) ) {
643 $vals[
"{$prefix}userhidden"] =
true;
646 if ( isset( $this->props[
'user'] ) ) {
647 $user = $rev->getUser( RevisionRecord::FOR_THIS_USER, $this->
getAuthority() );
649 $vals[
"{$prefix}user"] = $user->getName();
650 $vals[
"{$prefix}userid"] = $user->getId();
654 if ( $rev->isDeleted( RevisionRecord::DELETED_COMMENT ) ) {
655 $vals[
"{$prefix}commenthidden"] =
true;
658 if ( isset( $this->props[
'comment'] ) || isset( $this->props[
'parsedcomment'] ) ) {
659 $comment = $rev->getComment( RevisionRecord::FOR_THIS_USER, $this->
getAuthority() );
660 if ( $comment !==
null ) {
661 if ( isset( $this->props[
'comment'] ) ) {
662 $vals[
"{$prefix}comment"] = $comment->text;
664 $vals[
"{$prefix}parsedcomment"] = $this->commentFormatter->format(
665 $comment->text, $title
671 $this->
getMain()->setCacheMode(
'private' );
672 if ( $rev->isDeleted( RevisionRecord::DELETED_RESTRICTED ) ) {
673 $vals[
"{$prefix}suppressed"] =
true;
677 if ( $rev instanceof RevisionArchiveRecord ) {
678 $this->
getMain()->setCacheMode(
'private' );
679 $vals[
"{$prefix}archive"] =
true;
684 private function getUserForPreview(): UserIdentity {
686 if ( $this->tempUserCreator->shouldAutoCreate( $user,
'edit' ) ) {
687 return $this->userFactory->newUnsavedTempUser(
688 $this->tempUserCreator->getStashedName( $this->getRequest()->getSession() )
695 $slotRoles = $this->slotRoleRegistry->getKnownRoles();
696 sort( $slotRoles, SORT_STRING );
702 ParamValidator::PARAM_TYPE =>
'integer'
705 ParamValidator::PARAM_TYPE =>
'integer'
709 ParamValidator::PARAM_TYPE => $slotRoles,
710 ParamValidator::PARAM_ISMULTI =>
true,
714 ParamValidator::PARAM_TYPE =>
'text',
716 'section-{slot}' => [
718 ParamValidator::PARAM_TYPE =>
'string',
720 'contentformat-{slot}' => [
722 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
724 'contentmodel-{slot}' => [
726 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getContentModels(),
731 ParamValidator::PARAM_TYPE =>
'text',
732 ParamValidator::PARAM_DEPRECATED =>
true,
735 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
736 ParamValidator::PARAM_DEPRECATED =>
true,
739 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getContentModels(),
740 ParamValidator::PARAM_DEPRECATED =>
true,
743 ParamValidator::PARAM_DEFAULT =>
null,
744 ParamValidator::PARAM_DEPRECATED =>
true,
749 foreach ( $fromToParams as $k => $v ) {
755 foreach ( $fromToParams as $k => $v ) {
764 [
'torelative' => [ ParamValidator::PARAM_TYPE => [
'prev',
'next',
'cur' ], ] ],
769 ParamValidator::PARAM_DEFAULT =>
'diff|ids|title',
770 ParamValidator::PARAM_TYPE => [
782 ParamValidator::PARAM_ISMULTI =>
true,
787 ParamValidator::PARAM_TYPE => $slotRoles,
788 ParamValidator::PARAM_ISMULTI =>
true,
789 ParamValidator::PARAM_ALL =>
true,
793 ParamValidator::PARAM_TYPE => $this->differenceEngine->getSupportedFormats(),
794 ParamValidator::PARAM_DEFAULT =>
'table',
802 'action=compare&fromrev=1&torev=2'
803 =>
'apihelp-compare-example-1',
808 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Compare';