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