Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
GlobalWatchlistHooks
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
5 / 5
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 onApiOptions
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 onGetPreferences
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 onLoginFormValidErrorMessages
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 onSidebarBeforeOutput
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * Hook handler
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @ingroup SpecialPage
23 */
24
25namespace MediaWiki\Extension\GlobalWatchlist;
26
27use ApiOptions;
28use HTMLInfoField;
29use IBufferingStatsdDataFactory;
30use MediaWiki\Api\Hook\ApiOptionsHook;
31use MediaWiki\Hook\LoginFormValidErrorMessagesHook;
32use MediaWiki\Hook\SidebarBeforeOutputHook;
33use MediaWiki\Preferences\Hook\GetPreferencesHook;
34use MediaWiki\SpecialPage\SpecialPageFactory;
35use MediaWiki\User\User;
36use Skin;
37
38/**
39 * @author DannyS712
40 */
41class GlobalWatchlistHooks implements
42    ApiOptionsHook,
43    GetPreferencesHook,
44    LoginFormValidErrorMessagesHook,
45    SidebarBeforeOutputHook
46{
47
48    /** @var SpecialPageFactory */
49    private $specialPageFactory;
50
51    /** @var IBufferingStatsdDataFactory */
52    private $statsdDataFactory;
53
54    /**
55     * @param SpecialPageFactory $specialPageFactory
56     * @param IBufferingStatsdDataFactory $statsdDataFactory
57     */
58    public function __construct(
59        SpecialPageFactory $specialPageFactory,
60        IBufferingStatsdDataFactory $statsdDataFactory
61    ) {
62        $this->specialPageFactory = $specialPageFactory;
63        $this->statsdDataFactory = $statsdDataFactory;
64    }
65
66    /**
67     * @param ApiOptions $apiModule
68     * @param User $user
69     * @param array $changes Associative array of preference name => value
70     * @param string[] $resetKinds
71     */
72    public function onApiOptions( $apiModule, $user, $changes, $resetKinds ) {
73        if ( array_key_exists( SettingsManager::PREFERENCE_NAME, $changes ) ) {
74            $this->statsdDataFactory->increment( 'globalwatchlist.settings.manualchange' );
75        }
76    }
77
78    /**
79     * @param User $user
80     * @param array &$preferences
81     */
82    public function onGetPreferences( $user, &$preferences ) {
83        $preferences[ SettingsManager::PREFERENCE_NAME ] = [
84            'type' => 'api'
85        ];
86
87        $preferences[ 'globalwatchlist-prefs' ] = [
88            'class' => HTMLInfoField::class,
89            'section' => 'watchlist/globalwatchlist',
90            'label-message' => 'globalwatchlist-prefs-settings'
91        ];
92    }
93
94    /**
95     * Only logged-in users can use Special:GlobalWatchlist and Special:GlobalWatchlistSettings.
96     * Ensure that when anonymous users are redirected to Special:UserLogin, there is a note
97     * explaining why
98     *
99     * @param string[] &$messages
100     */
101    public function onLoginFormValidErrorMessages( array &$messages ) {
102        $messages[] = 'globalwatchlist-must-login';
103    }
104
105    /**
106     * Add a link to Special:GlobalWatchlist when on Special:Watchlist
107     *
108     * @param Skin $skin
109     * @param array &$sidebar Sidebar content. Modify $sidebar to add or modify sidebar portlets.
110     */
111    public function onSidebarBeforeOutput( $skin, &$sidebar ): void {
112        if ( !$skin->getTitle()->isSpecial( 'Watchlist' ) ) {
113            return;
114        }
115
116        $globalWatchlistTitle = $this->specialPageFactory
117            ->getPage( 'GlobalWatchlist' )
118            ->getPageTitle();
119
120        $link = [
121            'text' => $skin->msg( 'globalwatchlist-gotoglobal' )->text(),
122            'href' => $globalWatchlistTitle->getLinkURL(),
123            'title' => $skin->msg( 'globalwatchlist-gotoglobal-tooltip' )->text(),
124        ];
125        $sidebar['navigation'][] = $link;
126    }
127
128}