Support offsets in prefix searching
[mediawiki.git] / includes / specials / SpecialListgrouprights.php
blob8b9a0ee450bc08119545ba1325e8e457d7176066
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;
90 if ( $group == '*' ) {
91 // Do not make a link for the generic * group
92 $grouppage = htmlspecialchars( $groupnameLocalized );
93 } else {
94 $grouppage = Linker::link(
95 Title::newFromText( $grouppageLocalized ),
96 htmlspecialchars( $groupnameLocalized )
100 if ( $group === 'user' ) {
101 // Link to Special:listusers for implicit group 'user'
102 $grouplink = '<br />' . Linker::linkKnown(
103 SpecialPage::getTitleFor( 'Listusers' ),
104 $this->msg( 'listgrouprights-members' )->escaped()
106 } elseif ( !in_array( $group, $config->get( 'ImplicitGroups' ) ) ) {
107 $grouplink = '<br />' . Linker::linkKnown(
108 SpecialPage::getTitleFor( 'Listusers' ),
109 $this->msg( 'listgrouprights-members' )->escaped(),
110 array(),
111 array( 'group' => $group )
113 } else {
114 // No link to Special:listusers for other implicit groups as they are unlistable
115 $grouplink = '';
118 $revoke = isset( $revokePermissions[$group] ) ? $revokePermissions[$group] : array();
119 $addgroups = isset( $addGroups[$group] ) ? $addGroups[$group] : array();
120 $removegroups = isset( $removeGroups[$group] ) ? $removeGroups[$group] : array();
121 $addgroupsSelf = isset( $groupsAddToSelf[$group] ) ? $groupsAddToSelf[$group] : array();
122 $removegroupsSelf = isset( $groupsRemoveFromSelf[$group] )
123 ? $groupsRemoveFromSelf[$group]
124 : array();
126 $id = $group == '*' ? false : Sanitizer::escapeId( $group );
127 $out->addHTML( Html::rawElement( 'tr', array( 'id' => $id ), "
128 <td>$grouppage$grouplink</td>
129 <td>" .
130 $this->formatPermissions( $permissions, $revoke, $addgroups, $removegroups,
131 $addgroupsSelf, $removegroupsSelf ) .
132 '</td>
134 ) );
136 $out->addHTML( Xml::closeElement( 'table' ) );
137 $this->outputNamespaceProtectionInfo();
140 private function outputNamespaceProtectionInfo() {
141 global $wgParser, $wgContLang;
142 $out = $this->getOutput();
143 $namespaceProtection = $this->getConfig()->get( 'NamespaceProtection' );
145 if ( count( $namespaceProtection ) == 0 ) {
146 return;
149 $header = $this->msg( 'listgrouprights-namespaceprotection-header' )->parse();
150 $out->addHTML(
151 Html::rawElement( 'h2', array(), Html::element( 'span', array(
152 'class' => 'mw-headline',
153 'id' => $wgParser->guessSectionNameFromWikiText( $header )
154 ), $header ) ) .
155 Xml::openElement( 'table', array( 'class' => 'wikitable' ) ) .
156 Html::element(
157 'th',
158 array(),
159 $this->msg( 'listgrouprights-namespaceprotection-namespace' )->text()
161 Html::element(
162 'th',
163 array(),
164 $this->msg( 'listgrouprights-namespaceprotection-restrictedto' )->text()
168 ksort( $namespaceProtection );
169 foreach ( $namespaceProtection as $namespace => $rights ) {
170 if ( !in_array( $namespace, MWNamespace::getValidNamespaces() ) ) {
171 continue;
174 if ( $namespace == NS_MAIN ) {
175 $namespaceText = $this->msg( 'blanknamespace' )->text();
176 } else {
177 $namespaceText = $wgContLang->convertNamespace( $namespace );
180 $out->addHTML(
181 Xml::openElement( 'tr' ) .
182 Html::rawElement(
183 'td',
184 array(),
185 Linker::link(
186 SpecialPage::getTitleFor( 'Allpages' ),
187 $namespaceText,
188 array(),
189 array( 'namespace' => $namespace )
192 Xml::openElement( 'td' ) . Xml::openElement( 'ul' )
195 if ( !is_array( $rights ) ) {
196 $rights = array( $rights );
199 foreach ( $rights as $right ) {
200 $out->addHTML(
201 Html::rawElement( 'li', array(), $this->msg(
202 'listgrouprights-right-display',
203 User::getRightDescription( $right ),
204 Html::element(
205 'span',
206 array( 'class' => 'mw-listgrouprights-right-name' ),
207 $right
209 )->parse() )
213 $out->addHTML(
214 Xml::closeElement( 'ul' ) .
215 Xml::closeElement( 'td' ) .
216 Xml::closeElement( 'tr' )
219 $out->addHTML( Xml::closeElement( 'table' ) );
223 * Create a user-readable list of permissions from the given array.
225 * @param array $permissions Array of permission => bool (from $wgGroupPermissions items)
226 * @param array $revoke Array of permission => bool (from $wgRevokePermissions items)
227 * @param array $add Array of groups this group is allowed to add or true
228 * @param array $remove Array of groups this group is allowed to remove or true
229 * @param array $addSelf Array of groups this group is allowed to add to self or true
230 * @param array $removeSelf Array of group this group is allowed to remove from self or true
231 * @return string List of all granted permissions, separated by comma separator
233 private function formatPermissions( $permissions, $revoke, $add, $remove, $addSelf, $removeSelf ) {
234 $r = array();
235 foreach ( $permissions as $permission => $granted ) {
236 //show as granted only if it isn't revoked to prevent duplicate display of permissions
237 if ( $granted && ( !isset( $revoke[$permission] ) || !$revoke[$permission] ) ) {
238 $r[] = $this->msg( 'listgrouprights-right-display',
239 User::getRightDescription( $permission ),
240 '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
241 )->parse();
244 foreach ( $revoke as $permission => $revoked ) {
245 if ( $revoked ) {
246 $r[] = $this->msg( 'listgrouprights-right-revoked',
247 User::getRightDescription( $permission ),
248 '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
249 )->parse();
253 sort( $r );
255 $lang = $this->getLanguage();
256 $allGroups = User::getAllGroups();
258 $changeGroups = array(
259 'addgroup' => $add,
260 'removegroup' => $remove,
261 'addgroup-self' => $addSelf,
262 'removegroup-self' => $removeSelf
265 foreach ( $changeGroups as $messageKey => $changeGroup ) {
266 if ( $changeGroup === true ) {
267 // For grep: listgrouprights-addgroup-all, listgrouprights-removegroup-all,
268 // listgrouprights-addgroup-self-all, listgrouprights-removegroup-self-all
269 $r[] = $this->msg( 'listgrouprights-' . $messageKey . '-all' )->escaped();
270 } elseif ( is_array( $changeGroup ) ) {
271 $changeGroup = array_intersect( array_values( array_unique( $changeGroup ) ), $allGroups );
272 if ( count( $changeGroup ) ) {
273 // For grep: listgrouprights-addgroup, listgrouprights-removegroup,
274 // listgrouprights-addgroup-self, listgrouprights-removegroup-self
275 $r[] = $this->msg( 'listgrouprights-' . $messageKey,
276 $lang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $changeGroup ) ),
277 count( $changeGroup )
278 )->parse();
283 if ( empty( $r ) ) {
284 return '';
285 } else {
286 return '<ul><li>' . implode( "</li>\n<li>", $r ) . '</li></ul>';
290 protected function getGroupName() {
291 return 'users';