MediaWiki  master
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 
100  $linkRenderer = $this->getLinkRenderer();
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',
173  'id' => substr( Parser::guessSectionNameFromStrippedText( $header ), 1 )
174  ], $header ) ) .
175  Xml::openElement( 'table', [ 'class' => 'wikitable' ] ) .
177  'th',
178  [],
179  $this->msg( 'listgrouprights-namespaceprotection-namespace' )->text()
180  ) .
182  'th',
183  [],
184  $this->msg( 'listgrouprights-namespaceprotection-restrictedto' )->text()
185  )
186  );
187  $linkRenderer = $this->getLinkRenderer();
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' ) .
204  'td',
205  [],
206  $linkRenderer->makeLink(
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',
224  User::getRightDescription( $right ),
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 }
SpecialListGroupRights\execute
execute( $par)
Show the special page.
Definition: SpecialListGroupRights.php:65
SpecialPage\$linkRenderer
LinkRenderer null $linkRenderer
Definition: SpecialPage.php:80
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:912
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:790
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
Sanitizer\escapeIdForAttribute
static escapeIdForAttribute( $id, $mode=self::ID_PRIMARY)
Given a section name or other user-generated or otherwise unsafe string, escapes it to be a valid HTM...
Definition: Sanitizer.php:811
SpecialListGroupRights\$languageConverter
ILanguageConverter $languageConverter
Definition: SpecialListGroupRights.php:43
UserGroupMembership\getGroupName
static getGroupName( $group)
Gets the localized friendly name for a group, if it exists.
Definition: UserGroupMembership.php:170
SpecialPage\getTitleFor
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,...
Definition: SpecialPage.php:107
SpecialListGroupRights\outputNamespaceProtectionInfo
outputNamespaceProtectionInfo()
Definition: SpecialListGroupRights.php:161
SpecialListGroupRights\$nsInfo
NamespaceInfo $nsInfo
Definition: SpecialListGroupRights.php:37
SpecialPage\getLanguage
getLanguage()
Shortcut to get user's language.
Definition: SpecialPage.php:830
UserGroupMembership\getGroupPage
static getGroupPage( $group)
Gets the title of a page describing a particular user group.
Definition: UserGroupMembership.php:193
Xml\openElement
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:110
MediaWiki\Languages\LanguageConverterFactory
An interface for creating language converters.
Definition: LanguageConverterFactory.php:46
NS_MAIN
const NS_MAIN
Definition: Defines.php:64
MediaWiki\User\UserGroupManager
Managers user groups.
Definition: UserGroupManager.php:52
SpecialPage\addHelpLink
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Definition: SpecialPage.php:948
SpecialPage\getConfig
getConfig()
Shortcut to get main config object.
Definition: SpecialPage.php:878
SpecialListGroupRights\formatPermissions
formatPermissions( $permissions, $revoke, $add, $remove, $addSelf, $removeSelf)
Create a user-readable list of permissions from the given array.
Definition: SpecialListGroupRights.php:254
MediaWiki\Languages\LanguageConverterFactory\getLanguageConverter
getLanguageConverter( $language=null)
Provide a LanguageConverter for given language.
Definition: LanguageConverterFactory.php:125
UserGroupMembership\getLink
static getLink( $ugm, IContextSource $context, $format, $userName=null)
Gets a link for a user group, possibly including the expiry date if relevant.
Definition: UserGroupMembership.php:104
NS_PROJECT
const NS_PROJECT
Definition: Defines.php:68
Xml\element
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition: Xml.php:41
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:618
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:764
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:689
SpecialListGroupRights\__construct
__construct(NamespaceInfo $nsInfo, UserGroupManager $userGroupManager, LanguageConverterFactory $languageConverterFactory)
Definition: SpecialListGroupRights.php:50
ILanguageConverter
The shared interface for all language converters.
Definition: ILanguageConverter.php:29
$header
$header
Definition: updateCredits.php:37
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:43
SpecialPage\getLinkRenderer
getLinkRenderer()
Definition: SpecialPage.php:1028
Xml\closeElement
static closeElement( $element)
Shortcut to close an XML element.
Definition: Xml.php:119
User\getRightDescription
static getRightDescription( $right)
Get the description of a given right.
Definition: User.php:3554
SpecialListGroupRights\$userGroupManager
UserGroupManager $userGroupManager
Definition: SpecialListGroupRights.php:40
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:210
SpecialListGroupRights\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialListGroupRights.php:314
SpecialListGroupRights
This special page lists all defined user groups and the associated rights.
Definition: SpecialListGroupRights.php:34
Parser\guessSectionNameFromStrippedText
static guessSectionNameFromStrippedText( $text)
Like guessSectionNameFromWikiText(), but takes already-stripped text as input.
Definition: Parser.php:6099
NamespaceInfo
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
Definition: NamespaceInfo.php:35
SpecialPage\getContentLanguage
getContentLanguage()
Shortcut to get content language.
Definition: SpecialPage.php:840
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:232
SpecialPage\outputHeader
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
Definition: SpecialPage.php:709