Follow up r87630. The file cache is now checked at reportHTML() which overrides the...
[mediawiki.git] / includes / Namespace.php
blob34ab6bb6ad41aec032416fd157f0b5cc7d3dd065
1 <?php
2 /**
3 * Provide things related to namespaces
4 * @file
5 */
7 /**
8 * This is a utility class with only static functions
9 * for dealing with namespaces that encodes all the
10 * "magic" behaviors of them based on index. The textual
11 * names of the namespaces are handled by Language.php.
13 * These are synonyms for the names given in the language file
14 * Users and translators should not change them
18 class MWNamespace {
20 /**
21 * These namespaces should always be first-letter capitalized, now and
22 * forevermore. Historically, they could've probably been lowercased too,
23 * but some things are just too ingrained now. :)
25 private static $alwaysCapitalizedNamespaces = array( NS_SPECIAL, NS_USER, NS_MEDIAWIKI );
27 /**
28 * Throw an exception when trying to get the subject or talk page
29 * for a given namespace where it does not make sense.
30 * Special namespaces are defined in includes/define.php and have
31 * a value below 0 (ex: NS_SPECIAL = -1 , NS_MEDIA = -2)
33 * @param $ns Int: namespace index
35 private static function isMethodValidFor( $index, $method ) {
36 if( $index < NS_MAIN ) {
37 throw new MWException( "$method does not make any sense for given namespace $index" );
39 return true;
42 /**
43 * Can pages in the given namespace be moved?
45 * @param $index Int: namespace index
46 * @return bool
48 public static function isMovable( $index ) {
49 global $wgAllowImageMoving;
50 return !( $index < NS_MAIN || ($index == NS_FILE && !$wgAllowImageMoving) || $index == NS_CATEGORY );
53 /**
54 * Is the given namespace is a subject (non-talk) namespace?
56 * @param $index Int: namespace index
57 * @return bool
59 public static function isMain( $index ) {
60 return !self::isTalk( $index );
63 /**
64 * Is the given namespace a talk namespace?
66 * @param $index Int: namespace index
67 * @return bool
69 public static function isTalk( $index ) {
70 return $index > NS_MAIN
71 && $index % 2;
74 /**
75 * Get the talk namespace index for a given namespace
77 * @param $index Int: namespace index
78 * @return int
80 public static function getTalk( $index ) {
81 self::isMethodValidFor( $index, __METHOD__ );
82 return self::isTalk( $index )
83 ? $index
84 : $index + 1;
87 /**
88 * Get the subject namespace index for a given namespace
89 * Special namespaces (NS_MEDIA, NS_SPECIAL) are always the subject.
91 * @param $index Int: Namespace index
92 * @return int
94 public static function getSubject( $index ) {
95 # Handle special namespaces
96 if( $index < NS_MAIN ) {
97 return $index;
100 return self::isTalk( $index )
101 ? $index - 1
102 : $index;
106 * Get the associated namespace.
107 * For talk namespaces, returns the subject (non-talk) namespace
108 * For subject (non-talk) namespaces, returns the talk namespace
110 * @param $index Int: namespace index
111 * @return int or null if no associated namespace could be found
113 public static function getAssociated( $index ) {
114 self::isMethodValidFor( $index, __METHOD__ );
116 if( self::isMain( $index ) ) {
117 return self::getTalk( $index );
118 } elseif( self::isTalk( $index ) ) {
119 return self::getSubject( $index );
120 } else {
121 return null;
126 * Returns whether the specified namespace exists
128 public static function exists( $index ) {
129 $nslist = self::getCanonicalNamespaces();
130 return isset( $nslist[$index] );
134 * Returns array of all defined namespaces with their canonical
135 * (English) names.
137 * @return \array
138 * @since 1.17
140 public static function getCanonicalNamespaces() {
141 static $namespaces = null;
142 if ( $namespaces === null ) {
143 global $wgExtraNamespaces, $wgCanonicalNamespaceNames;
144 $namespaces = array( NS_MAIN => '' ) + $wgCanonicalNamespaceNames;
145 if ( is_array( $wgExtraNamespaces ) ) {
146 $namespaces += $wgExtraNamespaces;
148 wfRunHooks( 'CanonicalNamespaces', array( &$namespaces ) );
150 return $namespaces;
154 * Returns the canonical (English) name for a given index
156 * @param $index Int: namespace index
157 * @return string or false if no canonical definition.
159 public static function getCanonicalName( $index ) {
160 $nslist = self::getCanonicalNamespaces();
161 if( isset( $nslist[$index] ) ) {
162 return $nslist[$index];
163 } else {
164 return false;
169 * Returns the index for a given canonical name, or NULL
170 * The input *must* be converted to lower case first
172 * @param $name String: namespace name
173 * @return int
175 public static function getCanonicalIndex( $name ) {
176 static $xNamespaces = false;
177 if ( $xNamespaces === false ) {
178 $xNamespaces = array();
179 foreach ( self::getCanonicalNamespaces() as $i => $text ) {
180 $xNamespaces[strtolower($text)] = $i;
183 if ( array_key_exists( $name, $xNamespaces ) ) {
184 return $xNamespaces[$name];
185 } else {
186 return null;
191 * Returns an array of the namespaces (by integer id) that exist on the
192 * wiki. Used primarily by the api in help documentation.
193 * @return array
195 public static function getValidNamespaces() {
196 static $mValidNamespaces = null;
198 if ( is_null( $mValidNamespaces ) ) {
199 foreach ( array_keys( self::getCanonicalNamespaces() ) as $ns ) {
200 if ( $ns >= 0 ) {
201 $mValidNamespaces[] = $ns;
206 return $mValidNamespaces;
210 * Can this namespace ever have a talk namespace?
212 * @param $index Int: namespace index
213 * @return bool
215 public static function canTalk( $index ) {
216 return $index >= NS_MAIN;
220 * Does this namespace contain content, for the purposes of calculating
221 * statistics, etc?
223 * @param $index Int: index to check
224 * @return bool
226 public static function isContent( $index ) {
227 global $wgContentNamespaces;
228 return $index == NS_MAIN || in_array( $index, $wgContentNamespaces );
232 * Can pages in a namespace be watched?
234 * @param $index Int
235 * @return bool
237 public static function isWatchable( $index ) {
238 return $index >= NS_MAIN;
242 * Does the namespace allow subpages?
244 * @param $index int Index to check
245 * @return bool
247 public static function hasSubpages( $index ) {
248 global $wgNamespacesWithSubpages;
249 return !empty( $wgNamespacesWithSubpages[$index] );
253 * Get a list of all namespace indices which are considered to contain content
254 * @return array of namespace indices
256 public static function getContentNamespaces() {
257 global $wgContentNamespaces;
258 if( !is_array( $wgContentNamespaces ) || $wgContentNamespaces === array() ) {
259 return NS_MAIN;
260 } elseif ( !in_array( NS_MAIN, $wgContentNamespaces ) ) {
261 // always force NS_MAIN to be part of array (to match the algorithm used by isContent)
262 return array_merge( array( NS_MAIN ), $wgContentNamespaces );
263 } else {
264 return $wgContentNamespaces;
268 * Is the namespace first-letter capitalized?
270 * @param $index int Index to check
271 * @return bool
273 public static function isCapitalized( $index ) {
274 global $wgCapitalLinks, $wgCapitalLinkOverrides;
275 // Turn NS_MEDIA into NS_FILE
276 $index = $index === NS_MEDIA ? NS_FILE : $index;
278 // Make sure to get the subject of our namespace
279 $index = self::getSubject( $index );
281 // Some namespaces are special and should always be upper case
282 if ( in_array( $index, self::$alwaysCapitalizedNamespaces ) ) {
283 return true;
285 if ( isset( $wgCapitalLinkOverrides[ $index ] ) ) {
286 // $wgCapitalLinkOverrides is explicitly set
287 return $wgCapitalLinkOverrides[ $index ];
289 // Default to the global setting
290 return $wgCapitalLinks;
294 * Does the namespace (potentially) have different aliases for different
295 * genders. Not all languages make a distinction here.
297 * @since 1.18
298 * @param $index int Index to check
299 * @return bool
301 public static function hasGenderDistinction( $index ) {
302 return $index == NS_USER || $index == NS_USER_TALK;