Update to r32375 / bug 11874 -- !important may have whitespace between ! and important
[lhc/web/wiklou.git] / includes / SpecialUserrights.php
index 1b1367d..0e2cf56 100644 (file)
  * @addtogroup SpecialPage
  */
 class UserrightsPage extends SpecialPage {
+       # The target of the local right-adjuster's interest.  Can be gotten from
+       # either a GET parameter or a subpage-style parameter, so have a member
+       # variable for it.
+       protected $mTarget;
+       protected $isself = false;
+
        public function __construct() {
                parent::__construct( 'Userrights' );
        }
@@ -23,17 +29,44 @@ class UserrightsPage extends SpecialPage {
 
        public function userCanExecute( $user ) {
                $available = $this->changeableGroups();
-               return !empty( $available['add'] ) or !empty( $available['remove'] );
+               return !empty( $available['add'] ) 
+                       or !empty( $available['remove'] )
+                       or ($this->isself and 
+                               (!empty( $available['add-self'] )
+                                or !empty( $available['remove-self'] )));
        }
 
        /**
         * Manage forms to be shown according to posted data.
         * Depending on the submit button used, call a form or a save function.
+        *
+        * @param mixed $par String if any subpage provided, else null
         */
-       function execute() {
+       function execute( $par ) {
                // If the visitor doesn't have permissions to assign or remove
                // any groups, it's a bit silly to give them the user search prompt.
-               global $wgUser;
+               global $wgUser, $wgRequest;
+
+               if( $par ) {
+                       $this->mTarget = $par;
+               } else {
+                       $this->mTarget = $wgRequest->getVal( 'user' );
+               }
+
+               if (!$this->mTarget) {
+                       /*
+                        * If the user specified no target, and they can only
+                        * edit their own groups, automatically set them as the
+                        * target.
+                        */
+                       $available = $this->changeableGroups();
+                       if (empty($available['add']) && empty($available['remove']))
+                               $this->mTarget = $wgUser->getName();
+               }
+
+               if ($this->mTarget == $wgUser->getName())
+                       $this->isself = true;
+
                if( !$this->userCanExecute( $wgUser ) ) {
                        // fixme... there may be intermediate groups we can mention.
                        global $wgOut;
@@ -44,30 +77,38 @@ class UserrightsPage extends SpecialPage {
                        return;
                }
 
+               if ( wfReadOnly() ) {
+                       global $wgOut;
+                       $wgOut->readOnlyPage();
+                       return;
+               }
+
+               $this->outputHeader();
+
                $this->setHeaders();
 
                // show the general form
                $this->switchForm();
 
-               global $wgRequest;
                if( $wgRequest->wasPosted() ) {
-                       // show some more forms
-                       if( $wgRequest->getCheck( 'ssearchuser' ) ) {
-                               $this->editUserGroupsForm( $wgRequest->getVal( 'user-editname' ) );
-                       }
-
                        // save settings
                        if( $wgRequest->getCheck( 'saveusergroups' ) ) {
-                               $username = $wgRequest->getVal( 'user-editname' );
                                $reason = $wgRequest->getVal( 'user-reason' );
-                               if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $username ) ) {
-                                       $this->saveUserGroups( $username,
+                               if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $this->mTarget ) ) {
+                                       $this->saveUserGroups(
+                                               $this->mTarget,
                                                $wgRequest->getArray( 'removable' ),
                                                $wgRequest->getArray( 'available' ),
-                                               $reason );
+                                               $reason
+                                       );
                                }
                        }
                }
+
+               // show some more forms
+               if( $this->mTarget ) {
+                       $this->editUserGroupsForm( $this->mTarget );
+               }
        }
 
        /**
@@ -81,6 +122,8 @@ class UserrightsPage extends SpecialPage {
         * @return null
         */
        function saveUserGroups( $username, $removegroup, $addgroup, $reason = '') {
+               global $wgUser, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
+
                $user = $this->fetchUser( $username );
                if( !$user ) {
                        return;
@@ -88,10 +131,18 @@ class UserrightsPage extends SpecialPage {
                
                // Validate input set...
                $changeable = $this->changeableGroups();
+               if ($wgUser->getId() != 0 && $wgUser->getId() == $user->getId()) {
+                       $addable = array_merge($changeable['add'], $wgGroupsAddToSelf);
+                       $removable = array_merge($changeable['remove'], $wgGroupsRemoveFromSelf);
+               } else {
+                       $addable = $changeable['add'];
+                       $removable = $changeable['remove'];
+               }
+
                $removegroup = array_unique(
-                       array_intersect( (array)$removegroup, $changeable['remove'] ) );
+                       array_intersect( (array)$removegroup, $removable ) );
                $addgroup = array_unique(
-                       array_intersect( (array)$addgroup, $changeable['add'] ) );
+                       array_intersect( (array)$addgroup, $addable ) );
 
                $oldGroups = $user->getGroups();
                $newGroups = $oldGroups;
@@ -120,17 +171,19 @@ class UserrightsPage extends SpecialPage {
                        wfRunHooks( 'UserRights', array( &$user, $addgroup, $removegroup ) );
                }
 
-               $log = new LogPage( 'rights' );
+               if( $newGroups != $oldGroups ) {
+                       $log = new LogPage( 'rights' );
 
-               global $wgRequest;
-               $log->addEntry( 'rights',
-                       $user->getUserPage(),
-                       $wgRequest->getText( 'user-reason' ),
-                       array(
-                               $this->makeGroupNameList( $oldGroups ),
-                               $this->makeGroupNameList( $newGroups )
-                       )
-               );
+                       global $wgRequest;
+                       $log->addEntry( 'rights',
+                               $user->getUserPage(),
+                               $wgRequest->getText( 'user-reason' ),
+                               array(
+                                       $this->makeGroupNameList( $oldGroups ),
+                                       $this->makeGroupNameList( $newGroups )
+                               )
+                       );
+               }
        }
 
        /**
@@ -172,17 +225,17 @@ class UserrightsPage extends SpecialPage {
                        list( $name, $database ) = array_map( 'trim', $parts );
 
                        if( !$wgUser->isAllowed( 'userrights-interwiki' ) ) {
-                               $wgOut->addWikiText( wfMsg( 'userrights-no-interwiki' ) );
+                               $wgOut->addWikiMsg( 'userrights-no-interwiki' );
                                return null;
                        }
                        if( !UserRightsProxy::validDatabase( $database ) ) {
-                               $wgOut->addWikiText( wfMsg( 'userrights-nodatabase', $database ) );
+                               $wgOut->addWikiMsg( 'userrights-nodatabase', $database );
                                return null;
                        }
                }
                
                if( $name == '' ) {
-                       $wgOut->addWikiText( wfMsg( 'nouserspecified' ) );
+                       $wgOut->addWikiMsg( 'nouserspecified' );
                        return false;
                }
                
@@ -198,7 +251,7 @@ class UserrightsPage extends SpecialPage {
                        }
                        
                        if( !$name ) {
-                               $wgOut->addWikiText( wfMsg( 'noname' ) );
+                               $wgOut->addWikiMsg( 'noname' );
                                return null;
                        }
                }
@@ -210,7 +263,7 @@ class UserrightsPage extends SpecialPage {
                }
                
                if( !$user || $user->isAnon() ) {
-                       $wgOut->addWikiText( wfMsg( 'nosuchusershort', wfEscapeWikiText( $username ) ) );
+                       $wgOut->addWikiMsg( 'nosuchusershort', $username );
                        return null;
                }
                
@@ -225,15 +278,17 @@ class UserrightsPage extends SpecialPage {
         * Output a form to allow searching for a user
         */
        function switchForm() {
-               global $wgOut, $wgRequest;
-               $username = $wgRequest->getText( 'user-editname' );
-               $form  = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->escapeLocalURL(), 'name' => 'uluser' ) );
-               $form .= '<fieldset><legend>' . wfMsgHtml( 'userrights-lookup-user' ) . '</legend>';
-               $form .= '<p>' . Xml::inputLabel( wfMsg( 'userrights-user-editname' ), 'user-editname', 'username', 30, $username ) . '</p>';
-               $form .= '<p>' . Xml::submitButton( wfMsg( 'editusergroup' ), array( 'name' => 'ssearchuser' ) ) . '</p>';
-               $form .= '</fieldset>';
-               $form .= '</form>';
-               $wgOut->addHTML( $form );
+               global $wgOut, $wgScript;
+               $wgOut->addHTML(
+                       Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'name' => 'uluser', 'id' => 'mw-userrights-form1' ) ) .
+                       Xml::hidden( 'title',  'Special:Userrights' ) .
+                       Xml::openElement( 'fieldset' ) .
+                       Xml::element( 'legend', array(), wfMsg( 'userrights-lookup-user' ) ) .
+                       Xml::inputLabel( wfMsg( 'userrights-user-editname' ), 'user', 'username', 30, $this->mTarget ) . ' ' .
+                       Xml::submitButton( wfMsg( 'editusergroup' ) ) .
+                       Xml::closeElement( 'fieldset' ) .
+                       Xml::closeElement( 'form' ) . "\n"
+               );
        }
        
        /**
@@ -245,9 +300,15 @@ class UserrightsPage extends SpecialPage {
         * @return Array:  Tuple of addable, then removable groups
         */
        protected function splitGroups( $groups ) {
+               global $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
                list($addable, $removable) = array_values( $this->changeableGroups() );
-               $removable = array_intersect($removable, $groups ); // Can't remove groups the user doesn't have
-               $addable   = array_diff(     $addable,   $groups ); // Can't add groups the user does have
+               
+               $removable = array_intersect(
+                               array_merge($this->isself ? $wgGroupsRemoveFromSelf : array(), $removable), 
+                               $groups ); // Can't remove groups the user doesn't have
+               $addable   = array_diff(
+                               array_merge($this->isself ? $wgGroupsAddToSelf : array(), $addable),
+                               $groups ); // Can't add groups the user does have
                
                return array( $addable, $removable );
        }
