46 private $canonicalNamespaces =
null;
49 private $namespaceIndexes =
false;
52 private $validNamespaces =
null;
90 MainConfigNames::CanonicalNamespaceNames,
91 MainConfigNames::CapitalLinkOverrides,
92 MainConfigNames::CapitalLinks,
93 MainConfigNames::ContentNamespaces,
94 MainConfigNames::ExtraNamespaces,
95 MainConfigNames::ExtraSignatureNamespaces,
96 MainConfigNames::NamespaceContentModels,
97 MainConfigNames::NamespacesWithSubpages,
98 MainConfigNames::NonincludableNamespaces,
107 $this->options = $options;
108 $this->hookRunner =
new HookRunner( $hookContainer );
123 private function isMethodValidFor( $index, $method ) {
125 throw new MWException(
"$method does not make any sense for given namespace $index" );
139 private function makeValidNamespace( $index, $method ) {
143 || ctype_digit( $index )
145 || ( $index[0] ===
'-' && ctype_digit( substr( $index, 1 ) ) )
147 throw new InvalidArgumentException(
148 "$method called with non-integer (" . gettype( $index ) .
") namespace '$index'"
152 return intval( $index );
162 $extensionRegistry = ExtensionRegistry::getInstance();
163 $extNamespaces = $extensionRegistry->getAttribute(
'ImmovableNamespaces' );
165 $result = $index >=
NS_MAIN && !in_array( $index, $extNamespaces );
170 $this->hookRunner->onNamespaceIsMovable( $index, $result );
182 return !$this->
isTalk( $index );
192 $index = $this->makeValidNamespace( $index, __METHOD__ );
207 $index = $this->makeValidNamespace( $index, __METHOD__ );
209 $this->isMethodValidFor( $index, __METHOD__ );
210 return $this->
isTalk( $index )
225 if ( $target->getText() ===
'' ) {
226 throw new MWException(
'Can\'t determine talk page associated with relative section link' );
229 if ( $target->getInterwiki() !==
'' ) {
230 throw new MWException(
'Can\'t determine talk page associated with interwiki link' );
233 if ( $this->
isTalk( $target->getNamespace() ) ) {
238 return new TitleValue( $this->
getTalk( $target->getNamespace() ), $target->getDBkey() );
272 $index = $this->makeValidNamespace( $index, __METHOD__ );
274 # Handle special namespaces
279 return $this->isTalk( $index )
289 if ( $this->isSubject( $target->getNamespace() ) ) {
292 return new TitleValue( $this->getSubject( $target->getNamespace() ), $target->getDBkey() );
305 $this->isMethodValidFor( $index, __METHOD__ );
307 if ( $this->isSubject( $index ) ) {
308 return $this->getTalk( $index );
310 return $this->getSubject( $index );
320 if ( $target->getText() ===
'' ) {
321 throw new MWException(
'Can\'t determine talk page associated with relative section link' );
324 if ( $target->getInterwiki() !==
'' ) {
325 throw new MWException(
'Can\'t determine talk page associated with interwiki link' );
329 $this->getAssociated( $target->getNamespace() ), $target->getDBkey() );
340 $nslist = $this->getCanonicalNamespaces();
341 return isset( $nslist[$index] );
372 return $this->getSubject( $ns1 ) == $this->getSubject( $ns2 );
382 if ( $this->canonicalNamespaces ===
null ) {
383 $this->canonicalNamespaces =
384 [
NS_MAIN =>
'' ] + $this->options->get( MainConfigNames::CanonicalNamespaceNames );
385 $this->canonicalNamespaces +=
386 ExtensionRegistry::getInstance()->getAttribute(
'ExtensionNamespaces' );
387 if ( is_array( $this->options->get( MainConfigNames::ExtraNamespaces ) ) ) {
388 $this->canonicalNamespaces += $this->options->get( MainConfigNames::ExtraNamespaces );
390 $this->hookRunner->onCanonicalNamespaces( $this->canonicalNamespaces );
392 return $this->canonicalNamespaces;
402 $nslist = $this->getCanonicalNamespaces();
403 return $nslist[$index] ??
false;
414 if ( $this->namespaceIndexes ===
false ) {
415 $this->namespaceIndexes = [];
416 foreach ( $this->getCanonicalNamespaces() as $i => $text ) {
417 $this->namespaceIndexes[strtolower( $text )] = $i;
420 if ( array_key_exists( $name, $this->namespaceIndexes ) ) {
421 return $this->namespaceIndexes[$name];
433 if ( $this->validNamespaces ===
null ) {
434 $this->validNamespaces = [];
435 foreach ( array_keys( $this->getCanonicalNamespaces() ) as $ns ) {
437 $this->validNamespaces[] = $ns;
441 sort( $this->validNamespaces, SORT_NUMERIC );
444 return $this->validNamespaces;
466 in_array( $index, $this->options->get( MainConfigNames::ContentNamespaces ) );
477 return $this->isTalk( $index ) ||
478 in_array( $index, $this->options->get( MainConfigNames::ExtraSignatureNamespaces ) );
499 return !empty( $this->options->get( MainConfigNames::NamespacesWithSubpages )[$index] );
507 $contentNamespaces = $this->options->get( MainConfigNames::ContentNamespaces );
508 if ( !is_array( $contentNamespaces ) || $contentNamespaces === [] ) {
510 } elseif ( !in_array(
NS_MAIN, $contentNamespaces ) ) {
512 return array_merge( [
NS_MAIN ], $contentNamespaces );
514 return $contentNamespaces;
526 $this->getValidNamespaces(),
527 [ $this,
'isSubject' ]
539 $this->getValidNamespaces(),
555 $index = $this->getSubject( $index );
558 if ( in_array( $index, $this->alwaysCapitalizedNamespaces ) ) {
561 $overrides = $this->options->get( MainConfigNames::CapitalLinkOverrides );
562 if ( isset( $overrides[$index] ) ) {
564 return $overrides[$index];
567 return $this->options->get( MainConfigNames::CapitalLinks );
588 $namespaces = $this->options->get( MainConfigNames::NonincludableNamespaces );
589 return $namespaces && in_array( $index, $namespaces );
603 return $this->options->get( MainConfigNames::NamespaceContentModels )[$index] ??
null;
620 return MediaWikiServices::getInstance()
621 ->getPermissionManager()
622 ->getNamespaceRestrictionLevels( $index, $user );
635 $this->isMethodValidFor( $index, __METHOD__ );
639 } elseif ( $index ==
NS_FILE ) {
654 return array_keys( self::CANONICAL_NAMES );
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.
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.
getRestrictionLevels( $index, User $user=null)
Determine which restriction levels it makes sense to use in a namespace, optionally filtered by a use...
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.