Namespace.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * Provide things related to namespaces
  4. * @file
  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_FILE => 'File',
  19. NS_FILE_TALK => 'File_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. if( is_array( $wgExtraNamespaces ) ) {
  30. $wgCanonicalNamespaceNames = $wgCanonicalNamespaceNames + $wgExtraNamespaces;
  31. }
  32. /**
  33. * This is a utility class with only static functions
  34. * for dealing with namespaces that encodes all the
  35. * "magic" behaviors of them based on index. The textual
  36. * names of the namespaces are handled by Language.php.
  37. *
  38. * These are synonyms for the names given in the language file
  39. * Users and translators should not change them
  40. *
  41. */
  42. class MWNamespace {
  43. /**
  44. * Can pages in the given namespace be moved?
  45. *
  46. * @param $index Int: namespace index
  47. * @return bool
  48. */
  49. public static function isMovable( $index ) {
  50. global $wgAllowImageMoving;
  51. return !( $index < NS_MAIN || ($index == NS_FILE && !$wgAllowImageMoving) || $index == NS_CATEGORY );
  52. }
  53. /**
  54. * Is the given namespace is a subject (non-talk) namespace?
  55. *
  56. * @param $index Int: namespace index
  57. * @return bool
  58. */
  59. public static function isMain( $index ) {
  60. return !self::isTalk( $index );
  61. }
  62. /**
  63. * Is the given namespace a talk namespace?
  64. *
  65. * @param $index Int: namespace index
  66. * @return bool
  67. */
  68. public static function isTalk( $index ) {
  69. return $index > NS_MAIN
  70. && $index % 2;
  71. }
  72. /**
  73. * Get the talk namespace index for a given namespace
  74. *
  75. * @param $index Int: namespace index
  76. * @return int
  77. */
  78. public static function getTalk( $index ) {
  79. return self::isTalk( $index )
  80. ? $index
  81. : $index + 1;
  82. }
  83. /**
  84. * Get the subject namespace index for a given namespace
  85. *
  86. * @param $index Int: Namespace index
  87. * @return int
  88. */
  89. public static function getSubject( $index ) {
  90. return self::isTalk( $index )
  91. ? $index - 1
  92. : $index;
  93. }
  94. /**
  95. * Returns the canonical (English Wikipedia) name for a given index
  96. *
  97. * @param $index Int: namespace index
  98. * @return string or false if no canonical definition.
  99. */
  100. public static function getCanonicalName( $index ) {
  101. global $wgCanonicalNamespaceNames;
  102. if( isset( $wgCanonicalNamespaceNames[$index] ) ) {
  103. return $wgCanonicalNamespaceNames[$index];
  104. } else {
  105. return false;
  106. }
  107. }
  108. /**
  109. * Returns the index for a given canonical name, or NULL
  110. * The input *must* be converted to lower case first
  111. *
  112. * @param $name String: namespace name
  113. * @return int
  114. */
  115. public static function getCanonicalIndex( $name ) {
  116. global $wgCanonicalNamespaceNames;
  117. static $xNamespaces = false;
  118. if ( $xNamespaces === false ) {
  119. $xNamespaces = array();
  120. foreach ( $wgCanonicalNamespaceNames as $i => $text ) {
  121. $xNamespaces[strtolower($text)] = $i;
  122. }
  123. }
  124. if ( array_key_exists( $name, $xNamespaces ) ) {
  125. return $xNamespaces[$name];
  126. } else {
  127. return NULL;
  128. }
  129. }
  130. /**
  131. * Can this namespace ever have a talk namespace?
  132. *
  133. * @param $index Int: namespace index
  134. * @return bool
  135. */
  136. public static function canTalk( $index ) {
  137. return $index >= NS_MAIN;
  138. }
  139. /**
  140. * Does this namespace contain content, for the purposes of calculating
  141. * statistics, etc?
  142. *
  143. * @param $index Int: index to check
  144. * @return bool
  145. */
  146. public static function isContent( $index ) {
  147. global $wgContentNamespaces;
  148. return $index == NS_MAIN || in_array( $index, $wgContentNamespaces );
  149. }
  150. /**
  151. * Can pages in a namespace be watched?
  152. *
  153. * @param $index Int
  154. * @return bool
  155. */
  156. public static function isWatchable( $index ) {
  157. return $index >= NS_MAIN;
  158. }
  159. /**
  160. * Does the namespace allow subpages?
  161. *
  162. * @param $index int Index to check
  163. * @return bool
  164. */
  165. public static function hasSubpages( $index ) {
  166. global $wgNamespacesWithSubpages;
  167. return !empty( $wgNamespacesWithSubpages[$index] );
  168. }
  169. }