MediaWiki  REL1_19
Namespace.php
Go to the documentation of this file.
00001 <?php
00018 class MWNamespace {
00019 
00025         private static $alwaysCapitalizedNamespaces = array( NS_SPECIAL, NS_USER, NS_MEDIAWIKI );
00026 
00038         private static function isMethodValidFor( $index, $method ) {
00039                 if ( $index < NS_MAIN ) {
00040                         throw new MWException( "$method does not make any sense for given namespace $index" );
00041                 }
00042                 return true;
00043         }
00044 
00051         public static function isMovable( $index ) {
00052                 global $wgAllowImageMoving;
00053                 return !( $index < NS_MAIN || ( $index == NS_FILE && !$wgAllowImageMoving )  || $index == NS_CATEGORY );
00054         }
00055 
00063         public static function isSubject( $index ) {
00064                 return !self::isTalk( $index );
00065         }
00066 
00071         public static function isMain( $index ) {
00072                 wfDeprecated( __METHOD__, '1.19' );
00073                 return self::isSubject( $index );
00074         }
00075 
00082         public static function isTalk( $index ) {
00083                 return $index > NS_MAIN
00084                         && $index % 2;
00085         }
00086 
00093         public static function getTalk( $index ) {
00094                 self::isMethodValidFor( $index, __METHOD__ );
00095                 return self::isTalk( $index )
00096                         ? $index
00097                         : $index + 1;
00098         }
00099 
00107         public static function getSubject( $index ) {
00108                 # Handle special namespaces
00109                 if ( $index < NS_MAIN ) {
00110                         return $index;
00111                 }
00112 
00113                 return self::isTalk( $index )
00114                         ? $index - 1
00115                         : $index;
00116         }
00117 
00126         public static function getAssociated( $index ) {
00127                 self::isMethodValidFor( $index, __METHOD__ );
00128 
00129                 if ( self::isSubject( $index ) ) {
00130                         return self::getTalk( $index );
00131                 } elseif ( self::isTalk( $index ) ) {
00132                         return self::getSubject( $index );
00133                 } else {
00134                         return null;
00135                 }
00136         }
00137 
00146         public static function exists( $index ) {
00147                 $nslist = self::getCanonicalNamespaces();
00148                 return isset( $nslist[$index] );
00149         }
00150 
00165         public static function equals( $ns1, $ns2 ) {
00166                 return $ns1 == $ns2;
00167         }
00168 
00180         public static function subjectEquals( $ns1, $ns2 ) {
00181                 return self::getSubject( $ns1 ) == self::getSubject( $ns2 );
00182         }
00183 
00191         public static function getCanonicalNamespaces() {
00192                 static $namespaces = null;
00193                 if ( $namespaces === null ) {
00194                         global $wgExtraNamespaces, $wgCanonicalNamespaceNames;
00195                         $namespaces = array( NS_MAIN => '' ) + $wgCanonicalNamespaceNames;
00196                         if ( is_array( $wgExtraNamespaces ) ) {
00197                                 $namespaces += $wgExtraNamespaces;
00198                         }
00199                         wfRunHooks( 'CanonicalNamespaces', array( &$namespaces ) );
00200                 }
00201                 return $namespaces;
00202         }
00203 
00210         public static function getCanonicalName( $index ) {
00211                 $nslist = self::getCanonicalNamespaces();
00212                 if ( isset( $nslist[$index] ) ) {
00213                         return $nslist[$index];
00214                 } else {
00215                         return false;
00216                 }
00217         }
00218 
00226         public static function getCanonicalIndex( $name ) {
00227                 static $xNamespaces = false;
00228                 if ( $xNamespaces === false ) {
00229                         $xNamespaces = array();
00230                         foreach ( self::getCanonicalNamespaces() as $i => $text ) {
00231                                 $xNamespaces[strtolower( $text )] = $i;
00232                         }
00233                 }
00234                 if ( array_key_exists( $name, $xNamespaces ) ) {
00235                         return $xNamespaces[$name];
00236                 } else {
00237                         return null;
00238                 }
00239         }
00240 
00246         public static function getValidNamespaces() {
00247                 static $mValidNamespaces = null;
00248 
00249                 if ( is_null( $mValidNamespaces ) ) {
00250                         foreach ( array_keys( self::getCanonicalNamespaces() ) as $ns ) {
00251                                 if ( $ns >= 0 ) {
00252                                         $mValidNamespaces[] = $ns;
00253                                 }
00254                         }
00255                 }
00256 
00257                 return $mValidNamespaces;
00258         }
00259 
00266          public static function canTalk( $index ) {
00267                 return $index >= NS_MAIN;
00268          }
00269 
00277         public static function isContent( $index ) {
00278                 global $wgContentNamespaces;
00279                 return $index == NS_MAIN || in_array( $index, $wgContentNamespaces );
00280         }
00281 
00288         public static function isWatchable( $index ) {
00289                 return $index >= NS_MAIN;
00290         }
00291 
00298         public static function hasSubpages( $index ) {
00299                 global $wgNamespacesWithSubpages;
00300                 return !empty( $wgNamespacesWithSubpages[$index] );
00301         }
00302 
00307         public static function getContentNamespaces() {
00308                 global $wgContentNamespaces;
00309                 if ( !is_array( $wgContentNamespaces ) || $wgContentNamespaces === array() ) {
00310                         return NS_MAIN;
00311                 } elseif ( !in_array( NS_MAIN, $wgContentNamespaces ) ) {
00312                         // always force NS_MAIN to be part of array (to match the algorithm used by isContent)
00313                         return array_merge( array( NS_MAIN ), $wgContentNamespaces );
00314                 } else {
00315                         return $wgContentNamespaces;
00316                 }
00317         }
00324         public static function isCapitalized( $index ) {
00325                 global $wgCapitalLinks, $wgCapitalLinkOverrides;
00326                 // Turn NS_MEDIA into NS_FILE
00327                 $index = $index === NS_MEDIA ? NS_FILE : $index;
00328 
00329                 // Make sure to get the subject of our namespace
00330                 $index = self::getSubject( $index );
00331 
00332                 // Some namespaces are special and should always be upper case
00333                 if ( in_array( $index, self::$alwaysCapitalizedNamespaces ) ) {
00334                         return true;
00335                 }
00336                 if ( isset( $wgCapitalLinkOverrides[ $index ] ) ) {
00337                         // $wgCapitalLinkOverrides is explicitly set
00338                         return $wgCapitalLinkOverrides[ $index ];
00339                 }
00340                 // Default to the global setting
00341                 return $wgCapitalLinks;
00342         }
00343 
00352         public static function hasGenderDistinction( $index ) {
00353                 return $index == NS_USER || $index == NS_USER_TALK;
00354         }
00355 
00356 }