MediaWiki  master
NamespaceInfo Class Reference

This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them based on index. More...

Collaboration diagram for NamespaceInfo:

Public Member Functions

 __construct (ServiceOptions $options)
 
 canHaveTalkPage (LinkTarget $target)
 Can the title have a corresponding talk page? More...
 
 equals ( $ns1, $ns2)
 Returns whether the specified namespaces are the same namespace. More...
 
 exists ( $index)
 Returns whether the specified namespace exists. More...
 
 getAssociated ( $index)
 Get the associated namespace. More...
 
 getAssociatedPage (LinkTarget $target)
 
 getCanonicalIndex ( $name)
 Returns the index for a given canonical name, or NULL The input must be converted to lower case first. More...
 
 getCanonicalName ( $index)
 Returns the canonical (English) name for a given index. More...
 
 getCanonicalNamespaces ()
 Returns array of all defined namespaces with their canonical (English) names. More...
 
 getCategoryLinkType ( $index)
 Returns the link type to be used for categories. More...
 
 getContentNamespaces ()
 Get a list of all namespace indices which are considered to contain content. More...
 
 getNamespaceContentModel ( $index)
 Get the default content model for a namespace This does not mean that all pages in that namespace have the model. More...
 
 getRestrictionLevels ( $index, User $user=null)
 Determine which restriction levels it makes sense to use in a namespace, optionally filtered by a user's rights. More...
 
 getSubject ( $index)
 Get the subject namespace index for a given namespace Special namespaces (NS_MEDIA, NS_SPECIAL) are always the subject. More...
 
 getSubjectNamespaces ()
 List all namespace indices which are considered subject, aka not a talk or special namespace. More...
 
 getSubjectPage (LinkTarget $target)
 
 getTalk ( $index)
 Get the talk namespace index for a given namespace. More...
 
 getTalkNamespaces ()
 List all namespace indices which are considered talks, aka not a subject or special namespace. More...
 
 getTalkPage (LinkTarget $target)
 Get a LinkTarget referring to the talk page of $target. More...
 
 getValidNamespaces ()
 Returns an array of the namespaces (by integer id) that exist on the wiki. More...
 
 hasGenderDistinction ( $index)
 Does the namespace (potentially) have different aliases for different genders. More...
 
 hasSubpages ( $index)
 Does the namespace allow subpages? More...
 
 hasTalkNamespace ( $index)
 Does this namespace ever have a talk namespace? More...
 
 isCapitalized ( $index)
 Is the namespace first-letter capitalized? More...
 
 isContent ( $index)
 Does this namespace contain content, for the purposes of calculating statistics, etc? More...
 
 isMovable ( $index)
 Can pages in the given namespace be moved? More...
 
 isNonincludable ( $index)
 It is not possible to use pages from this namespace as template? More...
 
 isSubject ( $index)
 Is the given namespace is a subject (non-talk) namespace? More...
 
 isTalk ( $index)
 Is the given namespace a talk namespace? More...
 
 isWatchable ( $index)
 Can pages in a namespace be watched? More...
 
 subjectEquals ( $ns1, $ns2)
 Returns whether the specified namespaces share the same subject. More...
 
 wantSignatures ( $index)
 Might pages in this namespace require the use of the Signature button on the edit toolbar? More...
 

Static Public Member Functions

static getCommonNamespaces ()
 Retrieve the indexes for the namespaces defined by core. More...
 

Public Attributes

const CANONICAL_NAMES
 Definitions of the NS_ constants are in Defines.php. More...
 
const CONSTRUCTOR_OPTIONS
 
const NS_SPECIAL
 

Private Member Functions

 isMethodValidFor ( $index, $method)
 Throw an exception when trying to get the subject or talk page for a given namespace where it does not make sense. More...
 

Private Attributes

 $alwaysCapitalizedNamespaces = [ NS_SPECIAL, NS_USER, NS_MEDIAWIKI ]
 These namespaces should always be first-letter capitalized, now and forevermore. More...
 
