Merge "Put LESS test back into extension suite in XML"
[lhc/web/wiklou.git] / languages / Language.php
1 <?php
2 /**
3 * Internationalisation code.
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 * @ingroup Language
22 */
23
24 /**
25 * @defgroup Language Language
26 */
27
28 if ( !defined( 'MEDIAWIKI' ) ) {
29 echo "This file is part of MediaWiki, it is not a valid entry point.\n";
30 exit( 1 );
31 }
32
33 if ( function_exists( 'mb_strtoupper' ) ) {
34 mb_internal_encoding( 'UTF-8' );
35 }
36
37 /**
38 * a fake language converter
39 *
40 * @ingroup Language
41 */
42 class FakeConverter {
43 /**
44 * @var Language
45 */
46 public $mLang;
47 function __construct( $langobj ) { $this->mLang = $langobj; }
48 function autoConvert( $text, $variant = false ) { return $text; }
49 function autoConvertToAllVariants( $text ) { return array( $this->mLang->getCode() => $text ); }
50 function convert( $t ) { return $t; }
51 function convertTo( $text, $variant ) { return $text; }
52 function convertTitle( $t ) { return $t->getPrefixedText(); }
53 function convertNamespace( $ns ) { return $this->mLang->getFormattedNsText( $ns ); }
54 function getVariants() { return array( $this->mLang->getCode() ); }
55 function getVariantFallbacks( $variant ) { return $this->mLang->getCode(); }
56 function getPreferredVariant() { return $this->mLang->getCode(); }
57 function getDefaultVariant() { return $this->mLang->getCode(); }
58 function getURLVariant() { return ''; }
59 function getConvRuleTitle() { return false; }
60 function findVariantLink( &$l, &$n, $ignoreOtherCond = false ) { }
61 function getExtraHashOptions() { return ''; }
62 function getParsedTitle() { return ''; }
63 function markNoConversion( $text, $noParse = false ) { return $text; }
64 function convertCategoryKey( $key ) { return $key; }
65 /** @deprecated since 1.22 is no longer used */
66 function armourMath( $text ) { return $text; }
67 function validateVariant( $variant = null ) { return $variant === $this->mLang->getCode() ? $variant : null; }
68 function translate( $text, $variant ) { return $text; }
69 }
70
71 /**
72 * Internationalisation code
73 * @ingroup Language
74 */
75 class Language {
76
77 /**
78 * @var LanguageConverter
79 */
80 public $mConverter;
81
82 public $mVariants, $mCode, $mLoaded = false;
83 public $mMagicExtensions = array(), $mMagicHookDone = false;
84 private $mHtmlCode = null, $mParentLanguage = false;
85
86 public $dateFormatStrings = array();
87 public $mExtendedSpecialPageAliases;
88
89 protected $namespaceNames, $mNamespaceIds, $namespaceAliases;
90
91 /**
92 * ReplacementArray object caches
93 */
94 public $transformData = array();
95
96 /**
97 * @var LocalisationCache
98 */
99 static public $dataCache;
100
101 static public $mLangObjCache = array();
102
103 static public $mWeekdayMsgs = array(
104 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday',
105 'friday', 'saturday'
106 );
107
108 static public $mWeekdayAbbrevMsgs = array(
109 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'
110 );
111
112 static public $mMonthMsgs = array(
113 'january', 'february', 'march', 'april', 'may_long', 'june',
114 'july', 'august', 'september', 'october', 'november',
115 'december'
116 );
117 static public $mMonthGenMsgs = array(
118 'january-gen', 'february-gen', 'march-gen', 'april-gen', 'may-gen', 'june-gen',
119 'july-gen', 'august-gen', 'september-gen', 'october-gen', 'november-gen',
120 'december-gen'
121 );
122 static public $mMonthAbbrevMsgs = array(
123 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
124 'sep', 'oct', 'nov', 'dec'
125 );
126
127 static public $mIranianCalendarMonthMsgs = array(
128 'iranian-calendar-m1', 'iranian-calendar-m2', 'iranian-calendar-m3',
129 'iranian-calendar-m4', 'iranian-calendar-m5', 'iranian-calendar-m6',
130 'iranian-calendar-m7', 'iranian-calendar-m8', 'iranian-calendar-m9',
131 'iranian-calendar-m10', 'iranian-calendar-m11', 'iranian-calendar-m12'
132 );
133
134 static public $mHebrewCalendarMonthMsgs = array(
135 'hebrew-calendar-m1', 'hebrew-calendar-m2', 'hebrew-calendar-m3',
136 'hebrew-calendar-m4', 'hebrew-calendar-m5', 'hebrew-calendar-m6',
137 'hebrew-calendar-m7', 'hebrew-calendar-m8', 'hebrew-calendar-m9',
138 'hebrew-calendar-m10', 'hebrew-calendar-m11', 'hebrew-calendar-m12',
139 'hebrew-calendar-m6a', 'hebrew-calendar-m6b'
140 );
141
142 static public $mHebrewCalendarMonthGenMsgs = array(
143 'hebrew-calendar-m1-gen', 'hebrew-calendar-m2-gen', 'hebrew-calendar-m3-gen',
144 'hebrew-calendar-m4-gen', 'hebrew-calendar-m5-gen', 'hebrew-calendar-m6-gen',
145 'hebrew-calendar-m7-gen', 'hebrew-calendar-m8-gen', 'hebrew-calendar-m9-gen',
146 'hebrew-calendar-m10-gen', 'hebrew-calendar-m11-gen', 'hebrew-calendar-m12-gen',
147 'hebrew-calendar-m6a-gen', 'hebrew-calendar-m6b-gen'
148 );
149
150 static public $mHijriCalendarMonthMsgs = array(
151 'hijri-calendar-m1', 'hijri-calendar-m2', 'hijri-calendar-m3',
152 'hijri-calendar-m4', 'hijri-calendar-m5', 'hijri-calendar-m6',
153 'hijri-calendar-m7', 'hijri-calendar-m8', 'hijri-calendar-m9',
154 'hijri-calendar-m10', 'hijri-calendar-m11', 'hijri-calendar-m12'
155 );
156
157 /**
158 * @since 1.20
159 * @var array
160 */
161 static public $durationIntervals = array(
162 'millennia' => 31556952000,
163 'centuries' => 3155695200,
164 'decades' => 315569520,
165 'years' => 31556952, // 86400 * ( 365 + ( 24 * 3 + 25 ) / 400 )
166 'weeks' => 604800,
167 'days' => 86400,
168 'hours' => 3600,
169 'minutes' => 60,
170 'seconds' => 1,
171 );
172
173 /**
174 * Cache for language fallbacks.
175 * @see Language::getFallbacksIncludingSiteLanguage
176 * @since 1.21
177 * @var array
178 */
179 static private $fallbackLanguageCache = array();
180
181 /**
182 * Get a cached or new language object for a given language code
183 * @param $code String
184 * @return Language
185 */
186 static function factory( $code ) {
187 global $wgDummyLanguageCodes, $wgLangObjCacheSize;
188
189 if ( isset( $wgDummyLanguageCodes[$code] ) ) {
190 $code = $wgDummyLanguageCodes[$code];
191 }
192
193 // get the language object to process
194 $langObj = isset( self::$mLangObjCache[$code] )
195 ? self::$mLangObjCache[$code]
196 : self::newFromCode( $code );
197
198 // merge the language object in to get it up front in the cache
199 self::$mLangObjCache = array_merge( array( $code => $langObj ), self::$mLangObjCache );
200 // get rid of the oldest ones in case we have an overflow
201 self::$mLangObjCache = array_slice( self::$mLangObjCache, 0, $wgLangObjCacheSize, true );
202
203 return $langObj;
204 }
205
206 /**
207 * Create a language object for a given language code
208 * @param $code String
209 * @throws MWException
210 * @return Language
211 */
212 protected static function newFromCode( $code ) {
213 // Protect against path traversal below
214 if ( !Language::isValidCode( $code )
215 || strcspn( $code, ":/\\\000" ) !== strlen( $code )
216 ) {
217 throw new MWException( "Invalid language code \"$code\"" );
218 }
219
220 if ( !Language::isValidBuiltInCode( $code ) ) {
221 // It's not possible to customise this code with class files, so
222 // just return a Language object. This is to support uselang= hacks.
223 $lang = new Language;
224 $lang->setCode( $code );
225 return $lang;
226 }
227
228 // Check if there is a language class for the code
229 $class = self::classFromCode( $code );
230 self::preloadLanguageClass( $class );
231 if ( class_exists( $class ) ) {
232 $lang = new $class;
233 return $lang;
234 }
235
236 // Keep trying the fallback list until we find an existing class
237 $fallbacks = Language::getFallbacksFor( $code );
238 foreach ( $fallbacks as $fallbackCode ) {
239 if ( !Language::isValidBuiltInCode( $fallbackCode ) ) {
240 throw new MWException( "Invalid fallback '$fallbackCode' in fallback sequence for '$code'" );
241 }
242
243 $class = self::classFromCode( $fallbackCode );
244 self::preloadLanguageClass( $class );
245 if ( class_exists( $class ) ) {
246 $lang = Language::newFromCode( $fallbackCode );
247 $lang->setCode( $code );
248 return $lang;
249 }
250 }
251
252 throw new MWException( "Invalid fallback sequence for language '$code'" );
253 }
254
255 /**
256 * Checks whether any localisation is available for that language tag
257 * in MediaWiki (MessagesXx.php exists).
258 *
259 * @param string $code Language tag (in lower case)
260 * @return bool Whether language is supported
261 * @since 1.21
262 */
263 public static function isSupportedLanguage( $code ) {
264 return self::isValidBuiltInCode( $code )
265 && ( is_readable( self::getMessagesFileName( $code ) )
266 || is_readable( self::getJsonMessagesFileName( $code ) )
267 );
268 }
269
270 /**
271 * Returns true if a language code string is a well-formed language tag
272 * according to RFC 5646.
273 * This function only checks well-formedness; it doesn't check that
274 * language, script or variant codes actually exist in the repositories.
275 *
276 * Based on regexes by Mark Davis of the Unicode Consortium:
277 * http://unicode.org/repos/cldr/trunk/tools/java/org/unicode/cldr/util/data/langtagRegex.txt
278 *
279 * @param $code string
280 * @param $lenient boolean Whether to allow '_' as separator. The default is only '-'.
281 *
282 * @return bool
283 * @since 1.21
284 */
285 public static function isWellFormedLanguageTag( $code, $lenient = false ) {
286 $alpha = '[a-z]';
287 $digit = '[0-9]';
288 $alphanum = '[a-z0-9]';
289 $x = 'x'; # private use singleton
290 $singleton = '[a-wy-z]'; # other singleton
291 $s = $lenient ? '[-_]' : '-';
292
293 $language = "$alpha{2,8}|$alpha{2,3}$s$alpha{3}";
294 $script = "$alpha{4}"; # ISO 15924
295 $region = "(?:$alpha{2}|$digit{3})"; # ISO 3166-1 alpha-2 or UN M.49
296 $variant = "(?:$alphanum{5,8}|$digit$alphanum{3})";
297 $extension = "$singleton(?:$s$alphanum{2,8})+";
298 $privateUse = "$x(?:$s$alphanum{1,8})+";
299
300 # Define certain grandfathered codes, since otherwise the regex is pretty useless.
301 # Since these are limited, this is safe even later changes to the registry --
302 # the only oddity is that it might change the type of the tag, and thus
303 # the results from the capturing groups.
304 # http://www.iana.org/assignments/language-subtag-registry
305
306 $grandfathered = "en{$s}GB{$s}oed"
307 . "|i{$s}(?:ami|bnn|default|enochian|hak|klingon|lux|mingo|navajo|pwn|tao|tay|tsu)"
308 . "|no{$s}(?:bok|nyn)"
309 . "|sgn{$s}(?:BE{$s}(?:fr|nl)|CH{$s}de)"
310 . "|zh{$s}min{$s}nan";
311
312 $variantList = "$variant(?:$s$variant)*";
313 $extensionList = "$extension(?:$s$extension)*";
314
315 $langtag = "(?:($language)"
316 . "(?:$s$script)?"
317 . "(?:$s$region)?"
318 . "(?:$s$variantList)?"
319 . "(?:$s$extensionList)?"
320 . "(?:$s$privateUse)?)";
321
322 # The final breakdown, with capturing groups for each of these components
323 # The variants, extensions, grandfathered, and private-use may have interior '-'
324
325 $root = "^(?:$langtag|$privateUse|$grandfathered)$";
326
327 return (bool)preg_match( "/$root/", strtolower( $code ) );
328 }
329
330 /**
331 * Returns true if a language code string is of a valid form, whether or
332 * not it exists. This includes codes which are used solely for
333 * customisation via the MediaWiki namespace.
334 *
335 * @param $code string
336 *
337 * @return bool
338 */
339 public static function isValidCode( $code ) {
340 static $cache = array();
341 if ( isset( $cache[$code] ) ) {
342 return $cache[$code];
343 }
344 // People think language codes are html safe, so enforce it.
345 // Ideally we should only allow a-zA-Z0-9-
346 // but, .+ and other chars are often used for {{int:}} hacks
347 // see bugs 37564, 37587, 36938
348 $cache[$code] =
349 strcspn( $code, ":/\\\000&<>'\"" ) === strlen( $code )
350 && !preg_match( Title::getTitleInvalidRegex(), $code );
351
352 return $cache[$code];
353 }
354
355 /**
356 * Returns true if a language code is of a valid form for the purposes of
357 * internal customisation of MediaWiki, via Messages*.php or *.json.
358 *
359 * @param $code string
360 *
361 * @throws MWException
362 * @since 1.18
363 * @return bool
364 */
365 public static function isValidBuiltInCode( $code ) {
366
367 if ( !is_string( $code ) ) {
368 if ( is_object( $code ) ) {
369 $addmsg = " of class " . get_class( $code );
370 } else {
371 $addmsg = '';
372 }
373 $type = gettype( $code );
374 throw new MWException( __METHOD__ . " must be passed a string, $type given$addmsg" );
375 }
376
377 return (bool)preg_match( '/^[a-z0-9-]{2,}$/i', $code );
378 }
379
380 /**
381 * Returns true if a language code is an IETF tag known to MediaWiki.
382 *
383 * @param $code string
384 *
385 * @since 1.21
386 * @return bool
387 */
388 public static function isKnownLanguageTag( $tag ) {
389 static $coreLanguageNames;
390
391 // Quick escape for invalid input to avoid exceptions down the line
392 // when code tries to process tags which are not valid at all.
393 if ( !self::isValidBuiltInCode( $tag ) ) {
394 return false;
395 }
396
397 if ( $coreLanguageNames === null ) {
398 global $IP;
399 include "$IP/languages/Names.php";
400 }
401
402 if ( isset( $coreLanguageNames[$tag] )
403 || self::fetchLanguageName( $tag, $tag ) !== ''
404 ) {
405 return true;
406 }
407
408 return false;
409 }
410
411 /**
412 * @param $code
413 * @return String Name of the language class
414 */
415 public static function classFromCode( $code ) {
416 if ( $code == 'en' ) {
417 return 'Language';
418 } else {
419 return 'Language' . str_replace( '-', '_', ucfirst( $code ) );
420 }
421 }
422
423 /**
424 * Includes language class files
425 *
426 * @param $class string Name of the language class
427 */
428 public static function preloadLanguageClass( $class ) {
429 global $IP;
430
431 if ( $class === 'Language' ) {
432 return;
433 }
434
435 if ( file_exists( "$IP/languages/classes/$class.php" ) ) {
436 include_once "$IP/languages/classes/$class.php";
437 }
438 }
439
440 /**
441 * Get the LocalisationCache instance
442 *
443 * @return LocalisationCache
444 */
445 public static function getLocalisationCache() {
446 if ( is_null( self::$dataCache ) ) {
447 global $wgLocalisationCacheConf;
448 $class = $wgLocalisationCacheConf['class'];
449 self::$dataCache = new $class( $wgLocalisationCacheConf );
450 }
451 return self::$dataCache;
452 }
453
454 function __construct() {
455 $this->mConverter = new FakeConverter( $this );
456 // Set the code to the name of the descendant
457 if ( get_class( $this ) == 'Language' ) {
458 $this->mCode = 'en';
459 } else {
460 $this->mCode = str_replace( '_', '-', strtolower( substr( get_class( $this ), 8 ) ) );
461 }
462 self::getLocalisationCache();
463 }
464
465 /**
466 * Reduce memory usage
467 */
468 function __destruct() {
469 foreach ( $this as $name => $value ) {
470 unset( $this->$name );
471 }
472 }
473
474 /**
475 * Hook which will be called if this is the content language.
476 * Descendants can use this to register hook functions or modify globals
477 */
478 function initContLang() { }
479
480 /**
481 * Same as getFallbacksFor for current language.
482 * @return array|bool
483 * @deprecated in 1.19
484 */
485 function getFallbackLanguageCode() {
486 wfDeprecated( __METHOD__, '1.19' );
487 return self::getFallbackFor( $this->mCode );
488 }
489
490 /**
491 * @return array
492 * @since 1.19
493 */
494 function getFallbackLanguages() {
495 return self::getFallbacksFor( $this->mCode );
496 }
497
498 /**
499 * Exports $wgBookstoreListEn
500 * @return array
501 */
502 function getBookstoreList() {
503 return self::$dataCache->getItem( $this->mCode, 'bookstoreList' );
504 }
505
506 /**
507 * Returns an array of localised namespaces indexed by their numbers. If the namespace is not
508 * available in localised form, it will be included in English.
509 *
510 * @return array
511 */
512 public function getNamespaces() {
513 if ( is_null( $this->namespaceNames ) ) {
514 global $wgMetaNamespace, $wgMetaNamespaceTalk, $wgExtraNamespaces;
515
516 $this->namespaceNames = self::$dataCache->getItem( $this->mCode, 'namespaceNames' );
517 $validNamespaces = MWNamespace::getCanonicalNamespaces();
518
519 $this->namespaceNames = $wgExtraNamespaces + $this->namespaceNames + $validNamespaces;
520
521 $this->namespaceNames[NS_PROJECT] = $wgMetaNamespace;
522 if ( $wgMetaNamespaceTalk ) {
523 $this->namespaceNames[NS_PROJECT_TALK] = $wgMetaNamespaceTalk;
524 } else {
525 $talk = $this->namespaceNames[NS_PROJECT_TALK];
526 $this->namespaceNames[NS_PROJECT_TALK] =
527 $this->fixVariableInNamespace( $talk );
528 }
529
530 # Sometimes a language will be localised but not actually exist on this wiki.
531 foreach ( $this->namespaceNames as $key => $text ) {
532 if ( !isset( $validNamespaces[$key] ) ) {
533 unset( $this->namespaceNames[$key] );
534 }
535 }
536
537 # The above mixing may leave namespaces out of canonical order.
538 # Re-order by namespace ID number...
539 ksort( $this->namespaceNames );
540
541 wfRunHooks( 'LanguageGetNamespaces', array( &$this->namespaceNames ) );
542 }
543 return $this->namespaceNames;
544 }
545
546 /**
547 * Arbitrarily set all of the namespace names at once. Mainly used for testing
548 * @param $namespaces Array of namespaces (id => name)
549 */
550 public function setNamespaces( array $namespaces ) {
551 $this->namespaceNames = $namespaces;
552 $this->mNamespaceIds = null;
553 }
554
555 /**
556 * Resets all of the namespace caches. Mainly used for testing
557 */
558 public function resetNamespaces() {
559 $this->namespaceNames = null;
560 $this->mNamespaceIds = null;
561 $this->namespaceAliases = null;
562 }
563
564 /**
565 * A convenience function that returns the same thing as
566 * getNamespaces() except with the array values changed to ' '
567 * where it found '_', useful for producing output to be displayed
568 * e.g. in <select> forms.
569 *
570 * @return array
571 */
572 function getFormattedNamespaces() {
573 $ns = $this->getNamespaces();
574 foreach ( $ns as $k => $v ) {
575 $ns[$k] = strtr( $v, '_', ' ' );
576 }
577 return $ns;
578 }
579
580 /**
581 * Get a namespace value by key
582 * <code>
583 * $mw_ns = $wgContLang->getNsText( NS_MEDIAWIKI );
584 * echo $mw_ns; // prints 'MediaWiki'
585 * </code>
586 *
587 * @param $index Int: the array key of the namespace to return
588 * @return mixed, string if the namespace value exists, otherwise false
589 */
590 function getNsText( $index ) {
591 $ns = $this->getNamespaces();
592 return isset( $ns[$index] ) ? $ns[$index] : false;
593 }
594
595 /**
596 * A convenience function that returns the same thing as
597 * getNsText() except with '_' changed to ' ', useful for
598 * producing output.
599 *
600 * <code>
601 * $mw_ns = $wgContLang->getFormattedNsText( NS_MEDIAWIKI_TALK );
602 * echo $mw_ns; // prints 'MediaWiki talk'
603 * </code>
604 *
605 * @param int $index The array key of the namespace to return
606 * @return string Namespace name without underscores (empty string if namespace does not exist)
607 */
608 function getFormattedNsText( $index ) {
609 $ns = $this->getNsText( $index );
610 return strtr( $ns, '_', ' ' );
611 }
612
613 /**
614 * Returns gender-dependent namespace alias if available.
615 * @param $index Int: namespace index
616 * @param $gender String: gender key (male, female... )
617 * @return String
618 * @since 1.18
619 */
620 function getGenderNsText( $index, $gender ) {
621 global $wgExtraGenderNamespaces;
622
623 $ns = $wgExtraGenderNamespaces + self::$dataCache->getItem( $this->mCode, 'namespaceGenderAliases' );
624 return isset( $ns[$index][$gender] ) ? $ns[$index][$gender] : $this->getNsText( $index );
625 }
626
627 /**
628 * Whether this language makes distinguishes genders for example in
629 * namespaces.
630 * @return bool
631 * @since 1.18
632 */
633 function needsGenderDistinction() {
634 global $wgExtraGenderNamespaces, $wgExtraNamespaces;
635 if ( count( $wgExtraGenderNamespaces ) > 0 ) {
636 // $wgExtraGenderNamespaces overrides everything
637 return true;
638 } elseif ( isset( $wgExtraNamespaces[NS_USER] ) && isset( $wgExtraNamespaces[NS_USER_TALK] ) ) {
639 /// @todo There may be other gender namespace than NS_USER & NS_USER_TALK in the future
640 // $wgExtraNamespaces overrides any gender aliases specified in i18n files
641 return false;
642 } else {
643 // Check what is in i18n files
644 $aliases = self::$dataCache->getItem( $this->mCode, 'namespaceGenderAliases' );
645 return count( $aliases ) > 0;
646 }
647 }
648
649 /**
650 * Get a namespace key by value, case insensitive.
651 * Only matches namespace names for the current language, not the
652 * canonical ones defined in Namespace.php.
653 *
654 * @param $text String
655 * @return mixed An integer if $text is a valid value otherwise false
656 */
657 function getLocalNsIndex( $text ) {
658 $lctext = $this->lc( $text );
659 $ids = $this->getNamespaceIds();
660 return isset( $ids[$lctext] ) ? $ids[$lctext] : false;
661 }
662
663 /**
664 * @return array
665 */
666 function getNamespaceAliases() {
667 if ( is_null( $this->namespaceAliases ) ) {
668 $aliases = self::$dataCache->getItem( $this->mCode, 'namespaceAliases' );
669 if ( !$aliases ) {
670 $aliases = array();
671 } else {
672 foreach ( $aliases as $name => $index ) {
673 if ( $index === NS_PROJECT_TALK ) {
674 unset( $aliases[$name] );
675 $name = $this->fixVariableInNamespace( $name );
676 $aliases[$name] = $index;
677 }
678 }
679 }
680
681 global $wgExtraGenderNamespaces;
682 $genders = $wgExtraGenderNamespaces + (array)self::$dataCache->getItem( $this->mCode, 'namespaceGenderAliases' );
683 foreach ( $genders as $index => $forms ) {
684 foreach ( $forms as $alias ) {
685 $aliases[$alias] = $index;
686 }
687 }
688
689 # Also add converted namespace names as aliases, to avoid confusion.
690 $convertedNames = array();
691 foreach ( $this->getVariants() as $variant ) {
692 if ( $variant === $this->mCode ) {
693 continue;
694 }
695 foreach ( $this->getNamespaces() as $ns => $_ ) {
696 $convertedNames[$this->getConverter()->convertNamespace( $ns, $variant )] = $ns;
697 }
698 }
699
700 $this->namespaceAliases = $aliases + $convertedNames;
701 }
702 return $this->namespaceAliases;
703 }
704
705 /**
706 * @return array
707 */
708 function getNamespaceIds() {
709 if ( is_null( $this->mNamespaceIds ) ) {
710 global $wgNamespaceAliases;
711 # Put namespace names and aliases into a hashtable.
712 # If this is too slow, then we should arrange it so that it is done
713 # before caching. The catch is that at pre-cache time, the above
714 # class-specific fixup hasn't been done.
715 $this->mNamespaceIds = array();
716 foreach ( $this->getNamespaces() as $index => $name ) {
717 $this->mNamespaceIds[$this->lc( $name )] = $index;
718 }
719 foreach ( $this->getNamespaceAliases() as $name => $index ) {
720 $this->mNamespaceIds[$this->lc( $name )] = $index;
721 }
722 if ( $wgNamespaceAliases ) {
723 foreach ( $wgNamespaceAliases as $name => $index ) {
724 $this->mNamespaceIds[$this->lc( $name )] = $index;
725 }
726 }
727 }
728 return $this->mNamespaceIds;
729 }
730
731 /**
732 * Get a namespace key by value, case insensitive. Canonical namespace
733 * names override custom ones defined for the current language.
734 *
735 * @param $text String
736 * @return mixed An integer if $text is a valid value otherwise false
737 */
738 function getNsIndex( $text ) {
739 $lctext = $this->lc( $text );
740 $ns = MWNamespace::getCanonicalIndex( $lctext );
741 if ( $ns !== null ) {
742 return $ns;
743 }
744 $ids = $this->getNamespaceIds();
745 return isset( $ids[$lctext] ) ? $ids[$lctext] : false;
746 }
747
748 /**
749 * short names for language variants used for language conversion links.
750 *
751 * @param $code String
752 * @param $usemsg bool Use the "variantname-xyz" message if it exists
753 * @return string
754 */
755 function getVariantname( $code, $usemsg = true ) {
756 $msg = "variantname-$code";
757 if ( $usemsg && wfMessage( $msg )->exists() ) {
758 return $this->getMessageFromDB( $msg );
759 }
760 $name = self::fetchLanguageName( $code );
761 if ( $name ) {
762 return $name; # if it's defined as a language name, show that
763 } else {
764 # otherwise, output the language code
765 return $code;
766 }
767 }
768
769 /**
770 * @param $name string
771 * @return string
772 */
773 function specialPage( $name ) {
774 $aliases = $this->getSpecialPageAliases();
775 if ( isset( $aliases[$name][0] ) ) {
776 $name = $aliases[$name][0];
777 }
778 return $this->getNsText( NS_SPECIAL ) . ':' . $name;
779 }
780
781 /**
782 * @return array
783 */
784 function getDatePreferences() {
785 return self::$dataCache->getItem( $this->mCode, 'datePreferences' );
786 }
787
788 /**
789 * @return array
790 */
791 function getDateFormats() {
792 return self::$dataCache->getItem( $this->mCode, 'dateFormats' );
793 }
794
795 /**
796 * @return array|string
797 */
798 function getDefaultDateFormat() {
799 $df = self::$dataCache->getItem( $this->mCode, 'defaultDateFormat' );
800 if ( $df === 'dmy or mdy' ) {
801 global $wgAmericanDates;
802 return $wgAmericanDates ? 'mdy' : 'dmy';
803 } else {
804 return $df;
805 }
806 }
807
808 /**
809 * @return array
810 */
811 function getDatePreferenceMigrationMap() {
812 return self::$dataCache->getItem( $this->mCode, 'datePreferenceMigrationMap' );
813 }
814
815 /**
816 * @param $image
817 * @return array|null
818 */
819 function getImageFile( $image ) {
820 return self::$dataCache->getSubitem( $this->mCode, 'imageFiles', $image );
821 }
822
823 /**
824 * @return array
825 */
826 function getExtraUserToggles() {
827 return (array)self::$dataCache->getItem( $this->mCode, 'extraUserToggles' );
828 }
829
830 /**
831 * @param $tog
832 * @return string
833 */
834 function getUserToggle( $tog ) {
835 return $this->getMessageFromDB( "tog-$tog" );
836 }
837
838 /**
839 * Get native language names, indexed by code.
840 * Only those defined in MediaWiki, no other data like CLDR.
841 * If $customisedOnly is true, only returns codes with a messages file
842 *
843 * @param $customisedOnly bool
844 *
845 * @return array
846 * @deprecated in 1.20, use fetchLanguageNames()
847 */
848 public static function getLanguageNames( $customisedOnly = false ) {
849 return self::fetchLanguageNames( null, $customisedOnly ? 'mwfile' : 'mw' );
850 }
851
852 /**
853 * Get translated language names. This is done on best effort and
854 * by default this is exactly the same as Language::getLanguageNames.
855 * The CLDR extension provides translated names.
856 * @param $code String Language code.
857 * @return Array language code => language name
858 * @since 1.18.0
859 * @deprecated in 1.20, use fetchLanguageNames()
860 */
861 public static function getTranslatedLanguageNames( $code ) {
862 return self::fetchLanguageNames( $code, 'all' );
863 }
864
865 /**
866 * Get an array of language names, indexed by code.
867 * @param $inLanguage null|string: Code of language in which to return the names
868 * Use null for autonyms (native names)
869 * @param $include string:
870 * 'all' all available languages
871 * 'mw' only if the language is defined in MediaWiki or wgExtraLanguageNames (default)
872 * 'mwfile' only if the language is in 'mw' *and* has a message file
873 * @return array: language code => language name
874 * @since 1.20
875 */
876 public static function fetchLanguageNames( $inLanguage = null, $include = 'mw' ) {
877 global $wgExtraLanguageNames;
878 static $coreLanguageNames;
879
880 if ( $coreLanguageNames === null ) {
881 global $IP;
882 include "$IP/languages/Names.php";
883 }
884
885 $names = array();
886
887 if ( $inLanguage ) {
888 # TODO: also include when $inLanguage is null, when this code is more efficient
889 wfRunHooks( 'LanguageGetTranslatedLanguageNames', array( &$names, $inLanguage ) );
890 }
891
892 $mwNames = $wgExtraLanguageNames + $coreLanguageNames;
893 foreach ( $mwNames as $mwCode => $mwName ) {
894 # - Prefer own MediaWiki native name when not using the hook
895 # - For other names just add if not added through the hook
896 if ( $mwCode === $inLanguage || !isset( $names[$mwCode] ) ) {
897 $names[$mwCode] = $mwName;
898 }
899 }
900
901 if ( $include === 'all' ) {
902 return $names;
903 }
904
905 $returnMw = array();
906 $coreCodes = array_keys( $mwNames );
907 foreach ( $coreCodes as $coreCode ) {
908 $returnMw[$coreCode] = $names[$coreCode];
909 }
910
911 if ( $include === 'mwfile' ) {
912 $namesMwFile = array();
913 # We do this using a foreach over the codes instead of a directory
914 # loop so that messages files in extensions will work correctly.
915 foreach ( $returnMw as $code => $value ) {
916 if ( is_readable( self::getMessagesFileName( $code ) )
917 || is_readable( self::getJsonMessagesFileName( $code ) )
918 ) {
919 $namesMwFile[$code] = $names[$code];
920 }
921 }
922
923 return $namesMwFile;
924 }
925
926 # 'mw' option; default if it's not one of the other two options (all/mwfile)
927 return $returnMw;
928 }
929
930 /**
931 * @param $code string: The code of the language for which to get the name
932 * @param $inLanguage null|string: Code of language in which to return the name (null for autonyms)
933 * @param $include string: 'all', 'mw' or 'mwfile'; see fetchLanguageNames()
934 * @return string: Language name or empty
935 * @since 1.20
936 */
937 public static function fetchLanguageName( $code, $inLanguage = null, $include = 'all' ) {
938 $code = strtolower( $code );
939 $array = self::fetchLanguageNames( $inLanguage, $include );
940 return !array_key_exists( $code, $array ) ? '' : $array[$code];
941 }
942
943 /**
944 * Get a message from the MediaWiki namespace.
945 *
946 * @param $msg String: message name
947 * @return string
948 */
949 function getMessageFromDB( $msg ) {
950 return wfMessage( $msg )->inLanguage( $this )->text();
951 }
952
953 /**
954 * Get the native language name of $code.
955 * Only if defined in MediaWiki, no other data like CLDR.
956 * @param $code string
957 * @return string
958 * @deprecated in 1.20, use fetchLanguageName()
959 */
960 function getLanguageName( $code ) {
961 return self::fetchLanguageName( $code );
962 }
963
964 /**
965 * @param $key string
966 * @return string
967 */
968 function getMonthName( $key ) {
969 return $this->getMessageFromDB( self::$mMonthMsgs[$key - 1] );
970 }
971
972 /**
973 * @return array
974 */
975 function getMonthNamesArray() {
976 $monthNames = array( '' );
977 for ( $i = 1; $i < 13; $i++ ) {
978 $monthNames[] = $this->getMonthName( $i );
979 }
980 return $monthNames;
981 }
982
983 /**
984 * @param $key string
985 * @return string
986 */
987 function getMonthNameGen( $key ) {
988 return $this->getMessageFromDB( self::$mMonthGenMsgs[$key - 1] );
989 }
990
991 /**
992 * @param $key string
993 * @return string
994 */
995 function getMonthAbbreviation( $key ) {
996 return $this->getMessageFromDB( self::$mMonthAbbrevMsgs[$key - 1] );
997 }
998
999 /**
1000 * @return array
1001 */
1002 function getMonthAbbreviationsArray() {
1003 $monthNames = array( '' );
1004 for ( $i = 1; $i < 13; $i++ ) {
1005 $monthNames[] = $this->getMonthAbbreviation( $i );
1006 }
1007 return $monthNames;
1008 }
1009
1010 /**
1011 * @param $key string
1012 * @return string
1013 */
1014 function getWeekdayName( $key ) {
1015 return $this->getMessageFromDB( self::$mWeekdayMsgs[$key - 1] );
1016 }
1017
1018 /**
1019 * @param $key string
1020 * @return string
1021 */
1022 function getWeekdayAbbreviation( $key ) {
1023 return $this->getMessageFromDB( self::$mWeekdayAbbrevMsgs[$key - 1] );
1024 }
1025
1026 /**
1027 * @param $key string
1028 * @return string
1029 */
1030 function getIranianCalendarMonthName( $key ) {
1031 return $this->getMessageFromDB( self::$mIranianCalendarMonthMsgs[$key - 1] );
1032 }
1033
1034 /**
1035 * @param $key string
1036 * @return string
1037 */
1038 function getHebrewCalendarMonthName( $key ) {
1039 return $this->getMessageFromDB( self::$mHebrewCalendarMonthMsgs[$key - 1] );
1040 }
1041
1042 /**
1043 * @param $key string
1044 * @return string
1045 */
1046 function getHebrewCalendarMonthNameGen( $key ) {
1047 return $this->getMessageFromDB( self::$mHebrewCalendarMonthGenMsgs[$key - 1] );
1048 }
1049
1050 /**
1051 * @param $key string
1052 * @return string
1053 */
1054 function getHijriCalendarMonthName( $key ) {
1055 return $this->getMessageFromDB( self::$mHijriCalendarMonthMsgs[$key - 1] );
1056 }
1057
1058 /**
1059 * This is a workalike of PHP's date() function, but with better
1060 * internationalisation, a reduced set of format characters, and a better
1061 * escaping format.
1062 *
1063 * Supported format characters are dDjlNwzWFmMntLoYyaAgGhHiscrUeIOPTZ. See
1064 * the PHP manual for definitions. There are a number of extensions, which
1065 * start with "x":
1066 *
1067 * xn Do not translate digits of the next numeric format character
1068 * xN Toggle raw digit (xn) flag, stays set until explicitly unset
1069 * xr Use roman numerals for the next numeric format character
1070 * xh Use hebrew numerals for the next numeric format character
1071 * xx Literal x
1072 * xg Genitive month name
1073 *
1074 * xij j (day number) in Iranian calendar
1075 * xiF F (month name) in Iranian calendar
1076 * xin n (month number) in Iranian calendar
1077 * xiy y (two digit year) in Iranian calendar
1078 * xiY Y (full year) in Iranian calendar
1079 *
1080 * xjj j (day number) in Hebrew calendar
1081 * xjF F (month name) in Hebrew calendar
1082 * xjt t (days in month) in Hebrew calendar
1083 * xjx xg (genitive month name) in Hebrew calendar
1084 * xjn n (month number) in Hebrew calendar
1085 * xjY Y (full year) in Hebrew calendar
1086 *
1087 * xmj j (day number) in Hijri calendar
1088 * xmF F (month name) in Hijri calendar
1089 * xmn n (month number) in Hijri calendar
1090 * xmY Y (full year) in Hijri calendar
1091 *
1092 * xkY Y (full year) in Thai solar calendar. Months and days are
1093 * identical to the Gregorian calendar
1094 * xoY Y (full year) in Minguo calendar or Juche year.
1095 * Months and days are identical to the
1096 * Gregorian calendar
1097 * xtY Y (full year) in Japanese nengo. Months and days are
1098 * identical to the Gregorian calendar
1099 *
1100 * Characters enclosed in double quotes will be considered literal (with
1101 * the quotes themselves removed). Unmatched quotes will be considered
1102 * literal quotes. Example:
1103 *
1104 * "The month is" F => The month is January
1105 * i's" => 20'11"
1106 *
1107 * Backslash escaping is also supported.
1108 *
1109 * Input timestamp is assumed to be pre-normalized to the desired local
1110 * time zone, if any. Note that the format characters crUeIOPTZ will assume
1111 * $ts is UTC if $zone is not given.
1112 *
1113 * @param $format String
1114 * @param $ts String: 14-character timestamp
1115 * YYYYMMDDHHMMSS
1116 * 01234567890123
1117 * @param $zone DateTimeZone: Timezone of $ts
1118 * @todo handling of "o" format character for Iranian, Hebrew, Hijri & Thai?
1119 *
1120 * @throws MWException
1121 * @return string
1122 */
1123 function sprintfDate( $format, $ts, DateTimeZone $zone = null ) {
1124 $s = '';
1125 $raw = false;
1126 $roman = false;
1127 $hebrewNum = false;
1128 $dateTimeObj = false;
1129 $rawToggle = false;
1130 $iranian = false;
1131 $hebrew = false;
1132 $hijri = false;
1133 $thai = false;
1134 $minguo = false;
1135 $tenno = false;
1136
1137 if ( strlen( $ts ) !== 14 ) {
1138 throw new MWException( __METHOD__ . ": The timestamp $ts should have 14 characters" );
1139 }
1140
1141 if ( !ctype_digit( $ts ) ) {
1142 throw new MWException( __METHOD__ . ": The timestamp $ts should be a number" );
1143 }
1144
1145 for ( $p = 0; $p < strlen( $format ); $p++ ) {
1146 $num = false;
1147 $code = $format[$p];
1148 if ( $code == 'x' && $p < strlen( $format ) - 1 ) {
1149 $code .= $format[++$p];
1150 }
1151
1152 if ( ( $code === 'xi' || $code == 'xj' || $code == 'xk' || $code == 'xm' || $code == 'xo' || $code == 'xt' ) && $p < strlen( $format ) - 1 ) {
1153 $code .= $format[++$p];
1154 }
1155
1156 switch ( $code ) {
1157 case 'xx':
1158 $s .= 'x';
1159 break;
1160 case 'xn':
1161 $raw = true;
1162 break;
1163 case 'xN':
1164 $rawToggle = !$rawToggle;
1165 break;
1166 case 'xr':
1167 $roman = true;
1168 break;
1169 case 'xh':
1170 $hebrewNum = true;
1171 break;
1172 case 'xg':
1173 $s .= $this->getMonthNameGen( substr( $ts, 4, 2 ) );
1174 break;
1175 case 'xjx':
1176 if ( !$hebrew ) {
1177 $hebrew = self::tsToHebrew( $ts );
1178 }
1179 $s .= $this->getHebrewCalendarMonthNameGen( $hebrew[1] );
1180 break;
1181 case 'd':
1182 $num = substr( $ts, 6, 2 );
1183 break;
1184 case 'D':
1185 if ( !$dateTimeObj ) {
1186 $dateTimeObj = DateTime::createFromFormat(
1187 'YmdHis', $ts, $zone ?: new DateTimeZone( 'UTC' )
1188 );
1189 }
1190 $s .= $this->getWeekdayAbbreviation( $dateTimeObj->format( 'w' ) + 1 );
1191 break;
1192 case 'j':
1193 $num = intval( substr( $ts, 6, 2 ) );
1194 break;
1195 case 'xij':
1196 if ( !$iranian ) {
1197 $iranian = self::tsToIranian( $ts );
1198 }
1199 $num = $iranian[2];
1200 break;
1201 case 'xmj':
1202 if ( !$hijri ) {
1203 $hijri = self::tsToHijri( $ts );
1204 }
1205 $num = $hijri[2];
1206 break;
1207 case 'xjj':
1208 if ( !$hebrew ) {
1209 $hebrew = self::tsToHebrew( $ts );
1210 }
1211 $num = $hebrew[2];
1212 break;
1213 case 'l':
1214 if ( !$dateTimeObj ) {
1215 $dateTimeObj = DateTime::createFromFormat(
1216 'YmdHis', $ts, $zone ?: new DateTimeZone( 'UTC' )
1217 );
1218 }
1219 $s .= $this->getWeekdayName( $dateTimeObj->format( 'w' ) + 1 );
1220 break;
1221 case 'F':
1222 $s .= $this->getMonthName( substr( $ts, 4, 2 ) );
1223 break;
1224 case 'xiF':
1225 if ( !$iranian ) {
1226 $iranian = self::tsToIranian( $ts );
1227 }
1228 $s .= $this->getIranianCalendarMonthName( $iranian[1] );
1229 break;
1230 case 'xmF':
1231 if ( !$hijri ) {
1232 $hijri = self::tsToHijri( $ts );
1233 }
1234 $s .= $this->getHijriCalendarMonthName( $hijri[1] );
1235 break;
1236 case 'xjF':
1237 if ( !$hebrew ) {
1238 $hebrew = self::tsToHebrew( $ts );
1239 }
1240 $s .= $this->getHebrewCalendarMonthName( $hebrew[1] );
1241 break;
1242 case 'm':
1243 $num = substr( $ts, 4, 2 );
1244 break;
1245 case 'M':
1246 $s .= $this->getMonthAbbreviation( substr( $ts, 4, 2 ) );
1247 break;
1248 case 'n':
1249 $num = intval( substr( $ts, 4, 2 ) );
1250 break;
1251 case 'xin':
1252 if ( !$iranian ) {
1253 $iranian = self::tsToIranian( $ts );
1254 }
1255 $num = $iranian[1];
1256 break;
1257 case 'xmn':
1258 if ( !$hijri ) {
1259 $hijri = self::tsToHijri ( $ts );
1260 }
1261 $num = $hijri[1];
1262 break;
1263 case 'xjn':
1264 if ( !$hebrew ) {
1265 $hebrew = self::tsToHebrew( $ts );
1266 }
1267 $num = $hebrew[1];
1268 break;
1269 case 'xjt':
1270 if ( !$hebrew ) {
1271 $hebrew = self::tsToHebrew( $ts );
1272 }
1273 $num = $hebrew[3];
1274 break;
1275 case 'Y':
1276 $num = substr( $ts, 0, 4 );
1277 break;
1278 case 'xiY':
1279 if ( !$iranian ) {
1280 $iranian = self::tsToIranian( $ts );
1281 }
1282 $num = $iranian[0];
1283 break;
1284 case 'xmY':
1285 if ( !$hijri ) {
1286 $hijri = self::tsToHijri( $ts );
1287 }
1288 $num = $hijri[0];
1289 break;
1290 case 'xjY':
1291 if ( !$hebrew ) {
1292 $hebrew = self::tsToHebrew( $ts );
1293 }
1294 $num = $hebrew[0];
1295 break;
1296 case 'xkY':
1297 if ( !$thai ) {
1298 $thai = self::tsToYear( $ts, 'thai' );
1299 }
1300 $num = $thai[0];
1301 break;
1302 case 'xoY':
1303 if ( !$minguo ) {
1304 $minguo = self::tsToYear( $ts, 'minguo' );
1305 }
1306 $num = $minguo[0];
1307 break;
1308 case 'xtY':
1309 if ( !$tenno ) {
1310 $tenno = self::tsToYear( $ts, 'tenno' );
1311 }
1312 $num = $tenno[0];
1313 break;
1314 case 'y':
1315 $num = substr( $ts, 2, 2 );
1316 break;
1317 case 'xiy':
1318 if ( !$iranian ) {
1319 $iranian = self::tsToIranian( $ts );
1320 }
1321 $num = substr( $iranian[0], -2 );
1322 break;
1323 case 'a':
1324 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ? 'am' : 'pm';
1325 break;
1326 case 'A':
1327 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ? 'AM' : 'PM';
1328 break;
1329 case 'g':
1330 $h = substr( $ts, 8, 2 );
1331 $num = $h % 12 ? $h % 12 : 12;
1332 break;
1333 case 'G':
1334 $num = intval( substr( $ts, 8, 2 ) );
1335 break;
1336 case 'h':
1337 $h = substr( $ts, 8, 2 );
1338 $num = sprintf( '%02d', $h % 12 ? $h % 12 : 12 );
1339 break;
1340 case 'H':
1341 $num = substr( $ts, 8, 2 );
1342 break;
1343 case 'i':
1344 $num = substr( $ts, 10, 2 );
1345 break;
1346 case 's':
1347 $num = substr( $ts, 12, 2 );
1348 break;
1349 case 'c':
1350 case 'r':
1351 case 'e':
1352 case 'O':
1353 case 'P':
1354 case 'T':
1355 // Pass through string from $dateTimeObj->format()
1356 if ( !$dateTimeObj ) {
1357 $dateTimeObj = DateTime::createFromFormat(
1358 'YmdHis', $ts, $zone ?: new DateTimeZone( 'UTC' )
1359 );
1360 }
1361 $s .= $dateTimeObj->format( $code );
1362 break;
1363 case 'w':
1364 case 'N':
1365 case 'z':
1366 case 'W':
1367 case 't':
1368 case 'L':
1369 case 'o':
1370 case 'U':
1371 case 'I':
1372 case 'Z':
1373 // Pass through number from $dateTimeObj->format()
1374 if ( !$dateTimeObj ) {
1375 $dateTimeObj = DateTime::createFromFormat(
1376 'YmdHis', $ts, $zone ?: new DateTimeZone( 'UTC' )
1377 );
1378 }
1379 $num = $dateTimeObj->format( $code );
1380 break;
1381 case '\\':
1382 # Backslash escaping
1383 if ( $p < strlen( $format ) - 1 ) {
1384 $s .= $format[++$p];
1385 } else {
1386 $s .= '\\';
1387 }
1388 break;
1389 case '"':
1390 # Quoted literal
1391 if ( $p < strlen( $format ) - 1 ) {
1392 $endQuote = strpos( $format, '"', $p + 1 );
1393 if ( $endQuote === false ) {
1394 # No terminating quote, assume literal "
1395 $s .= '"';
1396 } else {
1397 $s .= substr( $format, $p + 1, $endQuote - $p - 1 );
1398 $p = $endQuote;
1399 }
1400 } else {
1401 # Quote at end of string, assume literal "
1402 $s .= '"';
1403 }
1404 break;
1405 default:
1406 $s .= $format[$p];
1407 }
1408 if ( $num !== false ) {
1409 if ( $rawToggle || $raw ) {
1410 $s .= $num;
1411 $raw = false;
1412 } elseif ( $roman ) {
1413 $s .= Language::romanNumeral( $num );
1414 $roman = false;
1415 } elseif ( $hebrewNum ) {
1416 $s .= self::hebrewNumeral( $num );
1417 $hebrewNum = false;
1418 } else {
1419 $s .= $this->formatNum( $num, true );
1420 }
1421 }
1422 }
1423 return $s;
1424 }
1425
1426 private static $GREG_DAYS = array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
1427 private static $IRANIAN_DAYS = array( 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29 );
1428
1429 /**
1430 * Algorithm by Roozbeh Pournader and Mohammad Toossi to convert
1431 * Gregorian dates to Iranian dates. Originally written in C, it
1432 * is released under the terms of GNU Lesser General Public
1433 * License. Conversion to PHP was performed by Niklas Laxström.
1434 *
1435 * Link: http://www.farsiweb.info/jalali/jalali.c
1436 *
1437 * @param $ts string
1438 *
1439 * @return string
1440 */
1441 private static function tsToIranian( $ts ) {
1442 $gy = substr( $ts, 0, 4 ) -1600;
1443 $gm = substr( $ts, 4, 2 ) -1;
1444 $gd = substr( $ts, 6, 2 ) -1;
1445
1446 # Days passed from the beginning (including leap years)
1447 $gDayNo = 365 * $gy
1448 + floor( ( $gy + 3 ) / 4 )
1449 - floor( ( $gy + 99 ) / 100 )
1450 + floor( ( $gy + 399 ) / 400 );
1451
1452 // Add days of the past months of this year
1453 for ( $i = 0; $i < $gm; $i++ ) {
1454 $gDayNo += self::$GREG_DAYS[$i];
1455 }
1456
1457 // Leap years
1458 if ( $gm > 1 && ( ( $gy % 4 === 0 && $gy % 100 !== 0 || ( $gy % 400 == 0 ) ) ) ) {
1459 $gDayNo++;
1460 }
1461
1462 // Days passed in current month
1463 $gDayNo += (int)$gd;
1464
1465 $jDayNo = $gDayNo - 79;
1466
1467 $jNp = floor( $jDayNo / 12053 );
1468 $jDayNo %= 12053;
1469
1470 $jy = 979 + 33 * $jNp + 4 * floor( $jDayNo / 1461 );
1471 $jDayNo %= 1461;
1472
1473 if ( $jDayNo >= 366 ) {
1474 $jy += floor( ( $jDayNo - 1 ) / 365 );
1475 $jDayNo = floor( ( $jDayNo - 1 ) % 365 );
1476 }
1477
1478 for ( $i = 0; $i < 11 && $jDayNo >= self::$IRANIAN_DAYS[$i]; $i++ ) {
1479 $jDayNo -= self::$IRANIAN_DAYS[$i];
1480 }
1481
1482 $jm = $i + 1;
1483 $jd = $jDayNo + 1;
1484
1485 return array( $jy, $jm, $jd );
1486 }
1487
1488 /**
1489 * Converting Gregorian dates to Hijri dates.
1490 *
1491 * Based on a PHP-Nuke block by Sharjeel which is released under GNU/GPL license
1492 *
1493 * @see http://phpnuke.org/modules.php?name=News&file=article&sid=8234&mode=thread&order=0&thold=0
1494 *
1495 * @param $ts string
1496 *
1497 * @return string
1498 */
1499 private static function tsToHijri( $ts ) {
1500 $year = substr( $ts, 0, 4 );
1501 $month = substr( $ts, 4, 2 );
1502 $day = substr( $ts, 6, 2 );
1503
1504 $zyr = $year;
1505 $zd = $day;
1506 $zm = $month;
1507 $zy = $zyr;
1508
1509 if (
1510 ( $zy > 1582 ) || ( ( $zy == 1582 ) && ( $zm > 10 ) ) ||
1511 ( ( $zy == 1582 ) && ( $zm == 10 ) && ( $zd > 14 ) )
1512 ) {
1513 $zjd = (int)( ( 1461 * ( $zy + 4800 + (int)( ( $zm - 14 ) / 12 ) ) ) / 4 ) +
1514 (int)( ( 367 * ( $zm - 2 - 12 * ( (int)( ( $zm - 14 ) / 12 ) ) ) ) / 12 ) -
1515 (int)( ( 3 * (int)( ( ( $zy + 4900 + (int)( ( $zm - 14 ) / 12 ) ) / 100 ) ) ) / 4 ) +
1516 $zd - 32075;
1517 } else {
1518 $zjd = 367 * $zy - (int)( ( 7 * ( $zy + 5001 + (int)( ( $zm - 9 ) / 7 ) ) ) / 4 ) +
1519 (int)( ( 275 * $zm ) / 9 ) + $zd + 1729777;
1520 }
1521
1522 $zl = $zjd -1948440 + 10632;
1523 $zn = (int)( ( $zl - 1 ) / 10631 );
1524 $zl = $zl - 10631 * $zn + 354;
1525 $zj = ( (int)( ( 10985 - $zl ) / 5316 ) ) * ( (int)( ( 50 * $zl ) / 17719 ) ) + ( (int)( $zl / 5670 ) ) * ( (int)( ( 43 * $zl ) / 15238 ) );
1526 $zl = $zl - ( (int)( ( 30 - $zj ) / 15 ) ) * ( (int)( ( 17719 * $zj ) / 50 ) ) - ( (int)( $zj / 16 ) ) * ( (int)( ( 15238 * $zj ) / 43 ) ) + 29;
1527 $zm = (int)( ( 24 * $zl ) / 709 );
1528 $zd = $zl - (int)( ( 709 * $zm ) / 24 );
1529 $zy = 30 * $zn + $zj - 30;
1530
1531 return array( $zy, $zm, $zd );
1532 }
1533
1534 /**
1535 * Converting Gregorian dates to Hebrew dates.
1536 *
1537 * Based on a JavaScript code by Abu Mami and Yisrael Hersch
1538 * (abu-mami@kaluach.net, http://www.kaluach.net), who permitted
1539 * to translate the relevant functions into PHP and release them under
1540 * GNU GPL.
1541 *
1542 * The months are counted from Tishrei = 1. In a leap year, Adar I is 13
1543 * and Adar II is 14. In a non-leap year, Adar is 6.
1544 *
1545 * @param $ts string
1546 *
1547 * @return string
1548 */
1549 private static function tsToHebrew( $ts ) {
1550 # Parse date
1551 $year = substr( $ts, 0, 4 );
1552 $month = substr( $ts, 4, 2 );
1553 $day = substr( $ts, 6, 2 );
1554
1555 # Calculate Hebrew year
1556 $hebrewYear = $year + 3760;
1557
1558 # Month number when September = 1, August = 12
1559 $month += 4;
1560 if ( $month > 12 ) {
1561 # Next year
1562 $month -= 12;
1563 $year++;
1564 $hebrewYear++;
1565 }
1566
1567 # Calculate day of year from 1 September
1568 $dayOfYear = $day;
1569 for ( $i = 1; $i < $month; $i++ ) {
1570 if ( $i == 6 ) {
1571 # February
1572 $dayOfYear += 28;
1573 # Check if the year is leap
1574 if ( $year % 400 == 0 || ( $year % 4 == 0 && $year % 100 > 0 ) ) {
1575 $dayOfYear++;
1576 }
1577 } elseif ( $i == 8 || $i == 10 || $i == 1 || $i == 3 ) {
1578 $dayOfYear += 30;
1579 } else {
1580 $dayOfYear += 31;
1581 }
1582 }
1583
1584 # Calculate the start of the Hebrew year
1585 $start = self::hebrewYearStart( $hebrewYear );
1586
1587 # Calculate next year's start
1588 if ( $dayOfYear <= $start ) {
1589 # Day is before the start of the year - it is the previous year
1590 # Next year's start
1591 $nextStart = $start;
1592 # Previous year
1593 $year--;
1594 $hebrewYear--;
1595 # Add days since previous year's 1 September
1596 $dayOfYear += 365;
1597 if ( ( $year % 400 == 0 ) || ( $year % 100 != 0 && $year % 4 == 0 ) ) {
1598 # Leap year
1599 $dayOfYear++;
1600 }
1601 # Start of the new (previous) year
1602 $start = self::hebrewYearStart( $hebrewYear );
1603 } else {
1604 # Next year's start
1605 $nextStart = self::hebrewYearStart( $hebrewYear + 1 );
1606 }
1607
1608 # Calculate Hebrew day of year
1609 $hebrewDayOfYear = $dayOfYear - $start;
1610
1611 # Difference between year's days
1612 $diff = $nextStart - $start;
1613 # Add 12 (or 13 for leap years) days to ignore the difference between
1614 # Hebrew and Gregorian year (353 at least vs. 365/6) - now the
1615 # difference is only about the year type
1616 if ( ( $year % 400 == 0 ) || ( $year % 100 != 0 && $year % 4 == 0 ) ) {
1617 $diff += 13;
1618 } else {
1619 $diff += 12;
1620 }
1621
1622 # Check the year pattern, and is leap year
1623 # 0 means an incomplete year, 1 means a regular year, 2 means a complete year
1624 # This is mod 30, to work on both leap years (which add 30 days of Adar I)
1625 # and non-leap years
1626 $yearPattern = $diff % 30;
1627 # Check if leap year
1628 $isLeap = $diff >= 30;
1629
1630 # Calculate day in the month from number of day in the Hebrew year
1631 # Don't check Adar - if the day is not in Adar, we will stop before;
1632 # if it is in Adar, we will use it to check if it is Adar I or Adar II
1633 $hebrewDay = $hebrewDayOfYear;
1634 $hebrewMonth = 1;
1635 $days = 0;
1636 while ( $hebrewMonth <= 12 ) {
1637 # Calculate days in this month
1638 if ( $isLeap && $hebrewMonth == 6 ) {
1639 # Adar in a leap year
1640 if ( $isLeap ) {
1641 # Leap year - has Adar I, with 30 days, and Adar II, with 29 days
1642 $days = 30;
1643 if ( $hebrewDay <= $days ) {
1644 # Day in Adar I
1645 $hebrewMonth = 13;
1646 } else {
1647 # Subtract the days of Adar I
1648 $hebrewDay -= $days;
1649 # Try Adar II
1650 $days = 29;
1651 if ( $hebrewDay <= $days ) {
1652 # Day in Adar II
1653 $hebrewMonth = 14;
1654 }
1655 }
1656 }
1657 } elseif ( $hebrewMonth == 2 && $yearPattern == 2 ) {
1658 # Cheshvan in a complete year (otherwise as the rule below)
1659 $days = 30;
1660 } elseif ( $hebrewMonth == 3 && $yearPattern == 0 ) {
1661 # Kislev in an incomplete year (otherwise as the rule below)
1662 $days = 29;
1663 } else {
1664 # Odd months have 30 days, even have 29
1665 $days = 30 - ( $hebrewMonth - 1 ) % 2;
1666 }
1667 if ( $hebrewDay <= $days ) {
1668 # In the current month
1669 break;
1670 } else {
1671 # Subtract the days of the current month
1672 $hebrewDay -= $days;
1673 # Try in the next month
1674 $hebrewMonth++;
1675 }
1676 }
1677
1678 return array( $hebrewYear, $hebrewMonth, $hebrewDay, $days );
1679 }
1680
1681 /**
1682 * This calculates the Hebrew year start, as days since 1 September.
1683 * Based on Carl Friedrich Gauss algorithm for finding Easter date.
1684 * Used for Hebrew date.
1685 *
1686 * @param $year int
1687 *
1688 * @return string
1689 */
1690 private static function hebrewYearStart( $year ) {
1691 $a = intval( ( 12 * ( $year - 1 ) + 17 ) % 19 );
1692 $b = intval( ( $year - 1 ) % 4 );
1693 $m = 32.044093161144 + 1.5542417966212 * $a + $b / 4.0 - 0.0031777940220923 * ( $year - 1 );
1694 if ( $m < 0 ) {
1695 $m--;
1696 }
1697 $Mar = intval( $m );
1698 if ( $m < 0 ) {
1699 $m++;
1700 }
1701 $m -= $Mar;
1702
1703 $c = intval( ( $Mar + 3 * ( $year - 1 ) + 5 * $b + 5 ) % 7 );
1704 if ( $c == 0 && $a > 11 && $m >= 0.89772376543210 ) {
1705 $Mar++;
1706 } elseif ( $c == 1 && $a > 6 && $m >= 0.63287037037037 ) {
1707 $Mar += 2;
1708 } elseif ( $c == 2 || $c == 4 || $c == 6 ) {
1709 $Mar++;
1710 }
1711
1712 $Mar += intval( ( $year - 3761 ) / 100 ) - intval( ( $year - 3761 ) / 400 ) - 24;
1713 return $Mar;
1714 }
1715
1716 /**
1717 * Algorithm to convert Gregorian dates to Thai solar dates,
1718 * Minguo dates or Minguo dates.
1719 *
1720 * Link: http://en.wikipedia.org/wiki/Thai_solar_calendar
1721 * http://en.wikipedia.org/wiki/Minguo_calendar
1722 * http://en.wikipedia.org/wiki/Japanese_era_name
1723 *
1724 * @param $ts String: 14-character timestamp
1725 * @param $cName String: calender name
1726 * @return Array: converted year, month, day
1727 */
1728 private static function tsToYear( $ts, $cName ) {
1729 $gy = substr( $ts, 0, 4 );
1730 $gm = substr( $ts, 4, 2 );
1731 $gd = substr( $ts, 6, 2 );
1732
1733 if ( !strcmp( $cName, 'thai' ) ) {
1734 # Thai solar dates
1735 # Add 543 years to the Gregorian calendar
1736 # Months and days are identical
1737 $gy_offset = $gy + 543;
1738 } elseif ( ( !strcmp( $cName, 'minguo' ) ) || !strcmp( $cName, 'juche' ) ) {
1739 # Minguo dates
1740 # Deduct 1911 years from the Gregorian calendar
1741 # Months and days are identical
1742 $gy_offset = $gy - 1911;
1743 } elseif ( !strcmp( $cName, 'tenno' ) ) {
1744 # Nengō dates up to Meiji period
1745 # Deduct years from the Gregorian calendar
1746 # depending on the nengo periods
1747 # Months and days are identical
1748 if ( ( $gy < 1912 ) || ( ( $gy == 1912 ) && ( $gm < 7 ) ) || ( ( $gy == 1912 ) && ( $gm == 7 ) && ( $gd < 31 ) ) ) {
1749 # Meiji period
1750 $gy_gannen = $gy - 1868 + 1;
1751 $gy_offset = $gy_gannen;
1752 if ( $gy_gannen == 1 ) {
1753 $gy_offset = '元';
1754 }
1755 $gy_offset = '明治' . $gy_offset;
1756 } elseif (
1757 ( ( $gy == 1912 ) && ( $gm == 7 ) && ( $gd == 31 ) ) ||
1758 ( ( $gy == 1912 ) && ( $gm >= 8 ) ) ||
1759 ( ( $gy > 1912 ) && ( $gy < 1926 ) ) ||
1760 ( ( $gy == 1926 ) && ( $gm < 12 ) ) ||
1761 ( ( $gy == 1926 ) && ( $gm == 12 ) && ( $gd < 26 ) )
1762 ) {
1763 # Taishō period
1764 $gy_gannen = $gy - 1912 + 1;
1765 $gy_offset = $gy_gannen;
1766 if ( $gy_gannen == 1 ) {
1767 $gy_offset = '元';
1768 }
1769 $gy_offset = '大正' . $gy_offset;
1770 } elseif (
1771 ( ( $gy == 1926 ) && ( $gm == 12 ) && ( $gd >= 26 ) ) ||
1772 ( ( $gy > 1926 ) && ( $gy < 1989 ) ) ||
1773 ( ( $gy == 1989 ) && ( $gm == 1 ) && ( $gd < 8 ) )
1774 ) {
1775 # Shōwa period
1776 $gy_gannen = $gy - 1926 + 1;
1777 $gy_offset = $gy_gannen;
1778 if ( $gy_gannen == 1 ) {
1779 $gy_offset = '元';
1780 }
1781 $gy_offset = '昭和' . $gy_offset;
1782 } else {
1783 # Heisei period
1784 $gy_gannen = $gy - 1989 + 1;
1785 $gy_offset = $gy_gannen;
1786 if ( $gy_gannen == 1 ) {
1787 $gy_offset = '元';
1788 }
1789 $gy_offset = '平成' . $gy_offset;
1790 }
1791 } else {
1792 $gy_offset = $gy;
1793 }
1794
1795 return array( $gy_offset, $gm, $gd );
1796 }
1797
1798 /**
1799 * Roman number formatting up to 10000
1800 *
1801 * @param $num int
1802 *
1803 * @return string
1804 */
1805 static function romanNumeral( $num ) {
1806 static $table = array(
1807 array( '', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X' ),
1808 array( '', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC', 'C' ),
1809 array( '', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM', 'M' ),
1810 array( '', 'M', 'MM', 'MMM', 'MMMM', 'MMMMM', 'MMMMMM', 'MMMMMMM', 'MMMMMMMM', 'MMMMMMMMM', 'MMMMMMMMMM' )
1811 );
1812
1813 $num = intval( $num );
1814 if ( $num > 10000 || $num <= 0 ) {
1815 return $num;
1816 }
1817
1818 $s = '';
1819 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1820 if ( $num >= $pow10 ) {
1821 $s .= $table[$i][(int)floor( $num / $pow10 )];
1822 }
1823 $num = $num % $pow10;
1824 }
1825 return $s;
1826 }
1827
1828 /**
1829 * Hebrew Gematria number formatting up to 9999
1830 *
1831 * @param $num int
1832 *
1833 * @return string
1834 */
1835 static function hebrewNumeral( $num ) {
1836 static $table = array(
1837 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' ),
1838 array( '', 'י', 'כ', 'ל', 'מ', 'נ', 'ס', 'ע', 'פ', 'צ', 'ק' ),
1839 array( '', 'ק', 'ר', 'ש', 'ת', 'תק', 'תר', 'תש', 'תת', 'תתק', 'תתר' ),
1840 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' )
1841 );
1842
1843 $num = intval( $num );
1844 if ( $num > 9999 || $num <= 0 ) {
1845 return $num;
1846 }
1847
1848 $s = '';
1849 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1850 if ( $num >= $pow10 ) {
1851 if ( $num == 15 || $num == 16 ) {
1852 $s .= $table[0][9] . $table[0][$num - 9];
1853 $num = 0;
1854 } else {
1855 $s .= $table[$i][intval( ( $num / $pow10 ) )];
1856 if ( $pow10 == 1000 ) {
1857 $s .= "'";
1858 }
1859 }
1860 }
1861 $num = $num % $pow10;
1862 }
1863 if ( strlen( $s ) == 2 ) {
1864 $str = $s . "'";
1865 } else {
1866 $str = substr( $s, 0, strlen( $s ) - 2 ) . '"';
1867 $str .= substr( $s, strlen( $s ) - 2, 2 );
1868 }
1869 $start = substr( $str, 0, strlen( $str ) - 2 );
1870 $end = substr( $str, strlen( $str ) - 2 );
1871 switch ( $end ) {
1872 case 'כ':
1873 $str = $start . 'ך';
1874 break;
1875 case 'מ':
1876 $str = $start . 'ם';
1877 break;
1878 case 'נ':
1879 $str = $start . 'ן';
1880 break;
1881 case 'פ':
1882 $str = $start . 'ף';
1883 break;
1884 case 'צ':
1885 $str = $start . 'ץ';
1886 break;
1887 }
1888 return $str;
1889 }
1890
1891 /**
1892 * Used by date() and time() to adjust the time output.
1893 *
1894 * @param $ts Int the time in date('YmdHis') format
1895 * @param $tz Mixed: adjust the time by this amount (default false, mean we
1896 * get user timecorrection setting)
1897 * @return int
1898 */
1899 function userAdjust( $ts, $tz = false ) {
1900 global $wgUser, $wgLocalTZoffset;
1901
1902 if ( $tz === false ) {
1903 $tz = $wgUser->getOption( 'timecorrection' );
1904 }
1905
1906 $data = explode( '|', $tz, 3 );
1907
1908 if ( $data[0] == 'ZoneInfo' ) {
1909 wfSuppressWarnings();
1910 $userTZ = timezone_open( $data[2] );
1911 wfRestoreWarnings();
1912 if ( $userTZ !== false ) {
1913 $date = date_create( $ts, timezone_open( 'UTC' ) );
1914 date_timezone_set( $date, $userTZ );
1915 $date = date_format( $date, 'YmdHis' );
1916 return $date;
1917 }
1918 # Unrecognized timezone, default to 'Offset' with the stored offset.
1919 $data[0] = 'Offset';
1920 }
1921
1922 $minDiff = 0;
1923 if ( $data[0] == 'System' || $tz == '' ) {
1924 #  Global offset in minutes.
1925 if ( isset( $wgLocalTZoffset ) ) {
1926 $minDiff = $wgLocalTZoffset;
1927 }
1928 } elseif ( $data[0] == 'Offset' ) {
1929 $minDiff = intval( $data[1] );
1930 } else {
1931 $data = explode( ':', $tz );
1932 if ( count( $data ) == 2 ) {
1933 $data[0] = intval( $data[0] );
1934 $data[1] = intval( $data[1] );
1935 $minDiff = abs( $data[0] ) * 60 + $data[1];
1936 if ( $data[0] < 0 ) {
1937 $minDiff = -$minDiff;
1938 }
1939 } else {
1940 $minDiff = intval( $data[0] ) * 60;
1941 }
1942 }
1943
1944 # No difference ? Return time unchanged
1945 if ( 0 == $minDiff ) {
1946 return $ts;
1947 }
1948
1949 wfSuppressWarnings(); // E_STRICT system time bitching
1950 # Generate an adjusted date; take advantage of the fact that mktime
1951 # will normalize out-of-range values so we don't have to split $minDiff
1952 # into hours and minutes.
1953 $t = mktime( (
1954 (int)substr( $ts, 8, 2 ) ), # Hours
1955 (int)substr( $ts, 10, 2 ) + $minDiff, # Minutes
1956 (int)substr( $ts, 12, 2 ), # Seconds
1957 (int)substr( $ts, 4, 2 ), # Month
1958 (int)substr( $ts, 6, 2 ), # Day
1959 (int)substr( $ts, 0, 4 ) ); # Year
1960
1961 $date = date( 'YmdHis', $t );
1962 wfRestoreWarnings();
1963
1964 return $date;
1965 }
1966
1967 /**
1968 * This is meant to be used by time(), date(), and timeanddate() to get
1969 * the date preference they're supposed to use, it should be used in
1970 * all children.
1971 *
1972 *<code>
1973 * function timeanddate([...], $format = true) {
1974 * $datePreference = $this->dateFormat($format);
1975 * [...]
1976 * }
1977 *</code>
1978 *
1979 * @param $usePrefs Mixed: if true, the user's preference is used
1980 * if false, the site/language default is used
1981 * if int/string, assumed to be a format.
1982 * @return string
1983 */
1984 function dateFormat( $usePrefs = true ) {
1985 global $wgUser;
1986
1987 if ( is_bool( $usePrefs ) ) {
1988 if ( $usePrefs ) {
1989 $datePreference = $wgUser->getDatePreference();
1990 } else {
1991 $datePreference = (string)User::getDefaultOption( 'date' );
1992 }
1993 } else {
1994 $datePreference = (string)$usePrefs;
1995 }
1996
1997 // return int
1998 if ( $datePreference == '' ) {
1999 return 'default';
2000 }
2001
2002 return $datePreference;
2003 }
2004
2005 /**
2006 * Get a format string for a given type and preference
2007 * @param $type string May be date, time or both
2008 * @param $pref string The format name as it appears in Messages*.php
2009 *
2010 * @since 1.22 New type 'pretty' that provides a more readable timestamp format
2011 *
2012 * @return string
2013 */
2014 function getDateFormatString( $type, $pref ) {
2015 if ( !isset( $this->dateFormatStrings[$type][$pref] ) ) {
2016 if ( $pref == 'default' ) {
2017 $pref = $this->getDefaultDateFormat();
2018 $df = self::$dataCache->getSubitem( $this->mCode, 'dateFormats', "$pref $type" );
2019 } else {
2020 $df = self::$dataCache->getSubitem( $this->mCode, 'dateFormats', "$pref $type" );
2021
2022 if ( $type === 'pretty' && $df === null ) {
2023 $df = $this->getDateFormatString( 'date', $pref );
2024 }
2025
2026 if ( $df === null ) {
2027 $pref = $this->getDefaultDateFormat();
2028 $df = self::$dataCache->getSubitem( $this->mCode, 'dateFormats', "$pref $type" );
2029 }
2030 }
2031 $this->dateFormatStrings[$type][$pref] = $df;
2032 }
2033 return $this->dateFormatStrings[$type][$pref];
2034 }
2035
2036 /**
2037 * @param $ts Mixed: the time format which needs to be turned into a
2038 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2039 * @param $adj Bool: whether to adjust the time output according to the
2040 * user configured offset ($timecorrection)
2041 * @param $format Mixed: true to use user's date format preference
2042 * @param $timecorrection String|bool the time offset as returned by
2043 * validateTimeZone() in Special:Preferences
2044 * @return string
2045 */
2046 function date( $ts, $adj = false, $format = true, $timecorrection = false ) {
2047 $ts = wfTimestamp( TS_MW, $ts );
2048 if ( $adj ) {
2049 $ts = $this->userAdjust( $ts, $timecorrection );
2050 }
2051 $df = $this->getDateFormatString( 'date', $this->dateFormat( $format ) );
2052 return $this->sprintfDate( $df, $ts );
2053 }
2054
2055 /**
2056 * @param $ts Mixed: the time format which needs to be turned into a
2057 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2058 * @param $adj Bool: whether to adjust the time output according to the
2059 * user configured offset ($timecorrection)
2060 * @param $format Mixed: true to use user's date format preference
2061 * @param $timecorrection String|bool the time offset as returned by
2062 * validateTimeZone() in Special:Preferences
2063 * @return string
2064 */
2065 function time( $ts, $adj = false, $format = true, $timecorrection = false ) {
2066 $ts = wfTimestamp( TS_MW, $ts );
2067 if ( $adj ) {
2068 $ts = $this->userAdjust( $ts, $timecorrection );
2069 }
2070 $df = $this->getDateFormatString( 'time', $this->dateFormat( $format ) );
2071 return $this->sprintfDate( $df, $ts );
2072 }
2073
2074 /**
2075 * @param $ts Mixed: the time format which needs to be turned into a
2076 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2077 * @param $adj Bool: whether to adjust the time output according to the
2078 * user configured offset ($timecorrection)
2079 * @param $format Mixed: what format to return, if it's false output the
2080 * default one (default true)
2081 * @param $timecorrection String|bool the time offset as returned by
2082 * validateTimeZone() in Special:Preferences
2083 * @return string
2084 */
2085 function timeanddate( $ts, $adj = false, $format = true, $timecorrection = false ) {
2086 $ts = wfTimestamp( TS_MW, $ts );
2087 if ( $adj ) {
2088 $ts = $this->userAdjust( $ts, $timecorrection );
2089 }
2090 $df = $this->getDateFormatString( 'both', $this->dateFormat( $format ) );
2091 return $this->sprintfDate( $df, $ts );
2092 }
2093
2094 /**
2095 * Takes a number of seconds and turns it into a text using values such as hours and minutes.
2096 *
2097 * @since 1.20
2098 *
2099 * @param integer $seconds The amount of seconds.
2100 * @param array $chosenIntervals The intervals to enable.
2101 *
2102 * @return string
2103 */
2104 public function formatDuration( $seconds, array $chosenIntervals = array() ) {
2105 $intervals = $this->getDurationIntervals( $seconds, $chosenIntervals );
2106
2107 $segments = array();
2108
2109 foreach ( $intervals as $intervalName => $intervalValue ) {
2110 // Messages: duration-seconds, duration-minutes, duration-hours, duration-days, duration-weeks,
2111 // duration-years, duration-decades, duration-centuries, duration-millennia
2112 $message = wfMessage( 'duration-' . $intervalName )->numParams( $intervalValue );
2113 $segments[] = $message->inLanguage( $this )->escaped();
2114 }
2115
2116 return $this->listToText( $segments );
2117 }
2118
2119 /**
2120 * Takes a number of seconds and returns an array with a set of corresponding intervals.
2121 * For example 65 will be turned into array( minutes => 1, seconds => 5 ).
2122 *
2123 * @since 1.20
2124 *
2125 * @param integer $seconds The amount of seconds.
2126 * @param array $chosenIntervals The intervals to enable.
2127 *
2128 * @return array
2129 */
2130 public function getDurationIntervals( $seconds, array $chosenIntervals = array() ) {
2131 if ( empty( $chosenIntervals ) ) {
2132 $chosenIntervals = array( 'millennia', 'centuries', 'decades', 'years', 'days', 'hours', 'minutes', 'seconds' );
2133 }
2134
2135 $intervals = array_intersect_key( self::$durationIntervals, array_flip( $chosenIntervals ) );
2136 $sortedNames = array_keys( $intervals );
2137 $smallestInterval = array_pop( $sortedNames );
2138
2139 $segments = array();
2140
2141 foreach ( $intervals as $name => $length ) {
2142 $value = floor( $seconds / $length );
2143
2144 if ( $value > 0 || ( $name == $smallestInterval && empty( $segments ) ) ) {
2145 $seconds -= $value * $length;
2146 $segments[$name] = $value;
2147 }
2148 }
2149
2150 return $segments;
2151 }
2152
2153 /**
2154 * Internal helper function for userDate(), userTime() and userTimeAndDate()
2155 *
2156 * @param $type String: can be 'date', 'time' or 'both'
2157 * @param $ts Mixed: the time format which needs to be turned into a
2158 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2159 * @param $user User object used to get preferences for timezone and format
2160 * @param $options Array, can contain the following keys:
2161 * - 'timecorrection': time correction, can have the following values:
2162 * - true: use user's preference
2163 * - false: don't use time correction
2164 * - integer: value of time correction in minutes
2165 * - 'format': format to use, can have the following values:
2166 * - true: use user's preference
2167 * - false: use default preference
2168 * - string: format to use
2169 * @since 1.19
2170 * @return String
2171 */
2172 private function internalUserTimeAndDate( $type, $ts, User $user, array $options ) {
2173 $ts = wfTimestamp( TS_MW, $ts );
2174 $options += array( 'timecorrection' => true, 'format' => true );
2175 if ( $options['timecorrection'] !== false ) {
2176 if ( $options['timecorrection'] === true ) {
2177 $offset = $user->getOption( 'timecorrection' );
2178 } else {
2179 $offset = $options['timecorrection'];
2180 }
2181 $ts = $this->userAdjust( $ts, $offset );
2182 }
2183 if ( $options['format'] === true ) {
2184 $format = $user->getDatePreference();
2185 } else {
2186 $format = $options['format'];
2187 }
2188 $df = $this->getDateFormatString( $type, $this->dateFormat( $format ) );
2189 return $this->sprintfDate( $df, $ts );
2190 }
2191
2192 /**
2193 * Get the formatted date for the given timestamp and formatted for
2194 * the given user.
2195 *
2196 * @param $ts Mixed: the time format which needs to be turned into a
2197 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2198 * @param $user User object used to get preferences for timezone and format
2199 * @param $options Array, can contain the following keys:
2200 * - 'timecorrection': time correction, can have the following values:
2201 * - true: use user's preference
2202 * - false: don't use time correction
2203 * - integer: value of time correction in minutes
2204 * - 'format': format to use, can have the following values:
2205 * - true: use user's preference
2206 * - false: use default preference
2207 * - string: format to use
2208 * @since 1.19
2209 * @return String
2210 */
2211 public function userDate( $ts, User $user, array $options = array() ) {
2212 return $this->internalUserTimeAndDate( 'date', $ts, $user, $options );
2213 }
2214
2215 /**
2216 * Get the formatted time for the given timestamp and formatted for
2217 * the given user.
2218 *
2219 * @param $ts Mixed: the time format which needs to be turned into a
2220 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2221 * @param $user User object used to get preferences for timezone and format
2222 * @param $options Array, can contain the following keys:
2223 * - 'timecorrection': time correction, can have the following values:
2224 * - true: use user's preference
2225 * - false: don't use time correction
2226 * - integer: value of time correction in minutes
2227 * - 'format': format to use, can have the following values:
2228 * - true: use user's preference
2229 * - false: use default preference
2230 * - string: format to use
2231 * @since 1.19
2232 * @return String
2233 */
2234 public function userTime( $ts, User $user, array $options = array() ) {
2235 return $this->internalUserTimeAndDate( 'time', $ts, $user, $options );
2236 }
2237
2238 /**
2239 * Get the formatted date and time for the given timestamp and formatted for
2240 * the given user.
2241 *
2242 * @param $ts Mixed: the time format which needs to be turned into a
2243 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2244 * @param $user User object used to get preferences for timezone and format
2245 * @param $options Array, can contain the following keys:
2246 * - 'timecorrection': time correction, can have the following values:
2247 * - true: use user's preference
2248 * - false: don't use time correction
2249 * - integer: value of time correction in minutes
2250 * - 'format': format to use, can have the following values:
2251 * - true: use user's preference
2252 * - false: use default preference
2253 * - string: format to use
2254 * @since 1.19
2255 * @return String
2256 */
2257 public function userTimeAndDate( $ts, User $user, array $options = array() ) {
2258 return $this->internalUserTimeAndDate( 'both', $ts, $user, $options );
2259 }
2260
2261 /**
2262 * Convert an MWTimestamp into a pretty human-readable timestamp using
2263 * the given user preferences and relative base time.
2264 *
2265 * DO NOT USE THIS FUNCTION DIRECTLY. Instead, call MWTimestamp::getHumanTimestamp
2266 * on your timestamp object, which will then call this function. Calling
2267 * this function directly will cause hooks to be skipped over.
2268 *
2269 * @see MWTimestamp::getHumanTimestamp
2270 * @param MWTimestamp $ts Timestamp to prettify
2271 * @param MWTimestamp $relativeTo Base timestamp
2272 * @param User $user User preferences to use
2273 * @return string Human timestamp
2274 * @since 1.22
2275 */
2276 public function getHumanTimestamp( MWTimestamp $ts, MWTimestamp $relativeTo, User $user ) {
2277 $diff = $ts->diff( $relativeTo );
2278 $diffDay = (bool)( (int)$ts->timestamp->format( 'w' ) - (int)$relativeTo->timestamp->format( 'w' ) );
2279 $days = $diff->days ?: (int)$diffDay;
2280 if ( $diff->invert || $days > 5 && $ts->timestamp->format( 'Y' ) !== $relativeTo->timestamp->format( 'Y' ) ) {
2281 // Timestamps are in different years: use full timestamp
2282 // Also do full timestamp for future dates
2283 /**
2284 * @FIXME Add better handling of future timestamps.
2285 */
2286 $format = $this->getDateFormatString( 'both', $user->getDatePreference() ?: 'default' );
2287 $ts = $this->sprintfDate( $format, $ts->getTimestamp( TS_MW ) );
2288 } elseif ( $days > 5 ) {
2289 // Timestamps are in same year, but more than 5 days ago: show day and month only.
2290 $format = $this->getDateFormatString( 'pretty', $user->getDatePreference() ?: 'default' );
2291 $ts = $this->sprintfDate( $format, $ts->getTimestamp( TS_MW ) );
2292 } elseif ( $days > 1 ) {
2293 // Timestamp within the past week: show the day of the week and time
2294 $format = $this->getDateFormatString( 'time', $user->getDatePreference() ?: 'default' );
2295 $weekday = self::$mWeekdayMsgs[$ts->timestamp->format( 'w' )];
2296 // Messages:
2297 // sunday-at, monday-at, tuesday-at, wednesday-at, thursday-at, friday-at, saturday-at
2298 $ts = wfMessage( "$weekday-at" )
2299 ->inLanguage( $this )
2300 ->params( $this->sprintfDate( $format, $ts->getTimestamp( TS_MW ) ) )
2301 ->text();
2302 } elseif ( $days == 1 ) {
2303 // Timestamp was yesterday: say 'yesterday' and the time.
2304 $format = $this->getDateFormatString( 'time', $user->getDatePreference() ?: 'default' );
2305 $ts = wfMessage( 'yesterday-at' )
2306 ->inLanguage( $this )
2307 ->params( $this->sprintfDate( $format, $ts->getTimestamp( TS_MW ) ) )
2308 ->text();
2309 } elseif ( $diff->h > 1 || $diff->h == 1 && $diff->i > 30 ) {
2310 // Timestamp was today, but more than 90 minutes ago: say 'today' and the time.
2311 $format = $this->getDateFormatString( 'time', $user->getDatePreference() ?: 'default' );
2312 $ts = wfMessage( 'today-at' )
2313 ->inLanguage( $this )
2314 ->params( $this->sprintfDate( $format, $ts->getTimestamp( TS_MW ) ) )
2315 ->text();
2316
2317 // From here on in, the timestamp was soon enough ago so that we can simply say
2318 // XX units ago, e.g., "2 hours ago" or "5 minutes ago"
2319 } elseif ( $diff->h == 1 ) {
2320 // Less than 90 minutes, but more than an hour ago.
2321 $ts = wfMessage( 'hours-ago' )->inLanguage( $this )->numParams( 1 )->text();
2322 } elseif ( $diff->i >= 1 ) {
2323 // A few minutes ago.
2324 $ts = wfMessage( 'minutes-ago' )->inLanguage( $this )->numParams( $diff->i )->text();
2325 } elseif ( $diff->s >= 30 ) {
2326 // Less than a minute, but more than 30 sec ago.
2327 $ts = wfMessage( 'seconds-ago' )->inLanguage( $this )->numParams( $diff->s )->text();
2328 } else {
2329 // Less than 30 seconds ago.
2330 $ts = wfMessage( 'just-now' )->text();
2331 }
2332
2333 return $ts;
2334 }
2335
2336 /**
2337 * @param $key string
2338 * @return array|null
2339 */
2340 function getMessage( $key ) {
2341 return self::$dataCache->getSubitem( $this->mCode, 'messages', $key );
2342 }
2343
2344 /**
2345 * @return array
2346 */
2347 function getAllMessages() {
2348 return self::$dataCache->getItem( $this->mCode, 'messages' );
2349 }
2350
2351 /**
2352 * @param $in
2353 * @param $out
2354 * @param $string
2355 * @return string
2356 */
2357 function iconv( $in, $out, $string ) {
2358 # This is a wrapper for iconv in all languages except esperanto,
2359 # which does some nasty x-conversions beforehand
2360
2361 # Even with //IGNORE iconv can whine about illegal characters in
2362 # *input* string. We just ignore those too.
2363 # REF: http://bugs.php.net/bug.php?id=37166
2364 # REF: https://bugzilla.wikimedia.org/show_bug.cgi?id=16885
2365 wfSuppressWarnings();
2366 $text = iconv( $in, $out . '//IGNORE', $string );
2367 wfRestoreWarnings();
2368 return $text;
2369 }
2370
2371 // callback functions for uc(), lc(), ucwords(), ucwordbreaks()
2372
2373 /**
2374 * @param $matches array
2375 * @return mixed|string
2376 */
2377 function ucwordbreaksCallbackAscii( $matches ) {
2378 return $this->ucfirst( $matches[1] );
2379 }
2380
2381 /**
2382 * @param $matches array
2383 * @return string
2384 */
2385 function ucwordbreaksCallbackMB( $matches ) {
2386 return mb_strtoupper( $matches[0] );
2387 }
2388
2389 /**
2390 * @param $matches array
2391 * @return string
2392 */
2393 function ucCallback( $matches ) {
2394 list( $wikiUpperChars ) = self::getCaseMaps();
2395 return strtr( $matches[1], $wikiUpperChars );
2396 }
2397
2398 /**
2399 * @param $matches array
2400 * @return string
2401 */
2402 function lcCallback( $matches ) {
2403 list( , $wikiLowerChars ) = self::getCaseMaps();
2404 return strtr( $matches[1], $wikiLowerChars );
2405 }
2406
2407 /**
2408 * @param $matches array
2409 * @return string
2410 */
2411 function ucwordsCallbackMB( $matches ) {
2412 return mb_strtoupper( $matches[0] );
2413 }
2414
2415 /**
2416 * @param $matches array
2417 * @return string
2418 */
2419 function ucwordsCallbackWiki( $matches ) {
2420 list( $wikiUpperChars ) = self::getCaseMaps();
2421 return strtr( $matches[0], $wikiUpperChars );
2422 }
2423
2424 /**
2425 * Make a string's first character uppercase
2426 *
2427 * @param $str string
2428 *
2429 * @return string
2430 */
2431 function ucfirst( $str ) {
2432 $o = ord( $str );
2433 if ( $o < 96 ) { // if already uppercase...
2434 return $str;
2435 } elseif ( $o < 128 ) {
2436 return ucfirst( $str ); // use PHP's ucfirst()
2437 } else {
2438 // fall back to more complex logic in case of multibyte strings
2439 return $this->uc( $str, true );
2440 }
2441 }
2442
2443 /**
2444 * Convert a string to uppercase
2445 *
2446 * @param $str string
2447 * @param $first bool
2448 *
2449 * @return string
2450 */
2451 function uc( $str, $first = false ) {
2452 if ( function_exists( 'mb_strtoupper' ) ) {
2453 if ( $first ) {
2454 if ( $this->isMultibyte( $str ) ) {
2455 return mb_strtoupper( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
2456 } else {
2457 return ucfirst( $str );
2458 }
2459 } else {
2460 return $this->isMultibyte( $str ) ? mb_strtoupper( $str ) : strtoupper( $str );
2461 }
2462 } else {
2463 if ( $this->isMultibyte( $str ) ) {
2464 $x = $first ? '^' : '';
2465 return preg_replace_callback(
2466 "/$x([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
2467 array( $this, 'ucCallback' ),
2468 $str
2469 );
2470 } else {
2471 return $first ? ucfirst( $str ) : strtoupper( $str );
2472 }
2473 }
2474 }
2475
2476 /**
2477 * @param $str string
2478 * @return mixed|string
2479 */
2480 function lcfirst( $str ) {
2481 $o = ord( $str );
2482 if ( !$o ) {
2483 return strval( $str );
2484 } elseif ( $o >= 128 ) {
2485 return $this->lc( $str, true );
2486 } elseif ( $o > 96 ) {
2487 return $str;
2488 } else {
2489 $str[0] = strtolower( $str[0] );
2490 return $str;
2491 }
2492 }
2493
2494 /**
2495 * @param $str string
2496 * @param $first bool
2497 * @return mixed|string
2498 */
2499 function lc( $str, $first = false ) {
2500 if ( function_exists( 'mb_strtolower' ) ) {
2501 if ( $first ) {
2502 if ( $this->isMultibyte( $str ) ) {
2503 return mb_strtolower( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
2504 } else {
2505 return strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 );
2506 }
2507 } else {
2508 return $this->isMultibyte( $str ) ? mb_strtolower( $str ) : strtolower( $str );
2509 }
2510 } else {
2511 if ( $this->isMultibyte( $str ) ) {
2512 $x = $first ? '^' : '';
2513 return preg_replace_callback(
2514 "/$x([A-Z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
2515 array( $this, 'lcCallback' ),
2516 $str
2517 );
2518 } else {
2519 return $first ? strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 ) : strtolower( $str );
2520 }
2521 }
2522 }
2523
2524 /**
2525 * @param $str string
2526 * @return bool
2527 */
2528 function isMultibyte( $str ) {
2529 return (bool)preg_match( '/[\x80-\xff]/', $str );
2530 }
2531
2532 /**
2533 * @param $str string
2534 * @return mixed|string
2535 */
2536 function ucwords( $str ) {
2537 if ( $this->isMultibyte( $str ) ) {
2538 $str = $this->lc( $str );
2539
2540 // regexp to find first letter in each word (i.e. after each space)
2541 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)| ([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
2542
2543 // function to use to capitalize a single char
2544 if ( function_exists( 'mb_strtoupper' ) ) {
2545 return preg_replace_callback(
2546 $replaceRegexp,
2547 array( $this, 'ucwordsCallbackMB' ),
2548 $str
2549 );
2550 } else {
2551 return preg_replace_callback(
2552 $replaceRegexp,
2553 array( $this, 'ucwordsCallbackWiki' ),
2554 $str
2555 );
2556 }
2557 } else {
2558 return ucwords( strtolower( $str ) );
2559 }
2560 }
2561
2562 /**
2563 * capitalize words at word breaks
2564 *
2565 * @param $str string
2566 * @return mixed
2567 */
2568 function ucwordbreaks( $str ) {
2569 if ( $this->isMultibyte( $str ) ) {
2570 $str = $this->lc( $str );
2571
2572 // since \b doesn't work for UTF-8, we explicitely define word break chars
2573 $breaks = "[ \-\(\)\}\{\.,\?!]";
2574
2575 // find first letter after word break
2576 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)|$breaks([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
2577
2578 if ( function_exists( 'mb_strtoupper' ) ) {
2579 return preg_replace_callback(
2580 $replaceRegexp,
2581 array( $this, 'ucwordbreaksCallbackMB' ),
2582 $str
2583 );
2584 } else {
2585 return preg_replace_callback(
2586 $replaceRegexp,
2587 array( $this, 'ucwordsCallbackWiki' ),
2588 $str
2589 );
2590 }
2591 } else {
2592 return preg_replace_callback(
2593 '/\b([\w\x80-\xff]+)\b/',
2594 array( $this, 'ucwordbreaksCallbackAscii' ),
2595 $str
2596 );
2597 }
2598 }
2599
2600 /**
2601 * Return a case-folded representation of $s
2602 *
2603 * This is a representation such that caseFold($s1)==caseFold($s2) if $s1
2604 * and $s2 are the same except for the case of their characters. It is not
2605 * necessary for the value returned to make sense when displayed.
2606 *
2607 * Do *not* perform any other normalisation in this function. If a caller
2608 * uses this function when it should be using a more general normalisation
2609 * function, then fix the caller.
2610 *
2611 * @param $s string
2612 *
2613 * @return string
2614 */
2615 function caseFold( $s ) {
2616 return $this->uc( $s );
2617 }
2618
2619 /**
2620 * @param $s string
2621 * @return string
2622 */
2623 function checkTitleEncoding( $s ) {
2624 if ( is_array( $s ) ) {
2625 throw new MWException( 'Given array to checkTitleEncoding.' );
2626 }
2627 if ( StringUtils::isUtf8( $s ) ) {
2628 return $s;
2629 }
2630
2631 return $this->iconv( $this->fallback8bitEncoding(), 'utf-8', $s );
2632 }
2633
2634 /**
2635 * @return array
2636 */
2637 function fallback8bitEncoding() {
2638 return self::$dataCache->getItem( $this->mCode, 'fallback8bitEncoding' );
2639 }
2640
2641 /**
2642 * Most writing systems use whitespace to break up words.
2643 * Some languages such as Chinese don't conventionally do this,
2644 * which requires special handling when breaking up words for
2645 * searching etc.
2646 *
2647 * @return bool
2648 */
2649 function hasWordBreaks() {
2650 return true;
2651 }
2652
2653 /**
2654 * Some languages such as Chinese require word segmentation,
2655 * Specify such segmentation when overridden in derived class.
2656 *
2657 * @param $string String
2658 * @return String
2659 */
2660 function segmentByWord( $string ) {
2661 return $string;
2662 }
2663
2664 /**
2665 * Some languages have special punctuation need to be normalized.
2666 * Make such changes here.
2667 *
2668 * @param $string String
2669 * @return String
2670 */
2671 function normalizeForSearch( $string ) {
2672 return self::convertDoubleWidth( $string );
2673 }
2674
2675 /**
2676 * convert double-width roman characters to single-width.
2677 * range: ff00-ff5f ~= 0020-007f
2678 *
2679 * @param $string string
2680 *
2681 * @return string
2682 */
2683 protected static function convertDoubleWidth( $string ) {
2684 static $full = null;
2685 static $half = null;
2686
2687 if ( $full === null ) {
2688 $fullWidth = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
2689 $halfWidth = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
2690 $full = str_split( $fullWidth, 3 );
2691 $half = str_split( $halfWidth );
2692 }
2693
2694 $string = str_replace( $full, $half, $string );
2695 return $string;
2696 }
2697
2698 /**
2699 * @param $string string
2700 * @param $pattern string
2701 * @return string
2702 */
2703 protected static function insertSpace( $string, $pattern ) {
2704 $string = preg_replace( $pattern, " $1 ", $string );
2705 $string = preg_replace( '/ +/', ' ', $string );
2706 return $string;
2707 }
2708
2709 /**
2710 * @param $termsArray array
2711 * @return array
2712 */
2713 function convertForSearchResult( $termsArray ) {
2714 # some languages, e.g. Chinese, need to do a conversion
2715 # in order for search results to be displayed correctly
2716 return $termsArray;
2717 }
2718
2719 /**
2720 * Get the first character of a string.
2721 *
2722 * @param $s string
2723 * @return string
2724 */
2725 function firstChar( $s ) {
2726 $matches = array();
2727 preg_match(
2728 '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
2729 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})/',
2730 $s,
2731 $matches
2732 );
2733
2734 if ( isset( $matches[1] ) ) {
2735 if ( strlen( $matches[1] ) != 3 ) {
2736 return $matches[1];
2737 }
2738
2739 // Break down Hangul syllables to grab the first jamo
2740 $code = utf8ToCodepoint( $matches[1] );
2741 if ( $code < 0xac00 || 0xd7a4 <= $code ) {
2742 return $matches[1];
2743 } elseif ( $code < 0xb098 ) {
2744 return "\xe3\x84\xb1";
2745 } elseif ( $code < 0xb2e4 ) {
2746 return "\xe3\x84\xb4";
2747 } elseif ( $code < 0xb77c ) {
2748 return "\xe3\x84\xb7";
2749 } elseif ( $code < 0xb9c8 ) {
2750 return "\xe3\x84\xb9";
2751 } elseif ( $code < 0xbc14 ) {
2752 return "\xe3\x85\x81";
2753 } elseif ( $code < 0xc0ac ) {
2754 return "\xe3\x85\x82";
2755 } elseif ( $code < 0xc544 ) {
2756 return "\xe3\x85\x85";
2757 } elseif ( $code < 0xc790 ) {
2758 return "\xe3\x85\x87";
2759 } elseif ( $code < 0xcc28 ) {
2760 return "\xe3\x85\x88";
2761 } elseif ( $code < 0xce74 ) {
2762 return "\xe3\x85\x8a";
2763 } elseif ( $code < 0xd0c0 ) {
2764 return "\xe3\x85\x8b";
2765 } elseif ( $code < 0xd30c ) {
2766 return "\xe3\x85\x8c";
2767 } elseif ( $code < 0xd558 ) {
2768 return "\xe3\x85\x8d";
2769 } else {
2770 return "\xe3\x85\x8e";
2771 }
2772 } else {
2773 return '';
2774 }
2775 }
2776
2777 function initEncoding() {
2778 # Some languages may have an alternate char encoding option
2779 # (Esperanto X-coding, Japanese furigana conversion, etc)
2780 # If this language is used as the primary content language,
2781 # an override to the defaults can be set here on startup.
2782 }
2783
2784 /**
2785 * @param $s string
2786 * @return string
2787 */
2788 function recodeForEdit( $s ) {
2789 # For some languages we'll want to explicitly specify
2790 # which characters make it into the edit box raw
2791 # or are converted in some way or another.
2792 global $wgEditEncoding;
2793 if ( $wgEditEncoding == '' || $wgEditEncoding == 'UTF-8' ) {
2794 return $s;
2795 } else {
2796 return $this->iconv( 'UTF-8', $wgEditEncoding, $s );
2797 }
2798 }
2799
2800 /**
2801 * @param $s string
2802 * @return string
2803 */
2804 function recodeInput( $s ) {
2805 # Take the previous into account.
2806 global $wgEditEncoding;
2807 if ( $wgEditEncoding != '' ) {
2808 $enc = $wgEditEncoding;
2809 } else {
2810 $enc = 'UTF-8';
2811 }
2812 if ( $enc == 'UTF-8' ) {
2813 return $s;
2814 } else {
2815 return $this->iconv( $enc, 'UTF-8', $s );
2816 }
2817 }
2818
2819 /**
2820 * Convert a UTF-8 string to normal form C. In Malayalam and Arabic, this
2821 * also cleans up certain backwards-compatible sequences, converting them
2822 * to the modern Unicode equivalent.
2823 *
2824 * This is language-specific for performance reasons only.
2825 *
2826 * @param $s string
2827 *
2828 * @return string
2829 */
2830 function normalize( $s ) {
2831 global $wgAllUnicodeFixes;
2832 $s = UtfNormal::cleanUp( $s );
2833 if ( $wgAllUnicodeFixes ) {
2834 $s = $this->transformUsingPairFile( 'normalize-ar.ser', $s );
2835 $s = $this->transformUsingPairFile( 'normalize-ml.ser', $s );
2836 }
2837
2838 return $s;
2839 }
2840
2841 /**
2842 * Transform a string using serialized data stored in the given file (which
2843 * must be in the serialized subdirectory of $IP). The file contains pairs
2844 * mapping source characters to destination characters.
2845 *
2846 * The data is cached in process memory. This will go faster if you have the
2847 * FastStringSearch extension.
2848 *
2849 * @param $file string
2850 * @param $string string
2851 *
2852 * @throws MWException
2853 * @return string
2854 */
2855 function transformUsingPairFile( $file, $string ) {
2856 if ( !isset( $this->transformData[$file] ) ) {
2857 $data = wfGetPrecompiledData( $file );
2858 if ( $data === false ) {
2859 throw new MWException( __METHOD__ . ": The transformation file $file is missing" );
2860 }
2861 $this->transformData[$file] = new ReplacementArray( $data );
2862 }
2863 return $this->transformData[$file]->replace( $string );
2864 }
2865
2866 /**
2867 * For right-to-left language support
2868 *
2869 * @return bool
2870 */
2871 function isRTL() {
2872 return self::$dataCache->getItem( $this->mCode, 'rtl' );
2873 }
2874
2875 /**
2876 * Return the correct HTML 'dir' attribute value for this language.
2877 * @return String
2878 */
2879 function getDir() {
2880 return $this->isRTL() ? 'rtl' : 'ltr';
2881 }
2882
2883 /**
2884 * Return 'left' or 'right' as appropriate alignment for line-start
2885 * for this language's text direction.
2886 *
2887 * Should be equivalent to CSS3 'start' text-align value....
2888 *
2889 * @return String
2890 */
2891 function alignStart() {
2892 return $this->isRTL() ? 'right' : 'left';
2893 }
2894
2895 /**
2896 * Return 'right' or 'left' as appropriate alignment for line-end
2897 * for this language's text direction.
2898 *
2899 * Should be equivalent to CSS3 'end' text-align value....
2900 *
2901 * @return String
2902 */
2903 function alignEnd() {
2904 return $this->isRTL() ? 'left' : 'right';
2905 }
2906
2907 /**
2908 * A hidden direction mark (LRM or RLM), depending on the language direction.
2909 * Unlike getDirMark(), this function returns the character as an HTML entity.
2910 * This function should be used when the output is guaranteed to be HTML,
2911 * because it makes the output HTML source code more readable. When
2912 * the output is plain text or can be escaped, getDirMark() should be used.
2913 *
2914 * @param $opposite Boolean Get the direction mark opposite to your language
2915 * @return string
2916 * @since 1.20
2917 */
2918 function getDirMarkEntity( $opposite = false ) {
2919 if ( $opposite ) {
2920 return $this->isRTL() ? '&lrm;' : '&rlm;';
2921 }
2922 return $this->isRTL() ? '&rlm;' : '&lrm;';
2923 }
2924
2925 /**
2926 * A hidden direction mark (LRM or RLM), depending on the language direction.
2927 * This function produces them as invisible Unicode characters and
2928 * the output may be hard to read and debug, so it should only be used
2929 * when the output is plain text or can be escaped. When the output is
2930 * HTML, use getDirMarkEntity() instead.
2931 *
2932 * @param $opposite Boolean Get the direction mark opposite to your language
2933 * @return string
2934 */
2935 function getDirMark( $opposite = false ) {
2936 $lrm = "\xE2\x80\x8E"; # LEFT-TO-RIGHT MARK, commonly abbreviated LRM
2937 $rlm = "\xE2\x80\x8F"; # RIGHT-TO-LEFT MARK, commonly abbreviated RLM
2938 if ( $opposite ) {
2939 return $this->isRTL() ? $lrm : $rlm;
2940 }
2941 return $this->isRTL() ? $rlm : $lrm;
2942 }
2943
2944 /**
2945 * @return array
2946 */
2947 function capitalizeAllNouns() {
2948 return self::$dataCache->getItem( $this->mCode, 'capitalizeAllNouns' );
2949 }
2950
2951 /**
2952 * An arrow, depending on the language direction.
2953 *
2954 * @param $direction String: the direction of the arrow: forwards (default), backwards, left, right, up, down.
2955 * @return string
2956 */
2957 function getArrow( $direction = 'forwards' ) {
2958 switch ( $direction ) {
2959 case 'forwards':
2960 return $this->isRTL() ? '←' : '→';
2961 case 'backwards':
2962 return $this->isRTL() ? '→' : '←';
2963 case 'left':
2964 return '←';
2965 case 'right':
2966 return '→';
2967 case 'up':
2968 return '↑';
2969 case 'down':
2970 return '↓';
2971 }
2972 }
2973
2974 /**
2975 * To allow "foo[[bar]]" to extend the link over the whole word "foobar"
2976 *
2977 * @return bool
2978 */
2979 function linkPrefixExtension() {
2980 return self::$dataCache->getItem( $this->mCode, 'linkPrefixExtension' );
2981 }
2982
2983 /**
2984 * Get all magic words from cache.
2985 * @return array
2986 */
2987 function getMagicWords() {
2988 return self::$dataCache->getItem( $this->mCode, 'magicWords' );
2989 }
2990
2991 /**
2992 * Run the LanguageGetMagic hook once.
2993 */
2994 protected function doMagicHook() {
2995 if ( $this->mMagicHookDone ) {
2996 return;
2997 }
2998 $this->mMagicHookDone = true;
2999 wfProfileIn( 'LanguageGetMagic' );
3000 wfRunHooks( 'LanguageGetMagic', array( &$this->mMagicExtensions, $this->getCode() ) );
3001 wfProfileOut( 'LanguageGetMagic' );
3002 }
3003
3004 /**
3005 * Fill a MagicWord object with data from here
3006 *
3007 * @param $mw
3008 */
3009 function getMagic( $mw ) {
3010 // Saves a function call
3011 if ( ! $this->mMagicHookDone ) {
3012 $this->doMagicHook();
3013 }
3014
3015 if ( isset( $this->mMagicExtensions[$mw->mId] ) ) {
3016 $rawEntry = $this->mMagicExtensions[$mw->mId];
3017 } else {
3018 $rawEntry = self::$dataCache->getSubitem(
3019 $this->mCode, 'magicWords', $mw->mId );
3020 }
3021
3022 if ( !is_array( $rawEntry ) ) {
3023 error_log( "\"$rawEntry\" is not a valid magic word for \"$mw->mId\"" );
3024 } else {
3025 $mw->mCaseSensitive = $rawEntry[0];
3026 $mw->mSynonyms = array_slice( $rawEntry, 1 );
3027 }
3028 }
3029
3030 /**
3031 * Add magic words to the extension array
3032 *
3033 * @param $newWords array
3034 */
3035 function addMagicWordsByLang( $newWords ) {
3036 $fallbackChain = $this->getFallbackLanguages();
3037 $fallbackChain = array_reverse( $fallbackChain );
3038 foreach ( $fallbackChain as $code ) {
3039 if ( isset( $newWords[$code] ) ) {
3040 $this->mMagicExtensions = $newWords[$code] + $this->mMagicExtensions;
3041 }
3042 }
3043 }
3044
3045 /**
3046 * Get special page names, as an associative array
3047 * case folded alias => real name
3048 */
3049 function getSpecialPageAliases() {
3050 // Cache aliases because it may be slow to load them
3051 if ( is_null( $this->mExtendedSpecialPageAliases ) ) {
3052 // Initialise array
3053 $this->mExtendedSpecialPageAliases =
3054 self::$dataCache->getItem( $this->mCode, 'specialPageAliases' );
3055 wfRunHooks( 'LanguageGetSpecialPageAliases',
3056 array( &$this->mExtendedSpecialPageAliases, $this->getCode() ) );
3057 }
3058
3059 return $this->mExtendedSpecialPageAliases;
3060 }
3061
3062 /**
3063 * Italic is unsuitable for some languages
3064 *
3065 * @param $text String: the text to be emphasized.
3066 * @return string
3067 */
3068 function emphasize( $text ) {
3069 return "<em>$text</em>";
3070 }
3071
3072 /**
3073 * Normally we output all numbers in plain en_US style, that is
3074 * 293,291.235 for twohundredninetythreethousand-twohundredninetyone
3075 * point twohundredthirtyfive. However this is not suitable for all
3076 * languages, some such as Punjabi want ੨੯੩,੨੯੫.੨੩੫ and others such as
3077 * Icelandic just want to use commas instead of dots, and dots instead
3078 * of commas like "293.291,235".
3079 *
3080 * An example of this function being called:
3081 * <code>
3082 * wfMessage( 'message' )->numParams( $num )->text()
3083 * </code>
3084 *
3085 * See $separatorTransformTable on MessageIs.php for
3086 * the , => . and . => , implementation.
3087 *
3088 * @todo check if it's viable to use localeconv() for the decimal
3089 * separator thing.
3090 * @param $number Mixed: the string to be formatted, should be an integer
3091 * or a floating point number.
3092 * @param $nocommafy Bool: set to true for special numbers like dates
3093 * @return string
3094 */
3095 public function formatNum( $number, $nocommafy = false ) {
3096 global $wgTranslateNumerals;
3097 if ( !$nocommafy ) {
3098 $number = $this->commafy( $number );
3099 $s = $this->separatorTransformTable();
3100 if ( $s ) {
3101 $number = strtr( $number, $s );
3102 }
3103 }
3104
3105 if ( $wgTranslateNumerals ) {
3106 $s = $this->digitTransformTable();
3107 if ( $s ) {
3108 $number = strtr( $number, $s );
3109 }
3110 }
3111
3112 return $number;
3113 }
3114
3115 /**
3116 * Front-end for non-commafied formatNum
3117 *
3118 * @param mixed $number the string to be formatted, should be an integer
3119 * or a floating point number.
3120 * @since 1.21
3121 * @return string
3122 */
3123 public function formatNumNoSeparators( $number ) {
3124 return $this->formatNum( $number, true );
3125 }
3126
3127 /**
3128 * @param $number string
3129 * @return string
3130 */
3131 function parseFormattedNumber( $number ) {
3132 $s = $this->digitTransformTable();
3133 if ( $s ) {
3134 $number = strtr( $number, array_flip( $s ) );
3135 }
3136
3137 $s = $this->separatorTransformTable();
3138 if ( $s ) {
3139 $number = strtr( $number, array_flip( $s ) );
3140 }
3141
3142 $number = strtr( $number, array( ',' => '' ) );
3143 return $number;
3144 }
3145
3146 /**
3147 * Adds commas to a given number
3148 * @since 1.19
3149 * @param $number mixed
3150 * @return string
3151 */
3152 function commafy( $number ) {
3153 $digitGroupingPattern = $this->digitGroupingPattern();
3154 if ( $number === null ) {
3155 return '';
3156 }
3157
3158 if ( !$digitGroupingPattern || $digitGroupingPattern === "###,###,###" ) {
3159 // default grouping is at thousands, use the same for ###,###,### pattern too.
3160 return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,', strrev( $number ) ) );
3161 } else {
3162 // Ref: http://cldr.unicode.org/translation/number-patterns
3163 $sign = "";
3164 if ( intval( $number ) < 0 ) {
3165 // For negative numbers apply the algorithm like positive number and add sign.
3166 $sign = "-";
3167 $number = substr( $number, 1 );
3168 }
3169 $integerPart = array();
3170 $decimalPart = array();
3171 $numMatches = preg_match_all( "/(#+)/", $digitGroupingPattern, $matches );
3172 preg_match( "/\d+/", $number, $integerPart );
3173 preg_match( "/\.\d*/", $number, $decimalPart );
3174 $groupedNumber = ( count( $decimalPart ) > 0 ) ? $decimalPart[0] : "";
3175 if ( $groupedNumber === $number ) {
3176 // the string does not have any number part. Eg: .12345
3177 return $sign . $groupedNumber;
3178 }
3179 $start = $end = strlen( $integerPart[0] );
3180 while ( $start > 0 ) {
3181 $match = $matches[0][$numMatches - 1];
3182 $matchLen = strlen( $match );
3183 $start = $end - $matchLen;
3184 if ( $start < 0 ) {
3185 $start = 0;
3186 }
3187 $groupedNumber = substr( $number, $start, $end -$start ) . $groupedNumber;
3188 $end = $start;
3189 if ( $numMatches > 1 ) {
3190 // use the last pattern for the rest of the number
3191 $numMatches--;
3192 }
3193 if ( $start > 0 ) {
3194 $groupedNumber = "," . $groupedNumber;
3195 }
3196 }
3197 return $sign . $groupedNumber;
3198 }
3199 }
3200
3201 /**
3202 * @return String
3203 */
3204 function digitGroupingPattern() {
3205 return self::$dataCache->getItem( $this->mCode, 'digitGroupingPattern' );
3206 }
3207
3208 /**
3209 * @return array
3210 */
3211 function digitTransformTable() {
3212 return self::$dataCache->getItem( $this->mCode, 'digitTransformTable' );
3213 }
3214
3215 /**
3216 * @return array
3217 */
3218 function separatorTransformTable() {
3219 return self::$dataCache->getItem( $this->mCode, 'separatorTransformTable' );
3220 }
3221
3222 /**
3223 * Take a list of strings and build a locale-friendly comma-separated
3224 * list, using the local comma-separator message.
3225 * The last two strings are chained with an "and".
3226 * NOTE: This function will only work with standard numeric array keys (0, 1, 2…)
3227 *
3228 * @param $l Array
3229 * @return string
3230 */
3231 function listToText( array $l ) {
3232 $m = count( $l ) - 1;
3233 if ( $m < 0 ) {
3234 return '';
3235 }
3236 if ( $m > 0 ) {
3237 $and = $this->getMessageFromDB( 'and' );
3238 $space = $this->getMessageFromDB( 'word-separator' );
3239 if ( $m > 1 ) {
3240 $comma = $this->getMessageFromDB( 'comma-separator' );
3241 }
3242 }
3243 $s = $l[$m];
3244 for ( $i = $m - 1; $i >= 0; $i-- ) {
3245 if ( $i == $m - 1 ) {
3246 $s = $l[$i] . $and . $space . $s;
3247 } else {
3248 $s = $l[$i] . $comma . $s;
3249 }
3250 }
3251 return $s;
3252 }
3253
3254 /**
3255 * Take a list of strings and build a locale-friendly comma-separated
3256 * list, using the local comma-separator message.
3257 * @param $list array of strings to put in a comma list
3258 * @return string
3259 */
3260 function commaList( array $list ) {
3261 return implode(
3262 wfMessage( 'comma-separator' )->inLanguage( $this )->escaped(),
3263 $list
3264 );
3265 }
3266
3267 /**
3268 * Take a list of strings and build a locale-friendly semicolon-separated
3269 * list, using the local semicolon-separator message.
3270 * @param $list array of strings to put in a semicolon list
3271 * @return string
3272 */
3273 function semicolonList( array $list ) {
3274 return implode(
3275 wfMessage( 'semicolon-separator' )->inLanguage( $this )->escaped(),
3276 $list
3277 );
3278 }
3279
3280 /**
3281 * Same as commaList, but separate it with the pipe instead.
3282 * @param $list array of strings to put in a pipe list
3283 * @return string
3284 */
3285 function pipeList( array $list ) {
3286 return implode(
3287 wfMessage( 'pipe-separator' )->inLanguage( $this )->escaped(),
3288 $list
3289 );
3290 }
3291
3292 /**
3293 * Truncate a string to a specified length in bytes, appending an optional
3294 * string (e.g. for ellipses)
3295 *
3296 * The database offers limited byte lengths for some columns in the database;
3297 * multi-byte character sets mean we need to ensure that only whole characters
3298 * are included, otherwise broken characters can be passed to the user
3299 *
3300 * If $length is negative, the string will be truncated from the beginning
3301 *
3302 * @param $string String to truncate
3303 * @param $length Int: maximum length (including ellipses)
3304 * @param $ellipsis String to append to the truncated text
3305 * @param $adjustLength Boolean: Subtract length of ellipsis from $length.
3306 * $adjustLength was introduced in 1.18, before that behaved as if false.
3307 * @return string
3308 */
3309 function truncate( $string, $length, $ellipsis = '...', $adjustLength = true ) {
3310 # Use the localized ellipsis character
3311 if ( $ellipsis == '...' ) {
3312 $ellipsis = wfMessage( 'ellipsis' )->inLanguage( $this )->escaped();
3313 }
3314 # Check if there is no need to truncate
3315 if ( $length == 0 ) {
3316 return $ellipsis; // convention
3317 } elseif ( strlen( $string ) <= abs( $length ) ) {
3318 return $string; // no need to truncate
3319 }
3320 $stringOriginal = $string;
3321 # If ellipsis length is >= $length then we can't apply $adjustLength
3322 if ( $adjustLength && strlen( $ellipsis ) >= abs( $length ) ) {
3323 $string = $ellipsis; // this can be slightly unexpected
3324 # Otherwise, truncate and add ellipsis...
3325 } else {
3326 $eLength = $adjustLength ? strlen( $ellipsis ) : 0;
3327 if ( $length > 0 ) {
3328 $length -= $eLength;
3329 $string = substr( $string, 0, $length ); // xyz...
3330 $string = $this->removeBadCharLast( $string );
3331 $string = rtrim( $string );
3332 $string = $string . $ellipsis;
3333 } else {
3334 $length += $eLength;
3335 $string = substr( $string, $length ); // ...xyz
3336 $string = $this->removeBadCharFirst( $string );
3337 $string = ltrim( $string );
3338 $string = $ellipsis . $string;
3339 }
3340 }
3341 # Do not truncate if the ellipsis makes the string longer/equal (bug 22181).
3342 # This check is *not* redundant if $adjustLength, due to the single case where
3343 # LEN($ellipsis) > ABS($limit arg); $stringOriginal could be shorter than $string.
3344 if ( strlen( $string ) < strlen( $stringOriginal ) ) {
3345 return $string;
3346 } else {
3347 return $stringOriginal;
3348 }
3349 }
3350
3351 /**
3352 * Remove bytes that represent an incomplete Unicode character
3353 * at the end of string (e.g. bytes of the char are missing)
3354 *
3355 * @param $string String
3356 * @return string
3357 */
3358 protected function removeBadCharLast( $string ) {
3359 if ( $string != '' ) {
3360 $char = ord( $string[strlen( $string ) - 1] );
3361 $m = array();
3362 if ( $char >= 0xc0 ) {
3363 # We got the first byte only of a multibyte char; remove it.
3364 $string = substr( $string, 0, -1 );
3365 } elseif ( $char >= 0x80 &&
3366 preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
3367 '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m )
3368 ) {
3369 # We chopped in the middle of a character; remove it
3370 $string = $m[1];
3371 }
3372 }
3373 return $string;
3374 }
3375
3376 /**
3377 * Remove bytes that represent an incomplete Unicode character
3378 * at the start of string (e.g. bytes of the char are missing)
3379 *
3380 * @param $string String
3381 * @return string
3382 */
3383 protected function removeBadCharFirst( $string ) {
3384 if ( $string != '' ) {
3385 $char = ord( $string[0] );
3386 if ( $char >= 0x80 && $char < 0xc0 ) {
3387 # We chopped in the middle of a character; remove the whole thing
3388 $string = preg_replace( '/^[\x80-\xbf]+/', '', $string );
3389 }
3390 }
3391 return $string;
3392 }
3393
3394 /**
3395 * Truncate a string of valid HTML to a specified length in bytes,
3396 * appending an optional string (e.g. for ellipses), and return valid HTML
3397 *
3398 * This is only intended for styled/linked text, such as HTML with
3399 * tags like <span> and <a>, were the tags are self-contained (valid HTML).
3400 * Also, this will not detect things like "display:none" CSS.
3401 *
3402 * Note: since 1.18 you do not need to leave extra room in $length for ellipses.
3403 *
3404 * @param string $text HTML string to truncate
3405 * @param int $length (zero/positive) Maximum length (including ellipses)
3406 * @param string $ellipsis String to append to the truncated text
3407 * @return string
3408 */
3409 function truncateHtml( $text, $length, $ellipsis = '...' ) {
3410 # Use the localized ellipsis character
3411 if ( $ellipsis == '...' ) {
3412 $ellipsis = wfMessage( 'ellipsis' )->inLanguage( $this )->escaped();
3413 }
3414 # Check if there is clearly no need to truncate
3415 if ( $length <= 0 ) {
3416 return $ellipsis; // no text shown, nothing to format (convention)
3417 } elseif ( strlen( $text ) <= $length ) {
3418 return $text; // string short enough even *with* HTML (short-circuit)
3419 }
3420
3421 $dispLen = 0; // innerHTML legth so far
3422 $testingEllipsis = false; // checking if ellipses will make string longer/equal?
3423 $tagType = 0; // 0-open, 1-close
3424 $bracketState = 0; // 1-tag start, 2-tag name, 0-neither
3425 $entityState = 0; // 0-not entity, 1-entity
3426 $tag = $ret = ''; // accumulated tag name, accumulated result string
3427 $openTags = array(); // open tag stack
3428 $maybeState = null; // possible truncation state
3429
3430 $textLen = strlen( $text );
3431 $neLength = max( 0, $length - strlen( $ellipsis ) ); // non-ellipsis len if truncated
3432 for ( $pos = 0; true; ++$pos ) {
3433 # Consider truncation once the display length has reached the maximim.
3434 # We check if $dispLen > 0 to grab tags for the $neLength = 0 case.
3435 # Check that we're not in the middle of a bracket/entity...
3436 if ( $dispLen && $dispLen >= $neLength && $bracketState == 0 && !$entityState ) {
3437 if ( !$testingEllipsis ) {
3438 $testingEllipsis = true;
3439 # Save where we are; we will truncate here unless there turn out to
3440 # be so few remaining characters that truncation is not necessary.
3441 if ( !$maybeState ) { // already saved? ($neLength = 0 case)
3442 $maybeState = array( $ret, $openTags ); // save state
3443 }
3444 } elseif ( $dispLen > $length && $dispLen > strlen( $ellipsis ) ) {
3445 # String in fact does need truncation, the truncation point was OK.
3446 list( $ret, $openTags ) = $maybeState; // reload state
3447 $ret = $this->removeBadCharLast( $ret ); // multi-byte char fix
3448 $ret .= $ellipsis; // add ellipsis
3449 break;
3450 }
3451 }
3452 if ( $pos >= $textLen ) {
3453 break; // extra iteration just for above checks
3454 }
3455
3456 # Read the next char...
3457 $ch = $text[$pos];
3458 $lastCh = $pos ? $text[$pos - 1] : '';
3459 $ret .= $ch; // add to result string
3460 if ( $ch == '<' ) {
3461 $this->truncate_endBracket( $tag, $tagType, $lastCh, $openTags ); // for bad HTML
3462 $entityState = 0; // for bad HTML
3463 $bracketState = 1; // tag started (checking for backslash)
3464 } elseif ( $ch == '>' ) {
3465 $this->truncate_endBracket( $tag, $tagType, $lastCh, $openTags );
3466 $entityState = 0; // for bad HTML
3467 $bracketState = 0; // out of brackets
3468 } elseif ( $bracketState == 1 ) {
3469 if ( $ch == '/' ) {
3470 $tagType = 1; // close tag (e.g. "</span>")
3471 } else {
3472 $tagType = 0; // open tag (e.g. "<span>")
3473 $tag .= $ch;
3474 }
3475 $bracketState = 2; // building tag name
3476 } elseif ( $bracketState == 2 ) {
3477 if ( $ch != ' ' ) {
3478 $tag .= $ch;
3479 } else {
3480 // Name found (e.g. "<a href=..."), add on tag attributes...
3481 $pos += $this->truncate_skip( $ret, $text, "<>", $pos + 1 );
3482 }
3483 } elseif ( $bracketState == 0 ) {
3484 if ( $entityState ) {
3485 if ( $ch == ';' ) {
3486 $entityState = 0;
3487 $dispLen++; // entity is one displayed char
3488 }
3489 } else {
3490 if ( $neLength == 0 && !$maybeState ) {
3491 // Save state without $ch. We want to *hit* the first
3492 // display char (to get tags) but not *use* it if truncating.
3493 $maybeState = array( substr( $ret, 0, -1 ), $openTags );
3494 }
3495 if ( $ch == '&' ) {
3496 $entityState = 1; // entity found, (e.g. "&#160;")
3497 } else {
3498 $dispLen++; // this char is displayed
3499 // Add the next $max display text chars after this in one swoop...
3500 $max = ( $testingEllipsis ? $length : $neLength ) - $dispLen;
3501 $skipped = $this->truncate_skip( $ret, $text, "<>&", $pos + 1, $max );
3502 $dispLen += $skipped;
3503 $pos += $skipped;
3504 }
3505 }
3506 }
3507 }
3508 // Close the last tag if left unclosed by bad HTML
3509 $this->truncate_endBracket( $tag, $text[$textLen - 1], $tagType, $openTags );
3510 while ( count( $openTags ) > 0 ) {
3511 $ret .= '</' . array_pop( $openTags ) . '>'; // close open tags
3512 }
3513 return $ret;
3514 }
3515
3516 /**
3517 * truncateHtml() helper function
3518 * like strcspn() but adds the skipped chars to $ret
3519 *
3520 * @param $ret
3521 * @param $text
3522 * @param $search
3523 * @param $start
3524 * @param $len
3525 * @return int
3526 */
3527 private function truncate_skip( &$ret, $text, $search, $start, $len = null ) {
3528 if ( $len === null ) {
3529 $len = -1; // -1 means "no limit" for strcspn
3530 } elseif ( $len < 0 ) {
3531 $len = 0; // sanity
3532 }
3533 $skipCount = 0;
3534 if ( $start < strlen( $text ) ) {
3535 $skipCount = strcspn( $text, $search, $start, $len );
3536 $ret .= substr( $text, $start, $skipCount );
3537 }
3538 return $skipCount;
3539 }
3540
3541 /**
3542 * truncateHtml() helper function
3543 * (a) push or pop $tag from $openTags as needed
3544 * (b) clear $tag value
3545 * @param &$tag string Current HTML tag name we are looking at
3546 * @param $tagType int (0-open tag, 1-close tag)
3547 * @param $lastCh string Character before the '>' that ended this tag
3548 * @param &$openTags array Open tag stack (not accounting for $tag)
3549 */
3550 private function truncate_endBracket( &$tag, $tagType, $lastCh, &$openTags ) {
3551 $tag = ltrim( $tag );
3552 if ( $tag != '' ) {
3553 if ( $tagType == 0 && $lastCh != '/' ) {
3554 $openTags[] = $tag; // tag opened (didn't close itself)
3555 } elseif ( $tagType == 1 ) {
3556 if ( $openTags && $tag == $openTags[count( $openTags ) - 1] ) {
3557 array_pop( $openTags ); // tag closed
3558 }
3559 }
3560 $tag = '';
3561 }
3562 }
3563
3564 /**
3565 * Grammatical transformations, needed for inflected languages
3566 * Invoked by putting {{grammar:case|word}} in a message
3567 *
3568 * @param $word string
3569 * @param $case string
3570 * @return string
3571 */
3572 function convertGrammar( $word, $case ) {
3573 global $wgGrammarForms;
3574 if ( isset( $wgGrammarForms[$this->getCode()][$case][$word] ) ) {
3575 return $wgGrammarForms[$this->getCode()][$case][$word];
3576 }
3577 return $word;
3578 }
3579 /**
3580 * Get the grammar forms for the content language
3581 * @return array of grammar forms
3582 * @since 1.20
3583 */
3584 function getGrammarForms() {
3585 global $wgGrammarForms;
3586 if ( isset( $wgGrammarForms[$this->getCode()] ) && is_array( $wgGrammarForms[$this->getCode()] ) ) {
3587 return $wgGrammarForms[$this->getCode()];
3588 }
3589 return array();
3590 }
3591 /**
3592 * Provides an alternative text depending on specified gender.
3593 * Usage {{gender:username|masculine|feminine|neutral}}.
3594 * username is optional, in which case the gender of current user is used,
3595 * but only in (some) interface messages; otherwise default gender is used.
3596 *
3597 * If no forms are given, an empty string is returned. If only one form is
3598 * given, it will be returned unconditionally. These details are implied by
3599 * the caller and cannot be overridden in subclasses.
3600 *
3601 * If more than one form is given, the default is to use the neutral one
3602 * if it is specified, and to use the masculine one otherwise. These
3603 * details can be overridden in subclasses.
3604 *
3605 * @param $gender string
3606 * @param $forms array
3607 *
3608 * @return string
3609 */
3610 function gender( $gender, $forms ) {
3611 if ( !count( $forms ) ) {
3612 return '';
3613 }
3614 $forms = $this->preConvertPlural( $forms, 2 );
3615 if ( $gender === 'male' ) {
3616 return $forms[0];
3617 }
3618 if ( $gender === 'female' ) {
3619 return $forms[1];
3620 }
3621 return isset( $forms[2] ) ? $forms[2] : $forms[0];
3622 }
3623
3624 /**
3625 * Plural form transformations, needed for some languages.
3626 * For example, there are 3 form of plural in Russian and Polish,
3627 * depending on "count mod 10". See [[w:Plural]]
3628 * For English it is pretty simple.
3629 *
3630 * Invoked by putting {{plural:count|wordform1|wordform2}}
3631 * or {{plural:count|wordform1|wordform2|wordform3}}
3632 *
3633 * Example: {{plural:{{NUMBEROFARTICLES}}|article|articles}}
3634 *
3635 * @param $count Integer: non-localized number
3636 * @param $forms Array: different plural forms
3637 * @return string Correct form of plural for $count in this language
3638 */
3639 function convertPlural( $count, $forms ) {
3640 // Handle explicit n=pluralform cases
3641 $forms = $this->handleExplicitPluralForms( $count, $forms );
3642 if ( is_string( $forms ) ) {
3643 return $forms;
3644 }
3645 if ( !count( $forms ) ) {
3646 return '';
3647 }
3648
3649 $pluralForm = $this->getPluralRuleIndexNumber( $count );
3650 $pluralForm = min( $pluralForm, count( $forms ) - 1 );
3651 return $forms[$pluralForm];
3652 }
3653
3654 /**
3655 * Handles explicit plural forms for Language::convertPlural()
3656 *
3657 * In {{PLURAL:$1|0=nothing|one|many}}, 0=nothing will be returned if $1 equals zero.
3658 * If an explicitly defined plural form matches the $count, then
3659 * string value returned, otherwise array returned for further consideration
3660 * by CLDR rules or overridden convertPlural().
3661 *
3662 * @since 1.23
3663 *
3664 * @param int $count non-localized number
3665 * @param array $forms different plural forms
3666 *
3667 * @return array|string
3668 */
3669 protected function handleExplicitPluralForms( $count, array $forms ) {
3670 foreach ( $forms as $index => $form ) {
3671 if ( preg_match( '/\d+=/i', $form ) ) {
3672 $pos = strpos( $form, '=' );
3673 if ( substr( $form, 0, $pos ) === (string) $count ) {
3674 return substr( $form, $pos + 1 );
3675 }
3676 unset( $forms[$index] );
3677 }
3678 }
3679 return array_values( $forms );
3680 }
3681
3682 /**
3683 * Checks that convertPlural was given an array and pads it to requested
3684 * amount of forms by copying the last one.
3685 *
3686 * @param $count Integer: How many forms should there be at least
3687 * @param $forms Array of forms given to convertPlural
3688 * @return array Padded array of forms or an exception if not an array
3689 */
3690 protected function preConvertPlural( /* Array */ $forms, $count ) {
3691 while ( count( $forms ) < $count ) {
3692 $forms[] = $forms[count( $forms ) - 1];
3693 }
3694 return $forms;
3695 }
3696
3697 /**
3698 * @todo Maybe translate block durations. Note that this function is somewhat misnamed: it
3699 * deals with translating the *duration* ("1 week", "4 days", etc), not the expiry time
3700 * (which is an absolute timestamp). Please note: do NOT add this blindly, as it is used
3701 * on old expiry lengths recorded in log entries. You'd need to provide the start date to
3702 * match up with it.
3703 *
3704 * @param $str String: the validated block duration in English
3705 * @return string Somehow translated block duration
3706 * @see LanguageFi.php for example implementation
3707 */
3708 function translateBlockExpiry( $str ) {
3709 $duration = SpecialBlock::getSuggestedDurations( $this );
3710 foreach ( $duration as $show => $value ) {
3711 if ( strcmp( $str, $value ) == 0 ) {
3712 return htmlspecialchars( trim( $show ) );
3713 }
3714 }
3715
3716 // Since usually only infinite or indefinite is only on list, so try
3717 // equivalents if still here.
3718 $indefs = array( 'infinite', 'infinity', 'indefinite' );
3719 if ( in_array( $str, $indefs ) ) {
3720 foreach ( $indefs as $val ) {
3721 $show = array_search( $val, $duration, true );
3722 if ( $show !== false ) {
3723 return htmlspecialchars( trim( $show ) );
3724 }
3725 }
3726 }
3727
3728 // If all else fails, return a standard duration or timestamp description.
3729 $time = strtotime( $str, 0 );
3730 if ( $time === false ) { // Unknown format. Return it as-is in case.
3731 return $str;
3732 } elseif ( $time !== strtotime( $str, 1 ) ) { // It's a relative timestamp.
3733 // $time is relative to 0 so it's a duration length.
3734 return $this->formatDuration( $time );
3735 } else { // It's an absolute timestamp.
3736 if ( $time === 0 ) {
3737 // wfTimestamp() handles 0 as current time instead of epoch.
3738 return $this->timeanddate( '19700101000000' );
3739 } else {
3740 return $this->timeanddate( $time );
3741 }
3742 }
3743 }
3744
3745 /**
3746 * languages like Chinese need to be segmented in order for the diff
3747 * to be of any use
3748 *
3749 * @param $text String
3750 * @return String
3751 */
3752 public function segmentForDiff( $text ) {
3753 return $text;
3754 }
3755
3756 /**
3757 * and unsegment to show the result
3758 *
3759 * @param $text String
3760 * @return String
3761 */
3762 public function unsegmentForDiff( $text ) {
3763 return $text;
3764 }
3765
3766 /**
3767 * Return the LanguageConverter used in the Language
3768 *
3769 * @since 1.19
3770 * @return LanguageConverter
3771 */
3772 public function getConverter() {
3773 return $this->mConverter;
3774 }
3775
3776 /**
3777 * convert text to all supported variants
3778 *
3779 * @param $text string
3780 * @return array
3781 */
3782 public function autoConvertToAllVariants( $text ) {
3783 return $this->mConverter->autoConvertToAllVariants( $text );
3784 }
3785
3786 /**
3787 * convert text to different variants of a language.
3788 *
3789 * @param $text string
3790 * @return string
3791 */
3792 public function convert( $text ) {
3793 return $this->mConverter->convert( $text );
3794 }
3795
3796 /**
3797 * Convert a Title object to a string in the preferred variant
3798 *
3799 * @param $title Title
3800 * @return string
3801 */
3802 public function convertTitle( $title ) {
3803 return $this->mConverter->convertTitle( $title );
3804 }
3805
3806 /**
3807 * Convert a namespace index to a string in the preferred variant
3808 *
3809 * @param $ns int
3810 * @return string
3811 */
3812 public function convertNamespace( $ns ) {
3813 return $this->mConverter->convertNamespace( $ns );
3814 }
3815
3816 /**
3817 * Check if this is a language with variants
3818 *
3819 * @return bool
3820 */
3821 public function hasVariants() {
3822 return count( $this->getVariants() ) > 1;
3823 }
3824
3825 /**
3826 * Check if the language has the specific variant
3827 *
3828 * @since 1.19
3829 * @param $variant string
3830 * @return bool
3831 */
3832 public function hasVariant( $variant ) {
3833 return (bool)$this->mConverter->validateVariant( $variant );
3834 }
3835
3836 /**
3837 * Put custom tags (e.g. -{ }-) around math to prevent conversion
3838 *
3839 * @param $text string
3840 * @return string
3841 * @deprecated since 1.22 is no longer used
3842 */
3843 public function armourMath( $text ) {
3844 return $this->mConverter->armourMath( $text );
3845 }
3846
3847 /**
3848 * Perform output conversion on a string, and encode for safe HTML output.
3849 * @param $text String text to be converted
3850 * @param $isTitle Bool whether this conversion is for the article title
3851 * @return string
3852 * @todo this should get integrated somewhere sane
3853 */
3854 public function convertHtml( $text, $isTitle = false ) {
3855 return htmlspecialchars( $this->convert( $text, $isTitle ) );
3856 }
3857
3858 /**
3859 * @param $key string
3860 * @return string
3861 */
3862 public function convertCategoryKey( $key ) {
3863 return $this->mConverter->convertCategoryKey( $key );
3864 }
3865
3866 /**
3867 * Get the list of variants supported by this language
3868 * see sample implementation in LanguageZh.php
3869 *
3870 * @return array an array of language codes
3871 */
3872 public function getVariants() {
3873 return $this->mConverter->getVariants();
3874 }
3875
3876 /**
3877 * @return string
3878 */
3879 public function getPreferredVariant() {
3880 return $this->mConverter->getPreferredVariant();
3881 }
3882
3883 /**
3884 * @return string
3885 */
3886 public function getDefaultVariant() {
3887 return $this->mConverter->getDefaultVariant();
3888 }
3889
3890 /**
3891 * @return string
3892 */
3893 public function getURLVariant() {
3894 return $this->mConverter->getURLVariant();
3895 }
3896
3897 /**
3898 * If a language supports multiple variants, it is
3899 * possible that non-existing link in one variant
3900 * actually exists in another variant. this function
3901 * tries to find it. See e.g. LanguageZh.php
3902 *
3903 * @param $link String: the name of the link
3904 * @param $nt Mixed: the title object of the link
3905 * @param $ignoreOtherCond Boolean: to disable other conditions when
3906 * we need to transclude a template or update a category's link
3907 * @return null the input parameters may be modified upon return
3908 */
3909 public function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
3910 $this->mConverter->findVariantLink( $link, $nt, $ignoreOtherCond );
3911 }
3912
3913 /**
3914 * returns language specific options used by User::getPageRenderHash()
3915 * for example, the preferred language variant
3916 *
3917 * @return string
3918 */
3919 function getExtraHashOptions() {
3920 return $this->mConverter->getExtraHashOptions();
3921 }
3922
3923 /**
3924 * For languages that support multiple variants, the title of an
3925 * article may be displayed differently in different variants. this
3926 * function returns the apporiate title defined in the body of the article.
3927 *
3928 * @return string
3929 */
3930 public function getParsedTitle() {
3931 return $this->mConverter->getParsedTitle();
3932 }
3933
3934 /**
3935 * Prepare external link text for conversion. When the text is
3936 * a URL, it shouldn't be converted, and it'll be wrapped in
3937 * the "raw" tag (-{R| }-) to prevent conversion.
3938 *
3939 * This function is called "markNoConversion" for historical
3940 * reasons.
3941 *
3942 * @param $text String: text to be used for external link
3943 * @param $noParse bool: wrap it without confirming it's a real URL first
3944 * @return string the tagged text
3945 */
3946 public function markNoConversion( $text, $noParse = false ) {
3947 // Excluding protocal-relative URLs may avoid many false positives.
3948 if ( $noParse || preg_match( '/^(?:' . wfUrlProtocolsWithoutProtRel() . ')/', $text ) ) {
3949 return $this->mConverter->markNoConversion( $text );
3950 } else {
3951 return $text;
3952 }
3953 }
3954
3955 /**
3956 * A regular expression to match legal word-trailing characters
3957 * which should be merged onto a link of the form [[foo]]bar.
3958 *
3959 * @return string
3960 */
3961 public function linkTrail() {
3962 return self::$dataCache->getItem( $this->mCode, 'linkTrail' );
3963 }
3964
3965 /**
3966 * A regular expression character set to match legal word-prefixing
3967 * characters which should be merged onto a link of the form foo[[bar]].
3968 *
3969 * @return string
3970 */
3971 public function linkPrefixCharset() {
3972 return self::$dataCache->getItem( $this->mCode, 'linkPrefixCharset' );
3973 }
3974
3975 /**
3976 * @return Language
3977 */
3978 function getLangObj() {
3979 return $this;
3980 }
3981
3982 /**
3983 * Get the "parent" language which has a converter to convert a "compatible" language
3984 * (in another variant) to this language (eg. zh for zh-cn, but not en for en-gb).
3985 *
3986 * @return Language|null
3987 * @since 1.22
3988 */
3989 public function getParentLanguage() {
3990 if ( $this->mParentLanguage !== false ) {
3991 return $this->mParentLanguage;
3992 }
3993
3994 $pieces = explode( '-', $this->getCode() );
3995 $code = $pieces[0];
3996 if ( !in_array( $code, LanguageConverter::$languagesWithVariants ) ) {
3997 $this->mParentLanguage = null;
3998 return null;
3999 }
4000 $lang = Language::factory( $code );
4001 if ( !$lang->hasVariant( $this->getCode() ) ) {
4002 $this->mParentLanguage = null;
4003 return null;
4004 }
4005
4006 $this->mParentLanguage = $lang;
4007 return $lang;
4008 }
4009
4010 /**
4011 * Get the RFC 3066 code for this language object
4012 *
4013 * NOTE: The return value of this function is NOT HTML-safe and must be escaped with
4014 * htmlspecialchars() or similar
4015 *
4016 * @return string
4017 */
4018 public function getCode() {
4019 return $this->mCode;
4020 }
4021
4022 /**
4023 * Get the code in Bcp47 format which we can use
4024 * inside of html lang="" tags.
4025 *
4026 * NOTE: The return value of this function is NOT HTML-safe and must be escaped with
4027 * htmlspecialchars() or similar.
4028 *
4029 * @since 1.19
4030 * @return string
4031 */
4032 public function getHtmlCode() {
4033 if ( is_null( $this->mHtmlCode ) ) {
4034 $this->mHtmlCode = wfBCP47( $this->getCode() );
4035 }
4036 return $this->mHtmlCode;
4037 }
4038
4039 /**
4040 * @param $code string
4041 */
4042 public function setCode( $code ) {
4043 $this->mCode = $code;
4044 // Ensure we don't leave incorrect cached data lying around
4045 $this->mHtmlCode = null;
4046 $this->mParentLanguage = false;
4047 }
4048
4049 /**
4050 * Get the name of a file for a certain language code
4051 * @param $prefix string Prepend this to the filename
4052 * @param $code string Language code
4053 * @param $suffix string Append this to the filename
4054 * @throws MWException
4055 * @return string $prefix . $mangledCode . $suffix
4056 */
4057 public static function getFileName( $prefix = 'Language', $code, $suffix = '.php' ) {
4058 if ( !self::isValidBuiltInCode( $code ) ) {
4059 throw new MWException( "Invalid language code \"$code\"" );
4060 }
4061
4062 return $prefix . str_replace( '-', '_', ucfirst( $code ) ) . $suffix;
4063 }
4064
4065 /**
4066 * Get the language code from a file name. Inverse of getFileName()
4067 * @param $filename string $prefix . $languageCode . $suffix
4068 * @param $prefix string Prefix before the language code
4069 * @param $suffix string Suffix after the language code
4070 * @return string Language code, or false if $prefix or $suffix isn't found
4071 */
4072 public static function getCodeFromFileName( $filename, $prefix = 'Language', $suffix = '.php' ) {
4073 $m = null;
4074 preg_match( '/' . preg_quote( $prefix, '/' ) . '([A-Z][a-z_]+)' .
4075 preg_quote( $suffix, '/' ) . '/', $filename, $m );
4076 if ( !count( $m ) ) {
4077 return false;
4078 }
4079 return str_replace( '_', '-', strtolower( $m[1] ) );
4080 }
4081
4082 /**
4083 * @param $code string
4084 * @return string
4085 */
4086 public static function getMessagesFileName( $code ) {
4087 global $IP;
4088 $file = self::getFileName( "$IP/languages/messages/Messages", $code, '.php' );
4089 wfRunHooks( 'Language::getMessagesFileName', array( $code, &$file ) );
4090 return $file;
4091 }
4092
4093 /**
4094 * @param $code string
4095 * @return string
4096 * @since 1.23
4097 */
4098 public static function getJsonMessagesFileName( $code ) {
4099 global $IP;
4100
4101 if ( !self::isValidBuiltInCode( $code ) ) {
4102 throw new MWException( "Invalid language code \"$code\"" );
4103 }
4104
4105 return "$IP/languages/i18n/$code.json" ;
4106 }
4107
4108 /**
4109 * @param $code string
4110 * @return string
4111 */
4112 public static function getClassFileName( $code ) {
4113 global $IP;
4114 return self::getFileName( "$IP/languages/classes/Language", $code, '.php' );
4115 }
4116
4117 /**
4118 * Get the first fallback for a given language.
4119 *
4120 * @param $code string
4121 *
4122 * @return bool|string
4123 */
4124 public static function getFallbackFor( $code ) {
4125 if ( $code === 'en' || !Language::isValidBuiltInCode( $code ) ) {
4126 return false;
4127 } else {
4128 $fallbacks = self::getFallbacksFor( $code );
4129 $first = array_shift( $fallbacks );
4130 return $first;
4131 }
4132 }
4133
4134 /**
4135 * Get the ordered list of fallback languages.
4136 *
4137 * @since 1.19
4138 * @param $code string Language code
4139 * @return array
4140 */
4141 public static function getFallbacksFor( $code ) {
4142 if ( $code === 'en' || !Language::isValidBuiltInCode( $code ) ) {
4143 return array();
4144 } else {
4145 $v = self::getLocalisationCache()->getItem( $code, 'fallback' );
4146 $v = array_map( 'trim', explode( ',', $v ) );
4147 if ( $v[count( $v ) - 1] !== 'en' ) {
4148 $v[] = 'en';
4149 }
4150 return $v;
4151 }
4152 }
4153
4154 /**
4155 * Get the ordered list of fallback languages, ending with the fallback
4156 * language chain for the site language.
4157 *
4158 * @since 1.22
4159 * @param string $code Language code
4160 * @return array array( fallbacks, site fallbacks )
4161 */
4162 public static function getFallbacksIncludingSiteLanguage( $code ) {
4163 global $wgLanguageCode;
4164
4165 // Usually, we will only store a tiny number of fallback chains, so we
4166 // keep them in static memory.
4167 $cacheKey = "{$code}-{$wgLanguageCode}";
4168
4169 if ( !array_key_exists( $cacheKey, self::$fallbackLanguageCache ) ) {
4170 $fallbacks = self::getFallbacksFor( $code );
4171
4172 // Append the site's fallback chain, including the site language itself
4173 $siteFallbacks = self::getFallbacksFor( $wgLanguageCode );
4174 array_unshift( $siteFallbacks, $wgLanguageCode );
4175
4176 // Eliminate any languages already included in the chain
4177 $siteFallbacks = array_diff( $siteFallbacks, $fallbacks );
4178
4179 self::$fallbackLanguageCache[$cacheKey] = array( $fallbacks, $siteFallbacks );
4180 }
4181 return self::$fallbackLanguageCache[$cacheKey];
4182 }
4183
4184 /**
4185 * Get all messages for a given language
4186 * WARNING: this may take a long time. If you just need all message *keys*
4187 * but need the *contents* of only a few messages, consider using getMessageKeysFor().
4188 *
4189 * @param $code string
4190 *
4191 * @return array
4192 */
4193 public static function getMessagesFor( $code ) {
4194 return self::getLocalisationCache()->getItem( $code, 'messages' );
4195 }
4196
4197 /**
4198 * Get a message for a given language
4199 *
4200 * @param $key string
4201 * @param $code string
4202 *
4203 * @return string
4204 */
4205 public static function getMessageFor( $key, $code ) {
4206 return self::getLocalisationCache()->getSubitem( $code, 'messages', $key );
4207 }
4208
4209 /**
4210 * Get all message keys for a given language. This is a faster alternative to
4211 * array_keys( Language::getMessagesFor( $code ) )
4212 *
4213 * @since 1.19
4214 * @param $code string Language code
4215 * @return array of message keys (strings)
4216 */
4217 public static function getMessageKeysFor( $code ) {
4218 return self::getLocalisationCache()->getSubItemList( $code, 'messages' );
4219 }
4220
4221 /**
4222 * @param $talk
4223 * @return mixed
4224 */
4225 function fixVariableInNamespace( $talk ) {
4226 if ( strpos( $talk, '$1' ) === false ) {
4227 return $talk;
4228 }
4229
4230 global $wgMetaNamespace;
4231 $talk = str_replace( '$1', $wgMetaNamespace, $talk );
4232
4233 # Allow grammar transformations
4234 # Allowing full message-style parsing would make simple requests
4235 # such as action=raw much more expensive than they need to be.
4236 # This will hopefully cover most cases.
4237 $talk = preg_replace_callback( '/{{grammar:(.*?)\|(.*?)}}/i',
4238 array( &$this, 'replaceGrammarInNamespace' ), $talk );
4239 return str_replace( ' ', '_', $talk );
4240 }
4241
4242 /**
4243 * @param $m string
4244 * @return string
4245 */
4246 function replaceGrammarInNamespace( $m ) {
4247 return $this->convertGrammar( trim( $m[2] ), trim( $m[1] ) );
4248 }
4249
4250 /**
4251 * @throws MWException
4252 * @return array
4253 */
4254 static function getCaseMaps() {
4255 static $wikiUpperChars, $wikiLowerChars;
4256 if ( isset( $wikiUpperChars ) ) {
4257 return array( $wikiUpperChars, $wikiLowerChars );
4258 }
4259
4260 wfProfileIn( __METHOD__ );
4261 $arr = wfGetPrecompiledData( 'Utf8Case.ser' );
4262 if ( $arr === false ) {
4263 throw new MWException(
4264 "Utf8Case.ser is missing, please run \"make\" in the serialized directory\n" );
4265 }
4266 $wikiUpperChars = $arr['wikiUpperChars'];
4267 $wikiLowerChars = $arr['wikiLowerChars'];
4268 wfProfileOut( __METHOD__ );
4269 return array( $wikiUpperChars, $wikiLowerChars );
4270 }
4271
4272 /**
4273 * Decode an expiry (block, protection, etc) which has come from the DB
4274 *
4275 * @todo FIXME: why are we returnings DBMS-dependent strings???
4276 *
4277 * @param $expiry String: Database expiry String
4278 * @param $format Bool|Int true to process using language functions, or TS_ constant
4279 * to return the expiry in a given timestamp
4280 * @return String
4281 * @since 1.18
4282 */
4283 public function formatExpiry( $expiry, $format = true ) {
4284 static $infinity;
4285 if ( $infinity === null ) {
4286 $infinity = wfGetDB( DB_SLAVE )->getInfinity();
4287 }
4288
4289 if ( $expiry == '' || $expiry == $infinity ) {
4290 return $format === true
4291 ? $this->getMessageFromDB( 'infiniteblock' )
4292 : $infinity;
4293 } else {
4294 return $format === true
4295 ? $this->timeanddate( $expiry, /* User preference timezone */ true )
4296 : wfTimestamp( $format, $expiry );
4297 }
4298 }
4299
4300 /**
4301 * @todo Document
4302 * @param $seconds int|float
4303 * @param $format Array Optional
4304 * If $format['avoid'] == 'avoidseconds' - don't mention seconds if $seconds >= 1 hour
4305 * If $format['avoid'] == 'avoidminutes' - don't mention seconds/minutes if $seconds > 48 hours
4306 * If $format['noabbrevs'] is true - use 'seconds' and friends instead of 'seconds-abbrev' and friends
4307 * For backwards compatibility, $format may also be one of the strings 'avoidseconds' or 'avoidminutes'
4308 * @return string
4309 */
4310 function formatTimePeriod( $seconds, $format = array() ) {
4311 if ( !is_array( $format ) ) {
4312 $format = array( 'avoid' => $format ); // For backwards compatibility
4313 }
4314 if ( !isset( $format['avoid'] ) ) {
4315 $format['avoid'] = false;
4316 }
4317 if ( !isset( $format['noabbrevs' ] ) ) {
4318 $format['noabbrevs'] = false;
4319 }
4320 $secondsMsg = wfMessage(
4321 $format['noabbrevs'] ? 'seconds' : 'seconds-abbrev' )->inLanguage( $this );
4322 $minutesMsg = wfMessage(
4323 $format['noabbrevs'] ? 'minutes' : 'minutes-abbrev' )->inLanguage( $this );
4324 $hoursMsg = wfMessage(
4325 $format['noabbrevs'] ? 'hours' : 'hours-abbrev' )->inLanguage( $this );
4326 $daysMsg = wfMessage(
4327 $format['noabbrevs'] ? 'days' : 'days-abbrev' )->inLanguage( $this );
4328
4329 if ( round( $seconds * 10 ) < 100 ) {
4330 $s = $this->formatNum( sprintf( "%.1f", round( $seconds * 10 ) / 10 ) );
4331 $s = $secondsMsg->params( $s )->text();
4332 } elseif ( round( $seconds ) < 60 ) {
4333 $s = $this->formatNum( round( $seconds ) );
4334 $s = $secondsMsg->params( $s )->text();
4335 } elseif ( round( $seconds ) < 3600 ) {
4336 $minutes = floor( $seconds / 60 );
4337 $secondsPart = round( fmod( $seconds, 60 ) );
4338 if ( $secondsPart == 60 ) {
4339 $secondsPart = 0;
4340 $minutes++;
4341 }
4342 $s = $minutesMsg->params( $this->formatNum( $minutes ) )->text();
4343 $s .= ' ';
4344 $s .= $secondsMsg->params( $this->formatNum( $secondsPart ) )->text();
4345 } elseif ( round( $seconds ) <= 2 * 86400 ) {
4346 $hours = floor( $seconds / 3600 );
4347 $minutes = floor( ( $seconds - $hours * 3600 ) / 60 );
4348 $secondsPart = round( $seconds - $hours * 3600 - $minutes * 60 );
4349 if ( $secondsPart == 60 ) {
4350 $secondsPart = 0;
4351 $minutes++;
4352 }
4353 if ( $minutes == 60 ) {
4354 $minutes = 0;
4355 $hours++;
4356 }
4357 $s = $hoursMsg->params( $this->formatNum( $hours ) )->text();
4358 $s .= ' ';
4359 $s .= $minutesMsg->params( $this->formatNum( $minutes ) )->text();
4360 if ( !in_array( $format['avoid'], array( 'avoidseconds', 'avoidminutes' ) ) ) {
4361 $s .= ' ' . $secondsMsg->params( $this->formatNum( $secondsPart ) )->text();
4362 }
4363 } else {
4364 $days = floor( $seconds / 86400 );
4365 if ( $format['avoid'] === 'avoidminutes' ) {
4366 $hours = round( ( $seconds - $days * 86400 ) / 3600 );
4367 if ( $hours == 24 ) {
4368 $hours = 0;
4369 $days++;
4370 }
4371 $s = $daysMsg->params( $this->formatNum( $days ) )->text();
4372 $s .= ' ';
4373 $s .= $hoursMsg->params( $this->formatNum( $hours ) )->text();
4374 } elseif ( $format['avoid'] === 'avoidseconds' ) {
4375 $hours = floor( ( $seconds - $days * 86400 ) / 3600 );
4376 $minutes = round( ( $seconds - $days * 86400 - $hours * 3600 ) / 60 );
4377 if ( $minutes == 60 ) {
4378 $minutes = 0;
4379 $hours++;
4380 }
4381 if ( $hours == 24 ) {
4382 $hours = 0;
4383 $days++;
4384 }
4385 $s = $daysMsg->params( $this->formatNum( $days ) )->text();
4386 $s .= ' ';
4387 $s .= $hoursMsg->params( $this->formatNum( $hours ) )->text();
4388 $s .= ' ';
4389 $s .= $minutesMsg->params( $this->formatNum( $minutes ) )->text();
4390 } else {
4391 $s = $daysMsg->params( $this->formatNum( $days ) )->text();
4392 $s .= ' ';
4393 $s .= $this->formatTimePeriod( $seconds - $days * 86400, $format );
4394 }
4395 }
4396 return $s;
4397 }
4398
4399 /**
4400 * Format a bitrate for output, using an appropriate
4401 * unit (bps, kbps, Mbps, Gbps, Tbps, Pbps, Ebps, Zbps or Ybps) according to the magnitude in question
4402 *
4403 * This use base 1000. For base 1024 use formatSize(), for another base
4404 * see formatComputingNumbers()
4405 *
4406 * @param $bps int
4407 * @return string
4408 */
4409 function formatBitrate( $bps ) {
4410 return $this->formatComputingNumbers( $bps, 1000, "bitrate-$1bits" );
4411 }
4412
4413 /**
4414 * @param $size int Size of the unit
4415 * @param $boundary int Size boundary (1000, or 1024 in most cases)
4416 * @param $messageKey string Message key to be uesd
4417 * @return string
4418 */
4419 function formatComputingNumbers( $size, $boundary, $messageKey ) {
4420 if ( $size <= 0 ) {
4421 return str_replace( '$1', $this->formatNum( $size ),
4422 $this->getMessageFromDB( str_replace( '$1', '', $messageKey ) )
4423 );
4424 }
4425 $sizes = array( '', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zeta', 'yotta' );
4426 $index = 0;
4427
4428 $maxIndex = count( $sizes ) - 1;
4429 while ( $size >= $boundary && $index < $maxIndex ) {
4430 $index++;
4431 $size /= $boundary;
4432 }
4433
4434 // For small sizes no decimal places necessary
4435 $round = 0;
4436 if ( $index > 1 ) {
4437 // For MB and bigger two decimal places are smarter
4438 $round = 2;
4439 }
4440 $msg = str_replace( '$1', $sizes[$index], $messageKey );
4441
4442 $size = round( $size, $round );
4443 $text = $this->getMessageFromDB( $msg );
4444 return str_replace( '$1', $this->formatNum( $size ), $text );
4445 }
4446
4447 /**
4448 * Format a size in bytes for output, using an appropriate
4449 * unit (B, KB, MB, GB, TB, PB, EB, ZB or YB) according to the magnitude in question
4450 *
4451 * This method use base 1024. For base 1000 use formatBitrate(), for
4452 * another base see formatComputingNumbers()
4453 *
4454 * @param $size int Size to format
4455 * @return string Plain text (not HTML)
4456 */
4457 function formatSize( $size ) {
4458 return $this->formatComputingNumbers( $size, 1024, "size-$1bytes" );
4459 }
4460
4461 /**
4462 * Make a list item, used by various special pages
4463 *
4464 * @param $page String Page link
4465 * @param $details String Text between brackets
4466 * @param $oppositedm Boolean Add the direction mark opposite to your
4467 * language, to display text properly
4468 * @return String
4469 */
4470 function specialList( $page, $details, $oppositedm = true ) {
4471 $dirmark = ( $oppositedm ? $this->getDirMark( true ) : '' ) .
4472 $this->getDirMark();
4473 $details = $details ? $dirmark . $this->getMessageFromDB( 'word-separator' ) .
4474 wfMessage( 'parentheses' )->rawParams( $details )->inLanguage( $this )->escaped() : '';
4475 return $page . $details;
4476 }
4477
4478 /**
4479 * Generate (prev x| next x) (20|50|100...) type links for paging
4480 *
4481 * @param $title Title object to link
4482 * @param $offset Integer offset parameter
4483 * @param $limit Integer limit parameter
4484 * @param $query array|String optional URL query parameter string
4485 * @param $atend Bool optional param for specified if this is the last page
4486 * @return String
4487 */
4488 public function viewPrevNext( Title $title, $offset, $limit, array $query = array(), $atend = false ) {
4489 // @todo FIXME: Why on earth this needs one message for the text and another one for tooltip?
4490
4491 # Make 'previous' link
4492 $prev = wfMessage( 'prevn' )->inLanguage( $this )->title( $title )->numParams( $limit )->text();
4493 if ( $offset > 0 ) {
4494 $plink = $this->numLink( $title, max( $offset - $limit, 0 ), $limit,
4495 $query, $prev, 'prevn-title', 'mw-prevlink' );
4496 } else {
4497 $plink = htmlspecialchars( $prev );
4498 }
4499
4500 # Make 'next' link
4501 $next = wfMessage( 'nextn' )->inLanguage( $this )->title( $title )->numParams( $limit )->text();
4502 if ( $atend ) {
4503 $nlink = htmlspecialchars( $next );
4504 } else {
4505 $nlink = $this->numLink( $title, $offset + $limit, $limit,
4506 $query, $next, 'nextn-title', 'mw-nextlink' );
4507 }
4508
4509 # Make links to set number of items per page
4510 $numLinks = array();
4511 foreach ( array( 20, 50, 100, 250, 500 ) as $num ) {
4512 $numLinks[] = $this->numLink( $title, $offset, $num,
4513 $query, $this->formatNum( $num ), 'shown-title', 'mw-numlink' );
4514 }
4515
4516 return wfMessage( 'viewprevnext' )->inLanguage( $this )->title( $title
4517 )->rawParams( $plink, $nlink, $this->pipeList( $numLinks ) )->escaped();
4518 }
4519
4520 /**
4521 * Helper function for viewPrevNext() that generates links
4522 *
4523 * @param $title Title object to link
4524 * @param $offset Integer offset parameter
4525 * @param $limit Integer limit parameter
4526 * @param $query Array extra query parameters
4527 * @param $link String text to use for the link; will be escaped
4528 * @param $tooltipMsg String name of the message to use as tooltip
4529 * @param $class String value of the "class" attribute of the link
4530 * @return String HTML fragment
4531 */
4532 private function numLink( Title $title, $offset, $limit, array $query, $link, $tooltipMsg, $class ) {
4533 $query = array( 'limit' => $limit, 'offset' => $offset ) + $query;
4534 $tooltip = wfMessage( $tooltipMsg )->inLanguage( $this )->title( $title )->numParams( $limit )->text();
4535 return Html::element( 'a', array( 'href' => $title->getLocalURL( $query ),
4536 'title' => $tooltip, 'class' => $class ), $link );
4537 }
4538
4539 /**
4540 * Get the conversion rule title, if any.
4541 *
4542 * @return string
4543 */
4544 public function getConvRuleTitle() {
4545 return $this->mConverter->getConvRuleTitle();
4546 }
4547
4548 /**
4549 * Get the compiled plural rules for the language
4550 * @since 1.20
4551 * @return array Associative array with plural form, and plural rule as key-value pairs
4552 */
4553 public function getCompiledPluralRules() {
4554 $pluralRules = self::$dataCache->getItem( strtolower( $this->mCode ), 'compiledPluralRules' );
4555 $fallbacks = Language::getFallbacksFor( $this->mCode );
4556 if ( !$pluralRules ) {
4557 foreach ( $fallbacks as $fallbackCode ) {
4558 $pluralRules = self::$dataCache->getItem( strtolower( $fallbackCode ), 'compiledPluralRules' );
4559 if ( $pluralRules ) {
4560 break;
4561 }
4562 }
4563 }
4564 return $pluralRules;
4565 }
4566
4567 /**
4568 * Get the plural rules for the language
4569 * @since 1.20
4570 * @return array Associative array with plural form number and plural rule as key-value pairs
4571 */
4572 public function getPluralRules() {
4573 $pluralRules = self::$dataCache->getItem( strtolower( $this->mCode ), 'pluralRules' );
4574 $fallbacks = Language::getFallbacksFor( $this->mCode );
4575 if ( !$pluralRules ) {
4576 foreach ( $fallbacks as $fallbackCode ) {
4577 $pluralRules = self::$dataCache->getItem( strtolower( $fallbackCode ), 'pluralRules' );
4578 if ( $pluralRules ) {
4579 break;
4580 }
4581 }
4582 }
4583 return $pluralRules;
4584 }
4585
4586 /**
4587 * Get the plural rule types for the language
4588 * @since 1.22
4589 * @return array Associative array with plural form number and plural rule type as key-value pairs
4590 */
4591 public function getPluralRuleTypes() {
4592 $pluralRuleTypes = self::$dataCache->getItem( strtolower( $this->mCode ), 'pluralRuleTypes' );
4593 $fallbacks = Language::getFallbacksFor( $this->mCode );
4594 if ( !$pluralRuleTypes ) {
4595 foreach ( $fallbacks as $fallbackCode ) {
4596 $pluralRuleTypes = self::$dataCache->getItem( strtolower( $fallbackCode ), 'pluralRuleTypes' );
4597 if ( $pluralRuleTypes ) {
4598 break;
4599 }
4600 }
4601 }
4602 return $pluralRuleTypes;
4603 }
4604
4605 /**
4606 * Find the index number of the plural rule appropriate for the given number
4607 * @return int The index number of the plural rule
4608 */
4609 public function getPluralRuleIndexNumber( $number ) {
4610 $pluralRules = $this->getCompiledPluralRules();
4611 $form = CLDRPluralRuleEvaluator::evaluateCompiled( $number, $pluralRules );
4612 return $form;
4613 }
4614
4615 /**
4616 * Find the plural rule type appropriate for the given number
4617 * For example, if the language is set to Arabic, getPluralType(5) should
4618 * return 'few'.
4619 * @since 1.22
4620 * @return string The name of the plural rule type, e.g. one, two, few, many
4621 */
4622 public function getPluralRuleType( $number ) {
4623 $index = $this->getPluralRuleIndexNumber( $number );
4624 $pluralRuleTypes = $this->getPluralRuleTypes();
4625 if ( isset( $pluralRuleTypes[$index] ) ) {
4626 return $pluralRuleTypes[$index];
4627 } else {
4628 return 'other';
4629 }
4630 }
4631 }