Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.00% |
81 / 90 |
|
50.00% |
4 / 8 |
CRAP | |
0.00% |
0 / 1 |
| RevisionCheck | |
90.00% |
81 / 90 |
|
50.00% |
4 / 8 |
36.23 | |
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 | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
2.01 | |||
| 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 $config |
| 88 | * @param Config $wikiConfig |
| 89 | * @param RevisionRecord $rev |
| 90 | * @param PermissionManager $permissionManager |
| 91 | * @return bool |
| 92 | */ |
| 93 | public static function revertPreCheck( UserIdentity $user, User $autoModeratorUser, LoggerInterface $logger, |
| 94 | RevisionStore $revisionStore, array $tags, RestrictionStore $restrictionStore, WikiPageFactory $wikiPageFactory, |
| 95 | Config $config, Config $wikiConfig, RevisionRecord $rev, PermissionManager $permissionManager |
| 96 | ): bool { |
| 97 | $wikiPageId = $rev->getPageId(); |
| 98 | // Skips reverts if AutoModerator is blocked |
| 99 | $autoModeratorBlock = $autoModeratorUser->getBlock(); |
| 100 | if ( $autoModeratorBlock && $autoModeratorBlock->appliesToPage( $wikiPageId ) ) { |
| 101 | $logger->debug( __METHOD__ . ': AutoModerator skip rev - AutoModerator is blocked' ); |
| 102 | return false; |
| 103 | } |
| 104 | // Skip AutoModerator edits |
| 105 | if ( self::areUsersEqual( $user, $autoModeratorUser ) ) { |
| 106 | $logger->debug( __METHOD__ . ': AutoModerator skip rev - AutoMod edits' ); |
| 107 | return false; |
| 108 | } |
| 109 | $parentId = $rev->getParentId(); |
| 110 | // Skip new page creations |
| 111 | if ( self::isNewPageCreation( $parentId ) ) { |
| 112 | $logger->debug( __METHOD__ . ': AutoModerator skip rev - new page creation' ); |
| 113 | return false; |
| 114 | } |
| 115 | // Skip already reverted edits |
| 116 | if ( in_array( ChangeTags::TAG_REVERTED, $tags ) ) { |
| 117 | $logger->debug( __METHOD__ . ': AutoModerator skip rev - already reverted' ); |
| 118 | return false; |
| 119 | } |
| 120 | // Skip reverts made to an AutoModerator bot revert or if |
| 121 | // the user reverts their own edit |
| 122 | if ( array_intersect( $tags, ChangeTags::REVERT_TAGS ) ) { |
| 123 | $parentRev = $revisionStore->getRevisionById( $parentId ); |
| 124 | if ( !$parentRev ) { |
| 125 | $logger->debug( __METHOD__ . ': AutoModerator skip rev - parent revision not found' ); |
| 126 | return false; |
| 127 | } |
| 128 | $parentRevUser = $parentRev->getUser(); |
| 129 | if ( $parentRevUser === null ) { |
| 130 | $logger->debug( __METHOD__ . ': AutoModerator skip rev - parent revision user is null' ); |
| 131 | return false; |
| 132 | } |
| 133 | if ( self::areUsersEqual( $parentRevUser, $autoModeratorUser ) ) { |
| 134 | $logger->debug( __METHOD__ . ': AutoModerator skip rev - AutoModerator reverts' ); |
| 135 | return false; |
| 136 | } |
| 137 | if ( self::areUsersEqual( $parentRevUser, $user ) ) { |
| 138 | $logger->debug( __METHOD__ . ': AutoModerator skip rev - own reverts' ); |
| 139 | return false; |
| 140 | } |
| 141 | } |
| 142 | // Skip page moves |
| 143 | $moveTags = [ |
| 144 | ChangeTags::TAG_NEW_REDIRECT, ChangeTags::TAG_REMOVED_REDIRECT, ChangeTags::TAG_CHANGED_REDIRECT_TARGET |
| 145 | ]; |
| 146 | foreach ( $moveTags as $moveTag ) { |
| 147 | if ( in_array( $moveTag, $tags ) ) { |
| 148 | $logger->debug( __METHOD__ . ': AutoModerator skip rev - page move' ); |
| 149 | return false; |
| 150 | } |
| 151 | } |
| 152 | // Skip edits from editors that have certain user rights |
| 153 | if ( self::shouldSkipUser( $permissionManager, $user, $wikiConfig, $config ) ) { |
| 154 | $logger->debug( __METHOD__ . ': AutoModerator skip rev - trusted user rights edits' ); |
| 155 | return false; |
| 156 | } |
| 157 | // Skip external users |
| 158 | if ( ExternalUserNames::isExternal( $user->getName() ) ) { |
| 159 | $logger->debug( __METHOD__ . ': AutoModerator skip rev - external user' ); |
| 160 | return false; |
| 161 | } |
| 162 | $wikiPage = $wikiPageFactory->newFromID( $wikiPageId ); |
| 163 | // Skip null pages |
| 164 | if ( $wikiPage === null ) { |
| 165 | $logger->debug( __METHOD__ . ': AutoModerator skip rev - wikiPage is null' ); |
| 166 | return false; |
| 167 | } |
| 168 | // Skip non-mainspace edit |
| 169 | if ( $wikiPage->getNamespace() !== NS_MAIN ) { |
| 170 | $logger->debug( __METHOD__ . ': AutoModerator skip rev - non-mainspace edits' ); |
| 171 | return false; |
| 172 | } |
| 173 | // Skip protected pages that only admins can edit. |
| 174 | // Automoderator should be able to revert semi-protected pages, |
| 175 | // so we won't be skipping those on pre-check. |
| 176 | if ( self::isProtectedPage( $restrictionStore, $wikiPage ) ) { |
| 177 | $logger->debug( __METHOD__ . ': AutoModerator skip rev - protected page' ); |
| 178 | return false; |
| 179 | } |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Check revision; revert if it meets configured critera |
| 185 | * @param array $score |
| 186 | * @param string $revertRiskModelName |
| 187 | * @return array |
| 188 | */ |
| 189 | public function maybeRollback( array $score, string $revertRiskModelName ): array { |
| 190 | $reverted = 0; |
| 191 | $status = 'Not reverted'; |
| 192 | $probability = $score[ 'output' ][ 'probabilities' ][ 'true' ]; |
| 193 | // Check if the threshold should be taken from the language-agnostic |
| 194 | // or the multilingual model based on what model was chosen in the job |
| 195 | if ( $probability > Util::getRevertThreshold( $this->config, $this->wikiConfig ) ) { |
| 196 | if ( $this->enforce ) { |
| 197 | $pageRollbackStatus = $this->doRollback(); |
| 198 | if ( !$pageRollbackStatus->isOK() ) { |
| 199 | $errorMessages = $pageRollbackStatus->getMessages( 'error' ); |
| 200 | // checks to see if there was an edit conflict or already rolled error message |
| 201 | // which would indicate that someone else |
| 202 | // has edited or rolled the page since the job began |
| 203 | if ( $errorMessages && |
| 204 | ( $errorMessages[0]->getKey() === "alreadyrolled" |
| 205 | || $errorMessages[0]->getKey() === "edit-conflict" ) ) { |
| 206 | $status = 'success'; |
| 207 | return [ $reverted => $status ]; |
| 208 | } |
| 209 | return [ $reverted => $errorMessages ? |
| 210 | wfMessage( $errorMessages[0] )->inLanguage( "en" )->plain() |
| 211 | : "Failed to save revision" ]; |
| 212 | } |
| 213 | } |
| 214 | $reverted = 1; |
| 215 | $status = 'success'; |
| 216 | } |
| 217 | return [ $reverted => $status ]; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * @param PermissionManager $permissionManager |
| 222 | * @param UserIdentity $user |
| 223 | * @param Config $wikiConfig |
| 224 | * @return bool |
| 225 | */ |
| 226 | public static function shouldSkipUser( PermissionManager $permissionManager, |
| 227 | UserIdentity $user, Config $wikiConfig, Config $config ): bool { |
| 228 | $isMultiLingualRevertRiskEnabled = Util::isWikiMultilingual( $config ); |
| 229 | $userRightsToSkip = $isMultiLingualRevertRiskEnabled ? |
| 230 | $wikiConfig->get( "AutoModeratorMultilingualConfigSkipUserRights" ) : |
| 231 | $wikiConfig->get( 'AutoModeratorSkipUserRights' ); |
| 232 | return $permissionManager->userHasAnyRight( |
| 233 | $user, ...(array)$userRightsToSkip |
| 234 | ); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * @param UserIdentity $user |
| 239 | * @param UserIdentity $autoModeratorUser |
| 240 | * @return bool |
| 241 | */ |
| 242 | public static function areUsersEqual( UserIdentity $user, UserIdentity $autoModeratorUser ): bool { |
| 243 | return $user->equals( $autoModeratorUser ); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * @param RestrictionStore $restrictionStore |
| 248 | * @param WikiPage $wikiPage |
| 249 | * @return bool |
| 250 | */ |
| 251 | public static function isProtectedPage( RestrictionStore $restrictionStore, WikiPage $wikiPage ): bool { |
| 252 | return $restrictionStore->isProtected( $wikiPage ) |
| 253 | && !$restrictionStore->isSemiProtected( $wikiPage ); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * @param int|null $parentId |
| 258 | * @return bool |
| 259 | */ |
| 260 | public static function isNewPageCreation( ?int $parentId ): bool { |
| 261 | return $parentId === null || $parentId === 0; |
| 262 | } |
| 263 | } |