MediaWiki  REL1_20
Namespace.php
Go to the documentation of this file.
00001 <?php
00033 class MWNamespace {
00034 
00040         private static $alwaysCapitalizedNamespaces = array( NS_SPECIAL, NS_USER, NS_MEDIAWIKI );
00041 
00053         private static function isMethodValidFor( $index, $method ) {
00054                 if ( $index < NS_MAIN ) {
00055                         throw new MWException( "$method does not make any sense for given namespace $index" );
00056                 }
00057                 return true;
00058         }
00059 
00066         public static function isMovable( $index ) {
00067                 global $wgAllowImageMoving;
00068 
00069                 $result = !( $index < NS_MAIN || ( $index == NS_FILE && !$wgAllowImageMoving )  || $index == NS_CATEGORY );
00070 
00074                 wfRunHooks( 'NamespaceIsMovable', array( $index, &$result ) );
00075 
00076                 return $result;
00077         }
00078 
00086         public static function isSubject( $index ) {
00087                 return !self::isTalk( $index );
00088         }
00089 
00095         public static function isMain( $index ) {
00096                 wfDeprecated( __METHOD__, '1.19' );
00097                 return self::isSubject( $index );
00098         }
00099 
00106         public static function isTalk( $index ) {
00107                 return $index > NS_MAIN
00108                         && $index % 2;
00109         }
00110 
00117         public static function getTalk( $index ) {
00118                 self::isMethodValidFor( $index, __METHOD__ );
00119                 return self::isTalk( $index )
00120                         ? $index
00121                         : $index + 1;
00122         }
00123 
00131         public static function getSubject( $index ) {
00132                 # Handle special namespaces
00133                 if ( $index < NS_MAIN ) {
00134                         return $index;
00135                 }
00136 
00137                 return self::isTalk( $index )
00138                         ? $index - 1
00139                         : $index;
00140         }
00141 
00150         public static function getAssociated( $index ) {
00151                 self::isMethodValidFor( $index, __METHOD__ );
00152 
00153                 if ( self::isSubject( $index ) ) {
00154                         return self::getTalk( $index );
00155                 } elseif ( self::isTalk( $index ) ) {
00156                         return self::getSubject( $index );
00157                 } else {
00158                         return null;
00159                 }
00160         }
00161 
00170         public static function exists( $index ) {
00171                 $nslist = self::getCanonicalNamespaces();
00172                 return isset( $nslist[$index] );
00173         }
00174 
00189         public static function equals( $ns1, $ns2 ) {
00190                 return $ns1 == $ns2;
00191         }
00192 
00204         public static function subjectEquals( $ns1, $ns2 ) {
00205                 return self::getSubject( $ns1 ) == self::getSubject( $ns2 );
00206         }
00207 
00215         public static function getCanonicalNamespaces() {
00216                 static $namespaces = null;
00217                 if ( $namespaces === null ) {
00218                         global $wgExtraNamespaces, $wgCanonicalNamespaceNames;
00219                         $namespaces = array( NS_MAIN => '' ) + $wgCanonicalNamespaceNames;
00220                         if ( is_array( $wgExtraNamespaces ) ) {
00221                                 $namespaces += $wgExtraNamespaces;
00222                         }
00223                         wfRunHooks( 'CanonicalNamespaces', array( &$namespaces ) );
00224                 }
00225                 return $namespaces;
00226         }
00227 
00234         public static function getCanonicalName( $index ) {
00235                 $nslist = self::getCanonicalNamespaces();
00236                 if ( isset( $nslist[$index] ) ) {
00237                         return $nslist[$index];
00238                 } else {
00239                         return false;
00240                 }
00241         }
00242 
00250         public static function getCanonicalIndex( $name ) {
00251                 static $xNamespaces = false;
00252                 if ( $xNamespaces === false ) {
00253                         $xNamespaces = array();
00254                         foreach ( self::getCanonicalNamespaces() as $i => $text ) {
00255                                 $xNamespaces[strtolower( $text )] = $i;
00256                         }
00257                 }
00258                 if ( array_key_exists( $name, $xNamespaces ) ) {
00259                         return $xNamespaces[$name];
00260                 } else {
00261                         return null;
00262                 }
00263         }
00264 
00270         public static function getValidNamespaces() {
00271                 static $mValidNamespaces = null;
00272 
00273                 if ( is_null( $mValidNamespaces ) ) {
00274                         foreach ( array_keys( self::getCanonicalNamespaces() ) as $ns ) {
00275                                 if ( $ns >= 0 ) {
00276                                         $mValidNamespaces[] = $ns;
00277                                 }
00278                         }
00279                 }
00280 
00281                 return $mValidNamespaces;
00282         }
00283 
00290          public static function canTalk( $index ) {
00291                 return $index >= NS_MAIN;
00292          }
00293 
00301         public static function isContent( $index ) {
00302                 global $wgContentNamespaces;
00303                 return $index == NS_MAIN || in_array( $index, $wgContentNamespaces );
00304         }
00305 
00312         public static function isWatchable( $index ) {
00313                 return $index >= NS_MAIN;
00314         }
00315 
00322         public static function hasSubpages( $index ) {
00323                 global $wgNamespacesWithSubpages;
00324                 return !empty( $wgNamespacesWithSubpages[$index] );
00325         }
00326 
00331         public static function getContentNamespaces() {
00332                 global $wgContentNamespaces;
00333                 if ( !is_array( $wgContentNamespaces ) || $wgContentNamespaces === array() ) {
00334                         return NS_MAIN;
00335                 } elseif ( !in_array( NS_MAIN, $wgContentNamespaces ) ) {
00336                         // always force NS_MAIN to be part of array (to match the algorithm used by isContent)
00337                         return array_merge( array( NS_MAIN ), $wgContentNamespaces );
00338                 } else {
00339                         return $wgContentNamespaces;
00340                 }
00341         }
00342 
00349         public static function getSubjectNamespaces() {
00350                 return array_filter(
00351                         MWNamespace::getValidNamespaces(),
00352                         'MWNamespace::isSubject'
00353                 );
00354         }
00355 
00362         public static function getTalkNamespaces() {
00363                 return array_filter(
00364                         MWNamespace::getValidNamespaces(),
00365                         'MWNamespace::isTalk'
00366                 );
00367         }
00368 
00375         public static function isCapitalized( $index ) {
00376                 global $wgCapitalLinks, $wgCapitalLinkOverrides;
00377                 // Turn NS_MEDIA into NS_FILE
00378                 $index = $index === NS_MEDIA ? NS_FILE : $index;
00379 
00380                 // Make sure to get the subject of our namespace
00381                 $index = self::getSubject( $index );
00382 
00383                 // Some namespaces are special and should always be upper case
00384                 if ( in_array( $index, self::$alwaysCapitalizedNamespaces ) ) {
00385                         return true;
00386                 }
00387                 if ( isset( $wgCapitalLinkOverrides[ $index ] ) ) {
00388                         // $wgCapitalLinkOverrides is explicitly set
00389                         return $wgCapitalLinkOverrides[ $index ];
00390                 }
00391                 // Default to the global setting
00392                 return $wgCapitalLinks;
00393         }
00394 
00403         public static function hasGenderDistinction( $index ) {
00404                 return $index == NS_USER || $index == NS_USER_TALK;
00405         }
00406 
00414         public static function isNonincludable( $index ) {
00415                 global $wgNonincludableNamespaces;
00416                 return $wgNonincludableNamespaces && in_array( $index, $wgNonincludableNamespaces );
00417         }
00418 
00419 }