3 * Provide things related to namespaces.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 * This is a utility class with only static functions
25 * for dealing with namespaces that encodes all the
26 * "magic" behaviors of them based on index. The textual
27 * names of the namespaces are handled by Language.php.
29 * These are synonyms for the names given in the language file
30 * Users and translators should not change them
36 * These namespaces should always be first-letter capitalized, now and
37 * forevermore. Historically, they could've probably been lowercased too,
38 * but some things are just too ingrained now. :)
40 private static $alwaysCapitalizedNamespaces = array( NS_SPECIAL
, NS_USER
, NS_MEDIAWIKI
);
43 * Throw an exception when trying to get the subject or talk page
44 * for a given namespace where it does not make sense.
45 * Special namespaces are defined in includes/Defines.php and have
46 * a value below 0 (ex: NS_SPECIAL = -1 , NS_MEDIA = -2)
53 private static function isMethodValidFor( $index, $method ) {
54 if ( $index < NS_MAIN
) {
55 throw new MWException( "$method does not make any sense for given namespace $index" );
61 * Can pages in the given namespace be moved?
63 * @param $index Int: namespace index
66 public static function isMovable( $index ) {
67 global $wgAllowImageMoving;
68 return !( $index < NS_MAIN ||
( $index == NS_FILE
&& !$wgAllowImageMoving ) ||
$index == NS_CATEGORY
);
72 * Is the given namespace is a subject (non-talk) namespace?
74 * @param $index Int: namespace index
78 public static function isSubject( $index ) {
79 return !self
::isTalk( $index );
83 * @see self::isSubject
84 * @deprecated Please use the more consistently named isSubject (since 1.19)
87 public static function isMain( $index ) {
88 wfDeprecated( __METHOD__
, '1.19' );
89 return self
::isSubject( $index );
93 * Is the given namespace a talk namespace?
95 * @param $index Int: namespace index
98 public static function isTalk( $index ) {
99 return $index > NS_MAIN
104 * Get the talk namespace index for a given namespace
106 * @param $index Int: namespace index
109 public static function getTalk( $index ) {
110 self
::isMethodValidFor( $index, __METHOD__
);
111 return self
::isTalk( $index )
117 * Get the subject namespace index for a given namespace
118 * Special namespaces (NS_MEDIA, NS_SPECIAL) are always the subject.
120 * @param $index Int: Namespace index
123 public static function getSubject( $index ) {
124 # Handle special namespaces
125 if ( $index < NS_MAIN
) {
129 return self
::isTalk( $index )
135 * Get the associated namespace.
136 * For talk namespaces, returns the subject (non-talk) namespace
137 * For subject (non-talk) namespaces, returns the talk namespace
139 * @param $index Int: namespace index
140 * @return int or null if no associated namespace could be found
142 public static function getAssociated( $index ) {
143 self
::isMethodValidFor( $index, __METHOD__
);
145 if ( self
::isSubject( $index ) ) {
146 return self
::getTalk( $index );
147 } elseif ( self
::isTalk( $index ) ) {
148 return self
::getSubject( $index );
155 * Returns whether the specified namespace exists
162 public static function exists( $index ) {
163 $nslist = self
::getCanonicalNamespaces();
164 return isset( $nslist[$index] );
168 * Returns whether the specified namespaces are the same namespace
170 * @note It's possible that in the future we may start using something
171 * other than just namespace indexes. Under that circumstance making use
172 * of this function rather than directly doing comparison will make
173 * sure that code will not potentially break.
175 * @param $ns1 int The first namespace index
176 * @param $ns2 int The second namespae index
181 public static function equals( $ns1, $ns2 ) {
186 * Returns whether the specified namespaces share the same subject.
187 * eg: NS_USER and NS_USER wil return true, as well
188 * NS_USER and NS_USER_TALK will return true.
190 * @param $ns1 int The first namespace index
191 * @param $ns2 int The second namespae index
196 public static function subjectEquals( $ns1, $ns2 ) {
197 return self
::getSubject( $ns1 ) == self
::getSubject( $ns2 );
201 * Returns array of all defined namespaces with their canonical
207 public static function getCanonicalNamespaces() {
208 static $namespaces = null;
209 if ( $namespaces === null ) {
210 global $wgExtraNamespaces, $wgCanonicalNamespaceNames;
211 $namespaces = array( NS_MAIN
=> '' ) +
$wgCanonicalNamespaceNames;
212 if ( is_array( $wgExtraNamespaces ) ) {
213 $namespaces +
= $wgExtraNamespaces;
215 wfRunHooks( 'CanonicalNamespaces', array( &$namespaces ) );
221 * Returns the canonical (English) name for a given index
223 * @param $index Int: namespace index
224 * @return string or false if no canonical definition.
226 public static function getCanonicalName( $index ) {
227 $nslist = self
::getCanonicalNamespaces();
228 if ( isset( $nslist[$index] ) ) {
229 return $nslist[$index];
236 * Returns the index for a given canonical name, or NULL
237 * The input *must* be converted to lower case first
239 * @param $name String: namespace name
242 public static function getCanonicalIndex( $name ) {
243 static $xNamespaces = false;
244 if ( $xNamespaces === false ) {
245 $xNamespaces = array();
246 foreach ( self
::getCanonicalNamespaces() as $i => $text ) {
247 $xNamespaces[strtolower( $text )] = $i;
250 if ( array_key_exists( $name, $xNamespaces ) ) {
251 return $xNamespaces[$name];
258 * Returns an array of the namespaces (by integer id) that exist on the
259 * wiki. Used primarily by the api in help documentation.
262 public static function getValidNamespaces() {
263 static $mValidNamespaces = null;
265 if ( is_null( $mValidNamespaces ) ) {
266 foreach ( array_keys( self
::getCanonicalNamespaces() ) as $ns ) {
268 $mValidNamespaces[] = $ns;
273 return $mValidNamespaces;
277 * Can this namespace ever have a talk namespace?
279 * @param $index Int: namespace index
282 public static function canTalk( $index ) {
283 return $index >= NS_MAIN
;
287 * Does this namespace contain content, for the purposes of calculating
290 * @param $index Int: index to check
293 public static function isContent( $index ) {
294 global $wgContentNamespaces;
295 return $index == NS_MAIN ||
in_array( $index, $wgContentNamespaces );
299 * Can pages in a namespace be watched?
304 public static function isWatchable( $index ) {
305 return $index >= NS_MAIN
;
309 * Does the namespace allow subpages?
311 * @param $index int Index to check
314 public static function hasSubpages( $index ) {
315 global $wgNamespacesWithSubpages;
316 return !empty( $wgNamespacesWithSubpages[$index] );
320 * Get a list of all namespace indices which are considered to contain content
321 * @return array of namespace indices
323 public static function getContentNamespaces() {
324 global $wgContentNamespaces;
325 if ( !is_array( $wgContentNamespaces ) ||
$wgContentNamespaces === array() ) {
327 } elseif ( !in_array( NS_MAIN
, $wgContentNamespaces ) ) {
328 // always force NS_MAIN to be part of array (to match the algorithm used by isContent)
329 return array_merge( array( NS_MAIN
), $wgContentNamespaces );
331 return $wgContentNamespaces;
335 * Is the namespace first-letter capitalized?
337 * @param $index int Index to check
340 public static function isCapitalized( $index ) {
341 global $wgCapitalLinks, $wgCapitalLinkOverrides;
342 // Turn NS_MEDIA into NS_FILE
343 $index = $index === NS_MEDIA ? NS_FILE
: $index;
345 // Make sure to get the subject of our namespace
346 $index = self
::getSubject( $index );
348 // Some namespaces are special and should always be upper case
349 if ( in_array( $index, self
::$alwaysCapitalizedNamespaces ) ) {
352 if ( isset( $wgCapitalLinkOverrides[ $index ] ) ) {
353 // $wgCapitalLinkOverrides is explicitly set
354 return $wgCapitalLinkOverrides[ $index ];
356 // Default to the global setting
357 return $wgCapitalLinks;
361 * Does the namespace (potentially) have different aliases for different
362 * genders. Not all languages make a distinction here.
365 * @param $index int Index to check
368 public static function hasGenderDistinction( $index ) {
369 return $index == NS_USER ||
$index == NS_USER_TALK
;
373 * It is not possible to use pages from this namespace as template?
376 * @param $index int Index to check
379 public static function isNonincludable( $index ) {
380 global $wgNonincludableNamespaces;
381 return $wgNonincludableNamespaces && in_array( $index, $wgNonincludableNamespaces );