MediaWiki  master
SpecialListGroupRights.php
Go to the documentation of this file.
1 <?php
30 
39 
41  private $nsInfo;
42 
44  private $userGroupManager;
45 
47  private $languageConverter;
48 
50  private $groupPermissionsLookup;
51 
58  public function __construct(
59  NamespaceInfo $nsInfo,
60  UserGroupManager $userGroupManager,
61  LanguageConverterFactory $languageConverterFactory,
62  GroupPermissionsLookup $groupPermissionsLookup
63  ) {
64  parent::__construct( 'Listgrouprights' );
65  $this->nsInfo = $nsInfo;
66  $this->userGroupManager = $userGroupManager;
67  $this->languageConverter = $languageConverterFactory->getLanguageConverter( $this->getContentLanguage() );
68  $this->groupPermissionsLookup = $groupPermissionsLookup;
69  }
70 
75  public function execute( $par ) {
76  $this->setHeaders();
77  $this->outputHeader();
78 
79  $out = $this->getOutput();
80  $out->addModuleStyles( 'mediawiki.special' );
81  $this->addHelpLink( 'Help:User_rights_and_groups' );
82 
83  $out->wrapWikiMsg( "<div class=\"mw-listgrouprights-key\">\n$1\n</div>", 'listgrouprights-key' );
84 
85  $out->addHTML(
86  Xml::openElement( 'table', [ 'class' => 'wikitable mw-listgrouprights-table' ] ) .
87  '<tr>' .
88  Xml::element( 'th', null, $this->msg( 'listgrouprights-group' )->text() ) .
89  Xml::element( 'th', null, $this->msg( 'listgrouprights-rights' )->text() ) .
90  '</tr>'
91  );
92 
93  $config = $this->getConfig();
94  $addGroups = $config->get( MainConfigNames::AddGroups );
95  $removeGroups = $config->get( MainConfigNames::RemoveGroups );
96  $groupsAddToSelf = $config->get( MainConfigNames::GroupsAddToSelf );
97  $groupsRemoveFromSelf = $config->get( MainConfigNames::GroupsRemoveFromSelf );
98  $allGroups = array_merge(
99  $this->userGroupManager->listAllGroups(),
100  $this->userGroupManager->listAllImplicitGroups()
101  );
102  asort( $allGroups );
103 
104  $linkRenderer = $this->getLinkRenderer();
105  $lang = $this->getLanguage();
106 
107  foreach ( $allGroups as $group ) {
108  $permissions = $this->groupPermissionsLookup->getGrantedPermissions( $group );
109  $groupname = ( $group == '*' ) // Replace * with a more descriptive groupname
110  ? 'all'
111  : $group;
112 
113  $groupnameLocalized = $lang->getGroupName( $groupname );
114 
115  $grouppageLocalizedTitle = UserGroupMembership::getGroupPage( $groupname )
116  ?: Title::makeTitleSafe( NS_PROJECT, $groupname );
117 
118  if ( $group == '*' || !$grouppageLocalizedTitle ) {
119  // Do not make a link for the generic * group or group with invalid group page
120  $grouppage = htmlspecialchars( $groupnameLocalized );
121  } else {
122  $grouppage = $linkRenderer->makeLink(
123  $grouppageLocalizedTitle,
124  $groupnameLocalized
125  );
126  }
127 
128  $groupWithParentheses = $this->msg( 'parentheses' )->rawParams( $group )->escaped();
129  $groupname = "<br /><code>$groupWithParentheses</code>";
130 
131  if ( $group === 'user' ) {
132  // Link to Special:listusers for implicit group 'user'
133  $grouplink = '<br />' . $linkRenderer->makeKnownLink(
134  SpecialPage::getTitleFor( 'Listusers' ),
135  $this->msg( 'listgrouprights-members' )->text()
136  );
137  } elseif ( !in_array( $group, $config->get( MainConfigNames::ImplicitGroups ) ) ) {
138  $grouplink = '<br />' . $linkRenderer->makeKnownLink(
139  SpecialPage::getTitleFor( 'Listusers' ),
140  $this->msg( 'listgrouprights-members' )->text(),
141  [],
142  [ 'group' => $group ]
143  );
144  } else {
145  // No link to Special:listusers for other implicit groups as they are unlistable
146  $grouplink = '';
147  }
148 
149  $revoke = $this->groupPermissionsLookup->getRevokedPermissions( $group );
150  $addgroups = $addGroups[$group] ?? [];
151  $removegroups = $removeGroups[$group] ?? [];
152  $addgroupsSelf = $groupsAddToSelf[$group] ?? [];
153  $removegroupsSelf = $groupsRemoveFromSelf[$group] ?? [];
154 
155  $id = $group == '*' ? false : Sanitizer::escapeIdForAttribute( $group );
156  $out->addHTML( Html::rawElement( 'tr', [ 'id' => $id ], "
157  <td>$grouppage$groupname$grouplink</td>
158  <td>" .
159  $this->formatPermissions( $permissions, $revoke, $addgroups, $removegroups,
160  $addgroupsSelf, $removegroupsSelf ) .
161  '</td>
162  '
163  ) );
164  }
165  $out->addHTML( Xml::closeElement( 'table' ) );
166  $this->outputNamespaceProtectionInfo();
167  }
168 
169  private function outputNamespaceProtectionInfo() {
170  $out = $this->getOutput();
171  $namespaceProtection = $this->getConfig()->get( MainConfigNames::NamespaceProtection );
172 
173  if ( count( $namespaceProtection ) == 0 ) {
174  return;
175  }
176 
177  $header = $this->msg( 'listgrouprights-namespaceprotection-header' )->text();
178  $out->addHTML(
179  Html::element( 'h2', [
181  ], $header ) .
182  Xml::openElement( 'table', [ 'class' => 'wikitable' ] ) .
183  Html::element(
184  'th',
185  [],
186  $this->msg( 'listgrouprights-namespaceprotection-namespace' )->text()
187  ) .
188  Html::element(
189  'th',
190  [],
191  $this->msg( 'listgrouprights-namespaceprotection-restrictedto' )->text()
192  )
193  );
194  $linkRenderer = $this->getLinkRenderer();
195  ksort( $namespaceProtection );
196  $validNamespaces = $this->nsInfo->getValidNamespaces();
197  foreach ( $namespaceProtection as $namespace => $rights ) {
198  if ( !in_array( $namespace, $validNamespaces ) ) {
199  continue;
200  }
201 
202  if ( $namespace == NS_MAIN ) {
203  $namespaceText = $this->msg( 'blanknamespace' )->text();
204  } else {
205  $namespaceText = $this->languageConverter->convertNamespace( $namespace );
206  }
207 
208  $out->addHTML(
209  Xml::openElement( 'tr' ) .
210  Html::rawElement(
211  'td',
212  [],
213  $linkRenderer->makeLink(
214  SpecialPage::getTitleFor( 'Allpages' ),
215  $namespaceText,
216  [],
217  [ 'namespace' => $namespace ]
218  )
219  ) .
220  Xml::openElement( 'td' ) . Xml::openElement( 'ul' )
221  );
222 
223  if ( !is_array( $rights ) ) {
224  $rights = [ $rights ];
225  }
226 
227  foreach ( $rights as $right ) {
228  $out->addHTML(
229  Html::rawElement( 'li', [], $this->msg(
230  'listgrouprights-right-display',
231  User::getRightDescription( $right ),
232  Html::element(
233  'span',
234  [ 'class' => 'mw-listgrouprights-right-name' ],
235  $right
236  )
237  )->parse() )
238  );
239  }
240 
241  $out->addHTML(
242  Xml::closeElement( 'ul' ) .
243  Xml::closeElement( 'td' ) .
244  Xml::closeElement( 'tr' )
245  );
246  }
247  $out->addHTML( Xml::closeElement( 'table' ) );
248  }
249 
261  private function formatPermissions( $permissions, $revoke, $add, $remove, $addSelf, $removeSelf ) {
262  $r = [];
263  foreach ( $permissions as $permission ) {
264  // show as granted only if it isn't revoked to prevent duplicate display of permissions
265  if ( !isset( $revoke[$permission] ) || !$revoke[$permission] ) {
266  $r[] = $this->msg( 'listgrouprights-right-display',
267  User::getRightDescription( $permission ),
268  '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
269  )->parse();
270  }
271  }
272  foreach ( $revoke as $permission ) {
273  $r[] = $this->msg( 'listgrouprights-right-revoked',
274  User::getRightDescription( $permission ),
275  '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
276  )->parse();
277  }
278 
279  sort( $r );
280 
281  $lang = $this->getLanguage();
282  $allGroups = $this->userGroupManager->listAllGroups();
283 
284  $changeGroups = [
285  'addgroup' => $add,
286  'removegroup' => $remove,
287  'addgroup-self' => $addSelf,
288  'removegroup-self' => $removeSelf
289  ];
290 
291  foreach ( $changeGroups as $messageKey => $changeGroup ) {
292  // @phan-suppress-next-line PhanTypeComparisonFromArray
293  if ( $changeGroup === true ) {
294  // For grep: listgrouprights-addgroup-all, listgrouprights-removegroup-all,
295  // listgrouprights-addgroup-self-all, listgrouprights-removegroup-self-all
296  $r[] = $this->msg( 'listgrouprights-' . $messageKey . '-all' )->escaped();
297  } elseif ( is_array( $changeGroup ) ) {
298  $changeGroup = array_intersect( array_values( array_unique( $changeGroup ) ), $allGroups );
299  if ( count( $changeGroup ) ) {
300  $groupLinks = [];
301  foreach ( $changeGroup as $group ) {
302  $groupLinks[] = UserGroupMembership::getLink( $group, $this->getContext(), 'wiki' );
303  }
304  // For grep: listgrouprights-addgroup, listgrouprights-removegroup,
305  // listgrouprights-addgroup-self, listgrouprights-removegroup-self
306  $r[] = $this->msg( 'listgrouprights-' . $messageKey,
307  $lang->listToText( $groupLinks ), count( $changeGroup ) )->parse();
308  }
309  }
310  }
311 
312  if ( empty( $r ) ) {
313  return '';
314  } else {
315  return '<ul><li>' . implode( "</li>\n<li>", $r ) . '</li></ul>';
316  }
317  }
318 
319  protected function getGroupName() {
320  return 'users';
321  }
322 }
const NS_MAIN
Definition: Defines.php:64
const NS_PROJECT
Definition: Defines.php:68
This class is a collection of static functions that serve two purposes:
Definition: Html.php:55
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.
Represents a title within MediaWiki.
Definition: Title.php:82
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
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:45
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 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:3352
static closeElement( $element)
Shortcut to close an XML element.
Definition: Xml.php:122
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:113
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition: Xml.php:44
if(!isset( $args[0])) $lang
$header