* (bug 24563) Entries on Special:WhatLinksHere now have a link to their history
[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( isset( $wgExtraNamespaces ) && 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 * These namespaces should always be first-letter capitalized, now and
50 * forevermore. Historically, they could've probably been lowercased too,
51 * but some things are just too ingrained now. :)
52 */
53 private static $alwaysCapitalizedNamespaces = array( NS_SPECIAL, NS_USER, NS_MEDIAWIKI );
54
55 /**
56 * Can pages in the given namespace be moved?
57 *
58 * @param $index Int: namespace index
59 * @return bool
60 */
61 public static function isMovable( $index ) {
62 global $wgAllowImageMoving;
63 return !( $index < NS_MAIN || ($index == NS_FILE && !$wgAllowImageMoving) || $index == NS_CATEGORY );
64 }
65
66 /**
67 * Is the given namespace is a subject (non-talk) namespace?
68 *
69 * @param $index Int: namespace index
70 * @return bool
71 */
72 public static function isMain( $index ) {
73 return !self::isTalk( $index );
74 }
75
76 /**
77 * Is the given namespace a talk namespace?
78 *
79 * @param $index Int: namespace index
80 * @return bool
81 */
82 public static function isTalk( $index ) {
83 return $index > NS_MAIN
84 && $index % 2;
85 }
86
87 /**
88 * Get the talk namespace index for a given namespace
89 *
90 * @param $index Int: namespace index
91 * @return int
92 */
93 public static function getTalk( $index ) {
94 return self::isTalk( $index )
95 ? $index
96 : $index + 1;
97 }
98
99 /**
100 * Get the subject namespace index for a given namespace
101 *
102 * @param $index Int: Namespace index
103 * @return int
104 */
105 public static function getSubject( $index ) {
106 return self::isTalk( $index )
107 ? $index - 1
108 : $index;
109 }
110
111 /**
112 * Returns whether the specified namespace exists
113 */
114 public static function exists( $index ) {
115 global $wgCanonicalNamespaceNames;
116 return isset( $wgCanonicalNamespaceNames[$index] );
117 }
118
119
120 /**
121 * Returns the canonical (English Wikipedia) name for a given index
122 *
123 * @param $index Int: namespace index
124 * @return string or false if no canonical definition.
125 */
126 public static function getCanonicalName( $index ) {
127 global $wgCanonicalNamespaceNames;
128 if( isset( $wgCanonicalNamespaceNames[$index] ) ) {
129 return $wgCanonicalNamespaceNames[$index];
130 } else {
131 return false;
132 }
133 }
134
135 /**
136 * Returns the index for a given canonical name, or NULL
137 * The input *must* be converted to lower case first
138 *
139 * @param $name String: namespace name
140 * @return int
141 */
142 public static function getCanonicalIndex( $name ) {
143 global $wgCanonicalNamespaceNames;
144 static $xNamespaces = false;
145 if ( $xNamespaces === false ) {
146 $xNamespaces = array();
147 foreach ( $wgCanonicalNamespaceNames as $i => $text ) {
148 $xNamespaces[strtolower($text)] = $i;
149 }
150 }
151 if ( array_key_exists( $name, $xNamespaces ) ) {
152 return $xNamespaces[$name];
153 } else {
154 return null;
155 }
156 }
157
158 /**
159 * Returns an array of the namespaces (by integer id) that exist on the
160 * wiki. Used primarily by the api in help documentation.
161 * @return array
162 */
163 public static function getValidNamespaces() {
164 static $mValidNamespaces = null;
165
166 if ( is_null( $mValidNamespaces ) ) {
167 global $wgCanonicalNamespaceNames;
168 $mValidNamespaces = array( NS_MAIN ); // Doesn't appear in $wgCanonicalNamespaceNames for some reason
169 foreach ( array_keys( $wgCanonicalNamespaceNames ) as $ns ) {
170 if ( $ns > 0 ) {
171 $mValidNamespaces[] = $ns;
172 }
173 }
174 }
175
176 return $mValidNamespaces;
177 }
178
179 /**
180 * Can this namespace ever have a talk namespace?
181 *
182 * @param $index Int: namespace index
183 * @return bool
184 */
185 public static function canTalk( $index ) {
186 return $index >= NS_MAIN;
187 }
188
189 /**
190 * Does this namespace contain content, for the purposes of calculating
191 * statistics, etc?
192 *
193 * @param $index Int: index to check
194 * @return bool
195 */
196 public static function isContent( $index ) {
197 global $wgContentNamespaces;
198 return $index == NS_MAIN || in_array( $index, $wgContentNamespaces );
199 }
200
201 /**
202 * Can pages in a namespace be watched?
203 *
204 * @param $index Int
205 * @return bool
206 */
207 public static function isWatchable( $index ) {
208 return $index >= NS_MAIN;
209 }
210
211 /**
212 * Does the namespace allow subpages?
213 *
214 * @param $index int Index to check
215 * @return bool
216 */
217 public static function hasSubpages( $index ) {
218 global $wgNamespacesWithSubpages;
219 return !empty( $wgNamespacesWithSubpages[$index] );
220 }
221
222 /**
223 * Is the namespace first-letter capitalized?
224 *
225 * @param $index int Index to check
226 * @return bool
227 */
228 public static function isCapitalized( $index ) {
229 global $wgCapitalLinks, $wgCapitalLinkOverrides;
230 // Turn NS_MEDIA into NS_FILE
231 $index = $index === NS_MEDIA ? NS_FILE : $index;
232
233 // Make sure to get the subject of our namespace
234 $index = self::getSubject( $index );
235
236 // Some namespaces are special and should always be upper case
237 if ( in_array( $index, self::$alwaysCapitalizedNamespaces ) ) {
238 return true;
239 }
240 if ( isset( $wgCapitalLinkOverrides[ $index ] ) ) {
241 // $wgCapitalLinkOverrides is explicitly set
242 return $wgCapitalLinkOverrides[ $index ];
243 }
244 // Default to the global setting
245 return $wgCapitalLinks;
246 }
247 }