Debian Codegraph @

skins/MinervaNeue/includes/Skins/SkinUserPageHelper.php

raw symbols SCIP
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
21 namespace MediaWiki\Minerva\Skins;
22
23 use MediaWiki\Context\IContextSource;
24 use MediaWiki\Title\Title;
25 use MediaWiki\User\User;
26 use MediaWiki\User\UserFactory;
27 use MediaWiki\User\UserNameUtils;
28
29 class 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 }