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