Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
83.64% |
46 / 55 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| RevisionFromEditCompleteHookHandler | |
83.64% |
46 / 55 |
|
50.00% |
1 / 2 |
13.74 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| onRevisionFromEditComplete | |
83.33% |
45 / 54 |
|
0.00% |
0 / 1 |
12.67 | |||
| 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\Config\Config; |
| 10 | use MediaWiki\JobQueue\JobQueueGroup; |
| 11 | use MediaWiki\Logger\LoggerFactory; |
| 12 | use MediaWiki\Page\Hook\RevisionFromEditCompleteHook; |
| 13 | use MediaWiki\Page\WikiPageFactory; |
| 14 | use MediaWiki\Permissions\PermissionManager; |
| 15 | use MediaWiki\Permissions\RestrictionStore; |
| 16 | use MediaWiki\Registration\ExtensionRegistry; |
| 17 | use MediaWiki\Revision\RevisionStore; |
| 18 | use MediaWiki\User\UserGroupManager; |
| 19 | |
| 20 | class RevisionFromEditCompleteHookHandler implements RevisionFromEditCompleteHook { |
| 21 | |
| 22 | public function __construct( |
| 23 | private readonly Config $wikiConfig, |
| 24 | private readonly UserGroupManager $userGroupManager, |
| 25 | private readonly Config $config, |
| 26 | private readonly WikiPageFactory $wikiPageFactory, |
| 27 | private readonly RevisionStore $revisionStore, |
| 28 | private readonly RestrictionStore $restrictionStore, |
| 29 | private readonly JobQueueGroup $jobQueueGroup, |
| 30 | private readonly PermissionManager $permissionManager, |
| 31 | ) { |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @inheritDoc |
| 36 | */ |
| 37 | public function onRevisionFromEditComplete( $wikiPage, $rev, $originalRevId, $user, &$tags ) { |
| 38 | if ( ExtensionRegistry::getInstance()->isLoaded( 'ORES' ) ) { |
| 39 | $oresModels = $this->config->get( 'OresModels' ); |
| 40 | |
| 41 | if ( array_key_exists( 'revertrisklanguageagnostic', $oresModels ) && |
| 42 | $oresModels[ 'revertrisklanguageagnostic' ][ 'enabled' ] |
| 43 | ) { |
| 44 | // ORES is loaded and model is enabled; not calling the job from this hook handler |
| 45 | return; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | if ( !$wikiPage || !$rev || !$user ) { |
| 50 | return; |
| 51 | } |
| 52 | $enabledConfigKey = Util::isWikiMultilingual( $this->config ) |
| 53 | ? "AutoModeratorMultilingualConfigEnableRevisionCheck" |
| 54 | : "AutoModeratorEnableRevisionCheck"; |
| 55 | $revisionCheckEnabled = $this->wikiConfig->has( $enabledConfigKey ) |
| 56 | && $this->wikiConfig->get( $enabledConfigKey ); |
| 57 | if ( !$revisionCheckEnabled ) { |
| 58 | return; |
| 59 | } |
| 60 | $autoModeratorUser = Util::getAutoModeratorUser( $this->config, $this->userGroupManager ); |
| 61 | $userId = $user->getId(); |
| 62 | $logger = LoggerFactory::getInstance( 'AutoModerator' ); |
| 63 | $title = $wikiPage->getTitle(); |
| 64 | $wikiPageId = $wikiPage->getId(); |
| 65 | $revId = $rev->getId(); |
| 66 | if ( !RevisionCheck::revertPreCheck( |
| 67 | $user, |
| 68 | $autoModeratorUser, |
| 69 | $logger, |
| 70 | $this->revisionStore, |
| 71 | $tags, |
| 72 | $this->restrictionStore, |
| 73 | $this->wikiPageFactory, |
| 74 | $this->config, |
| 75 | $this->wikiConfig, |
| 76 | $rev, |
| 77 | $this->permissionManager ) ) { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | $job = new AutoModeratorFetchRevScoreJob( $title, |
| 82 | [ |
| 83 | 'wikiPageId' => $wikiPageId, |
| 84 | 'revId' => $revId, |
| 85 | 'originalRevId' => $originalRevId, |
| 86 | // The test/production environments do not work when you pass the entire User object. |
| 87 | // To get around this, we have split the required parameters from the User object |
| 88 | // into individual parameters so that the test/production Job constructor will accept them. |
| 89 | 'userId' => $userId, |
| 90 | 'userName' => $user->getName(), |
| 91 | 'tags' => $tags, |
| 92 | 'scores' => null |
| 93 | ] |
| 94 | ); |
| 95 | try { |
| 96 | $this->jobQueueGroup->lazyPush( $job ); |
| 97 | $logger->debug( 'Job pushed for {rev}', [ |
| 98 | 'rev' => $revId, |
| 99 | ] ); |
| 100 | } catch ( Exception $e ) { |
| 101 | $msg = $e->getMessage(); |
| 102 | $logger->error( 'Job push failed for {rev}: {msg}', [ |
| 103 | 'rev' => $revId, |
| 104 | 'msg' => $msg |
| 105 | ] ); |
| 106 | } |
| 107 | } |
| 108 | } |