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