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