3 * Provide things related to namespaces
8 * Definitions of the NS_ constants are in Defines.php
11 $wgCanonicalNamespaceNames = array(
13 NS_SPECIAL
=> 'Special',
16 NS_USER_TALK
=> 'User_talk',
17 NS_PROJECT
=> 'Project',
18 NS_PROJECT_TALK
=> 'Project_talk',
20 NS_FILE_TALK
=> 'File_talk',
21 NS_MEDIAWIKI
=> 'MediaWiki',
22 NS_MEDIAWIKI_TALK
=> 'MediaWiki_talk',
23 NS_TEMPLATE
=> 'Template',
24 NS_TEMPLATE_TALK
=> 'Template_talk',
26 NS_HELP_TALK
=> 'Help_talk',
27 NS_CATEGORY
=> 'Category',
28 NS_CATEGORY_TALK
=> 'Category_talk',
32 if( is_array( $wgExtraNamespaces ) ) {
33 $wgCanonicalNamespaceNames = $wgCanonicalNamespaceNames +
$wgExtraNamespaces;
37 * This is a utility class with only static functions
38 * for dealing with namespaces that encodes all the
39 * "magic" behaviors of them based on index. The textual
40 * names of the namespaces are handled by Language.php.
42 * These are synonyms for the names given in the language file
43 * Users and translators should not change them
50 * These namespaces should always be first-letter capitalized, now and
51 * forevermore. Historically, they could've probably been lowercased too,
52 * but some things are just too ingrained now. :)
54 private static $alwaysCapitalizedNamespaces = array( NS_SPECIAL
, NS_USER
, NS_MEDIAWIKI
);
57 * Throw an exception when trying to get the subject or talk page
58 * for a given namespace where it does not make sense.
59 * Special namespaces are defined in includes/define.php and have
60 * a value below 0 (ex: NS_SPECIAL = -1 , NS_MEDIA = -2)
62 * @param $ns Int: namespace index
64 private static function isMethodValidFor( $index, $method ) {
65 if( $index < NS_MAIN
) {
66 throw new MWException( "$method does not make any sense for given namespace $index" );
73 * Can pages in the given namespace be moved?
75 * @param $index Int: namespace index
78 public static function isMovable( $index ) {
79 global $wgAllowImageMoving;
80 return !( $index < NS_MAIN ||
($index == NS_FILE
&& !$wgAllowImageMoving) ||
$index == NS_CATEGORY
);
84 * Is the given namespace is a subject (non-talk) namespace?
86 * @param $index Int: namespace index
89 public static function isMain( $index ) {
90 return !self
::isTalk( $index );
94 * Is the given namespace a talk namespace?
96 * @param $index Int: namespace index
99 public static function isTalk( $index ) {
100 return $index > NS_MAIN
105 * Get the talk namespace index for a given namespace
107 * @param $index Int: namespace index
110 public static function getTalk( $index ) {
111 self
::isMethodValidFor( $index, __METHOD__
);
112 return self
::isTalk( $index )
118 * Get the subject namespace index for a given namespace
119 * Special namespaces (NS_MEDIA, NS_SPECIAL) are always the subject.
121 * @param $index Int: Namespace index
124 public static function getSubject( $index ) {
125 # Handle special namespaces
126 if( $index < NS_MAIN
) {
130 return self
::isTalk( $index )
136 * Get the associated namespace.
137 * For talk namespaces, returns the subject (non-talk) namespace
138 * For subject (non-talk) namespaces, returns the talk namespace
140 * @param $index Int: namespace index
141 * @return int or null if no associated namespace could be found
143 public static function getAssociated( $index ) {
144 self
::isMethodValidFor( $index, __METHOD__
);
146 if( self
::isMain( $index ) ) {
147 return self
::getTalk( $index );
148 } elseif( self
::isTalk( $index ) ) {
149 return self
::getSubject( $index );
156 * Returns whether the specified namespace exists
158 public static function exists( $index ) {
159 $nslist = self
::getCanonicalNamespaces();
160 return isset( $nslist[$index] );
164 * Returns array of all defined namespaces with their canonical
170 public static function getCanonicalNamespaces() {
171 static $namespaces = null;
172 if ( $namespaces === null ) {
173 global $wgExtraNamespaces, $wgCanonicalNamespaceNames;
174 $namespaces = array( NS_MAIN
=> '' ) +
$wgCanonicalNamespaceNames;
175 if ( is_array( $wgExtraNamespaces ) ) {
176 $namespaces +
= $wgExtraNamespaces;
178 wfRunHooks( 'CanonicalNamespaces', array( &$namespaces ) );
184 * Returns the canonical (English) name for a given index
186 * @param $index Int: namespace index
187 * @return string or false if no canonical definition.
189 public static function getCanonicalName( $index ) {
190 $nslist = self
::getCanonicalNamespaces();
191 if( isset( $nslist[$index] ) ) {
192 return $nslist[$index];
199 * Returns the index for a given canonical name, or NULL
200 * The input *must* be converted to lower case first
202 * @param $name String: namespace name
205 public static function getCanonicalIndex( $name ) {
206 static $xNamespaces = false;
207 if ( $xNamespaces === false ) {
208 $xNamespaces = array();
209 foreach ( self
::getCanonicalNamespaces() as $i => $text ) {
210 $xNamespaces[strtolower($text)] = $i;
213 if ( array_key_exists( $name, $xNamespaces ) ) {
214 return $xNamespaces[$name];
221 * Returns an array of the namespaces (by integer id) that exist on the
222 * wiki. Used primarily by the api in help documentation.
225 public static function getValidNamespaces() {
226 static $mValidNamespaces = null;
228 if ( is_null( $mValidNamespaces ) ) {
229 foreach ( array_keys( self
::getCanonicalNamespaces() ) as $ns ) {
231 $mValidNamespaces[] = $ns;
236 return $mValidNamespaces;
240 * Can this namespace ever have a talk namespace?
242 * @param $index Int: namespace index
245 public static function canTalk( $index ) {
246 return $index >= NS_MAIN
;
250 * Does this namespace contain content, for the purposes of calculating
253 * @param $index Int: index to check
256 public static function isContent( $index ) {
257 global $wgContentNamespaces;
258 return $index == NS_MAIN ||
in_array( $index, $wgContentNamespaces );
262 * Can pages in a namespace be watched?
267 public static function isWatchable( $index ) {
268 return $index >= NS_MAIN
;
272 * Does the namespace allow subpages?
274 * @param $index int Index to check
277 public static function hasSubpages( $index ) {
278 global $wgNamespacesWithSubpages;
279 return !empty( $wgNamespacesWithSubpages[$index] );
283 * Get a list of all namespace indices which are considered to contain content
284 * @return array of namespace indices
286 public static function getContentNamespaces() {
287 global $wgContentNamespaces;
288 if( !is_array( $wgContentNamespaces ) ||
$wgContentNamespaces === array() ) {
290 } elseif ( !in_array( NS_MAIN
, $wgContentNamespaces ) ) {
291 // always force NS_MAIN to be part of array (to match the algorithm used by isContent)
292 return array_merge( array( NS_MAIN
), $wgContentNamespaces );
294 return $wgContentNamespaces;
298 * Is the namespace first-letter capitalized?
300 * @param $index int Index to check
303 public static function isCapitalized( $index ) {
304 global $wgCapitalLinks, $wgCapitalLinkOverrides;
305 // Turn NS_MEDIA into NS_FILE
306 $index = $index === NS_MEDIA ? NS_FILE
: $index;
308 // Make sure to get the subject of our namespace
309 $index = self
::getSubject( $index );
311 // Some namespaces are special and should always be upper case
312 if ( in_array( $index, self
::$alwaysCapitalizedNamespaces ) ) {
315 if ( isset( $wgCapitalLinkOverrides[ $index ] ) ) {
316 // $wgCapitalLinkOverrides is explicitly set
317 return $wgCapitalLinkOverrides[ $index ];
319 // Default to the global setting
320 return $wgCapitalLinks;
324 * Does the namespace (potentially) have different aliases for different
325 * genders. Not all languages make a distinction here.
328 * @param $index int Index to check
331 public static function hasGenderDistinction( $index ) {
332 return $index == NS_USER ||
$index == NS_USER_TALK
;