Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
97.66% |
125 / 128 |
|
75.00% |
6 / 8 |
CRAP | |
0.00% |
0 / 1 |
| RevertedTagUpdate | |
97.66% |
125 / 128 |
|
75.00% |
6 / 8 |
35 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| doUpdate | |
97.30% |
36 / 37 |
|
0.00% |
0 / 1 |
8 | |||
| shouldExecute | |
100.00% |
50 / 50 |
|
100.00% |
1 / 1 |
13 | |||
| handleSingleRevertedEdit | |
90.48% |
19 / 21 |
|
0.00% |
0 / 1 |
6.03 | |||
| getTagExtraParams | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| getRevertRevision | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| getNewestRevertedRevision | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| getOldestRevertedRevision | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\Storage; |
| 8 | |
| 9 | use MediaWiki\ChangeTags\ChangeTags; |
| 10 | use MediaWiki\ChangeTags\ChangeTagsStore; |
| 11 | use MediaWiki\Config\ServiceOptions; |
| 12 | use MediaWiki\Deferred\DeferrableUpdate; |
| 13 | use MediaWiki\Json\FormatJson; |
| 14 | use MediaWiki\MainConfigNames; |
| 15 | use MediaWiki\Revision\RevisionRecord; |
| 16 | use MediaWiki\Revision\RevisionStore; |
| 17 | use Psr\Log\LoggerInterface; |
| 18 | use Wikimedia\Rdbms\IConnectionProvider; |
| 19 | |
| 20 | /** |
| 21 | * Adds the mw-reverted tag to reverted edits after a revert is made. |
| 22 | * |
| 23 | * This class is used by RevertedTagUpdateJob to perform the actual update. |
| 24 | * |
| 25 | * @since 1.36 |
| 26 | * @author Ostrzyciel |
| 27 | */ |
| 28 | class RevertedTagUpdate implements DeferrableUpdate { |
| 29 | |
| 30 | /** |
| 31 | * @internal |
| 32 | */ |
| 33 | public const CONSTRUCTOR_OPTIONS = [ MainConfigNames::RevertedTagMaxDepth ]; |
| 34 | |
| 35 | /** @var RevisionRecord|null */ |
| 36 | private $revertRevision; |
| 37 | |
| 38 | /** @var RevisionRecord|null */ |
| 39 | private $newestRevertedRevision; |
| 40 | |
| 41 | /** @var RevisionRecord|null */ |
| 42 | private $oldestRevertedRevision; |
| 43 | |
| 44 | /** |
| 45 | * @param RevisionStore $revisionStore |
| 46 | * @param LoggerInterface $logger |
| 47 | * @param ChangeTagsStore $changeTagsStore |
| 48 | * @param IConnectionProvider $dbProvider |
| 49 | * @param ServiceOptions $options |
| 50 | * @param int $revertId ID of the revert |
| 51 | * @param EditResult $editResult EditResult object of this revert |
| 52 | */ |
| 53 | public function __construct( |
| 54 | private readonly RevisionStore $revisionStore, |
| 55 | private readonly LoggerInterface $logger, |
| 56 | private readonly ChangeTagsStore $changeTagsStore, |
| 57 | private readonly IConnectionProvider $dbProvider, |
| 58 | private readonly ServiceOptions $options, |
| 59 | private readonly int $revertId, |
| 60 | private readonly EditResult $editResult, |
| 61 | ) { |
| 62 | $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Marks reverted edits with `mw-reverted` tag. |
| 67 | */ |
| 68 | public function doUpdate() { |
| 69 | // Do extensive checks, as the update may be carried out even months after the edit |
| 70 | if ( !$this->shouldExecute() ) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | // Skip some of the DB code and just tag it if only one edit was reverted |
| 75 | if ( $this->handleSingleRevertedEdit() ) { |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | $maxDepth = $this->options->get( MainConfigNames::RevertedTagMaxDepth ); |
| 80 | $extraParams = $this->getTagExtraParams(); |
| 81 | $revertedRevisionIds = $this->revisionStore->getRevisionIdsBetween( |
| 82 | $this->getOldestRevertedRevision()->getPageId(), |
| 83 | $this->getOldestRevertedRevision(), |
| 84 | $this->getNewestRevertedRevision(), |
| 85 | $maxDepth, |
| 86 | [ RevisionStore::INCLUDE_BOTH, RevisionStore::INCLUDE_DELETED_REVISIONS ] |
| 87 | ); |
| 88 | |
| 89 | if ( count( $revertedRevisionIds ) > $maxDepth ) { |
| 90 | // This revert exceeds the depth limit |
| 91 | $this->logger->info( |
| 92 | 'The revert is deeper than $wgRevertedTagMaxDepth. Skipping...', |
| 93 | $extraParams |
| 94 | ); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | $revertedRevisions = $this->revisionStore->getRevisionsByIds( $revertedRevisionIds ); |
| 99 | |
| 100 | $currentRevision = null; |
| 101 | foreach ( $revertedRevisions as $revId => $revertedRevision ) { |
| 102 | $previousRevision = $currentRevision; |
| 103 | $currentRevision = $revertedRevision; |
| 104 | if ( $revertedRevision === null ) { |
| 105 | // Shouldn't happen, but necessary for static analysis |
| 106 | continue; |
| 107 | } |
| 108 | |
| 109 | $previousRevision ??= $this->revisionStore->getPreviousRevision( $revertedRevision ); |
| 110 | if ( $previousRevision !== null && |
| 111 | $revertedRevision->hasSameContent( $previousRevision ) |
| 112 | ) { |
| 113 | // This is a dummy revision (e.g. a page move or protection record) |
| 114 | // See: T265312 |
| 115 | continue; |
| 116 | } |
| 117 | $this->changeTagsStore->addTags( |
| 118 | [ ChangeTags::TAG_REVERTED ], |
| 119 | null, |
| 120 | $revId, |
| 121 | null, |
| 122 | FormatJson::encode( $extraParams ) |
| 123 | ); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Performs checks to determine whether the update should execute. |
| 129 | */ |
| 130 | private function shouldExecute(): bool { |
| 131 | $maxDepth = $this->options->get( MainConfigNames::RevertedTagMaxDepth ); |
| 132 | if ( |
| 133 | !in_array( ChangeTags::TAG_REVERTED, $this->changeTagsStore->getSoftwareTags() ) || |
| 134 | $maxDepth <= 0 |
| 135 | ) { |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | $extraParams = $this->getTagExtraParams(); |
| 140 | if ( !$this->editResult->isRevert() || |
| 141 | $this->editResult->getOldestRevertedRevisionId() === null || |
| 142 | $this->editResult->getNewestRevertedRevisionId() === null |
| 143 | ) { |
| 144 | $this->logger->error( 'Invalid EditResult specified.', $extraParams ); |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | if ( !$this->getOldestRevertedRevision() || !$this->getNewestRevertedRevision() ) { |
| 149 | $this->logger->error( |
| 150 | 'Could not find the newest or oldest reverted revision in the database.', |
| 151 | $extraParams |
| 152 | ); |
| 153 | return false; |
| 154 | } |
| 155 | if ( !$this->getRevertRevision() ) { |
| 156 | $this->logger->error( |
| 157 | 'Could not find the revert revision in the database.', |
| 158 | $extraParams |
| 159 | ); |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | if ( $this->getNewestRevertedRevision()->getPageId() !== |
| 164 | $this->getOldestRevertedRevision()->getPageId() |
| 165 | || |
| 166 | $this->getOldestRevertedRevision()->getPageId() !== |
| 167 | $this->getRevertRevision()->getPageId() |
| 168 | ) { |
| 169 | $this->logger->error( |
| 170 | 'The revert and reverted revisions belong to different pages.', |
| 171 | $extraParams |
| 172 | ); |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | if ( $this->getRevertRevision()->isDeleted( RevisionRecord::DELETED_TEXT ) ) { |
| 177 | // The revert's text is marked as deleted, which probably means the update |
| 178 | // shouldn't be executed. |
| 179 | $this->logger->info( |
| 180 | 'The revert\'s text had been marked as deleted before the update was ' . |
| 181 | 'executed. Skipping...', |
| 182 | $extraParams |
| 183 | ); |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | $changeTagsOnRevert = $this->changeTagsStore->getTags( |
| 188 | $this->dbProvider->getReplicaDatabase(), |
| 189 | null, |
| 190 | $this->revertId |
| 191 | ); |
| 192 | if ( in_array( ChangeTags::TAG_REVERTED, $changeTagsOnRevert ) ) { |
| 193 | // This is already marked as reverted, which means the update was delayed |
| 194 | // until the edit is approved. Apparently, the edit was not approved, as |
| 195 | // it was reverted, so the update should not be performed. |
| 196 | $this->logger->info( |
| 197 | 'The revert had been reverted before the update was executed. Skipping...', |
| 198 | $extraParams |
| 199 | ); |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Handles the case where only one edit was reverted. |
| 208 | * Returns true if the update was handled by this method, false otherwise. |
| 209 | * |
| 210 | * This is a much simpler case requiring less DB queries than when dealing with multiple |
| 211 | * reverted edits. |
| 212 | */ |
| 213 | private function handleSingleRevertedEdit(): bool { |
| 214 | if ( $this->editResult->getOldestRevertedRevisionId() !== |
| 215 | $this->editResult->getNewestRevertedRevisionId() |
| 216 | ) { |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | $revertedRevision = $this->getOldestRevertedRevision(); |
| 221 | if ( $revertedRevision === null || |
| 222 | $revertedRevision->isDeleted( RevisionRecord::DELETED_TEXT ) |
| 223 | ) { |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | $previousRevision = $this->revisionStore->getPreviousRevision( |
| 228 | $revertedRevision |
| 229 | ); |
| 230 | if ( $previousRevision !== null && |
| 231 | $revertedRevision->hasSameContent( $previousRevision ) |
| 232 | ) { |
| 233 | // Ignore the very rare case of a null edit. This should not occur unless |
| 234 | // someone does something weird with the page's history before the update |
| 235 | // is executed. |
| 236 | return true; |
| 237 | } |
| 238 | $this->changeTagsStore->addTags( |
| 239 | [ ChangeTags::TAG_REVERTED ], |
| 240 | null, |
| 241 | $this->editResult->getOldestRevertedRevisionId(), |
| 242 | null, |
| 243 | FormatJson::encode( $this->getTagExtraParams() ) |
| 244 | ); |
| 245 | return true; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Returns additional data to be saved in ct_params field of table 'change_tag'. |
| 250 | * |
| 251 | * Effectively a superset of what EditResult::jsonSerialize() returns. |
| 252 | */ |
| 253 | private function getTagExtraParams(): array { |
| 254 | return array_merge( |
| 255 | [ 'revertId' => $this->revertId ], |
| 256 | $this->editResult->jsonSerialize() |
| 257 | ); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Returns the revision that performed the revert. |
| 262 | * |
| 263 | * @return RevisionRecord|null |
| 264 | */ |
| 265 | private function getRevertRevision(): ?RevisionRecord { |
| 266 | if ( !$this->revertRevision ) { |
| 267 | $this->revertRevision = $this->revisionStore->getRevisionById( |
| 268 | $this->revertId |
| 269 | ); |
| 270 | } |
| 271 | return $this->revertRevision; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Returns the newest revision record that was reverted. |
| 276 | * |
| 277 | * @return RevisionRecord|null |
| 278 | */ |
| 279 | private function getNewestRevertedRevision(): ?RevisionRecord { |
| 280 | if ( !$this->newestRevertedRevision ) { |
| 281 | $this->newestRevertedRevision = $this->revisionStore->getRevisionById( |
| 282 | // @phan-suppress-next-line PhanTypeMismatchArgumentNullable newestRevertedRevision is checked |
| 283 | $this->editResult->getNewestRevertedRevisionId() |
| 284 | ); |
| 285 | } |
| 286 | return $this->newestRevertedRevision; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Returns the oldest revision record that was reverted. |
| 291 | * |
| 292 | * @return RevisionRecord|null |
| 293 | */ |
| 294 | private function getOldestRevertedRevision(): ?RevisionRecord { |
| 295 | if ( !$this->oldestRevertedRevision ) { |
| 296 | $this->oldestRevertedRevision = $this->revisionStore->getRevisionById( |
| 297 | // @phan-suppress-next-line PhanTypeMismatchArgumentNullable oldestRevertedRevision is checked |
| 298 | $this->editResult->getOldestRevertedRevisionId() |
| 299 | ); |
| 300 | } |
| 301 | return $this->oldestRevertedRevision; |
| 302 | } |
| 303 | } |