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