MediaWiki  1.23.1
Namespace.php
Go to the documentation of this file.
1 <?php
33 class MWNamespace {
34 
41 
54  private static function isMethodValidFor( $index, $method ) {
55  if ( $index < NS_MAIN ) {
56  throw new MWException( "$method does not make any sense for given namespace $index" );
57  }
58  return true;
59  }
60 
67  public static function isMovable( $index ) {
68  global $wgAllowImageMoving;
69 
70  $result = !( $index < NS_MAIN || ( $index == NS_FILE && !$wgAllowImageMoving ) || $index == NS_CATEGORY );
71 
75  wfRunHooks( 'NamespaceIsMovable', array( $index, &$result ) );
76 
77  return $result;
78  }
79 
87  public static function isSubject( $index ) {
88  return !self::isTalk( $index );
89  }
90 
96  public static function isMain( $index ) {
97  wfDeprecated( __METHOD__, '1.19' );
98  return self::isSubject( $index );
99  }
100 
107  public static function isTalk( $index ) {
108  return $index > NS_MAIN
109  && $index % 2;
110  }
111 
118  public static function getTalk( $index ) {
119  self::isMethodValidFor( $index, __METHOD__ );
120  return self::isTalk( $index )
121  ? $index
122  : $index + 1;
123  }
124 
132  public static function getSubject( $index ) {
133  # Handle special namespaces
134  if ( $index < NS_MAIN ) {
135  return $index;
136  }
137 
138  return self::isTalk( $index )
139  ? $index - 1
140  : $index;
141  }
142 
151  public static function getAssociated( $index ) {
152  self::isMethodValidFor( $index, __METHOD__ );
153 
154  if ( self::isSubject( $index ) ) {
155  return self::getTalk( $index );
156  } elseif ( self::isTalk( $index ) ) {
157  return self::getSubject( $index );
158  } else {
159  return null;
160  }
161  }
162 
171  public static function exists( $index ) {
172  $nslist = self::getCanonicalNamespaces();
173  return isset( $nslist[$index] );
174  }
175 
190  public static function equals( $ns1, $ns2 ) {
191  return $ns1 == $ns2;
192  }
193 
205  public static function subjectEquals( $ns1, $ns2 ) {
206  return self::getSubject( $ns1 ) == self::getSubject( $ns2 );
207  }
208 
218  public static function getCanonicalNamespaces( $rebuild = false ) {
219  static $namespaces = null;
220  if ( $namespaces === null || $rebuild ) {
221  global $wgExtraNamespaces, $wgCanonicalNamespaceNames;
223  if ( is_array( $wgExtraNamespaces ) ) {
224  $namespaces += $wgExtraNamespaces;
225  }
226  wfRunHooks( 'CanonicalNamespaces', array( &$namespaces ) );
227  }
228  return $namespaces;
229  }
230 
237  public static function getCanonicalName( $index ) {
238  $nslist = self::getCanonicalNamespaces();
239  if ( isset( $nslist[$index] ) ) {
240  return $nslist[$index];
241  } else {
242  return false;
243  }
244  }
245 
253  public static function getCanonicalIndex( $name ) {
254  static $xNamespaces = false;
255  if ( $xNamespaces === false ) {
256  $xNamespaces = array();
257  foreach ( self::getCanonicalNamespaces() as $i => $text ) {
258  $xNamespaces[strtolower( $text )] = $i;
259  }
260  }
261  if ( array_key_exists( $name, $xNamespaces ) ) {
262  return $xNamespaces[$name];
263  } else {
264  return null;
265  }
266  }
267 
273  public static function getValidNamespaces() {
274  static $mValidNamespaces = null;
275 
276  if ( is_null( $mValidNamespaces ) ) {
277  foreach ( array_keys( self::getCanonicalNamespaces() ) as $ns ) {
278  if ( $ns >= 0 ) {
279  $mValidNamespaces[] = $ns;
280  }
281  }
282  }
283 
284  return $mValidNamespaces;
285  }
286 
293  public static function canTalk( $index ) {
294  return $index >= NS_MAIN;
295  }
296 
304  public static function isContent( $index ) {
305  global $wgContentNamespaces;
306  return $index == NS_MAIN || in_array( $index, $wgContentNamespaces );
307  }
308 
315  public static function isWatchable( $index ) {
316  return $index >= NS_MAIN;
317  }
318 
325  public static function hasSubpages( $index ) {
326  global $wgNamespacesWithSubpages;
327  return !empty( $wgNamespacesWithSubpages[$index] );
328  }
329 
334  public static function getContentNamespaces() {
335  global $wgContentNamespaces;
336  if ( !is_array( $wgContentNamespaces ) || $wgContentNamespaces === array() ) {
337  return array( NS_MAIN );
338  } elseif ( !in_array( NS_MAIN, $wgContentNamespaces ) ) {
339  // always force NS_MAIN to be part of array (to match the algorithm used by isContent)
340  return array_merge( array( NS_MAIN ), $wgContentNamespaces );
341  } else {
342  return $wgContentNamespaces;
343  }
344  }
345 
352  public static function getSubjectNamespaces() {
353  return array_filter(
355  'MWNamespace::isSubject'
356  );
357  }
358 
365  public static function getTalkNamespaces() {
366  return array_filter(
368  'MWNamespace::isTalk'
369  );
370  }
371 
378  public static function isCapitalized( $index ) {
379  global $wgCapitalLinks, $wgCapitalLinkOverrides;
380  // Turn NS_MEDIA into NS_FILE
381  $index = $index === NS_MEDIA ? NS_FILE : $index;
382 
383  // Make sure to get the subject of our namespace
384  $index = self::getSubject( $index );
385 
386  // Some namespaces are special and should always be upper case
387  if ( in_array( $index, self::$alwaysCapitalizedNamespaces ) ) {
388  return true;
389  }
390  if ( isset( $wgCapitalLinkOverrides[$index] ) ) {
391  // $wgCapitalLinkOverrides is explicitly set
392  return $wgCapitalLinkOverrides[$index];
393  }
394  // Default to the global setting
395  return $wgCapitalLinks;
396  }
397 
406  public static function hasGenderDistinction( $index ) {
407  return $index == NS_USER || $index == NS_USER_TALK;
408  }
409 
417  public static function isNonincludable( $index ) {
418  global $wgNonincludableNamespaces;
419  return $wgNonincludableNamespaces && in_array( $index, $wgNonincludableNamespaces );
420  }
421 
430  public static function getNamespaceContentModel( $index ) {
431  global $wgNamespaceContentModels;
432  return isset( $wgNamespaceContentModels[$index] )
433  ? $wgNamespaceContentModels[$index]
434  : null;
435  }
436 
446  public static function getRestrictionLevels( $index, User $user = null ) {
447  global $wgNamespaceProtection, $wgRestrictionLevels;
448 
449  if ( !isset( $wgNamespaceProtection[$index] ) ) {
450  // All levels are valid if there's no namespace restriction.
451  // But still filter by user, if necessary
452  $levels = $wgRestrictionLevels;
453  if ( $user ) {
454  $levels = array_values( array_filter( $levels, function ( $level ) use ( $user ) {
455  $right = $level;
456  if ( $right == 'sysop' ) {
457  $right = 'editprotected'; // BC
458  }
459  if ( $right == 'autoconfirmed' ) {
460  $right = 'editsemiprotected'; // BC
461  }
462  return ( $right == '' || $user->isAllowed( $right ) );
463  } ) );
464  }
465  return $levels;
466  }
467 
468  // First, get the list of groups that can edit this namespace.
469  $namespaceGroups = array();
470  $combine = 'array_merge';
471  foreach ( (array)$wgNamespaceProtection[$index] as $right ) {
472  if ( $right == 'sysop' ) {
473  $right = 'editprotected'; // BC
474  }
475  if ( $right == 'autoconfirmed' ) {
476  $right = 'editsemiprotected'; // BC
477  }
478  if ( $right != '' ) {
479  $namespaceGroups = call_user_func( $combine, $namespaceGroups,
481  $combine = 'array_intersect';
482  }
483  }
484 
485  // Now, keep only those restriction levels where there is at least one
486  // group that can edit the namespace but would be blocked by the
487  // restriction.
488  $usableLevels = array( '' );
489  foreach ( $wgRestrictionLevels as $level ) {
490  $right = $level;
491  if ( $right == 'sysop' ) {
492  $right = 'editprotected'; // BC
493  }
494  if ( $right == 'autoconfirmed' ) {
495  $right = 'editsemiprotected'; // BC
496  }
497  if ( $right != '' && ( !$user || $user->isAllowed( $right ) ) &&
498  array_diff( $namespaceGroups, User::getGroupsWithPermission( $right ) )
499  ) {
500  $usableLevels[] = $level;
501  }
502  }
503 
504  return $usableLevels;
505  }
506 }
$result
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message. Please note the header message cannot receive/use parameters. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item. $reader:XMLReader object $logInfo:Array of information Return false to stop further processing of the tag 'ImportHandlePageXMLTag':When parsing a XML tag in a page. $reader:XMLReader object $pageInfo:Array of information Return false to stop further processing of the tag 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision. $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information Return false to stop further processing of the tag 'ImportHandleToplevelXMLTag':When parsing a top level XML tag. $reader:XMLReader object Return false to stop further processing of the tag 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload. $reader:XMLReader object $revisionInfo:Array of information Return false to stop further processing of the tag 'InfoAction':When building information to display on the action=info page. $context:IContextSource object & $pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect. $title:Title object for the current page $request:WebRequest $ignoreRedirect:boolean to skip redirect check $target:Title/string of redirect target $article:Article object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not. Return true without providing an interwiki to continue interwiki search. $prefix:interwiki prefix we are looking for. & $iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InternalParseBeforeSanitize':during Parser 's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings. Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InternalParseBeforeLinks':during Parser 's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InvalidateEmailComplete':Called after a user 's email has been invalidated successfully. $user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification. Callee may modify $url and $query, URL will be constructed as $url . $query & $url:URL to index.php & $query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) $article:article(object) being checked 'IsTrustedProxy':Override the result of wfIsTrustedProxy() $ip:IP being check $result:Change this value to override the result of wfIsTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from & $allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of User::isValidEmailAddr(), for instance to return false if the domain name doesn 't match your organization. $addr:The e-mail address entered by the user & $result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user & $result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we 're looking for a messages file for & $file:The messages file path, you can override this to change the location. 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces. Do not use this hook to add namespaces. Use CanonicalNamespaces for that. & $namespaces:Array of namespaces indexed by their numbers 'LanguageGetMagic':DEPRECATED, use $magicWords in a file listed in $wgExtensionMessagesFiles instead. Use this to define synonyms of magic words depending of the language $magicExtensions:associative array of magic words synonyms $lang:language code(string) 'LanguageGetSpecialPageAliases':DEPRECATED, use $specialPageAliases in a file listed in $wgExtensionMessagesFiles instead. Use to define aliases of special pages names depending of the language $specialPageAliases:associative array of magic words synonyms $lang:language code(string) 'LanguageGetTranslatedLanguageNames':Provide translated language names. & $names:array of language code=> language name $code language of the preferred translations 'LanguageLinks':Manipulate a page 's language links. This is called in various places to allow extensions to define the effective language links for a page. $title:The page 's Title. & $links:Associative array mapping language codes to prefixed links of the form "language:title". & $linkFlags:Associative array mapping prefixed links to arrays of flags. Currently unused, but planned to provide support for marking individual language links in the UI, e.g. for featured articles. 'LinkBegin':Used when generating internal and interwiki links in Linker::link(), before processing starts. Return false to skip default processing and return $ret. See documentation for Linker::link() for details on the expected meanings of parameters. $skin:the Skin object $target:the Title that the link is pointing to & $html:the contents that the< a > tag should have(raw HTML) $result
Definition: hooks.txt:1528
MWNamespace\subjectEquals
static subjectEquals( $ns1, $ns2)
Returns whether the specified namespaces share the same subject.
Definition: Namespace.php:205
MWNamespace\isNonincludable
static isNonincludable( $index)
It is not possible to use pages from this namespace as template?
Definition: Namespace.php:417
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
MWNamespace\getValidNamespaces
static getValidNamespaces()
Returns an array of the namespaces (by integer id) that exist on the wiki.
Definition: Namespace.php:273
MWNamespace\hasGenderDistinction
static hasGenderDistinction( $index)
Does the namespace (potentially) have different aliases for different genders.
Definition: Namespace.php:406
MWNamespace\isTalk
static isTalk( $index)
Is the given namespace a talk namespace?
Definition: Namespace.php:107
MWNamespace\getTalkNamespaces
static getTalkNamespaces()
List all namespace indices which are considered talks, aka not a subject or special namespace.
Definition: Namespace.php:365
MWNamespace\getNamespaceContentModel
static getNamespaceContentModel( $index)
Get the default content model for a namespace This does not mean that all pages in that namespace hav...
Definition: Namespace.php:430
$right
return false if a UserGetRights hook might remove the named right $right
Definition: hooks.txt:2798
NS_FILE
const NS_FILE
Definition: Defines.php:85
MWNamespace\getSubjectNamespaces
static getSubjectNamespaces()
List all namespace indices which are considered subject, aka not a talk or special namespace.
Definition: Namespace.php:352
MWNamespace\getContentNamespaces
static getContentNamespaces()
Get a list of all namespace indices which are considered to contain content.
Definition: Namespace.php:334
MWNamespace\isMain
static isMain( $index)
Definition: Namespace.php:96
$wgCanonicalNamespaceNames
if( $wgMetaNamespace===false) if( $wgResourceLoaderMaxQueryLength===false) $wgCanonicalNamespaceNames
Definitions of the NS_ constants are in Defines.php.
Definition: Setup.php:304
NS_MAIN
const NS_MAIN
Definition: Defines.php:79
MWNamespace\getCanonicalIndex
static getCanonicalIndex( $name)
Returns the index for a given canonical name, or NULL The input must be converted to lower case first...
Definition: Namespace.php:253
NS_SPECIAL
const NS_SPECIAL
Definition: Defines.php:68
MWException
MediaWiki exception.
Definition: MWException.php:26
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Definition: GlobalFunctions.php:1127
MWNamespace\canTalk
static canTalk( $index)
Can this namespace ever have a talk namespace?
Definition: Namespace.php:293
MWNamespace\isContent
static isContent( $index)
Does this namespace contain content, for the purposes of calculating statistics, etc?
Definition: Namespace.php:304
wfRunHooks
wfRunHooks( $event, array $args=array(), $deprecatedVersion=null)
Call hook functions defined in $wgHooks.
Definition: GlobalFunctions.php:4001
MWNamespace\hasSubpages
static hasSubpages( $index)
Does the namespace allow subpages?
Definition: Namespace.php:325
MWNamespace
This is a utility class with only static functions for dealing with namespaces that encodes all the "...
Definition: Namespace.php:33
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
MWNamespace\isMovable
static isMovable( $index)
Can pages in the given namespace be moved?
Definition: Namespace.php:67
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:93
MWNamespace\getRestrictionLevels
static getRestrictionLevels( $index, User $user=null)
Determine which restriction levels it makes sense to use in a namespace, optionally filtered by a use...
Definition: Namespace.php:446
MWNamespace\isSubject
static isSubject( $index)
Is the given namespace is a subject (non-talk) namespace?
Definition: Namespace.php:87
NS_USER_TALK
const NS_USER_TALK
Definition: Defines.php:82
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:336
NS_MEDIA
const NS_MEDIA
Definition: Defines.php:67
MWNamespace\exists
static exists( $index)
Returns whether the specified namespace exists.
Definition: Namespace.php:171
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:237
$namespaces
namespace and then decline to actually register it & $namespaces
Definition: hooks.txt:815
MWNamespace\equals
static equals( $ns1, $ns2)
Returns whether the specified namespaces are the same namespace.
Definition: Namespace.php:190
MWNamespace\isMethodValidFor
static isMethodValidFor( $index, $method)
Throw an exception when trying to get the subject or talk page for a given namespace where it does no...
Definition: Namespace.php:54
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
MWNamespace\getCanonicalNamespaces
static getCanonicalNamespaces( $rebuild=false)
Returns array of all defined namespaces with their canonical (English) names.
Definition: Namespace.php:218
MWNamespace\isCapitalized
static isCapitalized( $index)
Is the namespace first-letter capitalized?
Definition: Namespace.php:378
MWNamespace\$alwaysCapitalizedNamespaces
static $alwaysCapitalizedNamespaces
These namespaces should always be first-letter capitalized, now and forevermore.
Definition: Namespace.php:40
NS_USER
const NS_USER
Definition: Defines.php:81
$wgNamespaceProtection
if(!isset( $wgVersion)) if( $wgScript===false) if( $wgLoadScript===false) if( $wgArticlePath===false) if(!empty( $wgActionPaths) &&!isset( $wgActionPaths['view'])) if( $wgStylePath===false) if( $wgLocalStylePath===false) if( $wgStyleDirectory===false) if( $wgExtensionAssetsPath===false) if( $wgLogo===false) if( $wgUploadPath===false) if( $wgUploadDirectory===false) if( $wgReadOnlyFile===false) if( $wgFileCacheDirectory===false) if( $wgDeletedDirectory===false) if(isset( $wgFileStore['deleted']['directory'])) if(isset( $wgFooterIcons['copyright']) &&isset( $wgFooterIcons['copyright']['copyright']) && $wgFooterIcons['copyright']['copyright']===array()) if(isset( $wgFooterIcons['poweredby']) &&isset( $wgFooterIcons['poweredby']['mediawiki']) && $wgFooterIcons['poweredby']['mediawiki']['src']===null) $wgNamespaceProtection[NS_MEDIAWIKI]
Unconditional protection for NS_MEDIAWIKI since otherwise it's too easy for a sysadmin to set $wgName...
Definition: Setup.php:135
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition: Defines.php:87
MWNamespace\isWatchable
static isWatchable( $index)
Can pages in a namespace be watched?
Definition: Namespace.php:315
MWNamespace\getTalk
static getTalk( $index)
Get the talk namespace index for a given namespace.
Definition: Namespace.php:118
MWNamespace\getSubject
static getSubject( $index)
Get the subject namespace index for a given namespace Special namespaces (NS_MEDIA,...
Definition: Namespace.php:132
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:59
MWNamespace\getAssociated
static getAssociated( $index)
Get the associated namespace.
Definition: Namespace.php:151
MWNamespace\getCanonicalName
static getCanonicalName( $index)
Returns the canonical (English) name for a given index.
Definition: Namespace.php:237
User\getGroupsWithPermission
static getGroupsWithPermission( $role)
Get all the groups who have a given permission.
Definition: User.php:4123