Merge "HTML escape parameter 'text' of hook 'SkinEditSectionLinks'"
[lhc/web/wiklou.git] / includes / specials / SpecialListGroupRights.php
1 <?php
2 /**
3 * Implements Special:Listgrouprights
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 use MediaWiki\MediaWikiServices;
25
26 /**
27 * This special page lists all defined user groups and the associated rights.
28 * See also @ref $wgGroupPermissions.
29 *
30 * @ingroup SpecialPage
31 * @author Petr Kadlec <mormegil@centrum.cz>
32 */
33 class SpecialListGroupRights extends SpecialPage {
34 public function __construct() {
35 parent::__construct( 'Listgrouprights' );
36 }
37
38 /**
39 * Show the special page
40 * @param string|null $par
41 */
42 public function execute( $par ) {
43 $this->setHeaders();
44 $this->outputHeader();
45
46 $out = $this->getOutput();
47 $out->addModuleStyles( 'mediawiki.special' );
48
49 $out->wrapWikiMsg( "<div class=\"mw-listgrouprights-key\">\n$1\n</div>", 'listgrouprights-key' );
50
51 $out->addHTML(
52 Xml::openElement( 'table', [ 'class' => 'wikitable mw-listgrouprights-table' ] ) .
53 '<tr>' .
54 Xml::element( 'th', null, $this->msg( 'listgrouprights-group' )->text() ) .
55 Xml::element( 'th', null, $this->msg( 'listgrouprights-rights' )->text() ) .
56 '</tr>'
57 );
58
59 $config = $this->getConfig();
60 $groupPermissions = $config->get( 'GroupPermissions' );
61 $revokePermissions = $config->get( 'RevokePermissions' );
62 $addGroups = $config->get( 'AddGroups' );
63 $removeGroups = $config->get( 'RemoveGroups' );
64 $groupsAddToSelf = $config->get( 'GroupsAddToSelf' );
65 $groupsRemoveFromSelf = $config->get( 'GroupsRemoveFromSelf' );
66 $allGroups = array_unique( array_merge(
67 array_keys( $groupPermissions ),
68 array_keys( $revokePermissions ),
69 array_keys( $addGroups ),
70 array_keys( $removeGroups ),
71 array_keys( $groupsAddToSelf ),
72 array_keys( $groupsRemoveFromSelf )
73 ) );
74 asort( $allGroups );
75
76 $linkRenderer = $this->getLinkRenderer();
77
78 foreach ( $allGroups as $group ) {
79 $permissions = $groupPermissions[$group] ?? [];
80 $groupname = ( $group == '*' ) // Replace * with a more descriptive groupname
81 ? 'all'
82 : $group;
83
84 $groupnameLocalized = UserGroupMembership::getGroupName( $groupname );
85
86 $grouppageLocalizedTitle = UserGroupMembership::getGroupPage( $groupname )
87 ?: Title::newFromText( MediaWikiServices::getInstance()->getNamespaceInfo()->
88 getCanonicalName( NS_PROJECT ) . ':' . $groupname );
89
90 if ( $group == '*' || !$grouppageLocalizedTitle ) {
91 // Do not make a link for the generic * group or group with invalid group page
92 $grouppage = htmlspecialchars( $groupnameLocalized );
93 } else {
94 $grouppage = $linkRenderer->makeLink(
95 $grouppageLocalizedTitle,
96 $groupnameLocalized
97 );
98 }
99
100 if ( $group === 'user' ) {
101 // Link to Special:listusers for implicit group 'user'
102 $grouplink = '<br />' . $linkRenderer->makeKnownLink(
103 SpecialPage::getTitleFor( 'Listusers' ),
104 $this->msg( 'listgrouprights-members' )->text()
105 );
106 } elseif ( !in_array( $group, $config->get( 'ImplicitGroups' ) ) ) {
107 $grouplink = '<br />' . $linkRenderer->makeKnownLink(
108 SpecialPage::getTitleFor( 'Listusers' ),
109 $this->msg( 'listgrouprights-members' )->text(),
110 [],
111 [ 'group' => $group ]
112 );
113 } else {
114 // No link to Special:listusers for other implicit groups as they are unlistable
115 $grouplink = '';
116 }
117
118 $revoke = $revokePermissions[$group] ?? [];
119 $addgroups = $addGroups[$group] ?? [];
120 $removegroups = $removeGroups[$group] ?? [];
121 $addgroupsSelf = $groupsAddToSelf[$group] ?? [];
122 $removegroupsSelf = $groupsRemoveFromSelf[$group] ?? [];
123
124 $id = $group == '*' ? false : Sanitizer::escapeIdForAttribute( $group );
125 $out->addHTML( Html::rawElement( 'tr', [ 'id' => $id ], "
126 <td>$grouppage$grouplink</td>
127 <td>" .
128 $this->formatPermissions( $permissions, $revoke, $addgroups, $removegroups,
129 $addgroupsSelf, $removegroupsSelf ) .
130 '</td>
131 '
132 ) );
133 }
134 $out->addHTML( Xml::closeElement( 'table' ) );
135 $this->outputNamespaceProtectionInfo();
136 }
137
138 private function outputNamespaceProtectionInfo() {
139 $out = $this->getOutput();
140 $namespaceProtection = $this->getConfig()->get( 'NamespaceProtection' );
141
142 if ( count( $namespaceProtection ) == 0 ) {
143 return;
144 }
145
146 $header = $this->msg( 'listgrouprights-namespaceprotection-header' )->text();
147 $out->addHTML(
148 Html::rawElement( 'h2', [], Html::element( 'span', [
149 'class' => 'mw-headline',
150 'id' => substr( Parser::guessSectionNameFromStrippedText( $header ), 1 )
151 ], $header ) ) .
152 Xml::openElement( 'table', [ 'class' => 'wikitable' ] ) .
153 Html::element(
154 'th',
155 [],
156 $this->msg( 'listgrouprights-namespaceprotection-namespace' )->text()
157 ) .
158 Html::element(
159 'th',
160 [],
161 $this->msg( 'listgrouprights-namespaceprotection-restrictedto' )->text()
162 )
163 );
164 $linkRenderer = $this->getLinkRenderer();
165 ksort( $namespaceProtection );
166 $validNamespaces =
167 MediaWikiServices::getInstance()->getNamespaceInfo()->getValidNamespaces();
168 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
169 foreach ( $namespaceProtection as $namespace => $rights ) {
170 if ( !in_array( $namespace, $validNamespaces ) ) {
171 continue;
172 }
173
174 if ( $namespace == NS_MAIN ) {
175 $namespaceText = $this->msg( 'blanknamespace' )->text();
176 } else {
177 $namespaceText = $contLang->convertNamespace( $namespace );
178 }
179
180 $out->addHTML(
181 Xml::openElement( 'tr' ) .
182 Html::rawElement(
183 'td',
184 [],
185 $linkRenderer->makeLink(
186 SpecialPage::getTitleFor( 'Allpages' ),
187 $namespaceText,
188 [],
189 [ 'namespace' => $namespace ]
190 )
191 ) .
192 Xml::openElement( 'td' ) . Xml::openElement( 'ul' )
193 );
194
195 if ( !is_array( $rights ) ) {
196 $rights = [ $rights ];
197 }
198
199 foreach ( $rights as $right ) {
200 $out->addHTML(
201 Html::rawElement( 'li', [], $this->msg(
202 'listgrouprights-right-display',
203 User::getRightDescription( $right ),
204 Html::element(
205 'span',
206 [ 'class' => 'mw-listgrouprights-right-name' ],
207 $right
208 )
209 )->parse() )
210 );
211 }
212
213 $out->addHTML(
214 Xml::closeElement( 'ul' ) .
215 Xml::closeElement( 'td' ) .
216 Xml::closeElement( 'tr' )
217 );
218 }
219 $out->addHTML( Xml::closeElement( 'table' ) );
220 }
221
222 /**
223 * Create a user-readable list of permissions from the given array.
224 *
225 * @param array $permissions Array of permission => bool (from $wgGroupPermissions items)
226 * @param array $revoke Array of permission => bool (from $wgRevokePermissions items)
227 * @param array $add Array of groups this group is allowed to add or true
228 * @param array $remove Array of groups this group is allowed to remove or true
229 * @param array $addSelf Array of groups this group is allowed to add to self or true
230 * @param array $removeSelf Array of group this group is allowed to remove from self or true
231 * @return string HTML list of all granted permissions
232 */
233 private function formatPermissions( $permissions, $revoke, $add, $remove, $addSelf, $removeSelf ) {
234 $r = [];
235 foreach ( $permissions as $permission => $granted ) {
236 // show as granted only if it isn't revoked to prevent duplicate display of permissions
237 if ( $granted && ( !isset( $revoke[$permission] ) || !$revoke[$permission] ) ) {
238 $r[] = $this->msg( 'listgrouprights-right-display',
239 User::getRightDescription( $permission ),
240 '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
241 )->parse();
242 }
243 }
244 foreach ( $revoke as $permission => $revoked ) {
245 if ( $revoked ) {
246 $r[] = $this->msg( 'listgrouprights-right-revoked',
247 User::getRightDescription( $permission ),
248 '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
249 )->parse();
250 }
251 }
252
253 sort( $r );
254
255 $lang = $this->getLanguage();
256 $allGroups = User::getAllGroups();
257
258 $changeGroups = [
259 'addgroup' => $add,
260 'removegroup' => $remove,
261 'addgroup-self' => $addSelf,
262 'removegroup-self' => $removeSelf
263 ];
264
265 foreach ( $changeGroups as $messageKey => $changeGroup ) {
266 if ( $changeGroup === true ) {
267 // For grep: listgrouprights-addgroup-all, listgrouprights-removegroup-all,
268 // listgrouprights-addgroup-self-all, listgrouprights-removegroup-self-all
269 $r[] = $this->msg( 'listgrouprights-' . $messageKey . '-all' )->escaped();
270 } elseif ( is_array( $changeGroup ) ) {
271 $changeGroup = array_intersect( array_values( array_unique( $changeGroup ) ), $allGroups );
272 if ( count( $changeGroup ) ) {
273 $groupLinks = [];
274 foreach ( $changeGroup as $group ) {
275 $groupLinks[] = UserGroupMembership::getLink( $group, $this->getContext(), 'wiki' );
276 }
277 // For grep: listgrouprights-addgroup, listgrouprights-removegroup,
278 // listgrouprights-addgroup-self, listgrouprights-removegroup-self
279 $r[] = $this->msg( 'listgrouprights-' . $messageKey,
280 $lang->listToText( $groupLinks ), count( $changeGroup ) )->parse();
281 }
282 }
283 }
284
285 if ( empty( $r ) ) {
286 return '';
287 } else {
288 return '<ul><li>' . implode( "</li>\n<li>", $r ) . '</li></ul>';
289 }
290 }
291
292 protected function getGroupName() {
293 return 'users';
294 }
295 }