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;
69 $result = !( $index < NS_MAIN ||
( $index == NS_FILE
&& !$wgAllowImageMoving ) ||
$index == NS_CATEGORY
);
74 wfRunHooks( 'NamespaceIsMovable', array( $index, &$result ) );
80 * Is the given namespace is a subject (non-talk) namespace?
82 * @param $index Int: namespace index
86 public static function isSubject( $index ) {
87 return !self
::isTalk( $index );
91 * @see self::isSubject
92 * @deprecated Please use the more consistently named isSubject (since 1.19)
95 public static function isMain( $index ) {
96 wfDeprecated( __METHOD__
, '1.19' );
97 return self
::isSubject( $index );
101 * Is the given namespace a talk namespace?
103 * @param $index Int: namespace index
106 public static function isTalk( $index ) {
107 return $index > NS_MAIN
112 * Get the talk namespace index for a given namespace
114 * @param $index Int: namespace index
117 public static function getTalk( $index ) {
118 self
::isMethodValidFor( $index, __METHOD__
);
119 return self
::isTalk( $index )
125 * Get the subject namespace index for a given namespace
126 * Special namespaces (NS_MEDIA, NS_SPECIAL) are always the subject.
128 * @param $index Int: Namespace index
131 public static function getSubject( $index ) {
132 # Handle special namespaces
133 if ( $index < NS_MAIN
) {
137 return self
::isTalk( $index )
143 * Get the associated namespace.
144 * For talk namespaces, returns the subject (non-talk) namespace
145 * For subject (non-talk) namespaces, returns the talk namespace
147 * @param $index Int: namespace index
148 * @return int or null if no associated namespace could be found
150 public static function getAssociated( $index ) {
151 self
::isMethodValidFor( $index, __METHOD__
);
153 if ( self
::isSubject( $index ) ) {
154 return self
::getTalk( $index );
155 } elseif ( self
::isTalk( $index ) ) {
156 return self
::getSubject( $index );
163 * Returns whether the specified namespace exists
170 public static function exists( $index ) {
171 $nslist = self
::getCanonicalNamespaces();
172 return isset( $nslist[$index] );
176 * Returns whether the specified namespaces are the same namespace
178 * @note It's possible that in the future we may start using something
179 * other than just namespace indexes. Under that circumstance making use
180 * of this function rather than directly doing comparison will make
181 * sure that code will not potentially break.
183 * @param $ns1 int The first namespace index
184 * @param $ns2 int The second namespae index
189 public static function equals( $ns1, $ns2 ) {
194 * Returns whether the specified namespaces share the same subject.
195 * eg: NS_USER and NS_USER wil return true, as well
196 * NS_USER and NS_USER_TALK will return true.
198 * @param $ns1 int The first namespace index
199 * @param $ns2 int The second namespae index
204 public static function subjectEquals( $ns1, $ns2 ) {
205 return self
::getSubject( $ns1 ) == self
::getSubject( $ns2 );
209 * Returns array of all defined namespaces with their canonical
212 * @param bool $rebuild rebuild namespace list (default = false). Used for testing.
217 public static function getCanonicalNamespaces( $rebuild = false ) {
218 static $namespaces = null;
219 if ( $namespaces === null ||
$rebuild ) {
220 global $wgExtraNamespaces, $wgCanonicalNamespaceNames;
221 $namespaces = array( NS_MAIN
=> '' ) +
$wgCanonicalNamespaceNames;
222 if ( is_array( $wgExtraNamespaces ) ) {
223 $namespaces +
= $wgExtraNamespaces;
225 wfRunHooks( 'CanonicalNamespaces', array( &$namespaces ) );
231 * Returns the canonical (English) name for a given index
233 * @param $index Int: namespace index
234 * @return string or false if no canonical definition.
236 public static function getCanonicalName( $index ) {
237 $nslist = self
::getCanonicalNamespaces();
238 if ( isset( $nslist[$index] ) ) {
239 return $nslist[$index];
246 * Returns the index for a given canonical name, or NULL
247 * The input *must* be converted to lower case first
249 * @param $name String: namespace name
252 public static function getCanonicalIndex( $name ) {
253 static $xNamespaces = false;
254 if ( $xNamespaces === false ) {
255 $xNamespaces = array();
256 foreach ( self
::getCanonicalNamespaces() as $i => $text ) {
257 $xNamespaces[strtolower( $text )] = $i;
260 if ( array_key_exists( $name, $xNamespaces ) ) {
261 return $xNamespaces[$name];
268 * Returns an array of the namespaces (by integer id) that exist on the
269 * wiki. Used primarily by the api in help documentation.
272 public static function getValidNamespaces() {
273 static $mValidNamespaces = null;
275 if ( is_null( $mValidNamespaces ) ) {
276 foreach ( array_keys( self
::getCanonicalNamespaces() ) as $ns ) {
278 $mValidNamespaces[] = $ns;
283 return $mValidNamespaces;
287 * Can this namespace ever have a talk namespace?
289 * @param $index Int: namespace index
292 public static function canTalk( $index ) {
293 return $index >= NS_MAIN
;
297 * Does this namespace contain content, for the purposes of calculating
300 * @param $index Int: index to check
303 public static function isContent( $index ) {
304 global $wgContentNamespaces;
305 return $index == NS_MAIN ||
in_array( $index, $wgContentNamespaces );
309 * Can pages in a namespace be watched?
314 public static function isWatchable( $index ) {
315 return $index >= NS_MAIN
;
319 * Does the namespace allow subpages?
321 * @param $index int Index to check
324 public static function hasSubpages( $index ) {
325 global $wgNamespacesWithSubpages;
326 return !empty( $wgNamespacesWithSubpages[$index] );
330 * Get a list of all namespace indices which are considered to contain content
331 * @return array of namespace indices
333 public static function getContentNamespaces() {
334 global $wgContentNamespaces;
335 if ( !is_array( $wgContentNamespaces ) ||
$wgContentNamespaces === array() ) {
337 } elseif ( !in_array( NS_MAIN
, $wgContentNamespaces ) ) {
338 // always force NS_MAIN to be part of array (to match the algorithm used by isContent)
339 return array_merge( array( NS_MAIN
), $wgContentNamespaces );
341 return $wgContentNamespaces;
346 * List all namespace indices which are considered subject, aka not a talk
347 * or special namespace. See also MWNamespace::isSubject
349 * @return array of namespace indices
351 public static function getSubjectNamespaces() {
353 MWNamespace
::getValidNamespaces(),
354 'MWNamespace::isSubject'
359 * List all namespace indices which are considered talks, aka not a subject
360 * or special namespace. See also MWNamespace::isTalk
362 * @return array of namespace indices
364 public static function getTalkNamespaces() {
366 MWNamespace
::getValidNamespaces(),
367 'MWNamespace::isTalk'
372 * Is the namespace first-letter capitalized?
374 * @param $index int Index to check
377 public static function isCapitalized( $index ) {
378 global $wgCapitalLinks, $wgCapitalLinkOverrides;
379 // Turn NS_MEDIA into NS_FILE
380 $index = $index === NS_MEDIA ? NS_FILE
: $index;
382 // Make sure to get the subject of our namespace
383 $index = self
::getSubject( $index );
385 // Some namespaces are special and should always be upper case
386 if ( in_array( $index, self
::$alwaysCapitalizedNamespaces ) ) {
389 if ( isset( $wgCapitalLinkOverrides[ $index ] ) ) {
390 // $wgCapitalLinkOverrides is explicitly set
391 return $wgCapitalLinkOverrides[ $index ];
393 // Default to the global setting
394 return $wgCapitalLinks;
398 * Does the namespace (potentially) have different aliases for different
399 * genders. Not all languages make a distinction here.
402 * @param $index int Index to check
405 public static function hasGenderDistinction( $index ) {
406 return $index == NS_USER ||
$index == NS_USER_TALK
;
410 * It is not possible to use pages from this namespace as template?
413 * @param $index int Index to check
416 public static function isNonincludable( $index ) {
417 global $wgNonincludableNamespaces;
418 return $wgNonincludableNamespaces && in_array( $index, $wgNonincludableNamespaces );