| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Storage; |
| 22 |
|
| 23 |
use ChangeTags; |
| 24 |
use MediaWiki\ChangeTags\ChangeTagsStore; |
| 25 |
use MediaWiki\Config\ServiceOptions; |
| 26 |
use MediaWiki\Deferred\DeferrableUpdate; |
| 27 |
use MediaWiki\Json\FormatJson; |
| 28 |
use MediaWiki\MainConfigNames; |
| 29 |
use MediaWiki\Revision\RevisionRecord; |
| 30 |
use MediaWiki\Revision\RevisionStore; |
| 31 |
use Psr\Log\LoggerInterface; |
| 32 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class RevertedTagUpdate implements DeferrableUpdate { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public const CONSTRUCTOR_OPTIONS = [ MainConfigNames::RevertedTagMaxDepth ]; |
| 48 |
|
| 49 |
|
| 50 |
private $revisionStore; |
| 51 |
|
| 52 |
|
| 53 |
private $logger; |
| 54 |
|
| 55 |
|
| 56 |
private $dbProvider; |
| 57 |
|
| 58 |
|
| 59 |
private $options; |
| 60 |
|
| 61 |
|
| 62 |
private $revertId; |
| 63 |
|
| 64 |
|
| 65 |
private $editResult; |
| 66 |
|
| 67 |
|
| 68 |
private $revertRevision; |
| 69 |
|
| 70 |
|
| 71 |
private $newestRevertedRevision; |
| 72 |
|
| 73 |
|
| 74 |
private $oldestRevertedRevision; |
| 75 |
private ChangeTagsStore $changeTagsStore; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public function __construct( |
| 87 |
RevisionStore $revisionStore, |
| 88 |
LoggerInterface $logger, |
| 89 |
ChangeTagsStore $changeTagsStore, |
| 90 |
IConnectionProvider $dbProvider, |
| 91 |
ServiceOptions $serviceOptions, |
| 92 |
int $revertId, |
| 93 |
EditResult $editResult |
| 94 |
) { |
| 95 |
$serviceOptions->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 96 |
|
| 97 |
$this->revisionStore = $revisionStore; |
| 98 |
$this->logger = $logger; |
| 99 |
$this->dbProvider = $dbProvider; |
| 100 |
$this->options = $serviceOptions; |
| 101 |
$this->revertId = $revertId; |
| 102 |
$this->editResult = $editResult; |
| 103 |
$this->changeTagsStore = $changeTagsStore; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public function doUpdate() { |
| 110 |
|
| 111 |
if ( !$this->shouldExecute() ) { |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
if ( $this->handleSingleRevertedEdit() ) { |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
$maxDepth = $this->options->get( MainConfigNames::RevertedTagMaxDepth ); |
| 121 |
$extraParams = $this->getTagExtraParams(); |
| 122 |
$revertedRevisionIds = $this->revisionStore->getRevisionIdsBetween( |
| 123 |
$this->getOldestRevertedRevision()->getPageId(), |
| 124 |
$this->getOldestRevertedRevision(), |
| 125 |
$this->getNewestRevertedRevision(), |
| 126 |
$maxDepth, |
| 127 |
RevisionStore::INCLUDE_BOTH |
| 128 |
); |
| 129 |
|
| 130 |
if ( count( $revertedRevisionIds ) > $maxDepth ) { |
| 131 |
|
| 132 |
$this->logger->info( |
| 133 |
'The revert is deeper than $wgRevertedTagMaxDepth. Skipping...', |
| 134 |
$extraParams |
| 135 |
); |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
$revertedRevision = null; |
| 140 |
foreach ( $revertedRevisionIds as $revId ) { |
| 141 |
$previousRevision = $revertedRevision; |
| 142 |
|
| 143 |
$revertedRevision = $this->revisionStore->getRevisionById( $revId ); |
| 144 |
if ( $revertedRevision === null ) { |
| 145 |
|
| 146 |
continue; |
| 147 |
} |
| 148 |
|
| 149 |
$previousRevision ??= $this->revisionStore->getPreviousRevision( $revertedRevision ); |
| 150 |
if ( $previousRevision !== null && |
| 151 |
$revertedRevision->hasSameContent( $previousRevision ) |
| 152 |
) { |
| 153 |
|
| 154 |
|
| 155 |
continue; |
| 156 |
} |
| 157 |
$this->changeTagsStore->addTags( |
| 158 |
[ ChangeTags::TAG_REVERTED ], |
| 159 |
null, |
| 160 |
$revId, |
| 161 |
null, |
| 162 |
FormatJson::encode( $extraParams ) |
| 163 |
); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
private function shouldExecute(): bool { |
| 173 |
$maxDepth = $this->options->get( MainConfigNames::RevertedTagMaxDepth ); |
| 174 |
if ( |
| 175 |
!in_array( ChangeTags::TAG_REVERTED, $this->changeTagsStore->getSoftwareTags() ) || |
| 176 |
$maxDepth <= 0 |
| 177 |
) { |
| 178 |
return false; |
| 179 |
} |
| 180 |
|
| 181 |
$extraParams = $this->getTagExtraParams(); |
| 182 |
if ( !$this->editResult->isRevert() || |
| 183 |
$this->editResult->getOldestRevertedRevisionId() === null || |
| 184 |
$this->editResult->getNewestRevertedRevisionId() === null |
| 185 |
) { |
| 186 |
$this->logger->error( 'Invalid EditResult specified.', $extraParams ); |
| 187 |
return false; |
| 188 |
} |
| 189 |
|
| 190 |
if ( !$this->getOldestRevertedRevision() || !$this->getNewestRevertedRevision() ) { |
| 191 |
$this->logger->error( |
| 192 |
'Could not find the newest or oldest reverted revision in the database.', |
| 193 |
$extraParams |
| 194 |
); |
| 195 |
return false; |
| 196 |
} |
| 197 |
if ( !$this->getRevertRevision() ) { |
| 198 |
$this->logger->error( |
| 199 |
'Could not find the revert revision in the database.', |
| 200 |
$extraParams |
| 201 |
); |
| 202 |
return false; |
| 203 |
} |
| 204 |
|
| 205 |
if ( $this->getNewestRevertedRevision()->getPageId() !== |
| 206 |
$this->getOldestRevertedRevision()->getPageId() |
| 207 |
|| |
| 208 |
$this->getOldestRevertedRevision()->getPageId() !== |
| 209 |
$this->getRevertRevision()->getPageId() |
| 210 |
) { |
| 211 |
$this->logger->error( |
| 212 |
'The revert and reverted revisions belong to different pages.', |
| 213 |
$extraParams |
| 214 |
); |
| 215 |
return false; |
| 216 |
} |
| 217 |
|
| 218 |
if ( $this->getRevertRevision()->isDeleted( RevisionRecord::DELETED_TEXT ) ) { |
| 219 |
|
| 220 |
|
| 221 |
$this->logger->info( |
| 222 |
'The revert\'s text had been marked as deleted before the update was ' . |
| 223 |
'executed. Skipping...', |
| 224 |
$extraParams |
| 225 |
); |
| 226 |
return false; |
| 227 |
} |
| 228 |
|
| 229 |
$changeTagsOnRevert = $this->changeTagsStore->getTags( |
| 230 |
$this->dbProvider->getReplicaDatabase(), |
| 231 |
null, |
| 232 |
$this->revertId |
| 233 |
); |
| 234 |
if ( in_array( ChangeTags::TAG_REVERTED, $changeTagsOnRevert ) ) { |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
$this->logger->info( |
| 239 |
'The revert had been reverted before the update was executed. Skipping...', |
| 240 |
$extraParams |
| 241 |
); |
| 242 |
return false; |
| 243 |
} |
| 244 |
|
| 245 |
return true; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
private function handleSingleRevertedEdit(): bool { |
| 258 |
if ( $this->editResult->getOldestRevertedRevisionId() !== |
| 259 |
$this->editResult->getNewestRevertedRevisionId() |
| 260 |
) { |
| 261 |
return false; |
| 262 |
} |
| 263 |
|
| 264 |
$revertedRevision = $this->getOldestRevertedRevision(); |
| 265 |
if ( $revertedRevision === null || |
| 266 |
$revertedRevision->isDeleted( RevisionRecord::DELETED_TEXT ) |
| 267 |
) { |
| 268 |
return true; |
| 269 |
} |
| 270 |
|
| 271 |
$previousRevision = $this->revisionStore->getPreviousRevision( |
| 272 |
$revertedRevision |
| 273 |
); |
| 274 |
if ( $previousRevision !== null && |
| 275 |
$revertedRevision->hasSameContent( $previousRevision ) |
| 276 |
) { |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
return true; |
| 281 |
} |
| 282 |
$this->changeTagsStore->addTags( |
| 283 |
[ ChangeTags::TAG_REVERTED ], |
| 284 |
null, |
| 285 |
$this->editResult->getOldestRevertedRevisionId(), |
| 286 |
null, |
| 287 |
FormatJson::encode( $this->getTagExtraParams() ) |
| 288 |
); |
| 289 |
return true; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
private function getTagExtraParams(): array { |
| 300 |
return array_merge( |
| 301 |
[ 'revertId' => $this->revertId ], |
| 302 |
$this->editResult->jsonSerialize() |
| 303 |
); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
private function getRevertRevision(): ?RevisionRecord { |
| 312 |
if ( !isset( $this->revertRevision ) ) { |
| 313 |
$this->revertRevision = $this->revisionStore->getRevisionById( |
| 314 |
$this->revertId |
| 315 |
); |
| 316 |
} |
| 317 |
return $this->revertRevision; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
private function getNewestRevertedRevision(): ?RevisionRecord { |
| 326 |
if ( !isset( $this->newestRevertedRevision ) ) { |
| 327 |
$this->newestRevertedRevision = $this->revisionStore->getRevisionById( |
| 328 |
|
| 329 |
$this->editResult->getNewestRevertedRevisionId() |
| 330 |
); |
| 331 |
} |
| 332 |
return $this->newestRevertedRevision; |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
private function getOldestRevertedRevision(): ?RevisionRecord { |
| 341 |
if ( !isset( $this->oldestRevertedRevision ) ) { |
| 342 |
$this->oldestRevertedRevision = $this->revisionStore->getRevisionById( |
| 343 |
|
| 344 |
$this->editResult->getOldestRevertedRevisionId() |
| 345 |
); |
| 346 |
} |
| 347 |
return $this->oldestRevertedRevision; |
| 348 |
} |
| 349 |
} |