assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); $this->revisionStore = $revisionStore; $this->logger = $logger; $this->dbProvider = $dbProvider; $this->options = $serviceOptions; $this->revertId = $revertId; $this->editResult = $editResult; $this->changeTagsStore = $changeTagsStore; } /** * Marks reverted edits with `mw-reverted` tag. */ public function doUpdate() { // Do extensive checks, as the update may be carried out even months after the edit if ( !$this->shouldExecute() ) { return; } // Skip some of the DB code and just tag it if only one edit was reverted if ( $this->handleSingleRevertedEdit() ) { return; } $maxDepth = $this->options->get( MainConfigNames::RevertedTagMaxDepth ); $extraParams = $this->getTagExtraParams(); $revertedRevisionIds = $this->revisionStore->getRevisionIdsBetween( $this->getOldestRevertedRevision()->getPageId(), $this->getOldestRevertedRevision(), $this->getNewestRevertedRevision(), $maxDepth, RevisionStore::INCLUDE_BOTH ); if ( count( $revertedRevisionIds ) > $maxDepth ) { // This revert exceeds the depth limit $this->logger->info( 'The revert is deeper than $wgRevertedTagMaxDepth. Skipping...', $extraParams ); return; } $revertedRevision = null; foreach ( $revertedRevisionIds as $revId ) { $previousRevision = $revertedRevision; $revertedRevision = $this->revisionStore->getRevisionById( $revId ); if ( $revertedRevision === null ) { // Shouldn't happen, but necessary for static analysis continue; } $previousRevision ??= $this->revisionStore->getPreviousRevision( $revertedRevision ); if ( $previousRevision !== null && $revertedRevision->hasSameContent( $previousRevision ) ) { // This is a null revision (e.g. a page move or protection record) // See: T265312 continue; } $this->changeTagsStore->addTags( [ ChangeTags::TAG_REVERTED ], null, $revId, null, FormatJson::encode( $extraParams ) ); } } /** * Performs checks to determine whether the update should execute. * * @return bool */ private function shouldExecute(): bool { $maxDepth = $this->options->get( MainConfigNames::RevertedTagMaxDepth ); if ( !in_array( ChangeTags::TAG_REVERTED, $this->changeTagsStore->getSoftwareTags() ) || $maxDepth <= 0 ) { return false; } $extraParams = $this->getTagExtraParams(); if ( !$this->editResult->isRevert() || $this->editResult->getOldestRevertedRevisionId() === null || $this->editResult->getNewestRevertedRevisionId() === null ) { $this->logger->error( 'Invalid EditResult specified.', $extraParams ); return false; } if ( !$this->getOldestRevertedRevision() || !$this->getNewestRevertedRevision() ) { $this->logger->error( 'Could not find the newest or oldest reverted revision in the database.', $extraParams ); return false; } if ( !$this->getRevertRevision() ) { $this->logger->error( 'Could not find the revert revision in the database.', $extraParams ); return false; } if ( $this->getNewestRevertedRevision()->getPageId() !== $this->getOldestRevertedRevision()->getPageId() || $this->getOldestRevertedRevision()->getPageId() !== $this->getRevertRevision()->getPageId() ) { $this->logger->error( 'The revert and reverted revisions belong to different pages.', $extraParams ); return false; } if ( $this->getRevertRevision()->isDeleted( RevisionRecord::DELETED_TEXT ) ) { // The revert's text is marked as deleted, which probably means the update // shouldn't be executed. $this->logger->info( 'The revert\'s text had been marked as deleted before the update was ' . 'executed. Skipping...', $extraParams ); return false; } $changeTagsOnRevert = $this->changeTagsStore->getTags( $this->dbProvider->getReplicaDatabase(), null, $this->revertId ); if ( in_array( ChangeTags::TAG_REVERTED, $changeTagsOnRevert ) ) { // This is already marked as reverted, which means the update was delayed // until the edit is approved. Apparently, the edit was not approved, as // it was reverted, so the update should not be performed. $this->logger->info( 'The revert had been reverted before the update was executed. Skipping...', $extraParams ); return false; } return true; } /** * Handles the case where only one edit was reverted. * Returns true if the update was handled by this method, false otherwise. * * This is a much simpler case requiring less DB queries than when dealing with multiple * reverted edits. * * @return bool */ private function handleSingleRevertedEdit(): bool { if ( $this->editResult->getOldestRevertedRevisionId() !== $this->editResult->getNewestRevertedRevisionId() ) { return false; } $revertedRevision = $this->getOldestRevertedRevision(); if ( $revertedRevision === null || $revertedRevision->isDeleted( RevisionRecord::DELETED_TEXT ) ) { return true; } $previousRevision = $this->revisionStore->getPreviousRevision( $revertedRevision ); if ( $previousRevision !== null && $revertedRevision->hasSameContent( $previousRevision ) ) { // Ignore the very rare case of a null edit. This should not occur unless // someone does something weird with the page's history before the update // is executed. return true; } $this->changeTagsStore->addTags( [ ChangeTags::TAG_REVERTED ], null, $this->editResult->getOldestRevertedRevisionId(), null, FormatJson::encode( $this->getTagExtraParams() ) ); return true; } /** * Returns additional data to be saved in ct_params field of table 'change_tag'. * * Effectively a superset of what EditResult::jsonSerialize() returns. * * @return array */ private function getTagExtraParams(): array { return array_merge( [ 'revertId' => $this->revertId ], $this->editResult->jsonSerialize() ); } /** * Returns the revision that performed the revert. * * @return RevisionRecord|null */ private function getRevertRevision(): ?RevisionRecord { if ( !isset( $this->revertRevision ) ) { $this->revertRevision = $this->revisionStore->getRevisionById( $this->revertId ); } return $this->revertRevision; } /** * Returns the newest revision record that was reverted. * * @return RevisionRecord|null */ private function getNewestRevertedRevision(): ?RevisionRecord { if ( !isset( $this->newestRevertedRevision ) ) { $this->newestRevertedRevision = $this->revisionStore->getRevisionById( // @phan-suppress-next-line PhanTypeMismatchArgumentNullable newestRevertedRevision is checked $this->editResult->getNewestRevertedRevisionId() ); } return $this->newestRevertedRevision; } /** * Returns the oldest revision record that was reverted. * * @return RevisionRecord|null */ private function getOldestRevertedRevision(): ?RevisionRecord { if ( !isset( $this->oldestRevertedRevision ) ) { $this->oldestRevertedRevision = $this->revisionStore->getRevisionById( // @phan-suppress-next-line PhanTypeMismatchArgumentNullable oldestRevertedRevision is checked $this->editResult->getOldestRevertedRevisionId() ); } return $this->oldestRevertedRevision; } }