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