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