MediaWiki master
SpecialListGroupRights.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
35
45
46 private NamespaceInfo $nsInfo;
47 private UserGroupManager $userGroupManager;
48 private ILanguageConverter $languageConverter;
49 private GroupPermissionsLookup $groupPermissionsLookup;
50
57 public function __construct(
58 NamespaceInfo $nsInfo,
59 UserGroupManager $userGroupManager,
60 LanguageConverterFactory $languageConverterFactory,
61 GroupPermissionsLookup $groupPermissionsLookup
62 ) {
63 parent::__construct( 'Listgrouprights' );
64 $this->nsInfo = $nsInfo;
65 $this->userGroupManager = $userGroupManager;
66 $this->languageConverter = $languageConverterFactory->getLanguageConverter( $this->getContentLanguage() );
67 $this->groupPermissionsLookup = $groupPermissionsLookup;
68 }
69
74 public function execute( $par ) {
75 $this->setHeaders();
76 $this->outputHeader();
77
78 $out = $this->getOutput();
79 $out->addModuleStyles( 'mediawiki.special' );
80 $this->addHelpLink( 'Help:User_rights_and_groups' );
81
82 $out->wrapWikiMsg( "<div class=\"mw-listgrouprights-key\">\n$1\n</div>", 'listgrouprights-key' );
83
84 $out->addHTML(
85 Html::openElement( 'table', [ 'class' => 'wikitable mw-listgrouprights-table' ] ) .
86 '<tr>' .
87 Html::element( 'th', [], $this->msg( 'listgrouprights-group' )->text() ) .
88 Html::element( 'th', [], $this->msg( 'listgrouprights-rights' )->text() ) .
89 '</tr>'
90 );
91
92 $config = $this->getConfig();
93 $addGroups = $config->get( MainConfigNames::AddGroups );
94 $removeGroups = $config->get( MainConfigNames::RemoveGroups );
95 $groupsAddToSelf = $config->get( MainConfigNames::GroupsAddToSelf );
96 $groupsRemoveFromSelf = $config->get( MainConfigNames::GroupsRemoveFromSelf );
97 $allGroups = array_merge(
98 $this->userGroupManager->listAllGroups(),
99 $this->userGroupManager->listAllImplicitGroups()
100 );
101 asort( $allGroups );
102
103 $linkRenderer = $this->getLinkRenderer();
104 $lang = $this->getLanguage();
105
106 foreach ( $allGroups as $group ) {
107 $permissions = $this->groupPermissionsLookup->getGrantedPermissions( $group );
108 $groupname = ( $group == '*' ) // Replace * with a more descriptive groupname
109 ? 'all'
110 : $group;
111
112 $groupnameLocalized = $lang->getGroupName( $groupname );
113
114 $grouppageLocalizedTitle = UserGroupMembership::getGroupPage( $groupname )
115 ?: Title::makeTitleSafe( NS_PROJECT, $groupname );
116
117 if ( $group == '*' || !$grouppageLocalizedTitle ) {
118 // Do not make a link for the generic * group or group with invalid group page
119 $grouppage = htmlspecialchars( $groupnameLocalized );
120 } else {
121 $grouppage = $linkRenderer->makeLink(
122 $grouppageLocalizedTitle,
123 $groupnameLocalized
124 );
125 }
126
127 $groupWithParentheses = $this->msg( 'parentheses' )->rawParams( $group )->escaped();
128 $groupname = "<br /><code>$groupWithParentheses</code>";
129
130 if ( $group === 'user' ) {
131 // Link to Special:listusers for implicit group 'user'
132 $grouplink = '<br />' . $linkRenderer->makeKnownLink(
133 SpecialPage::getTitleFor( 'Listusers' ),
134 $this->msg( 'listgrouprights-members' )->text()
135 );
136 } elseif ( !in_array( $group, $config->get( MainConfigNames::ImplicitGroups ) ) ) {
137 $grouplink = '<br />' . $linkRenderer->makeKnownLink(
138 SpecialPage::getTitleFor( 'Listusers' ),
139 $this->msg( 'listgrouprights-members' )->text(),
140 [],
141 [ 'group' => $group ]
142 );
143 } else {
144 // No link to Special:listusers for other implicit groups as they are unlistable
145 $grouplink = '';
146 }
147
148 $revoke = $this->groupPermissionsLookup->getRevokedPermissions( $group );
149 $addgroups = $addGroups[$group] ?? [];
150 $removegroups = $removeGroups[$group] ?? [];
151 $addgroupsSelf = $groupsAddToSelf[$group] ?? [];
152 $removegroupsSelf = $groupsRemoveFromSelf[$group] ?? [];
153
154 $id = $group == '*' ? false : Sanitizer::escapeIdForAttribute( $group );
155 $out->addHTML( Html::rawElement( 'tr', [ 'id' => $id ], "
156 <td>$grouppage$groupname$grouplink</td>
157 <td>" .
158 $this->formatPermissions( $permissions, $revoke, $addgroups, $removegroups,
159 $addgroupsSelf, $removegroupsSelf ) .
160 '</td>
161 '
162 ) );
163 }
164 $out->addHTML( Html::closeElement( 'table' ) );
165 $this->outputNamespaceProtectionInfo();
166 }
167
168 private function outputNamespaceProtectionInfo() {
169 $out = $this->getOutput();
170 $namespaceProtection = $this->getConfig()->get( MainConfigNames::NamespaceProtection );
171
172 if ( count( $namespaceProtection ) == 0 ) {
173 return;
174 }
175
176 $header = $this->msg( 'listgrouprights-namespaceprotection-header' )->text();
177 $out->addHTML(
178 Html::element( 'h2', [
179 'id' => Sanitizer::escapeIdForAttribute( $header )
180 ], $header ) .
181 Html::openElement( 'table', [ 'class' => 'wikitable' ] ) .
183 'th',
184 [],
185 $this->msg( 'listgrouprights-namespaceprotection-namespace' )->text()
186 ) .
188 'th',
189 [],
190 $this->msg( 'listgrouprights-namespaceprotection-restrictedto' )->text()
191 )
192 );
193 $linkRenderer = $this->getLinkRenderer();
194 ksort( $namespaceProtection );
195 $validNamespaces = $this->nsInfo->getValidNamespaces();
196 foreach ( $namespaceProtection as $namespace => $rights ) {
197 if ( !in_array( $namespace, $validNamespaces ) ) {
198 continue;
199 }
200
201 if ( $namespace == NS_MAIN ) {
202 $namespaceText = $this->msg( 'blanknamespace' )->text();
203 } else {
204 $namespaceText = $this->languageConverter->convertNamespace( $namespace );
205 }
206
207 $out->addHTML(
208 Html::openElement( 'tr' ) .
209 Html::rawElement(
210 'td',
211 [],
212 $linkRenderer->makeLink(
213 SpecialPage::getTitleFor( 'Allpages' ),
214 $namespaceText,
215 [],
216 [ 'namespace' => $namespace ]
217 )
218 ) .
219 Html::openElement( 'td' ) . Html::openElement( 'ul' )
220 );
221
222 if ( !is_array( $rights ) ) {
223 $rights = [ $rights ];
224 }
225
226 foreach ( $rights as $right ) {
227 $out->addHTML( Html::rawElement( 'li', [],
228 $this->msg( 'listgrouprights-right-display' )
229 ->params( User::getRightDescription( $right ) )
230 ->rawParams( Html::element(
231 'span',
232 [ 'class' => 'mw-listgrouprights-right-name' ],
233 $right
234 ) )->parse()
235 ) );
236 }
237
238 $out->addHTML(
239 Html::closeElement( 'ul' ) .
240 Html::closeElement( 'td' ) .
241 Html::closeElement( 'tr' )
242 );
243 }
244 $out->addHTML( Html::closeElement( 'table' ) );
245 }
246
258 private function formatPermissions( $permissions, $revoke, $add, $remove, $addSelf, $removeSelf ) {
259 $r = [];
260 foreach ( $permissions as $permission ) {
261 // show as granted only if it isn't revoked to prevent duplicate display of permissions
262 if ( !isset( $revoke[$permission] ) || !$revoke[$permission] ) {
263 $r[] = $this->msg( 'listgrouprights-right-display' )
264 ->params( User::getRightDescription( $permission ) )
265 ->rawParams( Html::element(
266 'span',
267 [ 'class' => 'mw-listgrouprights-right-name' ],
268 $permission
269 ) )->parse();
270 }
271 }
272 foreach ( $revoke as $permission ) {
273 $r[] = $this->msg( 'listgrouprights-right-revoked' )
274 ->params( User::getRightDescription( $permission ) )
275 ->rawParams( Html::element(
276 'span',
277 [ 'class' => 'mw-listgrouprights-right-name' ],
278 $permission
279 ) )->parse();
280 }
281
282 sort( $r );
283
284 $lang = $this->getLanguage();
285 $allGroups = $this->userGroupManager->listAllGroups();
286
287 $changeGroups = [
288 'addgroup' => $add,
289 'removegroup' => $remove,
290 'addgroup-self' => $addSelf,
291 'removegroup-self' => $removeSelf
292 ];
293
294 foreach ( $changeGroups as $messageKey => $changeGroup ) {
295 // @phan-suppress-next-line PhanTypeComparisonFromArray
296 if ( $changeGroup === true ) {
297 // For grep: listgrouprights-addgroup-all, listgrouprights-removegroup-all,
298 // listgrouprights-addgroup-self-all, listgrouprights-removegroup-self-all
299 $r[] = $this->msg( 'listgrouprights-' . $messageKey . '-all' )->escaped();
300 } elseif ( is_array( $changeGroup ) ) {
301 $changeGroup = array_intersect( array_values( array_unique( $changeGroup ) ), $allGroups );
302 if ( count( $changeGroup ) ) {
303 $groupLinks = [];
304 foreach ( $changeGroup as $group ) {
305 $groupLinks[] = UserGroupMembership::getLinkWiki( $group, $this->getContext() );
306 }
307 // For grep: listgrouprights-addgroup, listgrouprights-removegroup,
308 // listgrouprights-addgroup-self, listgrouprights-removegroup-self
309 $r[] = $this->msg( 'listgrouprights-' . $messageKey,
310 $lang->listToText( $groupLinks ), count( $changeGroup ) )->parse();
311 }
312 }
313 }
314
315 if ( !$r ) {
316 return '';
317 } else {
318 return '<ul><li>' . implode( "</li>\n<li>", $r ) . '</li></ul>';
319 }
320 }
321
322 protected function getGroupName() {
323 return 'users';
324 }
325}
326
328class_alias( SpecialListGroupRights::class, 'SpecialListGroupRights' );
const NS_MAIN
Definition Defines.php:65
const NS_PROJECT
Definition Defines.php:69
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
An interface for creating language converters.
getLanguageConverter( $language=null)
Provide a LanguageConverter for given language.
A class containing constants representing the names of configuration variables.
const ImplicitGroups
Name constant for the ImplicitGroups setting, for use with Config::get()
const NamespaceProtection
Name constant for the NamespaceProtection setting, for use with Config::get()
const GroupsRemoveFromSelf
Name constant for the GroupsRemoveFromSelf setting, for use with Config::get()
const AddGroups
Name constant for the AddGroups setting, for use with Config::get()
const GroupsAddToSelf
Name constant for the GroupsAddToSelf setting, for use with Config::get()
const RemoveGroups
Name constant for the RemoveGroups setting, for use with Config::get()
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:46
Parent class for all special pages.
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
getConfig()
Shortcut to get main config object.
getContext()
Gets the context this SpecialPage is executed in.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
getContentLanguage()
Shortcut to get content language.
getLanguage()
Shortcut to get user's language.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages By default the message key is the canonical name of...
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
List all defined user groups and the associated rights.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
__construct(NamespaceInfo $nsInfo, UserGroupManager $userGroupManager, LanguageConverterFactory $languageConverterFactory, GroupPermissionsLookup $groupPermissionsLookup)
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
Represents a title within MediaWiki.
Definition Title.php:79
Represents a "user group membership" – a specific instance of a user belonging to a group.
internal since 1.36
Definition User.php:93
The shared interface for all language converters.
element(SerializerNode $parent, SerializerNode $node, $contents)
$header