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
215 public static function getCanonicalNamespaces() {
216 static $namespaces = null;
217 if ( $namespaces === null ) {
218 global $wgExtraNamespaces, $wgCanonicalNamespaceNames;
219 $namespaces = array( NS_MAIN
=> '' ) +
$wgCanonicalNamespaceNames;
220 if ( is_array( $wgExtraNamespaces ) ) {
221 $namespaces +
= $wgExtraNamespaces;
223 wfRunHooks( 'CanonicalNamespaces', array( &$namespaces ) );
229 * Returns the canonical (English) name for a given index
231 * @param $index Int: namespace index
232 * @return string or false if no canonical definition.
234 public static function getCanonicalName( $index ) {
235 $nslist = self
::getCanonicalNamespaces();
236 if ( isset( $nslist[$index] ) ) {
237 return $nslist[$index];
244 * Returns the index for a given canonical name, or NULL
245 * The input *must* be converted to lower case first
247 * @param $name String: namespace name
250 public static function getCanonicalIndex( $name ) {
251 static $xNamespaces = false;
252 if ( $xNamespaces === false ) {
253 $xNamespaces = array();
254 foreach ( self
::getCanonicalNamespaces() as $i => $text ) {
255 $xNamespaces[strtolower( $text )] = $i;
258 if ( array_key_exists( $name, $xNamespaces ) ) {
259 return $xNamespaces[$name];
266 * Returns an array of the namespaces (by integer id) that exist on the
267 * wiki. Used primarily by the api in help documentation.
270 public static function getValidNamespaces() {
271 static $mValidNamespaces = null;
273 if ( is_null( $mValidNamespaces ) ) {
274 foreach ( array_keys( self
::getCanonicalNamespaces() ) as $ns ) {
276 $mValidNamespaces[] = $ns;
281 return $mValidNamespaces;
285 * Can this namespace ever have a talk namespace?
287 * @param $index Int: namespace index
290 public static function canTalk( $index ) {
291 return $index >= NS_MAIN
;
295 * Does this namespace contain content, for the purposes of calculating
298 * @param $index Int: index to check
301 public static function isContent( $index ) {
302 global $wgContentNamespaces;
303 return $index == NS_MAIN ||
in_array( $index, $wgContentNamespaces );
307 * Can pages in a namespace be watched?
312 public static function isWatchable( $index ) {
313 return $index >= NS_MAIN
;
317 * Does the namespace allow subpages?
319 * @param $index int Index to check
322 public static function hasSubpages( $index ) {
323 global $wgNamespacesWithSubpages;
324 return !empty( $wgNamespacesWithSubpages[$index] );
328 * Get a list of all namespace indices which are considered to contain content
329 * @return array of namespace indices
331 public static function getContentNamespaces() {
332 global $wgContentNamespaces;
333 if ( !is_array( $wgContentNamespaces ) ||
$wgContentNamespaces === array() ) {
335 } elseif ( !in_array( NS_MAIN
, $wgContentNamespaces ) ) {
336 // always force NS_MAIN to be part of array (to match the algorithm used by isContent)
337 return array_merge( array( NS_MAIN
), $wgContentNamespaces );
339 return $wgContentNamespaces;
343 * Is the namespace first-letter capitalized?
345 * @param $index int Index to check
348 public static function isCapitalized( $index ) {
349 global $wgCapitalLinks, $wgCapitalLinkOverrides;
350 // Turn NS_MEDIA into NS_FILE
351 $index = $index === NS_MEDIA ? NS_FILE
: $index;
353 // Make sure to get the subject of our namespace
354 $index = self
::getSubject( $index );
356 // Some namespaces are special and should always be upper case
357 if ( in_array( $index, self
::$alwaysCapitalizedNamespaces ) ) {
360 if ( isset( $wgCapitalLinkOverrides[ $index ] ) ) {
361 // $wgCapitalLinkOverrides is explicitly set
362 return $wgCapitalLinkOverrides[ $index ];
364 // Default to the global setting
365 return $wgCapitalLinks;
369 * Does the namespace (potentially) have different aliases for different
370 * genders. Not all languages make a distinction here.
373 * @param $index int Index to check
376 public static function hasGenderDistinction( $index ) {
377 return $index == NS_USER ||
$index == NS_USER_TALK
;
381 * It is not possible to use pages from this namespace as template?
384 * @param $index int Index to check
387 public static function isNonincludable( $index ) {
388 global $wgNonincludableNamespaces;
389 return $wgNonincludableNamespaces && in_array( $index, $wgNonincludableNamespaces );