Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 16 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| Hooks | |
0.00% |
0 / 16 |
|
0.00% |
0 / 8 |
90 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| injectTagsIfPerformerUsesAMC | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| onUserGetDefaultOptions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onGetPreferences | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| onListDefinedTags | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onChangeTagsListActive | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onManualLogEntryBeforePublish | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| onRecentChange_save | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MobileFrontend\Amc; |
| 4 | |
| 5 | use MediaWiki\ChangeTags\Hook\ChangeTagsListActiveHook; |
| 6 | use MediaWiki\ChangeTags\Hook\ListDefinedTagsHook; |
| 7 | use MediaWiki\ChangeTags\Taggable; |
| 8 | use MediaWiki\Logging\Hook\ManualLogEntryBeforePublishHook; |
| 9 | use MediaWiki\Logging\ManualLogEntry; |
| 10 | use MediaWiki\Preferences\Hook\GetPreferencesHook; |
| 11 | use MediaWiki\RecentChanges\Hook\RecentChange_saveHook; |
| 12 | use MediaWiki\RecentChanges\RecentChange; |
| 13 | use MediaWiki\User\Hook\UserGetDefaultOptionsHook; |
| 14 | use MediaWiki\User\User; |
| 15 | use MediaWiki\User\UserFactory; |
| 16 | use MediaWiki\User\UserIdentity; |
| 17 | |
| 18 | /** |
| 19 | * Hooks for Advanced Mobile Contributions |
| 20 | * |
| 21 | * @package MobileFrontend\Amc |
| 22 | */ |
| 23 | final class Hooks implements |
| 24 | ListDefinedTagsHook, |
| 25 | ChangeTagsListActiveHook, |
| 26 | RecentChange_saveHook, |
| 27 | GetPreferencesHook, |
| 28 | UserGetDefaultOptionsHook, |
| 29 | ManualLogEntryBeforePublishHook |
| 30 | { |
| 31 | public function __construct( |
| 32 | private readonly UserFactory $userFactory, |
| 33 | ) { |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Helper method to tag objects like Logs or Recent changes |
| 38 | * @param Taggable $taggable |
| 39 | * @param UserIdentity $performer |
| 40 | * @return bool |
| 41 | */ |
| 42 | private static function injectTagsIfPerformerUsesAMC( Taggable $taggable, UserIdentity $performer ) { |
| 43 | $userMode = UserMode::newForUser( $performer ); |
| 44 | if ( $userMode->isEnabled() ) { |
| 45 | $taggable->addTags( [ Manager::AMC_EDIT_TAG ] ); |
| 46 | } |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Register default preference value for AMC opt-in |
| 52 | * |
| 53 | * @param array &$defaultUserOptions Reference to default options array |
| 54 | */ |
| 55 | public function onUserGetDefaultOptions( &$defaultUserOptions ) { |
| 56 | $defaultUserOptions[UserMode::USER_OPTION_MODE_AMC] = UserMode::OPTION_DISABLED; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Register AMC preference |
| 61 | * @param User $user |
| 62 | * @param array &$preferences |
| 63 | */ |
| 64 | public function onGetPreferences( $user, &$preferences ) { |
| 65 | $preferences[UserMode::USER_OPTION_MODE_AMC] = [ |
| 66 | 'type' => 'api', |
| 67 | 'default' => UserMode::OPTION_DISABLED |
| 68 | ]; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * ListDefinedTags hook handler |
| 73 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/ListDefinedTags |
| 74 | * |
| 75 | * @param array &$tags The list of tags. Add your extension's tags to this array. |
| 76 | */ |
| 77 | public function onListDefinedTags( &$tags ) { |
| 78 | $tags[] = Manager::AMC_EDIT_TAG; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * ChangeTagsListActive hook handler |
| 83 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/ChangeTagsListActive |
| 84 | * |
| 85 | * @param array &$tags The list of tags. Add your extension's tags to this array. |
| 86 | */ |
| 87 | public function onChangeTagsListActive( &$tags ) { |
| 88 | $tags[] = Manager::AMC_EDIT_TAG; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * ManualLogEntryBeforePublish hook handler that tags actions logged when user uses AMC mode |
| 93 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/ManualLogEntryBeforePublish |
| 94 | * |
| 95 | * @param ManualLogEntry $logEntry |
| 96 | */ |
| 97 | public function onManualLogEntryBeforePublish( $logEntry ): void { |
| 98 | $performer = $this->userFactory-> |
| 99 | newFromUserIdentity( $logEntry->getPerformerIdentity() ); |
| 100 | self::injectTagsIfPerformerUsesAMC( $logEntry, $performer ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * RecentChange_save hook handler that tags changes performed when user uses AMC mode |
| 105 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/RecentChange_save |
| 106 | * |
| 107 | * @param RecentChange $rc |
| 108 | */ |
| 109 | public function onRecentChange_save( $rc ) { |
| 110 | // To be safe, we should use the User objected provided via RecentChange, not the |
| 111 | // currently logged-in user. |
| 112 | self::injectTagsIfPerformerUsesAMC( $rc, $rc->getPerformerIdentity() ); |
| 113 | } |
| 114 | } |