PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/specials/SpecialListgrouprights.php

https://bitbucket.org/andersus/querytalogo
PHP | 298 lines | 218 code | 29 blank | 51 comment | 23 complexity | 278875b3939c137734f8740f2353d89c MD5 | raw file
Possible License(s): LGPL-3.0, MPL-2.0-no-copyleft-exception, JSON, MIT, CC0-1.0, BSD-3-Clause, Apache-2.0, BSD-2-Clause, LGPL-2.1, GPL-2.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. function __construct() {
  32. parent::__construct( 'Listgrouprights' );
  33. }
  34. /**
  35. * Show the special page
  36. * @param string|null $par
  37. */
  38. public function execute( $par ) {
  39. $this->setHeaders();
  40. $this->outputHeader();
  41. $out = $this->getOutput();
  42. $out->addModuleStyles( 'mediawiki.special' );
  43. $out->wrapWikiMsg( "<div class=\"mw-listgrouprights-key\">\n$1\n</div>", 'listgrouprights-key' );
  44. $out->addHTML(
  45. Xml::openElement( 'table', [ 'class' => 'wikitable mw-listgrouprights-table' ] ) .
  46. '<tr>' .
  47. Xml::element( 'th', null, $this->msg( 'listgrouprights-group' )->text() ) .
  48. Xml::element( 'th', null, $this->msg( 'listgrouprights-rights' )->text() ) .
  49. '</tr>'
  50. );
  51. $config = $this->getConfig();
  52. $groupPermissions = $config->get( 'GroupPermissions' );
  53. $revokePermissions = $config->get( 'RevokePermissions' );
  54. $addGroups = $config->get( 'AddGroups' );
  55. $removeGroups = $config->get( 'RemoveGroups' );
  56. $groupsAddToSelf = $config->get( 'GroupsAddToSelf' );
  57. $groupsRemoveFromSelf = $config->get( 'GroupsRemoveFromSelf' );
  58. $allGroups = array_unique( array_merge(
  59. array_keys( $groupPermissions ),
  60. array_keys( $revokePermissions ),
  61. array_keys( $addGroups ),
  62. array_keys( $removeGroups ),
  63. array_keys( $groupsAddToSelf ),
  64. array_keys( $groupsRemoveFromSelf )
  65. ) );
  66. asort( $allGroups );
  67. $linkRenderer = $this->getLinkRenderer();
  68. foreach ( $allGroups as $group ) {
  69. $permissions = isset( $groupPermissions[$group] )
  70. ? $groupPermissions[$group]
  71. : [];
  72. $groupname = ( $group == '*' ) // Replace * with a more descriptive groupname
  73. ? 'all'
  74. : $group;
  75. $msg = $this->msg( 'group-' . $groupname );
  76. $groupnameLocalized = !$msg->isBlank() ? $msg->text() : $groupname;
  77. $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
  78. $grouppageLocalized = !$msg->isBlank() ?
  79. $msg->text() :
  80. MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
  81. $grouppageLocalizedTitle = Title::newFromText( $grouppageLocalized );
  82. if ( $group == '*' || !$grouppageLocalizedTitle ) {
  83. // Do not make a link for the generic * group or group with invalid group page
  84. $grouppage = htmlspecialchars( $groupnameLocalized );
  85. } else {
  86. $grouppage = $linkRenderer->makeLink(
  87. $grouppageLocalizedTitle,
  88. $groupnameLocalized
  89. );
  90. }
  91. if ( $group === 'user' ) {
  92. // Link to Special:listusers for implicit group 'user'
  93. $grouplink = '<br />' . $linkRenderer->makeKnownLink(
  94. SpecialPage::getTitleFor( 'Listusers' ),
  95. $this->msg( 'listgrouprights-members' )->text()
  96. );
  97. } elseif ( !in_array( $group, $config->get( 'ImplicitGroups' ) ) ) {
  98. $grouplink = '<br />' . $linkRenderer->makeKnownLink(
  99. SpecialPage::getTitleFor( 'Listusers' ),
  100. $this->msg( 'listgrouprights-members' )->text(),
  101. [],
  102. [ 'group' => $group ]
  103. );
  104. } else {
  105. // No link to Special:listusers for other implicit groups as they are unlistable
  106. $grouplink = '';
  107. }
  108. $revoke = isset( $revokePermissions[$group] ) ? $revokePermissions[$group] : [];
  109. $addgroups = isset( $addGroups[$group] ) ? $addGroups[$group] : [];
  110. $removegroups = isset( $removeGroups[$group] ) ? $removeGroups[$group] : [];
  111. $addgroupsSelf = isset( $groupsAddToSelf[$group] ) ? $groupsAddToSelf[$group] : [];
  112. $removegroupsSelf = isset( $groupsRemoveFromSelf[$group] )
  113. ? $groupsRemoveFromSelf[$group]
  114. : [];
  115. $id = $group == '*' ? false : Sanitizer::escapeIdForAttribute( $group );
  116. $out->addHTML( Html::rawElement( 'tr', [ 'id' => $id ], "
  117. <td>$grouppage$grouplink</td>
  118. <td>" .
  119. $this->formatPermissions( $permissions, $revoke, $addgroups, $removegroups,
  120. $addgroupsSelf, $removegroupsSelf ) .
  121. '</td>
  122. '
  123. ) );
  124. }
  125. $out->addHTML( Xml::closeElement( 'table' ) );
  126. $this->outputNamespaceProtectionInfo();
  127. }
  128. private function outputNamespaceProtectionInfo() {
  129. global $wgParser, $wgContLang;
  130. $out = $this->getOutput();
  131. $namespaceProtection = $this->getConfig()->get( 'NamespaceProtection' );
  132. if ( count( $namespaceProtection ) == 0 ) {
  133. return;
  134. }
  135. $header = $this->msg( 'listgrouprights-namespaceprotection-header' )->parse();
  136. $out->addHTML(
  137. Html::rawElement( 'h2', [], Html::element( 'span', [
  138. 'class' => 'mw-headline',
  139. 'id' => $wgParser->guessSectionNameFromWikiText( $header )
  140. ], $header ) ) .
  141. Xml::openElement( 'table', [ 'class' => 'wikitable' ] ) .
  142. Html::element(
  143. 'th',
  144. [],
  145. $this->msg( 'listgrouprights-namespaceprotection-namespace' )->text()
  146. ) .
  147. Html::element(
  148. 'th',
  149. [],
  150. $this->msg( 'listgrouprights-namespaceprotection-restrictedto' )->text()
  151. )
  152. );
  153. $linkRenderer = $this->getLinkRenderer();
  154. ksort( $namespaceProtection );
  155. foreach ( $namespaceProtection as $namespace => $rights ) {
  156. if ( !in_array( $namespace, MWNamespace::getValidNamespaces() ) ) {
  157. continue;
  158. }
  159. if ( $namespace == NS_MAIN ) {
  160. $namespaceText = $this->msg( 'blanknamespace' )->text();
  161. } else {
  162. $namespaceText = $wgContLang->convertNamespace( $namespace );
  163. }
  164. $out->addHTML(
  165. Xml::openElement( 'tr' ) .
  166. Html::rawElement(
  167. 'td',
  168. [],
  169. $linkRenderer->makeLink(
  170. SpecialPage::getTitleFor( 'Allpages' ),
  171. $namespaceText,
  172. [],
  173. [ 'namespace' => $namespace ]
  174. )
  175. ) .
  176. Xml::openElement( 'td' ) . Xml::openElement( 'ul' )
  177. );
  178. if ( !is_array( $rights ) ) {
  179. $rights = [ $rights ];
  180. }
  181. foreach ( $rights as $right ) {
  182. $out->addHTML(
  183. Html::rawElement( 'li', [], $this->msg(
  184. 'listgrouprights-right-display',
  185. User::getRightDescription( $right ),
  186. Html::element(
  187. 'span',
  188. [ 'class' => 'mw-listgrouprights-right-name' ],
  189. $right
  190. )
  191. )->parse() )
  192. );
  193. }
  194. $out->addHTML(
  195. Xml::closeElement( 'ul' ) .
  196. Xml::closeElement( 'td' ) .
  197. Xml::closeElement( 'tr' )
  198. );
  199. }
  200. $out->addHTML( Xml::closeElement( 'table' ) );
  201. }
  202. /**
  203. * Create a user-readable list of permissions from the given array.
  204. *
  205. * @param array $permissions Array of permission => bool (from $wgGroupPermissions items)
  206. * @param array $revoke Array of permission => bool (from $wgRevokePermissions items)
  207. * @param array $add Array of groups this group is allowed to add or true
  208. * @param array $remove Array of groups this group is allowed to remove or true
  209. * @param array $addSelf Array of groups this group is allowed to add to self or true
  210. * @param array $removeSelf Array of group this group is allowed to remove from self or true
  211. * @return string List of all granted permissions, separated by comma separator
  212. */
  213. private function formatPermissions( $permissions, $revoke, $add, $remove, $addSelf, $removeSelf ) {
  214. $r = [];
  215. foreach ( $permissions as $permission => $granted ) {
  216. // show as granted only if it isn't revoked to prevent duplicate display of permissions
  217. if ( $granted && ( !isset( $revoke[$permission] ) || !$revoke[$permission] ) ) {
  218. $r[] = $this->msg( 'listgrouprights-right-display',
  219. User::getRightDescription( $permission ),
  220. '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
  221. )->parse();
  222. }
  223. }
  224. foreach ( $revoke as $permission => $revoked ) {
  225. if ( $revoked ) {
  226. $r[] = $this->msg( 'listgrouprights-right-revoked',
  227. User::getRightDescription( $permission ),
  228. '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
  229. )->parse();
  230. }
  231. }
  232. sort( $r );
  233. $lang = $this->getLanguage();
  234. $allGroups = User::getAllGroups();
  235. $changeGroups = [
  236. 'addgroup' => $add,
  237. 'removegroup' => $remove,
  238. 'addgroup-self' => $addSelf,
  239. 'removegroup-self' => $removeSelf
  240. ];
  241. foreach ( $changeGroups as $messageKey => $changeGroup ) {
  242. if ( $changeGroup === true ) {
  243. // For grep: listgrouprights-addgroup-all, listgrouprights-removegroup-all,
  244. // listgrouprights-addgroup-self-all, listgrouprights-removegroup-self-all
  245. $r[] = $this->msg( 'listgrouprights-' . $messageKey . '-all' )->escaped();
  246. } elseif ( is_array( $changeGroup ) ) {
  247. $changeGroup = array_intersect( array_values( array_unique( $changeGroup ) ), $allGroups );
  248. if ( count( $changeGroup ) ) {
  249. $groupLinks = [];
  250. foreach ( $changeGroup as $group ) {
  251. $groupLinks[] = UserGroupMembership::getLink( $group, $this->getContext(), 'wiki' );
  252. }
  253. // For grep: listgrouprights-addgroup, listgrouprights-removegroup,
  254. // listgrouprights-addgroup-self, listgrouprights-removegroup-self
  255. $r[] = $this->msg( 'listgrouprights-' . $messageKey,
  256. $lang->listToText( $groupLinks ), count( $changeGroup ) )->parse();
  257. }
  258. }
  259. }
  260. if ( empty( $r ) ) {
  261. return '';
  262. } else {
  263. return '<ul><li>' . implode( "</li>\n<li>", $r ) . '</li></ul>';
  264. }
  265. }
  266. protected function getGroupName() {
  267. return 'users';
  268. }
  269. }