@@ -255,7 +316,6 @@ class UserrightsPage extends SpecialPage {
        /**
         * Show the form to edit group memberships.
         *
-        * @todo make all CSS-y and semantic
         * @param $user      User or UserRightsProxy you're editing
         * @param $groups    Array:  Array of groups the user is in
         */
@@ -273,51 +333,54 @@ class UserrightsPage extends SpecialPage {
                        $grouplist = '<p>' . wfMsgHtml( 'userrights-groupsmember' ) . ' ' . implode( ', ', $list ) . '</p>';
                }
                $wgOut->addHTML(
-                       Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->escapeLocalURL(), 'name' => 'editGroup' ) ) .
-                       Xml::hidden( 'user-editname', $user->getName() ) .
-                       Xml::hidden( 'wpEditToken', $wgUser->editToken( $user->getName() ) ) .
+                       Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL(), 'name' => 'editGroup', 'id' => 'mw-userrights-form2' ) ) .
+                       Xml::hidden( 'user', $this->mTarget ) .
+                       Xml::hidden( 'wpEditToken', $wgUser->editToken( $this->mTarget ) ) .
                        Xml::openElement( 'fieldset' ) .
                        Xml::element( 'legend', array(), wfMsg( 'userrights-editusergroup' ) ) .
-                       wfMsgExt( 'editinguser', array( 'parse' ),
-                               wfEscapeWikiText( $user->getName() ) ) .
+                       wfMsgExt( 'editinguser', array( 'parse' ), wfEscapeWikiText( $user->getName() ) ) .
                        $grouplist .
                        $this->explainRights() .
