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