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