Merge "(bug 17602) fix Monobook action tabs not quite touching the page body"
[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 * Constructor
34 */
35 function __construct() {
36 parent::__construct( 'Listgrouprights' );
37 }
38
39 /**
40 * Show the special page
41 */
42 public function execute( $par ) {
43 global $wgImplicitGroups;
44 global $wgGroupPermissions, $wgRevokePermissions, $wgAddGroups, $wgRemoveGroups;
45 global $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
46
47 $this->setHeaders();
48 $this->outputHeader();
49
50 $out = $this->getOutput();
51 $out->addModuleStyles( 'mediawiki.special' );
52
53 $out->addHTML(
54 Xml::openElement( 'table', array( 'class' => 'wikitable mw-listgrouprights-table' ) ) .
55 '<tr>' .
56 Xml::element( 'th', null, $this->msg( 'listgrouprights-group' )->text() ) .
57 Xml::element( 'th', null, $this->msg( 'listgrouprights-rights' )->text() ) .
58 '</tr>'
59 );
60
61 $allGroups = array_unique( array_merge(
62 array_keys( $wgGroupPermissions ),
63 array_keys( $wgRevokePermissions ),
64 array_keys( $wgAddGroups ),
65 array_keys( $wgRemoveGroups ),
66 array_keys( $wgGroupsAddToSelf ),
67 array_keys( $wgGroupsRemoveFromSelf )
68 ) );
69 asort( $allGroups );
70
71 foreach ( $allGroups as $group ) {
72 $permissions = isset( $wgGroupPermissions[$group] )
73 ? $wgGroupPermissions[$group]
74 : array();
75 $groupname = ( $group == '*' ) // Replace * with a more descriptive groupname
76 ? 'all'
77 : $group;
78
79 $msg = $this->msg( 'group-' . $groupname );
80 $groupnameLocalized = !$msg->isBlank() ? $msg->text() : $groupname;
81
82 $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
83 $grouppageLocalized = !$msg->isBlank() ?
84 $msg->text() :
85 MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
86
87 if ( $group == '*' ) {
88 // Do not make a link for the generic * group
89 $grouppage = htmlspecialchars( $groupnameLocalized );
90 } else {
91 $grouppage = Linker::link(
92 Title::newFromText( $grouppageLocalized ),
93 htmlspecialchars( $groupnameLocalized )
94 );
95 }
96
97 if ( $group === 'user' ) {
98 // Link to Special:listusers for implicit group 'user'
99 $grouplink = '<br />' . Linker::linkKnown(
100 SpecialPage::getTitleFor( 'Listusers' ),
101 $this->msg( 'listgrouprights-members' )->escaped()
102 );
103 } elseif ( !in_array( $group, $wgImplicitGroups ) ) {
104 $grouplink = '<br />' . Linker::linkKnown(
105 SpecialPage::getTitleFor( 'Listusers' ),
106 $this->msg( 'listgrouprights-members' )->escaped(),
107 array(),
108 array( 'group' => $group )
109 );
110 } else {
111 // No link to Special:listusers for other implicit groups as they are unlistable
112 $grouplink = '';
113 }
114
115 $revoke = isset( $wgRevokePermissions[$group] ) ? $wgRevokePermissions[$group] : array();
116 $addgroups = isset( $wgAddGroups[$group] ) ? $wgAddGroups[$group] : array();
117 $removegroups = isset( $wgRemoveGroups[$group] ) ? $wgRemoveGroups[$group] : array();
118 $addgroupsSelf = isset( $wgGroupsAddToSelf[$group] ) ? $wgGroupsAddToSelf[$group] : array();
119 $removegroupsSelf = isset( $wgGroupsRemoveFromSelf[$group] ) ? $wgGroupsRemoveFromSelf[$group] : array();
120
121 $id = $group == '*' ? false : Sanitizer::escapeId( $group );
122 $out->addHTML( Html::rawElement( 'tr', array( 'id' => $id ),
123 "
124 <td>$grouppage$grouplink</td>
125 <td>" .
126 $this->formatPermissions( $permissions, $revoke, $addgroups, $removegroups,
127 $addgroupsSelf, $removegroupsSelf ) .
128 '</td>
129 '
130 ) );
131 }
132 $out->addHTML(
133 Xml::closeElement( 'table' ) . "\n<br /><hr />\n"
134 );
135 $out->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 array $permissions of permission => bool (from $wgGroupPermissions items)
142 * @param array $revoke of permission => bool (from $wgRevokePermissions items)
143 * @param array $add of groups this group is allowed to add or true
144 * @param array $remove of groups this group is allowed to remove or true
145 * @param array $addSelf of groups this group is allowed to add to self or true
146 * @param array $removeSelf 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 function formatPermissions( $permissions, $revoke, $add, $remove, $addSelf, $removeSelf ) {
150 $r = array();
151 foreach ( $permissions as $permission => $granted ) {
152 //show as granted only if it isn't revoked to prevent duplicate display of permissions
153 if ( $granted && ( !isset( $revoke[$permission] ) || !$revoke[$permission] ) ) {
154 $description = $this->msg( 'listgrouprights-right-display',
155 User::getRightDescription( $permission ),
156 '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
157 )->parse();
158 $r[] = $description;
159 }
160 }
161 foreach ( $revoke as $permission => $revoked ) {
162 if ( $revoked ) {
163 $description = $this->msg( 'listgrouprights-right-revoked',
164 User::getRightDescription( $permission ),
165 '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
166 )->parse();
167 $r[] = $description;
168 }
169 }
170
171 sort( $r );
172
173 $lang = $this->getLanguage();
174
175 if ( $add === true ) {
176 $r[] = $this->msg( 'listgrouprights-addgroup-all' )->escaped();
177 } elseif ( is_array( $add ) && count( $add ) ) {
178 $add = array_values( array_unique( $add ) );
179 $r[] = $this->msg( 'listgrouprights-addgroup',
180 $lang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $add ) ),
181 count( $add )
182 )->parse();
183 }
184
185 if ( $remove === true ) {
186 $r[] = $this->msg( 'listgrouprights-removegroup-all' )->escaped();
187 } elseif ( is_array( $remove ) && count( $remove ) ) {
188 $remove = array_values( array_unique( $remove ) );
189 $r[] = $this->msg( 'listgrouprights-removegroup',
190 $lang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $remove ) ),
191 count( $remove )
192 )->parse();
193 }
194
195 if ( $addSelf === true ) {
196 $r[] = $this->msg( 'listgrouprights-addgroup-self-all' )->escaped();
197 } elseif ( is_array( $addSelf ) && count( $addSelf ) ) {
198 $addSelf = array_values( array_unique( $addSelf ) );
199 $r[] = $this->msg( 'listgrouprights-addgroup-self',
200 $lang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $addSelf ) ),
201 count( $addSelf )
202 )->parse();
203 }
204
205 if ( $removeSelf === true ) {
206 $r[] = $this->msg( 'listgrouprights-removegroup-self-all' )->parse();
207 } elseif ( is_array( $removeSelf ) && count( $removeSelf ) ) {
208 $removeSelf = array_values( array_unique( $removeSelf ) );
209 $r[] = $this->msg( 'listgrouprights-removegroup-self',
210 $lang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $removeSelf ) ),
211 count( $removeSelf )
212 )->parse();
213 }
214
215 if ( empty( $r ) ) {
216 return '';
217 } else {
218 return '<ul><li>' . implode( "</li>\n<li>", $r ) . '</li></ul>';
219 }
220 }
221
222 protected function getGroupName() {
223 return 'users';
224 }
225 }