Merge "Shorten ternary expressions in RawAction.php"
[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 */
39 public function execute( $par ) {
40 global $wgImplicitGroups;
41 global $wgGroupPermissions, $wgRevokePermissions, $wgAddGroups, $wgRemoveGroups;
42 global $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
43
44 $this->setHeaders();
45 $this->outputHeader();
46
47 $out = $this->getOutput();
48 $out->addModuleStyles( 'mediawiki.special' );
49
50 $out->wrapWikiMsg( "<div class=\"mw-listgrouprights-key\">\n$1\n</div>", 'listgrouprights-key' );
51
52 $out->addHTML(
53 Xml::openElement( 'table', array( '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 $allGroups = array_unique( array_merge(
61 array_keys( $wgGroupPermissions ),
62 array_keys( $wgRevokePermissions ),
63 array_keys( $wgAddGroups ),
64 array_keys( $wgRemoveGroups ),
65 array_keys( $wgGroupsAddToSelf ),
66 array_keys( $wgGroupsRemoveFromSelf )
67 ) );
68 asort( $allGroups );
69
70 foreach ( $allGroups as $group ) {
71 $permissions = isset( $wgGroupPermissions[$group] )
72 ? $wgGroupPermissions[$group]
73 : array();
74 $groupname = ( $group == '*' ) // Replace * with a more descriptive groupname
75 ? 'all'
76 : $group;
77
78 $msg = $this->msg( 'group-' . $groupname );
79 $groupnameLocalized = !$msg->isBlank() ? $msg->text() : $groupname;
80
81 $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
82 $grouppageLocalized = !$msg->isBlank() ?
83 $msg->text() :
84 MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
85
86 if ( $group == '*' ) {
87 // Do not make a link for the generic * group
88 $grouppage = htmlspecialchars( $groupnameLocalized );
89 } else {
90 $grouppage = Linker::link(
91 Title::newFromText( $grouppageLocalized ),
92 htmlspecialchars( $groupnameLocalized )
93 );
94 }
95
96 if ( $group === 'user' ) {
97 // Link to Special:listusers for implicit group 'user'
98 $grouplink = '<br />' . Linker::linkKnown(
99 SpecialPage::getTitleFor( 'Listusers' ),
100 $this->msg( 'listgrouprights-members' )->escaped()
101 );
102 } elseif ( !in_array( $group, $wgImplicitGroups ) ) {
103 $grouplink = '<br />' . Linker::linkKnown(
104 SpecialPage::getTitleFor( 'Listusers' ),
105 $this->msg( 'listgrouprights-members' )->escaped(),
106 array(),
107 array( 'group' => $group )
108 );
109 } else {
110 // No link to Special:listusers for other implicit groups as they are unlistable
111 $grouplink = '';
112 }
113
114 $revoke = isset( $wgRevokePermissions[$group] ) ? $wgRevokePermissions[$group] : array();
115 $addgroups = isset( $wgAddGroups[$group] ) ? $wgAddGroups[$group] : array();
116 $removegroups = isset( $wgRemoveGroups[$group] ) ? $wgRemoveGroups[$group] : array();
117 $addgroupsSelf = isset( $wgGroupsAddToSelf[$group] ) ? $wgGroupsAddToSelf[$group] : array();
118 $removegroupsSelf = isset( $wgGroupsRemoveFromSelf[$group] )
119 ? $wgGroupsRemoveFromSelf[$group]
120 : array();
121
122 $id = $group == '*' ? false : Sanitizer::escapeId( $group );
123 $out->addHTML( Html::rawElement( 'tr', array( 'id' => $id ), "
124 <td>$grouppage$grouplink</td>
125 <td>" .
126 $this->formatPermissions( $permissions, $revoke, $addgroups, $removegroups,
127 $addgroupsSelf, $removegroupsSelf ) .
128 '</td>
129 '
130 ) );
131 }
132 $out->addHTML( Xml::closeElement( 'table' ) );
133 $this->outputNamespaceProtectionInfo();
134 }
135
136 private function outputNamespaceProtectionInfo() {
137 global $wgNamespaceProtection, $wgParser, $wgContLang;
138 $out = $this->getOutput();
139
140 if ( count( $wgNamespaceProtection ) == 0 ) {
141 return;
142 }
143
144 $header = $this->msg( 'listgrouprights-namespaceprotection-header' )->parse();
145 $out->addHTML(
146 Html::rawElement( 'h2', array(), Html::element( 'span', array(
147 'class' => 'mw-headline',
148 'id' => $wgParser->guessSectionNameFromWikiText( $header )
149 ), $header ) ) .
150 Xml::openElement( 'table', array( 'class' => 'wikitable' ) ) .
151 Html::element(
152 'th',
153 array(),
154 $this->msg( 'listgrouprights-namespaceprotection-namespace' )->text()
155 ) .
156 Html::element(
157 'th',
158 array(),
159 $this->msg( 'listgrouprights-namespaceprotection-restrictedto' )->text()
160 )
161 );
162
163 ksort( $wgNamespaceProtection );
164 foreach ( $wgNamespaceProtection 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 array(),
180 Linker::link(
181 SpecialPage::getTitleFor( 'Allpages' ),
182 $namespaceText,
183 array(),
184 array( 'namespace' => $namespace )
185 )
186 ) .
187 Xml::openElement( 'td' ) . Xml::openElement( 'ul' )
188 );
189
190 if ( !is_array( $rights ) ) {
191 $rights = array( $rights );
192 }
193
194 foreach ( $rights as $right ) {
195 $out->addHTML(
196 Html::rawElement( 'li', array(), $this->msg(
197 'listgrouprights-right-display',
198 User::getRightDescription( $right ),
199 Html::element(
200 'span',
201 array( '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 List of all granted permissions, separated by comma separator
227 */
228 private function formatPermissions( $permissions, $revoke, $add, $remove, $addSelf, $removeSelf ) {
229 $r = array();
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 $description = $this->msg( 'listgrouprights-right-display',
234 User::getRightDescription( $permission ),
235 '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
236 )->parse();
237 $r[] = $description;
238 }
239 }
240 foreach ( $revoke as $permission => $revoked ) {
241 if ( $revoked ) {
242 $description = $this->msg( 'listgrouprights-right-revoked',
243 User::getRightDescription( $permission ),
244 '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
245 )->parse();
246 $r[] = $description;
247 }
248 }
249
250 sort( $r );
251
252 $lang = $this->getLanguage();
253
254 if ( $add === true ) {
255 $r[] = $this->msg( 'listgrouprights-addgroup-all' )->escaped();
256 } elseif ( is_array( $add ) && count( $add ) ) {
257 $add = array_values( array_unique( $add ) );
258 $r[] = $this->msg( 'listgrouprights-addgroup',
259 $lang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $add ) ),
260 count( $add )
261 )->parse();
262 }
263
264 if ( $remove === true ) {
265 $r[] = $this->msg( 'listgrouprights-removegroup-all' )->escaped();
266 } elseif ( is_array( $remove ) && count( $remove ) ) {
267 $remove = array_values( array_unique( $remove ) );
268 $r[] = $this->msg( 'listgrouprights-removegroup',
269 $lang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $remove ) ),
270 count( $remove )
271 )->parse();
272 }
273
274 if ( $addSelf === true ) {
275 $r[] = $this->msg( 'listgrouprights-addgroup-self-all' )->escaped();
276 } elseif ( is_array( $addSelf ) && count( $addSelf ) ) {
277 $addSelf = array_values( array_unique( $addSelf ) );
278 $r[] = $this->msg( 'listgrouprights-addgroup-self',
279 $lang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $addSelf ) ),
280 count( $addSelf )
281 )->parse();
282 }
283
284 if ( $removeSelf === true ) {
285 $r[] = $this->msg( 'listgrouprights-removegroup-self-all' )->parse();
286 } elseif ( is_array( $removeSelf ) && count( $removeSelf ) ) {
287 $removeSelf = array_values( array_unique( $removeSelf ) );
288 $r[] = $this->msg( 'listgrouprights-removegroup-self',
289 $lang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $removeSelf ) ),
290 count( $removeSelf )
291 )->parse();
292 }
293
294 if ( empty( $r ) ) {
295 return '';
296 } else {
297 return '<ul><li>' . implode( "</li>\n<li>", $r ) . '</li></ul>';
298 }
299 }
300
301 protected function getGroupName() {
302 return 'users';
303 }
304 }