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