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