string [] null $canonicalNamespaces = null
 Canonical namespaces cache. More...
 
array false $namespaceIndexes = false
 Canonical namespaces index cache. More...
 
ServiceOptions $options
 
int [] null $validNamespaces = null
 Valid namespaces cache. More...
 

Detailed Description

This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them based on index.

The textual names of the namespaces are handled by Language.php.

Since
1.34

Definition at line 33 of file NamespaceInfo.php.

Constructor & Destructor Documentation

◆ __construct()

NamespaceInfo::__construct ( ServiceOptions  $options)
Parameters
ServiceOptions$options

Definition at line 100 of file NamespaceInfo.php.

References $options, and MediaWiki\Config\ServiceOptions\assertRequiredOptions().

Member Function Documentation

◆ canHaveTalkPage()

NamespaceInfo::canHaveTalkPage ( LinkTarget  $target)

Can the title have a corresponding talk page?

False for relative section-only links (with getText() === ''), interwiki links (with getInterwiki() !== ''), and pages in NS_SPECIAL.

See also
getTalkPage
Parameters
LinkTarget$target
Returns
bool True if this title either is a talk page or can have a talk page associated.

Definition at line 215 of file NamespaceInfo.php.

References MediaWiki\Linker\LinkTarget\getInterwiki(), MediaWiki\Linker\LinkTarget\getNamespace(), MediaWiki\Linker\LinkTarget\getText(), and NS_MAIN.

◆ equals()

NamespaceInfo::equals (   $ns1,
  $ns2 
)

Returns whether the specified namespaces are the same namespace.

Note
It's possible that in the future we may start using something other than just namespace indexes. Under that circumstance making use of this function rather than directly doing comparison will make sure that code will not potentially break.
Parameters
int$ns1The first namespace index
int$ns2The second namespace index
Returns
bool

Definition at line 318 of file NamespaceInfo.php.

◆ exists()

NamespaceInfo::exists (   $index)

Returns whether the specified namespace exists.

Parameters
int$index
Returns
bool

Definition at line 300 of file NamespaceInfo.php.

References getCanonicalNamespaces().

◆ getAssociated()

NamespaceInfo::getAssociated (   $index)

Get the associated namespace.

For talk namespaces, returns the subject (non-talk) namespace For subject (non-talk) namespaces, returns the talk namespace

Parameters
int$indexNamespace index
Returns
int
Exceptions
MWExceptionif called on a namespace that has no talk pages (e.g., NS_SPECIAL)

Definition at line 265 of file NamespaceInfo.php.

References getSubject(), getTalk(), isMethodValidFor(), and isSubject().

Referenced by getAssociatedPage().

◆ getAssociatedPage()

NamespaceInfo::getAssociatedPage ( LinkTarget  $target)
Parameters
LinkTarget$target
Returns
LinkTarget Talk page for $target if it's a subject page, subject page if it's a talk page
Exceptions
MWExceptionif $target's namespace doesn't have talk pages (e.g., NS_SPECIAL)

Definition at line 280 of file NamespaceInfo.php.

References getAssociated(), MediaWiki\Linker\LinkTarget\getDBkey(), MediaWiki\Linker\LinkTarget\getInterwiki(), MediaWiki\Linker\LinkTarget\getNamespace(), and MediaWiki\Linker\LinkTarget\getText().

◆ getCanonicalIndex()

NamespaceInfo::getCanonicalIndex (   $name)

Returns the index for a given canonical name, or NULL The input must be converted to lower case first.

Parameters
string$nameNamespace name
Returns
int|null

Definition at line 374 of file NamespaceInfo.php.

References getCanonicalNamespaces().

◆ getCanonicalName()

NamespaceInfo::getCanonicalName (   $index)

Returns the canonical (English) name for a given index.

Parameters
int$indexNamespace index
Returns
string|bool If no canonical definition.

Definition at line 362 of file NamespaceInfo.php.

References getCanonicalNamespaces().

