3 * Provide things related to namespaces
8 * This is a utility class with only static functions
9 * for dealing with namespaces that encodes all the
10 * "magic" behaviors of them based on index. The textual
11 * names of the namespaces are handled by Language.php.
13 * These are synonyms for the names given in the language file
14 * Users and translators should not change them
21 * These namespaces should always be first-letter capitalized, now and
22 * forevermore. Historically, they could've probably been lowercased too,
23 * but some things are just too ingrained now. :)
25 private static $alwaysCapitalizedNamespaces = array( NS_SPECIAL
, NS_USER
, NS_MEDIAWIKI
);
28 * Throw an exception when trying to get the subject or talk page
29 * for a given namespace where it does not make sense.
30 * Special namespaces are defined in includes/Defines.php and have
31 * a value below 0 (ex: NS_SPECIAL = -1 , NS_MEDIA = -2)
38 private static function isMethodValidFor( $index, $method ) {
39 if ( $index < NS_MAIN
) {
40 throw new MWException( "$method does not make any sense for given namespace $index" );
46 * Can pages in the given namespace be moved?
48 * @param $index Int: namespace index
51 public static function isMovable( $index ) {
52 global $wgAllowImageMoving;
53 return !( $index < NS_MAIN ||
( $index == NS_FILE
&& !$wgAllowImageMoving ) ||
$index == NS_CATEGORY
);
57 * Is the given namespace is a subject (non-talk) namespace?
59 * @param $index Int: namespace index
63 public static function isSubject( $index ) {
64 return !self
::isTalk( $index );
68 * @see self::isSubject
69 * @deprecated Please use the more consistently named isSubject (since 1.19)
71 public static function isMain( $index ) {
72 wfDeprecated( __METHOD__
, '1.19' );
73 return self
::isSubject( $index );
77 * Is the given namespace a talk namespace?
79 * @param $index Int: namespace index
82 public static function isTalk( $index ) {
83 return $index > NS_MAIN
88 * Get the talk namespace index for a given namespace
90 * @param $index Int: namespace index
93 public static function getTalk( $index ) {
94 self
::isMethodValidFor( $index, __METHOD__
);
95 return self
::isTalk( $index )
101 * Get the subject namespace index for a given namespace
102 * Special namespaces (NS_MEDIA, NS_SPECIAL) are always the subject.
104 * @param $index Int: Namespace index
107 public static function getSubject( $index ) {
108 # Handle special namespaces
109 if ( $index < NS_MAIN
) {
113 return self
::isTalk( $index )
119 * Get the associated namespace.
120 * For talk namespaces, returns the subject (non-talk) namespace
121 * For subject (non-talk) namespaces, returns the talk namespace
123 * @param $index Int: namespace index
124 * @return int or null if no associated namespace could be found
126 public static function getAssociated( $index ) {
127 self
::isMethodValidFor( $index, __METHOD__
);
129 if ( self
::isSubject( $index ) ) {
130 return self
::getTalk( $index );
131 } elseif ( self
::isTalk( $index ) ) {
132 return self
::getSubject( $index );
139 * Returns whether the specified namespace exists
146 public static function exists( $index ) {
147 $nslist = self
::getCanonicalNamespaces();
148 return isset( $nslist[$index] );
152 * Returns whether the specified namespaces are the same namespace
154 * @note It's possible that in the future we may start using something
155 * other than just namespace indexes. Under that circumstance making use
156 * of this function rather than directly doing comparison will make
157 * sure that code will not potentially break.
159 * @param $ns1 int The first namespace index
160 * @param $ns2 int The second namespae index
165 public static function equals( $ns1, $ns2 ) {
170 * Returns whether the specified namespaces share the same subject.
171 * eg: NS_USER and NS_USER wil return true, as well
172 * NS_USER and NS_USER_TALK will return true.
174 * @param $ns1 int The first namespace index
175 * @param $ns2 int The second namespae index
180 public static function subjectEquals( $ns1, $ns2 ) {
181 return self
::getSubject( $ns1 ) == self
::getSubject( $ns2 );
185 * Returns array of all defined namespaces with their canonical
191 public static function getCanonicalNamespaces() {
192 static $namespaces = null;
193 if ( $namespaces === null ) {
194 global $wgExtraNamespaces, $wgCanonicalNamespaceNames;
195 $namespaces = array( NS_MAIN
=> '' ) +
$wgCanonicalNamespaceNames;
196 if ( is_array( $wgExtraNamespaces ) ) {
197 $namespaces +
= $wgExtraNamespaces;
199 wfRunHooks( 'CanonicalNamespaces', array( &$namespaces ) );
205 * Returns the canonical (English) name for a given index
207 * @param $index Int: namespace index
208 * @return string or false if no canonical definition.
210 public static function getCanonicalName( $index ) {
211 $nslist = self
::getCanonicalNamespaces();
212 if ( isset( $nslist[$index] ) ) {
213 return $nslist[$index];
220 * Returns the index for a given canonical name, or NULL
221 * The input *must* be converted to lower case first
223 * @param $name String: namespace name
226 public static function getCanonicalIndex( $name ) {
227 static $xNamespaces = false;
228 if ( $xNamespaces === false ) {
229 $xNamespaces = array();
230 foreach ( self
::getCanonicalNamespaces() as $i => $text ) {
231 $xNamespaces[strtolower( $text )] = $i;
234 if ( array_key_exists( $name, $xNamespaces ) ) {
235 return $xNamespaces[$name];
242 * Returns an array of the namespaces (by integer id) that exist on the
243 * wiki. Used primarily by the api in help documentation.
246 public static function getValidNamespaces() {
247 static $mValidNamespaces = null;
249 if ( is_null( $mValidNamespaces ) ) {
250 foreach ( array_keys( self
::getCanonicalNamespaces() ) as $ns ) {
252 $mValidNamespaces[] = $ns;
257 return $mValidNamespaces;
261 * Can this namespace ever have a talk namespace?
263 * @param $index Int: namespace index
266 public static function canTalk( $index ) {
267 return $index >= NS_MAIN
;
271 * Does this namespace contain content, for the purposes of calculating
274 * @param $index Int: index to check
277 public static function isContent( $index ) {
278 global $wgContentNamespaces;
279 return $index == NS_MAIN ||
in_array( $index, $wgContentNamespaces );
283 * Can pages in a namespace be watched?
288 public static function isWatchable( $index ) {
289 return $index >= NS_MAIN
;
293 * Does the namespace allow subpages?
295 * @param $index int Index to check
298 public static function hasSubpages( $index ) {
299 global $wgNamespacesWithSubpages;
300 return !empty( $wgNamespacesWithSubpages[$index] );
304 * Get a list of all namespace indices which are considered to contain content
305 * @return array of namespace indices
307 public static function getContentNamespaces() {
308 global $wgContentNamespaces;
309 if ( !is_array( $wgContentNamespaces ) ||
$wgContentNamespaces === array() ) {
311 } elseif ( !in_array( NS_MAIN
, $wgContentNamespaces ) ) {
312 // always force NS_MAIN to be part of array (to match the algorithm used by isContent)
313 return array_merge( array( NS_MAIN
), $wgContentNamespaces );
315 return $wgContentNamespaces;
319 * Is the namespace first-letter capitalized?
321 * @param $index int Index to check
324 public static function isCapitalized( $index ) {
325 global $wgCapitalLinks, $wgCapitalLinkOverrides;
326 // Turn NS_MEDIA into NS_FILE
327 $index = $index === NS_MEDIA ? NS_FILE
: $index;
329 // Make sure to get the subject of our namespace
330 $index = self
::getSubject( $index );
332 // Some namespaces are special and should always be upper case
333 if ( in_array( $index, self
::$alwaysCapitalizedNamespaces ) ) {
336 if ( isset( $wgCapitalLinkOverrides[ $index ] ) ) {
337 // $wgCapitalLinkOverrides is explicitly set
338 return $wgCapitalLinkOverrides[ $index ];
340 // Default to the global setting
341 return $wgCapitalLinks;
345 * Does the namespace (potentially) have different aliases for different
346 * genders. Not all languages make a distinction here.
349 * @param $index int Index to check
352 public static function hasGenderDistinction( $index ) {
353 return $index == NS_USER ||
$index == NS_USER_TALK
;