MediaWiki REL1_34
MWGrants.php
Go to the documentation of this file.
1<?php
21
25class MWGrants {
26
31 public static function getValidGrants() {
33
34 return array_keys( $wgGrantPermissions );
35 }
36
41 public static function getRightsByGrant() {
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 = Language::factory( $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 ) ) {
90 $lang = Language::factory( $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 ) ) {
195 $lang = Language::factory( $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}
array $wgGrantPermissions
Map of (grant => right => boolean) Users authorize consumers (like Apps) to act on their behalf but o...
array $wgGrantPermissionGroups
Map of grants to their UI grouping.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
A collection of public static functions to deal with grants.
Definition MWGrants.php:25
static getGrantsWikiText( $grantsFilter, $lang=null)
Generate wikitext to display a list of grants.
Definition MWGrants.php:193
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:157
static grantNames(array $grants, $lang=null)
Fetch the display names for the grants.
Definition MWGrants.php:88
static getGrantsLink( $grant, $lang=null)
Generate a link to Special:ListGrants for a particular grant name.
Definition MWGrants.php:179
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:122
static getGrantRights( $grants)
Fetch the rights allowed by a set of grants.
Definition MWGrants.php:105
static getGrantGroups( $grantsFilter=null)
Divide the grants into groups.
Definition MWGrants.php:131
static getValidGrants()
List all known grants.
Definition MWGrants.php:31
MediaWikiServices is the service locator for the application scope of MediaWiki.
if(!isset( $args[0])) $lang