25use InvalidArgumentException;
49 private $canonicalNamespaces =
null;
52 private $namespaceIndexes =
false;
55 private $validNamespaces =
null;
59 private array $extensionNamespaces;
60 private array $extensionImmovableNamespaces;
106 array $extensionNamespaces,
107 array $extensionImmovableNamespaces
110 $this->options = $options;
111 $this->hookRunner =
new HookRunner( $hookContainer );
112 $this->extensionNamespaces = $extensionNamespaces;
113 $this->extensionImmovableNamespaces = $extensionImmovableNamespaces;
128 private function isMethodValidFor( $index, $method ) {
130 throw new MWException(
"$method does not make any sense for given namespace $index" );
144 private function makeValidNamespace( $index, $method ) {
148 || ctype_digit( $index )
150 || ( $index[0] ===
'-' && ctype_digit( substr( $index, 1 ) ) )
152 throw new InvalidArgumentException(
153 "$method called with non-integer (" . get_debug_type( $index ) .
") namespace '$index'"
157 return intval( $index );
167 $result = $index >=
NS_MAIN && !in_array( $index, $this->extensionImmovableNamespaces );
172 $this->hookRunner->onNamespaceIsMovable( $index, $result );
184 return !$this->
isTalk( $index );
194 $index = $this->makeValidNamespace( $index, __METHOD__ );
209 $index = $this->makeValidNamespace( $index, __METHOD__ );
211 $this->isMethodValidFor( $index, __METHOD__ );
212 return $this->
isTalk( $index )
227 if ( $target->getText() ===
'' ) {
228 throw new MWException(
'Can\'t determine talk page associated with relative section link' );
231 if ( $target->getInterwiki() !==
'' ) {
232 throw new MWException(
'Can\'t determine talk page associated with interwiki link' );
235 if ( $this->
isTalk( $target->getNamespace() ) ) {
240 return new TitleValue( $this->
getTalk( $target->getNamespace() ), $target->getDBkey() );
268 $index = $this->makeValidNamespace( $index, __METHOD__ );
270 # Handle special namespaces
275 return $this->isTalk( $index )
285 if ( $this->isSubject( $target->getNamespace() ) ) {
288 return new TitleValue( $this->getSubject( $target->getNamespace() ), $target->getDBkey() );
301 $this->isMethodValidFor( $index, __METHOD__ );
303 if ( $this->isSubject( $index ) ) {
304 return $this->getTalk( $index );
306 return $this->getSubject( $index );
316 if ( $target->getText() ===
'' ) {
317 throw new MWException(
'Can\'t determine talk page associated with relative section link' );
320 if ( $target->getInterwiki() !==
'' ) {
321 throw new MWException(
'Can\'t determine talk page associated with interwiki link' );
324 return new TitleValue(
325 $this->getAssociated( $target->getNamespace() ), $target->getDBkey() );
336 $nslist = $this->getCanonicalNamespaces();
337 return isset( $nslist[$index] );
368 return $this->getSubject( $ns1 ) == $this->getSubject( $ns2 );
378 if ( $this->canonicalNamespaces ===
null ) {
379 $this->canonicalNamespaces =
380 [
NS_MAIN =>
'' ] + $this->options->get( MainConfigNames::CanonicalNamespaceNames );
381 $this->canonicalNamespaces += $this->extensionNamespaces;
382 if ( is_array( $this->options->get( MainConfigNames::ExtraNamespaces ) ) ) {
383 $this->canonicalNamespaces += $this->options->get( MainConfigNames::ExtraNamespaces );
385 $this->hookRunner->onCanonicalNamespaces( $this->canonicalNamespaces );
387 return $this->canonicalNamespaces;
397 $nslist = $this->getCanonicalNamespaces();
398 return $nslist[$index] ??
false;
409 if ( $this->namespaceIndexes ===
false ) {
410 $this->namespaceIndexes = [];
411 foreach ( $this->getCanonicalNamespaces() as $i => $text ) {
412 $this->namespaceIndexes[strtolower( $text )] = $i;
415 if ( array_key_exists( $name, $this->namespaceIndexes ) ) {
416 return $this->namespaceIndexes[$name];
428 if ( $this->validNamespaces ===
null ) {
429 $this->validNamespaces = [];
430 foreach ( $this->getCanonicalNamespaces() as $ns => $_ ) {
432 $this->validNamespaces[] = $ns;
436 sort( $this->validNamespaces, SORT_NUMERIC );
439 return $this->validNamespaces;
461 in_array( $index, $this->options->get( MainConfigNames::ContentNamespaces ) );
472 return $this->isTalk( $index ) ||
473 in_array( $index, $this->options->get( MainConfigNames::ExtraSignatureNamespaces ) );
494 return !empty( $this->options->get( MainConfigNames::NamespacesWithSubpages )[$index] );
502 $contentNamespaces = $this->options->get( MainConfigNames::ContentNamespaces );
503 if ( !is_array( $contentNamespaces ) || $contentNamespaces === [] ) {
505 } elseif ( !in_array(
NS_MAIN, $contentNamespaces ) ) {
507 return array_merge( [
NS_MAIN ], $contentNamespaces );
509 return $contentNamespaces;
521 $this->getValidNamespaces(),
522 [ $this,
'isSubject' ]
534 $this->getValidNamespaces(),
550 $index = $this->getSubject( $index );
553 if ( in_array( $index, self::ALWAYS_CAPITALIZED_NAMESPACES ) ) {
556 $overrides = $this->options->get( MainConfigNames::CapitalLinkOverrides );
557 if ( isset( $overrides[$index] ) ) {
559 return $overrides[$index];
562 return $this->options->get( MainConfigNames::CapitalLinks );
583 $namespaces = $this->options->get( MainConfigNames::NonincludableNamespaces );
584 return $namespaces && in_array( $index, $namespaces );
598 return $this->options->get( MainConfigNames::NamespaceContentModels )[$index] ??
null;
611 $this->isMethodValidFor( $index, __METHOD__ );
615 } elseif ( $index ==
NS_FILE ) {
630 return array_keys( self::CANONICAL_NAMES );
635class_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()