use MediaWiki\Config\ServiceOptions;
use MediaWiki\Linker\LinkTarget;
+use MediaWiki\MediaWikiServices;
/**
* This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of
/** @var ServiceOptions */
private $options;
+ /**
+ * Definitions of the NS_ constants are in Defines.php
+ *
+ * @todo Make this const when HHVM support is dropped (T192166)
+ *
+ * @var array
+ * @internal
+ */
+ public static $canonicalNames = [
+ NS_MEDIA => 'Media',
+ NS_SPECIAL => 'Special',
+ NS_MAIN => '',
+ NS_TALK => 'Talk',
+ NS_USER => 'User',
+ NS_USER_TALK => 'User_talk',
+ NS_PROJECT => 'Project',
+ NS_PROJECT_TALK => 'Project_talk',
+ NS_FILE => 'File',
+ NS_FILE_TALK => 'File_talk',
+ NS_MEDIAWIKI => 'MediaWiki',
+ NS_MEDIAWIKI_TALK => 'MediaWiki_talk',
+ NS_TEMPLATE => 'Template',
+ NS_TEMPLATE_TALK => 'Template_talk',
+ NS_HELP => 'Help',
+ NS_HELP_TALK => 'Help_talk',
+ NS_CATEGORY => 'Category',
+ NS_CATEGORY_TALK => 'Category_talk',
+ ];
+
/**
* TODO Make this const when HHVM support is dropped (T192166)
*
'ExtraNamespaces',
'ExtraSignatureNamespaces',
'NamespaceContentModels',
- 'NamespaceProtection',
'NamespacesWithSubpages',
'NonincludableNamespaces',
- 'RestrictionLevels',
];
/**
* Returns array of all defined namespaces with their canonical
* (English) names.
*
- * @return array
+ * @return string[]
*/
public function getCanonicalNamespaces() {
if ( $this->canonicalNamespaces === null ) {
*/
public function getValidNamespaces() {
if ( is_null( $this->validNamespaces ) ) {
+ $this->validNamespaces = [];
foreach ( array_keys( $this->getCanonicalNamespaces() ) as $ns ) {
if ( $ns >= 0 ) {
$this->validNamespaces[] = $ns;
* Determine which restriction levels it makes sense to use in a namespace,
* optionally filtered by a user's rights.
*
- * @todo Move this to PermissionManager and remove the dependency here on permissions-related
- * config settings.
- *
+ * @deprecated since 1.34 User PermissionManager::getNamespaceRestrictionLevels instead.
* @param int $index Index to check
* @param User|null $user User to check
* @return array
*/
public function getRestrictionLevels( $index, User $user = null ) {
- if ( !isset( $this->options->get( 'NamespaceProtection' )[$index] ) ) {
- // All levels are valid if there's no namespace restriction.
- // But still filter by user, if necessary
- $levels = $this->options->get( 'RestrictionLevels' );
- if ( $user ) {
- $levels = array_values( array_filter( $levels, function ( $level ) use ( $user ) {
- $right = $level;
- if ( $right == 'sysop' ) {
- $right = 'editprotected'; // BC
- }
- if ( $right == 'autoconfirmed' ) {
- $right = 'editsemiprotected'; // BC
- }
- return ( $right == '' || $user->isAllowed( $right ) );
- } ) );
- }
- return $levels;
- }
-
- // $wgNamespaceProtection can require one or more rights to edit the namespace, which
- // may be satisfied by membership in multiple groups each giving a subset of those rights.
- // A restriction level is redundant if, for any one of the namespace rights, all groups
- // giving that right also give the restriction level's right. Or, conversely, a
- // restriction level is not redundant if, for every namespace right, there's at least one
- // group giving that right without the restriction level's right.
- //
- // First, for each right, get a list of groups with that right.
- $namespaceRightGroups = [];
- foreach ( (array)$this->options->get( 'NamespaceProtection' )[$index] as $right ) {
- if ( $right == 'sysop' ) {
- $right = 'editprotected'; // BC
- }
- if ( $right == 'autoconfirmed' ) {
- $right = 'editsemiprotected'; // BC
- }
- if ( $right != '' ) {
- $namespaceRightGroups[$right] = User::getGroupsWithPermission( $right );
- }
- }
-
- // Now, go through the protection levels one by one.
- $usableLevels = [ '' ];
- foreach ( $this->options->get( 'RestrictionLevels' ) as $level ) {
- $right = $level;
- if ( $right == 'sysop' ) {
- $right = 'editprotected'; // BC
- }
- if ( $right == 'autoconfirmed' ) {
- $right = 'editsemiprotected'; // BC
- }
-
- if ( $right != '' &&
- !isset( $namespaceRightGroups[$right] ) &&
- ( !$user || $user->isAllowed( $right ) )
- ) {
- // Do any of the namespace rights imply the restriction right? (see explanation above)
- foreach ( $namespaceRightGroups as $groups ) {
- if ( !array_diff( $groups, User::getGroupsWithPermission( $right ) ) ) {
- // Yes, this one does.
- continue 2;
- }
- }
- // No, keep the restriction level
- $usableLevels[] = $level;
- }
- }
-
- return $usableLevels;
+ // PermissionManager is not injected because adding an explicit dependency
+ // breaks MW installer by adding a dependency chain on the database before
+ // it was set up. Also, the method is deprecated and will be soon removed.
+ return MediaWikiServices::getInstance()
+ ->getPermissionManager()
+ ->getNamespaceRestrictionLevels( $index, $user );
}
/**
return 'page';
}
}
+
+ /**
+ * Retrieve the indexes for the namespaces defined by core.
+ *
+ * @since 1.34
+ *
+ * @return int[]
+ */
+ public static function getCommonNamespaces() {
+ return array_keys( self::$canonicalNames );
+ }
}