MediaWiki  master
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  $groupWithParentheses = $this->msg( 'parentheses' )->rawParams( $group )->escaped();
127  $groupname = "<br /><code>$groupWithParentheses</code>";
128 
129  if ( $group === 'user' ) {
130  // Link to Special:listusers for implicit group 'user'
131  $grouplink = '<br />' . $linkRenderer->makeKnownLink(
132  SpecialPage::getTitleFor( 'Listusers' ),
133  $this->msg( 'listgrouprights-members' )->text()
134  );
135  } elseif ( !in_array( $group, $config->get( MainConfigNames::ImplicitGroups ) ) ) {
136  $grouplink = '<br />' . $linkRenderer->makeKnownLink(
137  SpecialPage::getTitleFor( 'Listusers' ),
138  $this->msg( 'listgrouprights-members' )->text(),
139  [],
140  [ 'group' => $group ]
141  );
142  } else {
143  // No link to Special:listusers for other implicit groups as they are unlistable
144  $grouplink = '';
145  }
146 
147  $revoke = $this->groupPermissionsLookup->getRevokedPermissions( $group );
148  $addgroups = $addGroups[$group] ?? [];
149  $removegroups = $removeGroups[$group] ?? [];
150  $addgroupsSelf = $groupsAddToSelf[$group] ?? [];
151  $removegroupsSelf = $groupsRemoveFromSelf[$group] ?? [];
152 
153  $id = $group == '*' ? false : Sanitizer::escapeIdForAttribute( $group );
154  $out->addHTML( Html::rawElement( 'tr', [ 'id' => $id ], "
155  <td>$grouppage$groupname$grouplink</td>
156  <td>" .
157  $this->formatPermissions( $permissions, $revoke, $addgroups, $removegroups,
158  $addgroupsSelf, $removegroupsSelf ) .
159  '</td>
160  '
161  ) );
162  }
163  $out->addHTML( Xml::closeElement( 'table' ) );
164  $this->outputNamespaceProtectionInfo();
165  }
166 
167  private function outputNamespaceProtectionInfo() {
168  $out = $this->getOutput();
169  $namespaceProtection = $this->getConfig()->get( MainConfigNames::NamespaceProtection );
170 
171  if ( count( $namespaceProtection ) == 0 ) {
172  return;
173  }
174 
175  $header = $this->msg( 'listgrouprights-namespaceprotection-header' )->text();
176  $out->addHTML(
177  Html::rawElement( 'h2', [], Html::element( 'span', [
178  'class' => 'mw-headline',
179  'id' => substr( Parser::guessSectionNameFromStrippedText( $header ), 1 )
180  ], $header ) ) .
181  Xml::openElement( 'table', [ 'class' => 'wikitable' ] ) .
183  'th',
184  [],
185  $this->msg( 'listgrouprights-namespaceprotection-namespace' )->text()
186  ) .
188  'th',
189  [],
190  $this->msg( 'listgrouprights-namespaceprotection-restrictedto' )->text()
191  )
192  );
193  $linkRenderer = $this->getLinkRenderer();
194  ksort( $namespaceProtection );
195  $validNamespaces = $this->nsInfo->getValidNamespaces();
196  foreach ( $namespaceProtection as $namespace => $rights ) {
197  if ( !in_array( $namespace, $validNamespaces ) ) {
198  continue;
199  }
200 
201  if ( $namespace == NS_MAIN ) {
202  $namespaceText = $this->msg( 'blanknamespace' )->text();
203  } else {
204  $namespaceText = $this->languageConverter->convertNamespace( $namespace );
205  }
206 
207  $out->addHTML(
208  Xml::openElement( 'tr' ) .
210  'td',
211  [],
212  $linkRenderer->makeLink(
213  SpecialPage::getTitleFor( 'Allpages' ),
214  $namespaceText,
215  [],
216  [ 'namespace' => $namespace ]
217  )
218  ) .
219  Xml::openElement( 'td' ) . Xml::openElement( 'ul' )
220  );
221 
222  if ( !is_array( $rights ) ) {
223  $rights = [ $rights ];
224  }
225 
226  foreach ( $rights as $right ) {
227  $out->addHTML(
228  Html::rawElement( 'li', [], $this->msg(
229  'listgrouprights-right-display',
230  User::getRightDescription( $right ),
232  'span',
233  [ 'class' => 'mw-listgrouprights-right-name' ],
234  $right
235  )
236  )->parse() )
237  );
238  }
239 
240  $out->addHTML(
241  Xml::closeElement( 'ul' ) .
242  Xml::closeElement( 'td' ) .
243  Xml::closeElement( 'tr' )
244  );
245  }
246  $out->addHTML( Xml::closeElement( 'table' ) );
247  }
248 
260  private function formatPermissions( $permissions, $revoke, $add, $remove, $addSelf, $removeSelf ) {
261  $r = [];
262  foreach ( $permissions as $permission ) {
263  // show as granted only if it isn't revoked to prevent duplicate display of permissions
264  if ( !isset( $revoke[$permission] ) || !$revoke[$permission] ) {
265  $r[] = $this->msg( 'listgrouprights-right-display',
266  User::getRightDescription( $permission ),
267  '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
268  )->parse();
269  }
270  }
271  foreach ( $revoke as $permission ) {
272  $r[] = $this->msg( 'listgrouprights-right-revoked',
273  User::getRightDescription( $permission ),
274  '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
275  )->parse();
276  }
277 
278  sort( $r );
279 
280  $lang = $this->getLanguage();
281  $allGroups = $this->userGroupManager->listAllGroups();
282 
283  $changeGroups = [
284  'addgroup' => $add,
285  'removegroup' => $remove,
286  'addgroup-self' => $addSelf,
287  'removegroup-self' => $removeSelf
288  ];
289 
290  foreach ( $changeGroups as $messageKey => $changeGroup ) {
291  // @phan-suppress-next-line PhanTypeComparisonFromArray
292  if ( $changeGroup === true ) {
293  // For grep: listgrouprights-addgroup-all, listgrouprights-removegroup-all,
294  // listgrouprights-addgroup-self-all, listgrouprights-removegroup-self-all
295  $r[] = $this->msg( 'listgrouprights-' . $messageKey . '-all' )->escaped();
296  } elseif ( is_array( $changeGroup ) ) {
297  $changeGroup = array_intersect( array_values( array_unique( $changeGroup ) ), $allGroups );
298  if ( count( $changeGroup ) ) {
299  $groupLinks = [];
300  foreach ( $changeGroup as $group ) {
301  $groupLinks[] = UserGroupMembership::getLink( $group, $this->getContext(), 'wiki' );
302  }
303  // For grep: listgrouprights-addgroup, listgrouprights-removegroup,
304  // listgrouprights-addgroup-self, listgrouprights-removegroup-self
305  $r[] = $this->msg( 'listgrouprights-' . $messageKey,
306  $lang->listToText( $groupLinks ), count( $changeGroup ) )->parse();
307  }
308  }
309  }
310 
311  if ( empty( $r ) ) {
312  return '';
313  } else {
314  return '<ul><li>' . implode( "</li>\n<li>", $r ) . '</li></ul>';
315  }
316  }
317 
318  protected function getGroupName() {
319  return 'users';
320  }
321 }
const NS_MAIN
Definition: Defines.php:64
const NS_PROJECT
Definition: Defines.php:68
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:236
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:214
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:6222
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:946
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.
Definition: SpecialPage.php:44
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 makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:667
static getGroupPage( $group)
Gets the title of a page describing a particular user group.
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:3338
static closeElement( $element)
Shortcut to close an XML element.
Definition: Xml.php:121
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:112
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition: Xml.php:43
if(!isset( $args[0])) $lang
$header