Referenced by MovePage\moveSubpagesInternal().

◆ getCanonicalNamespaces()

NamespaceInfo::getCanonicalNamespaces ( )

Returns array of all defined namespaces with their canonical (English) names.

Returns
string[]

Definition at line 342 of file NamespaceInfo.php.

References $canonicalNamespaces, ExtensionRegistry\getInstance(), NS_MAIN, and Hooks\run().

Referenced by exists(), getCanonicalIndex(), getCanonicalName(), and getValidNamespaces().

◆ getCategoryLinkType()

NamespaceInfo::getCategoryLinkType (   $index)

Returns the link type to be used for categories.

This determines which section of a category page titles in the namespace will appear within.

Parameters
int$indexNamespace index
Returns
string One of 'subcat', 'file', 'page'

Definition at line 592 of file NamespaceInfo.php.

References isMethodValidFor(), NS_CATEGORY, and NS_FILE.

◆ getCommonNamespaces()

static NamespaceInfo::getCommonNamespaces ( )
static

Retrieve the indexes for the namespaces defined by core.

Since
1.34
Returns
int[]

Definition at line 611 of file NamespaceInfo.php.

◆ getContentNamespaces()

NamespaceInfo::getContentNamespaces ( )

Get a list of all namespace indices which are considered to contain content.

Returns
array Array of namespace indices

Definition at line 465 of file NamespaceInfo.php.

References NS_MAIN.

◆ getNamespaceContentModel()

NamespaceInfo::getNamespaceContentModel (   $index)

Get the default content model for a namespace This does not mean that all pages in that namespace have the model.

Note
To determine the default model for a new page's main slot, or any slot in general, use SlotRoleHandler::getDefaultModel() together with SlotRoleRegistry::getRoleHandler().
Parameters
int$indexIndex to check
Returns
null|string Default model name for the given namespace, if set

Definition at line 561 of file NamespaceInfo.php.

◆ getRestrictionLevels()

NamespaceInfo::getRestrictionLevels (   $index,
User  $user = null 
)

Determine which restriction levels it makes sense to use in a namespace, optionally filtered by a user's rights.

Deprecated:
since 1.34 User PermissionManager::getNamespaceRestrictionLevels instead.
Parameters
int$indexIndex to check
User | null$userUser to check
Returns
array

Definition at line 574 of file NamespaceInfo.php.

◆ getSubject()

NamespaceInfo::getSubject (   $index)

Get the subject namespace index for a given namespace Special namespaces (NS_MEDIA, NS_SPECIAL) are always the subject.

Parameters
int$indexNamespace index
Returns
int

Definition at line 234 of file NamespaceInfo.php.

References isTalk(), and NS_MAIN.

Referenced by getAssociated(), getSubjectPage(), isCapitalized(), and subjectEquals().

◆ getSubjectNamespaces()

NamespaceInfo::getSubjectNamespaces ( )

List all namespace indices which are considered subject, aka not a talk or special namespace.

See also NamespaceInfo::isSubject

Returns
array Array of namespace indices

Definition at line 483 of file NamespaceInfo.php.

References getValidNamespaces().

◆ getSubjectPage()

NamespaceInfo::getSubjectPage ( LinkTarget  $target)
Parameters
LinkTarget$target
Returns
LinkTarget Subject page for $target

Definition at line 249 of file NamespaceInfo.php.

References MediaWiki\Linker\LinkTarget\getDBkey(), MediaWiki\Linker\LinkTarget\getNamespace(), getSubject(), and isSubject().

◆ getTalk()

NamespaceInfo::getTalk (   $index)

Get the talk namespace index for a given namespace.

Parameters
int$indexNamespace index
Returns
int
Exceptions
MWExceptionif the given namespace doesn't have an associated talk namespace (e.g. NS_SPECIAL).

Definition at line 171 of file NamespaceInfo.php.

References isMethodValidFor(), and isTalk().

Referenced by getAssociated(), and getTalkPage().

