SpecialLinkSearch: clean up munged query variable handling
[mediawiki.git] / includes / specials / SpecialListgrouprights.php
blob828a93b9b9c976b4e752e7912b155c25e7e6835e
1 <?php
2 /**
3 * Implements Special:Listgrouprights
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.
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.
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * This special page lists all defined user groups and the associated rights.
26 * See also @ref $wgGroupPermissions.
28 * @ingroup SpecialPage
29 * @author Petr Kadlec <mormegil@centrum.cz>
31 class SpecialListGroupRights extends SpecialPage {
32 function __construct() {
33 parent::__construct( 'Listgrouprights' );
36 /**
37 * Show the special page
38 * @param string|null $par
40 public function execute( $par ) {
41 $this->setHeaders();
42 $this->outputHeader();
44 $out = $this->getOutput();
45 $out->addModuleStyles( 'mediawiki.special' );
47 $out->wrapWikiMsg( "<div class=\"mw-listgrouprights-key\">\n$1\n</div>", 'listgrouprights-key' );
49 $out->addHTML(
50 Xml::openElement( 'table', array( 'class' => 'wikitable mw-listgrouprights-table' ) ) .
51 '<tr>' .
52 Xml::element( 'th', null, $this->msg( 'listgrouprights-group' )->text() ) .
53 Xml::element( 'th', null, $this->msg( 'listgrouprights-rights' )->text() ) .
54 '</tr>'
57 $config = $this->getConfig();
58 $groupPermissions = $config->get( 'GroupPermissions' );
59 $revokePermissions = $config->get( 'RevokePermissions' );
60 $addGroups = $config->get( 'AddGroups' );
61 $removeGroups = $config->get( 'RemoveGroups' );
62 $groupsAddToSelf = $config->get( 'GroupsAddToSelf' );
63 $groupsRemoveFromSelf = $config->get( 'GroupsRemoveFromSelf' );
64 $allGroups = array_unique( array_merge(
65 array_keys( $groupPermissions ),
66 array_keys( $revokePermissions ),
67 array_keys( $addGroups ),
68 array_keys( $removeGroups ),
69 array_keys( $groupsAddToSelf ),
70 array_keys( $groupsRemoveFromSelf )
71 ) );
72 asort( $allGroups );
74 foreach ( $allGroups as $group ) {
75 $permissions = isset( $groupPermissions[$group] )
76 ? $groupPermissions[$group]
77 : array();
78 $groupname = ( $group == '*' ) // Replace * with a more descriptive groupname
79 ? 'all'
80 : $group;
82 $msg = $this->msg( 'group-' . $groupname );
83 $groupnameLocalized = !$msg->isBlank() ? $msg->text() : $groupname;
85 $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
86 $grouppageLocalized = !$msg->isBlank() ?
87 $msg->text() :
88 MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
89 $grouppageLocalizedTitle = Title::newFromText( $grouppageLocalized );
91 if ( $group == '*' || !$grouppageLocalizedTitle ) {
92 // Do not make a link for the generic * group or group with invalid group page
93 $grouppage = htmlspecialchars( $groupnameLocalized );
94 } else {
95 $grouppage = Linker::link(
96 $grouppageLocalizedTitle,
97 htmlspecialchars( $groupnameLocalized )
101 if ( $group === 'user' ) {
102 // Link to Special:listusers for implicit group 'user'
103 $grouplink = '<br />' . Linker::linkKnown(
104 SpecialPage::getTitleFor( 'Listusers' ),
105 $this->msg( 'listgrouprights-members' )->escaped()
107 } elseif ( !in_array( $group, $config->get( 'ImplicitGroups' ) ) ) {
108 $grouplink = '<br />' . Linker::linkKnown(
109 SpecialPage::getTitleFor( 'Listusers' ),
110 $this->msg( 'listgrouprights-members' )->escaped(),
111 array(),
112 array( 'group' => $group )
114 } else {
115 // No link to Special:listusers for other implicit groups as they are unlistable
116 $grouplink = '';
119 $revoke = isset( $revokePermissions[$group] ) ? $revokePermissions[$group] : array();
120 $addgroups = isset( $addGroups[$group] ) ? $addGroups[$group] : array();
121 $removegroups = isset( $removeGroups[$group] ) ? $removeGroups[$group] : array();
122 $addgroupsSelf = isset( $groupsAddToSelf[$group] ) ? $groupsAddToSelf[$group] : array();
123 $removegroupsSelf = isset( $groupsRemoveFromSelf[$group] )
124 ? $groupsRemoveFromSelf[$group]
125 : array();
127 $id = $group == '*' ? false : Sanitizer::escapeId( $group );
128 $out->addHTML( Html::rawElement( 'tr', array( 'id' => $id ), "
129 <td>$grouppage$grouplink</td>
130 <td>" .
131 $this->formatPermissions( $permissions, $revoke, $addgroups, $removegroups,
132 $addgroupsSelf, $removegroupsSelf ) .
133 '</td>
135 ) );
137 $out->addHTML( Xml::closeElement( 'table' ) );
138 $this->outputNamespaceProtectionInfo();
141 private function outputNamespaceProtectionInfo() {
142 global $wgParser, $wgContLang;
143 $out = $this->getOutput();
144 $namespaceProtection = $this->getConfig()->get( 'NamespaceProtection' );
146 if ( count( $namespaceProtection ) == 0 ) {
147 return;
150 $header = $this->msg( 'listgrouprights-namespaceprotection-header' )->parse();
151 $out->addHTML(
152 Html::rawElement( 'h2', array(), Html::element( 'span', array(
153 'class' => 'mw-headline',
154 'id' => $wgParser->guessSectionNameFromWikiText( $header )
155 ), $header ) ) .
156 Xml::openElement( 'table', array( 'class' => 'wikitable' ) ) .
157 Html::element(
158 'th',
159 array(),
160 $this->msg( 'listgrouprights-namespaceprotection-namespace' )->text()
162 Html::element(
163 'th',
164 array(),
165 $this->msg( 'listgrouprights-namespaceprotection-restrictedto' )->text()
169 ksort( $namespaceProtection );
170 foreach ( $namespaceProtection as $namespace => $rights ) {
171 if ( !in_array( $namespace, MWNamespace::getValidNamespaces() ) ) {
172 continue;
175 if ( $namespace == NS_MAIN ) {
176 $namespaceText = $this->msg( 'blanknamespace' )->text();
177 } else {
178 $namespaceText = $wgContLang->convertNamespace( $namespace );
181 $out->addHTML(
182 Xml::openElement( 'tr' ) .
183 Html::rawElement(
184 'td',
185 array(),
186 Linker::link(
187 SpecialPage::getTitleFor( 'Allpages' ),
188 $namespaceText,
189 array(),
190 array( 'namespace' => $namespace )
193 Xml::openElement( 'td' ) . Xml::openElement( 'ul' )
196 if ( !is_array( $rights ) ) {
197 $rights = array( $rights );
200 foreach ( $rights as $right ) {
201 $out->addHTML(
202 Html::rawElement( 'li', array(), $this->msg(
203 'listgrouprights-right-display',
204 User::getRightDescription( $right ),
205 Html::element(
206 'span',
207 array( 'class' => 'mw-listgrouprights-right-name' ),
208 $right
210 )->parse() )
214 $out->addHTML(
215 Xml::closeElement( 'ul' ) .
216 Xml::closeElement( 'td' ) .
217 Xml::closeElement( 'tr' )
220 $out->addHTML( Xml::closeElement( 'table' ) );
224 * Create a user-readable list of permissions from the given array.
226 * @param array $permissions Array of permission => bool (from $wgGroupPermissions items)
227 * @param array $revoke Array of permission => bool (from $wgRevokePermissions items)
228 * @param array $add Array of groups this group is allowed to add or true
229 * @param array $remove Array of groups this group is allowed to remove or true
230 * @param array $addSelf Array of groups this group is allowed to add to self or true
231 * @param array $removeSelf Array of group this group is allowed to remove from self or true
232 * @return string List of all granted permissions, separated by comma separator
234 private function formatPermissions( $permissions, $revoke, $add, $remove, $addSelf, $removeSelf ) {
235 $r = array();
236 foreach ( $permissions as $permission => $granted ) {
237 //show as granted only if it isn't revoked to prevent duplicate display of permissions
238 if ( $granted && ( !isset( $revoke[$permission] ) || !$revoke[$permission] ) ) {
239 $r[] = $this->msg( 'listgrouprights-right-display',
240 User::getRightDescription( $permission ),
241 '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
242 )->parse();
245 foreach ( $revoke as $permission => $revoked ) {
246 if ( $revoked ) {
247 $r[] = $this->msg( 'listgrouprights-right-revoked',
248 User::getRightDescription( $permission ),
249 '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
250 )->parse();
254 sort( $r );
256 $lang = $this->getLanguage();
257 $allGroups = User::getAllGroups();
259 $changeGroups = array(
260 'addgroup' => $add,
261 'removegroup' => $remove,
262 'addgroup-self' => $addSelf,
263 'removegroup-self' => $removeSelf
266 foreach ( $changeGroups as $messageKey => $changeGroup ) {
267 if ( $changeGroup === true ) {
268 // For grep: listgrouprights-addgroup-all, listgrouprights-removegroup-all,
269 // listgrouprights-addgroup-self-all, listgrouprights-removegroup-self-all
270 $r[] = $this->msg( 'listgrouprights-' . $messageKey . '-all' )->escaped();
271 } elseif ( is_array( $changeGroup ) ) {
272 $changeGroup = array_intersect( array_values( array_unique( $changeGroup ) ), $allGroups );
273 if ( count( $changeGroup ) ) {
274 // For grep: listgrouprights-addgroup, listgrouprights-removegroup,
275 // listgrouprights-addgroup-self, listgrouprights-removegroup-self
276 $r[] = $this->msg( 'listgrouprights-' . $messageKey,
277 $lang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $changeGroup ) ),
278 count( $changeGroup )
279 )->parse();
284 if ( empty( $r ) ) {
285 return '';
286 } else {
287 return '<ul><li>' . implode( "</li>\n<li>", $r ) . '</li></ul>';
291 protected function getGroupName() {
292 return 'users';