Rename autonym for 'no' from 'norsk bokmål' to 'norsk'
[lhc/web/wiklou.git] / includes / MWNamespace.php
1 <?php
2 /**
3 * Provide things related to namespaces.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
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.
28 *
29 * These are synonyms for the names given in the language file
30 * Users and translators should not change them
31 */
32 class MWNamespace {
33
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. :)
38 */
39 private static $alwaysCapitalizedNamespaces = [ NS_SPECIAL, NS_USER, NS_MEDIAWIKI ];
40
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)
46 *
47 * @param int $index
48 * @param string $method
49 *
50 * @throws MWException
51 * @return bool
52 */
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" );
56 }
57 return true;
58 }
59
60 /**
61 * Can pages in the given namespace be moved?
62 *
63 * @param int $index Namespace index
64 * @return bool
65 */
66 public static function isMovable( $index ) {
67 global $wgAllowImageMoving;
68
69 $result = !( $index < NS_MAIN || ( $index == NS_FILE && !$wgAllowImageMoving ) );
70
71 /**
72 * @since 1.20
73 */
74 Hooks::run( 'NamespaceIsMovable', [ $index, &$result ] );
75
76 return $result;
77 }
78
79 /**
80 * Is the given namespace is a subject (non-talk) namespace?
81 *
82 * @param int $index Namespace index
83 * @return bool
84 * @since 1.19
85 */
86 public static function isSubject( $index ) {
87 return !self::isTalk( $index );
88 }
89
90 /**
91 * Is the given namespace a talk namespace?
92 *
93 * @param int $index Namespace index
94 * @return bool
95 */
96 public static function isTalk( $index ) {
97 return $index > NS_MAIN
98 && $index % 2;
99 }
100
101 /**
102 * Get the talk namespace index for a given namespace
103 *
104 * @param int $index Namespace index
105 * @return int
106 */
107 public static function getTalk( $index ) {
108 self::isMethodValidFor( $index, __METHOD__ );
109 return self::isTalk( $index )
110 ? $index
111 : $index + 1;
112 }
113
114 /**
115 * Get the subject namespace index for a given namespace
116 * Special namespaces (NS_MEDIA, NS_SPECIAL) are always the subject.
117 *
118 * @param int $index Namespace index
119 * @return int
120 */
121 public static function getSubject( $index ) {
122 # Handle special namespaces
123 if ( $index < NS_MAIN ) {
124 return $index;
125 }
126
127 return self::isTalk( $index )
128 ? $index - 1
129 : $index;
130 }
131
132 /**
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
136 *
137 * @param int $index Namespace index
138 * @return int|null If no associated namespace could be found
139 */
140 public static function getAssociated( $index ) {
141 self::isMethodValidFor( $index, __METHOD__ );
142
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;
149 }
150 }
151
152 /**
153 * Returns whether the specified namespace exists
154 *
155 * @param int $index
156 *
157 * @return bool
158 * @since 1.19
159 */
160 public static function exists( $index ) {
161 $nslist = self::getCanonicalNamespaces();
162 return isset( $nslist[$index] );
163 }
164
165 /**
166 * Returns whether the specified namespaces are the same namespace
167 *
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.
172 *
173 * @param int $ns1 The first namespace index
174 * @param int $ns2 The second namespace index
175 *
176 * @return bool
177 * @since 1.19
178 */
179 public static function equals( $ns1, $ns2 ) {
180 return $ns1 == $ns2;
181 }
182
183 /**
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.
187 *
188 * @param int $ns1 The first namespace index
189 * @param int $ns2 The second namespace index
190 *
191 * @return bool
192 * @since 1.19
193 */
194 public static function subjectEquals( $ns1, $ns2 ) {
195 return self::getSubject( $ns1 ) == self::getSubject( $ns2 );
196 }
197
198 /**
199 * Returns array of all defined namespaces with their canonical
200 * (English) names.
201 *
202 * @param bool $rebuild Rebuild namespace list (default = false). Used for testing.
203 *
204 * @return array
205 * @since 1.17
206 */
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;
216 }
217 Hooks::run( 'CanonicalNamespaces', [ &$namespaces ] );
218 }
219 return $namespaces;
220 }
221
222 /**
223 * Returns the canonical (English) name for a given index
224 *
225 * @param int $index Namespace index
226 * @return string|bool If no canonical definition.
227 */
228 public static function getCanonicalName( $index ) {
229 $nslist = self::getCanonicalNamespaces();
230 if ( isset( $nslist[$index] ) ) {
231 return $nslist[$index];
232 } else {
233 return false;
234 }
235 }
236
237 /**
238 * Returns the index for a given canonical name, or NULL
239 * The input *must* be converted to lower case first
240 *
241 * @param string $name Namespace name
242 * @return int
243 */
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;
250 }
251 }
252 if ( array_key_exists( $name, $xNamespaces ) ) {
253 return $xNamespaces[$name];
254 } else {
255 return null;
256 }
257 }
258
259 /**
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
263 */
264 public static function getValidNamespaces() {
265 static $mValidNamespaces = null;
266
267 if ( is_null( $mValidNamespaces ) ) {
268 foreach ( array_keys( self::getCanonicalNamespaces() ) as $ns ) {
269 if ( $ns >= 0 ) {
270 $mValidNamespaces[] = $ns;
271 }
272 }
273 // T109137: sort numerically
274 sort( $mValidNamespaces, SORT_NUMERIC );
275 }
276
277 return $mValidNamespaces;
278 }
279
280 /**
281 * Can this namespace ever have a talk namespace?
282 *
283 * @param int $index Namespace index
284 * @return bool
285 */
286 public static function canTalk( $index ) {
287 return $index >= NS_MAIN;
288 }
289
290 /**
291 * Does this namespace contain content, for the purposes of calculating
292 * statistics, etc?
293 *
294 * @param int $index Index to check
295 * @return bool
296 */
297 public static function isContent( $index ) {
298 global $wgContentNamespaces;
299 return $index == NS_MAIN || in_array( $index, $wgContentNamespaces );
300 }
301
302 /**
303 * Might pages in this namespace require the use of the Signature button on
304 * the edit toolbar?
305 *
306 * @param int $index Index to check
307 * @return bool
308 */
309 public static function wantSignatures( $index ) {
310 global $wgExtraSignatureNamespaces;
311 return self::isTalk( $index ) || in_array( $index, $wgExtraSignatureNamespaces );
312 }
313
314 /**
315 * Can pages in a namespace be watched?
316 *
317 * @param int $index
318 * @return bool
319 */
320 public static function isWatchable( $index ) {
321 return $index >= NS_MAIN;
322 }
323
324 /**
325 * Does the namespace allow subpages?
326 *
327 * @param int $index Index to check
328 * @return bool
329 */
330 public static function hasSubpages( $index ) {
331 global $wgNamespacesWithSubpages;
332 return !empty( $wgNamespacesWithSubpages[$index] );
333 }
334
335 /**
336 * Get a list of all namespace indices which are considered to contain content
337 * @return array Array of namespace indices
338 */
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;
348 }
349 }
350
351 /**
352 * List all namespace indices which are considered subject, aka not a talk
353 * or special namespace. See also MWNamespace::isSubject
354 *
355 * @return array Array of namespace indices
356 */
357 public static function getSubjectNamespaces() {
358 return array_filter(
359 MWNamespace::getValidNamespaces(),
360 'MWNamespace::isSubject'
361 );
362 }
363
364 /**
365 * List all namespace indices which are considered talks, aka not a subject
366 * or special namespace. See also MWNamespace::isTalk
367 *
368 * @return array Array of namespace indices
369 */
370 public static function getTalkNamespaces() {
371 return array_filter(
372 MWNamespace::getValidNamespaces(),
373 'MWNamespace::isTalk'
374 );
375 }
376
377 /**
378 * Is the namespace first-letter capitalized?
379 *
380 * @param int $index Index to check
381 * @return bool
382 */
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;
387
388 // Make sure to get the subject of our namespace
389 $index = self::getSubject( $index );
390
391 // Some namespaces are special and should always be upper case
392 if ( in_array( $index, self::$alwaysCapitalizedNamespaces ) ) {
393 return true;
394 }
395 if ( isset( $wgCapitalLinkOverrides[$index] ) ) {
396 // $wgCapitalLinkOverrides is explicitly set
397 return $wgCapitalLinkOverrides[$index];
398 }
399 // Default to the global setting
400 return $wgCapitalLinks;
401 }
402
403 /**
404 * Does the namespace (potentially) have different aliases for different
405 * genders. Not all languages make a distinction here.
406 *
407 * @since 1.18
408 * @param int $index Index to check
409 * @return bool
410 */
411 public static function hasGenderDistinction( $index ) {
412 return $index == NS_USER || $index == NS_USER_TALK;
413 }
414
415 /**
416 * It is not possible to use pages from this namespace as template?
417 *
418 * @since 1.20
419 * @param int $index Index to check
420 * @return bool
421 */
422 public static function isNonincludable( $index ) {
423 global $wgNonincludableNamespaces;
424 return $wgNonincludableNamespaces && in_array( $index, $wgNonincludableNamespaces );
425 }
426
427 /**
428 * Get the default content model for a namespace
429 * This does not mean that all pages in that namespace have the model
430 *
431 * @since 1.21
432 * @param int $index Index to check
433 * @return null|string Default model name for the given namespace, if set
434 */
435 public static function getNamespaceContentModel( $index ) {
436 global $wgNamespaceContentModels;
437 return isset( $wgNamespaceContentModels[$index] )
438 ? $wgNamespaceContentModels[$index]
439 : null;
440 }
441
442 /**
443 * Determine which restriction levels it makes sense to use in a namespace,
444 * optionally filtered by a user's rights.
445 *
446 * @since 1.23
447 * @param int $index Index to check
448 * @param User $user User to check
449 * @return array
450 */
451 public static function getRestrictionLevels( $index, User $user = null ) {
452 global $wgNamespaceProtection, $wgRestrictionLevels;
453
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
463 }
464 if ( $right == 'autoconfirmed' ) {
465 $right = 'editsemiprotected'; // BC
466 }
467 return ( $right == '' || $user->isAllowed( $right ) );
468 } ) );
469 }
470 return $levels;
471 }
472
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
479 }
480 if ( $right == 'autoconfirmed' ) {
481 $right = 'editsemiprotected'; // BC
482 }
483 if ( $right != '' ) {
484 $namespaceGroups = call_user_func( $combine, $namespaceGroups,
485 User::getGroupsWithPermission( $right ) );
486 $combine = 'array_intersect';
487 }
488 }
489
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
498 }
499 if ( $right == 'autoconfirmed' ) {
500 $right = 'editsemiprotected'; // BC
501 }
502 if ( $right != '' && ( !$user || $user->isAllowed( $right ) ) &&
503 array_diff( $namespaceGroups, User::getGroupsWithPermission( $right ) )
504 ) {
505 $usableLevels[] = $level;
506 }
507 }
508
509 return $usableLevels;
510 }
511 }