MediaWiki  master
NamespaceInfo.php
Go to the documentation of this file.
1 <?php
28 
36 
43 
45  private $canonicalNamespaces = null;
46 
48  private $namespaceIndexes = false;
49 
51  private $validNamespaces = null;
52 
54  private $options;
55 
57  private $hookRunner;
58 
64  public const CANONICAL_NAMES = [
65  NS_MEDIA => 'Media',
66  NS_SPECIAL => 'Special',
67  NS_MAIN => '',
68  NS_TALK => 'Talk',
69  NS_USER => 'User',
70  NS_USER_TALK => 'User_talk',
71  NS_PROJECT => 'Project',
72  NS_PROJECT_TALK => 'Project_talk',
73  NS_FILE => 'File',
74  NS_FILE_TALK => 'File_talk',
75  NS_MEDIAWIKI => 'MediaWiki',
76  NS_MEDIAWIKI_TALK => 'MediaWiki_talk',
77  NS_TEMPLATE => 'Template',
78  NS_TEMPLATE_TALK => 'Template_talk',
79  NS_HELP => 'Help',
80  NS_HELP_TALK => 'Help_talk',
81  NS_CATEGORY => 'Category',
82  NS_CATEGORY_TALK => 'Category_talk',
83  ];
84 
88  public const CONSTRUCTOR_OPTIONS = [
89  'CanonicalNamespaceNames',
90  'CapitalLinkOverrides',
91  'CapitalLinks',
92  'ContentNamespaces',
93  'ExtraNamespaces',
94  'ExtraSignatureNamespaces',
95  'NamespaceContentModels',
96  'NamespacesWithSubpages',
97  'NonincludableNamespaces',
98  ];
99 
104  public function __construct( ServiceOptions $options, HookContainer $hookContainer ) {
105  $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
106  $this->options = $options;
107  $this->hookRunner = new HookRunner( $hookContainer );
108  }
109 
122  private function isMethodValidFor( $index, $method ) {
123  if ( $index < NS_MAIN ) {
124  throw new MWException( "$method does not make any sense for given namespace $index" );
125  }
126  return true;
127  }
128 
138  private function makeValidNamespace( $index, $method ) {
139  if ( !(
140  is_int( $index )
141  // Namespace index numbers as strings
142  || ctype_digit( $index )
143  // Negative numbers as strings
144  || ( $index[0] === '-' && ctype_digit( substr( $index, 1 ) ) )
145  ) ) {
146  throw new InvalidArgumentException(
147  "$method called with non-integer (" . gettype( $index ) . ") namespace '$index'"
148  );
149  }
150 
151  return intval( $index );
152  }
153 
160  public function isMovable( $index ) {
161  $result = $index >= NS_MAIN;
162 
166  $this->hookRunner->onNamespaceIsMovable( $index, $result );
167 
168  return $result;
169  }
170 
177  public function isSubject( $index ) {
178  return !$this->isTalk( $index );
179  }
180 
187  public function isTalk( $index ) {
188  $index = $this->makeValidNamespace( $index, __METHOD__ );
189 
190  return $index > NS_MAIN
191  && $index % 2;
192  }
193 
202  public function getTalk( $index ) {
203  $index = $this->makeValidNamespace( $index, __METHOD__ );
204 
205  $this->isMethodValidFor( $index, __METHOD__ );
206  return $this->isTalk( $index )
207  ? $index
208  : $index + 1;
209  }
210 
220  public function getTalkPage( LinkTarget $target ) : LinkTarget {
221  if ( $target->getText() === '' ) {
222  throw new MWException( 'Can\'t determine talk page associated with relative section link' );
223  }
224 
225  if ( $target->getInterwiki() !== '' ) {
226  throw new MWException( 'Can\'t determine talk page associated with interwiki link' );
227  }
228 
229  if ( $this->isTalk( $target->getNamespace() ) ) {
230  return $target;
231  }
232 
233  // NOTE: getTalk throws on bad namespaces!
234  return new TitleValue( $this->getTalk( $target->getNamespace() ), $target->getDBkey() );
235  }
236 
248  public function canHaveTalkPage( LinkTarget $target ) {
249  if ( $target->getText() === '' || $target->getInterwiki() !== '' ) {
250  return false;
251  }
252 
253  if ( $target->getNamespace() < NS_MAIN ) {
254  return false;
255  }
256 
257  return true;
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( 'CanonicalNamespaceNames' );
381  $this->canonicalNamespaces +=
382  ExtensionRegistry::getInstance()->getAttribute( 'ExtensionNamespaces' );
383  if ( is_array( $this->options->get( 'ExtraNamespaces' ) ) ) {
384  $this->canonicalNamespaces += $this->options->get( 'ExtraNamespaces' );
385  }
386  $this->hookRunner->onCanonicalNamespaces( $this->canonicalNamespaces );
387  }
389  }
390 
397  public function getCanonicalName( $index ) {
398  $nslist = $this->getCanonicalNamespaces();
399  return $nslist[$index] ?? false;
400  }
401 
409  public function getCanonicalIndex( $name ) {
410  if ( $this->namespaceIndexes === false ) {
411  $this->namespaceIndexes = [];
412  foreach ( $this->getCanonicalNamespaces() as $i => $text ) {
413  $this->namespaceIndexes[strtolower( $text )] = $i;
414  }
415  }
416  if ( array_key_exists( $name, $this->namespaceIndexes ) ) {
417  return $this->namespaceIndexes[$name];
418  } else {
419  return null;
420  }
421  }
422 
428  public function getValidNamespaces() {
429  if ( $this->validNamespaces === null ) {
430  $this->validNamespaces = [];
431  foreach ( array_keys( $this->getCanonicalNamespaces() ) as $ns ) {
432  if ( $ns >= 0 ) {
433  $this->validNamespaces[] = $ns;
434  }
435  }
436  // T109137: sort numerically
437  sort( $this->validNamespaces, SORT_NUMERIC );
438  }
439 
440  return $this->validNamespaces;
441  }
442 
449  public function hasTalkNamespace( $index ) {
450  return $index >= NS_MAIN;
451  }
452 
460  public function isContent( $index ) {
461  return $index == NS_MAIN || in_array( $index, $this->options->get( 'ContentNamespaces' ) );
462  }
463 
471  public function wantSignatures( $index ) {
472  return $this->isTalk( $index ) ||
473  in_array( $index, $this->options->get( 'ExtraSignatureNamespaces' ) );
474  }
475 
482  public function isWatchable( $index ) {
483  return $index >= NS_MAIN;
484  }
485 
492  public function hasSubpages( $index ) {
493  return !empty( $this->options->get( 'NamespacesWithSubpages' )[$index] );
494  }
495 
500  public function getContentNamespaces() {
501  $contentNamespaces = $this->options->get( 'ContentNamespaces' );
502  if ( !is_array( $contentNamespaces ) || $contentNamespaces === [] ) {
503  return [ NS_MAIN ];
504  } elseif ( !in_array( NS_MAIN, $contentNamespaces ) ) {
505  // always force NS_MAIN to be part of array (to match the algorithm used by isContent)
506  return array_merge( [ NS_MAIN ], $contentNamespaces );
507  } else {
508  return $contentNamespaces;
509  }
510  }
511 
518  public function getSubjectNamespaces() {
519  return array_filter(
520  $this->getValidNamespaces(),
521  [ $this, 'isSubject' ]
522  );
523  }
524 
531  public function getTalkNamespaces() {
532  return array_filter(
533  $this->getValidNamespaces(),
534  [ $this, 'isTalk' ]
535  );
536  }
537 
544  public function isCapitalized( $index ) {
545  // Turn NS_MEDIA into NS_FILE
546  $index = $index === NS_MEDIA ? NS_FILE : $index;
547 
548  // Make sure to get the subject of our namespace
549  $index = $this->getSubject( $index );
550 
551  // Some namespaces are special and should always be upper case
552  if ( in_array( $index, $this->alwaysCapitalizedNamespaces ) ) {
553  return true;
554  }
555  $overrides = $this->options->get( 'CapitalLinkOverrides' );
556  if ( isset( $overrides[$index] ) ) {
557  // CapitalLinkOverrides is explicitly set
558  return $overrides[$index];
559  }
560  // Default to the global setting
561  return $this->options->get( 'CapitalLinks' );
562  }
563 
571  public function hasGenderDistinction( $index ) {
572  return $index == NS_USER || $index == NS_USER_TALK;
573  }
574 
581  public function isNonincludable( $index ) {
582  $namespaces = $this->options->get( 'NonincludableNamespaces' );
583  return $namespaces && in_array( $index, $namespaces );
584  }
585 
596  public function getNamespaceContentModel( $index ) {
597  return $this->options->get( 'NamespaceContentModels' )[$index] ?? null;
598  }
599 
609  public function getRestrictionLevels( $index, User $user = null ) {
610  // PermissionManager is not injected because adding an explicit dependency
611  // breaks MW installer by adding a dependency chain on the database before
612  // it was set up. Also, the method is deprecated and will be soon removed.
613  wfDeprecated( __METHOD__, '1.34' );
614  return MediaWikiServices::getInstance()
615  ->getPermissionManager()
616  ->getNamespaceRestrictionLevels( $index, $user );
617  }
618 
628  public function getCategoryLinkType( $index ) {
629  $this->isMethodValidFor( $index, __METHOD__ );
630 
631  if ( $index == NS_CATEGORY ) {
632  return 'subcat';
633  } elseif ( $index == NS_FILE ) {
634  return 'file';
635  } else {
636  return 'page';
637  }
638  }
639 
647  public static function getCommonNamespaces() {
648  return array_keys( self::CANONICAL_NAMES );
649  }
650 }
MediaWiki\Linker\LinkTarget\getInterwiki
getInterwiki()
The interwiki component of this LinkTarget.
MediaWiki\Linker\LinkTarget\getText
getText()
Returns the link in text form, without namespace prefix or fragment.
NamespaceInfo\isMovable
isMovable( $index)
Can pages in the given namespace be moved?
Definition: NamespaceInfo.php:160
NS_HELP
const NS_HELP
Definition: Defines.php:81
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:165
NS_TEMPLATE_TALK
const NS_TEMPLATE_TALK
Definition: Defines.php:80
NamespaceInfo\isWatchable
isWatchable( $index)
Can pages in a namespace be watched?
Definition: NamespaceInfo.php:482
NamespaceInfo\makeValidNamespace
makeValidNamespace( $index, $method)
Throw if given index isn't an integer or integer-like string and so can't be a valid namespace.
Definition: NamespaceInfo.php:138
NamespaceInfo\getAssociatedPage
getAssociatedPage(LinkTarget $target)
Definition: NamespaceInfo.php:315
NamespaceInfo\hasTalkNamespace
hasTalkNamespace( $index)
Does this namespace ever have a talk namespace?
Definition: NamespaceInfo.php:449
NamespaceInfo\getValidNamespaces
getValidNamespaces()
Returns an array of the namespaces (by integer id) that exist on the wiki.
Definition: NamespaceInfo.php:428
NamespaceInfo\CANONICAL_NAMES
const CANONICAL_NAMES
Definitions of the NS_ constants are in Defines.php.
Definition: NamespaceInfo.php:64
NS_FILE
const NS_FILE
Definition: Defines.php:75
NamespaceInfo\getCanonicalNamespaces
getCanonicalNamespaces()
Returns array of all defined namespaces with their canonical (English) names.
Definition: NamespaceInfo.php:377
NS_TEMPLATE
const NS_TEMPLATE
Definition: Defines.php:79
NamespaceInfo\getCategoryLinkType
getCategoryLinkType( $index)
Returns the link type to be used for categories.
Definition: NamespaceInfo.php:628
NamespaceInfo\wantSignatures
wantSignatures( $index)
Might pages in this namespace require the use of the Signature button on the edit toolbar?
Definition: NamespaceInfo.php:471
NamespaceInfo\$options
ServiceOptions $options
Definition: NamespaceInfo.php:54
NamespaceInfo\subjectEquals
subjectEquals( $ns1, $ns2)
Returns whether the specified namespaces share the same subject.
Definition: NamespaceInfo.php:367
NamespaceInfo\isSubject
isSubject( $index)
Is the given namespace is a subject (non-talk) namespace?
Definition: NamespaceInfo.php:177
NamespaceInfo\$canonicalNamespaces
string[] null $canonicalNamespaces
Canonical namespaces cache.
Definition: NamespaceInfo.php:45
NamespaceInfo\$namespaceIndexes
array false $namespaceIndexes
Canonical namespaces index cache.
Definition: NamespaceInfo.php:48
NamespaceInfo\hasSubpages
hasSubpages( $index)
Does the namespace allow subpages?
Definition: NamespaceInfo.php:492
NS_MAIN
const NS_MAIN
Definition: Defines.php:69
ExtensionRegistry\getInstance
static getInstance()
Definition: ExtensionRegistry.php:136
NS_SPECIAL
const NS_SPECIAL
Definition: Defines.php:58
MediaWiki\Linker\LinkTarget\getNamespace
getNamespace()
Get the namespace index.
NamespaceInfo\$hookRunner
HookRunner $hookRunner
Definition: NamespaceInfo.php:57
MWException
MediaWiki exception.
Definition: MWException.php:29
NS_PROJECT
const NS_PROJECT
Definition: Defines.php:73
NamespaceInfo\getSubjectNamespaces
getSubjectNamespaces()
List all namespace indices which are considered subject, aka not a talk or special namespace.
Definition: NamespaceInfo.php:518
MediaWiki\Config\ServiceOptions
A class for passing options to services.
Definition: ServiceOptions.php:27
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Definition: GlobalFunctions.php:1027
NamespaceInfo\getSubject
getSubject( $index)
Get the subject namespace index for a given namespace Special namespaces (NS_MEDIA,...
Definition: NamespaceInfo.php:267
NamespaceInfo\getSubjectPage
getSubjectPage(LinkTarget $target)
Definition: NamespaceInfo.php:284
NamespaceInfo\getContentNamespaces
getContentNamespaces()
Get a list of all namespace indices which are considered to contain content.
Definition: NamespaceInfo.php:500
NamespaceInfo\canHaveTalkPage
canHaveTalkPage(LinkTarget $target)
Can the title have a corresponding talk page?
Definition: NamespaceInfo.php:248
NS_MEDIAWIKI_TALK
const NS_MEDIAWIKI_TALK
Definition: Defines.php:78
NamespaceInfo\getTalkNamespaces
getTalkNamespaces()
List all namespace indices which are considered talks, aka not a subject or special namespace.
Definition: NamespaceInfo.php:531
NamespaceInfo\isContent
isContent( $index)
Does this namespace contain content, for the purposes of calculating statistics, etc?
Definition: NamespaceInfo.php:460
NamespaceInfo\getTalk
getTalk( $index)
Get the talk namespace index for a given namespace.
Definition: NamespaceInfo.php:202
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:83
NamespaceInfo\CONSTRUCTOR_OPTIONS
const CONSTRUCTOR_OPTIONS
Definition: NamespaceInfo.php:88
NS_USER_TALK
const NS_USER_TALK
Definition: Defines.php:72
NS_MEDIA
const NS_MEDIA
Definition: Defines.php:57
NamespaceInfo\exists
exists( $index)
Returns whether the specified namespace exists.
Definition: NamespaceInfo.php:335
NamespaceInfo\$alwaysCapitalizedNamespaces
$alwaysCapitalizedNamespaces
These namespaces should always be first-letter capitalized, now and forevermore.
Definition: NamespaceInfo.php:42
MediaWiki\Linker\LinkTarget\getDBkey
getDBkey()
Get the main part with underscores.
NS_PROJECT_TALK
const NS_PROJECT_TALK
Definition: Defines.php:74
NamespaceInfo\getCanonicalName
getCanonicalName( $index)
Returns the canonical (English) name for a given index.
Definition: NamespaceInfo.php:397
NamespaceInfo\isTalk
isTalk( $index)
Is the given namespace a talk namespace?
Definition: NamespaceInfo.php:187
NamespaceInfo\__construct
__construct(ServiceOptions $options, HookContainer $hookContainer)
Definition: NamespaceInfo.php:104
NamespaceInfo\getNamespaceContentModel
getNamespaceContentModel( $index)
Get the default content model for a namespace This does not mean that all pages in that namespace hav...
Definition: NamespaceInfo.php:596
NamespaceInfo\hasGenderDistinction
hasGenderDistinction( $index)
Does the namespace (potentially) have different aliases for different genders.
Definition: NamespaceInfo.php:571
NamespaceInfo\equals
equals( $ns1, $ns2)
Returns whether the specified namespaces are the same namespace.
Definition: NamespaceInfo.php:353
NamespaceInfo\isNonincludable
isNonincludable( $index)
It is not possible to use pages from this namespace as template?
Definition: NamespaceInfo.php:581
NamespaceInfo\getCanonicalIndex
getCanonicalIndex( $name)
Returns the index for a given canonical name, or NULL The input must be converted to lower case first...
Definition: NamespaceInfo.php:409
NS_HELP_TALK
const NS_HELP_TALK
Definition: Defines.php:82
NamespaceInfo\isCapitalized
isCapitalized( $index)
Is the namespace first-letter capitalized?
Definition: NamespaceInfo.php:544
NS_USER
const NS_USER
Definition: Defines.php:71
NS_TALK
const NS_TALK
Definition: Defines.php:70
NamespaceInfo\getTalkPage
getTalkPage(LinkTarget $target)
Get a LinkTarget referring to the talk page of $target.
Definition: NamespaceInfo.php:220
MediaWiki\HookContainer\HookContainer
HookContainer class.
Definition: HookContainer.php:45
NamespaceInfo
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
Definition: NamespaceInfo.php:35
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition: Defines.php:77
MediaWiki\HookContainer\HookRunner
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Definition: HookRunner.php:567
NamespaceInfo\$validNamespaces
int[] null $validNamespaces
Valid namespaces cache.
Definition: NamespaceInfo.php:51
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
NS_FILE_TALK
const NS_FILE_TALK
Definition: Defines.php:76
NS_CATEGORY_TALK
const NS_CATEGORY_TALK
Definition: Defines.php:84
NamespaceInfo\getCommonNamespaces
static getCommonNamespaces()
Retrieve the indexes for the namespaces defined by core.
Definition: NamespaceInfo.php:647
NamespaceInfo\isMethodValidFor
isMethodValidFor( $index, $method)
Throw an exception when trying to get the subject or talk page for a given namespace where it does no...
Definition: NamespaceInfo.php:122
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:56
NamespaceInfo\getAssociated
getAssociated( $index)
Get the associated namespace.
Definition: NamespaceInfo.php:300
NamespaceInfo\getRestrictionLevels
getRestrictionLevels( $index, User $user=null)
Determine which restriction levels it makes sense to use in a namespace, optionally filtered by a use...
Definition: NamespaceInfo.php:609
MediaWiki\Config\ServiceOptions\assertRequiredOptions
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys,...
Definition: ServiceOptions.php:66
TitleValue
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:39