3 * Functions and constants to deal with grants
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 * A collection of public static functions to deal with grants.
27 * List all known grants.
30 public static function getValidGrants() {
31 global $wgGrantPermissions;
33 return array_keys( $wgGrantPermissions );
37 * Map all grants to corresponding user rights.
38 * @return array grant => array of rights
40 public static function getRightsByGrant() {
41 global $wgGrantPermissions;
44 foreach ( $wgGrantPermissions as $grant => $rights ) {
45 $res[$grant] = array_keys( array_filter( $rights ) );
51 * Fetch the display name of the grant
52 * @param string $grant
53 * @param Language|string|null $lang
54 * @return string Grant description
56 public static function grantName( $grant, $lang = null ) {
57 // Give grep a chance to find the usages:
58 // grant-blockusers, grant-createeditmovepage, grant-delete,
59 // grant-editinterface, grant-editmycssjs, grant-editmywatchlist,
60 // grant-editpage, grant-editprotected, grant-highvolume,
61 // grant-oversight, grant-patrol, grant-protect, grant-rollback,
62 // grant-sendemail, grant-uploadeditmovefile, grant-uploadfile,
63 // grant-basic, grant-viewdeleted, grant-viewmywatchlist,
64 // grant-createaccount
65 $msg = wfMessage( "grant-$grant" );
66 if ( $lang !== null ) {
67 if ( is_string( $lang ) ) {
68 $lang = Language
::factory( $lang );
70 $msg->inLanguage( $lang );
72 if ( !$msg->exists() ) {
73 $msg = wfMessage( 'grant-generic', $grant );
75 $msg->inLanguage( $lang );
82 * Fetch the display names for the grants.
83 * @param string[] $grants
84 * @param Language|string|null $lang
85 * @return string[] Corresponding grant descriptions
87 public static function grantNames( array $grants, $lang = null ) {
88 if ( $lang !== null ) {
89 if ( is_string( $lang ) ) {
90 $lang = Language
::factory( $lang );
95 foreach ( $grants as $grant ) {
96 $ret[] = self
::grantName( $grant, $lang );
102 * Fetch the rights allowed by a set of grants.
103 * @param string[]|string $grants
106 public static function getGrantRights( $grants ) {
107 global $wgGrantPermissions;
110 foreach ( (array)$grants as $grant ) {
111 if ( isset( $wgGrantPermissions[$grant] ) ) {
112 $rights = array_merge( $rights, array_keys( array_filter( $wgGrantPermissions[$grant] ) ) );
115 return array_unique( $rights );
119 * Test that all grants in the list are known.
120 * @param string[] $grants
123 public static function grantsAreValid( array $grants ) {
124 return array_diff( $grants, self
::getValidGrants() ) === [];
128 * Divide the grants into groups.
129 * @param string[]|null $grantsFilter
130 * @return array Map of (group => (grant list))
132 public static function getGrantGroups( $grantsFilter = null ) {
133 global $wgGrantPermissions, $wgGrantPermissionGroups;
135 if ( is_array( $grantsFilter ) ) {
136 $grantsFilter = array_flip( $grantsFilter );
140 foreach ( $wgGrantPermissions as $grant => $rights ) {
141 if ( $grantsFilter !== null && !isset( $grantsFilter[$grant] ) ) {
144 if ( isset( $wgGrantPermissionGroups[$grant] ) ) {
145 $groups[$wgGrantPermissionGroups[$grant]][] = $grant;
147 $groups['other'][] = $grant;
155 * Get the list of grants that are hidden and should always be granted
158 public static function getHiddenGrants() {
159 global $wgGrantPermissionGroups;
162 foreach ( $wgGrantPermissionGroups as $grant => $group ) {
163 if ( $group === 'hidden' ) {
171 * Generate a link to Special:ListGrants for a particular grant name.
173 * This should be used to link end users to a full description of what
174 * rights they are giving when they authorize a grant.
176 * @param string $grant the grant name
177 * @param Language|string|null $lang
178 * @return string (proto-relative) HTML link
180 public static function getGrantsLink( $grant, $lang = null ) {
181 return \Linker
::linkKnown(
182 \SpecialPage
::getTitleFor( 'Listgrants', false, $grant ),
183 htmlspecialchars( self
::grantName( $grant, $lang ) )
188 * Generate wikitext to display a list of grants
189 * @param string[]|null $grantsFilter If non-null, only display these grants.
190 * @param Language|string|null $lang
191 * @return string Wikitext
193 public static function getGrantsWikiText( $grantsFilter, $lang = null ) {
196 if ( is_string( $lang ) ) {
197 $lang = Language
::factory( $lang );
198 } elseif ( $lang === null ) {
203 foreach ( self
::getGrantGroups( $grantsFilter ) as $group => $grants ) {
204 if ( $group === 'hidden' ) {
205 continue; // implicitly granted
207 $s .= "*<span class=\"mw-grantgroup\">" .
208 wfMessage( "grant-group-$group" )->inLanguage( $lang )->text() . "</span>\n";
209 $s .= ":" . $lang->semicolonList( self
::grantNames( $grants, $lang ) ) . "\n";