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-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;
|
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;
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $fetchedData = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var User
|
|
|
|
*/
|
|
|
|
private $pageUser;
|
|
|
|
|
|
|
|
/**
|
2021-02-26 10:45:05 +00:00
|
|
|
* @param UserNameUtils $userNameUtils
|
2020-08-11 15:50:20 +00:00
|
|
|
* @param Title|null $title
|
2017-07-12 15:12:40 +00:00
|
|
|
*/
|
2021-02-26 10:45:05 +00:00
|
|
|
public function __construct( UserNameUtils $userNameUtils, Title $title = null ) {
|
|
|
|
$this->userNameUtils = $userNameUtils;
|
2018-04-15 23:27:21 +00:00
|
|
|
$this->title = $title;
|
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 ) ) {
|
2019-05-23 09:55:21 +00:00
|
|
|
return User::newFromAnyId( null, $titleText, null );
|
|
|
|
}
|
|
|
|
|
|
|
|
$pageUserId = User::idFromName( $titleText );
|
|
|
|
if ( $pageUserId ) {
|
|
|
|
return User::newFromId( $pageUserId );
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|