-                       "<table border='0'>
-                       <tr>
-                               <td></td>
-                               <td>
-                               <table width='400'>
-                                       <tr>
-                                               <td width='50%'>" . $this->removeSelect( $removable ) . "</td>
-                                               <td width='50%'>" . $this->addSelect( $addable ) . "</td>
-                                       </tr>
-                               </table>
-                       </tr>
-                       <tr>
-                               <td colspan='2'>" .
-                                       $wgOut->parse( wfMsg('userrights-groupshelp') ) .
-                               "</td>
-                       </tr>
-                       <tr>
-                               <td>" .
-                                       Xml::label( wfMsg( 'userrights-reason' ), 'wpReason' ) .
-                               "</td>
-                               <td>" .
-                                       Xml::input( 'user-reason', 60, false, array( 'id' => 'wpReason', 'maxlength' => 255 ) ) .
-                               "</td>
-                       </tr>
-                       <tr>
-                               <td></td>
-                               <td>" .
-                               Xml::submitButton( wfMsg( 'saveusergroups' ), array( 'name' => 'saveusergroups' ) ) .
-                               "</td>
-                       </tr>
-                       </table>\n" .
+                       Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-userrights-table-outer' ) ) .
+                               "<tr>
+                                       <td></td>
+                                       <td>" .
+                                               Xml::openElement( 'table', array( 'style' => 'width:400px;', 'id' => 'mw-userrights-table-inner' ) ) .  
+                                                       "<tr>
+                                                               <td style='width:50%;'>" . 
+                                                                       $this->removeSelect( $removable ) .
+                                                               "</td>
+                                                               <td style='width:50%;'>" . 
+                                                                       $this->addSelect( $addable ) .
+                                                               "</td>
+                                                       </tr>" .
+                                               Xml::closeElement( 'table' ) .
+                               "</tr>
+                               <tr>
+                                       <td colspan='2'>" .
+                                               $wgOut->parse( wfMsg( 'userrights-groupshelp' ) ) .
+                                       "</td>
+                               </tr>
+                               <tr>
+                                       <td>" .
+                                               Xml::label( wfMsg( 'userrights-reason' ), 'wpReason' ) .
+                                       "</td>
+                                       <td>" .
+                                               Xml::input( 'user-reason', 60, false, array( 'id' => 'wpReason', 'maxlength' => 255 ) ) .
+                                       "</td>
+                               </tr>
+                               <tr>
+                                       <td></td>
+                                       <td>" .
+                                               Xml::submitButton( wfMsg( 'saveusergroups' ), array( 'name' => 'saveusergroups' ) ) .
+                                       "</td>
+                               </tr>" .
+                       Xml::closeElement( 'table' ) . "\n" .
                        Xml::closeElement( 'fieldset' ) .
                        Xml::closeElement( 'form' ) . "\n"
                );
        }
