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