2022-01-31 15:01:32 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools;
|
|
|
|
|
|
|
|
use ApiBase;
|
|
|
|
use ApiMain;
|
2022-11-21 13:49:21 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\Hooks\HookUtils;
|
2022-09-08 00:29:35 +00:00
|
|
|
use MediaWiki\Extension\VisualEditor\VisualEditorParsoidClientFactory;
|
2022-09-02 01:49:44 +00:00
|
|
|
use MediaWiki\Revision\RevisionLookup;
|
2022-01-31 15:01:32 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
|
|
|
use Title;
|
|
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
|
|
|
|
|
|
|
class ApiDiscussionToolsCompare extends ApiBase {
|
|
|
|
|
2022-10-21 19:34:18 +00:00
|
|
|
private CommentParser $commentParser;
|
|
|
|
private VisualEditorParsoidClientFactory $parsoidClientFactory;
|
|
|
|
private RevisionLookup $revisionLookup;
|
2022-09-02 01:49:44 +00:00
|
|
|
|
2022-09-02 02:05:02 +00:00
|
|
|
public function __construct(
|
|
|
|
ApiMain $main,
|
|
|
|
string $name,
|
2022-09-08 00:29:35 +00:00
|
|
|
VisualEditorParsoidClientFactory $parsoidClientFactory,
|
2022-09-02 01:49:44 +00:00
|
|
|
CommentParser $commentParser,
|
|
|
|
RevisionLookup $revisionLookup
|
2022-09-02 02:05:02 +00:00
|
|
|
) {
|
2022-01-31 15:01:32 +00:00
|
|
|
parent::__construct( $main, $name );
|
2022-09-08 00:29:35 +00:00
|
|
|
$this->parsoidClientFactory = $parsoidClientFactory;
|
2022-09-02 02:05:02 +00:00
|
|
|
$this->commentParser = $commentParser;
|
2022-09-02 01:49:44 +00:00
|
|
|
$this->revisionLookup = $revisionLookup;
|
2022-01-31 15:01:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function execute() {
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
|
|
|
|
$this->requireOnlyOneParameter( $params, 'fromtitle', 'fromrev' );
|
|
|
|
$this->requireOnlyOneParameter( $params, 'totitle', 'torev' );
|
|
|
|
|
2022-09-02 01:49:44 +00:00
|
|
|
if ( isset( $params['torev'] ) ) {
|
|
|
|
$toRev = $this->revisionLookup->getRevisionById( $params['torev'] );
|
|
|
|
if ( !$toRev ) {
|
|
|
|
$this->dieWithError( [ 'apierror-nosuchrevid', $params['torev'] ] );
|
|
|
|
}
|
|
|
|
|
2022-01-31 15:01:32 +00:00
|
|
|
} else {
|
|
|
|
$toTitle = Title::newFromText( $params['totitle'] );
|
|
|
|
if ( !$toTitle ) {
|
|
|
|
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['totitle'] ) ] );
|
|
|
|
}
|
2022-09-02 01:49:44 +00:00
|
|
|
$toRev = $this->revisionLookup->getRevisionByTitle( $toTitle );
|
|
|
|
if ( !$toRev ) {
|
|
|
|
$this->dieWithError(
|
|
|
|
[ 'apierror-missingrev-title', wfEscapeWikiText( $toTitle->getPrefixedText() ) ],
|
|
|
|
'nosuchrevid'
|
|
|
|
);
|
|
|
|
}
|
2022-01-31 15:01:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// When polling for new comments this is an important optimisation,
|
|
|
|
// as usually there is no new revision.
|
|
|
|
if ( $toRev->getId() === $params['fromrev'] ) {
|
|
|
|
$this->addResult( $toRev, $toRev );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-02 01:49:44 +00:00
|
|
|
if ( isset( $params['fromrev'] ) ) {
|
|
|
|
$fromRev = $this->revisionLookup->getRevisionById( $params['fromrev'] );
|
|
|
|
if ( !$fromRev ) {
|
|
|
|
$this->dieWithError( [ 'apierror-nosuchrevid', $params['fromrev'] ] );
|
|
|
|
}
|
|
|
|
|
2022-01-31 15:01:32 +00:00
|
|
|
} else {
|
|
|
|
$fromTitle = Title::newFromText( $params['fromtitle'] );
|
|
|
|
if ( !$fromTitle ) {
|
|
|
|
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['fromtitle'] ) ] );
|
|
|
|
}
|
2022-09-02 01:49:44 +00:00
|
|
|
$fromRev = $this->revisionLookup->getRevisionByTitle( $fromTitle );
|
|
|
|
if ( !$fromRev ) {
|
|
|
|
$this->dieWithError(
|
|
|
|
[ 'apierror-missingrev-title', wfEscapeWikiText( $fromTitle->getPrefixedText() ) ],
|
|
|
|
'nosuchrevid'
|
|
|
|
);
|
|
|
|
}
|
2022-01-31 15:01:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $fromRev->hasSameContent( $toRev ) ) {
|
|
|
|
$this->addResult( $fromRev, $toRev );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-21 13:49:21 +00:00
|
|
|
$fromItemSet = HookUtils::parseRevisionParsoidHtml( $fromRev );
|
|
|
|
$toItemSet = HookUtils::parseRevisionParsoidHtml( $toRev );
|
2022-01-31 15:01:32 +00:00
|
|
|
|
|
|
|
$removedComments = [];
|
2022-02-19 06:31:34 +00:00
|
|
|
foreach ( $fromItemSet->getCommentItems() as $fromComment ) {
|
|
|
|
if ( !$toItemSet->findCommentById( $fromComment->getId() ) ) {
|
2022-01-31 15:01:32 +00:00
|
|
|
$removedComments[] = $fromComment->jsonSerializeForDiff();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$addedComments = [];
|
2022-02-19 06:31:34 +00:00
|
|
|
foreach ( $toItemSet->getCommentItems() as $toComment ) {
|
|
|
|
if ( !$fromItemSet->findCommentById( $toComment->getId() ) ) {
|
2022-01-31 15:01:32 +00:00
|
|
|
$addedComments[] = $toComment->jsonSerializeForDiff();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->addResult( $fromRev, $toRev, $removedComments, $addedComments );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add the result object from revisions and comment lists
|
|
|
|
*
|
|
|
|
* @param RevisionRecord $fromRev From revision
|
|
|
|
* @param RevisionRecord $toRev To revision
|
|
|
|
* @param array $removedComments Removed comments
|
|
|
|
* @param array $addedComments Added comments
|
|
|
|
*/
|
|
|
|
protected function addResult(
|
|
|
|
RevisionRecord $fromRev, RevisionRecord $toRev, array $removedComments = [], array $addedComments = []
|
|
|
|
) {
|
|
|
|
$fromTitle = Title::newFromLinkTarget(
|
|
|
|
$fromRev->getPageAsLinkTarget()
|
|
|
|
);
|
|
|
|
$toTitle = Title::newFromLinkTarget(
|
|
|
|
$toRev->getPageAsLinkTarget()
|
|
|
|
);
|
|
|
|
$result = [
|
|
|
|
'fromrevid' => $fromRev->getId(),
|
|
|
|
'fromtitle' => $fromTitle->getPrefixedText(),
|
|
|
|
'torevid' => $toRev->getId(),
|
|
|
|
'totitle' => $toTitle->getPrefixedText(),
|
|
|
|
'removedcomments' => $removedComments,
|
|
|
|
'addedcomments' => $addedComments,
|
|
|
|
];
|
|
|
|
$this->getResult()->addValue( null, $this->getModuleName(), $result );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getAllowedParams() {
|
|
|
|
return [
|
|
|
|
'fromtitle' => [
|
|
|
|
ApiBase::PARAM_HELP_MSG => 'apihelp-compare-param-fromtitle',
|
|
|
|
],
|
|
|
|
'fromrev' => [
|
|
|
|
ParamValidator::PARAM_TYPE => 'integer',
|
|
|
|
ApiBase::PARAM_HELP_MSG => 'apihelp-compare-param-fromrev',
|
|
|
|
],
|
|
|
|
'totitle' => [
|
|
|
|
ApiBase::PARAM_HELP_MSG => 'apihelp-compare-param-totitle',
|
|
|
|
],
|
|
|
|
'torev' => [
|
|
|
|
ParamValidator::PARAM_TYPE => 'integer',
|
|
|
|
ApiBase::PARAM_HELP_MSG => 'apihelp-compare-param-torev',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function needsToken() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function isInternal() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function isWriteMode() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|