MediaWiki  1.34.0
MWGrants.php
Go to the documentation of this file.
1 <?php
21 
25 class MWGrants {
26 
31  public static function getValidGrants() {
32  global $wgGrantPermissions;
33 
34  return array_keys( $wgGrantPermissions );
35  }
36 
41  public static function getRightsByGrant() {
42  global $wgGrantPermissions;
43 
44  $res = [];
45  foreach ( $wgGrantPermissions as $grant => $rights ) {
46  $res[$grant] = array_keys( array_filter( $rights ) );
47  }
48  return $res;
49  }
50 
57  public static function grantName( $grant, $lang = null ) {
58  // Give grep a chance to find the usages:
59  // grant-blockusers, grant-createeditmovepage, grant-delete,
60  // grant-editinterface, grant-editmycssjs, grant-editmywatchlist,
61  // grant-editsiteconfig, grant-editpage, grant-editprotected,
62  // grant-highvolume, grant-oversight, grant-patrol, grant-protect,
63  // grant-rollback, grant-sendemail, grant-uploadeditmovefile,
64  // grant-uploadfile, grant-basic, grant-viewdeleted,
65  // grant-viewmywatchlist, grant-createaccount
66  $msg = wfMessage( "grant-$grant" );
67  if ( $lang !== null ) {
68  if ( is_string( $lang ) ) {
70  }
71  $msg->inLanguage( $lang );
72  }
73  if ( !$msg->exists() ) {
74  $msg = wfMessage( 'grant-generic', $grant );
75  if ( $lang ) {
76  $msg->inLanguage( $lang );
77  }
78  }
79  return $msg->text();
80  }
81 
88  public static function grantNames( array $grants, $lang = null ) {
89  if ( $lang !== null && is_string( $lang ) ) {
91  }
92 
93  $ret = [];
94  foreach ( $grants as $grant ) {
95  $ret[] = self::grantName( $grant, $lang );
96  }
97  return $ret;
98  }
99 
105  public static function getGrantRights( $grants ) {
106  global $wgGrantPermissions;
107 
108  $rights = [];
109  foreach ( (array)$grants as $grant ) {
110  if ( isset( $wgGrantPermissions[$grant] ) ) {
111  $rights = array_merge( $rights, array_keys( array_filter( $wgGrantPermissions[$grant] ) ) );
112  }
113  }
114  return array_unique( $rights );
115  }
116 
122  public static function grantsAreValid( array $grants ) {
123  return array_diff( $grants, self::getValidGrants() ) === [];
124  }
125 
131  public static function getGrantGroups( $grantsFilter = null ) {
133 
134  if ( is_array( $grantsFilter ) ) {
135  $grantsFilter = array_flip( $grantsFilter );
136  }
137 
138  $groups = [];
139  foreach ( $wgGrantPermissions as $grant => $rights ) {
140  if ( $grantsFilter !== null && !isset( $grantsFilter[$grant] ) ) {
141  continue;
142  }
143  if ( isset( $wgGrantPermissionGroups[$grant] ) ) {
144  $groups[$wgGrantPermissionGroups[$grant]][] = $grant;
145  } else {
146  $groups['other'][] = $grant;
147  }
148  }
149 
150  return $groups;
151  }
152 
157  public static function getHiddenGrants() {
159 
160  $grants = [];
161  foreach ( $wgGrantPermissionGroups as $grant => $group ) {
162  if ( $group === 'hidden' ) {
163  $grants[] = $grant;
164  }
165  }
166  return $grants;
167  }
168 
179  public static function getGrantsLink( $grant, $lang = null ) {
180  $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
181  return $linkRenderer->makeKnownLink(
182  \SpecialPage::getTitleFor( 'Listgrants', false, $grant ),
183  self::grantName( $grant, $lang )
184  );
185  }
186 
193  public static function getGrantsWikiText( $grantsFilter, $lang = null ) {
194  if ( is_string( $lang ) ) {
196  } elseif ( $lang === null ) {
197  $lang = MediaWikiServices::getInstance()->getContentLanguage();
198  }
199 
200  $s = '';
201  foreach ( self::getGrantGroups( $grantsFilter ) as $group => $grants ) {
202  if ( $group === 'hidden' ) {
203  continue; // implicitly granted
204  }
205  $s .= "*<span class=\"mw-grantgroup\">" .
206  wfMessage( "grant-group-$group" )->inLanguage( $lang )->text() . "</span>\n";
207  $s .= ":" . $lang->semicolonList( self::grantNames( $grants, $lang ) ) . "\n";
208  }
209  return "$s\n";
210  }
211 
212 }
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
MWGrants\getGrantRights
static getGrantRights( $grants)
Fetch the rights allowed by a set of grants.
Definition: MWGrants.php:105
MWGrants\grantNames
static grantNames(array $grants, $lang=null)
Fetch the display names for the grants.
Definition: MWGrants.php:88
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1264
$s
$s
Definition: mergeMessageFileList.php:185
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:83
$res
$res
Definition: testCompression.php:52
MWGrants\getHiddenGrants
static getHiddenGrants()
Get the list of grants that are hidden and should always be granted.
Definition: MWGrants.php:157
MWGrants\getGrantsLink
static getGrantsLink( $grant, $lang=null)
Generate a link to Special:ListGrants for a particular grant name.
Definition: MWGrants.php:179
MWGrants\getGrantsWikiText
static getGrantsWikiText( $grantsFilter, $lang=null)
Generate wikitext to display a list of grants.
Definition: MWGrants.php:193
$wgGrantPermissions
array $wgGrantPermissions
Map of (grant => right => boolean) Users authorize consumers (like Apps) to act on their behalf but o...
Definition: DefaultSettings.php:5811
MWGrants\grantName
static grantName( $grant, $lang=null)
Fetch the display name of the grant.
Definition: MWGrants.php:57
MWGrants\getValidGrants
static getValidGrants()
List all known grants.
Definition: MWGrants.php:31
MWGrants\grantsAreValid
static grantsAreValid(array $grants)
Test that all grants in the list are known.
Definition: MWGrants.php:122
MWGrants\getGrantGroups
static getGrantGroups( $grantsFilter=null)
Divide the grants into groups.
Definition: MWGrants.php:131
MWGrants
A collection of public static functions to deal with grants.
Definition: MWGrants.php:25
MWGrants\getRightsByGrant
static getRightsByGrant()
Map all grants to corresponding user rights.
Definition: MWGrants.php:41
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:217
$wgGrantPermissionGroups
array $wgGrantPermissionGroups
Map of grants to their UI grouping.
Definition: DefaultSettings.php:5920