-       
+
        /**
         * Format a link to a group description page
         *
@@ -340,12 +403,20 @@ class UserrightsPage extends SpecialPage {
                global $wgUser, $wgLang;
 
                $out = array();
-               list( $add, $remove ) = array_values( $this->changeableGroups() );
+               list( $add, $remove, $addself, $rmself ) = array_values( $this->changeableGroups() );
 
                if( count( $add ) > 0 )
-                       $out[] = wfMsgExt( 'userrights-available-add', 'parseinline', $wgLang->listToText( $add ), count( $add ) );
+                       $out[] = wfMsgExt( 'userrights-available-add', 'parseinline', 
+                                       $wgLang->listToText( $add ), count( $add ) );
                if( count( $remove ) > 0 )
-                       $out[] = wfMsgExt( 'userrights-available-remove', 'parseinline', $wgLang->listToText( $remove ), count( $add ) );
+                       $out[] = wfMsgExt( 'userrights-available-remove', 'parseinline', 
+                                       $wgLang->listToText( $remove ), count( $add ) );
+               if( count( $addself ) > 0 )
+                       $out[] = wfMsgExt( 'userrights-available-add-self', 'parseinline',
+                                       $wgLang->listToText( $addself ), count( $addself ) );
+               if( count( $rmself ) > 0 )
+                       $out[] = wfMsgExt( 'userrights-available-remove-self', 'parseinline',
+                                       $wgLang->listToText( $rmself ), count( $rmself ) );
 
                return count( $out ) > 0
                        ? implode( '<br />', $out )
@@ -421,7 +492,7 @@ class UserrightsPage extends SpecialPage {
         * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) )
         */
        function changeableGroups() {
-               global $wgUser;
+               global $wgUser, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
 
                if( $wgUser->isAllowed( 'userrights' ) ) {
                        // This group gives the right to modify everything (reverse-
@@ -431,12 +502,18 @@ class UserrightsPage extends SpecialPage {
                        $all = array_merge( User::getAllGroups() );
                        return array(
                                'add' => $all,
-                               'remove' => $all
+                               'remove' => $all,
+                               'add-self' => array(),
+                               'remove-self' => array()
                        );
                }
 
                // Okay, it's not so simple, we will have to go through the arrays
-               $groups = array( 'add' => array(), 'remove' => array() );
+               $groups = array( 
+                               'add' => array(), 
+                               'remove' => array(),
+                               'add-self' => $wgGroupsAddToSelf,
+                               'remove-self' => $wgGroupsRemoveFromSelf);
                $addergroups = $wgUser->getEffectiveGroups();
 
                foreach ($addergroups as $addergroup) {
@@ -495,8 +572,7 @@ class UserrightsPage extends SpecialPage {
                                )
                        )
                );
-               $output->addHtml( "<h2>" . htmlspecialchars( LogPage::logName( 'rights' ) ) . "</h2>\n" );
+               $output->addHtml( Xml::element( 'h2', null, LogPage::logName( 'rights' ) . "\n" ) );
                $viewer->showList( $output );
        }
-       
 }