Reverted r42528. Links with href="#" make firefox scroll to the top of the page,...
[mediawiki.git] / includes / Namespace.php
blob7c7b7dedd364aea6f7b5c4334c54a0394b100de4
1 <?php
2 /**
3 * Provide things related to namespaces
4 * @file
5 */
7 /**
8 * Definitions of the NS_ constants are in Defines.php
9 * @private
11 $wgCanonicalNamespaceNames = array(
12 NS_MEDIA => 'Media',
13 NS_SPECIAL => 'Special',
14 NS_TALK => 'Talk',
15 NS_USER => 'User',
16 NS_USER_TALK => 'User_talk',
17 NS_PROJECT => 'Project',
18 NS_PROJECT_TALK => 'Project_talk',
19 NS_IMAGE => 'Image',
20 NS_IMAGE_TALK => 'Image_talk',
21 NS_MEDIAWIKI => 'MediaWiki',
22 NS_MEDIAWIKI_TALK => 'MediaWiki_talk',
23 NS_TEMPLATE => 'Template',
24 NS_TEMPLATE_TALK => 'Template_talk',
25 NS_HELP => 'Help',
26 NS_HELP_TALK => 'Help_talk',
27 NS_CATEGORY => 'Category',
28 NS_CATEGORY_TALK => 'Category_talk',
31 if( is_array( $wgExtraNamespaces ) ) {
32 $wgCanonicalNamespaceNames = $wgCanonicalNamespaceNames + $wgExtraNamespaces;
35 /**
36 * This is a utility class with only static functions
37 * for dealing with namespaces that encodes all the
38 * "magic" behaviors of them based on index. The textual
39 * names of the namespaces are handled by Language.php.
41 * These are synonyms for the names given in the language file
42 * Users and translators should not change them
46 class MWNamespace {
48 /**
49 * Can pages in the given namespace be moved?
51 * @param $index Int: namespace index
52 * @return bool
54 public static function isMovable( $index ) {
55 global $wgAllowImageMoving;
56 return !( $index < NS_MAIN || ($index == NS_IMAGE && !$wgAllowImageMoving) || $index == NS_CATEGORY );
59 /**
60 * Is the given namespace is a subject (non-talk) namespace?
62 * @param $index Int: namespace index
63 * @return bool
65 public static function isMain( $index ) {
66 return !self::isTalk( $index );
69 /**
70 * Is the given namespace a talk namespace?
72 * @param $index Int: namespace index
73 * @return bool
75 public static function isTalk( $index ) {
76 return $index > NS_MAIN
77 && $index % 2;
80 /**
81 * Get the talk namespace index for a given namespace
83 * @param $index Int: namespace index
84 * @return int
86 public static function getTalk( $index ) {
87 return self::isTalk( $index )
88 ? $index
89 : $index + 1;
92 /**
93 * Get the subject namespace index for a given namespace
95 * @param $index Int: Namespace index
96 * @return int
98 public static function getSubject( $index ) {
99 return self::isTalk( $index )
100 ? $index - 1
101 : $index;
105 * Returns the canonical (English Wikipedia) name for a given index
107 * @param $index Int: namespace index
108 * @return string
110 public static function getCanonicalName( $index ) {
111 global $wgCanonicalNamespaceNames;
112 return $wgCanonicalNamespaceNames[$index];
116 * Returns the index for a given canonical name, or NULL
117 * The input *must* be converted to lower case first
119 * @param $name String: namespace name
120 * @return int
122 public static function getCanonicalIndex( $name ) {
123 global $wgCanonicalNamespaceNames;
124 static $xNamespaces = false;
125 if ( $xNamespaces === false ) {
126 $xNamespaces = array();
127 foreach ( $wgCanonicalNamespaceNames as $i => $text ) {
128 $xNamespaces[strtolower($text)] = $i;
131 if ( array_key_exists( $name, $xNamespaces ) ) {
132 return $xNamespaces[$name];
133 } else {
134 return NULL;
139 * Can this namespace ever have a talk namespace?
141 * @param $index Int: namespace index
142 * @return bool
144 public static function canTalk( $index ) {
145 return $index >= NS_MAIN;
149 * Does this namespace contain content, for the purposes of calculating
150 * statistics, etc?
152 * @param $index Int: index to check
153 * @return bool
155 public static function isContent( $index ) {
156 global $wgContentNamespaces;
157 return $index == NS_MAIN || in_array( $index, $wgContentNamespaces );
161 * Can pages in a namespace be watched?
163 * @param $index Int
164 * @return bool
166 public static function isWatchable( $index ) {
167 return $index >= NS_MAIN;
171 * Does the namespace allow subpages?
173 * @param $index int Index to check
174 * @return bool
176 public static function hasSubpages( $index ) {
177 global $wgNamespacesWithSubpages;
178 return !empty( $wgNamespacesWithSubpages[$index] );