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  // @phan-suppress-next-line PhanTypeComparisonFromArray
268  if ( $changeGroup === true ) {
269  // For grep: listgrouprights-addgroup-all, listgrouprights-removegroup-all,
270  // listgrouprights-addgroup-self-all, listgrouprights-removegroup-self-all
271  $r[] = $this->msg( 'listgrouprights-' . $messageKey . '-all' )->escaped();
272  } elseif ( is_array( $changeGroup ) ) {
273  $changeGroup = array_intersect( array_values( array_unique( $changeGroup ) ), $allGroups );
274  if ( count( $changeGroup ) ) {
275  $groupLinks = [];
276  foreach ( $changeGroup as $group ) {
277  $groupLinks[] = UserGroupMembership::getLink( $group, $this->getContext(), 'wiki' );
278  }
279  // For grep: listgrouprights-addgroup, listgrouprights-removegroup,
280  // listgrouprights-addgroup-self, listgrouprights-removegroup-self
281  $r[] = $this->msg( 'listgrouprights-' . $messageKey,
282  $lang->listToText( $groupLinks ), count( $changeGroup ) )->parse();
283  }
284  }
285  }
286 
287  if ( empty( $r ) ) {
288  return '';
289  } else {
290  return '<ul><li>' . implode( "</li>\n<li>", $r ) . '</li></ul>';
291  }
292  }
293 
294  protected function getGroupName() {
295  return 'users';
296  }
297 }
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
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.
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.
static getAllGroups()
Return the set of defined explicit groups.
Definition: User.php:4805
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
const NS_PROJECT
Definition: Defines.php:64
static getRightDescription( $right)
Get the description of a given right.
Definition: User.php:5017
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! ...
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.
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.
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:6166
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:319