| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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 |
|
| 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 |
|
| 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 |
} |