GitInfo: Don't try shelling out if it's disabled
[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 /** @var string[]|null Canonical namespaces cache */
42 private static $canonicalNamespaces = null;
43
44 /** @var array|false Canonical namespaces index cache */
45 private static $namespaceIndexes = false;
46
47 /** @var int[]|null Valid namespaces cache */
48 private static $validNamespaces = null;
49
50 /**
51 * Throw an exception when trying to get the subject or talk page
52 * for a given namespace where it does not make sense.
53 * Special namespaces are defined in includes/Defines.php and have
54 * a value below 0 (ex: NS_SPECIAL = -1 , NS_MEDIA = -2)
55 *
56 * @param int $index
57 * @param string $method
58 *
59 * @throws MWException
60 * @return bool
61 */
62 private static function isMethodValidFor( $index, $method ) {
63 if ( $index < NS_MAIN ) {
64 throw new MWException( "$method does not make any sense for given namespace $index" );
65 }
66 return true;
67 }
68
69 /**
70 * Clear internal caches
71 *
72 * For use in unit testing when namespace configuration is changed.
73 *
74 * @since 1.31
75 */
76 public static function clearCaches() {
77 self::$canonicalNamespaces = null;
78 self::$namespaceIndexes = false;
79 self::$validNamespaces = null;
80 }
81
82 /**
83 * Can pages in the given namespace be moved?
84 *
85 * @param int $index Namespace index
86 * @return bool
87 */
88 public static function isMovable( $index ) {
89 global $wgAllowImageMoving;
90
91 $result = !( $index < NS_MAIN || ( $index == NS_FILE && !$wgAllowImageMoving ) );
92
93 /**
94 * @since 1.20
95 */
96 Hooks::run( 'NamespaceIsMovable', [ $index, &$result ] );
97
98 return $result;
99 }
100
101 /**
102 * Is the given namespace is a subject (non-talk) namespace?
103 *
104 * @param int $index Namespace index
105 * @return bool
106 * @since 1.19
107 */
108 public static function isSubject( $index ) {
109 return !self::isTalk( $index );
110 }
111
112 /**
113 * Is the given namespace a talk namespace?
114 *
115 * @param int $index Namespace index
116 * @return bool
117 */
118 public static function isTalk( $index ) {
119 return $index > NS_MAIN
120 && $index % 2;
121 }
122
123 /**
124 * Get the talk namespace index for a given namespace
125 *
126 * @param int $index Namespace index
127 * @return int
128 */
129 public static function getTalk( $index ) {
130 self::isMethodValidFor( $index, __METHOD__ );
131 return self::isTalk( $index )
132 ? $index
133 : $index + 1;
134 }
135
136 /**
137 * Get the subject namespace index for a given namespace
138 * Special namespaces (NS_MEDIA, NS_SPECIAL) are always the subject.
139 *
140 * @param int $index Namespace index
141 * @return int
142 */
143 public static function getSubject( $index ) {
144 # Handle special namespaces
145 if ( $index < NS_MAIN ) {
146 return $index;
147 }
148
149 return self::isTalk( $index )
150 ? $index - 1
151 : $index;
152 }
153
154 /**
155 * Get the associated namespace.
156 * For talk namespaces, returns the subject (non-talk) namespace
157 * For subject (non-talk) namespaces, returns the talk namespace
158 *
159 * @param int $index Namespace index
160 * @return int|null If no associated namespace could be found
161 */
162 public static function getAssociated( $index ) {
163 self::isMethodValidFor( $index, __METHOD__ );
164
165 if ( self::isSubject( $index ) ) {
166 return self::getTalk( $index );
167 } elseif ( self::isTalk( $index ) ) {
168 return self::getSubject( $index );
169 } else {
170 return null;
171 }
172 }
173
174 /**
175 * Returns whether the specified namespace exists
176 *
177 * @param int $index
178 *
179 * @return bool
180 * @since 1.19
181 */
182 public static function exists( $index ) {
183 $nslist = self::getCanonicalNamespaces();
184 return isset( $nslist[$index] );
185 }
186
187 /**
188 * Returns whether the specified namespaces are the same namespace
189 *
190 * @note It's possible that in the future we may start using something
191 * other than just namespace indexes. Under that circumstance making use
192 * of this function rather than directly doing comparison will make
193 * sure that code will not potentially break.
194 *
195 * @param int $ns1 The first namespace index
196 * @param int $ns2 The second namespace index
197 *
198 * @return bool
199 * @since 1.19
200 */
201 public static function equals( $ns1, $ns2 ) {
202 return $ns1 == $ns2;
203 }
204
205 /**
206 * Returns whether the specified namespaces share the same subject.
207 * eg: NS_USER and NS_USER wil return true, as well
208 * NS_USER and NS_USER_TALK will return true.
209 *
210 * @param int $ns1 The first namespace index
211 * @param int $ns2 The second namespace index
212 *
213 * @return bool
214 * @since 1.19
215 */
216 public static function subjectEquals( $ns1, $ns2 ) {
217 return self::getSubject( $ns1 ) == self::getSubject( $ns2 );
218 }
219
220 /**
221 * Returns array of all defined namespaces with their canonical
222 * (English) names.
223 *
224 * @param bool $rebuild Rebuild namespace list (default = false). Used for testing.
225 * Deprecated since 1.31, use self::clearCaches() instead.
226 *
227 * @return array
228 * @since 1.17
229 */
230 public static function getCanonicalNamespaces( $rebuild = false ) {
231 if ( $rebuild ) {
232 self::clearCaches();
233 }
234
235 if ( self::$canonicalNamespaces === null ) {
236 global $wgExtraNamespaces, $wgCanonicalNamespaceNames;
237 self::$canonicalNamespaces = [ NS_MAIN => '' ] + $wgCanonicalNamespaceNames;
238 // Add extension namespaces
239 self::$canonicalNamespaces +=
240 ExtensionRegistry::getInstance()->getAttribute( 'ExtensionNamespaces' );
241 if ( is_array( $wgExtraNamespaces ) ) {
242 self::$canonicalNamespaces += $wgExtraNamespaces;
243 }
244 Hooks::run( 'CanonicalNamespaces', [ &self::$canonicalNamespaces ] );
245 }
246 return self::$canonicalNamespaces;
247 }
248
249 /**
250 * Returns the canonical (English) name for a given index
251 *
252 * @param int $index Namespace index
253 * @return string|bool If no canonical definition.
254 */
255 public static function getCanonicalName( $index ) {
256 $nslist = self::getCanonicalNamespaces();
257 return $nslist[$index] ?? false;
258 }
259
260 /**
261 * Returns the index for a given canonical name, or NULL
262 * The input *must* be converted to lower case first
263 *
264 * @param string $name Namespace name
265 * @return int
266 */
267 public static function getCanonicalIndex( $name ) {
268 if ( self::$namespaceIndexes === false ) {
269 self::$namespaceIndexes = [];
270 foreach ( self::getCanonicalNamespaces() as $i => $text ) {
271 self::$namespaceIndexes[strtolower( $text )] = $i;
272 }
273 }
274 if ( array_key_exists( $name, self::$namespaceIndexes ) ) {
275 return self::$namespaceIndexes[$name];
276 } else {
277 return null;
278 }
279 }
280
281 /**
282 * Returns an array of the namespaces (by integer id) that exist on the
283 * wiki. Used primarily by the api in help documentation.
284 * @return array
285 */
286 public static function getValidNamespaces() {
287 if ( is_null( self::$validNamespaces ) ) {
288 foreach ( array_keys( self::getCanonicalNamespaces() ) as $ns ) {
289 if ( $ns >= 0 ) {
290 self::$validNamespaces[] = $ns;
291 }
292 }
293 // T109137: sort numerically
294 sort( self::$validNamespaces, SORT_NUMERIC );
295 }
296
297 return self::$validNamespaces;
298 }
299
300 /**
301 * Does this namespace ever have a talk namespace?
302 *
303 * @deprecated since 1.30, use hasTalkNamespace() instead.
304 *
305 * @param int $index Namespace index
306 * @return bool True if this namespace either is or has a corresponding talk namespace.
307 */
308 public static function canTalk( $index ) {
309 return self::hasTalkNamespace( $index );
310 }
311
312 /**
313 * Does this namespace ever have a talk namespace?
314 *
315 * @since 1.30
316 *
317 * @param int $index Namespace ID
318 * @return bool True if this namespace either is or has a corresponding talk namespace.
319 */
320 public static function hasTalkNamespace( $index ) {
321 return $index >= NS_MAIN;
322 }
323
324 /**
325 * Does this namespace contain content, for the purposes of calculating
326 * statistics, etc?
327 *
328 * @param int $index Index to check
329 * @return bool
330 */
331 public static function isContent( $index ) {
332 global $wgContentNamespaces;
333 return $index == NS_MAIN || in_array( $index, $wgContentNamespaces );
334 }
335
336 /**
337 * Might pages in this namespace require the use of the Signature button on
338 * the edit toolbar?
339 *
340 * @param int $index Index to check
341 * @return bool
342 */
343 public static function wantSignatures( $index ) {
344 global $wgExtraSignatureNamespaces;
345 return self::isTalk( $index ) || in_array( $index, $wgExtraSignatureNamespaces );
346 }
347
348 /**
349 * Can pages in a namespace be watched?
350 *
351 * @param int $index
352 * @return bool
353 */
354 public static function isWatchable( $index ) {
355 return $index >= NS_MAIN;
356 }
357
358 /**
359 * Does the namespace allow subpages?
360 *
361 * @param int $index Index to check
362 * @return bool
363 */
364 public static function hasSubpages( $index ) {
365 global $wgNamespacesWithSubpages;
366 return !empty( $wgNamespacesWithSubpages[$index] );
367 }
368
369 /**
370 * Get a list of all namespace indices which are considered to contain content
371 * @return array Array of namespace indices
372 */
373 public static function getContentNamespaces() {
374 global $wgContentNamespaces;
375 if ( !is_array( $wgContentNamespaces ) || $wgContentNamespaces === [] ) {
376 return [ NS_MAIN ];
377 } elseif ( !in_array( NS_MAIN, $wgContentNamespaces ) ) {
378 // always force NS_MAIN to be part of array (to match the algorithm used by isContent)
379 return array_merge( [ NS_MAIN ], $wgContentNamespaces );
380 } else {
381 return $wgContentNamespaces;
382 }
383 }
384
385 /**
386 * List all namespace indices which are considered subject, aka not a talk
387 * or special namespace. See also MWNamespace::isSubject
388 *
389 * @return array Array of namespace indices
390 */
391 public static function getSubjectNamespaces() {
392 return array_filter(
393 self::getValidNamespaces(),
394 'MWNamespace::isSubject'
395 );
396 }
397
398 /**
399 * List all namespace indices which are considered talks, aka not a subject
400 * or special namespace. See also MWNamespace::isTalk
401 *
402 * @return array Array of namespace indices
403 */
404 public static function getTalkNamespaces() {
405 return array_filter(
406 self::getValidNamespaces(),
407 'MWNamespace::isTalk'
408 );
409 }
410
411 /**
412 * Is the namespace first-letter capitalized?
413 *
414 * @param int $index Index to check
415 * @return bool
416 */
417 public static function isCapitalized( $index ) {
418 global $wgCapitalLinks, $wgCapitalLinkOverrides;
419 // Turn NS_MEDIA into NS_FILE
420 $index = $index === NS_MEDIA ? NS_FILE : $index;
421
422 // Make sure to get the subject of our namespace
423 $index = self::getSubject( $index );
424
425 // Some namespaces are special and should always be upper case
426 if ( in_array( $index, self::$alwaysCapitalizedNamespaces ) ) {
427 return true;
428 }
429 if ( isset( $wgCapitalLinkOverrides[$index] ) ) {
430 // $wgCapitalLinkOverrides is explicitly set
431 return $wgCapitalLinkOverrides[$index];
432 }
433 // Default to the global setting
434 return $wgCapitalLinks;
435 }
436
437 /**
438 * Does the namespace (potentially) have different aliases for different
439 * genders. Not all languages make a distinction here.
440 *
441 * @since 1.18
442 * @param int $index Index to check
443 * @return bool
444 */
445 public static function hasGenderDistinction( $index ) {
446 return $index == NS_USER || $index == NS_USER_TALK;
447 }
448
449 /**
450 * It is not possible to use pages from this namespace as template?
451 *
452 * @since 1.20
453 * @param int $index Index to check
454 * @return bool
455 */
456 public static function isNonincludable( $index ) {
457 global $wgNonincludableNamespaces;
458 return $wgNonincludableNamespaces && in_array( $index, $wgNonincludableNamespaces );
459 }
460
461 /**
462 * Get the default content model for a namespace
463 * This does not mean that all pages in that namespace have the model
464 *
465 * @since 1.21
466 * @param int $index Index to check
467 * @return null|string Default model name for the given namespace, if set
468 */
469 public static function getNamespaceContentModel( $index ) {
470 global $wgNamespaceContentModels;
471 return $wgNamespaceContentModels[$index] ?? null;
472 }
473
474 /**
475 * Determine which restriction levels it makes sense to use in a namespace,
476 * optionally filtered by a user's rights.
477 *
478 * @since 1.23
479 * @param int $index Index to check
480 * @param User $user User to check
481 * @return array
482 */
483 public static function getRestrictionLevels( $index, User $user = null ) {
484 global $wgNamespaceProtection, $wgRestrictionLevels;
485
486 if ( !isset( $wgNamespaceProtection[$index] ) ) {
487 // All levels are valid if there's no namespace restriction.
488 // But still filter by user, if necessary
489 $levels = $wgRestrictionLevels;
490 if ( $user ) {
491 $levels = array_values( array_filter( $levels, function ( $level ) use ( $user ) {
492 $right = $level;
493 if ( $right == 'sysop' ) {
494 $right = 'editprotected'; // BC
495 }
496 if ( $right == 'autoconfirmed' ) {
497 $right = 'editsemiprotected'; // BC
498 }
499 return ( $right == '' || $user->isAllowed( $right ) );
500 } ) );
501 }
502 return $levels;
503 }
504
505 // First, get the list of groups that can edit this namespace.
506 $namespaceGroups = [];
507 $combine = 'array_merge';
508 foreach ( (array)$wgNamespaceProtection[$index] as $right ) {
509 if ( $right == 'sysop' ) {
510 $right = 'editprotected'; // BC
511 }
512 if ( $right == 'autoconfirmed' ) {
513 $right = 'editsemiprotected'; // BC
514 }
515 if ( $right != '' ) {
516 $namespaceGroups = call_user_func( $combine, $namespaceGroups,
517 User::getGroupsWithPermission( $right ) );
518 $combine = 'array_intersect';
519 }
520 }
521
522 // Now, keep only those restriction levels where there is at least one
523 // group that can edit the namespace but would be blocked by the
524 // restriction.
525 $usableLevels = [ '' ];
526 foreach ( $wgRestrictionLevels as $level ) {
527 $right = $level;
528 if ( $right == 'sysop' ) {
529 $right = 'editprotected'; // BC
530 }
531 if ( $right == 'autoconfirmed' ) {
532 $right = 'editsemiprotected'; // BC
533 }
534 if ( $right != '' && ( !$user || $user->isAllowed( $right ) ) &&
535 array_diff( $namespaceGroups, User::getGroupsWithPermission( $right ) )
536 ) {
537 $usableLevels[] = $level;
538 }
539 }
540
541 return $usableLevels;
542 }
543 }