3 * Provide things related to namespaces
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
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
);
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/Defines.php and have
31 * a value below 0 (ex: NS_SPECIAL = -1 , NS_MEDIA = -2)
38 private static function isMethodValidFor( $index, $method ) {
39 if( $index < NS_MAIN
) {
40 throw new MWException( "$method does not make any sense for given namespace $index" );
46 * Can pages in the given namespace be moved?
48 * @param $index Int: namespace index
51 public static function isMovable( $index ) {
52 global $wgAllowImageMoving;
53 return !( $index < NS_MAIN ||
($index == NS_FILE
&& !$wgAllowImageMoving) ||
$index == NS_CATEGORY
);
57 * Is the given namespace is a subject (non-talk) namespace?
59 * @param $index Int: namespace index
62 public static function isMain( $index ) {
63 return !self
::isTalk( $index );
67 * Is the given namespace a talk namespace?
69 * @param $index Int: namespace index
72 public static function isTalk( $index ) {
73 return $index > NS_MAIN
78 * Get the talk namespace index for a given namespace
80 * @param $index Int: namespace index
83 public static function getTalk( $index ) {
84 self
::isMethodValidFor( $index, __METHOD__
);
85 return self
::isTalk( $index )
91 * Get the subject namespace index for a given namespace
92 * Special namespaces (NS_MEDIA, NS_SPECIAL) are always the subject.
94 * @param $index Int: Namespace index
97 public static function getSubject( $index ) {
98 # Handle special namespaces
99 if( $index < NS_MAIN
) {
103 return self
::isTalk( $index )
109 * Get the associated namespace.
110 * For talk namespaces, returns the subject (non-talk) namespace
111 * For subject (non-talk) namespaces, returns the talk namespace
113 * @param $index Int: namespace index
114 * @return int or null if no associated namespace could be found
116 public static function getAssociated( $index ) {
117 self
::isMethodValidFor( $index, __METHOD__
);
119 if( self
::isMain( $index ) ) {
120 return self
::getTalk( $index );
121 } elseif( self
::isTalk( $index ) ) {
122 return self
::getSubject( $index );
129 * Returns whether the specified namespace exists
135 public static function exists( $index ) {
136 $nslist = self
::getCanonicalNamespaces();
137 return isset( $nslist[$index] );
141 * Returns array of all defined namespaces with their canonical
147 public static function getCanonicalNamespaces() {
148 static $namespaces = null;
149 if ( $namespaces === null ) {
150 global $wgExtraNamespaces, $wgCanonicalNamespaceNames;
151 $namespaces = array( NS_MAIN
=> '' ) +
$wgCanonicalNamespaceNames;
152 if ( is_array( $wgExtraNamespaces ) ) {
153 $namespaces +
= $wgExtraNamespaces;
155 wfRunHooks( 'CanonicalNamespaces', array( &$namespaces ) );
161 * Returns the canonical (English) name for a given index
163 * @param $index Int: namespace index
164 * @return string or false if no canonical definition.
166 public static function getCanonicalName( $index ) {
167 $nslist = self
::getCanonicalNamespaces();
168 if( isset( $nslist[$index] ) ) {
169 return $nslist[$index];
176 * Returns the index for a given canonical name, or NULL
177 * The input *must* be converted to lower case first
179 * @param $name String: namespace name
182 public static function getCanonicalIndex( $name ) {
183 static $xNamespaces = false;
184 if ( $xNamespaces === false ) {
185 $xNamespaces = array();
186 foreach ( self
::getCanonicalNamespaces() as $i => $text ) {
187 $xNamespaces[strtolower($text)] = $i;
190 if ( array_key_exists( $name, $xNamespaces ) ) {
191 return $xNamespaces[$name];
198 * Returns an array of the namespaces (by integer id) that exist on the
199 * wiki. Used primarily by the api in help documentation.
202 public static function getValidNamespaces() {
203 static $mValidNamespaces = null;
205 if ( is_null( $mValidNamespaces ) ) {
206 foreach ( array_keys( self
::getCanonicalNamespaces() ) as $ns ) {
208 $mValidNamespaces[] = $ns;
213 return $mValidNamespaces;
217 * Can this namespace ever have a talk namespace?
219 * @param $index Int: namespace index
222 public static function canTalk( $index ) {
223 return $index >= NS_MAIN
;
227 * Does this namespace contain content, for the purposes of calculating
230 * @param $index Int: index to check
233 public static function isContent( $index ) {
234 global $wgContentNamespaces;
235 return $index == NS_MAIN ||
in_array( $index, $wgContentNamespaces );
239 * Can pages in a namespace be watched?
244 public static function isWatchable( $index ) {
245 return $index >= NS_MAIN
;
249 * Does the namespace allow subpages?
251 * @param $index int Index to check
254 public static function hasSubpages( $index ) {
255 global $wgNamespacesWithSubpages;
256 return !empty( $wgNamespacesWithSubpages[$index] );
260 * Get a list of all namespace indices which are considered to contain content
261 * @return array of namespace indices
263 public static function getContentNamespaces() {
264 global $wgContentNamespaces;
265 if( !is_array( $wgContentNamespaces ) ||
$wgContentNamespaces === array() ) {
267 } elseif ( !in_array( NS_MAIN
, $wgContentNamespaces ) ) {
268 // always force NS_MAIN to be part of array (to match the algorithm used by isContent)
269 return array_merge( array( NS_MAIN
), $wgContentNamespaces );
271 return $wgContentNamespaces;
275 * Is the namespace first-letter capitalized?
277 * @param $index int Index to check
280 public static function isCapitalized( $index ) {
281 global $wgCapitalLinks, $wgCapitalLinkOverrides;
282 // Turn NS_MEDIA into NS_FILE
283 $index = $index === NS_MEDIA ? NS_FILE
: $index;
285 // Make sure to get the subject of our namespace
286 $index = self
::getSubject( $index );
288 // Some namespaces are special and should always be upper case
289 if ( in_array( $index, self
::$alwaysCapitalizedNamespaces ) ) {
292 if ( isset( $wgCapitalLinkOverrides[ $index ] ) ) {
293 // $wgCapitalLinkOverrides is explicitly set
294 return $wgCapitalLinkOverrides[ $index ];
296 // Default to the global setting
297 return $wgCapitalLinks;
301 * Does the namespace (potentially) have different aliases for different
302 * genders. Not all languages make a distinction here.
305 * @param $index int Index to check
308 public static function hasGenderDistinction( $index ) {
309 return $index == NS_USER ||
$index == NS_USER_TALK
;