* (bug 19301) Duplicates entries in $wgAddGroups, $wgRemoveGroups, $wgGroupsAddToSelf...
[lhc/web/wiklou.git] / includes / specials / SpecialListgrouprights.php
1 <?php
2
3 /**
4 * This special page lists all defined user groups and the associated rights.
5 * See also @ref $wgGroupPermissions.
6 *
7 * @ingroup SpecialPage
8 * @author Petr Kadlec <mormegil@centrum.cz>
9 */
10 class SpecialListGroupRights extends SpecialPage {
11
12 var $skin;
13
14 /**
15 * Constructor
16 */
17 function __construct() {
18 global $wgUser;
19 parent::__construct( 'Listgrouprights' );
20 $this->skin = $wgUser->getSkin();
21 }
22
23 /**
24 * Show the special page
25 */
26 public function execute( $par ) {
27 global $wgOut, $wgImplicitGroups, $wgMessageCache;
28 global $wgGroupPermissions, $wgRevokePermissions, $wgAddGroups, $wgRemoveGroups;
29 global $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
30 $wgMessageCache->loadAllMessages();
31
32 $this->setHeaders();
33 $this->outputHeader();
34
35 $wgOut->addHTML(
36 Xml::openElement( 'table', array( 'class' => 'wikitable mw-listgrouprights-table' ) ) .
37 '<tr>' .
38 Xml::element( 'th', null, wfMsg( 'listgrouprights-group' ) ) .
39 Xml::element( 'th', null, wfMsg( 'listgrouprights-rights' ) ) .
40 '</tr>'
41 );
42
43 foreach( $wgGroupPermissions as $group => $permissions ) {
44 $groupname = ( $group == '*' ) ? 'all' : $group; // Replace * with a more descriptive groupname
45
46 $msg = wfMsg( 'group-' . $groupname );
47 if ( wfEmptyMsg( 'group-' . $groupname, $msg ) || $msg == '' ) {
48 $groupnameLocalized = $groupname;
49 } else {
50 $groupnameLocalized = $msg;
51 }
52
53 $msg = wfMsgForContent( 'grouppage-' . $groupname );
54 if ( wfEmptyMsg( 'grouppage-' . $groupname, $msg ) || $msg == '' ) {
55 $grouppageLocalized = MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
56 } else {
57 $grouppageLocalized = $msg;
58 }
59
60 if( $group == '*' ) {
61 // Do not make a link for the generic * group
62 $grouppage = htmlspecialchars($groupnameLocalized);
63 } else {
64 $grouppage = $this->skin->link(
65 Title::newFromText( $grouppageLocalized ),
66 htmlspecialchars($groupnameLocalized)
67 );
68 }
69
70 if ( $group === 'user' ) {
71 // Link to Special:listusers for implicit group 'user'
72 $grouplink = '<br />' . $this->skin->link(
73 SpecialPage::getTitleFor( 'Listusers' ),
74 wfMsgHtml( 'listgrouprights-members' ),
75 array(),
76 array(),
77 array( 'known', 'noclasses' )
78 );
79 } elseif ( !in_array( $group, $wgImplicitGroups ) ) {
80 $grouplink = '<br />' . $this->skin->link(
81 SpecialPage::getTitleFor( 'Listusers' ),
82 wfMsgHtml( 'listgrouprights-members' ),
83 array(),
84 array( 'group' => $group ),
85 array( 'known', 'noclasses' )
86 );
87 } else {
88 // No link to Special:listusers for other implicit groups as they are unlistable
89 $grouplink = '';
90 }
91
92 $revoke = isset( $wgRevokePermissions[$group] ) ? $wgRevokePermissions[$group] : array();
93 $addgroups = isset( $wgAddGroups[$group] ) ? $wgAddGroups[$group] : array();
94 $removegroups = isset( $wgRemoveGroups[$group] ) ? $wgRemoveGroups[$group] : array();
95 $addgroupsSelf = isset( $wgGroupsAddToSelf[$group] ) ? $wgGroupsAddToSelf[$group] : array();
96 $removegroupsSelf = isset( $wgGroupsRemoveFromSelf[$group] ) ? $wgGroupsRemoveFromSelf[$group] : array();
97
98 $wgOut->addHTML(
99 '<tr>
100 <td>' .
101 $grouppage . $grouplink .
102 '</td>
103 <td>' .
104 self::formatPermissions( $permissions, $revoke, $addgroups, $removegroups, $addgroupsSelf, $removegroupsSelf ) .
105 '</td>
106 </tr>'
107 );
108 }
109 $wgOut->addHTML(
110 Xml::closeElement( 'table' ) . "\n<br /><hr />\n"
111 );
112 $wgOut->wrapWikiMsg( "<div class=\"mw-listgrouprights-key\">\n$1</div>",'listgrouprights-key' );
113 }
114
115 /**
116 * Create a user-readable list of permissions from the given array.
117 *
118 * @param $permissions Array of permission => bool (from $wgGroupPermissions items)
119 * @param $revoke Array of permission => bool (from $wgRevokePermissions items)
120 * @param $add Array of groups this group is allowed to add or true
121 * @param $remove Array of groups this group is allowed to remove or true
122 * @param $addSelf Array of groups this group is allowed to add to self or true
123 * @param $removeSelf Array of group this group is allowed to remove from self or true
124 * @return string List of all granted permissions, separated by comma separator
125 */
126 private static function formatPermissions( $permissions, $revoke, $add, $remove, $addSelf, $removeSelf ) {
127 global $wgLang;
128
129 // prevent double entries if misconfigured, bug 19301
130 $add = array_unique( $add );
131 $remove = array_unique( $remove );
132 $addSelf = array_unique( $addSelf );
133 $removeSelf = array_unique( $removeSelf );
134
135 $r = array();
136 foreach( $permissions as $permission => $granted ) {
137 //show as granted only if it isn't revoked to prevent duplicate display of permissions
138 if( $granted && ( !isset( $revoke[$permission] ) || !$revoke[$permission] ) ) {
139 $description = wfMsgExt( 'listgrouprights-right-display', array( 'parseinline' ),
140 User::getRightDescription( $permission ),
141 $permission
142 );
143 $r[] = $description;
144 }
145 }
146 foreach( $revoke as $permission => $revoked ) {
147 if( $revoked ) {
148 $description = wfMsgExt( 'listgrouprights-right-revoked', array( 'parseinline' ),
149 User::getRightDescription( $permission ),
150 $permission
151 );
152 $r[] = $description;
153 }
154 }
155 sort( $r );
156 if( $add === true ){
157 $r[] = wfMsgExt( 'listgrouprights-addgroup-all', array( 'escape' ) );
158 } else if( is_array( $add ) && count( $add ) ) {
159 $r[] = wfMsgExt( 'listgrouprights-addgroup', array( 'parseinline' ), $wgLang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $add ) ), count( $add ) );
160 }
161 if( $remove === true ){
162 $r[] = wfMsgExt( 'listgrouprights-removegroup-all', array( 'escape' ) );
163 } else if( is_array( $remove ) && count( $remove ) ) {
164 $r[] = wfMsgExt( 'listgrouprights-removegroup', array( 'parseinline' ), $wgLang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $remove ) ), count( $remove ) );
165 }
166 if( $addSelf === true ){
167 $r[] = wfMsgExt( 'listgrouprights-addgroup-self-all', array( 'escape' ) );
168 } else if( is_array( $addSelf ) && count( $addSelf ) ) {
169 $r[] = wfMsgExt( 'listgrouprights-addgroup-self', array( 'parseinline' ), $wgLang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $addSelf ) ), count( $addSelf ) );
170 }
171 if( $removeSelf === true ){
172 $r[] = wfMsgExt( 'listgrouprights-removegroup-self-all', array( 'escape' ) );
173 } else if( is_array( $removeSelf ) && count( $removeSelf ) ) {
174 $r[] = wfMsgExt( 'listgrouprights-removegroup-self', array( 'parseinline' ), $wgLang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $removeSelf ) ), count( $removeSelf ) );
175 }
176 if( empty( $r ) ) {
177 return '';
178 } else {
179 return '<ul><li>' . implode( "</li>\n<li>", $r ) . '</li></ul>';
180 }
181 }
182 }