w/s diff. mostly eol w/s and using only tabs of width 4 to indent.
[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 var $skin;
34
35 /**
36 * Constructor
37 */
38 function __construct() {
39 global $wgUser;
40 parent::__construct( 'Listgrouprights' );
41 $this->skin = $wgUser->getSkin();
42 }
43
44 /**
45 * Show the special page
46 */
47 public function execute( $par ) {
48 global $wgOut, $wgImplicitGroups;
49 global $wgGroupPermissions, $wgRevokePermissions, $wgAddGroups, $wgRemoveGroups;
50 global $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
51
52 $this->setHeaders();
53 $this->outputHeader();
54 $wgOut->addModuleStyles( 'mediawiki.special' );
55
56 $wgOut->addHTML(
57 Xml::openElement( 'table', array( 'class' => 'wikitable mw-listgrouprights-table' ) ) .
58 '<tr>' .
59 Xml::element( 'th', null, wfMsg( 'listgrouprights-group' ) ) .
60 Xml::element( 'th', null, wfMsg( 'listgrouprights-rights' ) ) .
61 '</tr>'
62 );
63
64 $allGroups = array_unique( array_merge(
65 array_keys( $wgGroupPermissions ),
66 array_keys( $wgRevokePermissions ),
67 array_keys( $wgAddGroups ),
68 array_keys( $wgRemoveGroups ),
69 array_keys( $wgGroupsAddToSelf ),
70 array_keys( $wgGroupsRemoveFromSelf )
71 ) );
72 asort( $allGroups );
73
74 foreach ( $allGroups as $group ) {
75 $permissions = isset( $wgGroupPermissions[$group] )
76 ? $wgGroupPermissions[$group]
77 : array();
78 $groupname = ( $group == '*' ) // Replace * with a more descriptive groupname
79 ? 'all'
80 : $group;
81
82 $msg = wfMessage( 'group-' . $groupname );
83 $groupnameLocalized = !$msg->isBlank() ? $msg->text() : $groupname;
84
85 $msg = wfMessage( 'grouppage-' . $groupname )->inContentLanguage();
86 $grouppageLocalized = !$msg->isBlank() ?
87 $msg->text() :
88 MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
89
90 if( $group == '*' ) {
91 // Do not make a link for the generic * group
92 $grouppage = htmlspecialchars( $groupnameLocalized );
93 } else {
94 $grouppage = $this->skin->link(
95 Title::newFromText( $grouppageLocalized ),
96 htmlspecialchars( $groupnameLocalized )
97 );
98 }
99
100 if ( $group === 'user' ) {
101 // Link to Special:listusers for implicit group 'user'
102 $grouplink = '<br />' . $this->skin->link(
103 SpecialPage::getTitleFor( 'Listusers' ),
104 wfMsgHtml( 'listgrouprights-members' ),
105 array(),
106 array(),
107 array( 'known', 'noclasses' )
108 );
109 } elseif ( !in_array( $group, $wgImplicitGroups ) ) {
110 $grouplink = '<br />' . $this->skin->link(
111 SpecialPage::getTitleFor( 'Listusers' ),
112 wfMsgHtml( 'listgrouprights-members' ),
113 array(),
114 array( 'group' => $group ),
115 array( 'known', 'noclasses' )
116 );
117 } else {
118 // No link to Special:listusers for other implicit groups as they are unlistable
119 $grouplink = '';
120 }
121
122 $revoke = isset( $wgRevokePermissions[$group] ) ? $wgRevokePermissions[$group] : array();
123 $addgroups = isset( $wgAddGroups[$group] ) ? $wgAddGroups[$group] : array();
124 $removegroups = isset( $wgRemoveGroups[$group] ) ? $wgRemoveGroups[$group] : array();
125 $addgroupsSelf = isset( $wgGroupsAddToSelf[$group] ) ? $wgGroupsAddToSelf[$group] : array();
126 $removegroupsSelf = isset( $wgGroupsRemoveFromSelf[$group] ) ? $wgGroupsRemoveFromSelf[$group] : array();
127
128 $id = $group == '*' ? false : Sanitizer::escapeId( $group );
129 $wgOut->addHTML( Html::rawElement( 'tr', array( 'id' => $id ),
130 "
131 <td>$grouppage$grouplink</td>
132 <td>" .
133 self::formatPermissions( $permissions, $revoke, $addgroups, $removegroups, $addgroupsSelf, $removegroupsSelf ) .
134 '</td>
135 '
136 ) );
137 }
138 $wgOut->addHTML(
139 Xml::closeElement( 'table' ) . "\n<br /><hr />\n"
140 );
141 $wgOut->wrapWikiMsg( "<div class=\"mw-listgrouprights-key\">\n$1\n</div>", 'listgrouprights-key' );
142 }
143
144 /**
145 * Create a user-readable list of permissions from the given array.
146 *
147 * @param $permissions Array of permission => bool (from $wgGroupPermissions items)
148 * @param $revoke Array of permission => bool (from $wgRevokePermissions items)
149 * @param $add Array of groups this group is allowed to add or true
150 * @param $remove Array of groups this group is allowed to remove or true
151 * @param $addSelf Array of groups this group is allowed to add to self or true
152 * @param $removeSelf Array of group this group is allowed to remove from self or true
153 * @return string List of all granted permissions, separated by comma separator
154 */
155 private static function formatPermissions( $permissions, $revoke, $add, $remove, $addSelf, $removeSelf ) {
156 global $wgLang;
157
158 $r = array();
159 foreach( $permissions as $permission => $granted ) {
160 //show as granted only if it isn't revoked to prevent duplicate display of permissions
161 if( $granted && ( !isset( $revoke[$permission] ) || !$revoke[$permission] ) ) {
162 $description = wfMsgExt( 'listgrouprights-right-display', array( 'parseinline' ),
163 User::getRightDescription( $permission ),
164 '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
165 );
166 $r[] = $description;
167 }
168 }
169 foreach( $revoke as $permission => $revoked ) {
170 if( $revoked ) {
171 $description = wfMsgExt( 'listgrouprights-right-revoked', array( 'parseinline' ),
172 User::getRightDescription( $permission ),
173 '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
174 );
175 $r[] = $description;
176 }
177 }
178 sort( $r );
179 if( $add === true ){
180 $r[] = wfMsgExt( 'listgrouprights-addgroup-all', array( 'escape' ) );
181 } else if( is_array( $add ) && count( $add ) ) {
182 $add = array_values( array_unique( $add ) );
183 $r[] = wfMsgExt( 'listgrouprights-addgroup', array( 'parseinline' ), $wgLang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $add ) ), count( $add ) );
184 }
185 if( $remove === true ){
186 $r[] = wfMsgExt( 'listgrouprights-removegroup-all', array( 'escape' ) );
187 } else if( is_array( $remove ) && count( $remove ) ) {
188 $remove = array_values( array_unique( $remove ) );
189 $r[] = wfMsgExt( 'listgrouprights-removegroup', array( 'parseinline' ), $wgLang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $remove ) ), count( $remove ) );
190 }
191 if( $addSelf === true ){
192 $r[] = wfMsgExt( 'listgrouprights-addgroup-self-all', array( 'escape' ) );
193 } else if( is_array( $addSelf ) && count( $addSelf ) ) {
194 $addSelf = array_values( array_unique( $addSelf ) );
195 $r[] = wfMsgExt( 'listgrouprights-addgroup-self', array( 'parseinline' ), $wgLang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $addSelf ) ), count( $addSelf ) );
196 }
197 if( $removeSelf === true ){
198 $r[] = wfMsgExt( 'listgrouprights-removegroup-self-all', array( 'escape' ) );
199 } else if( is_array( $removeSelf ) && count( $removeSelf ) ) {
200 $removeSelf = array_values( array_unique( $removeSelf ) );
201 $r[] = wfMsgExt( 'listgrouprights-removegroup-self', array( 'parseinline' ), $wgLang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $removeSelf ) ), count( $removeSelf ) );
202 }
203 if( empty( $r ) ) {
204 return '';
205 } else {
206 return '<ul><li>' . implode( "</li>\n<li>", $r ) . '</li></ul>';
207 }
208 }
209 }