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