Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
88.24% |
60 / 68 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
ORESRecentChangeScoreSavedHookHandler | |
88.24% |
60 / 68 |
|
50.00% |
1 / 2 |
13.28 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
onORESRecentChangeScoreSavedHook | |
86.21% |
50 / 58 |
|
0.00% |
0 / 1 |
12.38 |
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 | private Config $wikiConfig; |
25 | |
26 | private UserGroupManager $userGroupManager; |
27 | |
28 | private Config $config; |
29 | |
30 | private WikiPageFactory $wikiPageFactory; |
31 | |
32 | private RevisionStore $revisionStore; |
33 | |
34 | private RestrictionStore $restrictionStore; |
35 | |
36 | private JobQueueGroup $jobQueueGroup; |
37 | |
38 | private ChangeTagsStore $changeTagsStore; |
39 | |
40 | private PermissionManager $permissionManager; |
41 | |
42 | private IConnectionProvider $connectionProvider; |
43 | |
44 | /** |
45 | * @param Config $wikiConfig |
46 | * @param UserGroupManager $userGroupManager |
47 | * @param Config $config |
48 | * @param WikiPageFactory $wikiPageFactory |
49 | * @param RevisionStore $revisionStore |
50 | * @param RestrictionStore $restrictionStore |
51 | * @param JobQueueGroup $jobQueueGroup |
52 | * @param ChangeTagsStore $changeTagsStore |
53 | * @param PermissionManager $permissionManager |
54 | * @param IConnectionProvider $connectionProvider |
55 | */ |
56 | public function __construct( |
57 | Config $wikiConfig, |
58 | UserGroupManager $userGroupManager, |
59 | Config $config, |
60 | WikiPageFactory $wikiPageFactory, |
61 | RevisionStore $revisionStore, |
62 | RestrictionStore $restrictionStore, |
63 | JobQueueGroup $jobQueueGroup, |
64 | ChangeTagsStore $changeTagsStore, |
65 | PermissionManager $permissionManager, |
66 | IConnectionProvider $connectionProvider |
67 | ) { |
68 | $this->wikiConfig = $wikiConfig; |
69 | $this->userGroupManager = $userGroupManager; |
70 | $this->config = $config; |
71 | $this->wikiPageFactory = $wikiPageFactory; |
72 | $this->revisionStore = $revisionStore; |
73 | $this->restrictionStore = $restrictionStore; |
74 | $this->jobQueueGroup = $jobQueueGroup; |
75 | $this->changeTagsStore = $changeTagsStore; |
76 | $this->permissionManager = $permissionManager; |
77 | $this->connectionProvider = $connectionProvider; |
78 | } |
79 | |
80 | /** |
81 | * @param RevisionRecord|null $revision |
82 | * @param array $scores |
83 | */ |
84 | public function onORESRecentChangeScoreSavedHook( $revision, $scores ) { |
85 | if ( !$revision || !$scores ) { |
86 | return; |
87 | } |
88 | $revisionCheckNotEnabled = $this->wikiConfig->has( 'AutoModeratorEnableRevisionCheck' ) && |
89 | !$this->wikiConfig->get( 'AutoModeratorEnableRevisionCheck' ); |
90 | $multilingualRevisionCheckNotEnabled = $this->wikiConfig |
91 | ->has( 'AutoModeratorMultilingualConfigEnableRevisionCheck' ) |
92 | && !$this->wikiConfig->get( 'AutoModeratorMultilingualConfigEnableRevisionCheck' ); |
93 | if ( $revisionCheckNotEnabled && $multilingualRevisionCheckNotEnabled ) { |
94 | return; |
95 | } |
96 | $user = $revision->getUser(); |
97 | if ( !$user ) { |
98 | return; |
99 | } |
100 | $wikiPageId = $revision->getPageId(); |
101 | if ( !$wikiPageId ) { |
102 | return; |
103 | } |
104 | $wikiPage = $this->wikiPageFactory->newFromID( $wikiPageId ); |
105 | if ( !$wikiPage ) { |
106 | return; |
107 | } |
108 | |
109 | $title = $wikiPage->getTitle(); |
110 | $revId = $revision->getId(); |
111 | $dbr = $this->connectionProvider->getReplicaDatabase(); |
112 | $tags = $this->changeTagsStore->getTags( $dbr, null, $revId, null ); |
113 | $logger = LoggerFactory::getInstance( 'AutoModerator' ); |
114 | $autoModeratorUser = Util::getAutoModeratorUser( $this->config, $this->userGroupManager ); |
115 | $userId = $user->getId(); |
116 | if ( !RevisionCheck::revertPreCheck( |
117 | $user, |
118 | $autoModeratorUser, |
119 | $logger, |
120 | $this->revisionStore, |
121 | $tags, |
122 | $this->restrictionStore, |
123 | $this->wikiPageFactory, |
124 | $this->wikiConfig, |
125 | $revision, |
126 | $this->permissionManager ) ) { |
127 | return; |
128 | } |
129 | |
130 | $job = new AutoModeratorFetchRevScoreJob( $title, |
131 | [ |
132 | 'wikiPageId' => $wikiPageId, |
133 | 'revId' => $revId, |
134 | 'originalRevId' => false, |
135 | // The test/production environments do not work when you pass the entire User object. |
136 | // To get around this, we have split the required parameters from the User object |
137 | // into individual parameters so that the test/production Job constructor will accept them. |
138 | 'userId' => $userId, |
139 | 'userName' => $user->getName(), |
140 | 'tags' => $tags, |
141 | // The score will be evaluated in the job to see whether the revision should be reverted or not |
142 | 'scores' => $scores |
143 | ] |
144 | ); |
145 | try { |
146 | $this->jobQueueGroup->lazyPush( $job ); |
147 | $logger->debug( 'Job pushed for {rev}', [ |
148 | 'rev' => $revId, |
149 | ] ); |
150 | } catch ( Exception $e ) { |
151 | $msg = $e->getMessage(); |
152 | $logger->error( 'Job push failed for {rev}: {msg}', [ |
153 | 'rev' => $revId, |
154 | 'msg' => $msg |
155 | ] ); |
156 | } |
157 | } |
158 | } |