39 private $guessedTitle =
false;
57 parent::__construct( $mainModule, $moduleName );
66 $params,
'fromtitle',
'fromid',
'fromrev',
'fromtext',
'fromslots'
69 $params,
'totitle',
'toid',
'torev',
'totext',
'torelative',
'toslots'
72 $this->props = array_fill_keys( $params[
'prop'],
true );
75 $this->
getMain()->setCacheMode(
'public' );
78 [ $fromRev, $fromRelRev, $fromValsRev ] = $this->getDiffRevision(
'from', $params );
81 if ( $params[
'torelative'] !==
null ) {
83 $this->
dieWithError(
'apierror-compare-relative-to-nothing' );
88 $this->
dieWithError( [
'apierror-compare-relative-to-deleted', $params[
'torelative'] ] );
90 switch ( $params[
'torelative'] ) {
93 [ $toRev, $toRelRev, $toValsRev ] = [ $fromRev, $fromRelRev, $fromValsRev ];
94 $fromRev = $this->revisionStore->getPreviousRevision( $toRelRev );
95 $fromRelRev = $fromRev;
96 $fromValsRev = $fromRev;
98 $title = Title::newFromPageIdentity( $toRelRev->getPage() );
100 'apiwarn-compare-no-prev',
108 $fromRev->setContent(
110 $toRelRev->getMainContentRaw()
111 ->getContentHandler()
118 $toRev = $this->revisionStore->getNextRevision( $fromRelRev );
122 $title = Title::newFromPageIdentity( $fromRelRev->getPage() );
124 'apiwarn-compare-no-next',
131 $toRev = MutableRevisionRecord::newFromParentRevision( $fromRelRev );
136 $title = $fromRelRev->getPage();
137 $toRev = $this->revisionStore->getRevisionByTitle( $title );
139 $title = Title::newFromPageIdentity( $title );
141 [
'apierror-missingrev-title',
wfEscapeWikiText( $title->getPrefixedText() ) ],
152 [ $toRev, $toRelRev, $toValsRev ] = $this->getDiffRevision(
'to', $params );
157 if ( !$fromRev || !$toRev ) {
163 if ( !$fromRev->userCan( RevisionRecord::DELETED_TEXT, $this->getAuthority() ) ) {
164 $this->
dieWithError( [
'apierror-missingcontent-revid', $fromRev->getId() ],
'missingcontent' );
166 if ( !$toRev->userCan( RevisionRecord::DELETED_TEXT, $this->getAuthority() ) ) {
167 $this->
dieWithError( [
'apierror-missingcontent-revid', $toRev->getId() ],
'missingcontent' );
173 $context->setTitle( Title::newFromPageIdentity( $fromRelRev->getPage() ) );
174 } elseif ( $toRelRev ) {
175 $context->setTitle( Title::newFromPageIdentity( $toRelRev->getPage() ) );
177 $guessedTitle = $this->guessTitle();
178 if ( $guessedTitle ) {
179 $context->setTitle( $guessedTitle );
182 $this->differenceEngine->setContext( $context );
183 $this->differenceEngine->setSlotDiffOptions( [
'diff-type' => $params[
'difftype'] ] );
184 $this->differenceEngine->setRevisions( $fromRev, $toRev );
185 if ( $params[
'slots'] ===
null ) {
186 $difftext = $this->differenceEngine->getDiffBody();
187 if ( $difftext ===
false ) {
192 foreach ( $params[
'slots'] as $role ) {
193 $difftext[$role] = $this->differenceEngine->getDiffBodyForRole( $role );
196 foreach ( $this->differenceEngine->getRevisionLoadErrors() as $msg ) {
202 $this->setVals( $vals,
'from', $fromValsRev );
203 $this->setVals( $vals,
'to', $toValsRev );
205 if ( isset( $this->props[
'rel'] ) ) {
207 $rev = $this->revisionStore->getPreviousRevision( $fromRev );
209 $vals[
'prev'] = $rev->getId();
213 $rev = $this->revisionStore->getNextRevision( $toRev );
215 $vals[
'next'] = $rev->getId();
220 if ( isset( $this->props[
'diffsize'] ) ) {
221 $vals[
'diffsize'] = 0;
222 foreach ( (array)$difftext as $text ) {
223 $vals[
'diffsize'] += strlen( $text );
226 if ( isset( $this->props[
'diff'] ) ) {
227 if ( is_array( $difftext ) ) {
229 $vals[
'bodies'] = $difftext;
249 private function getRevisionById( $id ) {
250 $rev = $this->revisionStore->getRevisionById( $id );
256 if ( !$rev && $this->
getAuthority()->isAllowedAny(
'deletedtext',
'undelete' ) ) {
258 $rev = $this->archivedRevisionLookup->getArchivedRevisionRecord(
null, $id );
272 private function guessTitle() {
273 if ( $this->guessedTitle !==
false ) {
274 return $this->guessedTitle;
277 $this->guessedTitle =
null;
280 foreach ( [
'from',
'to' ] as $prefix ) {
281 if ( $params[
"{$prefix}rev"] !==
null ) {
282 $rev = $this->getRevisionById( $params[
"{$prefix}rev"] );
284 $this->guessedTitle = Title::newFromPageIdentity( $rev->getPage() );
289 if ( $params[
"{$prefix}title"] !==
null ) {
290 $title = Title::newFromText( $params[
"{$prefix}title"] );
291 if ( $title && !$title->isExternal() ) {
292 $this->guessedTitle = $title;
297 if ( $params[
"{$prefix}id"] !==
null ) {
298 $title = Title::newFromID( $params[
"{$prefix}id"] );
300 $this->guessedTitle = $title;
306 return $this->guessedTitle;
314 private function guessModel( $role ) {
317 foreach ( [
'from',
'to' ] as $prefix ) {
318 if ( $params[
"{$prefix}rev"] !==
null ) {
319 $rev = $this->getRevisionById( $params[
"{$prefix}rev"] );
320 if ( $rev && $rev->hasSlot( $role ) ) {
321 return $rev->getSlot( $role, RevisionRecord::RAW )->getModel();
326 $guessedTitle = $this->guessTitle();
327 if ( $guessedTitle ) {
328 return $this->slotRoleRegistry->getRoleHandler( $role )->getDefaultModel( $guessedTitle );
331 if ( isset( $params[
"fromcontentmodel-$role"] ) ) {
332 return $params[
"fromcontentmodel-$role"];
334 if ( isset( $params[
"tocontentmodel-$role"] ) ) {
335 return $params[
"tocontentmodel-$role"];
338 if ( $role === SlotRecord::MAIN ) {
339 if ( isset( $params[
'fromcontentmodel'] ) ) {
340 return $params[
'fromcontentmodel'];
342 if ( isset( $params[
'tocontentmodel'] ) ) {
343 return $params[
'tocontentmodel'];
365 private function getDiffRevision( $prefix, array $params ) {
369 if ( $params[
"{$prefix}text"] !==
null ) {
370 $params[
"{$prefix}slots"] = [ SlotRecord::MAIN ];
372 $params[
"{$prefix}section-main"] =
null;
373 $params[
"{$prefix}contentmodel-main"] =
$params[
"{$prefix}contentmodel"];
374 $params[
"{$prefix}contentformat-main"] =
$params[
"{$prefix}contentformat"];
379 $suppliedContent =
$params[
"{$prefix}slots"] !==
null;
383 if ( $params[
"{$prefix}rev"] !==
null ) {
384 $revId =
$params[
"{$prefix}rev"];
385 } elseif ( $params[
"{$prefix}title"] !==
null || $params[
"{$prefix}id"] !==
null ) {
386 if ( $params[
"{$prefix}title"] !==
null ) {
387 $title = Title::newFromText( $params[
"{$prefix}title"] );
388 if ( !$title || $title->isExternal() ) {
394 $title = Title::newFromID( $params[
"{$prefix}id"] );
396 $this->
dieWithError( [
'apierror-nosuchpageid', $params[
"{$prefix}id"] ] );
399 $revId = $title->getLatestRevID();
403 if ( !$suppliedContent ) {
404 if ( $title->exists() ) {
406 [
'apierror-missingrev-title',
wfEscapeWikiText( $title->getPrefixedText() ) ],
411 [
'apierror-missingtitle-byname',
wfEscapeWikiText( $title->getPrefixedText() ) ],
418 if ( $revId !==
null ) {
419 $rev = $this->getRevisionById( $revId );
421 $this->
dieWithError( [
'apierror-nosuchrevid', $revId ] );
423 $title = Title::newFromPageIdentity( $rev->getPage() );
427 if ( !$suppliedContent ) {
431 if ( isset( $params[
"{$prefix}section"] ) ) {
432 $section =
$params[
"{$prefix}section"];
433 $newRev = MutableRevisionRecord::newFromParentRevision( $rev );
434 $content = $rev->getContent( SlotRecord::MAIN, RevisionRecord::FOR_THIS_USER,
438 [
'apierror-missingcontent-revid-role', $rev->getId(), SlotRecord::MAIN ],
'missingcontent'
441 $content = $content->getSection( $section );
444 [
"apierror-compare-nosuch{$prefix}section",
wfEscapeWikiText( $section ) ],
445 "nosuch{$prefix}section"
448 $newRev->setContent( SlotRecord::MAIN, $content );
451 return [ $newRev, $rev, $rev ];
457 $title = $this->guessTitle();
460 $newRev = MutableRevisionRecord::newFromParentRevision( $rev );
462 $newRev =
new MutableRevisionRecord( $title ?: Title::newMainPage() );
464 foreach ( $params[
"{$prefix}slots"] as $role ) {
465 $text =
$params[
"{$prefix}text-{$role}"];
466 if ( $text ===
null ) {
468 if ( $role === SlotRecord::MAIN ) {
469 $this->
dieWithError( [
'apierror-compare-maintextrequired', $prefix ] );
474 foreach ( [
'section',
'contentmodel',
'contentformat' ] as $param ) {
475 if ( isset( $params[
"{$prefix}{$param}-{$role}"] ) ) {
477 'apierror-compare-notext',
484 $newRev->removeSlot( $role );
488 $model =
$params[
"{$prefix}contentmodel-{$role}"];
489 $format =
$params[
"{$prefix}contentformat-{$role}"];
491 if ( !$model && $rev && $rev->hasSlot( $role ) ) {
492 $model = $rev->getSlot( $role, RevisionRecord::RAW )->getModel();
494 if ( !$model && $title && $role === SlotRecord::MAIN ) {
496 $model = $title->getContentModel();
499 $model = $this->guessModel( $role );
503 $this->
addWarning( [
'apiwarn-compare-nocontentmodel', $model ] );
507 $content = $this->contentHandlerFactory
508 ->getContentHandler( $model )
509 ->unserializeContent( $text, $format );
510 }
catch ( ContentSerializationException $ex ) {
512 'wrap' =>
ApiMessage::create(
'apierror-contentserializationexception',
'parseerror' )
516 if ( $params[
"{$prefix}pst"] ) {
520 $popts = ParserOptions::newFromContext( $this->
getContext() );
521 $content = $this->contentTransformer->preSaveTransform(
524 $this->getUserForPreview(),
529 $section =
$params[
"{$prefix}section-{$role}"];
530 if ( $section !==
null && $section !==
'' ) {
532 $this->
dieWithError(
"apierror-compare-no{$prefix}revision" );
534 $oldContent = $rev->getContent( $role, RevisionRecord::FOR_THIS_USER, $this->
getAuthority() );
535 if ( !$oldContent ) {
537 [
'apierror-missingcontent-revid-role', $rev->getId(),
wfEscapeWikiText( $role ) ],
541 if ( !$oldContent->getContentHandler()->supportsSections() ) {
542 $this->
dieWithError( [
'apierror-sectionsnotsupported', $content->getModel() ] );
545 $content = $oldContent->replaceSection( $section, $content,
'' );
546 }
catch ( TimeoutException $e ) {
548 }
catch ( Exception ) {
553 $this->
dieWithError( [
'apierror-sectionreplacefailed' ] );
558 if ( $role === SlotRecord::MAIN && isset( $params[
"{$prefix}section"] ) ) {
559 $section =
$params[
"{$prefix}section"];
560 $content = $content->getSection( $section );
563 [
"apierror-compare-nosuch{$prefix}section",
wfEscapeWikiText( $section ) ],
564 "nosuch{$prefix}section"
569 $newRev->setContent( $role, $content );
571 return [ $newRev, $rev, null ];
581 private function setVals( &$vals, $prefix, $rev ) {
583 $title = Title::newFromPageIdentity( $rev->getPage() );
584 if ( isset( $this->props[
'ids'] ) ) {
585 $vals[
"{$prefix}id"] = $title->getArticleID();
586 $vals[
"{$prefix}revid"] = $rev->getId();
588 if ( isset( $this->props[
'title'] ) ) {
591 if ( isset( $this->props[
'size'] ) ) {
592 $vals[
"{$prefix}size"] = $rev->getSize();
594 if ( isset( $this->props[
'timestamp'] ) ) {
595 $revTimestamp = $rev->getTimestamp();
596 if ( $revTimestamp ) {
597 $vals[
"{$prefix}timestamp"] =
wfTimestamp( TS::ISO_8601, $revTimestamp );
602 if ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
603 $vals[
"{$prefix}texthidden"] =
true;
607 if ( $rev->isDeleted( RevisionRecord::DELETED_USER ) ) {
608 $vals[
"{$prefix}userhidden"] =
true;
611 if ( isset( $this->props[
'user'] ) ) {
612 $user = $rev->getUser( RevisionRecord::FOR_THIS_USER, $this->
getAuthority() );
614 $vals[
"{$prefix}user"] = $user->getName();
615 $vals[
"{$prefix}userid"] = $user->getId();
619 if ( $rev->isDeleted( RevisionRecord::DELETED_COMMENT ) ) {
620 $vals[
"{$prefix}commenthidden"] =
true;
623 if ( isset( $this->props[
'comment'] ) || isset( $this->props[
'parsedcomment'] ) ) {
624 $comment = $rev->getComment( RevisionRecord::FOR_THIS_USER, $this->
getAuthority() );
625 if ( $comment !==
null ) {
626 if ( isset( $this->props[
'comment'] ) ) {
627 $vals[
"{$prefix}comment"] = $comment->text;
629 $vals[
"{$prefix}parsedcomment"] = $this->commentFormatter->format(
630 $comment->text, $title
636 $this->
getMain()->setCacheMode(
'private' );
637 if ( $rev->isDeleted( RevisionRecord::DELETED_RESTRICTED ) ) {
638 $vals[
"{$prefix}suppressed"] =
true;
642 if ( $rev instanceof RevisionArchiveRecord ) {
643 $this->
getMain()->setCacheMode(
'private' );
644 $vals[
"{$prefix}archive"] =
true;
649 private function getUserForPreview(): UserIdentity {
651 if ( $this->tempUserCreator->shouldAutoCreate( $user,
'edit' ) ) {
652 return $this->userFactory->newUnsavedTempUser(
653 $this->tempUserCreator->getStashedName( $this->getRequest()->getSession() )
661 $slotRoles = $this->slotRoleRegistry->getKnownRoles();
662 sort( $slotRoles, SORT_STRING );
668 ParamValidator::PARAM_TYPE =>
'integer'
671 ParamValidator::PARAM_TYPE =>
'integer'
675 ParamValidator::PARAM_TYPE => $slotRoles,
676 ParamValidator::PARAM_ISMULTI =>
true,
680 ParamValidator::PARAM_TYPE =>
'text',
682 'section-{slot}' => [
684 ParamValidator::PARAM_TYPE =>
'string',
686 'contentformat-{slot}' => [
688 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
690 'contentmodel-{slot}' => [
692 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getContentModels(),
697 ParamValidator::PARAM_TYPE =>
'text',
698 ParamValidator::PARAM_DEPRECATED =>
true,
701 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
702 ParamValidator::PARAM_DEPRECATED =>
true,
705 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getContentModels(),
706 ParamValidator::PARAM_DEPRECATED =>
true,
709 ParamValidator::PARAM_DEFAULT =>
null,
710 ParamValidator::PARAM_DEPRECATED =>
true,
715 foreach ( $fromToParams as $k => $v ) {
721 foreach ( $fromToParams as $k => $v ) {
730 [
'torelative' => [ ParamValidator::PARAM_TYPE => [
'prev',
'next',
'cur' ], ] ],
735 ParamValidator::PARAM_DEFAULT =>
'diff|ids|title',
736 ParamValidator::PARAM_TYPE => [
748 ParamValidator::PARAM_ISMULTI =>
true,
753 ParamValidator::PARAM_TYPE => $slotRoles,
754 ParamValidator::PARAM_ISMULTI =>
true,
755 ParamValidator::PARAM_ALL =>
true,
759 ParamValidator::PARAM_TYPE => $this->differenceEngine->getSupportedFormats(),
760 ParamValidator::PARAM_DEFAULT =>
'table',
769 'action=compare&fromrev=1&torev=2'
770 =>
'apihelp-compare-example-1',
776 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Compare';