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