MWGrants.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. */
  20. use MediaWiki\MediaWikiServices;
  21. /**
  22. * A collection of public static functions to deal with grants.
  23. */
  24. class MWGrants {
  25. /**
  26. * List all known grants.
  27. * @return array
  28. */
  29. public static function getValidGrants() {
  30. global $wgGrantPermissions;
  31. return array_keys( $wgGrantPermissions );
  32. }
  33. /**
  34. * Map all grants to corresponding user rights.
  35. * @return array grant => array of rights
  36. */
  37. public static function getRightsByGrant() {
  38. global $wgGrantPermissions;
  39. $res = [];
  40. foreach ( $wgGrantPermissions as $grant => $rights ) {
  41. $res[$grant] = array_keys( array_filter( $rights ) );
  42. }
  43. return $res;
  44. }
  45. /**
  46. * Fetch the display name of the grant
  47. * @param string $grant
  48. * @param Language|string|null $lang
  49. * @return string Grant description
  50. */
  51. public static function grantName( $grant, $lang = null ) {
  52. // Give grep a chance to find the usages:
  53. // grant-blockusers, grant-createeditmovepage, grant-delete,
  54. // grant-editinterface, grant-editmycssjs, grant-editmywatchlist,
  55. // grant-editsiteconfig, grant-editpage, grant-editprotected,
  56. // grant-highvolume, grant-oversight, grant-patrol, grant-protect,
  57. // grant-rollback, grant-sendemail, grant-uploadeditmovefile,
  58. // grant-uploadfile, grant-basic, grant-viewdeleted,
  59. // grant-viewmywatchlist, grant-createaccount
  60. $msg = wfMessage( "grant-$grant" );
  61. if ( $lang !== null ) {
  62. if ( is_string( $lang ) ) {
  63. $lang = Language::factory( $lang );
  64. }
  65. $msg->inLanguage( $lang );
  66. }
  67. if ( !$msg->exists() ) {
  68. $msg = wfMessage( 'grant-generic', $grant );
  69. if ( $lang ) {
  70. $msg->inLanguage( $lang );
  71. }
  72. }
  73. return $msg->text();
  74. }
  75. /**
  76. * Fetch the display names for the grants.
  77. * @param string[] $grants
  78. * @param Language|string|null $lang
  79. * @return string[] Corresponding grant descriptions
  80. */
  81. public static function grantNames( array $grants, $lang = null ) {
  82. if ( $lang !== null && is_string( $lang ) ) {
  83. $lang = Language::factory( $lang );
  84. }
  85. $ret = [];
  86. foreach ( $grants as $grant ) {
  87. $ret[] = self::grantName( $grant, $lang );
  88. }
  89. return $ret;
  90. }
  91. /**
  92. * Fetch the rights allowed by a set of grants.
  93. * @param string[]|string $grants
  94. * @return string[]
  95. */
  96. public static function getGrantRights( $grants ) {
  97. global $wgGrantPermissions;
  98. $rights = [];
  99. foreach ( (array)$grants as $grant ) {
  100. if ( isset( $wgGrantPermissions[$grant] ) ) {
  101. $rights = array_merge( $rights, array_keys( array_filter( $wgGrantPermissions[$grant] ) ) );
  102. }
  103. }
  104. return array_unique( $rights );
  105. }
  106. /**
  107. * Test that all grants in the list are known.
  108. * @param string[] $grants
  109. * @return bool
  110. */
  111. public static function grantsAreValid( array $grants ) {
  112. return array_diff( $grants, self::getValidGrants() ) === [];
  113. }
  114. /**
  115. * Divide the grants into groups.
  116. * @param string[]|null $grantsFilter
  117. * @return array Map of (group => (grant list))
  118. */
  119. public static function getGrantGroups( $grantsFilter = null ) {
  120. global $wgGrantPermissions, $wgGrantPermissionGroups;
  121. if ( is_array( $grantsFilter ) ) {
  122. $grantsFilter = array_flip( $grantsFilter );
  123. }
  124. $groups = [];
  125. foreach ( $wgGrantPermissions as $grant => $rights ) {
  126. if ( $grantsFilter !== null && !isset( $grantsFilter[$grant] ) ) {
  127. continue;
  128. }
  129. if ( isset( $wgGrantPermissionGroups[$grant] ) ) {
  130. $groups[$wgGrantPermissionGroups[$grant]][] = $grant;
  131. } else {
  132. $groups['other'][] = $grant;
  133. }
  134. }
  135. return $groups;
  136. }
  137. /**
  138. * Get the list of grants that are hidden and should always be granted
  139. * @return string[]
  140. */
  141. public static function getHiddenGrants() {
  142. global $wgGrantPermissionGroups;
  143. $grants = [];
  144. foreach ( $wgGrantPermissionGroups as $grant => $group ) {
  145. if ( $group === 'hidden' ) {
  146. $grants[] = $grant;
  147. }
  148. }
  149. return $grants;
  150. }
  151. /**
  152. * Generate a link to Special:ListGrants for a particular grant name.
  153. *
  154. * This should be used to link end users to a full description of what
  155. * rights they are giving when they authorize a grant.
  156. *
  157. * @param string $grant the grant name
  158. * @param Language|string|null $lang
  159. * @return string (proto-relative) HTML link
  160. */
  161. public static function getGrantsLink( $grant, $lang = null ) {
  162. $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
  163. return $linkRenderer->makeKnownLink(
  164. \SpecialPage::getTitleFor( 'Listgrants', false, $grant ),
  165. self::grantName( $grant, $lang )
  166. );
  167. }
  168. /**
  169. * Generate wikitext to display a list of grants
  170. * @param string[]|null $grantsFilter If non-null, only display these grants.
  171. * @param Language|string|null $lang
  172. * @return string Wikitext
  173. */
  174. public static function getGrantsWikiText( $grantsFilter, $lang = null ) {
  175. if ( is_string( $lang ) ) {
  176. $lang = Language::factory( $lang );
  177. } elseif ( $lang === null ) {
  178. $lang = MediaWikiServices::getInstance()->getContentLanguage();
  179. }
  180. $s = '';
  181. foreach ( self::getGrantGroups( $grantsFilter ) as $group => $grants ) {
  182. if ( $group === 'hidden' ) {
  183. continue; // implicitly granted
  184. }
  185. $s .= "*<span class=\"mw-grantgroup\">" .
  186. wfMessage( "grant-group-$group" )->inLanguage( $lang )->text() . "</span>\n";
  187. $s .= ":" . $lang->semicolonList( self::grantNames( $grants, $lang ) ) . "\n";
  188. }
  189. return "$s\n";
  190. }
  191. }