Fix for r75313: add array_unique(), otherwise groups are listed twice if they are...
[lhc/web/wiklou.git] / includes / specials / SpecialListgrouprights.php
1 <?php
2 /**
3 * Implements Special:Listgrouprights
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * This special page lists all defined user groups and the associated rights.
26 * See also @ref $wgGroupPermissions.
27 *
28 * @ingroup SpecialPage
29 * @author Petr Kadlec <mormegil@centrum.cz>
30 */
31 class SpecialListGroupRights extends SpecialPage {
32
33 var $skin;
34
35 /**
36 * Constructor
37 */
38 function __construct() {
39 global $wgUser;
40 parent::__construct( 'Listgrouprights' );
41 $this->skin = $wgUser->getSkin();
42 }
43
44 /**
45 * Show the special page
46 */
47 public function execute( $par ) {
48 global $wgOut, $wgImplicitGroups;
49 global $wgGroupPermissions, $wgRevokePermissions, $wgAddGroups, $wgRemoveGroups;
50 global $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
51
52 $this->setHeaders();
53 $this->outputHeader();
54
55 $wgOut->addHTML(
56 Xml::openElement( 'table', array( 'class' => 'wikitable mw-listgrouprights-table' ) ) .
57 '<tr>' .
58 Xml::element( 'th', null, wfMsg( 'listgrouprights-group' ) ) .
59 Xml::element( 'th', null, wfMsg( 'listgrouprights-rights' ) ) .
60 '</tr>'
61 );
62
63 $allGroups = array_unique( array_merge( array_keys( $wgGroupPermissions ),
64 array_keys( $wgRevokePermissions ) ) );
65 foreach ( $allGroups as $group ) {
66 $permissions = isset( $wgGroupPermissions[$group] ) ?
67 $wgGroupPermissions[$group] : array();
68 $groupname = ( $group == '*' ) ? 'all' : $group; // Replace * with a more descriptive groupname
69
70 $msg = wfMsg( 'group-' . $groupname );
71 if ( wfEmptyMsg( 'group-' . $groupname, $msg ) || $msg == '' ) {
72 $groupnameLocalized = $groupname;
73 } else {
74 $groupnameLocalized = $msg;
75 }
76
77 $msg = wfMsgForContent( 'grouppage-' . $groupname );
78 if ( wfEmptyMsg( 'grouppage-' . $groupname, $msg ) || $msg == '' ) {
79 $grouppageLocalized = MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
80 } else {
81 $grouppageLocalized = $msg;
82 }
83
84 if( $group == '*' ) {
85 // Do not make a link for the generic * group
86 $grouppage = htmlspecialchars( $groupnameLocalized );
87 } else {
88 $grouppage = $this->skin->link(
89 Title::newFromText( $grouppageLocalized ),
90 htmlspecialchars( $groupnameLocalized )
91 );
92 }
93
94 if ( $group === 'user' ) {
95 // Link to Special:listusers for implicit group 'user'
96 $grouplink = '<br />' . $this->skin->link(
97 SpecialPage::getTitleFor( 'Listusers' ),
98 wfMsgHtml( 'listgrouprights-members' ),
99 array(),
100 array(),
101 array( 'known', 'noclasses' )
102 );
103 } elseif ( !in_array( $group, $wgImplicitGroups ) ) {
104 $grouplink = '<br />' . $this->skin->link(
105 SpecialPage::getTitleFor( 'Listusers' ),
106 wfMsgHtml( 'listgrouprights-members' ),
107 array(),
108 array( 'group' => $group ),
109 array( 'known', 'noclasses' )
110 );
111 } else {
112 // No link to Special:listusers for other implicit groups as they are unlistable
113 $grouplink = '';
114 }
115
116 $revoke = isset( $wgRevokePermissions[$group] ) ? $wgRevokePermissions[$group] : array();
117 $addgroups = isset( $wgAddGroups[$group] ) ? $wgAddGroups[$group] : array();
118 $removegroups = isset( $wgRemoveGroups[$group] ) ? $wgRemoveGroups[$group] : array();
119 $addgroupsSelf = isset( $wgGroupsAddToSelf[$group] ) ? $wgGroupsAddToSelf[$group] : array();
120 $removegroupsSelf = isset( $wgGroupsRemoveFromSelf[$group] ) ? $wgGroupsRemoveFromSelf[$group] : array();
121
122 $id = $group == '*' ? false : Sanitizer::escapeId( $group );
123 $wgOut->addHTML( Html::rawElement( 'tr', array( 'id' => $id ),
124 "
125 <td>$grouppage$grouplink</td>
126 <td>" .
127 self::formatPermissions( $permissions, $revoke, $addgroups, $removegroups, $addgroupsSelf, $removegroupsSelf ) .
128 '</td>
129 '
130 ) );
131 }
132 $wgOut->addHTML(
133 Xml::closeElement( 'table' ) . "\n<br /><hr />\n"
134 );
135 $wgOut->wrapWikiMsg( "<div class=\"mw-listgrouprights-key\">\n$1\n</div>", 'listgrouprights-key' );
136 }
137
138 /**
139 * Create a user-readable list of permissions from the given array.
140 *
141 * @param $permissions Array of permission => bool (from $wgGroupPermissions items)
142 * @param $revoke Array of permission => bool (from $wgRevokePermissions items)
143 * @param $add Array of groups this group is allowed to add or true
144 * @param $remove Array of groups this group is allowed to remove or true
145 * @param $addSelf Array of groups this group is allowed to add to self or true
146 * @param $removeSelf Array of group this group is allowed to remove from self or true
147 * @return string List of all granted permissions, separated by comma separator
148 */
149 private static function formatPermissions( $permissions, $revoke, $add, $remove, $addSelf, $removeSelf ) {
150 global $wgLang;
151
152 $r = array();
153 foreach( $permissions as $permission => $granted ) {
154 //show as granted only if it isn't revoked to prevent duplicate display of permissions
155 if( $granted && ( !isset( $revoke[$permission] ) || !$revoke[$permission] ) ) {
156 $description = wfMsgExt( 'listgrouprights-right-display', array( 'parseinline' ),
157 User::getRightDescription( $permission ),
158 '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
159 );
160 $r[] = $description;
161 }
162 }
163 foreach( $revoke as $permission => $revoked ) {
164 if( $revoked ) {
165 $description = wfMsgExt( 'listgrouprights-right-revoked', array( 'parseinline' ),
166 User::getRightDescription( $permission ),
167 '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
168 );
169 $r[] = $description;
170 }
171 }
172 sort( $r );
173 if( $add === true ){
174 $r[] = wfMsgExt( 'listgrouprights-addgroup-all', array( 'escape' ) );
175 } else if( is_array( $add ) && count( $add ) ) {
176 $add = array_values( array_unique( $add ) );
177 $r[] = wfMsgExt( 'listgrouprights-addgroup', array( 'parseinline' ), $wgLang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $add ) ), count( $add ) );
178 }
179 if( $remove === true ){
180 $r[] = wfMsgExt( 'listgrouprights-removegroup-all', array( 'escape' ) );
181 } else if( is_array( $remove ) && count( $remove ) ) {
182 $remove = array_values( array_unique( $remove ) );
183 $r[] = wfMsgExt( 'listgrouprights-removegroup', array( 'parseinline' ), $wgLang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $remove ) ), count( $remove ) );
184 }
185 if( $addSelf === true ){
186 $r[] = wfMsgExt( 'listgrouprights-addgroup-self-all', array( 'escape' ) );
187 } else if( is_array( $addSelf ) && count( $addSelf ) ) {
188 $addSelf = array_values( array_unique( $addSelf ) );
189 $r[] = wfMsgExt( 'listgrouprights-addgroup-self', array( 'parseinline' ), $wgLang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $addSelf ) ), count( $addSelf ) );
190 }
191 if( $removeSelf === true ){
192 $r[] = wfMsgExt( 'listgrouprights-removegroup-self-all', array( 'escape' ) );
193 } else if( is_array( $removeSelf ) && count( $removeSelf ) ) {
194 $removeSelf = array_values( array_unique( $removeSelf ) );
195 $r[] = wfMsgExt( 'listgrouprights-removegroup-self', array( 'parseinline' ), $wgLang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $removeSelf ) ), count( $removeSelf ) );
196 }
197 if( empty( $r ) ) {
198 return '';
199 } else {
200 return '<ul><li>' . implode( "</li>\n<li>", $r ) . '</li></ul>';
201 }
202 }
203 }