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