1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
|
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\Api;
use DifferenceEngine;
use Exception;
use MediaWiki\CommentFormatter\CommentFormatter;
use MediaWiki\Content\IContentHandlerFactory;
use MediaWiki\Content\Transform\ContentTransformer;
use MediaWiki\Context\DerivativeContext;
use MediaWiki\Parser\ParserOptions;
use MediaWiki\Revision\ArchivedRevisionLookup;
use MediaWiki\Revision\MutableRevisionRecord;
use MediaWiki\Revision\RevisionArchiveRecord;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Revision\RevisionStore;
use MediaWiki\Revision\SlotRecord;
use MediaWiki\Revision\SlotRoleRegistry;
use MediaWiki\Title\Title;
use MediaWiki\User\TempUser\TempUserCreator;
use MediaWiki\User\UserFactory;
use MWContentSerializationException;
use Wikimedia\ParamValidator\ParamValidator;
use Wikimedia\RequestTimeout\TimeoutException;
/**
* @ingroup API
*/
class ApiComparePages extends ApiBase {
private RevisionStore $revisionStore;
private ArchivedRevisionLookup $archivedRevisionLookup;
private SlotRoleRegistry $slotRoleRegistry;
/** @var Title|null|false */
private $guessedTitle = false;
/** @var array<string,true> */
private $props;
private IContentHandlerFactory $contentHandlerFactory;
private ContentTransformer $contentTransformer;
private CommentFormatter $commentFormatter;
private TempUserCreator $tempUserCreator;
private UserFactory $userFactory;
private DifferenceEngine $differenceEngine;
public function __construct(
ApiMain $mainModule,
string $moduleName,
RevisionStore $revisionStore,
ArchivedRevisionLookup $archivedRevisionLookup,
SlotRoleRegistry $slotRoleRegistry,
IContentHandlerFactory $contentHandlerFactory,
ContentTransformer $contentTransformer,
CommentFormatter $commentFormatter,
TempUserCreator $tempUserCreator,
UserFactory $userFactory
) {
parent::__construct( $mainModule, $moduleName );
$this->revisionStore = $revisionStore;
$this->archivedRevisionLookup = $archivedRevisionLookup;
$this->slotRoleRegistry = $slotRoleRegistry;
$this->contentHandlerFactory = $contentHandlerFactory;
$this->contentTransformer = $contentTransformer;
$this->commentFormatter = $commentFormatter;
$this->tempUserCreator = $tempUserCreator;
$this->userFactory = $userFactory;
$this->differenceEngine = new DifferenceEngine;
}
public function execute() {
$params = $this->extractRequestParams();
// Parameter validation
$this->requireAtLeastOneParameter(
$params, 'fromtitle', 'fromid', 'fromrev', 'fromtext', 'fromslots'
);
$this->requireAtLeastOneParameter(
$params, 'totitle', 'toid', 'torev', 'totext', 'torelative', 'toslots'
);
$this->props = array_fill_keys( $params['prop'], true );
// Cache responses publicly by default. This may be overridden later.
$this->getMain()->setCacheMode( 'public' );
// Get the 'from' RevisionRecord
[ $fromRev, $fromRelRev, $fromValsRev ] = $this->getDiffRevision( 'from', $params );
// Get the 'to' RevisionRecord
if ( $params['torelative'] !== null ) {
if ( !$fromRelRev ) {
$this->dieWithError( 'apierror-compare-relative-to-nothing' );
}
if ( $params['torelative'] !== 'cur' && $fromRelRev instanceof RevisionArchiveRecord ) {
// RevisionStore's getPreviousRevision/getNextRevision blow up
// when passed an RevisionArchiveRecord for a deleted page
$this->dieWithError( [ 'apierror-compare-relative-to-deleted', $params['torelative'] ] );
}
switch ( $params['torelative'] ) {
case 'prev':
// Swap 'from' and 'to'
[ $toRev, $toRelRev, $toValsRev ] = [ $fromRev, $fromRelRev, $fromValsRev ];
$fromRev = $this->revisionStore->getPreviousRevision( $toRelRev );
$fromRelRev = $fromRev;
$fromValsRev = $fromRev;
if ( !$fromRev ) {
$title = Title::newFromLinkTarget( $toRelRev->getPageAsLinkTarget() );
$this->addWarning( [
'apiwarn-compare-no-prev',
wfEscapeWikiText( $title->getPrefixedText() ),
$toRelRev->getId()
] );
// (T203433) Create an empty dummy revision as the "previous".
// The main slot has to exist, the rest will be handled by DifferenceEngine.
$fromRev = new MutableRevisionRecord(
$title ?: $toRev->getPage()
);
$fromRev->setContent(
SlotRecord::MAIN,
$toRelRev->getContent( SlotRecord::MAIN, RevisionRecord::RAW )
->getContentHandler()
->makeEmptyContent()
);
}
break;
case 'next':
$toRev = $this->revisionStore->getNextRevision( $fromRelRev );
$toRelRev = $toRev;
$toValsRev = $toRev;
if ( !$toRev ) {
$title = Title::newFromLinkTarget( $fromRelRev->getPageAsLinkTarget() );
$this->addWarning( [
'apiwarn-compare-no-next',
wfEscapeWikiText( $title->getPrefixedText() ),
$fromRelRev->getId()
] );
// (T203433) The web UI treats "next" as "cur" in this case.
// Avoid repeating metadata by making a MutableRevisionRecord with no changes.
$toRev = MutableRevisionRecord::newFromParentRevision( $fromRelRev );
}
break;
case 'cur':
$title = $fromRelRev->getPageAsLinkTarget();
$toRev = $this->revisionStore->getRevisionByTitle( $title );
if ( !$toRev ) {
$title = Title::newFromLinkTarget( $title );
$this->dieWithError(
[ 'apierror-missingrev-title', wfEscapeWikiText( $title->getPrefixedText() ) ],
'nosuchrevid'
);
}
$toRelRev = $toRev;
$toValsRev = $toRev;
break;
}
} else {
[ $toRev, $toRelRev, $toValsRev ] = $this->getDiffRevision( 'to', $params );
}
// Handle missing from or to revisions (should never happen)
// @codeCoverageIgnoreStart
// @phan-suppress-next-line PhanPossiblyUndeclaredVariable T240141
if ( !$fromRev || !$toRev ) {
$this->dieWithError( 'apierror-baddiff' );
}
// @codeCoverageIgnoreEnd
// Handle revdel
if ( !$fromRev->userCan( RevisionRecord::DELETED_TEXT, $this->getAuthority() ) ) {
$this->dieWithError( [ 'apierror-missingcontent-revid', $fromRev->getId() ], 'missingcontent' );
}
if ( !$toRev->userCan( RevisionRecord::DELETED_TEXT, $this->getAuthority() ) ) {
$this->dieWithError( [ 'apierror-missingcontent-revid', $toRev->getId() ], 'missingcontent' );
}
// Get the diff
$context = new DerivativeContext( $this->getContext() );
if ( $fromRelRev && $fromRelRev->getPageAsLinkTarget() ) {
$context->setTitle( Title::newFromLinkTarget( $fromRelRev->getPageAsLinkTarget() ) );
// @phan-suppress-next-line PhanPossiblyUndeclaredVariable T240141
} elseif ( $toRelRev && $toRelRev->getPageAsLinkTarget() ) {
$context->setTitle( Title::newFromLinkTarget( $toRelRev->getPageAsLinkTarget() ) );
} else {
$guessedTitle = $this->guessTitle();
if ( $guessedTitle ) {
$context->setTitle( $guessedTitle );
}
}
$this->differenceEngine->setContext( $context );
$this->differenceEngine->setSlotDiffOptions( [ 'diff-type' => $params['difftype'] ] );
$this->differenceEngine->setRevisions( $fromRev, $toRev );
if ( $params['slots'] === null ) {
$difftext = $this->differenceEngine->getDiffBody();
if ( $difftext === false ) {
$this->dieWithError( 'apierror-baddiff' );
}
} else {
$difftext = [];
foreach ( $params['slots'] as $role ) {
$difftext[$role] = $this->differenceEngine->getDiffBodyForRole( $role );
}
}
foreach ( $this->differenceEngine->getRevisionLoadErrors() as $msg ) {
$this->addWarning( $msg );
}
// Fill in the response
$vals = [];
$this->setVals( $vals, 'from', $fromValsRev );
// @phan-suppress-next-line PhanPossiblyUndeclaredVariable T240141
$this->setVals( $vals, 'to', $toValsRev );
if ( isset( $this->props['rel'] ) ) {
if ( !$fromRev instanceof MutableRevisionRecord ) {
$rev = $this->revisionStore->getPreviousRevision( $fromRev );
if ( $rev ) {
$vals['prev'] = $rev->getId();
}
}
if ( !$toRev instanceof MutableRevisionRecord ) {
$rev = $this->revisionStore->getNextRevision( $toRev );
if ( $rev ) {
$vals['next'] = $rev->getId();
}
}
}
if ( isset( $this->props['diffsize'] ) ) {
$vals['diffsize'] = 0;
foreach ( (array)$difftext as $text ) {
$vals['diffsize'] += strlen( $text );
}
}
if ( isset( $this->props['diff'] ) ) {
if ( is_array( $difftext ) ) {
ApiResult::setArrayType( $difftext, 'kvp', 'diff' );
$vals['bodies'] = $difftext;
} else {
ApiResult::setContentValue( $vals, 'body', $difftext );
}
}
// Diffs can be really big and there's little point in having
// ApiResult truncate it to an empty response since the diff is the
// whole reason this module exists. So pass NO_SIZE_CHECK here.
$this->getResult()->addValue( null, $this->getModuleName(), $vals, ApiResult::NO_SIZE_CHECK );
}
/**
* Load a revision by ID
*
* Falls back to checking the archive table if appropriate.
*
* @param int $id
* @return RevisionRecord|null
*/
private function getRevisionById( $id ) {
$rev = $this->revisionStore->getRevisionById( $id );
if ( $rev ) {
$this->checkTitleUserPermissions( $rev->getPage(), 'read' );
}
if ( !$rev && $this->getAuthority()->isAllowedAny( 'deletedtext', 'undelete' ) ) {
// Try the 'archive' table
$rev = $this->archivedRevisionLookup->getArchivedRevisionRecord( null, $id );
if ( $rev ) {
$this->checkTitleUserPermissions( $rev->getPage(), 'deletedtext' );
}
}
return $rev;
}
/**
* Guess an appropriate default Title for this request
*
* @return Title|null
*/
private function guessTitle() {
if ( $this->guessedTitle !== false ) {
return $this->guessedTitle;
}
$this->guessedTitle = null;
$params = $this->extractRequestParams();
foreach ( [ 'from', 'to' ] as $prefix ) {
if ( $params["{$prefix}rev"] !== null ) {
$rev = $this->getRevisionById( $params["{$prefix}rev"] );
if ( $rev ) {
$this->guessedTitle = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
break;
}
}
if ( $params["{$prefix}title"] !== null ) {
$title = Title::newFromText( $params["{$prefix}title"] );
if ( $title && !$title->isExternal() ) {
$this->guessedTitle = $title;
break;
}
}
if ( $params["{$prefix}id"] !== null ) {
$title = Title::newFromID( $params["{$prefix}id"] );
if ( $title ) {
$this->guessedTitle = $title;
break;
}
}
}
return $this->guessedTitle;
}
/**
* Guess an appropriate default content model for this request
* @param string $role Slot for which to guess the model
* @return string|null Guessed content model
*/
private function guessModel( $role ) {
$params = $this->extractRequestParams();
foreach ( [ 'from', 'to' ] as $prefix ) {
if ( $params["{$prefix}rev"] !== null ) {
$rev = $this->getRevisionById( $params["{$prefix}rev"] );
if ( $rev && $rev->hasSlot( $role ) ) {
return $rev->getSlot( $role, RevisionRecord::RAW )->getModel();
}
}
}
$guessedTitle = $this->guessTitle();
if ( $guessedTitle ) {
return $this->slotRoleRegistry->getRoleHandler( $role )->getDefaultModel( $guessedTitle );
}
if ( isset( $params["fromcontentmodel-$role"] ) ) {
return $params["fromcontentmodel-$role"];
}
if ( isset( $params["tocontentmodel-$role"] ) ) {
return $params["tocontentmodel-$role"];
}
if ( $role === SlotRecord::MAIN ) {
if ( isset( $params['fromcontentmodel'] ) ) {
return $params['fromcontentmodel'];
}
if ( isset( $params['tocontentmodel'] ) ) {
return $params['tocontentmodel'];
}
}
return null;
}
/**
* Get the RevisionRecord for one side of the diff
*
* This uses the appropriate set of parameters to determine what content
* should be diffed.
*
* Returns three values:
* - A RevisionRecord holding the content
* - The revision specified, if any, even if content was supplied
* - The revision to pass to setVals(), if any
*
* @param string $prefix 'from' or 'to'
* @param array $params
* @return array [ RevisionRecord|null, RevisionRecord|null, RevisionRecord|null ]
*/
private function getDiffRevision( $prefix, array $params ) {
// Back compat params
$this->requireMaxOneParameter( $params, "{$prefix}text", "{$prefix}slots" );
$this->requireMaxOneParameter( $params, "{$prefix}section", "{$prefix}slots" );
if ( $params["{$prefix}text"] !== null ) {
$params["{$prefix}slots"] = [ SlotRecord::MAIN ];
$params["{$prefix}text-main"] = $params["{$prefix}text"];
$params["{$prefix}section-main"] = null;
$params["{$prefix}contentmodel-main"] = $params["{$prefix}contentmodel"];
$params["{$prefix}contentformat-main"] = $params["{$prefix}contentformat"];
}
$title = null;
$rev = null;
$suppliedContent = $params["{$prefix}slots"] !== null;
// Get the revision and title, if applicable
$revId = null;
if ( $params["{$prefix}rev"] !== null ) {
$revId = $params["{$prefix}rev"];
} elseif ( $params["{$prefix}title"] !== null || $params["{$prefix}id"] !== null ) {
if ( $params["{$prefix}title"] !== null ) {
$title = Title::newFromText( $params["{$prefix}title"] );
if ( !$title || $title->isExternal() ) {
$this->dieWithError(
[ 'apierror-invalidtitle', wfEscapeWikiText( $params["{$prefix}title"] ) ]
);
}
} else {
$title = Title::newFromID( $params["{$prefix}id"] );
if ( !$title ) {
$this->dieWithError( [ 'apierror-nosuchpageid', $params["{$prefix}id"] ] );
}
}
$revId = $title->getLatestRevID();
if ( !$revId ) {
$revId = null;
// Only die here if we're not using supplied text
if ( !$suppliedContent ) {
if ( $title->exists() ) {
$this->dieWithError(
[ 'apierror-missingrev-title', wfEscapeWikiText( $title->getPrefixedText() ) ],
'nosuchrevid'
);
} else {
$this->dieWithError(
[ 'apierror-missingtitle-byname', wfEscapeWikiText( $title->getPrefixedText() ) ],
'missingtitle'
);
}
}
}
}
if ( $revId !== null ) {
$rev = $this->getRevisionById( $revId );
if ( !$rev ) {
$this->dieWithError( [ 'apierror-nosuchrevid', $revId ] );
}
$title = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
// If we don't have supplied content, return here. Otherwise,
// continue on below with the supplied content.
if ( !$suppliedContent ) {
$newRev = $rev;
// Deprecated 'fromsection'/'tosection'
if ( isset( $params["{$prefix}section"] ) ) {
$section = $params["{$prefix}section"];
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable T240141
$newRev = MutableRevisionRecord::newFromParentRevision( $rev );
$content = $rev->getContent( SlotRecord::MAIN, RevisionRecord::FOR_THIS_USER,
$this->getAuthority() );
if ( !$content ) {
$this->dieWithError(
[ 'apierror-missingcontent-revid-role', $rev->getId(), SlotRecord::MAIN ], 'missingcontent'
);
}
$content = $content->getSection( $section );
if ( !$content ) {
$this->dieWithError(
[ "apierror-compare-nosuch{$prefix}section", wfEscapeWikiText( $section ) ],
"nosuch{$prefix}section"
);
}
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable T240141
$newRev->setContent( SlotRecord::MAIN, $content );
}
return [ $newRev, $rev, $rev ];
}
}
// Override $content based on supplied text
if ( !$title ) {
$title = $this->guessTitle();
}
if ( $rev ) {
$newRev = MutableRevisionRecord::newFromParentRevision( $rev );
} else {
$newRev = new MutableRevisionRecord( $title ?: Title::newMainPage() );
}
foreach ( $params["{$prefix}slots"] as $role ) {
$text = $params["{$prefix}text-{$role}"];
if ( $text === null ) {
// The SlotRecord::MAIN role can't be deleted
if ( $role === SlotRecord::MAIN ) {
$this->dieWithError( [ 'apierror-compare-maintextrequired', $prefix ] );
}
// These parameters make no sense without text. Reject them to avoid
// confusion.
foreach ( [ 'section', 'contentmodel', 'contentformat' ] as $param ) {
if ( isset( $params["{$prefix}{$param}-{$role}"] ) ) {
$this->dieWithError( [
'apierror-compare-notext',
wfEscapeWikiText( "{$prefix}{$param}-{$role}" ),
wfEscapeWikiText( "{$prefix}text-{$role}" ),
] );
}
}
$newRev->removeSlot( $role );
continue;
}
$model = $params["{$prefix}contentmodel-{$role}"];
$format = $params["{$prefix}contentformat-{$role}"];
if ( !$model && $rev && $rev->hasSlot( $role ) ) {
$model = $rev->getSlot( $role, RevisionRecord::RAW )->getModel();
}
if ( !$model && $title && $role === SlotRecord::MAIN ) {
// @todo: Use SlotRoleRegistry and do this for all slots
$model = $title->getContentModel();
}
if ( !$model ) {
$model = $this->guessModel( $role );
}
if ( !$model ) {
$model = CONTENT_MODEL_WIKITEXT;
$this->addWarning( [ 'apiwarn-compare-nocontentmodel', $model ] );
}
try {
$content = $this->contentHandlerFactory
->getContentHandler( $model )
->unserializeContent( $text, $format );
} catch ( MWContentSerializationException $ex ) {
$this->dieWithException( $ex, [
'wrap' => ApiMessage::create( 'apierror-contentserializationexception', 'parseerror' )
] );
}
if ( $params["{$prefix}pst"] ) {
if ( !$title ) {
$this->dieWithError( 'apierror-compare-no-title' );
}
$popts = ParserOptions::newFromContext( $this->getContext() );
$content = $this->contentTransformer->preSaveTransform(
$content,
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable T240141
$title,
$this->getUserForPreview(),
$popts
);
}
$section = $params["{$prefix}section-{$role}"];
if ( $section !== null && $section !== '' ) {
if ( !$rev ) {
$this->dieWithError( "apierror-compare-no{$prefix}revision" );
}
$oldContent = $rev->getContent( $role, RevisionRecord::FOR_THIS_USER, $this->getAuthority() );
if ( !$oldContent ) {
$this->dieWithError(
[ 'apierror-missingcontent-revid-role', $rev->getId(), wfEscapeWikiText( $role ) ],
'missingcontent'
);
}
if ( !$oldContent->getContentHandler()->supportsSections() ) {
$this->dieWithError( [ 'apierror-sectionsnotsupported', $content->getModel() ] );
}
try {
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable T240141
$content = $oldContent->replaceSection( $section, $content, '' );
} catch ( TimeoutException $e ) {
throw $e;
} catch ( Exception $ex ) {
// Probably a content model mismatch.
$content = null;
}
if ( !$content ) {
$this->dieWithError( [ 'apierror-sectionreplacefailed' ] );
}
}
// Deprecated 'fromsection'/'tosection'
if ( $role === SlotRecord::MAIN && isset( $params["{$prefix}section"] ) ) {
$section = $params["{$prefix}section"];
$content = $content->getSection( $section );
if ( !$content ) {
$this->dieWithError(
[ "apierror-compare-nosuch{$prefix}section", wfEscapeWikiText( $section ) ],
"nosuch{$prefix}section"
);
}
}
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable T240141
$newRev->setContent( $role, $content );
}
return [ $newRev, $rev, null ];
}
/**
* Set value fields from a RevisionRecord object
*
* @param array &$vals Result array to set data into
* @param string $prefix 'from' or 'to'
* @param RevisionRecord|null $rev
*/
private function setVals( &$vals, $prefix, $rev ) {
if ( $rev ) {
$title = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
if ( isset( $this->props['ids'] ) ) {
$vals["{$prefix}id"] = $title->getArticleID();
$vals["{$prefix}revid"] = $rev->getId();
}
if ( isset( $this->props['title'] ) ) {
ApiQueryBase::addTitleInfo( $vals, $title, $prefix );
}
if ( isset( $this->props['size'] ) ) {
$vals["{$prefix}size"] = $rev->getSize();
}
if ( isset( $this->props['timestamp'] ) ) {
$revTimestamp = $rev->getTimestamp();
if ( $revTimestamp ) {
$vals["{$prefix}timestamp"] = wfTimestamp( TS_ISO_8601, $revTimestamp );
}
}
$anyHidden = false;
if ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
$vals["{$prefix}texthidden"] = true;
$anyHidden = true;
}
if ( $rev->isDeleted( RevisionRecord::DELETED_USER ) ) {
$vals["{$prefix}userhidden"] = true;
$anyHidden = true;
}
if ( isset( $this->props['user'] ) ) {
$user = $rev->getUser( RevisionRecord::FOR_THIS_USER, $this->getAuthority() );
if ( $user ) {
$vals["{$prefix}user"] = $user->getName();
$vals["{$prefix}userid"] = $user->getId();
}
}
if ( $rev->isDeleted( RevisionRecord::DELETED_COMMENT ) ) {
$vals["{$prefix}commenthidden"] = true;
$anyHidden = true;
}
if ( isset( $this->props['comment'] ) || isset( $this->props['parsedcomment'] ) ) {
$comment = $rev->getComment( RevisionRecord::FOR_THIS_USER, $this->getAuthority() );
if ( $comment !== null ) {
if ( isset( $this->props['comment'] ) ) {
$vals["{$prefix}comment"] = $comment->text;
}
$vals["{$prefix}parsedcomment"] = $this->commentFormatter->format(
$comment->text, $title
);
}
}
if ( $anyHidden ) {
$this->getMain()->setCacheMode( 'private' );
if ( $rev->isDeleted( RevisionRecord::DELETED_RESTRICTED ) ) {
$vals["{$prefix}suppressed"] = true;
}
}
if ( $rev instanceof RevisionArchiveRecord ) {
$this->getMain()->setCacheMode( 'private' );
$vals["{$prefix}archive"] = true;
}
}
}
private function getUserForPreview() {
$user = $this->getUser();
if ( $this->tempUserCreator->shouldAutoCreate( $user, 'edit' ) ) {
return $this->userFactory->newUnsavedTempUser(
$this->tempUserCreator->getStashedName( $this->getRequest()->getSession() )
);
}
return $user;
}
public function getAllowedParams() {
$slotRoles = $this->slotRoleRegistry->getKnownRoles();
sort( $slotRoles, SORT_STRING );
// Parameters for the 'from' and 'to' content
$fromToParams = [
'title' => null,
'id' => [
ParamValidator::PARAM_TYPE => 'integer'
],
'rev' => [
ParamValidator::PARAM_TYPE => 'integer'
],
'slots' => [
ParamValidator::PARAM_TYPE => $slotRoles,
ParamValidator::PARAM_ISMULTI => true,
],
'text-{slot}' => [
ApiBase::PARAM_TEMPLATE_VARS => [ 'slot' => 'slots' ], // fixed below
ParamValidator::PARAM_TYPE => 'text',
],
'section-{slot}' => [
ApiBase::PARAM_TEMPLATE_VARS => [ 'slot' => 'slots' ], // fixed below
ParamValidator::PARAM_TYPE => 'string',
],
'contentformat-{slot}' => [
ApiBase::PARAM_TEMPLATE_VARS => [ 'slot' => 'slots' ], // fixed below
ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
],
'contentmodel-{slot}' => [
ApiBase::PARAM_TEMPLATE_VARS => [ 'slot' => 'slots' ], // fixed below
ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getContentModels(),
],
'pst' => false,
'text' => [
ParamValidator::PARAM_TYPE => 'text',
ParamValidator::PARAM_DEPRECATED => true,
],
'contentformat' => [
ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
ParamValidator::PARAM_DEPRECATED => true,
],
'contentmodel' => [
ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getContentModels(),
ParamValidator::PARAM_DEPRECATED => true,
],
'section' => [
ParamValidator::PARAM_DEFAULT => null,
ParamValidator::PARAM_DEPRECATED => true,
],
];
$ret = [];
foreach ( $fromToParams as $k => $v ) {
if ( isset( $v[ApiBase::PARAM_TEMPLATE_VARS]['slot'] ) ) {
$v[ApiBase::PARAM_TEMPLATE_VARS]['slot'] = 'fromslots';
}
$ret["from$k"] = $v;
}
foreach ( $fromToParams as $k => $v ) {
if ( isset( $v[ApiBase::PARAM_TEMPLATE_VARS]['slot'] ) ) {
$v[ApiBase::PARAM_TEMPLATE_VARS]['slot'] = 'toslots';
}
$ret["to$k"] = $v;
}
$ret = wfArrayInsertAfter(
$ret,
[ 'torelative' => [ ParamValidator::PARAM_TYPE => [ 'prev', 'next', 'cur' ], ] ],
'torev'
);
$ret['prop'] = [
ParamValidator::PARAM_DEFAULT => 'diff|ids|title',
ParamValidator::PARAM_TYPE => [
'diff',
'diffsize',
'rel',
'ids',
'title',
'user',
'comment',
'parsedcomment',
'size',
'timestamp',
],
ParamValidator::PARAM_ISMULTI => true,
ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
];
$ret['slots'] = [
ParamValidator::PARAM_TYPE => $slotRoles,
ParamValidator::PARAM_ISMULTI => true,
ParamValidator::PARAM_ALL => true,
];
$ret['difftype'] = [
ParamValidator::PARAM_TYPE => $this->differenceEngine->getSupportedFormats(),
ParamValidator::PARAM_DEFAULT => 'table',
];
return $ret;
}
protected function getExamplesMessages() {
return [
'action=compare&fromrev=1&torev=2'
=> 'apihelp-compare-example-1',
];
}
public function getHelpUrls() {
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Compare';
}
}
/** @deprecated class alias since 1.43 */
class_alias( ApiComparePages::class, 'ApiComparePages' );
|