5bae28f0803be70030754f5c252153a8823e0084
[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 function __construct() {
33 parent::__construct( 'Listgrouprights' );
34 }
35
36 /**
37 * Show the special page
38 * @param string|null $par
39 */
40 public function execute( $par ) {
41 $this->setHeaders();
42 $this->outputHeader();
43
44 $out = $this->getOutput();
45 $out->addModuleStyles( 'mediawiki.special' );
46
47 $out->wrapWikiMsg( "<div class=\"mw-listgrouprights-key\">\n$1\n</div>", 'listgrouprights-key' );
48
49 $out->addHTML(
50 Xml::openElement( 'table', array( 'class' => 'wikitable mw-listgrouprights-table' ) ) .
51 '<tr>' .
52 Xml::element( 'th', null, $this->msg( 'listgrouprights-group' )->text() ) .
53 Xml::element( 'th', null, $this->msg( 'listgrouprights-rights' )->text() ) .
54 '</tr>'
55 );
56
57 $config = $this->getConfig();
58 $groupPermissions = $config->get( 'GroupPermissions' );
59 $revokePermissions = $config->get( 'RevokePermissions' );
60 $addGroups = $config->get( 'AddGroups' );
61 $removeGroups = $config->get( 'RemoveGroups' );
62 $groupsAddToSelf = $config->get( 'GroupsAddToSelf' );
63 $groupsRemoveFromSelf = $config->get( 'GroupsRemoveFromSelf' );
64 $allGroups = array_unique( array_merge(
65 array_keys( $groupPermissions ),
66 array_keys( $revokePermissions ),
67 array_keys( $addGroups ),
68 array_keys( $removeGroups ),
69 array_keys( $groupsAddToSelf ),
70 array_keys( $groupsRemoveFromSelf )
71 ) );
72 asort( $allGroups );
73
74 foreach ( $allGroups as $group ) {
75 $permissions = isset( $groupPermissions[$group] )
76 ? $groupPermissions[$group]
77 : array();
78 $groupname = ( $group == '*' ) // Replace * with a more descriptive groupname
79 ? 'all'
80 : $group;
81
82 $msg = $this->msg( 'group-' . $groupname );
83 $groupnameLocalized = !$msg->isBlank() ? $msg->text() : $groupname;
84
85 $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
86 $grouppageLocalized = !$msg->isBlank() ?
87 $msg->text() :
88 MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
89
90 if ( $group == '*' ) {
91 // Do not make a link for the generic * group
92 $grouppage = htmlspecialchars( $groupnameLocalized );
93 } else {
94 $grouppage = Linker::link(
95 Title::newFromText( $grouppageLocalized ),
96 htmlspecialchars( $groupnameLocalized )
97 );
98 }
99
100 if ( $group === 'user' ) {
101 // Link to Special:listusers for implicit group 'user'
102 $grouplink = '<br />' . Linker::linkKnown(
103 SpecialPage::getTitleFor( 'Listusers' ),
104 $this->msg( 'listgrouprights-members' )->escaped()
105 );
106 } elseif ( !in_array( $group, $config->get( 'ImplicitGroups' ) ) ) {
107 $grouplink = '<br />' . Linker::linkKnown(
108 SpecialPage::getTitleFor( 'Listusers' ),
109 $this->msg( 'listgrouprights-members' )->escaped(),
110 array(),
111 array( 'group' => $group )
112 );
113 } else {
114 // No link to Special:listusers for other implicit groups as they are unlistable
115 $grouplink = '';
116 }
117
118 $revoke = isset( $revokePermissions[$group] ) ? $revokePermissions[$group] : array();
119 $addgroups = isset( $addGroups[$group] ) ? $addGroups[$group] : array();
120 $removegroups = isset( $removeGroups[$group] ) ? $removeGroups[$group] : array();
121 $addgroupsSelf = isset( $groupsAddToSelf[$group] ) ? $groupsAddToSelf[$group] : array();
122 $removegroupsSelf = isset( $groupsRemoveFromSelf[$group] )
123 ? $groupsRemoveFromSelf[$group]
124 : array();
125
126 $id = $group == '*' ? false : Sanitizer::escapeId( $group );
127 $out->addHTML( Html::rawElement( 'tr', array( 'id' => $id ), "
128 <td>$grouppage$grouplink</td>
129 <td>" .
130 $this->formatPermissions( $permissions, $revoke, $addgroups, $removegroups,
131 $addgroupsSelf, $removegroupsSelf ) .
132 '</td>
133 '
134 ) );
135 }
136 $out->addHTML( Xml::closeElement( 'table' ) );
137 $this->outputNamespaceProtectionInfo();
138 }
139
140 private function outputNamespaceProtectionInfo() {
141 global $wgParser, $wgContLang;
142 $out = $this->getOutput();
143 $namespaceProtection = $this->getConfig()->get( 'NamespaceProtection' );
144
145 if ( count( $namespaceProtection ) == 0 ) {
146 return;
147 }
148
149 $header = $this->msg( 'listgrouprights-namespaceprotection-header' )->parse();
150 $out->addHTML(
151 Html::rawElement( 'h2', array(), Html::element( 'span', array(
152 'class' => 'mw-headline',
153 'id' => $wgParser->guessSectionNameFromWikiText( $header )
154 ), $header ) ) .
155 Xml::openElement( 'table', array( 'class' => 'wikitable' ) ) .
156 Html::element(
157 'th',
158 array(),
159 $this->msg( 'listgrouprights-namespaceprotection-namespace' )->text()
160 ) .
161 Html::element(
162 'th',
163 array(),
164 $this->msg( 'listgrouprights-namespaceprotection-restrictedto' )->text()
165 )
166 );
167
168 ksort( $namespaceProtection );
169 foreach ( $namespaceProtection as $namespace => $rights ) {
170 if ( !in_array( $namespace, MWNamespace::getValidNamespaces() ) ) {
171 continue;
172 }
173
174 if ( $namespace == NS_MAIN ) {
175 $namespaceText = $this->msg( 'blanknamespace' )->text();
176 } else {
177 $namespaceText = $wgContLang->convertNamespace( $namespace );
178 }
179
180 $out->addHTML(
181 Xml::openElement( 'tr' ) .
182 Html::rawElement(
183 'td',
184 array(),
185 Linker::link(
186 SpecialPage::getTitleFor( 'Allpages' ),
187 $namespaceText,
188 array(),
189 array( 'namespace' => $namespace )
190 )
191 ) .
192 Xml::openElement( 'td' ) . Xml::openElement( 'ul' )
193 );
194
195 if ( !is_array( $rights ) ) {
196 $rights = array( $rights );
197 }
198
199 foreach ( $rights as $right ) {
200 $out->addHTML(
201 Html::rawElement( 'li', array(), $this->msg(
202 'listgrouprights-right-display',
203 User::getRightDescription( $right ),
204 Html::element(
205 'span',
206 array( 'class' => 'mw-listgrouprights-right-name' ),
207 $right
208 )
209 )->parse() )
210 );
211 }
212
213 $out->addHTML(
214 Xml::closeElement( 'ul' ) .
215 Xml::closeElement( 'td' ) .
216 Xml::closeElement( 'tr' )
217 );
218 }
219 $out->addHTML( Xml::closeElement( 'table' ) );
220 }
221
222 /**
223 * Create a user-readable list of permissions from the given array.
224 *
225 * @param array $permissions Array of permission => bool (from $wgGroupPermissions items)
226 * @param array $revoke Array of permission => bool (from $wgRevokePermissions items)
227 * @param array $add Array of groups this group is allowed to add or true
228 * @param array $remove Array of groups this group is allowed to remove or true
229 * @param array $addSelf Array of groups this group is allowed to add to self or true
230 * @param array $removeSelf Array of group this group is allowed to remove from self or true
231 * @return string List of all granted permissions, separated by comma separator
232 */
233 private function formatPermissions( $permissions, $revoke, $add, $remove, $addSelf, $removeSelf ) {
234 $r = array();
235 foreach ( $permissions as $permission => $granted ) {
236 //show as granted only if it isn't revoked to prevent duplicate display of permissions
237 if ( $granted && ( !isset( $revoke[$permission] ) || !$revoke[$permission] ) ) {
238 $description = $this->msg( 'listgrouprights-right-display',
239 User::getRightDescription( $permission ),
240 '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
241 )->parse();
242 $r[] = $description;
243 }
244 }
245 foreach ( $revoke as $permission => $revoked ) {
246 if ( $revoked ) {
247 $description = $this->msg( 'listgrouprights-right-revoked',
248 User::getRightDescription( $permission ),
249 '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
250 )->parse();
251 $r[] = $description;
252 }
253 }
254
255 sort( $r );
256
257 $lang = $this->getLanguage();
258 $allGroups = User::getAllGroups();
259
260 if ( $add === true ) {
261 $r[] = $this->msg( 'listgrouprights-addgroup-all' )->escaped();
262 } elseif ( is_array( $add ) ) {
263 $add = array_intersect( array_values( array_unique( $add ) ), $allGroups );
264 if ( count( $add ) ) {
265 $r[] = $this->msg( 'listgrouprights-addgroup',
266 $lang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $add ) ),
267 count( $add )
268 )->parse();
269 }
270 }
271
272 if ( $remove === true ) {
273 $r[] = $this->msg( 'listgrouprights-removegroup-all' )->escaped();
274 } elseif ( is_array( $remove ) ) {
275 $remove = array_intersect( array_values( array_unique( $remove ) ), $allGroups );
276 if ( count( $remove ) ) {
277 $r[] = $this->msg( 'listgrouprights-removegroup',
278 $lang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $remove ) ),
279 count( $remove )
280 )->parse();
281 }
282 }
283
284 if ( $addSelf === true ) {
285 $r[] = $this->msg( 'listgrouprights-addgroup-self-all' )->escaped();
286 } elseif ( is_array( $addSelf ) ) {
287 $addSelf = array_intersect( array_values( array_unique( $addSelf ) ), $allGroups );
288 if ( count( $addSelf ) ) {
289 $r[] = $this->msg( 'listgrouprights-addgroup-self',
290 $lang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $addSelf ) ),
291 count( $addSelf )
292 )->parse();
293 }
294 }
295
296 if ( $removeSelf === true ) {
297 $r[] = $this->msg( 'listgrouprights-removegroup-self-all' )->parse();
298 } elseif ( is_array( $removeSelf ) ) {
299 $removeSelf = array_intersect( array_values( array_unique( $removeSelf ) ), $allGroups );
300 if ( count( $removeSelf ) ) {
301 $r[] = $this->msg( 'listgrouprights-removegroup-self',
302 $lang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $removeSelf ) ),
303 count( $removeSelf )
304 )->parse();
305 }
306 }
307
308 if ( empty( $r ) ) {
309 return '';
310 } else {
311 return '<ul><li>' . implode( "</li>\n<li>", $r ) . '</li></ul>';
312 }
313 }
314
315 protected function getGroupName() {
316 return 'users';
317 }
318 }