MediaWiki REL1_37
SpecialListGroupRights.php
Go to the documentation of this file.
1<?php
26
35
37 private $nsInfo;
38
41
44
50 public function __construct(
53 LanguageConverterFactory $languageConverterFactory
54 ) {
55 parent::__construct( 'Listgrouprights' );
56 $this->nsInfo = $nsInfo;
57 $this->userGroupManager = $userGroupManager;
58 $this->languageConverter = $languageConverterFactory->getLanguageConverter( $this->getContentLanguage() );
59 }
60
65 public function execute( $par ) {
66 $this->setHeaders();
67 $this->outputHeader();
68
69 $out = $this->getOutput();
70 $out->addModuleStyles( 'mediawiki.special' );
71 $this->addHelpLink( 'Help:User_rights_and_groups' );
72
73 $out->wrapWikiMsg( "<div class=\"mw-listgrouprights-key\">\n$1\n</div>", 'listgrouprights-key' );
74
75 $out->addHTML(
76 Xml::openElement( 'table', [ 'class' => 'wikitable mw-listgrouprights-table' ] ) .
77 '<tr>' .
78 Xml::element( 'th', null, $this->msg( 'listgrouprights-group' )->text() ) .
79 Xml::element( 'th', null, $this->msg( 'listgrouprights-rights' )->text() ) .
80 '</tr>'
81 );
82
83 $config = $this->getConfig();
84 $groupPermissions = $config->get( 'GroupPermissions' );
85 $revokePermissions = $config->get( 'RevokePermissions' );
86 $addGroups = $config->get( 'AddGroups' );
87 $removeGroups = $config->get( 'RemoveGroups' );
88 $groupsAddToSelf = $config->get( 'GroupsAddToSelf' );
89 $groupsRemoveFromSelf = $config->get( 'GroupsRemoveFromSelf' );
90 $allGroups = array_unique( array_merge(
91 array_keys( $groupPermissions ),
92 array_keys( $revokePermissions ),
93 array_keys( $addGroups ),
94 array_keys( $removeGroups ),
95 array_keys( $groupsAddToSelf ),
96 array_keys( $groupsRemoveFromSelf )
97 ) );
98 asort( $allGroups );
99
101
102 foreach ( $allGroups as $group ) {
103 $permissions = $groupPermissions[$group] ?? [];
104 $groupname = ( $group == '*' ) // Replace * with a more descriptive groupname
105 ? 'all'
106 : $group;
107
108 $groupnameLocalized = UserGroupMembership::getGroupName( $groupname );
109
110 $grouppageLocalizedTitle = UserGroupMembership::getGroupPage( $groupname )
111 ?: Title::makeTitleSafe( NS_PROJECT, $groupname );
112
113 if ( $group == '*' || !$grouppageLocalizedTitle ) {
114 // Do not make a link for the generic * group or group with invalid group page
115 $grouppage = htmlspecialchars( $groupnameLocalized );
116 } else {
117 $grouppage = $linkRenderer->makeLink(
118 $grouppageLocalizedTitle,
119 $groupnameLocalized
120 );
121 }
122
123 if ( $group === 'user' ) {
124 // Link to Special:listusers for implicit group 'user'
125 $grouplink = '<br />' . $linkRenderer->makeKnownLink(
126 SpecialPage::getTitleFor( 'Listusers' ),
127 $this->msg( 'listgrouprights-members' )->text()
128 );
129 } elseif ( !in_array( $group, $config->get( 'ImplicitGroups' ) ) ) {
130 $grouplink = '<br />' . $linkRenderer->makeKnownLink(
131 SpecialPage::getTitleFor( 'Listusers' ),
132 $this->msg( 'listgrouprights-members' )->text(),
133 [],
134 [ 'group' => $group ]
135 );
136 } else {
137 // No link to Special:listusers for other implicit groups as they are unlistable
138 $grouplink = '';
139 }
140
141 $revoke = $revokePermissions[$group] ?? [];
142 $addgroups = $addGroups[$group] ?? [];
143 $removegroups = $removeGroups[$group] ?? [];
144 $addgroupsSelf = $groupsAddToSelf[$group] ?? [];
145 $removegroupsSelf = $groupsRemoveFromSelf[$group] ?? [];
146
147 $id = $group == '*' ? false : Sanitizer::escapeIdForAttribute( $group );
148 $out->addHTML( Html::rawElement( 'tr', [ 'id' => $id ], "
149 <td>$grouppage$grouplink</td>
150 <td>" .
151 $this->formatPermissions( $permissions, $revoke, $addgroups, $removegroups,
152 $addgroupsSelf, $removegroupsSelf ) .
153 '</td>
154 '
155 ) );
156 }
157 $out->addHTML( Xml::closeElement( 'table' ) );
159 }
160
161 private function outputNamespaceProtectionInfo() {
162 $out = $this->getOutput();
163 $namespaceProtection = $this->getConfig()->get( 'NamespaceProtection' );
164
165 if ( count( $namespaceProtection ) == 0 ) {
166 return;
167 }
168
169 $header = $this->msg( 'listgrouprights-namespaceprotection-header' )->text();
170 $out->addHTML(
171 Html::rawElement( 'h2', [], Html::element( 'span', [
172 'class' => 'mw-headline',
174 ], $header ) ) .
175 Xml::openElement( 'table', [ 'class' => 'wikitable' ] ) .
176 Html::element(
177 'th',
178 [],
179 $this->msg( 'listgrouprights-namespaceprotection-namespace' )->text()
180 ) .
181 Html::element(
182 'th',
183 [],
184 $this->msg( 'listgrouprights-namespaceprotection-restrictedto' )->text()
185 )
186 );
188 ksort( $namespaceProtection );
189 $validNamespaces = $this->nsInfo->getValidNamespaces();
190 foreach ( $namespaceProtection as $namespace => $rights ) {
191 if ( !in_array( $namespace, $validNamespaces ) ) {
192 continue;
193 }
194
195 if ( $namespace == NS_MAIN ) {
196 $namespaceText = $this->msg( 'blanknamespace' )->text();
197 } else {
198 $namespaceText = $this->languageConverter->convertNamespace( $namespace );
199 }
200
201 $out->addHTML(
202 Xml::openElement( 'tr' ) .
203 Html::rawElement(
204 'td',
205 [],
207 SpecialPage::getTitleFor( 'Allpages' ),
208 $namespaceText,
209 [],
210 [ 'namespace' => $namespace ]
211 )
212 ) .
213 Xml::openElement( 'td' ) . Xml::openElement( 'ul' )
214 );
215
216 if ( !is_array( $rights ) ) {
217 $rights = [ $rights ];
218 }
219
220 foreach ( $rights as $right ) {
221 $out->addHTML(
222 Html::rawElement( 'li', [], $this->msg(
223 'listgrouprights-right-display',
225 Html::element(
226 'span',
227 [ 'class' => 'mw-listgrouprights-right-name' ],
228 $right
229 )
230 )->parse() )
231 );
232 }
233
234 $out->addHTML(
235 Xml::closeElement( 'ul' ) .
236 Xml::closeElement( 'td' ) .
237 Xml::closeElement( 'tr' )
238 );
239 }
240 $out->addHTML( Xml::closeElement( 'table' ) );
241 }
242
254 private function formatPermissions( $permissions, $revoke, $add, $remove, $addSelf, $removeSelf ) {
255 $r = [];
256 foreach ( $permissions as $permission => $granted ) {
257 // show as granted only if it isn't revoked to prevent duplicate display of permissions
258 if ( $granted && ( !isset( $revoke[$permission] ) || !$revoke[$permission] ) ) {
259 $r[] = $this->msg( 'listgrouprights-right-display',
260 User::getRightDescription( $permission ),
261 '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
262 )->parse();
263 }
264 }
265 foreach ( $revoke as $permission => $revoked ) {
266 if ( $revoked ) {
267 $r[] = $this->msg( 'listgrouprights-right-revoked',
268 User::getRightDescription( $permission ),
269 '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
270 )->parse();
271 }
272 }
273
274 sort( $r );
275
276 $lang = $this->getLanguage();
277 $allGroups = $this->userGroupManager->listAllGroups();
278
279 $changeGroups = [
280 'addgroup' => $add,
281 'removegroup' => $remove,
282 'addgroup-self' => $addSelf,
283 'removegroup-self' => $removeSelf
284 ];
285
286 foreach ( $changeGroups as $messageKey => $changeGroup ) {
287 // @phan-suppress-next-line PhanTypeComparisonFromArray
288 if ( $changeGroup === true ) {
289 // For grep: listgrouprights-addgroup-all, listgrouprights-removegroup-all,
290 // listgrouprights-addgroup-self-all, listgrouprights-removegroup-self-all
291 $r[] = $this->msg( 'listgrouprights-' . $messageKey . '-all' )->escaped();
292 } elseif ( is_array( $changeGroup ) ) {
293 $changeGroup = array_intersect( array_values( array_unique( $changeGroup ) ), $allGroups );
294 if ( count( $changeGroup ) ) {
295 $groupLinks = [];
296 foreach ( $changeGroup as $group ) {
297 $groupLinks[] = UserGroupMembership::getLink( $group, $this->getContext(), 'wiki' );
298 }
299 // For grep: listgrouprights-addgroup, listgrouprights-removegroup,
300 // listgrouprights-addgroup-self, listgrouprights-removegroup-self
301 $r[] = $this->msg( 'listgrouprights-' . $messageKey,
302 $lang->listToText( $groupLinks ), count( $changeGroup ) )->parse();
303 }
304 }
305 }
306
307 if ( empty( $r ) ) {
308 return '';
309 } else {
310 return '<ul><li>' . implode( "</li>\n<li>", $r ) . '</li></ul>';
311 }
312 }
313
314 protected function getGroupName() {
315 return 'users';
316 }
317}
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.
makeKnownLink( $target, $text=null, array $extraAttribs=[], array $query=[])
makeLink( $target, $text=null, array $extraAttribs=[], array $query=[])
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:6102
This special page lists all defined user groups and the associated rights.
execute( $par)
Show the special page.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
ILanguageConverter $languageConverter
__construct(NamespaceInfo $nsInfo, UserGroupManager $userGroupManager, LanguageConverterFactory $languageConverterFactory)
formatPermissions( $permissions, $revoke, $add, $remove, $addSelf, $removeSelf)
Create a user-readable list of permissions from the given array.
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.
LinkRenderer null $linkRenderer
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 getRightDescription( $right)
Get the description of a given right.
Definition User.php:4167
The shared interface for all language converters.
if(!isset( $args[0])) $lang
$header