42 private $guessedTitle =
false;
65 parent::__construct( $mainModule, $moduleName );
66 $this->revisionStore = $revisionStore;
67 $this->archivedRevisionLookup = $archivedRevisionLookup;
68 $this->slotRoleRegistry = $slotRoleRegistry;
69 $this->contentHandlerFactory = $contentHandlerFactory;
70 $this->contentTransformer = $contentTransformer;
71 $this->commentFormatter = $commentFormatter;
72 $this->tempUserCreator = $tempUserCreator;
73 $this->userFactory = $userFactory;
82 $params,
'fromtitle',
'fromid',
'fromrev',
'fromtext',
'fromslots'
85 $params,
'totitle',
'toid',
'torev',
'totext',
'torelative',
'toslots'
88 $this->props = array_fill_keys( $params[
'prop'],
true );
91 $this->
getMain()->setCacheMode(
'public' );
94 [ $fromRev, $fromRelRev, $fromValsRev ] = $this->getDiffRevision(
'from', $params );
97 if ( $params[
'torelative'] !==
null ) {
99 $this->
dieWithError(
'apierror-compare-relative-to-nothing' );
104 $this->
dieWithError( [
'apierror-compare-relative-to-deleted', $params[
'torelative'] ] );
106 switch ( $params[
'torelative'] ) {
109 [ $toRev, $toRelRev, $toValsRev ] = [ $fromRev, $fromRelRev, $fromValsRev ];
110 $fromRev = $this->revisionStore->getPreviousRevision( $toRelRev );
111 $fromRelRev = $fromRev;
112 $fromValsRev = $fromRev;
114 $title = Title::newFromPageIdentity( $toRelRev->getPage() );
116 'apiwarn-compare-no-prev',
124 $fromRev->setContent(
126 $toRelRev->getMainContentRaw()
127 ->getContentHandler()
134 $toRev = $this->revisionStore->getNextRevision( $fromRelRev );
138 $title = Title::newFromPageIdentity( $fromRelRev->getPage() );
140 'apiwarn-compare-no-next',
147 $toRev = MutableRevisionRecord::newFromParentRevision( $fromRelRev );
152 $title = $fromRelRev->getPage();
153 $toRev = $this->revisionStore->getRevisionByTitle( $title );
155 $title = Title::newFromPageIdentity( $title );
157 [
'apierror-missingrev-title',
wfEscapeWikiText( $title->getPrefixedText() ) ],
168 [ $toRev, $toRelRev, $toValsRev ] = $this->getDiffRevision(
'to', $params );
173 if ( !$fromRev || !$toRev ) {
179 if ( !$fromRev->userCan( RevisionRecord::DELETED_TEXT, $this->getAuthority() ) ) {
180 $this->
dieWithError( [
'apierror-missingcontent-revid', $fromRev->getId() ],
'missingcontent' );
182 if ( !$toRev->userCan( RevisionRecord::DELETED_TEXT, $this->getAuthority() ) ) {
183 $this->
dieWithError( [
'apierror-missingcontent-revid', $toRev->getId() ],
'missingcontent' );
189 $context->setTitle( Title::newFromPageIdentity( $fromRelRev->getPage() ) );
190 } elseif ( $toRelRev ) {
191 $context->setTitle( Title::newFromPageIdentity( $toRelRev->getPage() ) );
193 $guessedTitle = $this->guessTitle();
194 if ( $guessedTitle ) {
195 $context->setTitle( $guessedTitle );
198 $this->differenceEngine->setContext( $context );
199 $this->differenceEngine->setSlotDiffOptions( [
'diff-type' => $params[
'difftype'] ] );
200 $this->differenceEngine->setRevisions( $fromRev, $toRev );
201 if ( $params[
'slots'] ===
null ) {
202 $difftext = $this->differenceEngine->getDiffBody();
203 if ( $difftext ===
false ) {
208 foreach ( $params[
'slots'] as $role ) {
209 $difftext[$role] = $this->differenceEngine->getDiffBodyForRole( $role );
212 foreach ( $this->differenceEngine->getRevisionLoadErrors() as $msg ) {
218 $this->setVals( $vals,
'from', $fromValsRev );
219 $this->setVals( $vals,
'to', $toValsRev );
221 if ( isset( $this->props[
'rel'] ) ) {
223 $rev = $this->revisionStore->getPreviousRevision( $fromRev );
225 $vals[
'prev'] = $rev->getId();
229 $rev = $this->revisionStore->getNextRevision( $toRev );
231 $vals[
'next'] = $rev->getId();
236 if ( isset( $this->props[
'diffsize'] ) ) {
237 $vals[
'diffsize'] = 0;
238 foreach ( (array)$difftext as $text ) {
239 $vals[
'diffsize'] += strlen( $text );
242 if ( isset( $this->props[
'diff'] ) ) {
243 if ( is_array( $difftext ) ) {
245 $vals[
'bodies'] = $difftext;
265 private function getRevisionById( $id ) {
266 $rev = $this->revisionStore->getRevisionById( $id );
272 if ( !$rev && $this->
getAuthority()->isAllowedAny(
'deletedtext',
'undelete' ) ) {
274 $rev = $this->archivedRevisionLookup->getArchivedRevisionRecord(
null, $id );
288 private function guessTitle() {
289 if ( $this->guessedTitle !==
false ) {
290 return $this->guessedTitle;
293 $this->guessedTitle =
null;
296 foreach ( [
'from',
'to' ] as $prefix ) {
297 if ( $params[
"{$prefix}rev"] !==
null ) {
298 $rev = $this->getRevisionById( $params[
"{$prefix}rev"] );
300 $this->guessedTitle = Title::newFromPageIdentity( $rev->getPage() );
305 if ( $params[
"{$prefix}title"] !==
null ) {
306 $title = Title::newFromText( $params[
"{$prefix}title"] );
307 if ( $title && !$title->isExternal() ) {
308 $this->guessedTitle = $title;
313 if ( $params[
"{$prefix}id"] !==
null ) {
314 $title = Title::newFromID( $params[
"{$prefix}id"] );
316 $this->guessedTitle = $title;
322 return $this->guessedTitle;
330 private function guessModel( $role ) {
333 foreach ( [
'from',
'to' ] as $prefix ) {
334 if ( $params[
"{$prefix}rev"] !==
null ) {
335 $rev = $this->getRevisionById( $params[
"{$prefix}rev"] );
336 if ( $rev && $rev->hasSlot( $role ) ) {
337 return $rev->getSlot( $role, RevisionRecord::RAW )->getModel();
342 $guessedTitle = $this->guessTitle();
343 if ( $guessedTitle ) {
344 return $this->slotRoleRegistry->getRoleHandler( $role )->getDefaultModel( $guessedTitle );
347 if ( isset( $params[
"fromcontentmodel-$role"] ) ) {
348 return $params[
"fromcontentmodel-$role"];
350 if ( isset( $params[
"tocontentmodel-$role"] ) ) {
351 return $params[
"tocontentmodel-$role"];
354 if ( $role === SlotRecord::MAIN ) {
355 if ( isset( $params[
'fromcontentmodel'] ) ) {
356 return $params[
'fromcontentmodel'];
358 if ( isset( $params[
'tocontentmodel'] ) ) {
359 return $params[
'tocontentmodel'];
381 private function getDiffRevision( $prefix, array $params ) {
385 if ( $params[
"{$prefix}text"] !==
null ) {
386 $params[
"{$prefix}slots"] = [ SlotRecord::MAIN ];
388 $params[
"{$prefix}section-main"] =
null;
389 $params[
"{$prefix}contentmodel-main"] =
$params[
"{$prefix}contentmodel"];
390 $params[
"{$prefix}contentformat-main"] =
$params[
"{$prefix}contentformat"];
395 $suppliedContent =
$params[
"{$prefix}slots"] !==
null;
399 if ( $params[
"{$prefix}rev"] !==
null ) {
400 $revId =
$params[
"{$prefix}rev"];
401 } elseif ( $params[
"{$prefix}title"] !==
null || $params[
"{$prefix}id"] !==
null ) {
402 if ( $params[
"{$prefix}title"] !==
null ) {
403 $title = Title::newFromText( $params[
"{$prefix}title"] );
404 if ( !$title || $title->isExternal() ) {
410 $title = Title::newFromID( $params[
"{$prefix}id"] );
412 $this->
dieWithError( [
'apierror-nosuchpageid', $params[
"{$prefix}id"] ] );
415 $revId = $title->getLatestRevID();
419 if ( !$suppliedContent ) {
420 if ( $title->exists() ) {
422 [
'apierror-missingrev-title',
wfEscapeWikiText( $title->getPrefixedText() ) ],
427 [
'apierror-missingtitle-byname',
wfEscapeWikiText( $title->getPrefixedText() ) ],
434 if ( $revId !==
null ) {
435 $rev = $this->getRevisionById( $revId );
437 $this->
dieWithError( [
'apierror-nosuchrevid', $revId ] );
439 $title = Title::newFromPageIdentity( $rev->getPage() );
443 if ( !$suppliedContent ) {
447 if ( isset( $params[
"{$prefix}section"] ) ) {
448 $section =
$params[
"{$prefix}section"];
450 $newRev = MutableRevisionRecord::newFromParentRevision( $rev );
451 $content = $rev->getContent( SlotRecord::MAIN, RevisionRecord::FOR_THIS_USER,
455 [
'apierror-missingcontent-revid-role', $rev->getId(), SlotRecord::MAIN ],
'missingcontent'
458 $content = $content->getSection( $section );
461 [
"apierror-compare-nosuch{$prefix}section",
wfEscapeWikiText( $section ) ],
462 "nosuch{$prefix}section"
466 $newRev->setContent( SlotRecord::MAIN, $content );
469 return [ $newRev, $rev, $rev ];
475 $title = $this->guessTitle();
478 $newRev = MutableRevisionRecord::newFromParentRevision( $rev );
480 $newRev =
new MutableRevisionRecord( $title ?: Title::newMainPage() );
482 foreach ( $params[
"{$prefix}slots"] as $role ) {
483 $text =
$params[
"{$prefix}text-{$role}"];
484 if ( $text ===
null ) {
486 if ( $role === SlotRecord::MAIN ) {
487 $this->
dieWithError( [
'apierror-compare-maintextrequired', $prefix ] );
492 foreach ( [
'section',
'contentmodel',
'contentformat' ] as $param ) {
493 if ( isset( $params[
"{$prefix}{$param}-{$role}"] ) ) {
495 'apierror-compare-notext',
502 $newRev->removeSlot( $role );
506 $model =
$params[
"{$prefix}contentmodel-{$role}"];
507 $format =
$params[
"{$prefix}contentformat-{$role}"];
509 if ( !$model && $rev && $rev->hasSlot( $role ) ) {
510 $model = $rev->getSlot( $role, RevisionRecord::RAW )->getModel();
512 if ( !$model && $title && $role === SlotRecord::MAIN ) {
514 $model = $title->getContentModel();
517 $model = $this->guessModel( $role );
521 $this->
addWarning( [
'apiwarn-compare-nocontentmodel', $model ] );
525 $content = $this->contentHandlerFactory
526 ->getContentHandler( $model )
527 ->unserializeContent( $text, $format );
528 }
catch ( MWContentSerializationException $ex ) {
530 'wrap' =>
ApiMessage::create(
'apierror-contentserializationexception',
'parseerror' )
534 if ( $params[
"{$prefix}pst"] ) {
538 $popts = ParserOptions::newFromContext( $this->
getContext() );
539 $content = $this->contentTransformer->preSaveTransform(
543 $this->getUserForPreview(),
548 $section =
$params[
"{$prefix}section-{$role}"];
549 if ( $section !==
null && $section !==
'' ) {
551 $this->
dieWithError(
"apierror-compare-no{$prefix}revision" );
553 $oldContent = $rev->getContent( $role, RevisionRecord::FOR_THIS_USER, $this->
getAuthority() );
554 if ( !$oldContent ) {
556 [
'apierror-missingcontent-revid-role', $rev->getId(),
wfEscapeWikiText( $role ) ],
560 if ( !$oldContent->getContentHandler()->supportsSections() ) {
561 $this->
dieWithError( [
'apierror-sectionsnotsupported', $content->getModel() ] );
565 $content = $oldContent->replaceSection( $section, $content,
'' );
566 }
catch ( TimeoutException $e ) {
568 }
catch ( Exception ) {
573 $this->
dieWithError( [
'apierror-sectionreplacefailed' ] );
578 if ( $role === SlotRecord::MAIN && isset( $params[
"{$prefix}section"] ) ) {
579 $section =
$params[
"{$prefix}section"];
580 $content = $content->getSection( $section );
583 [
"apierror-compare-nosuch{$prefix}section",
wfEscapeWikiText( $section ) ],
584 "nosuch{$prefix}section"
590 $newRev->setContent( $role, $content );
592 return [ $newRev, $rev, null ];
602 private function setVals( &$vals, $prefix, $rev ) {
604 $title = Title::newFromPageIdentity( $rev->getPage() );
605 if ( isset( $this->props[
'ids'] ) ) {
606 $vals[
"{$prefix}id"] = $title->getArticleID();
607 $vals[
"{$prefix}revid"] = $rev->getId();
609 if ( isset( $this->props[
'title'] ) ) {
612 if ( isset( $this->props[
'size'] ) ) {
613 $vals[
"{$prefix}size"] = $rev->getSize();
615 if ( isset( $this->props[
'timestamp'] ) ) {
616 $revTimestamp = $rev->getTimestamp();
617 if ( $revTimestamp ) {
618 $vals[
"{$prefix}timestamp"] =
wfTimestamp( TS::ISO_8601, $revTimestamp );
623 if ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
624 $vals[
"{$prefix}texthidden"] =
true;
628 if ( $rev->isDeleted( RevisionRecord::DELETED_USER ) ) {
629 $vals[
"{$prefix}userhidden"] =
true;
632 if ( isset( $this->props[
'user'] ) ) {
633 $user = $rev->getUser( RevisionRecord::FOR_THIS_USER, $this->
getAuthority() );
635 $vals[
"{$prefix}user"] = $user->getName();
636 $vals[
"{$prefix}userid"] = $user->getId();
640 if ( $rev->isDeleted( RevisionRecord::DELETED_COMMENT ) ) {
641 $vals[
"{$prefix}commenthidden"] =
true;
644 if ( isset( $this->props[
'comment'] ) || isset( $this->props[
'parsedcomment'] ) ) {
645 $comment = $rev->getComment( RevisionRecord::FOR_THIS_USER, $this->
getAuthority() );
646 if ( $comment !==
null ) {
647 if ( isset( $this->props[
'comment'] ) ) {
648 $vals[
"{$prefix}comment"] = $comment->text;
650 $vals[
"{$prefix}parsedcomment"] = $this->commentFormatter->format(
651 $comment->text, $title
657 $this->
getMain()->setCacheMode(
'private' );
658 if ( $rev->isDeleted( RevisionRecord::DELETED_RESTRICTED ) ) {
659 $vals[
"{$prefix}suppressed"] =
true;
663 if ( $rev instanceof RevisionArchiveRecord ) {
664 $this->
getMain()->setCacheMode(
'private' );
665 $vals[
"{$prefix}archive"] =
true;
670 private function getUserForPreview(): UserIdentity {
672 if ( $this->tempUserCreator->shouldAutoCreate( $user,
'edit' ) ) {
673 return $this->userFactory->newUnsavedTempUser(
674 $this->tempUserCreator->getStashedName( $this->getRequest()->getSession() )
682 $slotRoles = $this->slotRoleRegistry->getKnownRoles();
683 sort( $slotRoles, SORT_STRING );
689 ParamValidator::PARAM_TYPE =>
'integer'
692 ParamValidator::PARAM_TYPE =>
'integer'
696 ParamValidator::PARAM_TYPE => $slotRoles,
697 ParamValidator::PARAM_ISMULTI =>
true,
701 ParamValidator::PARAM_TYPE =>
'text',
703 'section-{slot}' => [
705 ParamValidator::PARAM_TYPE =>
'string',
707 'contentformat-{slot}' => [
709 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
711 'contentmodel-{slot}' => [
713 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getContentModels(),
718 ParamValidator::PARAM_TYPE =>
'text',
719 ParamValidator::PARAM_DEPRECATED =>
true,
722 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
723 ParamValidator::PARAM_DEPRECATED =>
true,
726 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getContentModels(),
727 ParamValidator::PARAM_DEPRECATED =>
true,
730 ParamValidator::PARAM_DEFAULT =>
null,
731 ParamValidator::PARAM_DEPRECATED =>
true,
736 foreach ( $fromToParams as $k => $v ) {
742 foreach ( $fromToParams as $k => $v ) {
751 [
'torelative' => [ ParamValidator::PARAM_TYPE => [
'prev',
'next',
'cur' ], ] ],
756 ParamValidator::PARAM_DEFAULT =>
'diff|ids|title',
757 ParamValidator::PARAM_TYPE => [
769 ParamValidator::PARAM_ISMULTI =>
true,
774 ParamValidator::PARAM_TYPE => $slotRoles,
775 ParamValidator::PARAM_ISMULTI =>
true,
776 ParamValidator::PARAM_ALL =>
true,
780 ParamValidator::PARAM_TYPE => $this->differenceEngine->getSupportedFormats(),
781 ParamValidator::PARAM_DEFAULT =>
'table',
790 'action=compare&fromrev=1&torev=2'
791 =>
'apihelp-compare-example-1',
797 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Compare';