For backport to 1.14.
[lhc/web/wiklou.git] / includes / Namespace.php
1 <?php
2 /**
3 * Provide things related to namespaces
4 * @file
5 */
6
7 /**
8 * Definitions of the NS_ constants are in Defines.php
9 * @private
10 */
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_FILE => 'File',
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',
25 NS_HELP => 'Help',
26 NS_HELP_TALK => 'Help_talk',
27 NS_CATEGORY => 'Category',
28 NS_CATEGORY_TALK => 'Category_talk',
29 );
30
31 if( is_array( $wgExtraNamespaces ) ) {
32 $wgCanonicalNamespaceNames = $wgCanonicalNamespaceNames + $wgExtraNamespaces;
33 }
34
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.
40 *
41 * These are synonyms for the names given in the language file
42 * Users and translators should not change them
43 *
44 */
45
46 class MWNamespace {
47
48 /**
49 * Can pages in the given namespace be moved?
50 *
51 * @param $index Int: namespace index
52 * @return bool
53 */
54 public static function isMovable( $index ) {
55 global $wgAllowImageMoving;
56 return !( $index < NS_MAIN || ($index == NS_FILE && !$wgAllowImageMoving) || $index == NS_CATEGORY );
57 }
58
59 /**
60 * Is the given namespace is a subject (non-talk) namespace?
61 *
62 * @param $index Int: namespace index
63 * @return bool
64 */
65 public static function isMain( $index ) {
66 return !self::isTalk( $index );
67 }
68
69 /**
70 * Is the given namespace a talk namespace?
71 *
72 * @param $index Int: namespace index
73 * @return bool
74 */
75 public static function isTalk( $index ) {
76 return $index > NS_MAIN
77 && $index % 2;
78 }
79
80 /**
81 * Get the talk namespace index for a given namespace
82 *
83 * @param $index Int: namespace index
84 * @return int
85 */
86 public static function getTalk( $index ) {
87 return self::isTalk( $index )
88 ? $index
89 : $index + 1;
90 }
91
92 /**
93 * Get the subject namespace index for a given namespace
94 *
95 * @param $index Int: Namespace index
96 * @return int
97 */
98 public static function getSubject( $index ) {
99 return self::isTalk( $index )
100 ? $index - 1
101 : $index;
102 }
103
104 /**
105 * Returns the canonical (English Wikipedia) name for a given index
106 *
107 * @param $index Int: namespace index
108 * @return string or false if no canonical definition.
109 */
110 public static function getCanonicalName( $index ) {
111 global $wgCanonicalNamespaceNames;
112 if( isset( $wgCanonicalNamespaceNames[$index] ) ) {
113 return $wgCanonicalNamespaceNames[$index];
114 } else {
115 return false;
116 }
117 }
118
119 /**
120 * Returns the index for a given canonical name, or NULL
121 * The input *must* be converted to lower case first
122 *
123 * @param $name String: namespace name
124 * @return int
125 */
126 public static function getCanonicalIndex( $name ) {
127 global $wgCanonicalNamespaceNames;
128 static $xNamespaces = false;
129 if ( $xNamespaces === false ) {
130 $xNamespaces = array();
131 foreach ( $wgCanonicalNamespaceNames as $i => $text ) {
132 $xNamespaces[strtolower($text)] = $i;
133 }
134 }
135 if ( array_key_exists( $name, $xNamespaces ) ) {
136 return $xNamespaces[$name];
137 } else {
138 return NULL;
139 }
140 }
141
142 /**
143 * Can this namespace ever have a talk namespace?
144 *
145 * @param $index Int: namespace index
146 * @return bool
147 */
148 public static function canTalk( $index ) {
149 return $index >= NS_MAIN;
150 }
151
152 /**
153 * Does this namespace contain content, for the purposes of calculating
154 * statistics, etc?
155 *
156 * @param $index Int: index to check
157 * @return bool
158 */
159 public static function isContent( $index ) {
160 global $wgContentNamespaces;
161 return $index == NS_MAIN || in_array( $index, $wgContentNamespaces );
162 }
163
164 /**
165 * Can pages in a namespace be watched?
166 *
167 * @param $index Int
168 * @return bool
169 */
170 public static function isWatchable( $index ) {
171 return $index >= NS_MAIN;
172 }
173
174 /**
175 * Does the namespace allow subpages?
176 *
177 * @param $index int Index to check
178 * @return bool
179 */
180 public static function hasSubpages( $index ) {
181 global $wgNamespacesWithSubpages;
182 return !empty( $wgNamespacesWithSubpages[$index] );
183 }
184
185 }