2017-07-12 15:12:40 +00:00
|
|
|
<?php
|
2018-04-15 23:21:12 +00:00
|
|
|
/**
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*/
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2020-05-01 01:16:53 +00:00
|
|
|
namespace MediaWiki\Minerva\Skins;
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2021-04-29 20:24:13 +00:00
|
|
|
use IContextSource;
|
2022-12-21 18:57:45 +00:00
|
|
|
use MediaWiki\User\UserFactory;
|
2021-02-26 10:45:05 +00:00
|
|
|
use MediaWiki\User\UserNameUtils;
|
2017-07-12 15:12:40 +00:00
|
|
|
use Title;
|
|
|
|
use User;
|
|
|
|
|
|
|
|
class SkinUserPageHelper {
|
2021-02-26 10:45:05 +00:00
|
|
|
/**
|
|
|
|
* @var UserNameUtils
|
|
|
|
*/
|
|
|
|
private $userNameUtils;
|
2021-04-29 20:24:13 +00:00
|
|
|
|
2022-12-21 18:57:45 +00:00
|
|
|
/**
|
|
|
|
* @var UserFactory
|
|
|
|
*/
|
|
|
|
private $userFactory;
|
|
|
|
|
2017-07-12 15:12:40 +00:00
|
|
|
/**
|
2019-05-24 23:42:00 +00:00
|
|
|
* @var Title|null
|
2017-07-12 15:12:40 +00:00
|
|
|
*/
|
|
|
|
private $title;
|
2021-04-29 20:24:13 +00:00
|
|
|
|
2017-07-12 15:12:40 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $fetchedData = false;
|
|
|
|
|
|
|
|
/**
|
2022-12-21 18:57:45 +00:00
|
|
|
* @var User|null
|
2017-07-12 15:12:40 +00:00
|
|
|
*/
|
|
|
|
private $pageUser;
|
|
|
|
|
2021-04-29 20:24:13 +00:00
|
|
|
/**
|
|
|
|
* @var IContextSource|null
|
|
|
|
*/
|
|
|
|
private $context;
|
|
|
|
|
2017-07-12 15:12:40 +00:00
|
|
|
/**
|
2021-02-26 10:45:05 +00:00
|
|
|
* @param UserNameUtils $userNameUtils
|
2022-12-21 18:57:45 +00:00
|
|
|
* @param UserFactory $userFactory
|
2020-08-11 15:50:20 +00:00
|
|
|
* @param Title|null $title
|
2021-04-29 20:24:13 +00:00
|
|
|
* @param IContextSource|null $context
|
2017-07-12 15:12:40 +00:00
|
|
|
*/
|
2022-12-21 18:57:45 +00:00
|
|
|
public function __construct(
|
|
|
|
UserNameUtils $userNameUtils,
|
|
|
|
UserFactory $userFactory,
|
|
|
|
Title $title = null,
|
|
|
|
IContextSource $context = null
|
|
|
|
) {
|
2021-02-26 10:45:05 +00:00
|
|
|
$this->userNameUtils = $userNameUtils;
|
2022-12-21 18:57:45 +00:00
|
|
|
$this->userFactory = $userFactory;
|
2018-04-15 23:27:21 +00:00
|
|
|
$this->title = $title;
|
2021-04-29 20:24:13 +00:00
|
|
|
$this->context = $context;
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-23 09:55:21 +00:00
|
|
|
* Fetch user data and store locally for performance improvement
|
|
|
|
* @return User|null
|
2017-07-12 15:12:40 +00:00
|
|
|
*/
|
|
|
|
private function fetchData() {
|
|
|
|
if ( $this->fetchedData === false ) {
|
2019-05-24 23:42:00 +00:00
|
|
|
if ( $this->title && $this->title->inNamespace( NS_USER ) && !$this->title->isSubpage()
|
|
|
|
) {
|
2019-05-23 09:55:21 +00:00
|
|
|
$this->pageUser = $this->buildPageUserObject( $this->title );
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
|
|
|
$this->fetchedData = true;
|
|
|
|
}
|
2019-05-23 09:55:21 +00:00
|
|
|
return $this->pageUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return new User object based on username or IP address.
|
|
|
|
* @param Title $title
|
|
|
|
* @return User|null
|
|
|
|
*/
|
|
|
|
private function buildPageUserObject( Title $title ) {
|
|
|
|
$titleText = $title->getText();
|
|
|
|
|
2021-02-26 10:45:05 +00:00
|
|
|
if ( $this->userNameUtils->isIP( $titleText ) ) {
|
2022-12-21 18:57:45 +00:00
|
|
|
return $this->userFactory->newAnonymous( $titleText );
|
2019-05-23 09:55:21 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 18:57:45 +00:00
|
|
|
$user = $this->userFactory->newFromName( $titleText );
|
|
|
|
if ( $user && $user->isRegistered() ) {
|
|
|
|
return $user;
|
2019-05-23 09:55:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return User|null
|
|
|
|
*/
|
|
|
|
public function getPageUser() {
|
2019-05-23 09:55:21 +00:00
|
|
|
return $this->fetchData();
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isUserPage() {
|
2019-05-23 09:55:21 +00:00
|
|
|
return $this->fetchData() !== null;
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
2021-04-29 20:24:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isUserPageAccessibleToCurrentUser() {
|
|
|
|
$pageUser = $this->fetchData();
|
|
|
|
$isHidden = $pageUser && $pageUser->isHidden();
|
|
|
|
$canViewHidden = $this->context && $this->context->getAuthority()->isAllowed( 'hideuser' );
|
|
|
|
return !$isHidden || $canViewHidden;
|
|
|
|
}
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|