◆ getTalkNamespaces()

NamespaceInfo::getTalkNamespaces ( )

List all namespace indices which are considered talks, aka not a subject or special namespace.

See also NamespaceInfo::isTalk

Returns
array Array of namespace indices

Definition at line 496 of file NamespaceInfo.php.

References getValidNamespaces().

◆ getTalkPage()

NamespaceInfo::getTalkPage ( LinkTarget  $target)

Get a LinkTarget referring to the talk page of $target.

See also
canHaveTalkPage
Parameters
LinkTarget$target
Returns
LinkTarget Talk page for $target
Exceptions
MWExceptionif $target doesn't have talk pages, e.g. because it's in NS_SPECIAL, because it's a relative section-only link, or it's an an interwiki link.

Definition at line 187 of file NamespaceInfo.php.

References MediaWiki\Linker\LinkTarget\getDBkey(), MediaWiki\Linker\LinkTarget\getInterwiki(), MediaWiki\Linker\LinkTarget\getNamespace(), getTalk(), MediaWiki\Linker\LinkTarget\getText(), and isTalk().

◆ getValidNamespaces()

NamespaceInfo::getValidNamespaces ( )

Returns an array of the namespaces (by integer id) that exist on the wiki.

Used primarily by the API in help documentation. The array is sorted numerically and omits negative namespaces.

Returns
array

Definition at line 393 of file NamespaceInfo.php.

References $validNamespaces, and getCanonicalNamespaces().

Referenced by getSubjectNamespaces(), and getTalkNamespaces().

◆ hasGenderDistinction()

NamespaceInfo::hasGenderDistinction (   $index)

Does the namespace (potentially) have different aliases for different genders.

Not all languages make a distinction here.

Parameters
int$indexIndex to check
Returns
bool

Definition at line 536 of file NamespaceInfo.php.

References NS_USER_TALK.

◆ hasSubpages()

NamespaceInfo::hasSubpages (   $index)

Does the namespace allow subpages?

Parameters
int$indexIndex to check
Returns
bool

Definition at line 457 of file NamespaceInfo.php.

Referenced by MovePage\moveSubpagesInternal().

◆ hasTalkNamespace()

NamespaceInfo::hasTalkNamespace (   $index)

Does this namespace ever have a talk namespace?

Parameters
int$indexNamespace ID
Returns
bool True if this namespace either is or has a corresponding talk namespace.

Definition at line 414 of file NamespaceInfo.php.

References NS_MAIN.

◆ isCapitalized()

NamespaceInfo::isCapitalized (   $index)

Is the namespace first-letter capitalized?

Parameters
int$indexIndex to check
Returns
bool

Definition at line 509 of file NamespaceInfo.php.

References getSubject(), NS_FILE, and NS_MEDIA.

◆ isContent()

NamespaceInfo::isContent (   $index)

Does this namespace contain content, for the purposes of calculating statistics, etc?

Parameters
int$indexIndex to check
Returns
bool

Definition at line 425 of file NamespaceInfo.php.

References NS_MAIN.

◆ isMethodValidFor()

NamespaceInfo::isMethodValidFor (   $index,
  $method 
)
private

Throw an exception when trying to get the subject or talk page for a given namespace where it does not make sense.

Special namespaces are defined in includes/Defines.php and have a value below 0 (ex: NS_SPECIAL = -1 , NS_MEDIA = -2)

Parameters
int$index
string$method
Exceptions
MWException
Returns
bool

Definition at line 117 of file NamespaceInfo.php.

References NS_MAIN.

Referenced by getAssociated(), getCategoryLinkType(), and getTalk().

◆ isMovable()

NamespaceInfo::isMovable (   $index)

Can pages in the given namespace be moved?

Parameters
int$indexNamespace index
Returns
bool

Definition at line 130 of file NamespaceInfo.php.

References NS_FILE, NS_MAIN, and Hooks\run().

◆ isNonincludable()

NamespaceInfo::isNonincludable (   $index)

