45 private $canonicalNamespaces =
null;
48 private $namespaceIndexes =
false;
51 private $validNamespaces =
null;
89 MainConfigNames::CanonicalNamespaceNames,
90 MainConfigNames::CapitalLinkOverrides,
91 MainConfigNames::CapitalLinks,
92 MainConfigNames::ContentNamespaces,
93 MainConfigNames::ExtraNamespaces,
94 MainConfigNames::ExtraSignatureNamespaces,
95 MainConfigNames::NamespaceContentModels,
96 MainConfigNames::NamespacesWithSubpages,
97 MainConfigNames::NonincludableNamespaces,
106 $this->options = $options;
107 $this->hookRunner =
new HookRunner( $hookContainer );
122 private function isMethodValidFor( $index, $method ) {
124 throw new MWException(
"$method does not make any sense for given namespace $index" );
138 private function makeValidNamespace( $index, $method ) {
142 || ctype_digit( $index )
144 || ( $index[0] ===
'-' && ctype_digit( substr( $index, 1 ) ) )
146 throw new InvalidArgumentException(
147 "$method called with non-integer (" . gettype( $index ) .
") namespace '$index'"
151 return intval( $index );
162 $extNamespaces = $extensionRegistry->getAttribute(
'ImmovableNamespaces' );
164 $result = $index >=
NS_MAIN && !in_array( $index, $extNamespaces );
169 $this->hookRunner->onNamespaceIsMovable( $index, $result );
181 return !$this->
isTalk( $index );
191 $index = $this->makeValidNamespace( $index, __METHOD__ );
206 $index = $this->makeValidNamespace( $index, __METHOD__ );
208 $this->isMethodValidFor( $index, __METHOD__ );
209 return $this->
isTalk( $index )
224 if ( $target->getText() ===
'' ) {
225 throw new MWException(
'Can\'t determine talk page associated with relative section link' );
228 if ( $target->getInterwiki() !==
'' ) {
229 throw new MWException(
'Can\'t determine talk page associated with interwiki link' );
232 if ( $this->
isTalk( $target->getNamespace() ) ) {
237 return new TitleValue( $this->
getTalk( $target->getNamespace() ), $target->getDBkey() );
271 $index = $this->makeValidNamespace( $index, __METHOD__ );
273 # Handle special namespaces
278 return $this->isTalk( $index )
288 if ( $this->isSubject( $target->getNamespace() ) ) {
291 return new TitleValue( $this->getSubject( $target->getNamespace() ), $target->getDBkey() );
304 $this->isMethodValidFor( $index, __METHOD__ );
306 if ( $this->isSubject( $index ) ) {
307 return $this->getTalk( $index );
309 return $this->getSubject( $index );
319 if ( $target->getText() ===
'' ) {
320 throw new MWException(
'Can\'t determine talk page associated with relative section link' );
323 if ( $target->getInterwiki() !==
'' ) {
324 throw new MWException(
'Can\'t determine talk page associated with interwiki link' );
328 $this->getAssociated( $target->getNamespace() ), $target->getDBkey() );
339 $nslist = $this->getCanonicalNamespaces();
340 return isset( $nslist[$index] );
371 return $this->getSubject( $ns1 ) == $this->getSubject( $ns2 );
381 if ( $this->canonicalNamespaces ===
null ) {
382 $this->canonicalNamespaces =
383 [
NS_MAIN =>
'' ] + $this->options->get( MainConfigNames::CanonicalNamespaceNames );
384 $this->canonicalNamespaces +=
386 if ( is_array( $this->options->get( MainConfigNames::ExtraNamespaces ) ) ) {
387 $this->canonicalNamespaces += $this->options->get( MainConfigNames::ExtraNamespaces );
389 $this->hookRunner->onCanonicalNamespaces( $this->canonicalNamespaces );
391 return $this->canonicalNamespaces;
401 $nslist = $this->getCanonicalNamespaces();
402 return $nslist[$index] ??
false;
413 if ( $this->namespaceIndexes ===
false ) {
414 $this->namespaceIndexes = [];
415 foreach ( $this->getCanonicalNamespaces() as $i => $text ) {
416 $this->namespaceIndexes[strtolower( $text )] = $i;
419 if ( array_key_exists( $name, $this->namespaceIndexes ) ) {
420 return $this->namespaceIndexes[$name];
432 if ( $this->validNamespaces ===
null ) {
433 $this->validNamespaces = [];
434 foreach ( array_keys( $this->getCanonicalNamespaces() ) as $ns ) {
436 $this->validNamespaces[] = $ns;
440 sort( $this->validNamespaces, SORT_NUMERIC );
443 return $this->validNamespaces;
465 in_array( $index, $this->options->get( MainConfigNames::ContentNamespaces ) );
476 return $this->isTalk( $index ) ||
477 in_array( $index, $this->options->get( MainConfigNames::ExtraSignatureNamespaces ) );
498 return !empty( $this->options->get( MainConfigNames::NamespacesWithSubpages )[$index] );
506 $contentNamespaces = $this->options->get( MainConfigNames::ContentNamespaces );
507 if ( !is_array( $contentNamespaces ) || $contentNamespaces === [] ) {
509 } elseif ( !in_array(
NS_MAIN, $contentNamespaces ) ) {
511 return array_merge( [
NS_MAIN ], $contentNamespaces );
513 return $contentNamespaces;
525 $this->getValidNamespaces(),
526 [ $this,
'isSubject' ]
538 $this->getValidNamespaces(),
554 $index = $this->getSubject( $index );
557 if ( in_array( $index, $this->alwaysCapitalizedNamespaces ) ) {
560 $overrides = $this->options->get( MainConfigNames::CapitalLinkOverrides );
561 if ( isset( $overrides[$index] ) ) {
563 return $overrides[$index];
566 return $this->options->get( MainConfigNames::CapitalLinks );
587 $namespaces = $this->options->get( MainConfigNames::NonincludableNamespaces );
588 return $namespaces && in_array( $index, $namespaces );
602 return $this->options->get( MainConfigNames::NamespaceContentModels )[$index] ??
null;
615 $this->isMethodValidFor( $index, __METHOD__ );
619 } elseif ( $index ==
NS_FILE ) {
634 return array_keys( self::CANONICAL_NAMES );
if(!defined('MW_SETUP_CALLBACK'))
A class containing constants representing the names of configuration variables.
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
getAssociatedPage(LinkTarget $target)
hasGenderDistinction( $index)
Does the namespace (potentially) have different aliases for different genders.
__construct(ServiceOptions $options, HookContainer $hookContainer)
equals( $ns1, $ns2)
Returns whether the specified namespaces are the same namespace.
wantSignatures( $index)
Might pages in this namespace require the use of the Signature button on the edit toolbar?
getNamespaceContentModel( $index)
Get the default content model for a namespace This does not mean that all pages in that namespace hav...
subjectEquals( $ns1, $ns2)
Returns whether the specified namespaces share the same subject.
isCapitalized( $index)
Is the namespace first-letter capitalized?
getCanonicalName( $index)
Returns the canonical (English) name for a given index.
getContentNamespaces()
Get a list of all namespace indices which are considered to contain content.
isNonincludable( $index)
It is not possible to use pages from this namespace as template?
getTalkNamespaces()
List all namespace indices which are considered talks, aka not a subject or special namespace.
hasTalkNamespace( $index)
Does this namespace ever have a talk namespace?
isSubject( $index)
Is the given namespace is a subject (non-talk) namespace?
getValidNamespaces()
Returns an array of the namespaces (by integer id) that exist on the wiki.
static getCommonNamespaces()
Retrieve the indexes for the namespaces defined by core.
exists( $index)
Returns whether the specified namespace exists.
isContent( $index)
Does this namespace contain content, for the purposes of calculating statistics, etc?
getSubjectPage(LinkTarget $target)
getCanonicalNamespaces()
Returns array of all defined namespaces with their canonical (English) names.
const CANONICAL_NAMES
Definitions of the NS_ constants are in Defines.php.
getSubject( $index)
Get the subject namespace index for a given namespace Special namespaces (NS_MEDIA,...
hasSubpages( $index)
Does the namespace allow subpages? Note that this refers to structured handling of subpages,...
canHaveTalkPage(LinkTarget $target)
Can the title have a corresponding talk page?
isWatchable( $index)
Can pages in a namespace be watched?
getTalk( $index)
Get the talk namespace index for a given namespace.
getSubjectNamespaces()
List all namespace indices which are considered subject, aka not a talk or special namespace.
isMovable( $index)
Can pages in the given namespace be moved?
getCategoryLinkType( $index)
Returns the link type to be used for categories.
getAssociated( $index)
Get the associated namespace.
getTalkPage(LinkTarget $target)
Get a LinkTarget referring to the talk page of $target.
const CONSTRUCTOR_OPTIONS
isTalk( $index)
Is the given namespace a talk namespace?
getCanonicalIndex( $name)
Returns the index for a given canonical name, or NULL The input must be converted to lower case first...
Represents a page (or page fragment) title within MediaWiki.