MediaWiki master
GrantsLocalization.php
Go to the documentation of this file.
1<?php
8
11use MediaWiki\Languages\LanguageFactory;
15
26 private GrantsInfo $grantsInfo;
27 private LinkRenderer $linkRenderer;
28 private LanguageFactory $languageFactory;
29 private Language $contentLanguage;
30
31 public function __construct(
32 GrantsInfo $grantsInfo,
33 LinkRenderer $linkRenderer,
34 LanguageFactory $languageFactory,
35 Language $contentLanguage
36 ) {
37 $this->grantsInfo = $grantsInfo;
38 $this->linkRenderer = $linkRenderer;
39 $this->languageFactory = $languageFactory;
40 $this->contentLanguage = $contentLanguage;
41 }
42
49 public function getGrantDescription( string $grant, $lang = null ): string {
50 // Give grep a chance to find the usages:
51 // grant-blockusers, grant-createeditmovepage, grant-delete,
52 // grant-editinterface, grant-editmycssjs, grant-editmywatchlist,
53 // grant-editsiteconfig, grant-editpage, grant-editprotected,
54 // grant-highvolume, grant-oversight, grant-patrol, grant-protect,
55 // grant-rollback, grant-sendemail, grant-uploadeditmovefile,
56 // grant-uploadfile, grant-basic, grant-viewdeleted,
57 // grant-viewmywatchlist, grant-createaccount, grant-mergehistory,
58 // grant-import
59
60 // TODO: replace wfMessage with something that can be injected like TextFormatter
61 $msg = wfMessage( "grant-$grant" );
62
63 if ( $lang ) {
64 $msg->inLanguage( $lang );
65 }
66
67 if ( !$msg->exists() ) {
68 $msg = $lang
69 ? wfMessage( 'grant-generic', $grant )->inLanguage( $lang )
70 : wfMessage( 'grant-generic', $grant );
71 }
72
73 return $msg->text();
74 }
75
82 public function getGrantDescriptions( array $grants, $lang = null ): array {
83 $ret = [];
84
85 foreach ( $grants as $grant ) {
86 $ret[$grant] = $this->getGrantDescription( $grant, $lang );
87 }
88 return $ret;
89 }
90
98 public function getGrantDescriptionsWithClasses( array $grants, $lang = null ): array {
99 $riskGroupsByGrant = $this->grantsInfo->getRiskGroupsByGrant( 'unknown' );
100 $grantDescriptions = $this->getGrantDescriptions( $grants, $lang );
101 $results = [];
102 foreach ( $grantDescriptions as $grant => $description ) {
103 $riskGroup = $riskGroupsByGrant[$grant] ?? 'unknown';
104 // Messages used here: grantriskgroup-vandalism, grantriskgroup-security,
105 // grantriskgroup-internal
106 $riskGroupMsg = wfMessage( "grantriskgroup-$riskGroup" );
107 if ( $lang ) {
108 $riskGroupMsg->inLanguage( $lang );
109 }
110 if ( $riskGroupMsg->exists() ) {
111 $riskDescription = $riskGroupMsg->text();
112 $riskDescriptionHTML = ' ' .
113 Html::element( 'span', [ 'class' => "mw-grant mw-grantriskgroup-$riskGroup" ], $riskDescription );
114 } else {
115 $riskDescription = '';
116 $riskDescriptionHTML = '';
117 }
118 $results[] = htmlspecialchars( $description ) . $riskDescriptionHTML;
119 }
120 return $results;
121 }
122
133 public function getGrantsLink( string $grant, $lang = null ): string {
134 $riskGroupsByGrant = $this->grantsInfo->getRiskGroupsByGrant( 'unknown' );
135 $riskGroup = $riskGroupsByGrant[$grant] ?? 'unknown';
136 return $this->linkRenderer->makeKnownLink(
137 SpecialPage::getTitleFor( 'Listgrants', false, $grant ),
138 new HtmlArmor( $this->getGrantDescriptionsWithClasses( [ $grant ], $lang )[ 0 ] )
139 );
140 }
141
154 public function getGrantsWikiText( $grantsFilter, $lang = null ): string {
155 if ( is_string( $lang ) ) {
156 $lang = $this->languageFactory->getLanguage( $lang );
157 } elseif ( $lang === null ) {
158 $lang = $this->contentLanguage;
159 }
160
161 $s = '';
162 foreach ( $this->grantsInfo->getGrantGroups( $grantsFilter ) as $group => $grants ) {
163 if ( $group === 'hidden' ) {
164 continue; // implicitly granted
165 }
166 $grantDescriptionsWithClasses = $this->getGrantDescriptionsWithClasses( $grants, $lang );
167 // Give grep a chance to find the usages:
168 // grant-group-page-interaction, grant-group-file-interaction
169 // grant-group-watchlist-interaction, grant-group-email,
170 // grant-group-high-volume, grant-group-customization,
171 // grant-group-administration, grant-group-private-information,
172 // grant-group-other
173 $s .= "*<span class=\"mw-grantgroup\">" .
174 // TODO: replace wfMessage with something that can be injected like TextFormatter
175 wfMessage( "grant-group-$group" )->inLanguage( $lang )->text() . "</span>\n";
176 $s .= ":" . $lang->semicolonList( $grantDescriptionsWithClasses ) . "\n";
177 }
178 return "$s\n";
179 }
180}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:68
This class is a collection of static functions that serve two purposes:
Definition Html.php:43
Base class for language-specific code.
Definition Language.php:70
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.
Marks HTML that shouldn't be escaped.
Definition HtmlArmor.php:18