Merge "Title: Title::getSubpage should not lose the interwiki prefix"
[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 $this->addHelpLink( 'Help:User_rights_and_groups' );
49
50 $out->wrapWikiMsg( "<div class=\"mw-listgrouprights-key\">\n$1\n</div>", 'listgrouprights-key' );
51
52 $out->addHTML(
53 Xml::openElement( 'table', [ 'class' => 'wikitable mw-listgrouprights-table' ] ) .
54 '<tr>' .
55 Xml::element( 'th', null, $this->msg( 'listgrouprights-group' )->text() ) .
56 Xml::element( 'th', null, $this->msg( 'listgrouprights-rights' )->text() ) .
57 '</tr>'
58 );
59
60 $config = $this->getConfig();
61 $groupPermissions = $config->get( 'GroupPermissions' );
62 $revokePermissions = $config->get( 'RevokePermissions' );
63 $addGroups = $config->get( 'AddGroups' );
64 $removeGroups = $config->get( 'RemoveGroups' );
65 $groupsAddToSelf = $config->get( 'GroupsAddToSelf' );
66 $groupsRemoveFromSelf = $config->get( 'GroupsRemoveFromSelf' );
67 $allGroups = array_unique( array_merge(
68 array_keys( $groupPermissions ),
69 array_keys( $revokePermissions ),
70 array_keys( $addGroups ),
71 array_keys( $removeGroups ),
72 array_keys( $groupsAddToSelf ),
73 array_keys( $groupsRemoveFromSelf )
74 ) );
75 asort( $allGroups );
76
77 $linkRenderer = $this->getLinkRenderer();
78
79 foreach ( $allGroups as $group ) {
80 $permissions = $groupPermissions[$group] ?? [];
81 $groupname = ( $group == '*' ) // Replace * with a more descriptive groupname
82 ? 'all'
83 : $group;
84
85 $groupnameLocalized = UserGroupMembership::getGroupName( $groupname );
86
87 $grouppageLocalizedTitle = UserGroupMembership::getGroupPage( $groupname )
88 ?: Title::newFromText( MediaWikiServices::getInstance()->getNamespaceInfo()->
89 getCanonicalName( NS_PROJECT ) . ':' . $groupname );
90
91 if ( $group == '*' || !$grouppageLocalizedTitle ) {
92 // Do not make a link for the generic * group or group with invalid group page
93 $grouppage = htmlspecialchars( $groupnameLocalized );
94 } else {
95 $grouppage = $linkRenderer->makeLink(
96 $grouppageLocalizedTitle,
97 $groupnameLocalized
98 );
99 }
100
101 if ( $group === 'user' ) {
102 // Link to Special:listusers for implicit group 'user'
103 $grouplink = '<br />' . $linkRenderer->makeKnownLink(
104 SpecialPage::getTitleFor( 'Listusers' ),
105 $this->msg( 'listgrouprights-members' )->text()
106 );
107 } elseif ( !in_array( $group, $config->get( 'ImplicitGroups' ) ) ) {
108 $grouplink = '<br />' . $linkRenderer->makeKnownLink(
109 SpecialPage::getTitleFor( 'Listusers' ),
110 $this->msg( 'listgrouprights-members' )->text(),
111 [],
112 [ 'group' => $group ]
113 );
114 } else {
115 // No link to Special:listusers for other implicit groups as they are unlistable
116 $grouplink = '';
117 }
118
119 $revoke = $revokePermissions[$group] ?? [];
120 $addgroups = $addGroups[$group] ?? [];
121 $removegroups = $removeGroups[$group] ?? [];
122 $addgroupsSelf = $groupsAddToSelf[$group] ?? [];
123 $removegroupsSelf = $groupsRemoveFromSelf[$group] ?? [];
124
125 $id = $group == '*' ? false : Sanitizer::escapeIdForAttribute( $group );
126 $out->addHTML( Html::rawElement( 'tr', [ 'id' => $id ], "
127 <td>$grouppage$grouplink</td>
128 <td>" .
129 $this->formatPermissions( $permissions, $revoke, $addgroups, $removegroups,
130 $addgroupsSelf, $removegroupsSelf ) .
131 '</td>
132 '
133 ) );
134 }
135 $out->addHTML( Xml::closeElement( 'table' ) );
136 $this->outputNamespaceProtectionInfo();
137 }
138
139 private function outputNamespaceProtectionInfo() {
140 $out = $this->getOutput();
141 $namespaceProtection = $this->getConfig()->get( 'NamespaceProtection' );
142
143 if ( count( $namespaceProtection ) == 0 ) {
144 return;
145 }
146
147 $header = $this->msg( 'listgrouprights-namespaceprotection-header' )->text();
148 $out->addHTML(
149 Html::rawElement( 'h2', [], Html::element( 'span', [
150 'class' => 'mw-headline',
151 'id' => substr( Parser::guessSectionNameFromStrippedText( $header ), 1 )
152 ], $header ) ) .
153 Xml::openElement( 'table', [ 'class' => 'wikitable' ] ) .
154 Html::element(
155 'th',
156 [],
157 $this->msg( 'listgrouprights-namespaceprotection-namespace' )->text()
158 ) .
159 Html::element(
160 'th',
161 [],
162 $this->msg( 'listgrouprights-namespaceprotection-restrictedto' )->text()
163 )
164 );
165 $linkRenderer = $this->getLinkRenderer();
166 ksort( $namespaceProtection );
167 $validNamespaces =
168 MediaWikiServices::getInstance()->getNamespaceInfo()->getValidNamespaces();
169 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
170 foreach ( $namespaceProtection as $namespace => $rights ) {
171 if ( !in_array( $namespace, $validNamespaces ) ) {
172 continue;
173 }
174
175 if ( $namespace == NS_MAIN ) {
176 $namespaceText = $this->msg( 'blanknamespace' )->text();
177 } else {
178 $namespaceText = $contLang->convertNamespace( $namespace );
179 }
180
181 $out->addHTML(
182 Xml::openElement( 'tr' ) .
183 Html::rawElement(
184 'td',
185 [],
186 $linkRenderer->makeLink(
187 SpecialPage::getTitleFor( 'Allpages' ),
188 $namespaceText,
189 [],
190 [ 'namespace' => $namespace ]
191 )
192 ) .
193 Xml::openElement( 'td' ) . Xml::openElement( 'ul' )
194 );
195
196 if ( !is_array( $rights ) ) {
197 $rights = [ $rights ];
198 }
199
200 foreach ( $rights as $right ) {
201 $out->addHTML(
202 Html::rawElement( 'li', [], $this->msg(
203 'listgrouprights-right-display',
204 User::getRightDescription( $right ),
205 Html::element(
206 'span',
207 [ 'class' => 'mw-listgrouprights-right-name' ],
208 $right
209 )
210 )->parse() )
211 );
212 }
213
214 $out->addHTML(
215 Xml::closeElement( 'ul' ) .
216 Xml::closeElement( 'td' ) .
217 Xml::closeElement( 'tr' )
218 );
219 }
220 $out->addHTML( Xml::closeElement( 'table' ) );
221 }
222
223 /**
224 * Create a user-readable list of permissions from the given array.
225 *
226 * @param array $permissions Array of permission => bool (from $wgGroupPermissions items)
227 * @param array $revoke Array of permission => bool (from $wgRevokePermissions items)
228 * @param array $add Array of groups this group is allowed to add or true
229 * @param array $remove Array of groups this group is allowed to remove or true
230 * @param array $addSelf Array of groups this group is allowed to add to self or true
231 * @param array $removeSelf Array of group this group is allowed to remove from self or true
232 * @return string HTML list of all granted permissions
233 */
234 private function formatPermissions( $permissions, $revoke, $add, $remove, $addSelf, $removeSelf ) {
235 $r = [];
236 foreach ( $permissions as $permission => $granted ) {
237 // show as granted only if it isn't revoked to prevent duplicate display of permissions
238 if ( $granted && ( !isset( $revoke[$permission] ) || !$revoke[$permission] ) ) {
239 $r[] = $this->msg( 'listgrouprights-right-display',
240 User::getRightDescription( $permission ),
241 '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
242 )->parse();
243 }
244 }
245 foreach ( $revoke as $permission => $revoked ) {
246 if ( $revoked ) {
247 $r[] = $this->msg( 'listgrouprights-right-revoked',
248 User::getRightDescription( $permission ),
249 '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
250 )->parse();
251 }
252 }
253
254 sort( $r );
255
256 $lang = $this->getLanguage();
257 $allGroups = User::getAllGroups();
258
259 $changeGroups = [
260 'addgroup' => $add,
261 'removegroup' => $remove,
262 'addgroup-self' => $addSelf,
263 'removegroup-self' => $removeSelf
264 ];
265
266 foreach ( $changeGroups as $messageKey => $changeGroup ) {
267 if ( $changeGroup === true ) {
268 // For grep: listgrouprights-addgroup-all, listgrouprights-removegroup-all,
269 // listgrouprights-addgroup-self-all, listgrouprights-removegroup-self-all
270 $r[] = $this->msg( 'listgrouprights-' . $messageKey . '-all' )->escaped();
271 } elseif ( is_array( $changeGroup ) ) {
272 $changeGroup = array_intersect( array_values( array_unique( $changeGroup ) ), $allGroups );
273 if ( count( $changeGroup ) ) {
274 $groupLinks = [];
275 foreach ( $changeGroup as $group ) {
276 $groupLinks[] = UserGroupMembership::getLink( $group, $this->getContext(), 'wiki' );
277 }
278 // For grep: listgrouprights-addgroup, listgrouprights-removegroup,
279 // listgrouprights-addgroup-self, listgrouprights-removegroup-self
280 $r[] = $this->msg( 'listgrouprights-' . $messageKey,
281 $lang->listToText( $groupLinks ), count( $changeGroup ) )->parse();
282 }
283 }
284 }
285
286 if ( empty( $r ) ) {
287 return '';
288 } else {
289 return '<ul><li>' . implode( "</li>\n<li>", $r ) . '</li></ul>';
290 }
291 }
292
293 protected function getGroupName() {
294 return 'users';
295 }
296 }