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