Clarify comment on $wgRateLimits.
[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 /*
46 WARNING: The statement below may fail on some versions of PHP: see bug 12294
47 */
48
49 class Namespace {
50
51 /**
52 * Can pages in the given namespace be moved?
53 *
54 * @param int $index Namespace index
55 * @return bool
56 */
57 public static function isMovable( $index ) {
58 return !( $index < NS_MAIN || $index == NS_IMAGE || $index == NS_CATEGORY );
59 }
60
61 /**
62 * Is the given namespace is a subject (non-talk) namespace?
63 *
64 * @param int $index Namespace index
65 * @return bool
66 */
67 public static function isMain( $index ) {
68 return !self::isTalk( $index );
69 }
70
71 /**
72 * Is the given namespace a talk namespace?
73 *
74 * @param int $index Namespace index
75 * @return bool
76 */
77 public static function isTalk( $index ) {
78 return $index > NS_MAIN
79 && $index % 2;
80 }
81
82 /**
83 * Get the talk namespace index for a given namespace
84 *
85 * @param int $index Namespace index
86 * @return int
87 */
88 public static function getTalk( $index ) {
89 return self::isTalk( $index )
90 ? $index
91 : $index + 1;
92 }
93
94 /**
95 * Get the subject namespace index for a given namespace
96 *
97 * @param int $index Namespace index
98 * @return int
99 */
100 public static function getSubject( $index ) {
101 return self::isTalk( $index )
102 ? $index - 1
103 : $index;
104 }
105
106 /**
107 * Returns the canonical (English Wikipedia) name for a given index
108 *
109 * @param int $index Namespace index
110 * @return string
111 */
112 public static function getCanonicalName( $index ) {
113 global $wgCanonicalNamespaceNames;
114 return $wgCanonicalNamespaceNames[$index];
115 }
116
117 /**
118 * Returns the index for a given canonical name, or NULL
119 * The input *must* be converted to lower case first
120 *
121 * @param string $name Namespace name
122 * @return int
123 */
124 public static function getCanonicalIndex( $name ) {
125 global $wgCanonicalNamespaceNames;
126 static $xNamespaces = false;
127 if ( $xNamespaces === false ) {
128 $xNamespaces = array();
129 foreach ( $wgCanonicalNamespaceNames as $i => $text ) {
130 $xNamespaces[strtolower($text)] = $i;
131 }
132 }
133 if ( array_key_exists( $name, $xNamespaces ) ) {
134 return $xNamespaces[$name];
135 } else {
136 return NULL;
137 }
138 }
139
140 /**
141 * Can this namespace ever have a talk namespace?
142 *
143 * @param $index Namespace index
144 * @return bool
145 */
146 public static function canTalk( $index ) {
147 return $index >= NS_MAIN;
148 }
149
150 /**
151 * Does this namespace contain content, for the purposes
152 * of calculating statistics, etc?
153 *
154 * @param $index Index to check
155 * @return bool
156 */
157 public static function isContent( $index ) {
158 global $wgContentNamespaces;
159 return $index == NS_MAIN || in_array( $index, $wgContentNamespaces );
160 }
161
162 /**
163 * Can pages in a namespace be watched?
164 *
165 * @param int $index
166 * @return bool
167 */
168 public static function isWatchable( $index ) {
169 return $index >= NS_MAIN;
170 }
171
172 }