Debian Codegraph @

includes/Permissions/GrantsLocalization.php

raw symbols SCIP
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
21 namespace MediaWiki\Permissions;
22
23 use HtmlArmor;
24 use MediaWiki\Html\Html;
25 use MediaWiki\Language\Language;
26 use MediaWiki\Languages\LanguageFactory;
27 use MediaWiki\Linker\LinkRenderer;
28 use MediaWiki\SpecialPage\SpecialPage;
29
30 /**
31 * This separate service is needed because the ::getGrantsLink method requires a LinkRenderer
32 * and if we tried to inject a LinkRenderer into the GrantsInfo service, it would result in
33 * recursive service instantiation for sessions using the BotPasswordSessionProvider, as a
34 * result of injecting the LinkRenderer when trying to use a GrantsInfo method that doesn't
35 * even need it.
36 *
37 * @since 1.38
38 */
39 class GrantsLocalization {
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
57 /**
58 * Fetch the description of the grant.
59 * @param string $grant
60 * @param Language|string|null $lang
61 * @return string Grant description
62 */
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
90 /**
91 * Fetch the descriptions for the grants.
92 * @param string[] $grants
93 * @param Language|string|null $lang
94 * @return string[] Corresponding grant descriptions, keyed by grant name
95 */
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
105 /**
106 * Fetch the descriptions for the grants, like getGrantDescriptions, but with HTML classes
107 * for styling. The HTML is wikitext-compatible.
108 * @param string[] $grants
109 * @param Language|string|null $lang
110 * @return string[] Grant description HTML for each grant, in the same order
111 */
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
135 /**
136 * Generate a link to Special:ListGrants for a particular grant name.
137 *
138 * This can be used to link end users to a full description of what
139 * rights they are giving when they authorize a grant.
140 *
141 * @param string $grant the grant name
142 * @param Language|string|null $lang
143 * @return string (proto-relative) HTML link
144 */
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
154 /**
155 * Generate wikitext to display a list of grants. It will be in the format
156 * * <grant-group-$group>
157 * : <grant-$grant>; <grant-$grant>; ...
158 * * ...
159 * with some HTML classes for styling.
160 * @param string[]|null $grantsFilter If non-null, only display these grants.
161 * @param Language|string|null $lang
162 * @return string Wikitext
163 */
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 }