MediaWiki master
GrantsLocalization.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Permissions;
22
23use HtmlArmor;
24use Language;
29
41 private $grantsInfo;
42
44 private $linkRenderer;
45
47 private $languageFactory;
48
50 private $contentLanguage;
51
58 public function __construct(
59 GrantsInfo $grantsInfo,
60 LinkRenderer $linkRenderer,
61 LanguageFactory $languageFactory,
62 Language $contentLanguage
63 ) {
64 $this->grantsInfo = $grantsInfo;
65 $this->linkRenderer = $linkRenderer;
66 $this->languageFactory = $languageFactory;
67 $this->contentLanguage = $contentLanguage;
68 }
69
76 public function getGrantDescription( string $grant, $lang = null ): string {
77 // Give grep a chance to find the usages:
78 // grant-blockusers, grant-createeditmovepage, grant-delete,
79 // grant-editinterface, grant-editmycssjs, grant-editmywatchlist,
80 // grant-editsiteconfig, grant-editpage, grant-editprotected,
81 // grant-highvolume, grant-oversight, grant-patrol, grant-protect,
82 // grant-rollback, grant-sendemail, grant-uploadeditmovefile,
83 // grant-uploadfile, grant-basic, grant-viewdeleted,
84 // grant-viewmywatchlist, grant-createaccount, grant-mergehistory,
85 // grant-import
86
87 // TODO: replace wfMessage with something that can be injected like TextFormatter
88 $msg = wfMessage( "grant-$grant" );
89
90 if ( $lang ) {
91 $msg->inLanguage( $lang );
92 }
93
94 if ( !$msg->exists() ) {
95 $msg = $lang
96 ? wfMessage( 'grant-generic', $grant )->inLanguage( $lang )
97 : wfMessage( 'grant-generic', $grant );
98 }
99
100 return $msg->text();
101 }
102
109 public function getGrantDescriptions( array $grants, $lang = null ): array {
110 $ret = [];
111
112 foreach ( $grants as $grant ) {
113 $ret[$grant] = $this->getGrantDescription( $grant, $lang );
114 }
115 return $ret;
116 }
117
125 public function getGrantDescriptionsWithClasses( array $grants, $lang = null ): array {
126 $riskGroupsByGrant = $this->grantsInfo->getRiskGroupsByGrant( 'unknown' );
127 $grantDescriptions = $this->getGrantDescriptions( $grants, $lang );
128 $results = [];
129 foreach ( $grantDescriptions as $grant => $description ) {
130 $riskGroup = $riskGroupsByGrant[$grant] ?? 'unknown';
131 // Messages used here: grantriskgroup-vandalism, grantriskgroup-security,
132 // grantriskgroup-internal
133 $riskGroupMsg = wfMessage( "grantriskgroup-$riskGroup" );
134 if ( $lang ) {
135 $riskGroupMsg->inLanguage( $lang );
136 }
137 if ( $riskGroupMsg->exists() ) {
138 $riskDescription = $riskGroupMsg->text();
139 } else {
140 $riskDescription = '';
141 }
142 $results[] = htmlspecialchars( $description ) . ' ' .
143 Html::element( 'span', [ 'class' => "mw-grant mw-grantriskgroup-$riskGroup" ], $riskDescription );
144 }
145 return $results;
146 }
147
158 public function getGrantsLink( string $grant, $lang = null ): string {
159 $riskGroupsByGrant = $this->grantsInfo->getRiskGroupsByGrant( 'unknown' );
160 $riskGroup = $riskGroupsByGrant[$grant] ?? 'unknown';
161 return $this->linkRenderer->makeKnownLink(
162 SpecialPage::getTitleFor( 'Listgrants', false, $grant ),
163 new HtmlArmor( $this->getGrantDescriptionsWithClasses( [ $grant ], $lang )[ 0 ] )
164 );
165 }
166
177 public function getGrantsWikiText( $grantsFilter, $lang = null ): string {
178 if ( is_string( $lang ) ) {
179 $lang = $this->languageFactory->getLanguage( $lang );
180 } elseif ( $lang === null ) {
181 $lang = $this->contentLanguage;
182 }
183
184 $s = '';
185 foreach ( $this->grantsInfo->getGrantGroups( $grantsFilter ) as $group => $grants ) {
186 if ( $group === 'hidden' ) {
187 continue; // implicitly granted
188 }
189 $grantDescriptionsWithClasses = $this->getGrantDescriptionsWithClasses( $grants, $lang );
190 // Give grep a chance to find the usages:
191 // grant-group-page-interaction, grant-group-file-interaction
192 // grant-group-watchlist-interaction, grant-group-email,
193 // grant-group-high-volume, grant-group-customization,
194 // grant-group-administration, grant-group-private-information,
195 // grant-group-other
196 $s .= "*<span class=\"mw-grantgroup\">" .
197 // TODO: replace wfMessage with something that can be injected like TextFormatter
198 wfMessage( "grant-group-$group" )->inLanguage( $lang )->text() . "</span>\n";
199 $s .= ":" . $lang->semicolonList( $grantDescriptionsWithClasses ) . "\n";
200 }
201 return "$s\n";
202 }
203}
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
Base class for language-specific code.
Definition Language.php:63
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
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.