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