Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.71% covered (warning)
85.71%
36 / 42
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
UserMenuComponent
85.71% covered (warning)
85.71%
36 / 42
0.00% covered (danger)
0.00%
0 / 1
8.19
0.00% covered (danger)
0.00%
0 / 1
 __construct
85.71% covered (warning)
85.71%
36 / 42
0.00% covered (danger)
0.00%
0 / 1
8.19
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 *
17 * @file
18 */
19namespace MediaWiki\Skin\WikimediaApiPortal\Component;
20
21use IContextSource;
22use MediaWiki\Config\ServiceOptions;
23use MediaWiki\Html\Html;
24use MediaWiki\SpecialPage\SpecialPage;
25use MediaWiki\SpecialPage\SpecialPageFactory;
26use MediaWiki\Title\Title;
27use MediaWiki\Title\TitleFactory;
28use MediaWiki\User\User;
29use Message;
30use OOUI\IconWidget;
31use Wikimedia\Message\IMessageFormatterFactory;
32
33class UserMenuComponent extends MessageComponent {
34    public const CONSTRUCTOR_OPTIONS = [
35        'WMAPIPExtraUserMenuSpecialPages',
36    ];
37
38    // Personal url keys that will be allowed in the user menu
39    private const PERSONAL_LINKS_ALLOWED_LIST = [ 'logout', 'preferences' ];
40
41    /**
42     * @param ServiceOptions $options
43     * @param IMessageFormatterFactory $messageFormatterFactory
44     * @param IContextSource $contextSource
45     * @param TitleFactory $titleFactory
46     * @param SpecialPageFactory $specialPageFactory
47     * @param User $user
48     * @param Title $title
49     * @param array $personalUrls
50     */
51    public function __construct(
52        ServiceOptions $options,
53        IMessageFormatterFactory $messageFormatterFactory,
54        IContextSource $contextSource,
55        TitleFactory $titleFactory,
56        SpecialPageFactory $specialPageFactory,
57        User $user,
58        Title $title,
59        array $personalUrls
60    ) {
61        parent::__construct(
62            'UserMenu',
63            $messageFormatterFactory,
64            $contextSource
65        );
66
67        $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
68
69        if ( $user->isAnon() || $user->isTemp() ) {
70            $this->args = [
71                'showOnlyLogin' => true,
72                'login-href' => SpecialPage::getTitleFor( 'Userlogin' )->getLocalURL( [
73                    'returnto' => $title
74                ] ),
75                'login-label' => Message::newFromKey( 'wikimediaapiportal-skin-login-link-label' )->text(),
76            ];
77            return;
78        }
79
80        $items = [];
81
82        $extraSpecialPages = $options->get( 'WMAPIPExtraUserMenuSpecialPages' );
83        foreach ( $extraSpecialPages as $specialPageName ) {
84            $title = $titleFactory->newFromText( $specialPageName, NS_SPECIAL );
85            $specialPageObj = $specialPageFactory->getPage( $specialPageName );
86            if ( $title && $specialPageObj ) {
87                $items[] = Html::element(
88                    'a',
89                    [ 'href' => $title->getLocalURL() ],
90                    $specialPageObj->getDescription()
91                );
92            }
93        }
94
95        foreach ( $personalUrls as $key => $data ) {
96            if ( in_array( $key, self::PERSONAL_LINKS_ALLOWED_LIST ) ) {
97                $items[] = Html::element(
98                    'a',
99                    [ 'href' => $data['href'] ],
100                    $data['text']
101                );
102            }
103        }
104
105        $label = new IconWidget( [
106            'icon' => 'userAvatarOutline',
107            'title' => $user->getName()
108        ] );
109
110        $this->args = [
111            'showOnlyLogin' => false,
112            'user-menu-label' => $label,
113            'user-menu-items' => $items,
114        ];
115    }
116}