It is not possible to use pages from this namespace as template?

Parameters
int$indexIndex to check
Returns
bool

Definition at line 546 of file NamespaceInfo.php.

◆ isSubject()

NamespaceInfo::isSubject (   $index)

Is the given namespace is a subject (non-talk) namespace?

Parameters
int$indexNamespace index
Returns
bool

Definition at line 148 of file NamespaceInfo.php.

References isTalk().

Referenced by getAssociated(), and getSubjectPage().

◆ isTalk()

NamespaceInfo::isTalk (   $index)

Is the given namespace a talk namespace?

Parameters
int$indexNamespace index
Returns
bool

Definition at line 158 of file NamespaceInfo.php.

References NS_MAIN.

Referenced by getSubject(), getTalk(), getTalkPage(), isSubject(), and wantSignatures().

◆ isWatchable()

NamespaceInfo::isWatchable (   $index)

Can pages in a namespace be watched?

Parameters
int$index
Returns
bool

Definition at line 447 of file NamespaceInfo.php.

References NS_MAIN.

◆ subjectEquals()

NamespaceInfo::subjectEquals (   $ns1,
  $ns2 
)

Returns whether the specified namespaces share the same subject.

eg: NS_USER and NS_USER wil return true, as well NS_USER and NS_USER_TALK will return true.

Parameters
int$ns1The first namespace index
int$ns2The second namespace index
Returns
bool

Definition at line 332 of file NamespaceInfo.php.

References getSubject().

◆ wantSignatures()

NamespaceInfo::wantSignatures (   $index)

Might pages in this namespace require the use of the Signature button on the edit toolbar?

Parameters
int$indexIndex to check
Returns
bool

Definition at line 436 of file NamespaceInfo.php.

References isTalk().

Member Data Documentation

◆ $alwaysCapitalizedNamespaces

NamespaceInfo::$alwaysCapitalizedNamespaces = [ NS_SPECIAL, NS_USER, NS_MEDIAWIKI ]
private

These namespaces should always be first-letter capitalized, now and forevermore.

Historically, they could've probably been lowercased too, but some things are just too ingrained now. :)

Definition at line 40 of file NamespaceInfo.php.

◆ $canonicalNamespaces

string [] null NamespaceInfo::$canonicalNamespaces = null
private

Canonical namespaces cache.

Definition at line 43 of file NamespaceInfo.php.

Referenced by getCanonicalNamespaces().

◆ $namespaceIndexes

array false NamespaceInfo::$namespaceIndexes = false
private

Canonical namespaces index cache.

Definition at line 46 of file NamespaceInfo.php.

◆ $options

ServiceOptions NamespaceInfo::$options
private

Definition at line 52 of file NamespaceInfo.php.

Referenced by __construct().

◆ $validNamespaces

int [] null NamespaceInfo::$validNamespaces = null
private

Valid namespaces cache.

Definition at line 49 of file NamespaceInfo.php.

Referenced by getValidNamespaces().

◆ CANONICAL_NAMES

const NamespaceInfo::CANONICAL_NAMES
Initial value:
= [
NS_MEDIA => 'Media'

Definitions of the NS_ constants are in Defines.php.

Definition at line 59 of file NamespaceInfo.php.

◆ CONSTRUCTOR_OPTIONS

const NamespaceInfo::CONSTRUCTOR_OPTIONS
Initial value:
= [
'AllowImageMoving',
'CanonicalNamespaceNames',
'CapitalLinkOverrides',
'CapitalLinks',
'ContentNamespaces',
'ExtraNamespaces',
'ExtraSignatureNamespaces',
'NamespaceContentModels',
'NamespacesWithSubpages',
'NonincludableNamespaces',
]
Since
1.34

Definition at line 84 of file NamespaceInfo.php.

◆ NS_SPECIAL

const NamespaceInfo::NS_SPECIAL
Initial value:
=> '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',
]

Definition at line 61 of file NamespaceInfo.php.


The documentation for this class was generated from the following file: