Fix disabling of APC cache when loading message files: apc.enabled has been PHP_INI_S...
[lhc/web/wiklou.git] / includes / specials / SpecialUserrights.php
index 7add7b4..10c5dd3 100644 (file)
@@ -34,8 +34,8 @@ class UserrightsPage extends SpecialPage {
                return !empty( $available['add'] )
                        or !empty( $available['remove'] )
                        or ( ( $this->isself || !$checkIfSelf ) and
-                               (!empty( $available['add-self'] )
-                                or !empty( $available['remove-self'] )));
+                               ( !empty( $available['add-self'] )
+                                or !empty( $available['remove-self'] ) ) );
        }
 
        /**
@@ -44,7 +44,7 @@ class UserrightsPage extends SpecialPage {
         *
         * @param $par Mixed: string if any subpage provided, else null
         */
-       function execute( $par ) {
+       public 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, $wgRequest;
@@ -54,28 +54,39 @@ class UserrightsPage extends SpecialPage {
                } else {
                        $this->mTarget = $wgRequest->getVal( 'user' );
                }
+               $this->mTarget = User::getCanonicalName( $this->mTarget );
+
+               /*
+                * If the user is blocked and they only have "partial" access
+                * (e.g. they don't have the userrights permission), then don't
+                * allow them to use Special:UserRights.
+                */
+               if( $wgUser->isBlocked() && !$wgUser->isAllowed( 'userrights' ) ) {
+                       $wgOut->blockedPage();
+                       return;
+               }
 
-               if (!$this->mTarget) {
+               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']))
+                       if ( empty( $available['add'] ) && empty( $available['remove'] ) )
                                $this->mTarget = $wgUser->getName();
                }
 
-               if ($this->mTarget == $wgUser->getName())
+               if ( $this->mTarget == $wgUser->getName() )
                        $this->isself = true;
 
                if( !$this->userCanChangeRights( $wgUser, true ) ) {
                        // fixme... there may be intermediate groups we can mention.
                        global $wgOut;
-                       $wgOut->showPermissionsErrorPage( array(
+                       $wgOut->showPermissionsErrorPage( array( array(
                                $wgUser->isAnon()
                                        ? 'userrights-nologin'
-                                       : 'userrights-notallowed' ) );
+                                       : 'userrights-notallowed' ) ) );
                        return;
                }
 
@@ -102,9 +113,9 @@ class UserrightsPage extends SpecialPage {
                                                $this->mTarget,
                                                $reason
                                        );
-                                       
+
                                        global $wgOut;
-                                       
+
                                        $url = $this->getSuccessURL();
                                        $wgOut->redirect( $url );
                                        return;
@@ -117,7 +128,7 @@ class UserrightsPage extends SpecialPage {
                        $this->editUserGroupsForm( $this->mTarget );
                }
        }
-       
+
        function getSuccessURL() {
                return $this->getTitle( $this->mTarget )->getFullURL();
        }
