MediaWiki  REL1_22
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 
00054     private static function isMethodValidFor( $index, $method ) {
00055         if ( $index < NS_MAIN ) {
00056             throw new MWException( "$method does not make any sense for given namespace $index" );
00057         }
00058         return true;
00059     }
00060 
00067     public static function isMovable( $index ) {
00068         global $wgAllowImageMoving;
00069 
00070         $result = !( $index < NS_MAIN || ( $index == NS_FILE && !$wgAllowImageMoving ) || $index == NS_CATEGORY );
00071 
00075         wfRunHooks( 'NamespaceIsMovable', array( $index, &$result ) );
00076 
00077         return $result;
00078     }
00079 
00087     public static function isSubject( $index ) {
00088         return !self::isTalk( $index );
00089     }
00090 
00096     public static function isMain( $index ) {
00097         wfDeprecated( __METHOD__, '1.19' );
00098         return self::isSubject( $index );
00099     }
00100 
00107     public static function isTalk( $index ) {
00108         return $index > NS_MAIN
00109             && $index % 2;
00110     }
00111 
00118     public static function getTalk( $index ) {
00119         self::isMethodValidFor( $index, __METHOD__ );
00120         return self::isTalk( $index )
00121             ? $index
00122             : $index + 1;
00123     }
00124 
00132     public static function getSubject( $index ) {
00133         # Handle special namespaces
00134         if ( $index < NS_MAIN ) {
00135             return $index;
00136         }
00137 
00138         return self::isTalk( $index )
00139             ? $index - 1
00140             : $index;
00141     }
00142 
00151     public static function getAssociated( $index ) {
00152         self::isMethodValidFor( $index, __METHOD__ );
00153 
00154         if ( self::isSubject( $index ) ) {
00155             return self::getTalk( $index );
00156         } elseif ( self::isTalk( $index ) ) {
00157             return self::getSubject( $index );
00158         } else {
00159             return null;
00160         }
00161     }
00162 
00171     public static function exists( $index ) {
00172         $nslist = self::getCanonicalNamespaces();
00173         return isset( $nslist[$index] );
00174     }
00175 
00190     public static function equals( $ns1, $ns2 ) {
00191         return $ns1 == $ns2;
00192     }
00193 
00205     public static function subjectEquals( $ns1, $ns2 ) {
00206         return self::getSubject( $ns1 ) == self::getSubject( $ns2 );
00207     }
00208 
00218     public static function getCanonicalNamespaces( $rebuild = false ) {
00219         static $namespaces = null;
00220         if ( $namespaces === null || $rebuild ) {
00221             global $wgExtraNamespaces, $wgCanonicalNamespaceNames;
00222             $namespaces = array( NS_MAIN => '' ) + $wgCanonicalNamespaceNames;
00223             if ( is_array( $wgExtraNamespaces ) ) {
00224                 $namespaces += $wgExtraNamespaces;
00225             }
00226             wfRunHooks( 'CanonicalNamespaces', array( &$namespaces ) );
00227         }
00228         return $namespaces;
00229     }
00230 
00237     public static function getCanonicalName( $index ) {
00238         $nslist = self::getCanonicalNamespaces();
00239         if ( isset( $nslist[$index] ) ) {
00240             return $nslist[$index];
00241         } else {
00242             return false;
00243         }
00244     }
00245 
00253     public static function getCanonicalIndex( $name ) {
00254         static $xNamespaces = false;
00255         if ( $xNamespaces === false ) {
00256             $xNamespaces = array();
00257             foreach ( self::getCanonicalNamespaces() as $i => $text ) {
00258                 $xNamespaces[strtolower( $text )] = $i;
00259             }
00260         }
00261         if ( array_key_exists( $name, $xNamespaces ) ) {
00262             return $xNamespaces[$name];
00263         } else {
00264             return null;
00265         }
00266     }
00267 
00273     public static function getValidNamespaces() {
00274         static $mValidNamespaces = null;
00275 
00276         if ( is_null( $mValidNamespaces ) ) {
00277             foreach ( array_keys( self::getCanonicalNamespaces() ) as $ns ) {
00278                 if ( $ns >= 0 ) {
00279                     $mValidNamespaces[] = $ns;
00280                 }
00281             }
00282         }
00283 
00284         return $mValidNamespaces;
00285     }
00286 
00293     public static function canTalk( $index ) {
00294         return $index >= NS_MAIN;
00295     }
00296 
00304     public static function isContent( $index ) {
00305         global $wgContentNamespaces;
00306         return $index == NS_MAIN || in_array( $index, $wgContentNamespaces );
00307     }
00308 
00315     public static function isWatchable( $index ) {
00316         return $index >= NS_MAIN;
00317     }
00318 
00325     public static function hasSubpages( $index ) {
00326         global $wgNamespacesWithSubpages;
00327         return !empty( $wgNamespacesWithSubpages[$index] );
00328     }
00329 
00334     public static function getContentNamespaces() {
00335         global $wgContentNamespaces;
00336         if ( !is_array( $wgContentNamespaces ) || $wgContentNamespaces === array() ) {
00337             return array( NS_MAIN );
00338         } elseif ( !in_array( NS_MAIN, $wgContentNamespaces ) ) {
00339             // always force NS_MAIN to be part of array (to match the algorithm used by isContent)
00340             return array_merge( array( NS_MAIN ), $wgContentNamespaces );
00341         } else {
00342             return $wgContentNamespaces;
00343         }
00344     }
00345 
00352     public static function getSubjectNamespaces() {
00353         return array_filter(
00354             MWNamespace::getValidNamespaces(),
00355             'MWNamespace::isSubject'
00356         );
00357     }
00358 
00365     public static function getTalkNamespaces() {
00366         return array_filter(
00367             MWNamespace::getValidNamespaces(),
00368             'MWNamespace::isTalk'
00369         );
00370     }
00371 
00378     public static function isCapitalized( $index ) {
00379         global $wgCapitalLinks, $wgCapitalLinkOverrides;
00380         // Turn NS_MEDIA into NS_FILE
00381         $index = $index === NS_MEDIA ? NS_FILE : $index;
00382 
00383         // Make sure to get the subject of our namespace
00384         $index = self::getSubject( $index );
00385 
00386         // Some namespaces are special and should always be upper case
00387         if ( in_array( $index, self::$alwaysCapitalizedNamespaces ) ) {
00388             return true;
00389         }
00390         if ( isset( $wgCapitalLinkOverrides[$index] ) ) {
00391             // $wgCapitalLinkOverrides is explicitly set
00392             return $wgCapitalLinkOverrides[$index];
00393         }
00394         // Default to the global setting
00395         return $wgCapitalLinks;
00396     }
00397 
00406     public static function hasGenderDistinction( $index ) {
00407         return $index == NS_USER || $index == NS_USER_TALK;
00408     }
00409 
00417     public static function isNonincludable( $index ) {
00418         global $wgNonincludableNamespaces;
00419         return $wgNonincludableNamespaces && in_array( $index, $wgNonincludableNamespaces );
00420     }
00421 
00430     public static function getNamespaceContentModel( $index ) {
00431         global $wgNamespaceContentModels;
00432         return isset( $wgNamespaceContentModels[$index] )
00433             ? $wgNamespaceContentModels[$index]
00434             : null;
00435     }
00436 }