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 return self
::isSubject( $index );
76 * Is the given namespace a talk namespace?
78 * @param $index Int: namespace index
81 public static function isTalk( $index ) {
82 return $index > NS_MAIN
87 * Get the talk namespace index for a given namespace
89 * @param $index Int: namespace index
92 public static function getTalk( $index ) {
93 self
::isMethodValidFor( $index, __METHOD__
);
94 return self
::isTalk( $index )
100 * Get the subject namespace index for a given namespace
101 * Special namespaces (NS_MEDIA, NS_SPECIAL) are always the subject.
103 * @param $index Int: Namespace index
106 public static function getSubject( $index ) {
107 # Handle special namespaces
108 if( $index < NS_MAIN
) {
112 return self
::isTalk( $index )
118 * Get the associated namespace.
119 * For talk namespaces, returns the subject (non-talk) namespace
120 * For subject (non-talk) namespaces, returns the talk namespace
122 * @param $index Int: namespace index
123 * @return int or null if no associated namespace could be found
125 public static function getAssociated( $index ) {
126 self
::isMethodValidFor( $index, __METHOD__
);
128 if( self
::isMain( $index ) ) {
129 return self
::getTalk( $index );
130 } elseif( self
::isTalk( $index ) ) {
131 return self
::getSubject( $index );
138 * Returns whether the specified namespace exists
145 public static function exists( $index ) {
146 $nslist = self
::getCanonicalNamespaces();
147 return isset( $nslist[$index] );
151 * Returns whether the specified namespaces are the same namespace
153 * @note It's possible that in the future we may start using something
154 * other than just namespace indexes. Under that circumstance making use
155 * of this function rather than directly doing comparison will make
156 * sure that code will not potentially break.
158 * @param $ns1 The first namespace index
159 * @param $ns2 The second namespae index
164 public static function equals( $ns1, $ns2 ) {
169 * Returns whether the specified namespaces share the same subject.
170 * eg: NS_USER and NS_USER wil return true, as well
171 * NS_USER and NS_USER_TALK will return true.
173 * @param $ns1 The first namespace index
174 * @param $ns2 The second namespae index
179 public static function subjectEquals( $ns1, $ns2 ) {
180 return self
::getSubject( $ns1 ) == self
::getSubject( $ns2 );
184 * Returns array of all defined namespaces with their canonical
190 public static function getCanonicalNamespaces() {
191 static $namespaces = null;
192 if ( $namespaces === null ) {
193 global $wgExtraNamespaces, $wgCanonicalNamespaceNames;
194 $namespaces = array( NS_MAIN
=> '' ) +
$wgCanonicalNamespaceNames;
195 if ( is_array( $wgExtraNamespaces ) ) {
196 $namespaces +
= $wgExtraNamespaces;
198 wfRunHooks( 'CanonicalNamespaces', array( &$namespaces ) );
204 * Returns the canonical (English) name for a given index
206 * @param $index Int: namespace index
207 * @return string or false if no canonical definition.
209 public static function getCanonicalName( $index ) {
210 $nslist = self
::getCanonicalNamespaces();
211 if( isset( $nslist[$index] ) ) {
212 return $nslist[$index];
219 * Returns the index for a given canonical name, or NULL
220 * The input *must* be converted to lower case first
222 * @param $name String: namespace name
225 public static function getCanonicalIndex( $name ) {
226 static $xNamespaces = false;
227 if ( $xNamespaces === false ) {
228 $xNamespaces = array();
229 foreach ( self
::getCanonicalNamespaces() as $i => $text ) {
230 $xNamespaces[strtolower($text)] = $i;
233 if ( array_key_exists( $name, $xNamespaces ) ) {
234 return $xNamespaces[$name];
241 * Returns an array of the namespaces (by integer id) that exist on the
242 * wiki. Used primarily by the api in help documentation.
245 public static function getValidNamespaces() {
246 static $mValidNamespaces = null;
248 if ( is_null( $mValidNamespaces ) ) {
249 foreach ( array_keys( self
::getCanonicalNamespaces() ) as $ns ) {
251 $mValidNamespaces[] = $ns;
256 return $mValidNamespaces;
260 * Can this namespace ever have a talk namespace?
262 * @param $index Int: namespace index
265 public static function canTalk( $index ) {
266 return $index >= NS_MAIN
;
270 * Does this namespace contain content, for the purposes of calculating
273 * @param $index Int: index to check
276 public static function isContent( $index ) {
277 global $wgContentNamespaces;
278 return $index == NS_MAIN ||
in_array( $index, $wgContentNamespaces );
282 * Can pages in a namespace be watched?
287 public static function isWatchable( $index ) {
288 return $index >= NS_MAIN
;
292 * Does the namespace allow subpages?
294 * @param $index int Index to check
297 public static function hasSubpages( $index ) {
298 global $wgNamespacesWithSubpages;
299 return !empty( $wgNamespacesWithSubpages[$index] );
303 * Get a list of all namespace indices which are considered to contain content
304 * @return array of namespace indices
306 public static function getContentNamespaces() {
307 global $wgContentNamespaces;
308 if( !is_array( $wgContentNamespaces ) ||
$wgContentNamespaces === array() ) {
310 } elseif ( !in_array( NS_MAIN
, $wgContentNamespaces ) ) {
311 // always force NS_MAIN to be part of array (to match the algorithm used by isContent)
312 return array_merge( array( NS_MAIN
), $wgContentNamespaces );
314 return $wgContentNamespaces;
318 * Is the namespace first-letter capitalized?
320 * @param $index int Index to check
323 public static function isCapitalized( $index ) {
324 global $wgCapitalLinks, $wgCapitalLinkOverrides;
325 // Turn NS_MEDIA into NS_FILE
326 $index = $index === NS_MEDIA ? NS_FILE
: $index;
328 // Make sure to get the subject of our namespace
329 $index = self
::getSubject( $index );
331 // Some namespaces are special and should always be upper case
332 if ( in_array( $index, self
::$alwaysCapitalizedNamespaces ) ) {
335 if ( isset( $wgCapitalLinkOverrides[ $index ] ) ) {
336 // $wgCapitalLinkOverrides is explicitly set
337 return $wgCapitalLinkOverrides[ $index ];
339 // Default to the global setting
340 return $wgCapitalLinks;
344 * Does the namespace (potentially) have different aliases for different
345 * genders. Not all languages make a distinction here.
348 * @param $index int Index to check
351 public static function hasGenderDistinction( $index ) {
352 return $index == NS_USER ||
$index == NS_USER_TALK
;