48 private $guessedTitle =
false;
82 parent::__construct( $mainModule, $moduleName );
83 $this->revisionStore = $revisionStore;
84 $this->archivedRevisionLookup = $archivedRevisionLookup;
85 $this->slotRoleRegistry = $slotRoleRegistry;
86 $this->contentHandlerFactory = $contentHandlerFactory;
87 $this->contentTransformer = $contentTransformer;
88 $this->commentFormatter = $commentFormatter;
89 $this->tempUserCreator = $tempUserCreator;
90 $this->userFactory = $userFactory;
99 $params,
'fromtitle',
'fromid',
'fromrev',
'fromtext',
'fromslots'
102 $params,
'totitle',
'toid',
'torev',
'totext',
'torelative',
'toslots'
105 $this->props = array_fill_keys(
$params[
'prop'],
true );
108 $this->
getMain()->setCacheMode(
'public' );
111 [ $fromRev, $fromRelRev, $fromValsRev ] = $this->getDiffRevision(
'from',
$params );
114 if (
$params[
'torelative'] !==
null ) {
115 if ( !$fromRelRev ) {
116 $this->
dieWithError(
'apierror-compare-relative-to-nothing' );
121 $this->
dieWithError( [
'apierror-compare-relative-to-deleted', $params[
'torelative'] ] );
123 switch (
$params[
'torelative'] ) {
126 [ $toRev, $toRelRev, $toValsRev ] = [ $fromRev, $fromRelRev, $fromValsRev ];
127 $fromRev = $this->revisionStore->getPreviousRevision( $toRelRev );
128 $fromRelRev = $fromRev;
129 $fromValsRev = $fromRev;
131 $title = Title::newFromLinkTarget( $toRelRev->getPageAsLinkTarget() );
133 'apiwarn-compare-no-prev',
141 $title ?: $toRev->getPage()
145 $toRelRev->getContent( SlotRecord::MAIN, RevisionRecord::RAW )
146 ->getContentHandler()
153 $toRev = $this->revisionStore->getNextRevision( $fromRelRev );
157 $title = Title::newFromLinkTarget( $fromRelRev->getPageAsLinkTarget() );
159 'apiwarn-compare-no-next',
166 $toRev = MutableRevisionRecord::newFromParentRevision( $fromRelRev );
171 $title = $fromRelRev->getPageAsLinkTarget();
172 $toRev = $this->revisionStore->getRevisionByTitle( $title );
174 $title = Title::newFromLinkTarget( $title );
176 [
'apierror-missingrev-title',
wfEscapeWikiText( $title->getPrefixedText() ) ],
185 [ $toRev, $toRelRev, $toValsRev ] = $this->getDiffRevision(
'to',
$params );
191 if ( !$fromRev || !$toRev ) {
197 if ( !$fromRev->userCan( RevisionRecord::DELETED_TEXT, $this->getAuthority() ) ) {
198 $this->
dieWithError( [
'apierror-missingcontent-revid', $fromRev->getId() ],
'missingcontent' );
200 if ( !$toRev->userCan( RevisionRecord::DELETED_TEXT, $this->getAuthority() ) ) {
201 $this->
dieWithError( [
'apierror-missingcontent-revid', $toRev->getId() ],
'missingcontent' );
206 if ( $fromRelRev && $fromRelRev->getPageAsLinkTarget() ) {
207 $context->setTitle( Title::newFromLinkTarget( $fromRelRev->getPageAsLinkTarget() ) );
209 } elseif ( $toRelRev && $toRelRev->getPageAsLinkTarget() ) {
210 $context->setTitle( Title::newFromLinkTarget( $toRelRev->getPageAsLinkTarget() ) );
212 $guessedTitle = $this->guessTitle();
213 if ( $guessedTitle ) {
214 $context->setTitle( $guessedTitle );
217 $this->differenceEngine->setContext( $context );
218 $this->differenceEngine->setSlotDiffOptions( [
'diff-type' =>
$params[
'difftype'] ] );
219 $this->differenceEngine->setRevisions( $fromRev, $toRev );
220 if (
$params[
'slots'] ===
null ) {
221 $difftext = $this->differenceEngine->getDiffBody();
222 if ( $difftext ===
false ) {
227 foreach (
$params[
'slots'] as $role ) {
228 $difftext[$role] = $this->differenceEngine->getDiffBodyForRole( $role );
231 foreach ( $this->differenceEngine->getRevisionLoadErrors() as $msg ) {
237 $this->setVals( $vals,
'from', $fromValsRev );
239 $this->setVals( $vals,
'to', $toValsRev );
241 if ( isset( $this->props[
'rel'] ) ) {
243 $rev = $this->revisionStore->getPreviousRevision( $fromRev );
245 $vals[
'prev'] = $rev->getId();
249 $rev = $this->revisionStore->getNextRevision( $toRev );
251 $vals[
'next'] = $rev->getId();
256 if ( isset( $this->props[
'diffsize'] ) ) {
257 $vals[
'diffsize'] = 0;
258 foreach ( (array)$difftext as $text ) {
259 $vals[
'diffsize'] += strlen( $text );
262 if ( isset( $this->props[
'diff'] ) ) {
263 if ( is_array( $difftext ) ) {
264 ApiResult::setArrayType( $difftext,
'kvp',
'diff' );
265 $vals[
'bodies'] = $difftext;
267 ApiResult::setContentValue( $vals,
'body', $difftext );
285 private function getRevisionById( $id ) {
286 $rev = $this->revisionStore->getRevisionById( $id );
287 if ( !$rev && $this->
getAuthority()->isAllowedAny(
'deletedtext',
'undelete' ) ) {
289 $rev = $this->archivedRevisionLookup->getArchivedRevisionRecord(
null, $id );
299 private function guessTitle() {
300 if ( $this->guessedTitle !==
false ) {
301 return $this->guessedTitle;
304 $this->guessedTitle =
null;
307 foreach ( [
'from',
'to' ] as $prefix ) {
308 if (
$params[
"{$prefix}rev"] !==
null ) {
309 $rev = $this->getRevisionById(
$params[
"{$prefix}rev"] );
311 $this->guessedTitle = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
316 if (
$params[
"{$prefix}title"] !==
null ) {
317 $title = Title::newFromText(
$params[
"{$prefix}title"] );
318 if ( $title && !$title->isExternal() ) {
319 $this->guessedTitle = $title;
324 if (
$params[
"{$prefix}id"] !==
null ) {
325 $title = Title::newFromID(
$params[
"{$prefix}id"] );
327 $this->guessedTitle = $title;
333 return $this->guessedTitle;
341 private function guessModel( $role ) {
344 foreach ( [
'from',
'to' ] as $prefix ) {
345 if (
$params[
"{$prefix}rev"] !==
null ) {
346 $rev = $this->getRevisionById(
$params[
"{$prefix}rev"] );
347 if ( $rev && $rev->hasSlot( $role ) ) {
348 return $rev->getSlot( $role, RevisionRecord::RAW )->getModel();
353 $guessedTitle = $this->guessTitle();
354 if ( $guessedTitle ) {
355 return $this->slotRoleRegistry->getRoleHandler( $role )->getDefaultModel( $guessedTitle );
358 if ( isset(
$params[
"fromcontentmodel-$role"] ) ) {
359 return $params[
"fromcontentmodel-$role"];
361 if ( isset(
$params[
"tocontentmodel-$role"] ) ) {
362 return $params[
"tocontentmodel-$role"];
365 if ( $role === SlotRecord::MAIN ) {
366 if ( isset(
$params[
'fromcontentmodel'] ) ) {
367 return $params[
'fromcontentmodel'];
369 if ( isset(
$params[
'tocontentmodel'] ) ) {
370 return $params[
'tocontentmodel'];
392 private function getDiffRevision( $prefix, array
$params ) {
396 if (
$params[
"{$prefix}text"] !==
null ) {
397 $params[
"{$prefix}slots"] = [ SlotRecord::MAIN ];
399 $params[
"{$prefix}section-main"] =
null;
400 $params[
"{$prefix}contentmodel-main"] =
$params[
"{$prefix}contentmodel"];
401 $params[
"{$prefix}contentformat-main"] =
$params[
"{$prefix}contentformat"];
406 $suppliedContent =
$params[
"{$prefix}slots"] !==
null;
410 if (
$params[
"{$prefix}rev"] !==
null ) {
411 $revId =
$params[
"{$prefix}rev"];
412 } elseif (
$params[
"{$prefix}title"] !==
null ||
$params[
"{$prefix}id"] !==
null ) {
413 if (
$params[
"{$prefix}title"] !==
null ) {
414 $title = Title::newFromText(
$params[
"{$prefix}title"] );
415 if ( !$title || $title->isExternal() ) {
421 $title = Title::newFromID(
$params[
"{$prefix}id"] );
423 $this->
dieWithError( [
'apierror-nosuchpageid', $params[
"{$prefix}id"] ] );
426 $revId = $title->getLatestRevID();
430 if ( !$suppliedContent ) {
431 if ( $title->exists() ) {
433 [
'apierror-missingrev-title',
wfEscapeWikiText( $title->getPrefixedText() ) ],
438 [
'apierror-missingtitle-byname',
wfEscapeWikiText( $title->getPrefixedText() ) ],
445 if ( $revId !==
null ) {
446 $rev = $this->getRevisionById( $revId );
448 $this->
dieWithError( [
'apierror-nosuchrevid', $revId ] );
450 $title = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
454 if ( !$suppliedContent ) {
458 if ( isset(
$params[
"{$prefix}section"] ) ) {
459 $section =
$params[
"{$prefix}section"];
461 $newRev = MutableRevisionRecord::newFromParentRevision( $rev );
462 $content = $rev->getContent( SlotRecord::MAIN, RevisionRecord::FOR_THIS_USER,
466 [
'apierror-missingcontent-revid-role', $rev->getId(), SlotRecord::MAIN ],
'missingcontent'
469 $content = $content->getSection( $section );
472 [
"apierror-compare-nosuch{$prefix}section",
wfEscapeWikiText( $section ) ],
473 "nosuch{$prefix}section"
477 $newRev->setContent( SlotRecord::MAIN, $content );
480 return [ $newRev, $rev, $rev ];
486 $title = $this->guessTitle();
489 $newRev = MutableRevisionRecord::newFromParentRevision( $rev );
493 foreach (
$params[
"{$prefix}slots"] as $role ) {
494 $text =
$params[
"{$prefix}text-{$role}"];
495 if ( $text ===
null ) {
497 if ( $role === SlotRecord::MAIN ) {
498 $this->
dieWithError( [
'apierror-compare-maintextrequired', $prefix ] );
503 foreach ( [
'section',
'contentmodel',
'contentformat' ] as $param ) {
504 if ( isset(
$params[
"{$prefix}{$param}-{$role}"] ) ) {
506 'apierror-compare-notext',
513 $newRev->removeSlot( $role );
517 $model =
$params[
"{$prefix}contentmodel-{$role}"];
518 $format =
$params[
"{$prefix}contentformat-{$role}"];
520 if ( !$model && $rev && $rev->hasSlot( $role ) ) {
521 $model = $rev->getSlot( $role, RevisionRecord::RAW )->getModel();
523 if ( !$model && $title && $role === SlotRecord::MAIN ) {
525 $model = $title->getContentModel();
528 $model = $this->guessModel( $role );
532 $this->
addWarning( [
'apiwarn-compare-nocontentmodel', $model ] );
536 $content = $this->contentHandlerFactory
537 ->getContentHandler( $model )
538 ->unserializeContent( $text, $format );
541 'wrap' => ApiMessage::create(
'apierror-contentserializationexception',
'parseerror' )
545 if (
$params[
"{$prefix}pst"] ) {
550 $content = $this->contentTransformer->preSaveTransform(
554 $this->getUserForPreview(),
559 $section =
$params[
"{$prefix}section-{$role}"];
560 if ( $section !==
null && $section !==
'' ) {
562 $this->
dieWithError(
"apierror-compare-no{$prefix}revision" );
564 $oldContent = $rev->getContent( $role, RevisionRecord::FOR_THIS_USER, $this->
getAuthority() );
565 if ( !$oldContent ) {
567 [
'apierror-missingcontent-revid-role', $rev->getId(),
wfEscapeWikiText( $role ) ],
571 if ( !$oldContent->getContentHandler()->supportsSections() ) {
572 $this->
dieWithError( [
'apierror-sectionsnotsupported', $content->getModel() ] );
576 $content = $oldContent->replaceSection( $section, $content,
'' );
577 }
catch ( TimeoutException $e ) {
579 }
catch ( Exception $ex ) {
584 $this->
dieWithError( [
'apierror-sectionreplacefailed' ] );
589 if ( $role === SlotRecord::MAIN && isset(
$params[
"{$prefix}section"] ) ) {
590 $section =
$params[
"{$prefix}section"];
591 $content = $content->getSection( $section );
594 [
"apierror-compare-nosuch{$prefix}section",
wfEscapeWikiText( $section ) ],
595 "nosuch{$prefix}section"
601 $newRev->setContent( $role, $content );
603 return [ $newRev, $rev, null ];
613 private function setVals( &$vals, $prefix, $rev ) {
615 $title = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
616 if ( isset( $this->props[
'ids'] ) ) {
617 $vals[
"{$prefix}id"] = $title->getArticleID();
618 $vals[
"{$prefix}revid"] = $rev->getId();
620 if ( isset( $this->props[
'title'] ) ) {
623 if ( isset( $this->props[
'size'] ) ) {
624 $vals[
"{$prefix}size"] = $rev->getSize();
626 if ( isset( $this->props[
'timestamp'] ) ) {
627 $revTimestamp = $rev->getTimestamp();
628 if ( $revTimestamp ) {
629 $vals[
"{$prefix}timestamp"] =
wfTimestamp( TS_ISO_8601, $revTimestamp );
634 if ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
635 $vals[
"{$prefix}texthidden"] =
true;
639 if ( $rev->isDeleted( RevisionRecord::DELETED_USER ) ) {
640 $vals[
"{$prefix}userhidden"] =
true;
643 if ( isset( $this->props[
'user'] ) ) {
644 $user = $rev->getUser( RevisionRecord::FOR_THIS_USER, $this->
getAuthority() );
646 $vals[
"{$prefix}user"] = $user->getName();
647 $vals[
"{$prefix}userid"] = $user->getId();
651 if ( $rev->isDeleted( RevisionRecord::DELETED_COMMENT ) ) {
652 $vals[
"{$prefix}commenthidden"] =
true;
655 if ( isset( $this->props[
'comment'] ) || isset( $this->props[
'parsedcomment'] ) ) {
656 $comment = $rev->getComment( RevisionRecord::FOR_THIS_USER, $this->
getAuthority() );
657 if ( $comment !==
null ) {
658 if ( isset( $this->props[
'comment'] ) ) {
659 $vals[
"{$prefix}comment"] = $comment->text;
661 $vals[
"{$prefix}parsedcomment"] = $this->commentFormatter->format(
662 $comment->text, $title
668 $this->
getMain()->setCacheMode(
'private' );
669 if ( $rev->isDeleted( RevisionRecord::DELETED_RESTRICTED ) ) {
670 $vals[
"{$prefix}suppressed"] =
true;
675 $this->
getMain()->setCacheMode(
'private' );
676 $vals[
"{$prefix}archive"] =
true;
681 private function getUserForPreview() {
683 if ( $this->tempUserCreator->shouldAutoCreate( $user,
'edit' ) ) {
684 return $this->userFactory->newUnsavedTempUser(
685 $this->tempUserCreator->getStashedName( $this->getRequest()->getSession() )
692 $slotRoles = $this->slotRoleRegistry->getKnownRoles();
693 sort( $slotRoles, SORT_STRING );
699 ParamValidator::PARAM_TYPE =>
'integer'
702 ParamValidator::PARAM_TYPE =>
'integer'
706 ParamValidator::PARAM_TYPE => $slotRoles,
707 ParamValidator::PARAM_ISMULTI =>
true,
711 ParamValidator::PARAM_TYPE =>
'text',
713 'section-{slot}' => [
715 ParamValidator::PARAM_TYPE =>
'string',
717 'contentformat-{slot}' => [
719 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
721 'contentmodel-{slot}' => [
723 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getContentModels(),
728 ParamValidator::PARAM_TYPE =>
'text',
729 ParamValidator::PARAM_DEPRECATED =>
true,
732 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
733 ParamValidator::PARAM_DEPRECATED =>
true,
736 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getContentModels(),
737 ParamValidator::PARAM_DEPRECATED =>
true,
740 ParamValidator::PARAM_DEFAULT =>
null,
741 ParamValidator::PARAM_DEPRECATED =>
true,
746 foreach ( $fromToParams as $k => $v ) {
752 foreach ( $fromToParams as $k => $v ) {
761 [
'torelative' => [ ParamValidator::PARAM_TYPE => [
'prev',
'next',
'cur' ], ] ],
766 ParamValidator::PARAM_DEFAULT =>
'diff|ids|title',
767 ParamValidator::PARAM_TYPE => [
779 ParamValidator::PARAM_ISMULTI =>
true,
784 ParamValidator::PARAM_TYPE => $slotRoles,
785 ParamValidator::PARAM_ISMULTI =>
true,
786 ParamValidator::PARAM_ALL =>
true,
790 ParamValidator::PARAM_TYPE => $this->differenceEngine->getSupportedFormats(),
791 ParamValidator::PARAM_DEFAULT =>
'table',
799 'action=compare&fromrev=1&torev=2'
800 =>
'apihelp-compare-example-1',
805 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Compare';
getHelpUrls()
Return links to more detailed help pages about the module.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
__construct(ApiMain $mainModule, $moduleName, RevisionStore $revisionStore, ArchivedRevisionLookup $archivedRevisionLookup, SlotRoleRegistry $slotRoleRegistry, IContentHandlerFactory $contentHandlerFactory, ContentTransformer $contentTransformer, CommentFormatter $commentFormatter, TempUserCreator $tempUserCreator, UserFactory $userFactory)
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
This is the main API class, used for both external and internal processing.