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