Rename path param "name" to user in UserContributions endpoints. Name is more appropriate as "name" could be username as well as IP address or range of IP addresses. It also fits better with the implementation, as the user parameter gets validated as a User object. Bug: T259680 Change-Id: I6a146d99d1063c250f2ac460e4576bc287259766
80 lines
1.8 KiB
PHP
80 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Rest\Handler;
|
|
|
|
use MediaWiki\Rest\Handler;
|
|
use MediaWiki\Rest\LocalizedHttpException;
|
|
use MediaWiki\Revision\ContributionsLookup;
|
|
use MediaWiki\User\UserIdentity;
|
|
use MediaWiki\User\UserNameUtils;
|
|
use RequestContext;
|
|
use Wikimedia\Message\MessageValue;
|
|
|
|
/**
|
|
* @since 1.35
|
|
*/
|
|
abstract class AbstractContributionHandler extends Handler {
|
|
|
|
/**
|
|
* @var ContributionsLookup
|
|
*/
|
|
protected $contributionsLookup;
|
|
|
|
/** Hard limit results to 20 contributions */
|
|
protected const MAX_LIMIT = 20;
|
|
|
|
/**
|
|
* @var bool User is requesting their own contributions
|
|
*/
|
|
protected $me;
|
|
|
|
/**
|
|
* @var UserNameUtils
|
|
*/
|
|
protected $userNameUtils;
|
|
|
|
/**
|
|
* @param ContributionsLookup $contributionsLookup
|
|
* @param UserNameUtils $userNameUtils
|
|
*/
|
|
public function __construct(
|
|
ContributionsLookup $contributionsLookup,
|
|
UserNameUtils $userNameUtils
|
|
) {
|
|
$this->contributionsLookup = $contributionsLookup;
|
|
$this->userNameUtils = $userNameUtils;
|
|
}
|
|
|
|
protected function postInitSetup() {
|
|
$this->me = $this->getConfig()['mode'] === 'me';
|
|
}
|
|
|
|
/**
|
|
* Returns the user who's contributions we are requesting.
|
|
* Either me (requesting user) or another user.
|
|
*
|
|
* @return UserIdentity
|
|
* @throws LocalizedHttpException
|
|
*/
|
|
protected function getTargetUser() {
|
|
if ( $this->me ) {
|
|
$user = RequestContext::getMain()->getUser();
|
|
if ( $user->isAnon() ) {
|
|
throw new LocalizedHttpException(
|
|
new MessageValue( 'rest-permission-denied-anon' ), 401
|
|
);
|
|
}
|
|
return $user;
|
|
}
|
|
|
|
/* @var UserIdentity $user */
|
|
$user = $this->getValidatedParams()['user'];
|
|
$name = $user->getName();
|
|
if ( !$this->userNameUtils->isIp( $name ) && !$user->isRegistered() ) {
|
|
throw new LocalizedHttpException(
|
|
new MessageValue( 'rest-nonexistent-user', [ $name ] ), 404
|
|
);
|
|
}
|
|
return $user;
|
|
}
|
|
}
|