Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
71.79% |
56 / 78 |
|
62.50% |
5 / 8 |
CRAP | |
0.00% |
0 / 1 |
| ApiDiscussionToolsCompare | |
71.79% |
56 / 78 |
|
62.50% |
5 / 8 |
30.90 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| getRevision | |
31.25% |
5 / 16 |
|
0.00% |
0 / 1 |
17.70 | |||
| execute | |
64.29% |
18 / 28 |
|
0.00% |
0 / 1 |
12.69 | |||
| addResult | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 | |||
| getAllowedParams | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
1 | |||
| needsToken | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isInternal | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isWriteMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Extension\DiscussionTools; |
| 4 | |
| 5 | use MediaWiki\Api\ApiBase; |
| 6 | use MediaWiki\Api\ApiMain; |
| 7 | use MediaWiki\Api\ApiUsageException; |
| 8 | use MediaWiki\Extension\DiscussionTools\Hooks\HookUtils; |
| 9 | use MediaWiki\Extension\VisualEditor\VisualEditorParsoidClientFactory; |
| 10 | use MediaWiki\Revision\RevisionLookup; |
| 11 | use MediaWiki\Revision\RevisionRecord; |
| 12 | use MediaWiki\Title\Title; |
| 13 | use Wikimedia\ParamValidator\ParamValidator; |
| 14 | |
| 15 | class ApiDiscussionToolsCompare extends ApiBase { |
| 16 | |
| 17 | private CommentParser $commentParser; |
| 18 | private VisualEditorParsoidClientFactory $parsoidClientFactory; |
| 19 | private RevisionLookup $revisionLookup; |
| 20 | |
| 21 | public function __construct( |
| 22 | ApiMain $main, |
| 23 | string $name, |
| 24 | VisualEditorParsoidClientFactory $parsoidClientFactory, |
| 25 | CommentParser $commentParser, |
| 26 | RevisionLookup $revisionLookup |
| 27 | ) { |
| 28 | parent::__construct( $main, $name ); |
| 29 | $this->parsoidClientFactory = $parsoidClientFactory; |
| 30 | $this->commentParser = $commentParser; |
| 31 | $this->revisionLookup = $revisionLookup; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @throws ApiUsageException |
| 36 | */ |
| 37 | private function getRevision( array $params, string $prefix ): RevisionRecord { |
| 38 | if ( isset( $params["{$prefix}rev"] ) ) { |
| 39 | $rev = $this->revisionLookup->getRevisionById( $params["{$prefix}rev"] ); |
| 40 | if ( !$rev ) { |
| 41 | $this->dieWithError( [ 'apierror-nosuchrevid', $params["{$prefix}rev"] ] ); |
| 42 | } |
| 43 | |
| 44 | } else { |
| 45 | $title = Title::newFromText( $params["{$prefix}title"] ); |
| 46 | if ( !$title ) { |
| 47 | $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params["{$prefix}title"] ) ] ); |
| 48 | } |
| 49 | $rev = $this->revisionLookup->getRevisionByTitle( $title ); |
| 50 | if ( !$rev ) { |
| 51 | $this->dieWithError( |
| 52 | [ 'apierror-missingrev-title', wfEscapeWikiText( $title->getPrefixedText() ) ], |
| 53 | 'nosuchrevid' |
| 54 | ); |
| 55 | } |
| 56 | } |
| 57 | // To keep things simple, don't allow viewing deleted revisions through this API |
| 58 | // (even if the current user could view them if we checked with userCan()). |
| 59 | if ( !$rev->audienceCan( RevisionRecord::DELETED_TEXT, RevisionRecord::FOR_PUBLIC ) ) { |
| 60 | $this->dieWithError( [ 'apierror-missingcontent-revid', $rev->getId() ], 'missingcontent' ); |
| 61 | } |
| 62 | return $rev; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @inheritDoc |
| 67 | * @throws ApiUsageException |
| 68 | */ |
| 69 | public function execute() { |
| 70 | $params = $this->extractRequestParams(); |
| 71 | |
| 72 | $this->requireOnlyOneParameter( $params, 'fromtitle', 'fromrev' ); |
| 73 | $this->requireOnlyOneParameter( $params, 'totitle', 'torev' ); |
| 74 | |
| 75 | $toRev = $this->getRevision( $params, 'to' ); |
| 76 | |
| 77 | // When polling for new comments this is an important optimisation, |
| 78 | // as usually there is no new revision. |
| 79 | if ( $toRev->getId() === $params['fromrev'] ) { |
| 80 | $this->addResult( $toRev, $toRev ); |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | $fromRev = $this->getRevision( $params, 'from' ); |
| 85 | |
| 86 | if ( $fromRev->hasSameContent( $toRev ) ) { |
| 87 | $this->addResult( $fromRev, $toRev ); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | $fromStatus = HookUtils::parseRevisionParsoidHtml( $fromRev, __METHOD__ ); |
| 92 | $toStatus = HookUtils::parseRevisionParsoidHtml( $toRev, __METHOD__ ); |
| 93 | if ( !$fromStatus->isOK() ) { |
| 94 | $this->dieStatus( $fromStatus ); |
| 95 | } |
| 96 | if ( !$toStatus->isOK() ) { |
| 97 | $this->dieStatus( $toStatus ); |
| 98 | } |
| 99 | $fromItemSet = $fromStatus->getValueOrThrow(); |
| 100 | $toItemSet = $toStatus->getValueOrThrow(); |
| 101 | |
| 102 | $removedComments = []; |
| 103 | foreach ( $fromItemSet->getCommentItems() as $fromComment ) { |
| 104 | if ( !$toItemSet->findCommentById( $fromComment->getId() ) ) { |
| 105 | $removedComments[] = $fromComment->jsonSerializeForDiff(); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | $addedComments = []; |
| 110 | foreach ( $toItemSet->getCommentItems() as $toComment ) { |
| 111 | if ( !$fromItemSet->findCommentById( $toComment->getId() ) ) { |
| 112 | $addedComments[] = $toComment->jsonSerializeForDiff(); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | $this->addResult( $fromRev, $toRev, $removedComments, $addedComments ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Add the result object from revisions and comment lists |
| 121 | * |
| 122 | * @param RevisionRecord $fromRev From revision |
| 123 | * @param RevisionRecord $toRev To revision |
| 124 | * @param array $removedComments Removed comments |
| 125 | * @param array $addedComments Added comments |
| 126 | */ |
| 127 | protected function addResult( |
| 128 | RevisionRecord $fromRev, RevisionRecord $toRev, array $removedComments = [], array $addedComments = [] |
| 129 | ) { |
| 130 | $fromTitle = Title::newFromPageIdentity( $fromRev->getPage() ); |
| 131 | $toTitle = Title::newFromPageIdentity( $toRev->getPage() ); |
| 132 | $result = [ |
| 133 | 'fromrevid' => $fromRev->getId(), |
| 134 | 'fromtitle' => $fromTitle->getPrefixedText(), |
| 135 | 'torevid' => $toRev->getId(), |
| 136 | 'totitle' => $toTitle->getPrefixedText(), |
| 137 | 'removedcomments' => $removedComments, |
| 138 | 'addedcomments' => $addedComments, |
| 139 | ]; |
| 140 | $this->getResult()->addValue( null, $this->getModuleName(), $result ); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @inheritDoc |
| 145 | */ |
| 146 | public function getAllowedParams() { |
| 147 | return [ |
| 148 | 'fromtitle' => [ |
| 149 | ApiBase::PARAM_HELP_MSG => 'apihelp-compare-param-fromtitle', |
| 150 | ], |
| 151 | 'fromrev' => [ |
| 152 | ParamValidator::PARAM_TYPE => 'integer', |
| 153 | ApiBase::PARAM_HELP_MSG => 'apihelp-compare-param-fromrev', |
| 154 | ], |
| 155 | 'totitle' => [ |
| 156 | ApiBase::PARAM_HELP_MSG => 'apihelp-compare-param-totitle', |
| 157 | ], |
| 158 | 'torev' => [ |
| 159 | ParamValidator::PARAM_TYPE => 'integer', |
| 160 | ApiBase::PARAM_HELP_MSG => 'apihelp-compare-param-torev', |
| 161 | ], |
| 162 | ]; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * @inheritDoc |
| 167 | */ |
| 168 | public function needsToken() { |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * @inheritDoc |
| 174 | */ |
| 175 | public function isInternal() { |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * @inheritDoc |
| 181 | */ |
| 182 | public function isWriteMode() { |
| 183 | return false; |
| 184 | } |
| 185 | } |