3 * Implements Special:Listgrouprights
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.
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.
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
21 * @ingroup SpecialPage
25 * This special page lists all defined user groups and the associated rights.
26 * See also @ref $wgGroupPermissions.
28 * @ingroup SpecialPage
29 * @author Petr Kadlec <mormegil@centrum.cz>
31 class SpecialListGroupRights
extends SpecialPage
{
32 function __construct() {
33 parent
::__construct( 'Listgrouprights' );
37 * Show the special page
38 * @param string|null $par
40 public function execute( $par ) {
41 global $wgImplicitGroups;
42 global $wgGroupPermissions, $wgRevokePermissions, $wgAddGroups, $wgRemoveGroups;
43 global $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
46 $this->outputHeader();
48 $out = $this->getOutput();
49 $out->addModuleStyles( 'mediawiki.special' );
51 $out->wrapWikiMsg( "<div class=\"mw-listgrouprights-key\">\n$1\n</div>", 'listgrouprights-key' );
54 Xml
::openElement( 'table', array( 'class' => 'wikitable mw-listgrouprights-table' ) ) .
56 Xml
::element( 'th', null, $this->msg( 'listgrouprights-group' )->text() ) .
57 Xml
::element( 'th', null, $this->msg( 'listgrouprights-rights' )->text() ) .
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 )
71 foreach ( $allGroups as $group ) {
72 $permissions = isset( $wgGroupPermissions[$group] )
73 ?
$wgGroupPermissions[$group]
75 $groupname = ( $group == '*' ) // Replace * with a more descriptive groupname
79 $msg = $this->msg( 'group-' . $groupname );
80 $groupnameLocalized = !$msg->isBlank() ?
$msg->text() : $groupname;
82 $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
83 $grouppageLocalized = !$msg->isBlank() ?
85 MWNamespace
::getCanonicalName( NS_PROJECT
) . ':' . $groupname;
87 if ( $group == '*' ) {
88 // Do not make a link for the generic * group
89 $grouppage = htmlspecialchars( $groupnameLocalized );
91 $grouppage = Linker
::link(
92 Title
::newFromText( $grouppageLocalized ),
93 htmlspecialchars( $groupnameLocalized )
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()
103 } elseif ( !in_array( $group, $wgImplicitGroups ) ) {
104 $grouplink = '<br />' . Linker
::linkKnown(
105 SpecialPage
::getTitleFor( 'Listusers' ),
106 $this->msg( 'listgrouprights-members' )->escaped(),
108 array( 'group' => $group )
111 // No link to Special:listusers for other implicit groups as they are unlistable
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] )
120 ?
$wgGroupsRemoveFromSelf[$group]
123 $id = $group == '*' ?
false : Sanitizer
::escapeId( $group );
124 $out->addHTML( Html
::rawElement( 'tr', array( 'id' => $id ), "
125 <td>$grouppage$grouplink</td>
127 $this->formatPermissions( $permissions, $revoke, $addgroups, $removegroups,
128 $addgroupsSelf, $removegroupsSelf ) .
133 $out->addHTML( Xml
::closeElement( 'table' ) );
134 $this->outputNamespaceProtectionInfo();
137 private function outputNamespaceProtectionInfo() {
138 global $wgNamespaceProtection, $wgParser, $wgContLang;
139 $out = $this->getOutput();
141 if ( count( $wgNamespaceProtection ) == 0 ) {
145 $header = $this->msg( 'listgrouprights-namespaceprotection-header' )->parse();
147 Html
::rawElement( 'h2', array(), Html
::element( 'span', array(
148 'class' => 'mw-headline',
149 'id' => $wgParser->guessSectionNameFromWikiText( $header )
151 Xml
::openElement( 'table', array( 'class' => 'wikitable' ) ) .
155 $this->msg( 'listgrouprights-namespaceprotection-namespace' )->text()
160 $this->msg( 'listgrouprights-namespaceprotection-restrictedto' )->text()
164 ksort( $wgNamespaceProtection );
165 foreach ( $wgNamespaceProtection as $namespace => $rights ) {
166 if ( !in_array( $namespace, MWNamespace
::getValidNamespaces() ) ) {
170 if ( $namespace == NS_MAIN
) {
171 $namespaceText = $this->msg( 'blanknamespace' )->text();
173 $namespaceText = $wgContLang->convertNamespace( $namespace );
177 Xml
::openElement( 'tr' ) .
182 SpecialPage
::getTitleFor( 'Allpages' ),
185 array( 'namespace' => $namespace )
188 Xml
::openElement( 'td' ) . Xml
::openElement( 'ul' )
191 if ( !is_array( $rights ) ) {
192 $rights = array( $rights );
195 foreach ( $rights as $right ) {
197 Html
::rawElement( 'li', array(), $this->msg(
198 'listgrouprights-right-display',
199 User
::getRightDescription( $right ),
202 array( 'class' => 'mw-listgrouprights-right-name' ),
210 Xml
::closeElement( 'ul' ) .
211 Xml
::closeElement( 'td' ) .
212 Xml
::closeElement( 'tr' )
215 $out->addHTML( Xml
::closeElement( 'table' ) );
219 * Create a user-readable list of permissions from the given array.
221 * @param array $permissions Array of permission => bool (from $wgGroupPermissions items)
222 * @param array $revoke Array of permission => bool (from $wgRevokePermissions items)
223 * @param array $add Array of groups this group is allowed to add or true
224 * @param array $remove Array of groups this group is allowed to remove or true
225 * @param array $addSelf Array of groups this group is allowed to add to self or true
226 * @param array $removeSelf Array of group this group is allowed to remove from self or true
227 * @return string List of all granted permissions, separated by comma separator
229 private function formatPermissions( $permissions, $revoke, $add, $remove, $addSelf, $removeSelf ) {
231 foreach ( $permissions as $permission => $granted ) {
232 //show as granted only if it isn't revoked to prevent duplicate display of permissions
233 if ( $granted && ( !isset( $revoke[$permission] ) ||
!$revoke[$permission] ) ) {
234 $description = $this->msg( 'listgrouprights-right-display',
235 User
::getRightDescription( $permission ),
236 '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
241 foreach ( $revoke as $permission => $revoked ) {
243 $description = $this->msg( 'listgrouprights-right-revoked',
244 User
::getRightDescription( $permission ),
245 '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
253 $lang = $this->getLanguage();
255 if ( $add === true ) {
256 $r[] = $this->msg( 'listgrouprights-addgroup-all' )->escaped();
257 } elseif ( is_array( $add ) && count( $add ) ) {
258 $add = array_values( array_unique( $add ) );
259 $r[] = $this->msg( 'listgrouprights-addgroup',
260 $lang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $add ) ),
265 if ( $remove === true ) {
266 $r[] = $this->msg( 'listgrouprights-removegroup-all' )->escaped();
267 } elseif ( is_array( $remove ) && count( $remove ) ) {
268 $remove = array_values( array_unique( $remove ) );
269 $r[] = $this->msg( 'listgrouprights-removegroup',
270 $lang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $remove ) ),
275 if ( $addSelf === true ) {
276 $r[] = $this->msg( 'listgrouprights-addgroup-self-all' )->escaped();
277 } elseif ( is_array( $addSelf ) && count( $addSelf ) ) {
278 $addSelf = array_values( array_unique( $addSelf ) );
279 $r[] = $this->msg( 'listgrouprights-addgroup-self',
280 $lang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $addSelf ) ),
285 if ( $removeSelf === true ) {
286 $r[] = $this->msg( 'listgrouprights-removegroup-self-all' )->parse();
287 } elseif ( is_array( $removeSelf ) && count( $removeSelf ) ) {
288 $removeSelf = array_values( array_unique( $removeSelf ) );
289 $r[] = $this->msg( 'listgrouprights-removegroup-self',
290 $lang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $removeSelf ) ),
298 return '<ul><li>' . implode( "</li>\n<li>", $r ) . '</li></ul>';
302 protected function getGroupName() {