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