46 private $mUser =
null;
67 parent::__construct( $mainModule, $moduleName );
68 $this->userGroupManager = $userGroupManager;
69 $this->watchedItemStore = $watchedItemStore;
72 $this->watchlistExpiryEnabled = $this->
getConfig()->get( MainConfigNames::WatchlistExpiry );
73 $this->watchlistMaxDuration =
74 $this->
getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration );
75 $this->watchlistManager = $watchlistManager;
76 $this->userOptionsLookup = $userOptionsLookup;
84 if ( !$this->
getAuthority()->isAllowed(
'userrights' ) ) {
85 $block = $pUser->getBlock( Authority::READ_LATEST );
86 if ( $block && $block->isSitewide() ) {
94 $expiry = (array)$params[
'expiry'];
95 $add = (array)$params[
'add'];
98 } elseif ( count( $expiry ) !== count( $add ) ) {
99 if ( count( $expiry ) === 1 ) {
100 $expiry = array_fill( 0, count( $add ), $expiry[0] );
103 'apierror-toofewexpiries',
112 foreach ( $expiry as $index => $expiryValue ) {
113 $group = $add[$index];
114 $groupExpiries[$group] = SpecialUserRights::expiryToTimestamp( $expiryValue );
116 if ( $groupExpiries[$group] ===
false ) {
121 if ( $groupExpiries[$group] && $groupExpiries[$group] <
wfTimestampNow() ) {
126 $user = $this->getUrUser( $params );
128 $tags = $params[
'tags'];
131 if ( $tags !==
null ) {
133 if ( !$ableToTag->isOK() ) {
141 $r[
'user'] = $user->getName();
142 $r[
'userid'] = $user->getId();
143 [ $r[
'added'], $r[
'removed'] ] = $form->doSaveUserGroups(
145 $user, $add, (array)$params[
'remove'],
146 $params[
'reason'], (array)$tags, $groupExpiries
150 $watchuser = $params[
'watchuser'];
151 $userPage = Title::makeTitle(
NS_USER, $user->getName() );
152 if ( $watchuser && $user->getWikiId() === UserIdentity::LOCAL ) {
153 $this->
setWatch(
'watch', $userPage, $this->
getUser(),
null, $watchlistExpiry );
156 $watchlistExpiry =
null;
158 $r[
'watchuser'] = $watchuser;
159 if ( $watchlistExpiry !==
null ) {
161 $this->watchedItemStore,
168 ApiResult::setIndexedTagName( $r[
'added'],
'group' );
169 ApiResult::setIndexedTagName( $r[
'removed'],
'group' );
177 private function getUrUser( array $params ) {
178 if ( $this->mUser !==
null ) {
184 $user = $params[
'user'] ??
'#' . $params[
'userid'];
188 $status = $form->fetchUser( $user );
189 if ( !$status->isOK() ) {
193 $this->mUser = $status->value;
195 return $status->value;
207 $allGroups = $this->userGroupManager->listAllGroups();
215 ParamValidator::PARAM_TYPE =>
'user',
216 UserDef::PARAM_ALLOWED_USER_TYPES => [
'name',
'id' ],
219 ParamValidator::PARAM_TYPE =>
'integer',
220 ParamValidator::PARAM_DEPRECATED =>
true,
223 ParamValidator::PARAM_TYPE => $allGroups,
224 ParamValidator::PARAM_ISMULTI =>
true
227 ParamValidator::PARAM_ISMULTI =>
true,
228 ParamValidator::PARAM_ALLOW_DUPLICATES =>
true,
229 ParamValidator::PARAM_DEFAULT =>
'infinite',
232 ParamValidator::PARAM_TYPE => $allGroups,
233 ParamValidator::PARAM_ISMULTI =>
true
236 ParamValidator::PARAM_DEFAULT =>
''
243 ParamValidator::PARAM_TYPE =>
'tags',
244 ParamValidator::PARAM_ISMULTI =>
true
246 'watchuser' =>
false,
252 if ( $this->watchlistExpiryEnabled ) {
254 'watchlistexpiry' => [
255 ParamValidator::PARAM_TYPE =>
'expiry',
256 ExpiryDef::PARAM_MAX => $this->watchlistMaxDuration,
257 ExpiryDef::PARAM_USE_MAX =>
true,
270 return $this->getUrUser( $params )->getName();
275 'action=userrights&user=FooBot&add=bot&remove=sysop|bureaucrat&token=123ABC'
276 =>
'apihelp-userrights-example-user',
277 'action=userrights&userid=123&add=bot&remove=sysop|bureaucrat&token=123ABC'
278 =>
'apihelp-userrights-example-userid',
279 'action=userrights&user=SometimeSysop&add=sysop&expiry=1%20month&token=123ABC'
280 =>
'apihelp-userrights-example-expiry',
285 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:User_group_membership';
getExpiryFromParams(array $params)
Get formatted expiry from the given parameters, or null if no expiry was provided.
setWatch(string $watch, Title $title, User $user, ?string $userOption=null, ?string $expiry=null)
Set a watch (or unwatch) based the based on a watchlist parameter.
getWatchlistExpiry(WatchedItemStoreInterface $store, Title $title, UserIdentity $user)
Get existing expiry from the database.
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
This abstract class implements many basic API functions, and is the base of all API classes.
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
const PARAM_HELP_MSG_APPEND
((string|array|Message)[]) Specify additional i18n messages to append to the normal message for this ...
requireOnlyOneParameter( $params,... $required)
Die if 0 or more than one of a certain set of parameters is set and not false.
getResult()
Get the result object.
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
const GET_VALUES_FOR_HELP
getAllowedParams() flag: When this is set, the result could take longer to generate,...
getModuleName()
Get the name of the module being executed by this instance.
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
dieBlocked(Block $block)
Throw an ApiUsageException, which will (if uncaught) call the main module's error handler and die wit...
This is the main API class, used for both external and internal processing.
getHelpUrls()
Return links to more detailed help pages about the module.
getAllowedParams( $flags=0)
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
mustBePosted()
Indicates whether this module must be called with a POST request.
__construct(ApiMain $mainModule, $moduleName, UserGroupManager $userGroupManager, WatchedItemStoreInterface $watchedItemStore, WatchlistManager $watchlistManager, UserOptionsLookup $userOptionsLookup)
needsToken()
Returns the token type this module requires in order to execute.
isWriteMode()
Indicates whether this module requires write mode.
getExamplesMessages()
Returns usage examples for this module.
getWebUITokenSalt(array $params)
Fetch the salt used in the Web UI corresponding to this module.
getContext()
Get the base IContextSource object.
A class containing constants representing the names of configuration variables.
trait ApiWatchlistTrait
An ApiWatchlistTrait adds class properties and convenience methods for APIs that allow you to watch a...