Merge "Rename autonym for 'no' from 'norsk bokmål' to 'norsk'"
[lhc/web/wiklou.git] / includes / api / ApiUserrights.php
index 4ef974c..2a364d9 100644 (file)
@@ -1,9 +1,7 @@
 <?php
 
 /**
- *
- *
- * Created on Mar 24, 2009
+ * API userrights module
  *
  * Copyright © 2009 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
  *
@@ -59,6 +57,41 @@ class ApiUserrights extends ApiBase {
 
                $params = $this->extractRequestParams();
 
+               // Figure out expiry times from the input
+               // $params['expiry'] may not be set in subclasses
+               if ( isset( $params['expiry'] ) ) {
+                       $expiry = (array)$params['expiry'];
+               } else {
+                       $expiry = [ 'infinity' ];
+               }
+               if ( count( $expiry ) !== count( $params['add'] ) ) {
+                       if ( count( $expiry ) === 1 ) {
+                               $expiry = array_fill( 0, count( $params['add'] ), $expiry[0] );
+                       } else {
+                               $this->dieWithError( [
+                                       'apierror-toofewexpiries',
+                                       count( $expiry ),
+                                       count( $params['add'] )
+                               ] );
+                       }
+               }
+
+               // Validate the expiries
+               $groupExpiries = [];
+               foreach ( $expiry as $index => $expiryValue ) {
+                       $group = $params['add'][$index];
+                       $groupExpiries[$group] = UserrightsPage::expiryToTimestamp( $expiryValue );
+
+                       if ( $groupExpiries[$group] === false ) {
+                               $this->dieWithError( [ 'apierror-invalidexpiry', wfEscapeWikiText( $expiryValue ) ] );
+                       }
+
+                       // not allowed to have things expiring in the past
+                       if ( $groupExpiries[$group] && $groupExpiries[$group] < wfTimestampNow() ) {
+                               $this->dieWithError( [ 'apierror-pastexpiry', wfEscapeWikiText( $expiryValue ) ] );
+                       }
+               }
+
                $user = $this->getUrUser( $params );
 
                $tags = $params['tags'];
@@ -76,8 +109,8 @@ class ApiUserrights extends ApiBase {
                $r['user'] = $user->getName();
                $r['userid'] = $user->getId();
                list( $r['added'], $r['removed'] ) = $form->doSaveUserGroups(
-                       $user, (array)$params['add'],
-                       (array)$params['remove'], $params['reason'], $tags
+                       $user, (array)$params['add'], (array)$params['remove'],
+                       $params['reason'], $tags, $groupExpiries
                );
 
                $result = $this->getResult();
@@ -120,7 +153,7 @@ class ApiUserrights extends ApiBase {
        }
 
        public function getAllowedParams() {
-               return [
+               $a = [
                        'user' => [
                                ApiBase::PARAM_TYPE => 'user',
                        ],
@@ -131,6 +164,11 @@ class ApiUserrights extends ApiBase {
                                ApiBase::PARAM_TYPE => $this->getAllGroups(),
                                ApiBase::PARAM_ISMULTI => true
                        ],
+                       'expiry' => [
+                               ApiBase::PARAM_ISMULTI => true,
+                               ApiBase::PARAM_ALLOW_DUPLICATES => true,
+                               ApiBase::PARAM_DFLT => 'infinite',
+                       ],
                        'remove' => [
                                ApiBase::PARAM_TYPE => $this->getAllGroups(),
                                ApiBase::PARAM_ISMULTI => true
@@ -147,6 +185,10 @@ class ApiUserrights extends ApiBase {
                                ApiBase::PARAM_ISMULTI => true
                        ],
                ];
+               if ( !$this->getUserRightsPage()->canProcessExpiries() ) {
+                       unset( $a['expiry'] );
+               }
+               return $a;
        }
 
        public function needsToken() {
@@ -158,15 +200,20 @@ class ApiUserrights extends ApiBase {
        }
 
        protected function getExamplesMessages() {
-               return [
+               $a = [
                        'action=userrights&user=FooBot&add=bot&remove=sysop|bureaucrat&token=123ABC'
                                => 'apihelp-userrights-example-user',
                        'action=userrights&userid=123&add=bot&remove=sysop|bureaucrat&token=123ABC'
                                => 'apihelp-userrights-example-userid',
                ];
+               if ( $this->getUserRightsPage()->canProcessExpiries() ) {
+                       $a['action=userrights&user=SometimeSysop&add=sysop&expiry=1%20month&token=123ABC']
+                               = 'apihelp-userrights-example-expiry';
+               }
+               return $a;
        }
 
        public function getHelpUrls() {
-               return 'https://www.mediawiki.org/wiki/API:User_group_membership';
+               return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:User_group_membership';
        }
 }