Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.19% |
46 / 54 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ORESRecentChangeScoreSavedHookHandler | |
85.19% |
46 / 54 |
|
50.00% |
1 / 2 |
10.33 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| onORESRecentChangeScoreSavedHook | |
84.91% |
45 / 53 |
|
0.00% |
0 / 1 |
9.28 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace AutoModerator\Hooks; |
| 4 | |
| 5 | use AutoModerator\RevisionCheck; |
| 6 | use AutoModerator\Services\AutoModeratorFetchRevScoreJob; |
| 7 | use AutoModerator\Util; |
| 8 | use Exception; |
| 9 | use MediaWiki\ChangeTags\ChangeTagsStore; |
| 10 | use MediaWiki\Config\Config; |
| 11 | use MediaWiki\JobQueue\JobQueueGroup; |
| 12 | use MediaWiki\Logger\LoggerFactory; |
| 13 | use MediaWiki\Page\WikiPageFactory; |
| 14 | use MediaWiki\Permissions\PermissionManager; |
| 15 | use MediaWiki\Permissions\RestrictionStore; |
| 16 | use MediaWiki\Revision\RevisionRecord; |
| 17 | use MediaWiki\Revision\RevisionStore; |
| 18 | use MediaWiki\User\UserGroupManager; |
| 19 | use ORES\Hooks\ORESRecentChangeScoreSavedHook; |
| 20 | use Wikimedia\Rdbms\IConnectionProvider; |
| 21 | |
| 22 | class ORESRecentChangeScoreSavedHookHandler implements ORESRecentChangeScoreSavedHook { |
| 23 | |
| 24 | public function __construct( |
| 25 | private readonly UserGroupManager $userGroupManager, |
| 26 | private readonly Config $config, |
| 27 | private readonly WikiPageFactory $wikiPageFactory, |
| 28 | private readonly RevisionStore $revisionStore, |
| 29 | private readonly RestrictionStore $restrictionStore, |
| 30 | private readonly JobQueueGroup $jobQueueGroup, |
| 31 | private readonly ChangeTagsStore $changeTagsStore, |
| 32 | private readonly PermissionManager $permissionManager, |
| 33 | private readonly IConnectionProvider $connectionProvider, |
| 34 | ) { |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @param RevisionRecord|null $revision |
| 39 | * @param array $scores |
| 40 | */ |
| 41 | public function onORESRecentChangeScoreSavedHook( $revision, $scores ) { |
| 42 | if ( !$revision || !$scores ) { |
| 43 | return; |
| 44 | } |
| 45 | if ( !Util::getEnableRevisionCheck( $this->config ) ) { |
| 46 | return; |
| 47 | } |
| 48 | $user = $revision->getUser(); |
| 49 | if ( !$user ) { |
| 50 | return; |
| 51 | } |
| 52 | $wikiPageId = $revision->getPageId(); |
| 53 | if ( !$wikiPageId ) { |
| 54 | return; |
| 55 | } |
| 56 | $wikiPage = $this->wikiPageFactory->newFromID( $wikiPageId ); |
| 57 | if ( !$wikiPage ) { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | $title = $wikiPage->getTitle(); |
| 62 | $revId = $revision->getId(); |
| 63 | $dbr = $this->connectionProvider->getReplicaDatabase(); |
| 64 | $tags = $this->changeTagsStore->getTags( $dbr, null, $revId, null ); |
| 65 | $logger = LoggerFactory::getInstance( 'AutoModerator' ); |
| 66 | $autoModeratorUser = Util::getAutoModeratorUser( $this->config, $this->userGroupManager ); |
| 67 | $userId = $user->getId(); |
| 68 | if ( !RevisionCheck::revertPreCheck( |
| 69 | $user, |
| 70 | $autoModeratorUser, |
| 71 | $logger, |
| 72 | $this->revisionStore, |
| 73 | $tags, |
| 74 | $this->restrictionStore, |
| 75 | $this->wikiPageFactory, |
| 76 | $this->config, |
| 77 | $revision, |
| 78 | $this->permissionManager ) ) { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | $job = new AutoModeratorFetchRevScoreJob( $title, |
| 83 | [ |
| 84 | 'wikiPageId' => $wikiPageId, |
| 85 | 'revId' => $revId, |
| 86 | 'originalRevId' => false, |
| 87 | // The test/production environments do not work when you pass the entire User object. |
| 88 | // To get around this, we have split the required parameters from the User object |
| 89 | // into individual parameters so that the test/production Job constructor will accept them. |
| 90 | 'userId' => $userId, |
| 91 | 'userName' => $user->getName(), |
| 92 | 'tags' => $tags, |
| 93 | // The score will be evaluated in the job to see whether the revision should be reverted or not |
| 94 | 'scores' => $scores |
| 95 | ] |
| 96 | ); |
| 97 | try { |
| 98 | $this->jobQueueGroup->lazyPush( $job ); |
| 99 | $logger->debug( 'Job pushed for {rev}', [ |
| 100 | 'rev' => $revId, |
| 101 | ] ); |
| 102 | } catch ( Exception $e ) { |
| 103 | $msg = $e->getMessage(); |
| 104 | $logger->error( 'Job push failed for {rev}: {msg}', [ |
| 105 | 'rev' => $revId, |
| 106 | 'msg' => $msg |
| 107 | ] ); |
| 108 | } |
| 109 | } |
| 110 | } |