25use InvalidArgumentException;
49 private $canonicalNamespaces =
null;
52 private $namespaceIndexes =
false;
55 private $validNamespaces =
null;
63 private array $extensionNamespaces;
65 private array $extensionImmovableNamespaces;
117 array $extensionNamespaces,
118 array $extensionImmovableNamespaces
121 $this->options = $options;
122 $this->hookRunner =
new HookRunner( $hookContainer );
123 $this->extensionNamespaces = $extensionNamespaces;
124 $this->extensionImmovableNamespaces = $extensionImmovableNamespaces;
139 private function isMethodValidFor( $index, $method ) {
141 throw new MWException(
"$method does not make any sense for given namespace $index" );
155 private function makeValidNamespace( $index, $method ) {
159 || ctype_digit( $index )
161 || ( $index[0] ===
'-' && ctype_digit( substr( $index, 1 ) ) )
163 throw new InvalidArgumentException(
164 "$method called with non-integer (" . gettype( $index ) .
") namespace '$index'"
168 return intval( $index );
178 $result = $index >=
NS_MAIN && !in_array( $index, $this->extensionImmovableNamespaces );
183 $this->hookRunner->onNamespaceIsMovable( $index, $result );
195 return !$this->
isTalk( $index );
205 $index = $this->makeValidNamespace( $index, __METHOD__ );
220 $index = $this->makeValidNamespace( $index, __METHOD__ );
222 $this->isMethodValidFor( $index, __METHOD__ );
223 return $this->
isTalk( $index )
238 if ( $target->getText() ===
'' ) {
239 throw new MWException(
'Can\'t determine talk page associated with relative section link' );
242 if ( $target->getInterwiki() !==
'' ) {
243 throw new MWException(
'Can\'t determine talk page associated with interwiki link' );
246 if ( $this->
isTalk( $target->getNamespace() ) ) {
251 return new TitleValue( $this->
getTalk( $target->getNamespace() ), $target->getDBkey() );
279 $index = $this->makeValidNamespace( $index, __METHOD__ );
281 # Handle special namespaces
286 return $this->isTalk( $index )
296 if ( $this->isSubject( $target->getNamespace() ) ) {
299 return new TitleValue( $this->getSubject( $target->getNamespace() ), $target->getDBkey() );
312 $this->isMethodValidFor( $index, __METHOD__ );
314 if ( $this->isSubject( $index ) ) {
315 return $this->getTalk( $index );
317 return $this->getSubject( $index );
327 if ( $target->getText() ===
'' ) {
328 throw new MWException(
'Can\'t determine talk page associated with relative section link' );
331 if ( $target->getInterwiki() !==
'' ) {
332 throw new MWException(
'Can\'t determine talk page associated with interwiki link' );
335 return new TitleValue(
336 $this->getAssociated( $target->getNamespace() ), $target->getDBkey() );
347 $nslist = $this->getCanonicalNamespaces();
348 return isset( $nslist[$index] );
379 return $this->getSubject( $ns1 ) == $this->getSubject( $ns2 );
389 if ( $this->canonicalNamespaces ===
null ) {
390 $this->canonicalNamespaces =
391 [
NS_MAIN =>
'' ] + $this->options->get( MainConfigNames::CanonicalNamespaceNames );
392 $this->canonicalNamespaces += $this->extensionNamespaces;
393 if ( is_array( $this->options->get( MainConfigNames::ExtraNamespaces ) ) ) {
394 $this->canonicalNamespaces += $this->options->get( MainConfigNames::ExtraNamespaces );
396 $this->hookRunner->onCanonicalNamespaces( $this->canonicalNamespaces );
398 return $this->canonicalNamespaces;
408 $nslist = $this->getCanonicalNamespaces();
409 return $nslist[$index] ??
false;
420 if ( $this->namespaceIndexes ===
false ) {
421 $this->namespaceIndexes = [];
422 foreach ( $this->getCanonicalNamespaces() as $i => $text ) {
423 $this->namespaceIndexes[strtolower( $text )] = $i;
426 if ( array_key_exists( $name, $this->namespaceIndexes ) ) {
427 return $this->namespaceIndexes[$name];
439 if ( $this->validNamespaces ===
null ) {
440 $this->validNamespaces = [];
441 foreach ( $this->getCanonicalNamespaces() as $ns => $_ ) {
443 $this->validNamespaces[] = $ns;
447 sort( $this->validNamespaces, SORT_NUMERIC );
450 return $this->validNamespaces;
472 in_array( $index, $this->options->get( MainConfigNames::ContentNamespaces ) );
483 return $this->isTalk( $index ) ||
484 in_array( $index, $this->options->get( MainConfigNames::ExtraSignatureNamespaces ) );
505 return !empty( $this->options->get( MainConfigNames::NamespacesWithSubpages )[$index] );
513 $contentNamespaces = $this->options->get( MainConfigNames::ContentNamespaces );
514 if ( !is_array( $contentNamespaces ) || $contentNamespaces === [] ) {
516 } elseif ( !in_array(
NS_MAIN, $contentNamespaces ) ) {
518 return array_merge( [
NS_MAIN ], $contentNamespaces );
520 return $contentNamespaces;
532 $this->getValidNamespaces(),
533 [ $this,
'isSubject' ]
545 $this->getValidNamespaces(),
561 $index = $this->getSubject( $index );
564 if ( in_array( $index, $this->alwaysCapitalizedNamespaces ) ) {
567 $overrides = $this->options->get( MainConfigNames::CapitalLinkOverrides );
568 if ( isset( $overrides[$index] ) ) {
570 return $overrides[$index];
573 return $this->options->get( MainConfigNames::CapitalLinks );
594 $namespaces = $this->options->get( MainConfigNames::NonincludableNamespaces );
595 return $namespaces && in_array( $index, $namespaces );
609 return $this->options->get( MainConfigNames::NamespaceContentModels )[$index] ??
null;
622 $this->isMethodValidFor( $index, __METHOD__ );
626 } elseif ( $index ==
NS_FILE ) {
641 return array_keys( self::CANONICAL_NAMES );
646class_alias( NamespaceInfo::class,
'NamespaceInfo' );
if(!defined('MW_SETUP_CALLBACK'))
A class containing constants representing the names of configuration variables.
const ContentNamespaces
Name constant for the ContentNamespaces setting, for use with Config::get()
const CapitalLinks
Name constant for the CapitalLinks setting, for use with Config::get()
const ExtraSignatureNamespaces
Name constant for the ExtraSignatureNamespaces setting, for use with Config::get()
const NamespaceContentModels
Name constant for the NamespaceContentModels setting, for use with Config::get()
const NamespacesWithSubpages
Name constant for the NamespacesWithSubpages setting, for use with Config::get()
const ExtraNamespaces
Name constant for the ExtraNamespaces setting, for use with Config::get()
const CanonicalNamespaceNames
Name constant for the CanonicalNamespaceNames setting, for use with Config::get()
const NonincludableNamespaces
Name constant for the NonincludableNamespaces setting, for use with Config::get()
const CapitalLinkOverrides
Name constant for the CapitalLinkOverrides setting, for use with Config::get()