MediaWiki  master
SpecialListGroupRights.php
Go to the documentation of this file.
1 <?php
25 
34  public function __construct() {
35  parent::__construct( 'Listgrouprights' );
36  }
37 
42  public function execute( $par ) {
43  $this->setHeaders();
44  $this->outputHeader();
45 
46  $out = $this->getOutput();
47  $out->addModuleStyles( 'mediawiki.special' );
48  $this->addHelpLink( 'Help:User_rights_and_groups' );
49 
50  $out->wrapWikiMsg( "<div class=\"mw-listgrouprights-key\">\n$1\n</div>", 'listgrouprights-key' );
51 
52  $out->addHTML(
53  Xml::openElement( 'table', [ 'class' => 'wikitable mw-listgrouprights-table' ] ) .
54  '<tr>' .
55  Xml::element( 'th', null, $this->msg( 'listgrouprights-group' )->text() ) .
56  Xml::element( 'th', null, $this->msg( 'listgrouprights-rights' )->text() ) .
57  '</tr>'
58  );
59 
60  $config = $this->getConfig();
61  $groupPermissions = $config->get( 'GroupPermissions' );
62  $revokePermissions = $config->get( 'RevokePermissions' );
63  $addGroups = $config->get( 'AddGroups' );
64  $removeGroups = $config->get( 'RemoveGroups' );
65  $groupsAddToSelf = $config->get( 'GroupsAddToSelf' );
66  $groupsRemoveFromSelf = $config->get( 'GroupsRemoveFromSelf' );
67  $allGroups = array_unique( array_merge(
68  array_keys( $groupPermissions ),
69  array_keys( $revokePermissions ),
70  array_keys( $addGroups ),
71  array_keys( $removeGroups ),
72  array_keys( $groupsAddToSelf ),
73  array_keys( $groupsRemoveFromSelf )
74  ) );
75  asort( $allGroups );
76 
77  $linkRenderer = $this->getLinkRenderer();
78 
79  foreach ( $allGroups as $group ) {
80  $permissions = $groupPermissions[$group] ?? [];
81  $groupname = ( $group == '*' ) // Replace * with a more descriptive groupname
82  ? 'all'
83  : $group;
84 
85  $groupnameLocalized = UserGroupMembership::getGroupName( $groupname );
86 
87  $grouppageLocalizedTitle = UserGroupMembership::getGroupPage( $groupname )
88  ?: Title::newFromText( MediaWikiServices::getInstance()->getNamespaceInfo()->
89  getCanonicalName( NS_PROJECT ) . ':' . $groupname );
90 
91  if ( $group == '*' || !$grouppageLocalizedTitle ) {
92  // Do not make a link for the generic * group or group with invalid group page
93  $grouppage = htmlspecialchars( $groupnameLocalized );
94  } else {
95  $grouppage = $linkRenderer->makeLink(
96  $grouppageLocalizedTitle,
97  $groupnameLocalized
98  );
99  }
100 
101  if ( $group === 'user' ) {
102  // Link to Special:listusers for implicit group 'user'
103  $grouplink = '<br />' . $linkRenderer->makeKnownLink(
104  SpecialPage::getTitleFor( 'Listusers' ),
105  $this->msg( 'listgrouprights-members' )->text()
106  );
107  } elseif ( !in_array( $group, $config->get( 'ImplicitGroups' ) ) ) {
108  $grouplink = '<br />' . $linkRenderer->makeKnownLink(
109  SpecialPage::getTitleFor( 'Listusers' ),
110  $this->msg( 'listgrouprights-members' )->text(),
111  [],
112  [ 'group' => $group ]
113  );
114  } else {
115  // No link to Special:listusers for other implicit groups as they are unlistable
116  $grouplink = '';
117  }
118 
119  $revoke = $revokePermissions[$group] ?? [];
120  $addgroups = $addGroups[$group] ?? [];
121  $removegroups = $removeGroups[$group] ?? [];
122  $addgroupsSelf = $groupsAddToSelf[$group] ?? [];
123  $removegroupsSelf = $groupsRemoveFromSelf[$group] ?? [];
124 
125  $id = $group == '*' ? false : Sanitizer::escapeIdForAttribute( $group );
126  $out->addHTML( Html::rawElement( 'tr', [ 'id' => $id ], "
127  <td>$grouppage$grouplink</td>
128  <td>" .
129  $this->formatPermissions( $permissions, $revoke, $addgroups, $removegroups,
130  $addgroupsSelf, $removegroupsSelf ) .
131  '</td>
132  '
133  ) );
134  }
135  $out->addHTML( Xml::closeElement( 'table' ) );
137  }
138 
139  private function outputNamespaceProtectionInfo() {
140  $out = $this->getOutput();
141  $namespaceProtection = $this->getConfig()->get( 'NamespaceProtection' );
142 
143  if ( count( $namespaceProtection ) == 0 ) {
144  return;
145  }
146 
147  $header = $this->msg( 'listgrouprights-namespaceprotection-header' )->text();
148  $out->addHTML(
149  Html::rawElement( 'h2', [], Html::element( 'span', [
150  'class' => 'mw-headline',
151  'id' => substr( Parser::guessSectionNameFromStrippedText( $header ), 1 )
152  ], $header ) ) .
153  Xml::openElement( 'table', [ 'class' => 'wikitable' ] ) .
155  'th',
156  [],
157  $this->msg( 'listgrouprights-namespaceprotection-namespace' )->text()
158  ) .
160  'th',
161  [],
162  $this->msg( 'listgrouprights-namespaceprotection-restrictedto' )->text()
163  )
164  );
165  $linkRenderer = $this->getLinkRenderer();
166  ksort( $namespaceProtection );
167  $validNamespaces =
168  MediaWikiServices::getInstance()->getNamespaceInfo()->getValidNamespaces();
169  $contLang = MediaWikiServices::getInstance()->getContentLanguage();
170  foreach ( $namespaceProtection as $namespace => $rights ) {
171  if ( !in_array( $namespace, $validNamespaces ) ) {
172  continue;
173  }
174 
175  if ( $namespace == NS_MAIN ) {
176  $namespaceText = $this->msg( 'blanknamespace' )->text();
177  } else {
178  $namespaceText = $contLang->convertNamespace( $namespace );
179  }
180 
181  $out->addHTML(
182  Xml::openElement( 'tr' ) .
184  'td',
185  [],
186  $linkRenderer->makeLink(
187  SpecialPage::getTitleFor( 'Allpages' ),
188  $namespaceText,
189  [],
190  [ 'namespace' => $namespace ]
191  )
192  ) .
193  Xml::openElement( 'td' ) . Xml::openElement( 'ul' )
194  );
195 
196  if ( !is_array( $rights ) ) {
197  $rights = [ $rights ];
198  }
199 
200  foreach ( $rights as $right ) {
201  $out->addHTML(
202  Html::rawElement( 'li', [], $this->msg(
203  'listgrouprights-right-display',
204  User::getRightDescription( $right ),
206  'span',
207  [ 'class' => 'mw-listgrouprights-right-name' ],
208  $right
209  )
210  )->parse() )
211  );
212  }
213 
214  $out->addHTML(
215  Xml::closeElement( 'ul' ) .
216  Xml::closeElement( 'td' ) .
217  Xml::closeElement( 'tr' )
218  );
219  }
220  $out->addHTML( Xml::closeElement( 'table' ) );
221  }
222 
234  private function formatPermissions( $permissions, $revoke, $add, $remove, $addSelf, $removeSelf ) {
235  $r = [];
236  foreach ( $permissions as $permission => $granted ) {
237  // show as granted only if it isn't revoked to prevent duplicate display of permissions
238  if ( $granted && ( !isset( $revoke[$permission] ) || !$revoke[$permission] ) ) {
239  $r[] = $this->msg( 'listgrouprights-right-display',
240  User::getRightDescription( $permission ),
241  '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
242  )->parse();
243  }
244  }
245  foreach ( $revoke as $permission => $revoked ) {
246  if ( $revoked ) {
247  $r[] = $this->msg( 'listgrouprights-right-revoked',
248  User::getRightDescription( $permission ),
249  '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
250  )->parse();
251  }
252  }
253 
254  sort( $r );
255 
256  $lang = $this->getLanguage();
257  $allGroups = User::getAllGroups();
258 
259  $changeGroups = [
260  'addgroup' => $add,
261  'removegroup' => $remove,
262  'addgroup-self' => $addSelf,
263  'removegroup-self' => $removeSelf
264  ];
265 
266  foreach ( $changeGroups as $messageKey => $changeGroup ) {
267  if ( $changeGroup === true ) {
268  // For grep: listgrouprights-addgroup-all, listgrouprights-removegroup-all,
269  // listgrouprights-addgroup-self-all, listgrouprights-removegroup-self-all
270  $r[] = $this->msg( 'listgrouprights-' . $messageKey . '-all' )->escaped();
271  } elseif ( is_array( $changeGroup ) ) {
272  $changeGroup = array_intersect( array_values( array_unique( $changeGroup ) ), $allGroups );
273  if ( count( $changeGroup ) ) {
274  $groupLinks = [];
275  foreach ( $changeGroup as $group ) {
276  $groupLinks[] = UserGroupMembership::getLink( $group, $this->getContext(), 'wiki' );
277  }
278  // For grep: listgrouprights-addgroup, listgrouprights-removegroup,
279  // listgrouprights-addgroup-self, listgrouprights-removegroup-self
280  $r[] = $this->msg( 'listgrouprights-' . $messageKey,
281  $lang->listToText( $groupLinks ), count( $changeGroup ) )->parse();
282  }
283  }
284  }
285 
286  if ( empty( $r ) ) {
287  return '';
288  } else {
289  return '<ul><li>' . implode( "</li>\n<li>", $r ) . '</li></ul>';
290  }
291  }
292 
293  protected function getGroupName() {
294  return 'users';
295  }
296 }
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:231
getContext()
Gets the context this SpecialPage is executed in.
const NS_MAIN
Definition: Defines.php:60
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:193
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
if(!isset( $args[0])) $lang
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:209
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
getOutput()
Get the OutputPage being used for this instance.
static getLink( $ugm, IContextSource $context, $format, $userName=null)
Gets a link for a user group, possibly including the expiry date if relevant.
execute( $par)
Show the special page.
This list may contain false positives That usually means there is additional text with links below the first Each row contains links to the first and second as well as the first line of the second redirect text
static getAllGroups()
Return the set of defined explicit groups.
Definition: User.php:4856
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition: hooks.txt:773
const NS_PROJECT
Definition: Defines.php:64
static getRightDescription( $right)
Get the description of a given right.
Definition: User.php:5069
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:108
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:1295
static getGroupName( $group)
Gets the localized friendly name for a group, if it exists.
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes! ...
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:773
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don&#39;t need a full Title object...
Definition: SpecialPage.php:83
$header
static getGroupPage( $group)
Gets the title of a page describing a particular user group.
This special page lists all defined user groups and the associated rights.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
static closeElement( $element)
Shortcut to close an XML element.
Definition: Xml.php:117
getConfig()
Shortcut to get main config object.
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition: Xml.php:41
getLanguage()
Shortcut to get user&#39;s language.
msg( $key)
Wrapper around wfMessage that sets the current context.
formatPermissions( $permissions, $revoke, $add, $remove, $addSelf, $removeSelf)
Create a user-readable list of permissions from the given array.
static guessSectionNameFromStrippedText( $text)
Like guessSectionNameFromWikiText(), but takes already-stripped text as input.
Definition: Parser.php:6177
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
MediaWiki Linker LinkRenderer null $linkRenderer
Definition: SpecialPage.php:67
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:322