MediaWiki  master
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 ) ) {
69  $lang = MediaWikiServices::getInstance()->getLanguageFactory()
70  ->getLanguage( $lang );
71  }
72  $msg->inLanguage( $lang );
73  }
74  if ( !$msg->exists() ) {
75  $msg = wfMessage( 'grant-generic', $grant );
76  if ( $lang ) {
77  $msg->inLanguage( $lang );
78  }
79  }
80  return $msg->text();
81  }
82 
89  public static function grantNames( array $grants, $lang = null ) {
90  if ( $lang !== null && is_string( $lang ) ) {
91  $lang = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( $lang );
92  }
93 
94  $ret = [];
95  foreach ( $grants as $grant ) {
96  $ret[] = self::grantName( $grant, $lang );
97  }
98  return $ret;
99  }
100 
106  public static function getGrantRights( $grants ) {
107  global $wgGrantPermissions;
108 
109  $rights = [];
110  foreach ( (array)$grants as $grant ) {
111  if ( isset( $wgGrantPermissions[$grant] ) ) {
112  $rights = array_merge( $rights, array_keys( array_filter( $wgGrantPermissions[$grant] ) ) );
113  }
114  }
115  return array_unique( $rights );
116  }
117 
123  public static function grantsAreValid( array $grants ) {
124  return array_diff( $grants, self::getValidGrants() ) === [];
125  }
126 
132  public static function getGrantGroups( $grantsFilter = null ) {
134 
135  if ( is_array( $grantsFilter ) ) {
136  $grantsFilter = array_flip( $grantsFilter );
137  }
138 
139  $groups = [];
140  foreach ( $wgGrantPermissions as $grant => $rights ) {
141  if ( $grantsFilter !== null && !isset( $grantsFilter[$grant] ) ) {
142  continue;
143  }
144  if ( isset( $wgGrantPermissionGroups[$grant] ) ) {
145  $groups[$wgGrantPermissionGroups[$grant]][] = $grant;
146  } else {
147  $groups['other'][] = $grant;
148  }
149  }
150 
151  return $groups;
152  }
153 
158  public static function getHiddenGrants() {
160 
161  $grants = [];
162  foreach ( $wgGrantPermissionGroups as $grant => $group ) {
163  if ( $group === 'hidden' ) {
164  $grants[] = $grant;
165  }
166  }
167  return $grants;
168  }
169 
180  public static function getGrantsLink( $grant, $lang = null ) {
181  $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
182  return $linkRenderer->makeKnownLink(
183  \SpecialPage::getTitleFor( 'Listgrants', false, $grant ),
184  self::grantName( $grant, $lang )
185  );
186  }
187 
194  public static function getGrantsWikiText( $grantsFilter, $lang = null ) {
195  if ( is_string( $lang ) ) {
196  $lang = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( $lang );
197  } elseif ( $lang === null ) {
198  $lang = MediaWikiServices::getInstance()->getContentLanguage();
199  }
200 
201  $s = '';
202  foreach ( self::getGrantGroups( $grantsFilter ) as $group => $grants ) {
203  if ( $group === 'hidden' ) {
204  continue; // implicitly granted
205  }
206  $s .= "*<span class=\"mw-grantgroup\">" .
207  wfMessage( "grant-group-$group" )->inLanguage( $lang )->text() . "</span>\n";
208  $s .= ":" . $lang->semicolonList( self::grantNames( $grants, $lang ) ) . "\n";
209  }
210  return "$s\n";
211  }
212 
213 }
static getValidGrants()
List all known grants.
Definition: MWGrants.php:31
if(!isset( $args[0])) $lang
static getGrantGroups( $grantsFilter=null)
Divide the grants into groups.
Definition: MWGrants.php:132
A collection of public static functions to deal with grants.
Definition: MWGrants.php:25
static grantName( $grant, $lang=null)
Fetch the display name of the grant.
Definition: MWGrants.php:57
static getHiddenGrants()
Get the list of grants that are hidden and should always be granted.
Definition: MWGrants.php:158
array $wgGrantPermissionGroups
Map of grants to their UI grouping.
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
array $wgGrantPermissions
Map of (grant => right => boolean) Users authorize consumers (like Apps) to act on their behalf but o...
static getGrantsWikiText( $grantsFilter, $lang=null)
Generate wikitext to display a list of grants.
Definition: MWGrants.php:194
static getRightsByGrant()
Map all grants to corresponding user rights.
Definition: MWGrants.php:41
static grantsAreValid(array $grants)
Test that all grants in the list are known.
Definition: MWGrants.php:123
static getGrantsLink( $grant, $lang=null)
Generate a link to Special:ListGrants for a particular grant name.
Definition: MWGrants.php:180
static grantNames(array $grants, $lang=null)
Fetch the display names for the grants.
Definition: MWGrants.php:89
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
static getGrantRights( $grants)
Fetch the rights allowed by a set of grants.
Definition: MWGrants.php:106