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/define.php and have
31 * a value below 0 (ex: NS_SPECIAL = -1 , NS_MEDIA = -2)
33 * @param $ns Int: namespace index
35 private static function isMethodValidFor( $index, $method ) {
36 if( $index < NS_MAIN
) {
37 throw new MWException( "$method does not make any sense for given namespace $index" );
43 * Can pages in the given namespace be moved?
45 * @param $index Int: namespace index
48 public static function isMovable( $index ) {
49 global $wgAllowImageMoving;
50 return !( $index < NS_MAIN ||
($index == NS_FILE
&& !$wgAllowImageMoving) ||
$index == NS_CATEGORY
);
54 * Is the given namespace is a subject (non-talk) namespace?
56 * @param $index Int: namespace index
59 public static function isMain( $index ) {
60 return !self
::isTalk( $index );
64 * Is the given namespace a talk namespace?
66 * @param $index Int: namespace index
69 public static function isTalk( $index ) {
70 return $index > NS_MAIN
75 * Get the talk namespace index for a given namespace
77 * @param $index Int: namespace index
80 public static function getTalk( $index ) {
81 self
::isMethodValidFor( $index, __METHOD__
);
82 return self
::isTalk( $index )
88 * Get the subject namespace index for a given namespace
89 * Special namespaces (NS_MEDIA, NS_SPECIAL) are always the subject.
91 * @param $index Int: Namespace index
94 public static function getSubject( $index ) {
95 # Handle special namespaces
96 if( $index < NS_MAIN
) {
100 return self
::isTalk( $index )
106 * Get the associated namespace.
107 * For talk namespaces, returns the subject (non-talk) namespace
108 * For subject (non-talk) namespaces, returns the talk namespace
110 * @param $index Int: namespace index
111 * @return int or null if no associated namespace could be found
113 public static function getAssociated( $index ) {
114 self
::isMethodValidFor( $index, __METHOD__
);
116 if( self
::isMain( $index ) ) {
117 return self
::getTalk( $index );
118 } elseif( self
::isTalk( $index ) ) {
119 return self
::getSubject( $index );
126 * Returns whether the specified namespace exists
128 public static function exists( $index ) {
129 $nslist = self
::getCanonicalNamespaces();
130 return isset( $nslist[$index] );
134 * Returns array of all defined namespaces with their canonical
140 public static function getCanonicalNamespaces() {
141 static $namespaces = null;
142 if ( $namespaces === null ) {
143 global $wgExtraNamespaces, $wgCanonicalNamespaceNames;
144 $namespaces = array( NS_MAIN
=> '' ) +
$wgCanonicalNamespaceNames;
145 if ( is_array( $wgExtraNamespaces ) ) {
146 $namespaces +
= $wgExtraNamespaces;
148 wfRunHooks( 'CanonicalNamespaces', array( &$namespaces ) );
154 * Returns the canonical (English) name for a given index
156 * @param $index Int: namespace index
157 * @return string or false if no canonical definition.
159 public static function getCanonicalName( $index ) {
160 $nslist = self
::getCanonicalNamespaces();
161 if( isset( $nslist[$index] ) ) {
162 return $nslist[$index];
169 * Returns the index for a given canonical name, or NULL
170 * The input *must* be converted to lower case first
172 * @param $name String: namespace name
175 public static function getCanonicalIndex( $name ) {
176 static $xNamespaces = false;
177 if ( $xNamespaces === false ) {
178 $xNamespaces = array();
179 foreach ( self
::getCanonicalNamespaces() as $i => $text ) {
180 $xNamespaces[strtolower($text)] = $i;
183 if ( array_key_exists( $name, $xNamespaces ) ) {
184 return $xNamespaces[$name];
191 * Returns an array of the namespaces (by integer id) that exist on the
192 * wiki. Used primarily by the api in help documentation.
195 public static function getValidNamespaces() {
196 static $mValidNamespaces = null;
198 if ( is_null( $mValidNamespaces ) ) {
199 foreach ( array_keys( self
::getCanonicalNamespaces() ) as $ns ) {
201 $mValidNamespaces[] = $ns;
206 return $mValidNamespaces;
210 * Can this namespace ever have a talk namespace?
212 * @param $index Int: namespace index
215 public static function canTalk( $index ) {
216 return $index >= NS_MAIN
;
220 * Does this namespace contain content, for the purposes of calculating
223 * @param $index Int: index to check
226 public static function isContent( $index ) {
227 global $wgContentNamespaces;
228 return $index == NS_MAIN ||
in_array( $index, $wgContentNamespaces );
232 * Can pages in a namespace be watched?
237 public static function isWatchable( $index ) {
238 return $index >= NS_MAIN
;
242 * Does the namespace allow subpages?
244 * @param $index int Index to check
247 public static function hasSubpages( $index ) {
248 global $wgNamespacesWithSubpages;
249 return !empty( $wgNamespacesWithSubpages[$index] );
253 * Get a list of all namespace indices which are considered to contain content
254 * @return array of namespace indices
256 public static function getContentNamespaces() {
257 global $wgContentNamespaces;
258 if( !is_array( $wgContentNamespaces ) ||
$wgContentNamespaces === array() ) {
260 } elseif ( !in_array( NS_MAIN
, $wgContentNamespaces ) ) {
261 // always force NS_MAIN to be part of array (to match the algorithm used by isContent)
262 return array_merge( array( NS_MAIN
), $wgContentNamespaces );
264 return $wgContentNamespaces;
268 * Is the namespace first-letter capitalized?
270 * @param $index int Index to check
273 public static function isCapitalized( $index ) {
274 global $wgCapitalLinks, $wgCapitalLinkOverrides;
275 // Turn NS_MEDIA into NS_FILE
276 $index = $index === NS_MEDIA ? NS_FILE
: $index;
278 // Make sure to get the subject of our namespace
279 $index = self
::getSubject( $index );
281 // Some namespaces are special and should always be upper case
282 if ( in_array( $index, self
::$alwaysCapitalizedNamespaces ) ) {
285 if ( isset( $wgCapitalLinkOverrides[ $index ] ) ) {
286 // $wgCapitalLinkOverrides is explicitly set
287 return $wgCapitalLinkOverrides[ $index ];
289 // Default to the global setting
290 return $wgCapitalLinks;
294 * Does the namespace (potentially) have different aliases for different
295 * genders. Not all languages make a distinction here.
298 * @param $index int Index to check
301 public static function hasGenderDistinction( $index ) {
302 return $index == NS_USER ||
$index == NS_USER_TALK
;