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 * Can pages in the given namespace be moved?
59 * @param $index Int: namespace index
62 public static function isMovable( $index ) {
63 global $wgAllowImageMoving;
64 return !( $index < NS_MAIN ||
($index == NS_FILE
&& !$wgAllowImageMoving) ||
$index == NS_CATEGORY
);
68 * Is the given namespace is a subject (non-talk) namespace?
70 * @param $index Int: namespace index
73 public static function isMain( $index ) {
74 return !self
::isTalk( $index );
78 * Is the given namespace a talk namespace?
80 * @param $index Int: namespace index
83 public static function isTalk( $index ) {
84 return $index > NS_MAIN
89 * Get the talk namespace index for a given namespace
91 * @param $index Int: namespace index
94 public static function getTalk( $index ) {
95 return self
::isTalk( $index )
101 * Get the subject namespace index for a given namespace
103 * @param $index Int: Namespace index
106 public static function getSubject( $index ) {
107 return self
::isTalk( $index )
113 * Returns whether the specified namespace exists
115 public static function exists( $index ) {
116 $nslist = self
::getCanonicalNamespaces();
117 return isset( $nslist[$index] );
122 * Returns array of all defined namespaces with their canonical
128 public static function getCanonicalNamespaces() {
129 static $namespaces = null;
130 if ( $namespaces === null ) {
131 global $wgExtraNamespaces, $wgCanonicalNamespaceNames;
132 if ( is_array( $wgExtraNamespaces ) ) {
133 $namespaces = $wgCanonicalNamespaceNames +
$wgExtraNamespaces;
135 $namespaces = $wgCanonicalNamespaceNames;
137 $namespaces[NS_MAIN
] = '';
138 wfRunHooks( 'CanonicalNamespaces', array( &$namespaces ) );
144 * Returns the canonical (English) name for a given index
146 * @param $index Int: namespace index
147 * @return string or false if no canonical definition.
149 public static function getCanonicalName( $index ) {
150 $nslist = self
::getCanonicalNamespaces();
151 if( isset( $nslist[$index] ) ) {
152 return $nslist[$index];
159 * Returns the index for a given canonical name, or NULL
160 * The input *must* be converted to lower case first
162 * @param $name String: namespace name
165 public static function getCanonicalIndex( $name ) {
166 static $xNamespaces = false;
167 if ( $xNamespaces === false ) {
168 $xNamespaces = array();
169 foreach ( self
::getCanonicalNamespaces() as $i => $text ) {
170 $xNamespaces[strtolower($text)] = $i;
173 if ( array_key_exists( $name, $xNamespaces ) ) {
174 return $xNamespaces[$name];
181 * Returns an array of the namespaces (by integer id) that exist on the
182 * wiki. Used primarily by the api in help documentation.
185 public static function getValidNamespaces() {
186 static $mValidNamespaces = null;
188 if ( is_null( $mValidNamespaces ) ) {
189 foreach ( array_keys( self
::getCanonicalNamespaces() ) as $ns ) {
191 $mValidNamespaces[] = $ns;
196 return $mValidNamespaces;
200 * Can this namespace ever have a talk namespace?
202 * @param $index Int: namespace index
205 public static function canTalk( $index ) {
206 return $index >= NS_MAIN
;
210 * Does this namespace contain content, for the purposes of calculating
213 * @param $index Int: index to check
216 public static function isContent( $index ) {
217 global $wgContentNamespaces;
218 return $index == NS_MAIN ||
in_array( $index, $wgContentNamespaces );
222 * Can pages in a namespace be watched?
227 public static function isWatchable( $index ) {
228 return $index >= NS_MAIN
;
232 * Does the namespace allow subpages?
234 * @param $index int Index to check
237 public static function hasSubpages( $index ) {
238 global $wgNamespacesWithSubpages;
239 return !empty( $wgNamespacesWithSubpages[$index] );
243 * Is the namespace first-letter capitalized?
245 * @param $index int Index to check
248 public static function isCapitalized( $index ) {
249 global $wgCapitalLinks, $wgCapitalLinkOverrides;
250 // Turn NS_MEDIA into NS_FILE
251 $index = $index === NS_MEDIA ? NS_FILE
: $index;
253 // Make sure to get the subject of our namespace
254 $index = self
::getSubject( $index );
256 // Some namespaces are special and should always be upper case
257 if ( in_array( $index, self
::$alwaysCapitalizedNamespaces ) ) {
260 if ( isset( $wgCapitalLinkOverrides[ $index ] ) ) {
261 // $wgCapitalLinkOverrides is explicitly set
262 return $wgCapitalLinkOverrides[ $index ];
264 // Default to the global setting
265 return $wgCapitalLinks;