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 $alwaysCapitalizedNamespaces = [ NS_SPECIAL, NS_USER, NS_MEDIAWIKI ];
47
49 private $canonicalNamespaces = null;
50
52 private $namespaceIndexes = false;
53
55 private $validNamespaces = null;
56
58 private $options;
59
61 private $hookRunner;
62
63 private array $extensionNamespaces;
64
65 private array $extensionImmovableNamespaces;
66
72 public const CANONICAL_NAMES = [
73 NS_MEDIA => 'Media',
74 NS_SPECIAL => 'Special',
75 NS_MAIN => '',
76 NS_TALK => 'Talk',
77 NS_USER => 'User',
78 NS_USER_TALK => 'User_talk',
79 NS_PROJECT => 'Project',
80 NS_PROJECT_TALK => 'Project_talk',
81 NS_FILE => 'File',
82 NS_FILE_TALK => 'File_talk',
83 NS_MEDIAWIKI => 'MediaWiki',
84 NS_MEDIAWIKI_TALK => 'MediaWiki_talk',
85 NS_TEMPLATE => 'Template',
86 NS_TEMPLATE_TALK => 'Template_talk',
87 NS_HELP => 'Help',
88 NS_HELP_TALK => 'Help_talk',
89 NS_CATEGORY => 'Category',
90 NS_CATEGORY_TALK => 'Category_talk',
91 ];
92
96 public const CONSTRUCTOR_OPTIONS = [
106 ];
107
114 public function __construct(
115 ServiceOptions $options,
116 HookContainer $hookContainer,
117 array $extensionNamespaces,
118 array $extensionImmovableNamespaces
119 ) {
120 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
121 $this->options = $options;
122 $this->hookRunner = new HookRunner( $hookContainer );
123 $this->extensionNamespaces = $extensionNamespaces;
124 $this->extensionImmovableNamespaces = $extensionImmovableNamespaces;
125 }
126
139 private function isMethodValidFor( $index, $method ) {
140 if ( $index < NS_MAIN ) {
141 throw new MWException( "$method does not make any sense for given namespace $index" );
142 }
143 return true;
144 }
145
155 private function makeValidNamespace( $index, $method ) {
156 if ( !(
157 is_int( $index )
158 // Namespace index numbers as strings
159 || ctype_digit( $index )
160 // Negative numbers as strings
161 || ( $index[0] === '-' && ctype_digit( substr( $index, 1 ) ) )
162 ) ) {
163 throw new InvalidArgumentException(
164 "$method called with non-integer (" . gettype( $index ) . ") namespace '$index'"
165 );
166 }
167
168 return intval( $index );
169 }
170
177 public function isMovable( $index ) {
178 $result = $index >= NS_MAIN && !in_array( $index, $this->extensionImmovableNamespaces );
179
183 $this->hookRunner->onNamespaceIsMovable( $index, $result );
184
185 return $result;
186 }
187
194 public function isSubject( $index ) {
195 return !$this->isTalk( $index );
196 }
197
204 public function isTalk( $index ) {
205 $index = $this->makeValidNamespace( $index, __METHOD__ );
206
207 return $index > NS_MAIN
208 && $index % 2 === 1;
209 }
210
219 public function getTalk( $index ) {
220 $index = $this->makeValidNamespace( $index, __METHOD__ );
221
222 $this->isMethodValidFor( $index, __METHOD__ );
223 return $this->isTalk( $index )
224 ? $index
225 : $index + 1;
226 }
227
237 public function getTalkPage( LinkTarget $target ): LinkTarget {
238 if ( $target->getText() === '' ) {
239 throw new MWException( 'Can\'t determine talk page associated with relative section link' );
240 }
241
242 if ( $target->getInterwiki() !== '' ) {
243 throw new MWException( 'Can\'t determine talk page associated with interwiki link' );
244 }
245
246 if ( $this->isTalk( $target->getNamespace() ) ) {
247 return $target;
248 }
249
250 // NOTE: getTalk throws on bad namespaces!
251 return new TitleValue( $this->getTalk( $target->getNamespace() ), $target->getDBkey() );
252 }
253
265 public function canHaveTalkPage( LinkTarget $target ) {
266 return $target->getNamespace() >= NS_MAIN &&
267 !$target->isExternal() &&
268 $target->getText() !== '';
269 }
270
278 public function getSubject( $index ) {
279 $index = $this->makeValidNamespace( $index, __METHOD__ );
280
281 # Handle special namespaces
282 if ( $index < NS_MAIN ) {
283 return $index;
284 }
285
286 return $this->isTalk( $index )
287 ? $index - 1
288 : $index;
289 }
290
295 public function getSubjectPage( LinkTarget $target ): LinkTarget {
296 if ( $this->isSubject( $target->getNamespace() ) ) {
297 return $target;
298 }
299 return new TitleValue( $this->getSubject( $target->getNamespace() ), $target->getDBkey() );
300 }
301
311 public function getAssociated( $index ) {
312 $this->isMethodValidFor( $index, __METHOD__ );
313
314 if ( $this->isSubject( $index ) ) {
315 return $this->getTalk( $index );
316 }
317 return $this->getSubject( $index );
318 }
319
326 public function getAssociatedPage( LinkTarget $target ): LinkTarget {
327 if ( $target->getText() === '' ) {
328 throw new MWException( 'Can\'t determine talk page associated with relative section link' );
329 }
330
331 if ( $target->getInterwiki() !== '' ) {
332 throw new MWException( 'Can\'t determine talk page associated with interwiki link' );
333 }
334
335 return new TitleValue(
336 $this->getAssociated( $target->getNamespace() ), $target->getDBkey() );
337 }
338
346 public function exists( $index ) {
347 $nslist = $this->getCanonicalNamespaces();
348 return isset( $nslist[$index] );
349 }
350
364 public function equals( $ns1, $ns2 ) {
365 return $ns1 == $ns2;
366 }
367
378 public function subjectEquals( $ns1, $ns2 ) {
379 return $this->getSubject( $ns1 ) == $this->getSubject( $ns2 );
380 }
381
388 public function getCanonicalNamespaces() {
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 );
395 }
396 $this->hookRunner->onCanonicalNamespaces( $this->canonicalNamespaces );
397 }
398 return $this->canonicalNamespaces;
399 }
400
407 public function getCanonicalName( $index ) {
408 $nslist = $this->getCanonicalNamespaces();
409 return $nslist[$index] ?? false;
410 }
411
419 public function getCanonicalIndex( $name ) {
420 if ( $this->namespaceIndexes === false ) {
421 $this->namespaceIndexes = [];
422 foreach ( $this->getCanonicalNamespaces() as $i => $text ) {
423 $this->namespaceIndexes[strtolower( $text )] = $i;
424 }
425 }
426 if ( array_key_exists( $name, $this->namespaceIndexes ) ) {
427 return $this->namespaceIndexes[$name];
428 } else {
429 return null;
430 }
431 }
432
438 public function getValidNamespaces() {
439 if ( $this->validNamespaces === null ) {
440 $this->validNamespaces = [];
441 foreach ( $this->getCanonicalNamespaces() as $ns => $_ ) {
442 if ( $ns >= 0 ) {
443 $this->validNamespaces[] = $ns;
444 }
445 }
446 // T109137: sort numerically
447 sort( $this->validNamespaces, SORT_NUMERIC );
448 }
449
450 return $this->validNamespaces;
451 }
452
459 public function hasTalkNamespace( $index ) {
460 return $index >= NS_MAIN;
461 }
462
470 public function isContent( $index ) {
471 return $index == NS_MAIN ||
472 in_array( $index, $this->options->get( MainConfigNames::ContentNamespaces ) );
473 }
474
482 public function wantSignatures( $index ) {
483 return $this->isTalk( $index ) ||
484 in_array( $index, $this->options->get( MainConfigNames::ExtraSignatureNamespaces ) );
485 }
486
493 public function isWatchable( $index ) {
494 return $index >= NS_MAIN;
495 }
496
504 public function hasSubpages( $index ) {
505 return !empty( $this->options->get( MainConfigNames::NamespacesWithSubpages )[$index] );
506 }
507
512 public function getContentNamespaces() {
513 $contentNamespaces = $this->options->get( MainConfigNames::ContentNamespaces );
514 if ( !is_array( $contentNamespaces ) || $contentNamespaces === [] ) {
515 return [ NS_MAIN ];
516 } elseif ( !in_array( NS_MAIN, $contentNamespaces ) ) {
517 // always force NS_MAIN to be part of array (to match the algorithm used by isContent)
518 return array_merge( [ NS_MAIN ], $contentNamespaces );
519 } else {
520 return $contentNamespaces;
521 }
522 }
523
530 public function getSubjectNamespaces() {
531 return array_filter(
532 $this->getValidNamespaces(),
533 [ $this, 'isSubject' ]
534 );
535 }
536
543 public function getTalkNamespaces() {
544 return array_filter(
545 $this->getValidNamespaces(),
546 [ $this, 'isTalk' ]
547 );
548 }
549
556 public function isCapitalized( $index ) {
557 // Turn NS_MEDIA into NS_FILE
558 $index = $index === NS_MEDIA ? NS_FILE : $index;
559
560 // Make sure to get the subject of our namespace
561 $index = $this->getSubject( $index );
562
563 // Some namespaces are special and should always be upper case
564 if ( in_array( $index, $this->alwaysCapitalizedNamespaces ) ) {
565 return true;
566 }
567 $overrides = $this->options->get( MainConfigNames::CapitalLinkOverrides );
568 if ( isset( $overrides[$index] ) ) {
569 // CapitalLinkOverrides is explicitly set
570 return $overrides[$index];
571 }
572 // Default to the global setting
573 return $this->options->get( MainConfigNames::CapitalLinks );
574 }
575
583 public function hasGenderDistinction( $index ) {
584 return $index == NS_USER || $index == NS_USER_TALK;
585 }
586
593 public function isNonincludable( $index ) {
594 $namespaces = $this->options->get( MainConfigNames::NonincludableNamespaces );
595 return $namespaces && in_array( $index, $namespaces );
596 }
597
608 public function getNamespaceContentModel( $index ) {
609 return $this->options->get( MainConfigNames::NamespaceContentModels )[$index] ?? null;
610 }
611
621 public function getCategoryLinkType( $index ) {
622 $this->isMethodValidFor( $index, __METHOD__ );
623
624 if ( $index == NS_CATEGORY ) {
625 return 'subcat';
626 } elseif ( $index == NS_FILE ) {
627 return 'file';
628 } else {
629 return 'page';
630 }
631 }
632
640 public static function getCommonNamespaces() {
641 return array_keys( self::CANONICAL_NAMES );
642 }
643}
644
646class_alias( NamespaceInfo::class, 'NamespaceInfo' );
const NS_HELP
Definition Defines.php:76
const NS_USER
Definition Defines.php:66
const NS_FILE
Definition Defines.php:70
const NS_MEDIAWIKI_TALK
Definition Defines.php:73
const NS_MAIN
Definition Defines.php:64
const NS_PROJECT_TALK
Definition Defines.php:69
const NS_MEDIAWIKI
Definition Defines.php:72
const NS_TEMPLATE
Definition Defines.php:74
const NS_SPECIAL
Definition Defines.php:53
const NS_FILE_TALK
Definition Defines.php:71
const NS_HELP_TALK
Definition Defines.php:77
const NS_CATEGORY_TALK
Definition Defines.php:79
const NS_MEDIA
Definition Defines.php:52
const NS_TALK
Definition Defines.php:65
const NS_USER_TALK
Definition Defines.php:67
const NS_PROJECT
Definition Defines.php:68
const NS_CATEGORY
Definition Defines.php:78
const NS_TEMPLATE_TALK
Definition Defines.php:75
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.