MediaWiki master
GrantsLocalization.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Permissions;
22
23use HtmlArmor;
29
40 private GrantsInfo $grantsInfo;
41 private LinkRenderer $linkRenderer;
42 private LanguageFactory $languageFactory;
43 private Language $contentLanguage;
44
45 public function __construct(
46 GrantsInfo $grantsInfo,
47 LinkRenderer $linkRenderer,
48 LanguageFactory $languageFactory,
49 Language $contentLanguage
50 ) {
51 $this->grantsInfo = $grantsInfo;
52 $this->linkRenderer = $linkRenderer;
53 $this->languageFactory = $languageFactory;
54 $this->contentLanguage = $contentLanguage;
55 }
56
63 public function getGrantDescription( string $grant, $lang = null ): string {
64 // Give grep a chance to find the usages:
65 // grant-blockusers, grant-createeditmovepage, grant-delete,
66 // grant-editinterface, grant-editmycssjs, grant-editmywatchlist,
67 // grant-editsiteconfig, grant-editpage, grant-editprotected,
68 // grant-highvolume, grant-oversight, grant-patrol, grant-protect,
69 // grant-rollback, grant-sendemail, grant-uploadeditmovefile,
70 // grant-uploadfile, grant-basic, grant-viewdeleted,
71 // grant-viewmywatchlist, grant-createaccount, grant-mergehistory,
72 // grant-import
73
74 // TODO: replace wfMessage with something that can be injected like TextFormatter
75 $msg = wfMessage( "grant-$grant" );
76
77 if ( $lang ) {
78 $msg->inLanguage( $lang );
79 }
80
81 if ( !$msg->exists() ) {
82 $msg = $lang
83 ? wfMessage( 'grant-generic', $grant )->inLanguage( $lang )
84 : wfMessage( 'grant-generic', $grant );
85 }
86
87 return $msg->text();
88 }
89
96 public function getGrantDescriptions( array $grants, $lang = null ): array {
97 $ret = [];
98
99 foreach ( $grants as $grant ) {
100 $ret[$grant] = $this->getGrantDescription( $grant, $lang );
101 }
102 return $ret;
103 }
104
112 public function getGrantDescriptionsWithClasses( array $grants, $lang = null ): array {
113 $riskGroupsByGrant = $this->grantsInfo->getRiskGroupsByGrant( 'unknown' );
114 $grantDescriptions = $this->getGrantDescriptions( $grants, $lang );
115 $results = [];
116 foreach ( $grantDescriptions as $grant => $description ) {
117 $riskGroup = $riskGroupsByGrant[$grant] ?? 'unknown';
118 // Messages used here: grantriskgroup-vandalism, grantriskgroup-security,
119 // grantriskgroup-internal
120 $riskGroupMsg = wfMessage( "grantriskgroup-$riskGroup" );
121 if ( $lang ) {
122 $riskGroupMsg->inLanguage( $lang );
123 }
124 if ( $riskGroupMsg->exists() ) {
125 $riskDescription = $riskGroupMsg->text();
126 } else {
127 $riskDescription = '';
128 }
129 $results[] = htmlspecialchars( $description ) . ' ' .
130 Html::element( 'span', [ 'class' => "mw-grant mw-grantriskgroup-$riskGroup" ], $riskDescription );
131 }
132 return $results;
133 }
134
145 public function getGrantsLink( string $grant, $lang = null ): string {
146 $riskGroupsByGrant = $this->grantsInfo->getRiskGroupsByGrant( 'unknown' );
147 $riskGroup = $riskGroupsByGrant[$grant] ?? 'unknown';
148 return $this->linkRenderer->makeKnownLink(
149 SpecialPage::getTitleFor( 'Listgrants', false, $grant ),
150 new HtmlArmor( $this->getGrantDescriptionsWithClasses( [ $grant ], $lang )[ 0 ] )
151 );
152 }
153
164 public function getGrantsWikiText( $grantsFilter, $lang = null ): string {
165 if ( is_string( $lang ) ) {
166 $lang = $this->languageFactory->getLanguage( $lang );
167 } elseif ( $lang === null ) {
168 $lang = $this->contentLanguage;
169 }
170
171 $s = '';
172 foreach ( $this->grantsInfo->getGrantGroups( $grantsFilter ) as $group => $grants ) {
173 if ( $group === 'hidden' ) {
174 continue; // implicitly granted
175 }
176 $grantDescriptionsWithClasses = $this->getGrantDescriptionsWithClasses( $grants, $lang );
177 // Give grep a chance to find the usages:
178 // grant-group-page-interaction, grant-group-file-interaction
179 // grant-group-watchlist-interaction, grant-group-email,
180 // grant-group-high-volume, grant-group-customization,
181 // grant-group-administration, grant-group-private-information,
182 // grant-group-other
183 $s .= "*<span class=\"mw-grantgroup\">" .
184 // TODO: replace wfMessage with something that can be injected like TextFormatter
185 wfMessage( "grant-group-$group" )->inLanguage( $lang )->text() . "</span>\n";
186 $s .= ":" . $lang->semicolonList( $grantDescriptionsWithClasses ) . "\n";
187 }
188 return "$s\n";
189 }
190}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
Marks HTML that shouldn't be escaped.
Definition HtmlArmor.php:30
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
Base class for language-specific code.
Definition Language.php:78
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Class that generates HTML for internal links.
Users can authorize applications to use their account via OAuth.
This separate service is needed because the ::getGrantsLink method requires a LinkRenderer and if we ...
__construct(GrantsInfo $grantsInfo, LinkRenderer $linkRenderer, LanguageFactory $languageFactory, Language $contentLanguage)
getGrantsLink(string $grant, $lang=null)
Generate a link to Special:ListGrants for a particular grant name.
getGrantDescription(string $grant, $lang=null)
Fetch the description of the grant.
getGrantDescriptionsWithClasses(array $grants, $lang=null)
Fetch the descriptions for the grants, like getGrantDescriptions, but with HTML classes for styling.
getGrantsWikiText( $grantsFilter, $lang=null)
Generate wikitext to display a list of grants.
getGrantDescriptions(array $grants, $lang=null)
Fetch the descriptions for the grants.
Parent class for all special pages.