MediaWiki master
NamespaceInfo.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Title;
24
25use InvalidArgumentException;
31use MWException;
32
40
46 private const ALWAYS_CAPITALIZED_NAMESPACES = [ NS_SPECIAL, NS_USER, NS_MEDIAWIKI ];
47
49 private $canonicalNamespaces = null;
50
52 private $namespaceIndexes = false;
53
55 private $validNamespaces = null;
56
57 private ServiceOptions $options;
58 private HookRunner $hookRunner;
59 private array $extensionNamespaces;
60 private array $extensionImmovableNamespaces;
61
67 public const CANONICAL_NAMES = [
68 NS_MEDIA => 'Media',
69 NS_SPECIAL => 'Special',
70 NS_MAIN => '',
71 NS_TALK => 'Talk',
72 NS_USER => 'User',
73 NS_USER_TALK => 'User_talk',
74 NS_PROJECT => 'Project',
75 NS_PROJECT_TALK => 'Project_talk',
76 NS_FILE => 'File',
77 NS_FILE_TALK => 'File_talk',
78 NS_MEDIAWIKI => 'MediaWiki',
79 NS_MEDIAWIKI_TALK => 'MediaWiki_talk',
80 NS_TEMPLATE => 'Template',
81 NS_TEMPLATE_TALK => 'Template_talk',
82 NS_HELP => 'Help',
83 NS_HELP_TALK => 'Help_talk',
84 NS_CATEGORY => 'Category',
85 NS_CATEGORY_TALK => 'Category_talk',
86 ];
87
91 public const CONSTRUCTOR_OPTIONS = [
101 ];
102
103 public function __construct(
104 ServiceOptions $options,
105 HookContainer $hookContainer,
106 array $extensionNamespaces,
107 array $extensionImmovableNamespaces
108 ) {
109 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
110 $this->options = $options;
111 $this->hookRunner = new HookRunner( $hookContainer );
112 $this->extensionNamespaces = $extensionNamespaces;
113 $this->extensionImmovableNamespaces = $extensionImmovableNamespaces;
114 }
115
128 private function isMethodValidFor( $index, $method ) {
129 if ( $index < NS_MAIN ) {
130 throw new MWException( "$method does not make any sense for given namespace $index" );
131 }
132 return true;
133 }
134
144 private function makeValidNamespace( $index, $method ) {
145 if ( !(
146 is_int( $index )
147 // Namespace index numbers as strings
148 || ctype_digit( $index )
149 // Negative numbers as strings
150 || ( $index[0] === '-' && ctype_digit( substr( $index, 1 ) ) )
151 ) ) {
152 throw new InvalidArgumentException(
153 "$method called with non-integer (" . get_debug_type( $index ) . ") namespace '$index'"
154 );
155 }
156
157 return intval( $index );
158 }
159
166 public function isMovable( $index ) {
167 $result = $index >= NS_MAIN && !in_array( $index, $this->extensionImmovableNamespaces );
168
172 $this->hookRunner->onNamespaceIsMovable( $index, $result );
173
174 return $result;
175 }
176
183 public function isSubject( $index ) {
184 return !$this->isTalk( $index );
185 }
186
193 public function isTalk( $index ) {
194 $index = $this->makeValidNamespace( $index, __METHOD__ );
195
196 return $index > NS_MAIN
197 && $index % 2 === 1;
198 }
199
208 public function getTalk( $index ) {
209 $index = $this->makeValidNamespace( $index, __METHOD__ );
210
211 $this->isMethodValidFor( $index, __METHOD__ );
212 return $this->isTalk( $index )
213 ? $index
214 : $index + 1;
215 }
216
226 public function getTalkPage( LinkTarget $target ): LinkTarget {
227 if ( $target->getText() === '' ) {
228 throw new MWException( 'Can\'t determine talk page associated with relative section link' );
229 }
230
231 if ( $target->getInterwiki() !== '' ) {
232 throw new MWException( 'Can\'t determine talk page associated with interwiki link' );
233 }
234
235 if ( $this->isTalk( $target->getNamespace() ) ) {
236 return $target;
237 }
238
239 // NOTE: getTalk throws on bad namespaces!
240 return new TitleValue( $this->getTalk( $target->getNamespace() ), $target->getDBkey() );
241 }
242
254 public function canHaveTalkPage( LinkTarget $target ) {
255 return $target->getNamespace() >= NS_MAIN &&
256 !$target->isExternal() &&
257 $target->getText() !== '';
258 }
259
267 public function getSubject( $index ) {
268 $index = $this->makeValidNamespace( $index, __METHOD__ );
269
270 # Handle special namespaces
271 if ( $index < NS_MAIN ) {
272 return $index;
273 }
274
275 return $this->isTalk( $index )
276 ? $index - 1
277 : $index;
278 }
279
284 public function getSubjectPage( LinkTarget $target ): LinkTarget {
285 if ( $this->isSubject( $target->getNamespace() ) ) {
286 return $target;
287 }
288 return new TitleValue( $this->getSubject( $target->getNamespace() ), $target->getDBkey() );
289 }
290
300 public function getAssociated( $index ) {
301 $this->isMethodValidFor( $index, __METHOD__ );
302
303 if ( $this->isSubject( $index ) ) {
304 return $this->getTalk( $index );
305 }
306 return $this->getSubject( $index );
307 }
308
315 public function getAssociatedPage( LinkTarget $target ): LinkTarget {
316 if ( $target->getText() === '' ) {
317 throw new MWException( 'Can\'t determine talk page associated with relative section link' );
318 }
319
320 if ( $target->getInterwiki() !== '' ) {
321 throw new MWException( 'Can\'t determine talk page associated with interwiki link' );
322 }
323
324 return new TitleValue(
325 $this->getAssociated( $target->getNamespace() ), $target->getDBkey() );
326 }
327
335 public function exists( $index ) {
336 $nslist = $this->getCanonicalNamespaces();
337 return isset( $nslist[$index] );
338 }
339
353 public function equals( $ns1, $ns2 ) {
354 return $ns1 == $ns2;
355 }
356
367 public function subjectEquals( $ns1, $ns2 ) {
368 return $this->getSubject( $ns1 ) == $this->getSubject( $ns2 );
369 }
370
377 public function getCanonicalNamespaces() {
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 );
384 }
385 $this->hookRunner->onCanonicalNamespaces( $this->canonicalNamespaces );
386 }
387 return $this->canonicalNamespaces;
388 }
389
396 public function getCanonicalName( $index ) {
397 $nslist = $this->getCanonicalNamespaces();
398 return $nslist[$index] ?? false;
399 }
400
408 public function getCanonicalIndex( $name ) {
409 if ( $this->namespaceIndexes === false ) {
410 $this->namespaceIndexes = [];
411 foreach ( $this->getCanonicalNamespaces() as $i => $text ) {
412 $this->namespaceIndexes[strtolower( $text )] = $i;
413 }
414 }
415 if ( array_key_exists( $name, $this->namespaceIndexes ) ) {
416 return $this->namespaceIndexes[$name];
417 } else {
418 return null;
419 }
420 }
421
427 public function getValidNamespaces() {
428 if ( $this->validNamespaces === null ) {
429 $this->validNamespaces = [];
430 foreach ( $this->getCanonicalNamespaces() as $ns => $_ ) {
431 if ( $ns >= 0 ) {
432 $this->validNamespaces[] = $ns;
433 }
434 }
435 // T109137: sort numerically
436 sort( $this->validNamespaces, SORT_NUMERIC );
437 }
438
439 return $this->validNamespaces;
440 }
441
448 public function hasTalkNamespace( $index ) {
449 return $index >= NS_MAIN;
450 }
451
459 public function isContent( $index ) {
460 return $index == NS_MAIN ||
461 in_array( $index, $this->options->get( MainConfigNames::ContentNamespaces ) );
462 }
463
471 public function wantSignatures( $index ) {
472 return $this->isTalk( $index ) ||
473 in_array( $index, $this->options->get( MainConfigNames::ExtraSignatureNamespaces ) );
474 }
475
482 public function isWatchable( $index ) {
483 return $index >= NS_MAIN;
484 }
485
493 public function hasSubpages( $index ) {
494 return !empty( $this->options->get( MainConfigNames::NamespacesWithSubpages )[$index] );
495 }
496
501 public function getContentNamespaces() {
502 $contentNamespaces = $this->options->get( MainConfigNames::ContentNamespaces );
503 if ( !is_array( $contentNamespaces ) || $contentNamespaces === [] ) {
504 return [ NS_MAIN ];
505 } elseif ( !in_array( NS_MAIN, $contentNamespaces ) ) {
506 // always force NS_MAIN to be part of array (to match the algorithm used by isContent)
507 return array_merge( [ NS_MAIN ], $contentNamespaces );
508 } else {
509 return $contentNamespaces;
510 }
511 }
512
519 public function getSubjectNamespaces() {
520 return array_filter(
521 $this->getValidNamespaces(),
522 [ $this, 'isSubject' ]
523 );
524 }
525
532 public function getTalkNamespaces() {
533 return array_filter(
534 $this->getValidNamespaces(),
535 [ $this, 'isTalk' ]
536 );
537 }
538
545 public function isCapitalized( $index ) {
546 // Turn NS_MEDIA into NS_FILE
547 $index = $index === NS_MEDIA ? NS_FILE : $index;
548
549 // Make sure to get the subject of our namespace
550 $index = $this->getSubject( $index );
551
552 // Some namespaces are special and should always be upper case
553 if ( in_array( $index, self::ALWAYS_CAPITALIZED_NAMESPACES ) ) {
554 return true;
555 }
556 $overrides = $this->options->get( MainConfigNames::CapitalLinkOverrides );
557 if ( isset( $overrides[$index] ) ) {
558 // CapitalLinkOverrides is explicitly set
559 return $overrides[$index];
560 }
561 // Default to the global setting
562 return $this->options->get( MainConfigNames::CapitalLinks );
563 }
564
572 public function hasGenderDistinction( $index ) {
573 return $index == NS_USER || $index == NS_USER_TALK;
574 }
575
582 public function isNonincludable( $index ) {
583 $namespaces = $this->options->get( MainConfigNames::NonincludableNamespaces );
584 return $namespaces && in_array( $index, $namespaces );
585 }
586
597 public function getNamespaceContentModel( $index ) {
598 return $this->options->get( MainConfigNames::NamespaceContentModels )[$index] ?? null;
599 }
600
610 public function getCategoryLinkType( $index ) {
611 $this->isMethodValidFor( $index, __METHOD__ );
612
613 if ( $index == NS_CATEGORY ) {
614 return 'subcat';
615 } elseif ( $index == NS_FILE ) {
616 return 'file';
617 } else {
618 return 'page';
619 }
620 }
621
629 public static function getCommonNamespaces() {
630 return array_keys( self::CANONICAL_NAMES );
631 }
632}
633
635class_alias( NamespaceInfo::class, 'NamespaceInfo' );
const NS_HELP
Definition Defines.php:77
const NS_USER
Definition Defines.php:67
const NS_FILE
Definition Defines.php:71
const NS_MEDIAWIKI_TALK
Definition Defines.php:74
const NS_MAIN
Definition Defines.php:65
const NS_PROJECT_TALK
Definition Defines.php:70
const NS_MEDIAWIKI
Definition Defines.php:73
const NS_TEMPLATE
Definition Defines.php:75
const NS_SPECIAL
Definition Defines.php:54
const NS_FILE_TALK
Definition Defines.php:72
const NS_HELP_TALK
Definition Defines.php:78
const NS_CATEGORY_TALK
Definition Defines.php:80
const NS_MEDIA
Definition Defines.php:53
const NS_TALK
Definition Defines.php:66
const NS_USER_TALK
Definition Defines.php:68
const NS_PROJECT
Definition Defines.php:69
const NS_CATEGORY
Definition Defines.php:79
const NS_TEMPLATE_TALK
Definition Defines.php:76
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
MediaWiki exception.
A class for passing options to services.
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys,...
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
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()
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
hasGenderDistinction( $index)
Does the namespace (potentially) have different aliases for different genders.
wantSignatures( $index)
Might pages in this namespace require the use of the Signature button on the edit toolbar?
isMovable( $index)
Can pages in the given namespace be moved?
getTalkNamespaces()
List all namespace indices which are considered talks, aka not a subject or special namespace.
getContentNamespaces()
Get a list of all namespace indices which are considered to contain content.
hasSubpages( $index)
Does the namespace allow subpages? Note that this refers to structured handling of subpages,...
isCapitalized( $index)
Is the namespace first-letter capitalized?
equals( $ns1, $ns2)
Returns whether the specified namespaces are the same namespace.
getCategoryLinkType( $index)
Returns the link type to be used for categories.
isSubject( $index)
Is the given namespace is a subject (non-talk) namespace?
getTalkPage(LinkTarget $target)
Get a LinkTarget referring to the talk page of $target.
hasTalkNamespace( $index)
Does this namespace ever have a talk namespace?
getCanonicalIndex( $name)
Returns the index for a given canonical name, or NULL The input must be converted to lower case first...
getSubjectPage(LinkTarget $target)
isNonincludable( $index)
It is not possible to use pages from this namespace as template?
canHaveTalkPage(LinkTarget $target)
Can the title have a corresponding talk page?
static getCommonNamespaces()
Retrieve the indexes for the namespaces defined by core.
const CANONICAL_NAMES
Definitions of the NS_ constants are in Defines.php.
getCanonicalNamespaces()
Returns array of all defined namespaces with their canonical (English) names.
getNamespaceContentModel( $index)
Get the default content model for a namespace This does not mean that all pages in that namespace hav...
exists( $index)
Returns whether the specified namespace exists.
getCanonicalName( $index)
Returns the canonical (English) name for a given index.
subjectEquals( $ns1, $ns2)
Returns whether the specified namespaces share the same subject.
isTalk( $index)
Is the given namespace a talk namespace?
isContent( $index)
Does this namespace contain content, for the purposes of calculating statistics, etc?
getSubject( $index)
Get the subject namespace index for a given namespace Special namespaces (NS_MEDIA,...
isWatchable( $index)
Can pages in a namespace be watched?
getTalk( $index)
Get the talk namespace index for a given namespace.
getAssociatedPage(LinkTarget $target)
__construct(ServiceOptions $options, HookContainer $hookContainer, array $extensionNamespaces, array $extensionImmovableNamespaces)
getSubjectNamespaces()
List all namespace indices which are considered subject, aka not a talk or special namespace.
getAssociated( $index)
Get the associated namespace.
getValidNamespaces()
Returns an array of the namespaces (by integer id) that exist on the wiki.
Represents the target of a wiki link.
Represents the target of a wiki link.
getNamespace()
Get the namespace index.
isExternal()
Whether this LinkTarget has an interwiki component.
getText()
Get the main part of the link target, in text form.