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