2004-02-18 02:15:00 +00:00
|
|
|
<?php
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Provide things related to namespaces
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @file
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* This is a utility class with only static functions
|
|
|
|
|
* for dealing with namespaces that encodes all the
|
|
|
|
|
* "magic" behaviors of them based on index. The textual
|
|
|
|
|
* names of the namespaces are handled by Language.php.
|
|
|
|
|
*
|
|
|
|
|
* These are synonyms for the names given in the language file
|
|
|
|
|
* Users and translators should not change them
|
2004-09-03 23:00:01 +00:00
|
|
|
*
|
|
|
|
|
*/
|
2007-12-16 15:39:24 +00:00
|
|
|
|
2008-03-21 23:13:34 +00:00
|
|
|
class MWNamespace {
|
2003-04-14 23:10:40 +00:00
|
|
|
|
2009-10-09 12:52:16 +00:00
|
|
|
/**
|
2010-02-05 04:25:30 +00:00
|
|
|
* These namespaces should always be first-letter capitalized, now and
|
|
|
|
|
* forevermore. Historically, they could've probably been lowercased too,
|
2009-10-09 12:52:16 +00:00
|
|
|
* but some things are just too ingrained now. :)
|
|
|
|
|
*/
|
2009-12-15 04:20:17 +00:00
|
|
|
private static $alwaysCapitalizedNamespaces = array( NS_SPECIAL, NS_USER, NS_MEDIAWIKI );
|
2009-10-09 12:52:16 +00:00
|
|
|
|
2011-02-21 22:17:06 +00:00
|
|
|
/**
|
2011-02-23 19:56:33 +00:00
|
|
|
* Throw an exception when trying to get the subject or talk page
|
|
|
|
|
* for a given namespace where it does not make sense.
|
2011-08-20 12:47:17 +00:00
|
|
|
* Special namespaces are defined in includes/Defines.php and have
|
2011-02-21 22:17:06 +00:00
|
|
|
* a value below 0 (ex: NS_SPECIAL = -1 , NS_MEDIA = -2)
|
|
|
|
|
*
|
2011-05-21 19:07:24 +00:00
|
|
|
* @param $index
|
|
|
|
|
* @param $method
|
|
|
|
|
*
|
|
|
|
|
* @return true
|
2011-02-21 22:17:06 +00:00
|
|
|
*/
|
|
|
|
|
private static function isMethodValidFor( $index, $method ) {
|
|
|
|
|
if( $index < NS_MAIN ) {
|
2011-02-23 19:56:33 +00:00
|
|
|
throw new MWException( "$method does not make any sense for given namespace $index" );
|
2011-02-21 22:17:06 +00:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2007-08-14 01:14:42 +00:00
|
|
|
* Can pages in the given namespace be moved?
|
|
|
|
|
*
|
2008-05-21 18:18:58 +00:00
|
|
|
* @param $index Int: namespace index
|
2004-09-02 23:28:24 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2007-08-14 01:14:42 +00:00
|
|
|
public static function isMovable( $index ) {
|
2008-07-10 21:53:14 +00:00
|
|
|
global $wgAllowImageMoving;
|
2008-12-01 17:14:30 +00:00
|
|
|
return !( $index < NS_MAIN || ($index == NS_FILE && !$wgAllowImageMoving) || $index == NS_CATEGORY );
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
2005-11-03 01:29:02 +00:00
|
|
|
/**
|
2007-08-14 01:14:42 +00:00
|
|
|
* Is the given namespace is a subject (non-talk) namespace?
|
|
|
|
|
*
|
2008-05-21 18:18:58 +00:00
|
|
|
* @param $index Int: namespace index
|
2005-11-03 01:29:02 +00:00
|
|
|
* @return bool
|
2011-11-22 13:34:55 +00:00
|
|
|
* @since 1.19
|
2005-11-03 01:29:02 +00:00
|
|
|
*/
|
2011-11-22 13:34:55 +00:00
|
|
|
public static function isSubject( $index ) {
|
2007-08-14 01:14:42 +00:00
|
|
|
return !self::isTalk( $index );
|
2005-11-03 01:29:02 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-22 13:34:55 +00:00
|
|
|
/**
|
|
|
|
|
* @see self::isSubject
|
|
|
|
|
* @deprecated Please use the more consistently named isSubject (since 1.19)
|
|
|
|
|
*/
|
|
|
|
|
public static function isMain( $index ) {
|
2011-12-13 19:51:03 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.19' );
|
2011-11-22 13:34:55 +00:00
|
|
|
return self::isSubject( $index );
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2007-08-14 01:14:42 +00:00
|
|
|
* Is the given namespace a talk namespace?
|
|
|
|
|
*
|
2008-05-21 18:18:58 +00:00
|
|
|
* @param $index Int: namespace index
|
2004-09-02 23:28:24 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2007-08-14 01:14:42 +00:00
|
|
|
public static function isTalk( $index ) {
|
|
|
|
|
return $index > NS_MAIN
|
|
|
|
|
&& $index % 2;
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2007-08-14 01:14:42 +00:00
|
|
|
* Get the talk namespace index for a given namespace
|
|
|
|
|
*
|
2008-05-21 18:18:58 +00:00
|
|
|
* @param $index Int: namespace index
|
2007-08-14 01:14:42 +00:00
|
|
|
* @return int
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2007-08-14 01:14:42 +00:00
|
|
|
public static function getTalk( $index ) {
|
2011-02-21 22:17:06 +00:00
|
|
|
self::isMethodValidFor( $index, __METHOD__ );
|
2007-08-14 01:14:42 +00:00
|
|
|
return self::isTalk( $index )
|
|
|
|
|
? $index
|
|
|
|
|
: $index + 1;
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
2007-08-14 01:14:42 +00:00
|
|
|
/**
|
|
|
|
|
* Get the subject namespace index for a given namespace
|
2011-02-21 22:17:06 +00:00
|
|
|
* Special namespaces (NS_MEDIA, NS_SPECIAL) are always the subject.
|
2007-08-14 01:14:42 +00:00
|
|
|
*
|
2008-05-21 18:18:58 +00:00
|
|
|
* @param $index Int: Namespace index
|
2007-08-14 01:14:42 +00:00
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public static function getSubject( $index ) {
|
2011-02-21 22:17:06 +00:00
|
|
|
# Handle special namespaces
|
|
|
|
|
if( $index < NS_MAIN ) {
|
|
|
|
|
return $index;
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-14 01:14:42 +00:00
|
|
|
return self::isTalk( $index )
|
|
|
|
|
? $index - 1
|
|
|
|
|
: $index;
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
2010-02-05 04:25:30 +00:00
|
|
|
|
2011-02-21 22:17:06 +00:00
|
|
|
/**
|
|
|
|
|
* Get the associated namespace.
|
|
|
|
|
* For talk namespaces, returns the subject (non-talk) namespace
|
|
|
|
|
* For subject (non-talk) namespaces, returns the talk namespace
|
|
|
|
|
*
|
|
|
|
|
* @param $index Int: namespace index
|
|
|
|
|
* @return int or null if no associated namespace could be found
|
|
|
|
|
*/
|
|
|
|
|
public static function getAssociated( $index ) {
|
|
|
|
|
self::isMethodValidFor( $index, __METHOD__ );
|
|
|
|
|
|
|
|
|
|
if( self::isMain( $index ) ) {
|
|
|
|
|
return self::getTalk( $index );
|
|
|
|
|
} elseif( self::isTalk( $index ) ) {
|
|
|
|
|
return self::getSubject( $index );
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-16 04:06:30 +00:00
|
|
|
/**
|
|
|
|
|
* Returns whether the specified namespace exists
|
2011-05-21 19:07:24 +00:00
|
|
|
*
|
|
|
|
|
* @param $index
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
2011-11-22 13:34:55 +00:00
|
|
|
* @since 1.19
|
2009-10-16 04:06:30 +00:00
|
|
|
*/
|
|
|
|
|
public static function exists( $index ) {
|
2010-08-20 10:25:10 +00:00
|
|
|
$nslist = self::getCanonicalNamespaces();
|
|
|
|
|
return isset( $nslist[$index] );
|
2009-10-16 04:06:30 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-22 13:34:55 +00:00
|
|
|
/**
|
|
|
|
|
* Returns whether the specified namespaces are the same namespace
|
|
|
|
|
*
|
|
|
|
|
* @note It's possible that in the future we may start using something
|
|
|
|
|
* other than just namespace indexes. Under that circumstance making use
|
|
|
|
|
* of this function rather than directly doing comparison will make
|
|
|
|
|
* sure that code will not potentially break.
|
|
|
|
|
*
|
2011-12-05 02:29:08 +00:00
|
|
|
* @param $ns1 int The first namespace index
|
|
|
|
|
* @param $ns2 int The second namespae index
|
2011-11-22 13:34:55 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
* @since 1.19
|
|
|
|
|
*/
|
|
|
|
|
public static function equals( $ns1, $ns2 ) {
|
|
|
|
|
return $ns1 == $ns2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns whether the specified namespaces share the same subject.
|
|
|
|
|
* eg: NS_USER and NS_USER wil return true, as well
|
|
|
|
|
* NS_USER and NS_USER_TALK will return true.
|
|
|
|
|
*
|
2011-12-05 02:29:08 +00:00
|
|
|
* @param $ns1 int The first namespace index
|
|
|
|
|
* @param $ns2 int The second namespae index
|
2011-11-22 13:34:55 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
* @since 1.19
|
|
|
|
|
*/
|
|
|
|
|
public static function subjectEquals( $ns1, $ns2 ) {
|
|
|
|
|
return self::getSubject( $ns1 ) == self::getSubject( $ns2 );
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2010-08-20 10:25:10 +00:00
|
|
|
* Returns array of all defined namespaces with their canonical
|
|
|
|
|
* (English) names.
|
|
|
|
|
*
|
|
|
|
|
* @return \array
|
|
|
|
|
* @since 1.17
|
|
|
|
|
*/
|
|
|
|
|
public static function getCanonicalNamespaces() {
|
|
|
|
|
static $namespaces = null;
|
|
|
|
|
if ( $namespaces === null ) {
|
|
|
|
|
global $wgExtraNamespaces, $wgCanonicalNamespaceNames;
|
2011-01-21 03:48:00 +00:00
|
|
|
$namespaces = array( NS_MAIN => '' ) + $wgCanonicalNamespaceNames;
|
2010-08-20 10:25:10 +00:00
|
|
|
if ( is_array( $wgExtraNamespaces ) ) {
|
2011-01-21 03:48:00 +00:00
|
|
|
$namespaces += $wgExtraNamespaces;
|
2010-08-20 10:25:10 +00:00
|
|
|
}
|
2010-08-24 19:58:55 +00:00
|
|
|
wfRunHooks( 'CanonicalNamespaces', array( &$namespaces ) );
|
2010-08-20 10:25:10 +00:00
|
|
|
}
|
|
|
|
|
return $namespaces;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the canonical (English) name for a given index
|
2007-08-14 01:14:42 +00:00
|
|
|
*
|
2008-05-21 18:18:58 +00:00
|
|
|
* @param $index Int: namespace index
|
2008-12-16 23:57:21 +00:00
|
|
|
* @return string or false if no canonical definition.
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2007-08-14 01:14:42 +00:00
|
|
|
public static function getCanonicalName( $index ) {
|
2010-08-20 10:25:10 +00:00
|
|
|
$nslist = self::getCanonicalNamespaces();
|
|
|
|
|
if( isset( $nslist[$index] ) ) {
|
|
|
|
|
return $nslist[$index];
|
2008-12-16 23:57:21 +00:00
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2004-04-05 10:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Returns the index for a given canonical name, or NULL
|
|
|
|
|
* The input *must* be converted to lower case first
|
2007-08-14 01:14:42 +00:00
|
|
|
*
|
2008-05-21 18:18:58 +00:00
|
|
|
* @param $name String: namespace name
|
2007-08-14 01:14:42 +00:00
|
|
|
* @return int
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2007-08-14 01:14:42 +00:00
|
|
|
public static function getCanonicalIndex( $name ) {
|
2004-04-05 10:38:40 +00:00
|
|
|
static $xNamespaces = false;
|
|
|
|
|
if ( $xNamespaces === false ) {
|
|
|
|
|
$xNamespaces = array();
|
2010-08-20 10:25:10 +00:00
|
|
|
foreach ( self::getCanonicalNamespaces() as $i => $text ) {
|
2004-04-05 10:38:40 +00:00
|
|
|
$xNamespaces[strtolower($text)] = $i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( array_key_exists( $name, $xNamespaces ) ) {
|
|
|
|
|
return $xNamespaces[$name];
|
|
|
|
|
} else {
|
2009-12-11 21:07:27 +00:00
|
|
|
return null;
|
2004-04-05 10:38:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2010-07-25 22:29:05 +00:00
|
|
|
/**
|
|
|
|
|
* Returns an array of the namespaces (by integer id) that exist on the
|
|
|
|
|
* wiki. Used primarily by the api in help documentation.
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function getValidNamespaces() {
|
|
|
|
|
static $mValidNamespaces = null;
|
|
|
|
|
|
|
|
|
|
if ( is_null( $mValidNamespaces ) ) {
|
2010-08-20 10:25:10 +00:00
|
|
|
foreach ( array_keys( self::getCanonicalNamespaces() ) as $ns ) {
|
2010-08-23 08:46:13 +00:00
|
|
|
if ( $ns >= 0 ) {
|
2010-07-25 22:29:05 +00:00
|
|
|
$mValidNamespaces[] = $ns;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $mValidNamespaces;
|
|
|
|
|
}
|
|
|
|
|
|
2006-04-12 15:38:17 +00:00
|
|
|
/**
|
|
|
|
|
* Can this namespace ever have a talk namespace?
|
2007-08-14 01:14:42 +00:00
|
|
|
*
|
2008-05-21 18:18:58 +00:00
|
|
|
* @param $index Int: namespace index
|
2007-08-14 01:14:42 +00:00
|
|
|
* @return bool
|
2006-04-12 15:38:17 +00:00
|
|
|
*/
|
2007-08-14 01:14:42 +00:00
|
|
|
public static function canTalk( $index ) {
|
2010-02-05 04:25:30 +00:00
|
|
|
return $index >= NS_MAIN;
|
2006-04-12 15:38:17 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-01-08 15:32:58 +00:00
|
|
|
/**
|
2008-05-23 22:00:14 +00:00
|
|
|
* Does this namespace contain content, for the purposes of calculating
|
|
|
|
|
* statistics, etc?
|
2007-01-08 15:32:58 +00:00
|
|
|
*
|
2008-05-21 18:18:58 +00:00
|
|
|
* @param $index Int: index to check
|
2007-01-08 15:32:58 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public static function isContent( $index ) {
|
|
|
|
|
global $wgContentNamespaces;
|
|
|
|
|
return $index == NS_MAIN || in_array( $index, $wgContentNamespaces );
|
2007-07-05 18:20:46 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-07-05 18:20:46 +00:00
|
|
|
/**
|
|
|
|
|
* Can pages in a namespace be watched?
|
|
|
|
|
*
|
2008-05-21 18:18:58 +00:00
|
|
|
* @param $index Int
|
2007-07-05 18:20:46 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public static function isWatchable( $index ) {
|
|
|
|
|
return $index >= NS_MAIN;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2008-05-23 22:00:14 +00:00
|
|
|
/**
|
|
|
|
|
* Does the namespace allow subpages?
|
|
|
|
|
*
|
|
|
|
|
* @param $index int Index to check
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public static function hasSubpages( $index ) {
|
|
|
|
|
global $wgNamespacesWithSubpages;
|
|
|
|
|
return !empty( $wgNamespacesWithSubpages[$index] );
|
|
|
|
|
}
|
2010-02-05 04:25:30 +00:00
|
|
|
|
2010-12-22 14:16:25 +00:00
|
|
|
/**
|
|
|
|
|
* Get a list of all namespace indices which are considered to contain content
|
|
|
|
|
* @return array of namespace indices
|
|
|
|
|
*/
|
|
|
|
|
public static function getContentNamespaces() {
|
|
|
|
|
global $wgContentNamespaces;
|
|
|
|
|
if( !is_array( $wgContentNamespaces ) || $wgContentNamespaces === array() ) {
|
|
|
|
|
return NS_MAIN;
|
|
|
|
|
} elseif ( !in_array( NS_MAIN, $wgContentNamespaces ) ) {
|
|
|
|
|
// always force NS_MAIN to be part of array (to match the algorithm used by isContent)
|
|
|
|
|
return array_merge( array( NS_MAIN ), $wgContentNamespaces );
|
|
|
|
|
} else {
|
|
|
|
|
return $wgContentNamespaces;
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-10-09 12:52:16 +00:00
|
|
|
/**
|
|
|
|
|
* Is the namespace first-letter capitalized?
|
2010-02-05 04:25:30 +00:00
|
|
|
*
|
2009-10-09 12:52:16 +00:00
|
|
|
* @param $index int Index to check
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public static function isCapitalized( $index ) {
|
|
|
|
|
global $wgCapitalLinks, $wgCapitalLinkOverrides;
|
|
|
|
|
// Turn NS_MEDIA into NS_FILE
|
|
|
|
|
$index = $index === NS_MEDIA ? NS_FILE : $index;
|
2010-02-05 04:25:30 +00:00
|
|
|
|
2009-10-09 12:52:16 +00:00
|
|
|
// Make sure to get the subject of our namespace
|
|
|
|
|
$index = self::getSubject( $index );
|
2010-02-05 04:25:30 +00:00
|
|
|
|
2009-10-09 12:52:16 +00:00
|
|
|
// Some namespaces are special and should always be upper case
|
|
|
|
|
if ( in_array( $index, self::$alwaysCapitalizedNamespaces ) ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $wgCapitalLinkOverrides[ $index ] ) ) {
|
|
|
|
|
// $wgCapitalLinkOverrides is explicitly set
|
|
|
|
|
return $wgCapitalLinkOverrides[ $index ];
|
|
|
|
|
}
|
|
|
|
|
// Default to the global setting
|
|
|
|
|
return $wgCapitalLinks;
|
|
|
|
|
}
|
2011-02-12 20:40:40 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Does the namespace (potentially) have different aliases for different
|
|
|
|
|
* genders. Not all languages make a distinction here.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.18
|
|
|
|
|
* @param $index int Index to check
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public static function hasGenderDistinction( $index ) {
|
|
|
|
|
return $index == NS_USER || $index == NS_USER_TALK;
|
|
|
|
|
}
|
|
|
|
|
|
2008-04-14 07:45:50 +00:00
|
|
|
}
|