Merge "Maintenance: Add an easy way to access Config instances"
[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 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 global $wgImplicitGroups;
42 global $wgGroupPermissions, $wgRevokePermissions, $wgAddGroups, $wgRemoveGroups;
43 global $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
44
45 $this->setHeaders();
46 $this->outputHeader();
47
48 $out = $this->getOutput();
49 $out->addModuleStyles( 'mediawiki.special' );
50
51 $out->wrapWikiMsg( "<div class=\"mw-listgrouprights-key\">\n$1\n</div>", 'listgrouprights-key' );
52
53 $out->addHTML(
54 Xml::openElement( 'table', array( 'class' => 'wikitable mw-listgrouprights-table' ) ) .
55 '<tr>' .
56 Xml::element( 'th', null, $this->msg( 'listgrouprights-group' )->text() ) .
57 Xml::element( 'th', null, $this->msg( 'listgrouprights-rights' )->text() ) .
58 '</tr>'
59 );
60
61 $allGroups = array_unique( array_merge(
62 array_keys( $wgGroupPermissions ),
63 array_keys( $wgRevokePermissions ),
64 array_keys( $wgAddGroups ),
65 array_keys( $wgRemoveGroups ),
66 array_keys( $wgGroupsAddToSelf ),
67 array_keys( $wgGroupsRemoveFromSelf )
68 ) );
69 asort( $allGroups );
70
71 foreach ( $allGroups as $group ) {
72 $permissions = isset( $wgGroupPermissions[$group] )
73 ? $wgGroupPermissions[$group]
74 : array();
75 $groupname = ( $group == '*' ) // Replace * with a more descriptive groupname
76 ? 'all'
77 : $group;
78
79 $msg = $this->msg( 'group-' . $groupname );
80 $groupnameLocalized = !$msg->isBlank() ? $msg->text() : $groupname;
81
82 $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
83 $grouppageLocalized = !$msg->isBlank() ?
84 $msg->text() :
85 MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
86
87 if ( $group == '*' ) {
88 // Do not make a link for the generic * group
89 $grouppage = htmlspecialchars( $groupnameLocalized );
90 } else {
91 $grouppage = Linker::link(
92 Title::newFromText( $grouppageLocalized ),
93 htmlspecialchars( $groupnameLocalized )
94 );
95 }
96
97 if ( $group === 'user' ) {
98 // Link to Special:listusers for implicit group 'user'
99 $grouplink = '<br />' . Linker::linkKnown(
100 SpecialPage::getTitleFor( 'Listusers' ),
101 $this->msg( 'listgrouprights-members' )->escaped()
102 );
103 } elseif ( !in_array( $group, $wgImplicitGroups ) ) {
104 $grouplink = '<br />' . Linker::linkKnown(
105 SpecialPage::getTitleFor( 'Listusers' ),
106 $this->msg( 'listgrouprights-members' )->escaped(),
107 array(),
108 array( '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 = isset( $wgRevokePermissions[$group] ) ? $wgRevokePermissions[$group] : array();
116 $addgroups = isset( $wgAddGroups[$group] ) ? $wgAddGroups[$group] : array();
117 $removegroups = isset( $wgRemoveGroups[$group] ) ? $wgRemoveGroups[$group] : array();
118 $addgroupsSelf = isset( $wgGroupsAddToSelf[$group] ) ? $wgGroupsAddToSelf[$group] : array();
119 $removegroupsSelf = isset( $wgGroupsRemoveFromSelf[$group] )
120 ? $wgGroupsRemoveFromSelf[$group]
121 : array();
122
123 $id = $group == '*' ? false : Sanitizer::escapeId( $group );
124 $out->addHTML( Html::rawElement( 'tr', array( 'id' => $id ), "
125 <td>$grouppage$grouplink</td>
126 <td>" .
127 $this->formatPermissions( $permissions, $revoke, $addgroups, $removegroups,
128 $addgroupsSelf, $removegroupsSelf ) .
129 '</td>
130 '
131 ) );
132 }
133 $out->addHTML( Xml::closeElement( 'table' ) );
134 $this->outputNamespaceProtectionInfo();
135 }
136
137 private function outputNamespaceProtectionInfo() {
138 global $wgNamespaceProtection, $wgParser, $wgContLang;
139 $out = $this->getOutput();
140
141 if ( count( $wgNamespaceProtection ) == 0 ) {
142 return;
143 }
144
145 $header = $this->msg( 'listgrouprights-namespaceprotection-header' )->parse();
146 $out->addHTML(
147 Html::rawElement( 'h2', array(), Html::element( 'span', array(
148 'class' => 'mw-headline',
149 'id' => $wgParser->guessSectionNameFromWikiText( $header )
150 ), $header ) ) .
151 Xml::openElement( 'table', array( 'class' => 'wikitable' ) ) .
152 Html::element(
153 'th',
154 array(),
155 $this->msg( 'listgrouprights-namespaceprotection-namespace' )->text()
156 ) .
157 Html::element(
158 'th',
159 array(),
160 $this->msg( 'listgrouprights-namespaceprotection-restrictedto' )->text()
161 )
162 );
163
164 ksort( $wgNamespaceProtection );
165 foreach ( $wgNamespaceProtection as $namespace => $rights ) {
166 if ( !in_array( $namespace, MWNamespace::getValidNamespaces() ) ) {
167 continue;
168 }
169
170 if ( $namespace == NS_MAIN ) {
171 $namespaceText = $this->msg( 'blanknamespace' )->text();
172 } else {
173 $namespaceText = $wgContLang->convertNamespace( $namespace );
174 }
175
176 $out->addHTML(
177 Xml::openElement( 'tr' ) .
178 Html::rawElement(
179 'td',
180 array(),
181 Linker::link(
182 SpecialPage::getTitleFor( 'Allpages' ),
183 $namespaceText,
184 array(),
185 array( 'namespace' => $namespace )
186 )
187 ) .
188 Xml::openElement( 'td' ) . Xml::openElement( 'ul' )
189 );
190
191 if ( !is_array( $rights ) ) {
192 $rights = array( $rights );
193 }
194
195 foreach ( $rights as $right ) {
196 $out->addHTML(
197 Html::rawElement( 'li', array(), $this->msg(
198 'listgrouprights-right-display',
199 User::getRightDescription( $right ),
200 Html::element(
201 'span',
202 array( 'class' => 'mw-listgrouprights-right-name' ),
203 $right
204 )
205 )->parse() )
206 );
207 }
208
209 $out->addHTML(
210 Xml::closeElement( 'ul' ) .
211 Xml::closeElement( 'td' ) .
212 Xml::closeElement( 'tr' )
213 );
214 }
215 $out->addHTML( Xml::closeElement( 'table' ) );
216 }
217
218 /**
219 * Create a user-readable list of permissions from the given array.
220 *
221 * @param array $permissions Array of permission => bool (from $wgGroupPermissions items)
222 * @param array $revoke Array of permission => bool (from $wgRevokePermissions items)
223 * @param array $add Array of groups this group is allowed to add or true
224 * @param array $remove Array of groups this group is allowed to remove or true
225 * @param array $addSelf Array of groups this group is allowed to add to self or true
226 * @param array $removeSelf Array of group this group is allowed to remove from self or true
227 * @return string List of all granted permissions, separated by comma separator
228 */
229 private function formatPermissions( $permissions, $revoke, $add, $remove, $addSelf, $removeSelf ) {
230 $r = array();
231 foreach ( $permissions as $permission => $granted ) {
232 //show as granted only if it isn't revoked to prevent duplicate display of permissions
233 if ( $granted && ( !isset( $revoke[$permission] ) || !$revoke[$permission] ) ) {
234 $description = $this->msg( 'listgrouprights-right-display',
235 User::getRightDescription( $permission ),
236 '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
237 )->parse();
238 $r[] = $description;
239 }
240 }
241 foreach ( $revoke as $permission => $revoked ) {
242 if ( $revoked ) {
243 $description = $this->msg( 'listgrouprights-right-revoked',
244 User::getRightDescription( $permission ),
245 '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
246 )->parse();
247 $r[] = $description;
248 }
249 }
250
251 sort( $r );
252
253 $lang = $this->getLanguage();
254
255 if ( $add === true ) {
256 $r[] = $this->msg( 'listgrouprights-addgroup-all' )->escaped();
257 } elseif ( is_array( $add ) && count( $add ) ) {
258 $add = array_values( array_unique( $add ) );
259 $r[] = $this->msg( 'listgrouprights-addgroup',
260 $lang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $add ) ),
261 count( $add )
262 )->parse();
263 }
264
265 if ( $remove === true ) {
266 $r[] = $this->msg( 'listgrouprights-removegroup-all' )->escaped();
267 } elseif ( is_array( $remove ) && count( $remove ) ) {
268 $remove = array_values( array_unique( $remove ) );
269 $r[] = $this->msg( 'listgrouprights-removegroup',
270 $lang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $remove ) ),
271 count( $remove )
272 )->parse();
273 }
274
275 if ( $addSelf === true ) {
276 $r[] = $this->msg( 'listgrouprights-addgroup-self-all' )->escaped();
277 } elseif ( is_array( $addSelf ) && count( $addSelf ) ) {
278 $addSelf = array_values( array_unique( $addSelf ) );
279 $r[] = $this->msg( 'listgrouprights-addgroup-self',
280 $lang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $addSelf ) ),
281 count( $addSelf )
282 )->parse();
283 }
284
285 if ( $removeSelf === true ) {
286 $r[] = $this->msg( 'listgrouprights-removegroup-self-all' )->parse();
287 } elseif ( is_array( $removeSelf ) && count( $removeSelf ) ) {
288 $removeSelf = array_values( array_unique( $removeSelf ) );
289 $r[] = $this->msg( 'listgrouprights-removegroup-self',
290 $lang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $removeSelf ) ),
291 count( $removeSelf )
292 )->parse();
293 }
294
295 if ( empty( $r ) ) {
296 return '';
297 } else {
298 return '<ul><li>' . implode( "</li>\n<li>", $r ) . '</li></ul>';
299 }
300 }
301
302 protected function getGroupName() {
303 return 'users';
304 }
305 }