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