Correct the address of the FSF in some of the GPL headers
[lhc/web/wiklou.git] / includes / specials / SpecialListgrouprights.php
1 <?php
2 /**
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 */
19
20 /**
21 * This special page lists all defined user groups and the associated rights.
22 * See also @ref $wgGroupPermissions.
23 *
24 * @ingroup SpecialPage
25 * @author Petr Kadlec <mormegil@centrum.cz>
26 */
27 class SpecialListGroupRights extends SpecialPage {
28
29 var $skin;
30
31 /**
32 * Constructor
33 */
34 function __construct() {
35 global $wgUser;
36 parent::__construct( 'Listgrouprights' );
37 $this->skin = $wgUser->getSkin();
38 }
39
40 /**
41 * Show the special page
42 */
43 public function execute( $par ) {
44 global $wgOut, $wgImplicitGroups, $wgMessageCache;
45 global $wgGroupPermissions, $wgRevokePermissions, $wgAddGroups, $wgRemoveGroups;
46 global $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
47 $wgMessageCache->loadAllMessages();
48
49 $this->setHeaders();
50 $this->outputHeader();
51
52 $wgOut->addHTML(
53 Xml::openElement( 'table', array( 'class' => 'wikitable mw-listgrouprights-table' ) ) .
54 '<tr>' .
55 Xml::element( 'th', null, wfMsg( 'listgrouprights-group' ) ) .
56 Xml::element( 'th', null, wfMsg( 'listgrouprights-rights' ) ) .
57 '</tr>'
58 );
59
60 foreach( $wgGroupPermissions as $group => $permissions ) {
61 $groupname = ( $group == '*' ) ? 'all' : $group; // Replace * with a more descriptive groupname
62
63 $msg = wfMsg( 'group-' . $groupname );
64 if ( wfEmptyMsg( 'group-' . $groupname, $msg ) || $msg == '' ) {
65 $groupnameLocalized = $groupname;
66 } else {
67 $groupnameLocalized = $msg;
68 }
69
70 $msg = wfMsgForContent( 'grouppage-' . $groupname );
71 if ( wfEmptyMsg( 'grouppage-' . $groupname, $msg ) || $msg == '' ) {
72 $grouppageLocalized = MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
73 } else {
74 $grouppageLocalized = $msg;
75 }
76
77 if( $group == '*' ) {
78 // Do not make a link for the generic * group
79 $grouppage = htmlspecialchars( $groupnameLocalized );
80 } else {
81 $grouppage = $this->skin->link(
82 Title::newFromText( $grouppageLocalized ),
83 htmlspecialchars( $groupnameLocalized )
84 );
85 }
86
87 if ( $group === 'user' ) {
88 // Link to Special:listusers for implicit group 'user'
89 $grouplink = '<br />' . $this->skin->link(
90 SpecialPage::getTitleFor( 'Listusers' ),
91 wfMsgHtml( 'listgrouprights-members' ),
92 array(),
93 array(),
94 array( 'known', 'noclasses' )
95 );
96 } elseif ( !in_array( $group, $wgImplicitGroups ) ) {
97 $grouplink = '<br />' . $this->skin->link(
98 SpecialPage::getTitleFor( 'Listusers' ),
99 wfMsgHtml( 'listgrouprights-members' ),
100 array(),
101 array( 'group' => $group ),
102 array( 'known', 'noclasses' )
103 );
104 } else {
105 // No link to Special:listusers for other implicit groups as they are unlistable
106 $grouplink = '';
107 }
108
109 $revoke = isset( $wgRevokePermissions[$group] ) ? $wgRevokePermissions[$group] : array();
110 $addgroups = isset( $wgAddGroups[$group] ) ? $wgAddGroups[$group] : array();
111 $removegroups = isset( $wgRemoveGroups[$group] ) ? $wgRemoveGroups[$group] : array();
112 $addgroupsSelf = isset( $wgGroupsAddToSelf[$group] ) ? $wgGroupsAddToSelf[$group] : array();
113 $removegroupsSelf = isset( $wgGroupsRemoveFromSelf[$group] ) ? $wgGroupsRemoveFromSelf[$group] : array();
114
115 $id = $group == '*' ? false : Sanitizer::escapeId( $group );
116 $wgOut->addHTML( Html::rawElement( 'tr', array( 'id' => $id ),
117 "
118 <td>$grouppage$grouplink</td>
119 <td>" .
120 self::formatPermissions( $permissions, $revoke, $addgroups, $removegroups, $addgroupsSelf, $removegroupsSelf ) .
121 '</td>
122 '
123 ) );
124 }
125 $wgOut->addHTML(
126 Xml::closeElement( 'table' ) . "\n<br /><hr />\n"
127 );
128 $wgOut->wrapWikiMsg( "<div class=\"mw-listgrouprights-key\">\n$1\n</div>", 'listgrouprights-key' );
129 }
130
131 /**
132 * Create a user-readable list of permissions from the given array.
133 *
134 * @param $permissions Array of permission => bool (from $wgGroupPermissions items)
135 * @param $revoke Array of permission => bool (from $wgRevokePermissions items)
136 * @param $add Array of groups this group is allowed to add or true
137 * @param $remove Array of groups this group is allowed to remove or true
138 * @param $addSelf Array of groups this group is allowed to add to self or true
139 * @param $removeSelf Array of group this group is allowed to remove from self or true
140 * @return string List of all granted permissions, separated by comma separator
141 */
142 private static function formatPermissions( $permissions, $revoke, $add, $remove, $addSelf, $removeSelf ) {
143 global $wgLang;
144
145 $r = array();
146 foreach( $permissions as $permission => $granted ) {
147 //show as granted only if it isn't revoked to prevent duplicate display of permissions
148 if( $granted && ( !isset( $revoke[$permission] ) || !$revoke[$permission] ) ) {
149 $description = wfMsgExt( 'listgrouprights-right-display', array( 'parseinline' ),
150 User::getRightDescription( $permission ),
151 '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
152 );
153 $r[] = $description;
154 }
155 }
156 foreach( $revoke as $permission => $revoked ) {
157 if( $revoked ) {
158 $description = wfMsgExt( 'listgrouprights-right-revoked', array( 'parseinline' ),
159 User::getRightDescription( $permission ),
160 '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
161 );
162 $r[] = $description;
163 }
164 }
165 sort( $r );
166 if( $add === true ){
167 $r[] = wfMsgExt( 'listgrouprights-addgroup-all', array( 'escape' ) );
168 } else if( is_array( $add ) && count( $add ) ) {
169 $add = array_values( array_unique( $add ) );
170 $r[] = wfMsgExt( 'listgrouprights-addgroup', array( 'parseinline' ), $wgLang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $add ) ), count( $add ) );
171 }
172 if( $remove === true ){
173 $r[] = wfMsgExt( 'listgrouprights-removegroup-all', array( 'escape' ) );
174 } else if( is_array( $remove ) && count( $remove ) ) {
175 $remove = array_values( array_unique( $remove ) );
176 $r[] = wfMsgExt( 'listgrouprights-removegroup', array( 'parseinline' ), $wgLang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $remove ) ), count( $remove ) );
177 }
178 if( $addSelf === true ){
179 $r[] = wfMsgExt( 'listgrouprights-addgroup-self-all', array( 'escape' ) );
180 } else if( is_array( $addSelf ) && count( $addSelf ) ) {
181 $addSelf = array_values( array_unique( $addSelf ) );
182 $r[] = wfMsgExt( 'listgrouprights-addgroup-self', array( 'parseinline' ), $wgLang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $addSelf ) ), count( $addSelf ) );
183 }
184 if( $removeSelf === true ){
185 $r[] = wfMsgExt( 'listgrouprights-removegroup-self-all', array( 'escape' ) );
186 } else if( is_array( $removeSelf ) && count( $removeSelf ) ) {
187 $removeSelf = array_values( array_unique( $removeSelf ) );
188 $r[] = wfMsgExt( 'listgrouprights-removegroup-self', array( 'parseinline' ), $wgLang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $removeSelf ) ), count( $removeSelf ) );
189 }
190 if( empty( $r ) ) {
191 return '';
192 } else {
193 return '<ul><li>' . implode( "</li>\n<li>", $r ) . '</li></ul>';
194 }
195 }
196 }