Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
90.70% |
78 / 86 |
|
62.50% |
5 / 8 |
CRAP | |
0.00% |
0 / 1 |
RevisionCheck | |
90.70% |
78 / 86 |
|
62.50% |
5 / 8 |
34.93 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
doRollback | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
revertPreCheck | |
96.30% |
52 / 54 |
|
0.00% |
0 / 1 |
18 | |||
maybeRollback | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
8 | |||
shouldSkipUser | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
areUsersEqual | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isProtectedPage | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
isNewPageCreation | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
16 | * |
17 | * @file |
18 | */ |
19 | |
20 | namespace AutoModerator; |
21 | |
22 | use AutoModerator\Services\AutoModeratorRollback; |
23 | use MediaWiki\ChangeTags\ChangeTags; |
24 | use MediaWiki\Config\Config; |
25 | use MediaWiki\Page\WikiPage; |
26 | use MediaWiki\Page\WikiPageFactory; |
27 | use MediaWiki\Permissions\PermissionManager; |
28 | use MediaWiki\Permissions\RestrictionStore; |
29 | use MediaWiki\Revision\RevisionRecord; |
30 | use MediaWiki\Revision\RevisionStore; |
31 | use MediaWiki\User\ExternalUserNames; |
32 | use MediaWiki\User\User; |
33 | use MediaWiki\User\UserIdentity; |
34 | use Psr\Log\LoggerInterface; |
35 | use StatusValue; |
36 | |
37 | class RevisionCheck { |
38 | |
39 | /** @var Config */ |
40 | private Config $wikiConfig; |
41 | |
42 | /** @var Config */ |
43 | private Config $config; |
44 | |
45 | /** @var bool */ |
46 | private bool $enforce; |
47 | |
48 | /** @var AutoModeratorRollback */ |
49 | private AutoModeratorRollback $rollbackPage; |
50 | |
51 | /** |
52 | * @param Config $wikiConfig |
53 | * @param Config $config |
54 | * @param AutoModeratorRollback $rollbackPage |
55 | * @param bool $enforce Perform reverts if true, take no action if false |
56 | */ |
57 | public function __construct( |
58 | Config $wikiConfig, |
59 | Config $config, |
60 | AutoModeratorRollback $rollbackPage, |
61 | bool $enforce = false |
62 | ) { |
63 | $this->wikiConfig = $wikiConfig; |
64 | $this->config = $config; |
65 | $this->enforce = $enforce; |
66 | $this->rollbackPage = $rollbackPage; |
67 | } |
68 | |
69 | /** |
70 | * Perform rollback |
71 | */ |
72 | private function doRollback(): StatusValue { |
73 | return $this->rollbackPage |
74 | ->rollback(); |
75 | } |
76 | |
77 | /** |
78 | * Precheck a revision; if any of the checks don't pass, |
79 | * a revision won't be scored |
80 | * @param UserIdentity $user |
81 | * @param User $autoModeratorUser |
82 | * @param LoggerInterface $logger |
83 | * @param RevisionStore $revisionStore |
84 | * @param string[] $tags |
85 | * @param RestrictionStore $restrictionStore |
86 | * @param WikiPageFactory $wikiPageFactory |
87 | * @param Config $wikiConfig |
88 | * @param RevisionRecord $rev |
89 | * @param PermissionManager $permissionManager |
90 | * @return bool |
91 | */ |
92 | public static function revertPreCheck( UserIdentity $user, User $autoModeratorUser, LoggerInterface $logger, |
93 | RevisionStore $revisionStore, array $tags, RestrictionStore $restrictionStore, WikiPageFactory $wikiPageFactory, |
94 | Config $wikiConfig, RevisionRecord $rev, PermissionManager $permissionManager |
95 | ): bool { |
96 | $wikiPageId = $rev->getPageId(); |
97 | // Skips reverts if AutoModerator is blocked |
98 | $autoModeratorBlock = $autoModeratorUser->getBlock(); |
99 | if ( $autoModeratorBlock && $autoModeratorBlock->appliesToPage( $wikiPageId ) ) { |
100 | $logger->debug( __METHOD__ . ': AutoModerator skip rev - AutoModerator is blocked' ); |
101 | return false; |
102 | } |
103 | // Skip AutoModerator edits |
104 | if ( self::areUsersEqual( $user, $autoModeratorUser ) ) { |
105 | $logger->debug( __METHOD__ . ': AutoModerator skip rev - AutoMod edits' ); |
106 | return false; |
107 | } |
108 | $parentId = $rev->getParentId(); |
109 | // Skip new page creations |
110 | if ( self::isNewPageCreation( $parentId ) ) { |
111 | $logger->debug( __METHOD__ . ': AutoModerator skip rev - new page creation' ); |
112 | return false; |
113 | } |
114 | // Skip already reverted edits |
115 | if ( in_array( ChangeTags::TAG_REVERTED, $tags ) ) { |
116 | $logger->debug( __METHOD__ . ': AutoModerator skip rev - already reverted' ); |
117 | return false; |
118 | } |
119 | // Skip reverts made to an AutoModerator bot revert or if |
120 | // the user reverts their own edit |
121 | if ( array_intersect( $tags, ChangeTags::REVERT_TAGS ) ) { |
122 | $parentRev = $revisionStore->getRevisionById( $parentId ); |
123 | if ( !$parentRev ) { |
124 | $logger->debug( __METHOD__ . ': AutoModerator skip rev - parent revision not found' ); |
125 | return false; |
126 | } |
127 | $parentRevUser = $parentRev->getUser(); |
128 | if ( $parentRevUser === null ) { |
129 | $logger->debug( __METHOD__ . ': AutoModerator skip rev - parent revision user is null' ); |
130 | return false; |
131 | } |
132 | if ( self::areUsersEqual( $parentRevUser, $autoModeratorUser ) ) { |
133 | $logger->debug( __METHOD__ . ': AutoModerator skip rev - AutoModerator reverts' ); |
134 | return false; |
135 | } |
136 | if ( self::areUsersEqual( $parentRevUser, $user ) ) { |
137 | $logger->debug( __METHOD__ . ': AutoModerator skip rev - own reverts' ); |
138 | return false; |
139 | } |
140 | } |
141 | // Skip page moves |
142 | $moveTags = [ |
143 | ChangeTags::TAG_NEW_REDIRECT, ChangeTags::TAG_REMOVED_REDIRECT, ChangeTags::TAG_CHANGED_REDIRECT_TARGET |
144 | ]; |
145 | foreach ( $moveTags as $moveTag ) { |
146 | if ( in_array( $moveTag, $tags ) ) { |
147 | $logger->debug( __METHOD__ . ': AutoModerator skip rev - page move' ); |
148 | return false; |
149 | } |
150 | } |
151 | // Skip edits from editors that have certain user rights |
152 | if ( self::shouldSkipUser( $permissionManager, $user, $wikiConfig ) ) { |
153 | $logger->debug( __METHOD__ . ': AutoModerator skip rev - trusted user rights edits' ); |
154 | return false; |
155 | } |
156 | // Skip external users |
157 | if ( ExternalUserNames::isExternal( $user->getName() ) ) { |
158 | $logger->debug( __METHOD__ . ': AutoModerator skip rev - external user' ); |
159 | return false; |
160 | } |
161 | $wikiPage = $wikiPageFactory->newFromID( $wikiPageId ); |
162 | // Skip null pages |
163 | if ( $wikiPage === null ) { |
164 | $logger->debug( __METHOD__ . ': AutoModerator skip rev - wikiPage is null' ); |
165 | return false; |
166 | } |
167 | // Skip non-mainspace edit |
168 | if ( $wikiPage->getNamespace() !== NS_MAIN ) { |
169 | $logger->debug( __METHOD__ . ': AutoModerator skip rev - non-mainspace edits' ); |
170 | return false; |
171 | } |
172 | // Skip protected pages that only admins can edit. |
173 | // Automoderator should be able to revert semi-protected pages, |
174 | // so we won't be skipping those on pre-check. |
175 | if ( self::isProtectedPage( $restrictionStore, $wikiPage ) ) { |
176 | $logger->debug( __METHOD__ . ': AutoModerator skip rev - protected page' ); |
177 | return false; |
178 | } |
179 | return true; |
180 | } |
181 | |
182 | /** |
183 | * Check revision; revert if it meets configured critera |
184 | * @param array $score |
185 | * @param string $revertRiskModelName |
186 | * @return array |
187 | */ |
188 | public function maybeRollback( array $score, string $revertRiskModelName ): array { |
189 | $reverted = 0; |
190 | $status = 'Not reverted'; |
191 | $probability = $score[ 'output' ][ 'probabilities' ][ 'true' ]; |
192 | // Check if the threshold should be taken from the language-agnostic |
193 | // or the multilingual model based on what model was chosen in the job |
194 | if ( $probability > Util::getRevertThreshold( $this->wikiConfig, $revertRiskModelName ) ) { |
195 | if ( $this->enforce ) { |
196 | $pageRollbackStatus = $this->doRollback(); |
197 | if ( !$pageRollbackStatus->isOK() ) { |
198 | $errorMessages = $pageRollbackStatus->getMessages( 'error' ); |
199 | // checks to see if there was an edit conflict or already rolled error message |
200 | // which would indicate that someone else |
201 | // has edited or rolled the page since the job began |
202 | if ( $errorMessages && |
203 | ( $errorMessages[0]->getKey() === "alreadyrolled" |
204 | || $errorMessages[0]->getKey() === "edit-conflict" ) ) { |
205 | $status = 'success'; |
206 | return [ $reverted => $status ]; |
207 | } |
208 | return [ $reverted => $errorMessages ? |
209 | wfMessage( $errorMessages[0] )->inLanguage( "en" )->plain() |
210 | : "Failed to save revision" ]; |
211 | } |
212 | } |
213 | $reverted = 1; |
214 | $status = 'success'; |
215 | } |
216 | return [ $reverted => $status ]; |
217 | } |
218 | |
219 | /** |
220 | * @param PermissionManager $permissionManager |
221 | * @param UserIdentity $user |
222 | * @param Config $wikiConfig |
223 | * @return bool |
224 | */ |
225 | public static function shouldSkipUser( PermissionManager $permissionManager, |
226 | UserIdentity $user, Config $wikiConfig ): bool { |
227 | return $permissionManager->userHasAnyRight( |
228 | $user, ...(array)$wikiConfig->get( 'AutoModeratorSkipUserRights' ) |
229 | ); |
230 | } |
231 | |
232 | /** |
233 | * @param UserIdentity $user |
234 | * @param UserIdentity $autoModeratorUser |
235 | * @return bool |
236 | */ |
237 | public static function areUsersEqual( UserIdentity $user, UserIdentity $autoModeratorUser ): bool { |
238 | return $user->equals( $autoModeratorUser ); |
239 | } |
240 | |
241 | /** |
242 | * @param RestrictionStore $restrictionStore |
243 | * @param WikiPage $wikiPage |
244 | * @return bool |
245 | */ |
246 | public static function isProtectedPage( RestrictionStore $restrictionStore, WikiPage $wikiPage ): bool { |
247 | return $restrictionStore->isProtected( $wikiPage ) |
248 | && !$restrictionStore->isSemiProtected( $wikiPage ); |
249 | } |
250 | |
251 | /** |
252 | * @param int|null $parentId |
253 | * @return bool |
254 | */ |
255 | public static function isNewPageCreation( ?int $parentId ): bool { |
256 | return $parentId === null || $parentId === 0; |
257 | } |
258 | } |