Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / MWNamespace.php
blob4c5561fba871681692ccc96612343e6689a45c7d
1 <?php
2 /**
3 * Provide things related to namespaces.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 /**
24 * This is a utility class with only static functions
25 * for dealing with namespaces that encodes all the
26 * "magic" behaviors of them based on index. The textual
27 * names of the namespaces are handled by Language.php.
29 * These are synonyms for the names given in the language file
30 * Users and translators should not change them
32 class MWNamespace {
34 /**
35 * These namespaces should always be first-letter capitalized, now and
36 * forevermore. Historically, they could've probably been lowercased too,
37 * but some things are just too ingrained now. :)
39 private static $alwaysCapitalizedNamespaces = [ NS_SPECIAL, NS_USER, NS_MEDIAWIKI ];
41 /**
42 * Throw an exception when trying to get the subject or talk page
43 * for a given namespace where it does not make sense.
44 * Special namespaces are defined in includes/Defines.php and have
45 * a value below 0 (ex: NS_SPECIAL = -1 , NS_MEDIA = -2)
47 * @param int $index
48 * @param string $method
50 * @throws MWException
51 * @return bool
53 private static function isMethodValidFor( $index, $method ) {
54 if ( $index < NS_MAIN ) {
55 throw new MWException( "$method does not make any sense for given namespace $index" );
57 return true;
60 /**
61 * Can pages in the given namespace be moved?
63 * @param int $index Namespace index
64 * @return bool
66 public static function isMovable( $index ) {
67 global $wgAllowImageMoving;
69 $result = !( $index < NS_MAIN || ( $index == NS_FILE && !$wgAllowImageMoving ) );
71 /**
72 * @since 1.20
74 Hooks::run( 'NamespaceIsMovable', [ $index, &$result ] );
76 return $result;
79 /**
80 * Is the given namespace is a subject (non-talk) namespace?
82 * @param int $index Namespace index
83 * @return bool
84 * @since 1.19
86 public static function isSubject( $index ) {
87 return !self::isTalk( $index );
90 /**
91 * Is the given namespace a talk namespace?
93 * @param int $index Namespace index
94 * @return bool
96 public static function isTalk( $index ) {
97 return $index > NS_MAIN
98 && $index % 2;
102 * Get the talk namespace index for a given namespace
104 * @param int $index Namespace index
105 * @return int
107 public static function getTalk( $index ) {
108 self::isMethodValidFor( $index, __METHOD__ );
109 return self::isTalk( $index )
110 ? $index
111 : $index + 1;
115 * Get the subject namespace index for a given namespace
116 * Special namespaces (NS_MEDIA, NS_SPECIAL) are always the subject.
118 * @param int $index Namespace index
119 * @return int
121 public static function getSubject( $index ) {
122 # Handle special namespaces
123 if ( $index < NS_MAIN ) {
124 return $index;
127 return self::isTalk( $index )
128 ? $index - 1
129 : $index;
133 * Get the associated namespace.
134 * For talk namespaces, returns the subject (non-talk) namespace
135 * For subject (non-talk) namespaces, returns the talk namespace
137 * @param int $index Namespace index
138 * @return int|null If no associated namespace could be found
140 public static function getAssociated( $index ) {
141 self::isMethodValidFor( $index, __METHOD__ );
143 if ( self::isSubject( $index ) ) {
144 return self::getTalk( $index );
145 } elseif ( self::isTalk( $index ) ) {
146 return self::getSubject( $index );
147 } else {
148 return null;
153 * Returns whether the specified namespace exists
155 * @param int $index
157 * @return bool
158 * @since 1.19
160 public static function exists( $index ) {
161 $nslist = self::getCanonicalNamespaces();
162 return isset( $nslist[$index] );
166 * Returns whether the specified namespaces are the same namespace
168 * @note It's possible that in the future we may start using something
169 * other than just namespace indexes. Under that circumstance making use
170 * of this function rather than directly doing comparison will make
171 * sure that code will not potentially break.
173 * @param int $ns1 The first namespace index
174 * @param int $ns2 The second namespace index
176 * @return bool
177 * @since 1.19
179 public static function equals( $ns1, $ns2 ) {
180 return $ns1 == $ns2;
184 * Returns whether the specified namespaces share the same subject.
185 * eg: NS_USER and NS_USER wil return true, as well
186 * NS_USER and NS_USER_TALK will return true.
188 * @param int $ns1 The first namespace index
189 * @param int $ns2 The second namespace index
191 * @return bool
192 * @since 1.19
194 public static function subjectEquals( $ns1, $ns2 ) {
195 return self::getSubject( $ns1 ) == self::getSubject( $ns2 );
199 * Returns array of all defined namespaces with their canonical
200 * (English) names.
202 * @param bool $rebuild Rebuild namespace list (default = false). Used for testing.
204 * @return array
205 * @since 1.17
207 public static function getCanonicalNamespaces( $rebuild = false ) {
208 static $namespaces = null;
209 if ( $namespaces === null || $rebuild ) {
210 global $wgExtraNamespaces, $wgCanonicalNamespaceNames;
211 $namespaces = [ NS_MAIN => '' ] + $wgCanonicalNamespaceNames;
212 // Add extension namespaces
213 $namespaces += ExtensionRegistry::getInstance()->getAttribute( 'ExtensionNamespaces' );
214 if ( is_array( $wgExtraNamespaces ) ) {
215 $namespaces += $wgExtraNamespaces;
217 Hooks::run( 'CanonicalNamespaces', [ &$namespaces ] );
219 return $namespaces;
223 * Returns the canonical (English) name for a given index
225 * @param int $index Namespace index
226 * @return string|bool If no canonical definition.
228 public static function getCanonicalName( $index ) {
229 $nslist = self::getCanonicalNamespaces();
230 if ( isset( $nslist[$index] ) ) {
231 return $nslist[$index];
232 } else {
233 return false;
238 * Returns the index for a given canonical name, or NULL
239 * The input *must* be converted to lower case first
241 * @param string $name Namespace name
242 * @return int
244 public static function getCanonicalIndex( $name ) {
245 static $xNamespaces = false;
246 if ( $xNamespaces === false ) {
247 $xNamespaces = [];
248 foreach ( self::getCanonicalNamespaces() as $i => $text ) {
249 $xNamespaces[strtolower( $text )] = $i;
252 if ( array_key_exists( $name, $xNamespaces ) ) {
253 return $xNamespaces[$name];
254 } else {
255 return null;
260 * Returns an array of the namespaces (by integer id) that exist on the
261 * wiki. Used primarily by the api in help documentation.
262 * @return array
264 public static function getValidNamespaces() {
265 static $mValidNamespaces = null;
267 if ( is_null( $mValidNamespaces ) ) {
268 foreach ( array_keys( self::getCanonicalNamespaces() ) as $ns ) {
269 if ( $ns >= 0 ) {
270 $mValidNamespaces[] = $ns;
273 // T109137: sort numerically
274 sort( $mValidNamespaces, SORT_NUMERIC );
277 return $mValidNamespaces;
281 * Can this namespace ever have a talk namespace?
283 * @param int $index Namespace index
284 * @return bool
286 public static function canTalk( $index ) {
287 return $index >= NS_MAIN;
291 * Does this namespace contain content, for the purposes of calculating
292 * statistics, etc?
294 * @param int $index Index to check
295 * @return bool
297 public static function isContent( $index ) {
298 global $wgContentNamespaces;
299 return $index == NS_MAIN || in_array( $index, $wgContentNamespaces );
303 * Might pages in this namespace require the use of the Signature button on
304 * the edit toolbar?
306 * @param int $index Index to check
307 * @return bool
309 public static function wantSignatures( $index ) {
310 global $wgExtraSignatureNamespaces;
311 return self::isTalk( $index ) || in_array( $index, $wgExtraSignatureNamespaces );
315 * Can pages in a namespace be watched?
317 * @param int $index
318 * @return bool
320 public static function isWatchable( $index ) {
321 return $index >= NS_MAIN;
325 * Does the namespace allow subpages?
327 * @param int $index Index to check
328 * @return bool
330 public static function hasSubpages( $index ) {
331 global $wgNamespacesWithSubpages;
332 return !empty( $wgNamespacesWithSubpages[$index] );
336 * Get a list of all namespace indices which are considered to contain content
337 * @return array Array of namespace indices
339 public static function getContentNamespaces() {
340 global $wgContentNamespaces;
341 if ( !is_array( $wgContentNamespaces ) || $wgContentNamespaces === [] ) {
342 return [ NS_MAIN ];
343 } elseif ( !in_array( NS_MAIN, $wgContentNamespaces ) ) {
344 // always force NS_MAIN to be part of array (to match the algorithm used by isContent)
345 return array_merge( [ NS_MAIN ], $wgContentNamespaces );
346 } else {
347 return $wgContentNamespaces;
352 * List all namespace indices which are considered subject, aka not a talk
353 * or special namespace. See also MWNamespace::isSubject
355 * @return array Array of namespace indices
357 public static function getSubjectNamespaces() {
358 return array_filter(
359 MWNamespace::getValidNamespaces(),
360 'MWNamespace::isSubject'
365 * List all namespace indices which are considered talks, aka not a subject
366 * or special namespace. See also MWNamespace::isTalk
368 * @return array Array of namespace indices
370 public static function getTalkNamespaces() {
371 return array_filter(
372 MWNamespace::getValidNamespaces(),
373 'MWNamespace::isTalk'
378 * Is the namespace first-letter capitalized?
380 * @param int $index Index to check
381 * @return bool
383 public static function isCapitalized( $index ) {
384 global $wgCapitalLinks, $wgCapitalLinkOverrides;
385 // Turn NS_MEDIA into NS_FILE
386 $index = $index === NS_MEDIA ? NS_FILE : $index;
388 // Make sure to get the subject of our namespace
389 $index = self::getSubject( $index );
391 // Some namespaces are special and should always be upper case
392 if ( in_array( $index, self::$alwaysCapitalizedNamespaces ) ) {
393 return true;
395 if ( isset( $wgCapitalLinkOverrides[$index] ) ) {
396 // $wgCapitalLinkOverrides is explicitly set
397 return $wgCapitalLinkOverrides[$index];
399 // Default to the global setting
400 return $wgCapitalLinks;
404 * Does the namespace (potentially) have different aliases for different
405 * genders. Not all languages make a distinction here.
407 * @since 1.18
408 * @param int $index Index to check
409 * @return bool
411 public static function hasGenderDistinction( $index ) {
412 return $index == NS_USER || $index == NS_USER_TALK;
416 * It is not possible to use pages from this namespace as template?
418 * @since 1.20
419 * @param int $index Index to check
420 * @return bool
422 public static function isNonincludable( $index ) {
423 global $wgNonincludableNamespaces;
424 return $wgNonincludableNamespaces && in_array( $index, $wgNonincludableNamespaces );
428 * Get the default content model for a namespace
429 * This does not mean that all pages in that namespace have the model
431 * @since 1.21
432 * @param int $index Index to check
433 * @return null|string Default model name for the given namespace, if set
435 public static function getNamespaceContentModel( $index ) {
436 global $wgNamespaceContentModels;
437 return isset( $wgNamespaceContentModels[$index] )
438 ? $wgNamespaceContentModels[$index]
439 : null;
443 * Determine which restriction levels it makes sense to use in a namespace,
444 * optionally filtered by a user's rights.
446 * @since 1.23
447 * @param int $index Index to check
448 * @param User $user User to check
449 * @return array
451 public static function getRestrictionLevels( $index, User $user = null ) {
452 global $wgNamespaceProtection, $wgRestrictionLevels;
454 if ( !isset( $wgNamespaceProtection[$index] ) ) {
455 // All levels are valid if there's no namespace restriction.
456 // But still filter by user, if necessary
457 $levels = $wgRestrictionLevels;
458 if ( $user ) {
459 $levels = array_values( array_filter( $levels, function ( $level ) use ( $user ) {
460 $right = $level;
461 if ( $right == 'sysop' ) {
462 $right = 'editprotected'; // BC
464 if ( $right == 'autoconfirmed' ) {
465 $right = 'editsemiprotected'; // BC
467 return ( $right == '' || $user->isAllowed( $right ) );
468 } ) );
470 return $levels;
473 // First, get the list of groups that can edit this namespace.
474 $namespaceGroups = [];
475 $combine = 'array_merge';
476 foreach ( (array)$wgNamespaceProtection[$index] as $right ) {
477 if ( $right == 'sysop' ) {
478 $right = 'editprotected'; // BC
480 if ( $right == 'autoconfirmed' ) {
481 $right = 'editsemiprotected'; // BC
483 if ( $right != '' ) {
484 $namespaceGroups = call_user_func( $combine, $namespaceGroups,
485 User::getGroupsWithPermission( $right ) );
486 $combine = 'array_intersect';
490 // Now, keep only those restriction levels where there is at least one
491 // group that can edit the namespace but would be blocked by the
492 // restriction.
493 $usableLevels = [ '' ];
494 foreach ( $wgRestrictionLevels as $level ) {
495 $right = $level;
496 if ( $right == 'sysop' ) {
497 $right = 'editprotected'; // BC
499 if ( $right == 'autoconfirmed' ) {
500 $right = 'editsemiprotected'; // BC
502 if ( $right != '' && ( !$user || $user->isAllowed( $right ) ) &&
503 array_diff( $namespaceGroups, User::getGroupsWithPermission( $right ) )
505 $usableLevels[] = $level;
509 return $usableLevels;