PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/specials/SpecialListgrouprights.php

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