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;
54 $result = !( $index < NS_MAIN ||
( $index == NS_FILE
&& !$wgAllowImageMoving ) ||
$index == NS_CATEGORY
);
59 wfRunHooks( 'NamespaceIsMovable', array( $index, &$result ) );
65 * Is the given namespace is a subject (non-talk) namespace?
67 * @param $index Int: namespace index
71 public static function isSubject( $index ) {
72 return !self
::isTalk( $index );
76 * @see self::isSubject
77 * @deprecated Please use the more consistently named isSubject (since 1.19)
80 public static function isMain( $index ) {
81 wfDeprecated( __METHOD__
, '1.19' );
82 return self
::isSubject( $index );
86 * Is the given namespace a talk namespace?
88 * @param $index Int: namespace index
91 public static function isTalk( $index ) {
92 return $index > NS_MAIN
97 * Get the talk namespace index for a given namespace
99 * @param $index Int: namespace index
102 public static function getTalk( $index ) {
103 self
::isMethodValidFor( $index, __METHOD__
);
104 return self
::isTalk( $index )
110 * Get the subject namespace index for a given namespace
111 * Special namespaces (NS_MEDIA, NS_SPECIAL) are always the subject.
113 * @param $index Int: Namespace index
116 public static function getSubject( $index ) {
117 # Handle special namespaces
118 if ( $index < NS_MAIN
) {
122 return self
::isTalk( $index )
128 * Get the associated namespace.
129 * For talk namespaces, returns the subject (non-talk) namespace
130 * For subject (non-talk) namespaces, returns the talk namespace
132 * @param $index Int: namespace index
133 * @return int or null if no associated namespace could be found
135 public static function getAssociated( $index ) {
136 self
::isMethodValidFor( $index, __METHOD__
);
138 if ( self
::isSubject( $index ) ) {
139 return self
::getTalk( $index );
140 } elseif ( self
::isTalk( $index ) ) {
141 return self
::getSubject( $index );
148 * Returns whether the specified namespace exists
155 public static function exists( $index ) {
156 $nslist = self
::getCanonicalNamespaces();
157 return isset( $nslist[$index] );
161 * Returns whether the specified namespaces are the same namespace
163 * @note It's possible that in the future we may start using something
164 * other than just namespace indexes. Under that circumstance making use
165 * of this function rather than directly doing comparison will make
166 * sure that code will not potentially break.
168 * @param $ns1 int The first namespace index
169 * @param $ns2 int The second namespae index
174 public static function equals( $ns1, $ns2 ) {
179 * Returns whether the specified namespaces share the same subject.
180 * eg: NS_USER and NS_USER wil return true, as well
181 * NS_USER and NS_USER_TALK will return true.
183 * @param $ns1 int The first namespace index
184 * @param $ns2 int The second namespae index
189 public static function subjectEquals( $ns1, $ns2 ) {
190 return self
::getSubject( $ns1 ) == self
::getSubject( $ns2 );
194 * Returns array of all defined namespaces with their canonical
200 public static function getCanonicalNamespaces() {
201 static $namespaces = null;
202 if ( $namespaces === null ) {
203 global $wgExtraNamespaces, $wgCanonicalNamespaceNames;
204 $namespaces = array( NS_MAIN
=> '' ) +
$wgCanonicalNamespaceNames;
205 if ( is_array( $wgExtraNamespaces ) ) {
206 $namespaces +
= $wgExtraNamespaces;
208 wfRunHooks( 'CanonicalNamespaces', array( &$namespaces ) );
214 * Returns the canonical (English) name for a given index
216 * @param $index Int: namespace index
217 * @return string or false if no canonical definition.
219 public static function getCanonicalName( $index ) {
220 $nslist = self
::getCanonicalNamespaces();
221 if ( isset( $nslist[$index] ) ) {
222 return $nslist[$index];
229 * Returns the index for a given canonical name, or NULL
230 * The input *must* be converted to lower case first
232 * @param $name String: namespace name
235 public static function getCanonicalIndex( $name ) {
236 static $xNamespaces = false;
237 if ( $xNamespaces === false ) {
238 $xNamespaces = array();
239 foreach ( self
::getCanonicalNamespaces() as $i => $text ) {
240 $xNamespaces[strtolower( $text )] = $i;
243 if ( array_key_exists( $name, $xNamespaces ) ) {
244 return $xNamespaces[$name];
251 * Returns an array of the namespaces (by integer id) that exist on the
252 * wiki. Used primarily by the api in help documentation.
255 public static function getValidNamespaces() {
256 static $mValidNamespaces = null;
258 if ( is_null( $mValidNamespaces ) ) {
259 foreach ( array_keys( self
::getCanonicalNamespaces() ) as $ns ) {
261 $mValidNamespaces[] = $ns;
266 return $mValidNamespaces;
270 * Can this namespace ever have a talk namespace?
272 * @param $index Int: namespace index
275 public static function canTalk( $index ) {
276 return $index >= NS_MAIN
;
280 * Does this namespace contain content, for the purposes of calculating
283 * @param $index Int: index to check
286 public static function isContent( $index ) {
287 global $wgContentNamespaces;
288 return $index == NS_MAIN ||
in_array( $index, $wgContentNamespaces );
292 * Can pages in a namespace be watched?
297 public static function isWatchable( $index ) {
298 return $index >= NS_MAIN
;
302 * Does the namespace allow subpages?
304 * @param $index int Index to check
307 public static function hasSubpages( $index ) {
308 global $wgNamespacesWithSubpages;
309 return !empty( $wgNamespacesWithSubpages[$index] );
313 * Get a list of all namespace indices which are considered to contain content
314 * @return array of namespace indices
316 public static function getContentNamespaces() {
317 global $wgContentNamespaces;
318 if ( !is_array( $wgContentNamespaces ) ||
$wgContentNamespaces === array() ) {
320 } elseif ( !in_array( NS_MAIN
, $wgContentNamespaces ) ) {
321 // always force NS_MAIN to be part of array (to match the algorithm used by isContent)
322 return array_merge( array( NS_MAIN
), $wgContentNamespaces );
324 return $wgContentNamespaces;
328 * Is the namespace first-letter capitalized?
330 * @param $index int Index to check
333 public static function isCapitalized( $index ) {
334 global $wgCapitalLinks, $wgCapitalLinkOverrides;
335 // Turn NS_MEDIA into NS_FILE
336 $index = $index === NS_MEDIA ? NS_FILE
: $index;
338 // Make sure to get the subject of our namespace
339 $index = self
::getSubject( $index );
341 // Some namespaces are special and should always be upper case
342 if ( in_array( $index, self
::$alwaysCapitalizedNamespaces ) ) {
345 if ( isset( $wgCapitalLinkOverrides[ $index ] ) ) {
346 // $wgCapitalLinkOverrides is explicitly set
347 return $wgCapitalLinkOverrides[ $index ];
349 // Default to the global setting
350 return $wgCapitalLinks;
354 * Does the namespace (potentially) have different aliases for different
355 * genders. Not all languages make a distinction here.
358 * @param $index int Index to check
361 public static function hasGenderDistinction( $index ) {
362 return $index == NS_USER ||
$index == NS_USER_TALK
;
366 * It is not possible to use pages from this namespace as template?
369 * @param $index int Index to check
372 public static function isNonincludableNamespace( $index ) {
373 global $wgNonincludableNamespaces;
374 return $wgNonincludableNamespaces && in_array( $index, $wgNonincludableNamespaces );