Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
73.08% covered (warning)
73.08%
19 / 26
62.50% covered (warning)
62.50%
5 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
SkinUserPageHelper
73.08% covered (warning)
73.08%
19 / 26
62.50% covered (warning)
62.50%
5 / 8
24.32
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setContext
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 setTitle
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 fetchData
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
5
 buildPageUserObject
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
4.05
 getPageUser
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isUserPage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isUserPageAccessibleToCurrentUser
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
20
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 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Minerva\Skins;
22
23use MediaWiki\Context\IContextSource;
24use MediaWiki\Title\Title;
25use MediaWiki\User\User;
26use MediaWiki\User\UserFactory;
27use MediaWiki\User\UserNameUtils;
28
29class SkinUserPageHelper {
30    private UserFactory $userFactory;
31    private UserNameUtils $userNameUtils;
32    private IContextSource $context;
33    private ?Title $title;
34    private bool $fetchedData = false;
35    private ?User $pageUser = null;
36
37    public function __construct(
38        UserFactory $userFactory,
39        UserNameUtils $userNameUtils
40    ) {
41        $this->userFactory = $userFactory;
42        $this->userNameUtils = $userNameUtils;
43    }
44
45    public function setContext( IContextSource $context ): self {
46        $this->context = $context;
47        return $this;
48    }
49
50    public function setTitle( ?Title $title ): self {
51        $this->title = $title;
52        $this->fetchedData = false;
53        $this->pageUser = null;
54        return $this;
55    }
56
57    /**
58     * Fetch user data and store locally for performance improvement
59     */
60    private function fetchData(): ?User {
61        if ( !$this->fetchedData ) {
62            if ( $this->title && $this->title->inNamespace( NS_USER ) && !$this->title->isSubpage() ) {
63                $this->pageUser = $this->buildPageUserObject( $this->title );
64            }
65            $this->fetchedData = true;
66        }
67        return $this->pageUser;
68    }
69
70    /**
71     * Return new User object based on username or IP address.
72     */
73    private function buildPageUserObject( Title $title ): ?User {
74        $titleText = $title->getText();
75
76        if ( $this->userNameUtils->isIP( $titleText ) ) {
77            return $this->userFactory->newAnonymous( $titleText );
78        }
79
80        $user = $this->userFactory->newFromName( $titleText );
81        if ( $user && $user->isRegistered() ) {
82            return $user;
83        }
84
85        return null;
86    }
87
88    public function getPageUser(): ?User {
89        return $this->fetchData();
90    }
91
92    public function isUserPage(): bool {
93        return $this->fetchData() !== null;
94    }
95
96    public function isUserPageAccessibleToCurrentUser(): bool {
97        $pageUser = $this->fetchData();
98        $isHidden = $pageUser && $pageUser->isHidden();
99        $canViewHidden = $this->context && $this->context->getAuthority()->isAllowed( 'hideuser' );
100        return !$isHidden || $canViewHidden;
101    }
102}