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 ) {
42 $this->outputHeader();
44 $out = $this->getOutput();
45 $out->addModuleStyles( 'mediawiki.special' );
47 $out->wrapWikiMsg( "<div class=\"mw-listgrouprights-key\">\n$1\n</div>", 'listgrouprights-key' );
50 Xml
::openElement( 'table', array( 'class' => 'wikitable mw-listgrouprights-table' ) ) .
52 Xml
::element( 'th', null, $this->msg( 'listgrouprights-group' )->text() ) .
53 Xml
::element( 'th', null, $this->msg( 'listgrouprights-rights' )->text() ) .
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 )
74 foreach ( $allGroups as $group ) {
75 $permissions = isset( $groupPermissions[$group] )
76 ?
$groupPermissions[$group]
78 $groupname = ( $group == '*' ) // Replace * with a more descriptive groupname
82 $msg = $this->msg( 'group-' . $groupname );
83 $groupnameLocalized = !$msg->isBlank() ?
$msg->text() : $groupname;
85 $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
86 $grouppageLocalized = !$msg->isBlank() ?
88 MWNamespace
::getCanonicalName( NS_PROJECT
) . ':' . $groupname;
90 if ( $group == '*' ) {
91 // Do not make a link for the generic * group
92 $grouppage = htmlspecialchars( $groupnameLocalized );
94 $grouppage = Linker
::link(
95 Title
::newFromText( $grouppageLocalized ),
96 htmlspecialchars( $groupnameLocalized )
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()
106 } elseif ( !in_array( $group, $config->get( 'ImplicitGroups' ) ) ) {
107 $grouplink = '<br />' . Linker
::linkKnown(
108 SpecialPage
::getTitleFor( 'Listusers' ),
109 $this->msg( 'listgrouprights-members' )->escaped(),
111 array( 'group' => $group )
114 // No link to Special:listusers for other implicit groups as they are unlistable
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]
126 $id = $group == '*' ?
false : Sanitizer
::escapeId( $group );
127 $out->addHTML( Html
::rawElement( 'tr', array( 'id' => $id ), "
128 <td>$grouppage$grouplink</td>
130 $this->formatPermissions( $permissions, $revoke, $addgroups, $removegroups,
131 $addgroupsSelf, $removegroupsSelf ) .
136 $out->addHTML( Xml
::closeElement( 'table' ) );
137 $this->outputNamespaceProtectionInfo();
140 private function outputNamespaceProtectionInfo() {
141 global $wgParser, $wgContLang;
142 $out = $this->getOutput();
143 $namespaceProtection = $this->getConfig()->get( 'NamespaceProtection' );
145 if ( count( $namespaceProtection ) == 0 ) {
149 $header = $this->msg( 'listgrouprights-namespaceprotection-header' )->parse();
151 Html
::rawElement( 'h2', array(), Html
::element( 'span', array(
152 'class' => 'mw-headline',
153 'id' => $wgParser->guessSectionNameFromWikiText( $header )
155 Xml
::openElement( 'table', array( 'class' => 'wikitable' ) ) .
159 $this->msg( 'listgrouprights-namespaceprotection-namespace' )->text()
164 $this->msg( 'listgrouprights-namespaceprotection-restrictedto' )->text()
168 ksort( $namespaceProtection );
169 foreach ( $namespaceProtection as $namespace => $rights ) {
170 if ( !in_array( $namespace, MWNamespace
::getValidNamespaces() ) ) {
174 if ( $namespace == NS_MAIN
) {
175 $namespaceText = $this->msg( 'blanknamespace' )->text();
177 $namespaceText = $wgContLang->convertNamespace( $namespace );
181 Xml
::openElement( 'tr' ) .
186 SpecialPage
::getTitleFor( 'Allpages' ),
189 array( 'namespace' => $namespace )
192 Xml
::openElement( 'td' ) . Xml
::openElement( 'ul' )
195 if ( !is_array( $rights ) ) {
196 $rights = array( $rights );
199 foreach ( $rights as $right ) {
201 Html
::rawElement( 'li', array(), $this->msg(
202 'listgrouprights-right-display',
203 User
::getRightDescription( $right ),
206 array( 'class' => 'mw-listgrouprights-right-name' ),
214 Xml
::closeElement( 'ul' ) .
215 Xml
::closeElement( 'td' ) .
216 Xml
::closeElement( 'tr' )
219 $out->addHTML( Xml
::closeElement( 'table' ) );
223 * Create a user-readable list of permissions from the given array.
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
233 private function formatPermissions( $permissions, $revoke, $add, $remove, $addSelf, $removeSelf ) {
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 $r[] = $this->msg( 'listgrouprights-right-display',
239 User
::getRightDescription( $permission ),
240 '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
244 foreach ( $revoke as $permission => $revoked ) {
246 $r[] = $this->msg( 'listgrouprights-right-revoked',
247 User
::getRightDescription( $permission ),
248 '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
255 $lang = $this->getLanguage();
256 $allGroups = User
::getAllGroups();
258 $changeGroups = array(
260 'removegroup' => $remove,
261 'addgroup-self' => $addSelf,
262 'removegroup-self' => $removeSelf
265 foreach ( $changeGroups as $messageKey => $changeGroup ) {
266 if ( $changeGroup === true ) {
267 // For grep: listgrouprights-addgroup-all, listgrouprights-removegroup-all,
268 // listgrouprights-addgroup-self-all, listgrouprights-removegroup-self-all
269 $r[] = $this->msg( 'listgrouprights-' . $messageKey . '-all' )->escaped();
270 } elseif ( is_array( $changeGroup ) ) {
271 $changeGroup = array_intersect( array_values( array_unique( $changeGroup ) ), $allGroups );
272 if ( count( $changeGroup ) ) {
273 // For grep: listgrouprights-addgroup, listgrouprights-removegroup,
274 // listgrouprights-addgroup-self, listgrouprights-removegroup-self
275 $r[] = $this->msg( 'listgrouprights-' . $messageKey,
276 $lang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $changeGroup ) ),
277 count( $changeGroup )
286 return '<ul><li>' . implode( "</li>\n<li>", $r ) . '</li></ul>';
290 protected function getGroupName() {