| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Specials; |
| 22 |
|
| 23 |
use MediaWiki\Html\Html; |
| 24 |
use MediaWiki\Language\ILanguageConverter; |
| 25 |
use MediaWiki\Languages\LanguageConverterFactory; |
| 26 |
use MediaWiki\MainConfigNames; |
| 27 |
use MediaWiki\Parser\Sanitizer; |
| 28 |
use MediaWiki\Permissions\GroupPermissionsLookup; |
| 29 |
use MediaWiki\SpecialPage\SpecialPage; |
| 30 |
use MediaWiki\Title\NamespaceInfo; |
| 31 |
use MediaWiki\Title\Title; |
| 32 |
use MediaWiki\User\User; |
| 33 |
use MediaWiki\User\UserGroupManager; |
| 34 |
use MediaWiki\User\UserGroupMembership; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
class SpecialListGroupRights extends SpecialPage { |
| 45 |
|
| 46 |
private NamespaceInfo $nsInfo; |
| 47 |
private UserGroupManager $userGroupManager; |
| 48 |
private ILanguageConverter $languageConverter; |
| 49 |
private GroupPermissionsLookup $groupPermissionsLookup; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function __construct( |
| 58 |
NamespaceInfo $nsInfo, |
| 59 |
UserGroupManager $userGroupManager, |
| 60 |
LanguageConverterFactory $languageConverterFactory, |
| 61 |
GroupPermissionsLookup $groupPermissionsLookup |
| 62 |
) { |
| 63 |
parent::__construct( 'Listgrouprights' ); |
| 64 |
$this->nsInfo = $nsInfo; |
| 65 |
$this->userGroupManager = $userGroupManager; |
| 66 |
$this->languageConverter = $languageConverterFactory->getLanguageConverter( $this->getContentLanguage() ); |
| 67 |
$this->groupPermissionsLookup = $groupPermissionsLookup; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public function execute( $par ) { |
| 75 |
$this->setHeaders(); |
| 76 |
$this->outputHeader(); |
| 77 |
|
| 78 |
$out = $this->getOutput(); |
| 79 |
$out->addModuleStyles( 'mediawiki.special' ); |
| 80 |
$this->addHelpLink( 'Help:User_rights_and_groups' ); |
| 81 |
|
| 82 |
$out->wrapWikiMsg( "<div class=\"mw-listgrouprights-key\">\n$1\n</div>", 'listgrouprights-key' ); |
| 83 |
|
| 84 |
$out->addHTML( |
| 85 |
Html::openElement( 'table', [ 'class' => 'wikitable mw-listgrouprights-table' ] ) . |
| 86 |
'<tr>' . |
| 87 |
Html::element( 'th', [], $this->msg( 'listgrouprights-group' )->text() ) . |
| 88 |
Html::element( 'th', [], $this->msg( 'listgrouprights-rights' )->text() ) . |
| 89 |
'</tr>' |
| 90 |
); |
| 91 |
|
| 92 |
$config = $this->getConfig(); |
| 93 |
$addGroups = $config->get( MainConfigNames::AddGroups ); |
| 94 |
$removeGroups = $config->get( MainConfigNames::RemoveGroups ); |
| 95 |
$groupsAddToSelf = $config->get( MainConfigNames::GroupsAddToSelf ); |
| 96 |
$groupsRemoveFromSelf = $config->get( MainConfigNames::GroupsRemoveFromSelf ); |
| 97 |
$allGroups = array_merge( |
| 98 |
$this->userGroupManager->listAllGroups(), |
| 99 |
$this->userGroupManager->listAllImplicitGroups() |
| 100 |
); |
| 101 |
asort( $allGroups ); |
| 102 |
|
| 103 |
$linkRenderer = $this->getLinkRenderer(); |
| 104 |
$lang = $this->getLanguage(); |
| 105 |
|
| 106 |
foreach ( $allGroups as $group ) { |
| 107 |
$permissions = $this->groupPermissionsLookup->getGrantedPermissions( $group ); |
| 108 |
$groupname = ( $group == '*' ) |
| 109 |
? 'all' |
| 110 |
: $group; |
| 111 |
|
| 112 |
$groupnameLocalized = $lang->getGroupName( $groupname ); |
| 113 |
|
| 114 |
$grouppageLocalizedTitle = UserGroupMembership::getGroupPage( $groupname ) |
| 115 |
?: Title::makeTitleSafe( NS_PROJECT, $groupname ); |
| 116 |
|
| 117 |
if ( $group == '*' || !$grouppageLocalizedTitle ) { |
| 118 |
|
| 119 |
$grouppage = htmlspecialchars( $groupnameLocalized ); |
| 120 |
} else { |
| 121 |
$grouppage = $linkRenderer->makeLink( |
| 122 |
$grouppageLocalizedTitle, |
| 123 |
$groupnameLocalized |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
$groupWithParentheses = $this->msg( 'parentheses' )->rawParams( $group )->escaped(); |
| 128 |
$groupname = "<br /><code>$groupWithParentheses</code>"; |
| 129 |
|
| 130 |
if ( $group === 'user' ) { |
| 131 |
|
| 132 |
$grouplink = '<br />' . $linkRenderer->makeKnownLink( |
| 133 |
SpecialPage::getTitleFor( 'Listusers' ), |
| 134 |
$this->msg( 'listgrouprights-members' )->text() |
| 135 |
); |
| 136 |
} elseif ( !in_array( $group, $config->get( MainConfigNames::ImplicitGroups ) ) ) { |
| 137 |
$grouplink = '<br />' . $linkRenderer->makeKnownLink( |
| 138 |
SpecialPage::getTitleFor( 'Listusers' ), |
| 139 |
$this->msg( 'listgrouprights-members' )->text(), |
| 140 |
[], |
| 141 |
[ 'group' => $group ] |
| 142 |
); |
| 143 |
} else { |
| 144 |
|
| 145 |
$grouplink = ''; |
| 146 |
} |
| 147 |
|
| 148 |
$revoke = $this->groupPermissionsLookup->getRevokedPermissions( $group ); |
| 149 |
$addgroups = $addGroups[$group] ?? []; |
| 150 |
$removegroups = $removeGroups[$group] ?? []; |
| 151 |
$addgroupsSelf = $groupsAddToSelf[$group] ?? []; |
| 152 |
$removegroupsSelf = $groupsRemoveFromSelf[$group] ?? []; |
| 153 |
|
| 154 |
$id = $group == '*' ? false : Sanitizer::escapeIdForAttribute( $group ); |
| 155 |
$out->addHTML( Html::rawElement( 'tr', [ 'id' => $id ], " |
| 156 |
<td>$grouppage$groupname$grouplink</td> |
| 157 |
<td>" . |
| 158 |
$this->formatPermissions( $permissions, $revoke, $addgroups, $removegroups, |
| 159 |
$addgroupsSelf, $removegroupsSelf ) . |
| 160 |
'</td> |
| 161 |
' |
| 162 |
) ); |
| 163 |
} |
| 164 |
$out->addHTML( Html::closeElement( 'table' ) ); |
| 165 |
$this->outputNamespaceProtectionInfo(); |
| 166 |
} |
| 167 |
|
| 168 |
private function outputNamespaceProtectionInfo() { |
| 169 |
$out = $this->getOutput(); |
| 170 |
$namespaceProtection = $this->getConfig()->get( MainConfigNames::NamespaceProtection ); |
| 171 |
|
| 172 |
if ( count( $namespaceProtection ) == 0 ) { |
| 173 |
return; |
| 174 |
} |
| 175 |
|
| 176 |
$header = $this->msg( 'listgrouprights-namespaceprotection-header' )->text(); |
| 177 |
$out->addHTML( |
| 178 |
Html::element( 'h2', [ |
| 179 |
'id' => Sanitizer::escapeIdForAttribute( $header ) |
| 180 |
], $header ) . |
| 181 |
Html::openElement( 'table', [ 'class' => 'wikitable' ] ) . |
| 182 |
Html::element( |
| 183 |
'th', |
| 184 |
[], |
| 185 |
$this->msg( 'listgrouprights-namespaceprotection-namespace' )->text() |
| 186 |
) . |
| 187 |
Html::element( |
| 188 |
'th', |
| 189 |
[], |
| 190 |
$this->msg( 'listgrouprights-namespaceprotection-restrictedto' )->text() |
| 191 |
) |
| 192 |
); |
| 193 |
$linkRenderer = $this->getLinkRenderer(); |
| 194 |
ksort( $namespaceProtection ); |
| 195 |
$validNamespaces = $this->nsInfo->getValidNamespaces(); |
| 196 |
foreach ( $namespaceProtection as $namespace => $rights ) { |
| 197 |
if ( !in_array( $namespace, $validNamespaces ) ) { |
| 198 |
continue; |
| 199 |
} |
| 200 |
|
| 201 |
if ( $namespace == NS_MAIN ) { |
| 202 |
$namespaceText = $this->msg( 'blanknamespace' )->text(); |
| 203 |
} else { |
| 204 |
$namespaceText = $this->languageConverter->convertNamespace( $namespace ); |
| 205 |
} |
| 206 |
|
| 207 |
$out->addHTML( |
| 208 |
Html::openElement( 'tr' ) . |
| 209 |
Html::rawElement( |
| 210 |
'td', |
| 211 |
[], |
| 212 |
$linkRenderer->makeLink( |
| 213 |
SpecialPage::getTitleFor( 'Allpages' ), |
| 214 |
$namespaceText, |
| 215 |
[], |
| 216 |
[ 'namespace' => $namespace ] |
| 217 |
) |
| 218 |
) . |
| 219 |
Html::openElement( 'td' ) . Html::openElement( 'ul' ) |
| 220 |
); |
| 221 |
|
| 222 |
if ( !is_array( $rights ) ) { |
| 223 |
$rights = [ $rights ]; |
| 224 |
} |
| 225 |
|
| 226 |
foreach ( $rights as $right ) { |
| 227 |
$out->addHTML( Html::rawElement( 'li', [], |
| 228 |
$this->msg( 'listgrouprights-right-display' ) |
| 229 |
->params( User::getRightDescription( $right ) ) |
| 230 |
->rawParams( Html::element( |
| 231 |
'span', |
| 232 |
[ 'class' => 'mw-listgrouprights-right-name' ], |
| 233 |
$right |
| 234 |
) )->parse() |
| 235 |
) ); |
| 236 |
} |
| 237 |
|
| 238 |
$out->addHTML( |
| 239 |
Html::closeElement( 'ul' ) . |
| 240 |
Html::closeElement( 'td' ) . |
| 241 |
Html::closeElement( 'tr' ) |
| 242 |
); |
| 243 |
} |
| 244 |
$out->addHTML( Html::closeElement( 'table' ) ); |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
private function formatPermissions( $permissions, $revoke, $add, $remove, $addSelf, $removeSelf ) { |
| 259 |
$r = []; |
| 260 |
foreach ( $permissions as $permission ) { |
| 261 |
|
| 262 |
if ( !isset( $revoke[$permission] ) || !$revoke[$permission] ) { |
| 263 |
$r[] = $this->msg( 'listgrouprights-right-display' ) |
| 264 |
->params( User::getRightDescription( $permission ) ) |
| 265 |
->rawParams( Html::element( |
| 266 |
'span', |
| 267 |
[ 'class' => 'mw-listgrouprights-right-name' ], |
| 268 |
$permission |
| 269 |
) )->parse(); |
| 270 |
} |
| 271 |
} |
| 272 |
foreach ( $revoke as $permission ) { |
| 273 |
$r[] = $this->msg( 'listgrouprights-right-revoked' ) |
| 274 |
->params( User::getRightDescription( $permission ) ) |
| 275 |
->rawParams( Html::element( |
| 276 |
'span', |
| 277 |
[ 'class' => 'mw-listgrouprights-right-name' ], |
| 278 |
$permission |
| 279 |
) )->parse(); |
| 280 |
} |
| 281 |
|
| 282 |
sort( $r ); |
| 283 |
|
| 284 |
$lang = $this->getLanguage(); |
| 285 |
$allGroups = $this->userGroupManager->listAllGroups(); |
| 286 |
|
| 287 |
$changeGroups = [ |
| 288 |
'addgroup' => $add, |
| 289 |
'removegroup' => $remove, |
| 290 |
'addgroup-self' => $addSelf, |
| 291 |
'removegroup-self' => $removeSelf |
| 292 |
]; |
| 293 |
|
| 294 |
foreach ( $changeGroups as $messageKey => $changeGroup ) { |
| 295 |
|
| 296 |
if ( $changeGroup === true ) { |
| 297 |
|
| 298 |
|
| 299 |
$r[] = $this->msg( 'listgrouprights-' . $messageKey . '-all' )->escaped(); |
| 300 |
} elseif ( is_array( $changeGroup ) ) { |
| 301 |
$changeGroup = array_intersect( array_values( array_unique( $changeGroup ) ), $allGroups ); |
| 302 |
if ( count( $changeGroup ) ) { |
| 303 |
$groupLinks = []; |
| 304 |
foreach ( $changeGroup as $group ) { |
| 305 |
$groupLinks[] = UserGroupMembership::getLinkWiki( $group, $this->getContext() ); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
$r[] = $this->msg( 'listgrouprights-' . $messageKey, |
| 310 |
$lang->listToText( $groupLinks ), count( $changeGroup ) )->parse(); |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
if ( !$r ) { |
| 316 |
return ''; |
| 317 |
} else { |
| 318 |
return '<ul><li>' . implode( "</li>\n<li>", $r ) . '</li></ul>'; |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
protected function getGroupName() { |
| 323 |
return 'users'; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
class_alias( SpecialListGroupRights::class, 'SpecialListGroupRights' ); |