5930b70de0967241c51da7b472f9624d5ba00d27
[lhc/web/wiklou.git] / includes / Namespace.php
1 <?php
2 /**
3 * Provide things related to namespaces
4 * @package MediaWiki
5 */
6
7 /**
8 * Definitions of the NS_ constants are in Defines.php
9 * @private
10 */
11 $wgCanonicalNamespaceNames = array(
12 NS_MEDIA => 'Media',
13 NS_SPECIAL => 'Special',
14 NS_TALK => 'Talk',
15 NS_USER => 'User',
16 NS_USER_TALK => 'User_talk',
17 NS_PROJECT => 'Project',
18 NS_PROJECT_TALK => 'Project_talk',
19 NS_IMAGE => 'Image',
20 NS_IMAGE_TALK => 'Image_talk',
21 NS_MEDIAWIKI => 'MediaWiki',
22 NS_MEDIAWIKI_TALK => 'MediaWiki_talk',
23 NS_TEMPLATE => 'Template',
24 NS_TEMPLATE_TALK => 'Template_talk',
25 NS_HELP => 'Help',
26 NS_HELP_TALK => 'Help_talk',
27 NS_CATEGORY => 'Category',
28 NS_CATEGORY_TALK => 'Category_talk',
29 );
30
31 if( is_array( $wgExtraNamespaces ) ) {
32 $wgCanonicalNamespaceNames = $wgCanonicalNamespaceNames + $wgExtraNamespaces;
33 }
34
35 /**
36 * This is a utility class with only static functions
37 * for dealing with namespaces that encodes all the
38 * "magic" behaviors of them based on index. The textual
39 * names of the namespaces are handled by Language.php.
40 *
41 * These are synonyms for the names given in the language file
42 * Users and translators should not change them
43 *
44 * @package MediaWiki
45 */
46 class Namespace {
47
48 /**
49 * Check if the given namespace might be moved
50 * @return bool
51 */
52 static function isMovable( $index ) {
53 return !( $index < NS_MAIN || $index == NS_IMAGE || $index == NS_CATEGORY );
54 }
55
56 /**
57 * Check if the given namespace is not a talk page
58 * @return bool
59 */
60 static function isMain( $index ) {
61 return ! Namespace::isTalk( $index );
62 }
63
64 /**
65 * Check if the give namespace is a talk page
66 * @return bool
67 */
68 static function isTalk( $index ) {
69 return ($index > NS_MAIN) // Special namespaces are negative
70 && ($index % 2); // Talk namespaces are odd-numbered
71 }
72
73 /**
74 * Get the talk namespace corresponding to the given index
75 */
76 static function getTalk( $index ) {
77 if ( Namespace::isTalk( $index ) ) {
78 return $index;
79 } else {
80 # FIXME
81 return $index + 1;
82 }
83 }
84
85 static function getSubject( $index ) {
86 if ( Namespace::isTalk( $index ) ) {
87 return $index - 1;
88 } else {
89 return $index;
90 }
91 }
92
93 /**
94 * Returns the canonical (English Wikipedia) name for a given index
95 */
96 static function getCanonicalName( $index ) {
97 global $wgCanonicalNamespaceNames;
98 return $wgCanonicalNamespaceNames[$index];
99 }
100
101 /**
102 * Returns the index for a given canonical name, or NULL
103 * The input *must* be converted to lower case first
104 */
105 static function getCanonicalIndex( $name ) {
106 global $wgCanonicalNamespaceNames;
107 static $xNamespaces = false;
108 if ( $xNamespaces === false ) {
109 $xNamespaces = array();
110 foreach ( $wgCanonicalNamespaceNames as $i => $text ) {
111 $xNamespaces[strtolower($text)] = $i;
112 }
113 }
114 if ( array_key_exists( $name, $xNamespaces ) ) {
115 return $xNamespaces[$name];
116 } else {
117 return NULL;
118 }
119 }
120
121 /**
122 * Can this namespace ever have a talk namespace?
123 * @param $index Namespace index
124 */
125 static function canTalk( $index ) {
126 return( $index >= NS_MAIN );
127 }
128
129 /**
130 * Does this namespace contain content, for the purposes
131 * of calculating statistics, etc?
132 *
133 * @param $index Index to check
134 * @return bool
135 */
136 public static function isContent( $index ) {
137 global $wgContentNamespaces;
138 return $index == NS_MAIN || in_array( $index, $wgContentNamespaces );
139 }
140
141 }
142
143 ?>