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
24 use MediaWiki\MediaWikiServices
;
27 * This special page lists all defined user groups and the associated rights.
28 * See also @ref $wgGroupPermissions.
30 * @ingroup SpecialPage
31 * @author Petr Kadlec <mormegil@centrum.cz>
33 class SpecialListGroupRights
extends SpecialPage
{
34 public function __construct() {
35 parent
::__construct( 'Listgrouprights' );
39 * Show the special page
40 * @param string|null $par
42 public function execute( $par ) {
44 $this->outputHeader();
46 $out = $this->getOutput();
47 $out->addModuleStyles( 'mediawiki.special' );
49 $out->wrapWikiMsg( "<div class=\"mw-listgrouprights-key\">\n$1\n</div>", 'listgrouprights-key' );
52 Xml
::openElement( 'table', [ 'class' => 'wikitable mw-listgrouprights-table' ] ) .
54 Xml
::element( 'th', null, $this->msg( 'listgrouprights-group' )->text() ) .
55 Xml
::element( 'th', null, $this->msg( 'listgrouprights-rights' )->text() ) .
59 $config = $this->getConfig();
60 $groupPermissions = $config->get( 'GroupPermissions' );
61 $revokePermissions = $config->get( 'RevokePermissions' );
62 $addGroups = $config->get( 'AddGroups' );
63 $removeGroups = $config->get( 'RemoveGroups' );
64 $groupsAddToSelf = $config->get( 'GroupsAddToSelf' );
65 $groupsRemoveFromSelf = $config->get( 'GroupsRemoveFromSelf' );
66 $allGroups = array_unique( array_merge(
67 array_keys( $groupPermissions ),
68 array_keys( $revokePermissions ),
69 array_keys( $addGroups ),
70 array_keys( $removeGroups ),
71 array_keys( $groupsAddToSelf ),
72 array_keys( $groupsRemoveFromSelf )
76 $linkRenderer = $this->getLinkRenderer();
78 foreach ( $allGroups as $group ) {
79 $permissions = $groupPermissions[$group] ??
[];
80 $groupname = ( $group == '*' ) // Replace * with a more descriptive groupname
84 $groupnameLocalized = UserGroupMembership
::getGroupName( $groupname );
86 $grouppageLocalizedTitle = UserGroupMembership
::getGroupPage( $groupname )
87 ?
: Title
::newFromText( MediaWikiServices
::getInstance()->getNamespaceInfo()->
88 getCanonicalName( NS_PROJECT
) . ':' . $groupname );
90 if ( $group == '*' ||
!$grouppageLocalizedTitle ) {
91 // Do not make a link for the generic * group or group with invalid group page
92 $grouppage = htmlspecialchars( $groupnameLocalized );
94 $grouppage = $linkRenderer->makeLink(
95 $grouppageLocalizedTitle,
100 if ( $group === 'user' ) {
101 // Link to Special:listusers for implicit group 'user'
102 $grouplink = '<br />' . $linkRenderer->makeKnownLink(
103 SpecialPage
::getTitleFor( 'Listusers' ),
104 $this->msg( 'listgrouprights-members' )->text()
106 } elseif ( !in_array( $group, $config->get( 'ImplicitGroups' ) ) ) {
107 $grouplink = '<br />' . $linkRenderer->makeKnownLink(
108 SpecialPage
::getTitleFor( 'Listusers' ),
109 $this->msg( 'listgrouprights-members' )->text(),
111 [ 'group' => $group ]
114 // No link to Special:listusers for other implicit groups as they are unlistable
118 $revoke = $revokePermissions[$group] ??
[];
119 $addgroups = $addGroups[$group] ??
[];
120 $removegroups = $removeGroups[$group] ??
[];
121 $addgroupsSelf = $groupsAddToSelf[$group] ??
[];
122 $removegroupsSelf = $groupsRemoveFromSelf[$group] ??
[];
124 $id = $group == '*' ?
false : Sanitizer
::escapeIdForAttribute( $group );
125 $out->addHTML( Html
::rawElement( 'tr', [ 'id' => $id ], "
126 <td>$grouppage$grouplink</td>
128 $this->formatPermissions( $permissions, $revoke, $addgroups, $removegroups,
129 $addgroupsSelf, $removegroupsSelf ) .
134 $out->addHTML( Xml
::closeElement( 'table' ) );
135 $this->outputNamespaceProtectionInfo();
138 private function outputNamespaceProtectionInfo() {
139 $out = $this->getOutput();
140 $namespaceProtection = $this->getConfig()->get( 'NamespaceProtection' );
142 if ( count( $namespaceProtection ) == 0 ) {
146 $header = $this->msg( 'listgrouprights-namespaceprotection-header' )->text();
148 Html
::rawElement( 'h2', [], Html
::element( 'span', [
149 'class' => 'mw-headline',
150 'id' => substr( Parser
::guessSectionNameFromStrippedText( $header ), 1 )
152 Xml
::openElement( 'table', [ 'class' => 'wikitable' ] ) .
156 $this->msg( 'listgrouprights-namespaceprotection-namespace' )->text()
161 $this->msg( 'listgrouprights-namespaceprotection-restrictedto' )->text()
164 $linkRenderer = $this->getLinkRenderer();
165 ksort( $namespaceProtection );
167 MediaWikiServices
::getInstance()->getNamespaceInfo()->getValidNamespaces();
168 $contLang = MediaWikiServices
::getInstance()->getContentLanguage();
169 foreach ( $namespaceProtection as $namespace => $rights ) {
170 if ( !in_array( $namespace, $validNamespaces ) ) {
174 if ( $namespace == NS_MAIN
) {
175 $namespaceText = $this->msg( 'blanknamespace' )->text();
177 $namespaceText = $contLang->convertNamespace( $namespace );
181 Xml
::openElement( 'tr' ) .
185 $linkRenderer->makeLink(
186 SpecialPage
::getTitleFor( 'Allpages' ),
189 [ 'namespace' => $namespace ]
192 Xml
::openElement( 'td' ) . Xml
::openElement( 'ul' )
195 if ( !is_array( $rights ) ) {
196 $rights = [ $rights ];
199 foreach ( $rights as $right ) {
201 Html
::rawElement( 'li', [], $this->msg(
202 'listgrouprights-right-display',
203 User
::getRightDescription( $right ),
206 [ '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 HTML list of all granted permissions
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();
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 ) ) {
274 foreach ( $changeGroup as $group ) {
275 $groupLinks[] = UserGroupMembership
::getLink( $group, $this->getContext(), 'wiki' );
277 // For grep: listgrouprights-addgroup, listgrouprights-removegroup,
278 // listgrouprights-addgroup-self, listgrouprights-removegroup-self
279 $r[] = $this->msg( 'listgrouprights-' . $messageKey,
280 $lang->listToText( $groupLinks ), count( $changeGroup ) )->parse();
288 return '<ul><li>' . implode( "</li>\n<li>", $r ) . '</li></ul>';
292 protected function getGroupName() {