@@ -130,11 +141,12 @@ class UserrightsPage extends SpecialPage {
         * @param $reason String: reason for group change
         * @return null
         */
-       function saveUserGroups( $username, $reason = '') {
+       function saveUserGroups( $username, $reason = '' ) {
                global $wgRequest, $wgUser, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
 
                $user = $this->fetchUser( $username );
-               if( !$user ) {
+               if( $user instanceof WikiErrorMsg ) {
+                       $wgOut->addWikiMsgArray( $user->getMessageKey(), $user->getMessageArgs() );
                        return;
                }
 
@@ -144,18 +156,19 @@ class UserrightsPage extends SpecialPage {
 
                // This could possibly create a highly unlikely race condition if permissions are changed between
                //  when the form is loaded and when the form is saved. Ignoring it for the moment.
-               foreach ($allgroups as $group) {
+               foreach ( $allgroups as $group ) {
                        // We'll tell it to remove all unchecked groups, and add all checked groups.
                        // Later on, this gets filtered for what can actually be removed
-                       if ($wgRequest->getCheck( "wpGroup-$group" )) {
+                       if ( $wgRequest->getCheck( "wpGroup-$group" ) ) {
                                $addgroup[] = $group;
                        } else {
                                $removegroup[] = $group;
                        }
                }
-               self::doSaveUserGroups( $user, $addgroup, $removegroup, $reason );
+               
+               $this->doSaveUserGroups( $user, $addgroup, $removegroup, $reason );
        }
-       
+
        /**
         * Save user groups changes in the database.
         *
@@ -165,13 +178,13 @@ class UserrightsPage extends SpecialPage {
         * @param $reason String: reason for group change
         * @return Array: Tuple of added, then removed groups
         */
-       static function doSaveUserGroups( $user, $add, $remove, $reason = '' ) {
+       function doSaveUserGroups( $user, $add, $remove, $reason = '' ) {
                global $wgUser;
 
                // Validate input set...
-               $isself = ($user->getName() == $wgUser->getName());
+               $isself = ( $user->getName() == $wgUser->getName() );
                $groups = $user->getGroups();
-               $changeable = $wgUser->changeableGroups();
+               $changeable = $this->changeableGroups();
                $addable = array_merge( $changeable['add'], $isself ? $changeable['add-self'] : array() );
                $removable = array_merge( $changeable['remove'], $isself ? $changeable['remove-self'] : array() );
 
@@ -179,19 +192,21 @@ class UserrightsPage extends SpecialPage {
                        array_intersect( (array)$remove, $removable, $groups ) );
                $add = array_unique( array_diff(
                        array_intersect( (array)$add, $addable ),
-                       $groups ) );
+                       $groups )
+               );
 
                $oldGroups = $user->getGroups();
                $newGroups = $oldGroups;
+
                // remove then add groups
                if( $remove ) {
-                       $newGroups = array_diff($newGroups, $remove);
+                       $newGroups = array_diff( $newGroups, $remove );
                        foreach( $remove as $group ) {
                                $user->removeGroup( $group );
                        }
                }
                if( $add ) {
-                       $newGroups = array_merge($newGroups, $add);
+                       $newGroups = array_merge( $newGroups, $add );
                        foreach( $add as $group ) {
                                $user->addGroup( $group );
                        }
@@ -206,31 +221,24 @@ class UserrightsPage extends SpecialPage {
                wfRunHooks( 'UserRights', array( &$user, $add, $remove ) );
 
                if( $newGroups != $oldGroups ) {
-                       self::addLogEntry( $user, $oldGroups, $newGroups, $reason );
+                       $this->addLogEntry( $user, $oldGroups, $newGroups, $reason );
                }
                return array( $add, $remove );
        }
 
-       
+
        /**
         * Add a rights log entry for an action.
         */
-       static function addLogEntry( $user, $oldGroups, $newGroups, $reason ) {
-               // Just overriding addLogEntry in a subclass would be cleaner,
-               // but that requires PHP 5.3 (late static bindings)
-               if( !wfRunHooks( 'UserRightsLogEntry', array( $user, $oldGroups,
-                               $newGroups, $reason ) ) ) {
-                       return;
-               }
-               
+       function addLogEntry( $user, $oldGroups, $newGroups, $reason ) {
                $log = new LogPage( 'rights' );
 
                $log->addEntry( 'rights',
                        $user->getUserPage(),
                        $reason,
                        array(
-                               self::makeGroupNameListForLog( $oldGroups ),
-                               self::makeGroupNameListForLog( $newGroups )
+                               $this->makeGroupNameListForLog( $oldGroups ),
+                               $this->makeGroupNameListForLog( $newGroups )
                        )
                );
        }
@@ -243,7 +251,8 @@ class UserrightsPage extends SpecialPage {
                global $wgOut;
 
                $user = $this->fetchUser( $username );
-               if( !$user ) {
+               if( $user instanceof WikiErrorMsg ) {
+                       $wgOut->addWikiMsgArray( $user->getMessageKey(), $user->getMessageArgs() );
                        return;
                }
 
@@ -261,10 +270,10 @@ class UserrightsPage extends SpecialPage {
         * return a user (or proxy) object for manipulating it.
         *
         * Side effects: error output for invalid access
-        * @return mixed User, UserRightsProxy, or null
+        * @return mixed User, UserRightsProxy, or WikiErrorMsg
         */
        function fetchUser( $username ) {
-               global $wgOut, $wgUser, $wgUserrightsInterwikiDelimiter;
+               global $wgUser, $wgUserrightsInterwikiDelimiter;
 
                $parts = explode( $wgUserrightsInterwikiDelimiter, $username );
                if( count( $parts ) < 2 ) {
@@ -272,20 +281,21 @@ class UserrightsPage extends SpecialPage {
                        $database = '';
                } else {
                        list( $name, $database ) = array_map( 'trim', $parts );
-
-                       if( !$wgUser->isAllowed( 'userrights-interwiki' ) ) {
-                               $wgOut->addWikiMsg( 'userrights-no-interwiki' );
-                               return null;
-                       }
-                       if( !UserRightsProxy::validDatabase( $database ) ) {
-                               $wgOut->addWikiMsg( 'userrights-nodatabase', $database );
-                               return null;
+                       
+                       if( $database == wfWikiID() ) {
+                               $database = '';
+                       } else {
+                               if( !$wgUser->isAllowed( 'userrights-interwiki' ) ) {
+                                       return new WikiErrorMsg( 'userrights-no-interwiki' );
+                               }
+                               if( !UserRightsProxy::validDatabase( $database ) ) {
+                                       return new WikiErrorMsg( 'userrights-nodatabase', $database );
+                               }
                        }
                }
 
                if( $name == '' ) {
-                       $wgOut->addWikiMsg( 'nouserspecified' );
-                       return false;
+                       return new WikiErrorMsg( 'nouserspecified' );
                }
 
                if( $name{0} == '#' ) {
@@ -300,8 +310,7 @@ class UserrightsPage extends SpecialPage {
                        }
 
                        if( !$name ) {
-                               $wgOut->addWikiMsg( 'noname' );
-                               return null;
+                               return new WikiErrorMsg( 'noname' );
                        }
                }
 
@@ -312,14 +321,13 @@ class UserrightsPage extends SpecialPage {
                }
 
                if( !$user || $user->isAnon() ) {
-                       $wgOut->addWikiMsg( 'nosuchusershort', $username );
-                       return null;
+                       return new WikiErrorMsg( 'nosuchusershort', $username );
                }
 
                return $user;
        }
 
-       static function makeGroupNameList( $ids ) {
+       function makeGroupNameList( $ids ) {
                if( empty( $ids ) ) {
                        return wfMsgForContent( 'rightsnone' );
                } else {
@@ -327,11 +335,11 @@ class UserrightsPage extends SpecialPage {
                }
        }
 
-       static function makeGroupNameListForLog( $ids ) {
+       function makeGroupNameListForLog( $ids ) {
                if( empty( $ids ) ) {
                        return '';
                } else {
-                       return self::makeGroupNameList( $ids );
+                       return $this->makeGroupNameList( $ids );
                }
        }
 
@@ -361,14 +369,16 @@ class UserrightsPage extends SpecialPage {
         * @return Array:  Tuple of addable, then removable groups
         */
        protected function splitGroups( $groups ) {
-               list($addable, $removable, $addself, $removeself) = array_values( $this->changeableGroups() );
+               list( $addable, $removable, $addself, $removeself ) = array_values( $this->changeableGroups() );
 
                $removable = array_intersect(
-                               array_merge( $this->isself ? $removeself : array(), $removable ),
-                               $groups ); // Can't remove groups the user doesn't have
-               $addable   = array_diff(
-                               array_merge( $this->isself ? $addself : array(), $addable ),
-                               $groups ); // Can't add groups the user does have
+                       array_merge( $this->isself ? $removeself : array(), $removable ),
+                       $groups
+               ); // Can't remove groups the user doesn't have
+               $addable = array_diff(
+                       array_merge( $this->isself ? $addself : array(), $addable ),
+                       $groups
+               ); // Can't add groups the user does have
 
                return array( $addable, $removable );
        }
@@ -431,17 +441,17 @@ class UserrightsPage extends SpecialPage {
        private static function buildGroupLink( $group ) {
                static $cache = array();
                if( !isset( $cache[$group] ) )
-                       $cache[$group] = User::makeGroupLinkHtml( $group, User::getGroupName( $group ) );
+                       $cache[$group] = User::makeGroupLinkHtml( $group, htmlspecialchars( User::getGroupName( $group ) ) );
                return $cache[$group];
        }
-       
+
        /**
         * Returns an array of all groups that may be edited
         * @return array Array of groups that may be edited.
         */
-        protected static function getAllGroups() {
-               return User::getAllGroups();
-        }
+       protected static function getAllGroups() {
+               return User::getAllGroups();
+       }
 
        /**
         * Adds a table with checkboxes where you can select what groups to add/remove
@@ -453,11 +463,11 @@ class UserrightsPage extends SpecialPage {
                $allgroups = $this->getAllGroups();
                $ret = '';
 
-               $column = 1;
-               $settable_col = '';
-               $unsettable_col = '';
+               # Put all column info into an associative array so that extensions can
+               # more easily manage it.
+               $columns = array( 'unchangeable' => array(), 'changeable' => array() );
 
-               foreach ($allgroups as $group) {
+               foreach( $allgroups as $group ) {
                        $set = in_array( $group, $usergroups );
                        # Should the checkbox be disabled?
                        $disabled = !(
@@ -465,53 +475,54 @@ class UserrightsPage extends SpecialPage {
                                ( !$set && $this->canAdd( $group ) ) );
                        # Do we need to point out that this action is irreversible?
                        $irreversible = !$disabled && (
-                               ($set && !$this->canAdd( $group )) ||
-                               (!$set && !$this->canRemove( $group ) ) );
-
-                       $attr = $disabled ? array( 'disabled' => 'disabled' ) : array();
-                       $text = $irreversible
-                               ? wfMsgHtml( 'userrights-irreversible-marker', User::getGroupMember( $group ) )
-                               : User::getGroupMember( $group );
-                       $checkbox = Xml::checkLabel( $text, "wpGroup-$group",
-                               "wpGroup-$group", $set, $attr );
-                       $checkbox = $disabled ? Xml::tags( 'span', array( 'class' => 'mw-userrights-disabled' ), $checkbox ) : $checkbox;
-
-                       if ($disabled) {
-                               $unsettable_col .= "$checkbox<br />\n";
+                               ( $set && !$this->canAdd( $group ) ) ||
+                               ( !$set && !$this->canRemove( $group ) ) );
+
+                       $checkbox = array(
+                               'set' => $set,
+                               'disabled' => $disabled,
+                               'irreversible' => $irreversible
+                       );
+
+                       if( $disabled ) {
+                               $columns['unchangeable'][$group] = $checkbox;
                        } else {
-                               $settable_col .= "$checkbox<br />\n";
+                               $columns['changeable'][$group] = $checkbox;
                        }
                }
 
-               if ($column) {
-                       $ret .= Xml::openElement( 'table', array( 'border' => '0', 'class' => 'mw-userrights-groups' ) ) .
-                               "<tr>
-";
-                       if( $settable_col !== '' ) {
-                               $ret .= xml::element( 'th', null, wfMsg( 'userrights-changeable-col' ) );
-                       }
-                       if( $unsettable_col !== '' ) {
-                               $ret .= xml::element( 'th', null, wfMsg( 'userrights-unchangeable-col' ) );
-                       }
-                       $ret.= "</tr>
-                               <tr>
-";
-                       if( $settable_col !== '' ) {
-                               $ret .=
-"                                      <td style='vertical-align:top;'>
-                                               $settable_col
-                                       </td>
-";
-                       }
-                       if( $unsettable_col !== '' ) {
-                               $ret .=
-"                                      <td style='vertical-align:top;'>
-                                               $unsettable_col
-                                       </td>
-";
+               # Build the HTML table
+               $ret .= Xml::openElement( 'table', array( 'border' => '0', 'class' => 'mw-userrights-groups' ) ) .
+                       "<tr>\n";
+               foreach( $columns as $name => $column ) {
+                       if( $column === array() )
+                               continue;
+                       $ret .= xml::element( 'th', null, wfMsg( 'userrights-' . $name . '-col' ) );
+               }
+               $ret.= "</tr>\n<tr>\n";
+               foreach( $columns as $column ) {
+                       if( $column === array() )
+                               continue;
+                       $ret .= "\t<td style='vertical-align:top;'>\n";
+                       foreach( $column as $group => $checkbox ) {
+                               $attr = $checkbox['disabled'] ? array( 'disabled' => 'disabled' ) : array();
+
+                               if ( $checkbox['irreversible'] ) {
+                                       $text = htmlspecialchars( wfMsg( 'userrights-irreversible-marker', 
+                                               User::getGroupMember( $group ) ) );
+                               } else {
+                                       $text = htmlspecialchars( User::getGroupMember( $group ) );
+                               }
+                               $checkboxHtml = Xml::checkLabel( $text, "wpGroup-" . $group,
+                                       "wpGroup-" . $group, $checkbox['set'], $attr );
+                               $ret .= "\t\t" . ( $checkbox['disabled']
+                                       ? Xml::tags( 'span', array( 'class' => 'mw-userrights-disabled' ), $checkboxHtml )
+                                       : $checkboxHtml
+                               ) . "<br />\n";
                        }
-                       $ret .= Xml::closeElement( 'tr' ) . Xml::closeElement( 'table' );
+                       $ret .= "\t</td>\n";
                }
+               $ret .= Xml::closeElement( 'tr' ) . Xml::closeElement( 'table' );
 
                return $ret;
        }
@@ -524,7 +535,7 @@ class UserrightsPage extends SpecialPage {
                // $this->changeableGroups()['remove'] doesn't work, of course. Thanks,
                // PHP.
                $groups = $this->changeableGroups();
-               return in_array( $group, $groups['remove'] ) || ($this->isself && in_array( $group, $groups['remove-self'] ));
+               return in_array( $group, $groups['remove'] ) || ( $this->isself && in_array( $group, $groups['remove-self'] ) );
        }
 
        /**
@@ -533,7 +544,7 @@ class UserrightsPage extends SpecialPage {
         */
        private function canAdd( $group ) {
                $groups = $this->changeableGroups();
-               return in_array( $group, $groups['add'] ) || ($this->isself && in_array( $group, $groups['add-self'] ));
+               return in_array( $group, $groups['add'] ) || ( $this->isself && in_array( $group, $groups['add-self'] ) );
        }
 
        /**
@@ -543,11 +554,7 @@ class UserrightsPage extends SpecialPage {
         */
        function changeableGroups() {
                global $wgUser;
-               $groups = $wgUser->changeableGroups();
-               // Run a hook because we can
-               wfRunHooks( 'UserrightsChangeableGroups', array( $this,
-                       $wgUser, $wgUser->getEffectiveGroups(), &$groups ) );
-               return $groups;
+               return $wgUser->changeableGroups();
        }
 
        /**