MediaWiki  master
NamespaceInfo.php
Go to the documentation of this file.
1 <?php
28 
36 
42  private $alwaysCapitalizedNamespaces = [ NS_SPECIAL, NS_USER, NS_MEDIAWIKI ];
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  MainConfigNames::CanonicalNamespaceNames,
90  MainConfigNames::CapitalLinkOverrides,
91  MainConfigNames::CapitalLinks,
92  MainConfigNames::ContentNamespaces,
93  MainConfigNames::ExtraNamespaces,
94  MainConfigNames::ExtraSignatureNamespaces,
95  MainConfigNames::NamespaceContentModels,
96  MainConfigNames::NamespacesWithSubpages,
97  MainConfigNames::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  $extensionRegistry = ExtensionRegistry::getInstance();
162  $extNamespaces = $extensionRegistry->getAttribute( 'ImmovableNamespaces' );
163 
164  $result = $index >= NS_MAIN && !in_array( $index, $extNamespaces );
165 
169  $this->hookRunner->onNamespaceIsMovable( $index, $result );
170 
171  return $result;
172  }
173 
180  public function isSubject( $index ) {
181  return !$this->isTalk( $index );
182  }
183 
190  public function isTalk( $index ) {
191  $index = $this->makeValidNamespace( $index, __METHOD__ );
192 
193  return $index > NS_MAIN
194  && $index % 2;
195  }
196 
205  public function getTalk( $index ) {
206  $index = $this->makeValidNamespace( $index, __METHOD__ );
207 
208  $this->isMethodValidFor( $index, __METHOD__ );
209  return $this->isTalk( $index )
210  ? $index
211  : $index + 1;
212  }
213 
223  public function getTalkPage( LinkTarget $target ): LinkTarget {
224  if ( $target->getText() === '' ) {
225  throw new MWException( 'Can\'t determine talk page associated with relative section link' );
226  }
227 
228  if ( $target->getInterwiki() !== '' ) {
229  throw new MWException( 'Can\'t determine talk page associated with interwiki link' );
230  }
231 
232  if ( $this->isTalk( $target->getNamespace() ) ) {
233  return $target;
234  }
235 
236  // NOTE: getTalk throws on bad namespaces!
237  return new TitleValue( $this->getTalk( $target->getNamespace() ), $target->getDBkey() );
238  }
239 
251  public function canHaveTalkPage( LinkTarget $target ) {
252  if ( $target->getText() === '' || $target->getInterwiki() !== '' ) {
253  return false;
254  }
255 
256  if ( $target->getNamespace() < NS_MAIN ) {
257  return false;
258  }
259 
260  return true;
261  }
262 
270  public function getSubject( $index ) {
271  $index = $this->makeValidNamespace( $index, __METHOD__ );
272 
273  # Handle special namespaces
274  if ( $index < NS_MAIN ) {
275  return $index;
276  }
277 
278  return $this->isTalk( $index )
279  ? $index - 1
280  : $index;
281  }
282 
287  public function getSubjectPage( LinkTarget $target ): LinkTarget {
288  if ( $this->isSubject( $target->getNamespace() ) ) {
289  return $target;
290  }
291  return new TitleValue( $this->getSubject( $target->getNamespace() ), $target->getDBkey() );
292  }
293 
303  public function getAssociated( $index ) {
304  $this->isMethodValidFor( $index, __METHOD__ );
305 
306  if ( $this->isSubject( $index ) ) {
307  return $this->getTalk( $index );
308  }
309  return $this->getSubject( $index );
310  }
311 
318  public function getAssociatedPage( LinkTarget $target ): LinkTarget {
319  if ( $target->getText() === '' ) {
320  throw new MWException( 'Can\'t determine talk page associated with relative section link' );
321  }
322 
323  if ( $target->getInterwiki() !== '' ) {
324  throw new MWException( 'Can\'t determine talk page associated with interwiki link' );
325  }
326 
327  return new TitleValue(
328  $this->getAssociated( $target->getNamespace() ), $target->getDBkey() );
329  }
330 
338  public function exists( $index ) {
339  $nslist = $this->getCanonicalNamespaces();
340  return isset( $nslist[$index] );
341  }
342 
356  public function equals( $ns1, $ns2 ) {
357  return $ns1 == $ns2;
358  }
359 
370  public function subjectEquals( $ns1, $ns2 ) {
371  return $this->getSubject( $ns1 ) == $this->getSubject( $ns2 );
372  }
373 
380  public function getCanonicalNamespaces() {
381  if ( $this->canonicalNamespaces === null ) {
382  $this->canonicalNamespaces =
383  [ NS_MAIN => '' ] + $this->options->get( MainConfigNames::CanonicalNamespaceNames );
384  $this->canonicalNamespaces +=
385  ExtensionRegistry::getInstance()->getAttribute( 'ExtensionNamespaces' );
386  if ( is_array( $this->options->get( MainConfigNames::ExtraNamespaces ) ) ) {
387  $this->canonicalNamespaces += $this->options->get( MainConfigNames::ExtraNamespaces );
388  }
389  $this->hookRunner->onCanonicalNamespaces( $this->canonicalNamespaces );
390  }
391  return $this->canonicalNamespaces;
392  }
393 
400  public function getCanonicalName( $index ) {
401  $nslist = $this->getCanonicalNamespaces();
402  return $nslist[$index] ?? false;
403  }
404 
412  public function getCanonicalIndex( $name ) {
413  if ( $this->namespaceIndexes === false ) {
414  $this->namespaceIndexes = [];
415  foreach ( $this->getCanonicalNamespaces() as $i => $text ) {
416  $this->namespaceIndexes[strtolower( $text )] = $i;
417  }
418  }
419  if ( array_key_exists( $name, $this->namespaceIndexes ) ) {
420  return $this->namespaceIndexes[$name];
421  } else {
422  return null;
423  }
424  }
425 
431  public function getValidNamespaces() {
432  if ( $this->validNamespaces === null ) {
433  $this->validNamespaces = [];
434  foreach ( array_keys( $this->getCanonicalNamespaces() ) as $ns ) {
435  if ( $ns >= 0 ) {
436  $this->validNamespaces[] = $ns;
437  }
438  }
439  // T109137: sort numerically
440  sort( $this->validNamespaces, SORT_NUMERIC );
441  }
442 
443  return $this->validNamespaces;
444  }
445 
452  public function hasTalkNamespace( $index ) {
453  return $index >= NS_MAIN;
454  }
455 
463  public function isContent( $index ) {
464  return $index == NS_MAIN ||
465  in_array( $index, $this->options->get( MainConfigNames::ContentNamespaces ) );
466  }
467 
475  public function wantSignatures( $index ) {
476  return $this->isTalk( $index ) ||
477  in_array( $index, $this->options->get( MainConfigNames::ExtraSignatureNamespaces ) );
478  }
479 
486  public function isWatchable( $index ) {
487  return $index >= NS_MAIN;
488  }
489 
497  public function hasSubpages( $index ) {
498  return !empty( $this->options->get( MainConfigNames::NamespacesWithSubpages )[$index] );
499  }
500 
505  public function getContentNamespaces() {
506  $contentNamespaces = $this->options->get( MainConfigNames::ContentNamespaces );
507  if ( !is_array( $contentNamespaces ) || $contentNamespaces === [] ) {
508  return [ NS_MAIN ];
509  } elseif ( !in_array( NS_MAIN, $contentNamespaces ) ) {
510  // always force NS_MAIN to be part of array (to match the algorithm used by isContent)
511  return array_merge( [ NS_MAIN ], $contentNamespaces );
512  } else {
513  return $contentNamespaces;
514  }
515  }
516 
523  public function getSubjectNamespaces() {
524  return array_filter(
525  $this->getValidNamespaces(),
526  [ $this, 'isSubject' ]
527  );
528  }
529 
536  public function getTalkNamespaces() {
537  return array_filter(
538  $this->getValidNamespaces(),
539  [ $this, 'isTalk' ]
540  );
541  }
542 
549  public function isCapitalized( $index ) {
550  // Turn NS_MEDIA into NS_FILE
551  $index = $index === NS_MEDIA ? NS_FILE : $index;
552 
553  // Make sure to get the subject of our namespace
554  $index = $this->getSubject( $index );
555 
556  // Some namespaces are special and should always be upper case
557  if ( in_array( $index, $this->alwaysCapitalizedNamespaces ) ) {
558  return true;
559  }
560  $overrides = $this->options->get( MainConfigNames::CapitalLinkOverrides );
561  if ( isset( $overrides[$index] ) ) {
562  // CapitalLinkOverrides is explicitly set
563  return $overrides[$index];
564  }
565  // Default to the global setting
566  return $this->options->get( MainConfigNames::CapitalLinks );
567  }
568 
576  public function hasGenderDistinction( $index ) {
577  return $index == NS_USER || $index == NS_USER_TALK;
578  }
579 
586  public function isNonincludable( $index ) {
587  $namespaces = $this->options->get( MainConfigNames::NonincludableNamespaces );
588  return $namespaces && in_array( $index, $namespaces );
589  }
590 
601  public function getNamespaceContentModel( $index ) {
602  return $this->options->get( MainConfigNames::NamespaceContentModels )[$index] ?? null;
603  }
604 
614  public function getCategoryLinkType( $index ) {
615  $this->isMethodValidFor( $index, __METHOD__ );
616 
617  if ( $index == NS_CATEGORY ) {
618  return 'subcat';
619  } elseif ( $index == NS_FILE ) {
620  return 'file';
621  } else {
622  return 'page';
623  }
624  }
625 
633  public static function getCommonNamespaces() {
634  return array_keys( self::CANONICAL_NAMES );
635  }
636 }
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:88
MediaWiki exception.
Definition: MWException.php:32
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...
Definition: HookRunner.php:565
A class containing constants representing the names of configuration variables.
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
getAssociatedPage(LinkTarget $target)
hasGenderDistinction( $index)
Does the namespace (potentially) have different aliases for different genders.
__construct(ServiceOptions $options, HookContainer $hookContainer)
equals( $ns1, $ns2)
Returns whether the specified namespaces are the same namespace.
wantSignatures( $index)
Might pages in this namespace require the use of the Signature button on the edit toolbar?
getNamespaceContentModel( $index)
Get the default content model for a namespace This does not mean that all pages in that namespace hav...
subjectEquals( $ns1, $ns2)
Returns whether the specified namespaces share the same subject.
isCapitalized( $index)
Is the namespace first-letter capitalized?
getCanonicalName( $index)
Returns the canonical (English) name for a given index.
getContentNamespaces()
Get a list of all namespace indices which are considered to contain content.
isNonincludable( $index)
It is not possible to use pages from this namespace as template?
getTalkNamespaces()
List all namespace indices which are considered talks, aka not a subject or special namespace.
hasTalkNamespace( $index)
Does this namespace ever have a talk namespace?
isSubject( $index)
Is the given namespace is a subject (non-talk) namespace?
getValidNamespaces()
Returns an array of the namespaces (by integer id) that exist on the wiki.
static getCommonNamespaces()
Retrieve the indexes for the namespaces defined by core.
exists( $index)
Returns whether the specified namespace exists.
isContent( $index)
Does this namespace contain content, for the purposes of calculating statistics, etc?
getSubjectPage(LinkTarget $target)
getCanonicalNamespaces()
Returns array of all defined namespaces with their canonical (English) names.
const CANONICAL_NAMES
Definitions of the NS_ constants are in Defines.php.
getSubject( $index)
Get the subject namespace index for a given namespace Special namespaces (NS_MEDIA,...
hasSubpages( $index)
Does the namespace allow subpages? Note that this refers to structured handling of subpages,...
canHaveTalkPage(LinkTarget $target)
Can the title have a corresponding talk page?
isWatchable( $index)
Can pages in a namespace be watched?
getTalk( $index)
Get the talk namespace index for a given namespace.
getSubjectNamespaces()
List all namespace indices which are considered subject, aka not a talk or special namespace.
isMovable( $index)
Can pages in the given namespace be moved?
getCategoryLinkType( $index)
Returns the link type to be used for categories.
getAssociated( $index)
Get the associated namespace.
getTalkPage(LinkTarget $target)
Get a LinkTarget referring to the talk page of $target.
const CONSTRUCTOR_OPTIONS
isTalk( $index)
Is the given namespace a talk namespace?
getCanonicalIndex( $name)
Returns the index for a given canonical name, or NULL The input must be converted to lower case first...
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:40
getInterwiki()
The interwiki component of this LinkTarget.
getNamespace()
Get the namespace index.
getText()
Get the main part of the link target, in text form.