5d862b4ffd3eaee38c64385f3a72065da2e74116
[lhc/web/wiklou.git] / languages / Language.php
1 <?php
2 /**
3 * @addtogroup Language
4 */
5
6 if( !defined( 'MEDIAWIKI' ) ) {
7 echo "This file is part of MediaWiki, it is not a valid entry point.\n";
8 exit( 1 );
9 }
10
11 # Read language names
12 global $wgLanguageNames;
13 require_once( dirname(__FILE__) . '/Names.php' ) ;
14
15 global $wgInputEncoding, $wgOutputEncoding;
16
17 /**
18 * These are always UTF-8, they exist only for backwards compatibility
19 */
20 $wgInputEncoding = "UTF-8";
21 $wgOutputEncoding = "UTF-8";
22
23 if( function_exists( 'mb_strtoupper' ) ) {
24 mb_internal_encoding('UTF-8');
25 }
26
27 /* a fake language converter */
28 class FakeConverter {
29 var $mLang;
30 function FakeConverter($langobj) {$this->mLang = $langobj;}
31 function convert($t, $i) {return $t;}
32 function parserConvert($t, $p) {return $t;}
33 function getVariants() { return array( $this->mLang->getCode() ); }
34 function getPreferredVariant() {return $this->mLang->getCode(); }
35 function findVariantLink(&$l, &$n) {}
36 function getExtraHashOptions() {return '';}
37 function getParsedTitle() {return '';}
38 function markNoConversion($text, $noParse=false) {return $text;}
39 function convertCategoryKey( $key ) {return $key; }
40 function convertLinkToAllVariants($text){ return array( $this->mLang->getCode() => $text); }
41 function armourMath($text){ return $text; }
42 }
43
44 #--------------------------------------------------------------------------
45 # Internationalisation code
46 #--------------------------------------------------------------------------
47
48 class Language {
49 var $mConverter, $mVariants, $mCode, $mLoaded = false;
50 var $mMagicExtensions = array(), $mMagicHookDone = false;
51
52 static public $mLocalisationKeys = array( 'fallback', 'namespaceNames',
53 'skinNames', 'mathNames',
54 'bookstoreList', 'magicWords', 'messages', 'rtl', 'digitTransformTable',
55 'separatorTransformTable', 'fallback8bitEncoding', 'linkPrefixExtension',
56 'defaultUserOptionOverrides', 'linkTrail', 'namespaceAliases',
57 'dateFormats', 'datePreferences', 'datePreferenceMigrationMap',
58 'defaultDateFormat', 'extraUserToggles', 'specialPageAliases' );
59
60 static public $mMergeableMapKeys = array( 'messages', 'namespaceNames', 'mathNames',
61 'dateFormats', 'defaultUserOptionOverrides', 'magicWords' );
62
63 static public $mMergeableListKeys = array( 'extraUserToggles' );
64
65 static public $mMergeableAliasListKeys = array( 'specialPageAliases' );
66
67 static public $mLocalisationCache = array();
68
69 static public $mWeekdayMsgs = array(
70 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday',
71 'friday', 'saturday'
72 );
73
74 static public $mWeekdayAbbrevMsgs = array(
75 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'
76 );
77
78 static public $mMonthMsgs = array(
79 'january', 'february', 'march', 'april', 'may_long', 'june',
80 'july', 'august', 'september', 'october', 'november',
81 'december'
82 );
83 static public $mMonthGenMsgs = array(
84 'january-gen', 'february-gen', 'march-gen', 'april-gen', 'may-gen', 'june-gen',
85 'july-gen', 'august-gen', 'september-gen', 'october-gen', 'november-gen',
86 'december-gen'
87 );
88 static public $mMonthAbbrevMsgs = array(
89 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
90 'sep', 'oct', 'nov', 'dec'
91 );
92
93 static public $mIranianCalendarMonthMsgs = array(
94 'iranian-calendar-m1', 'iranian-calendar-m2', 'iranian-calendar-m3',
95 'iranian-calendar-m4', 'iranian-calendar-m5', 'iranian-calendar-m6',
96 'iranian-calendar-m7', 'iranian-calendar-m8', 'iranian-calendar-m9',
97 'iranian-calendar-m10', 'iranian-calendar-m11', 'iranian-calendar-m12'
98 );
99
100 static public $mHebrewCalendarMonthMsgs = array(
101 'hebrew-calendar-m1', 'hebrew-calendar-m2', 'hebrew-calendar-m3',
102 'hebrew-calendar-m4', 'hebrew-calendar-m5', 'hebrew-calendar-m6',
103 'hebrew-calendar-m7', 'hebrew-calendar-m8', 'hebrew-calendar-m9',
104 'hebrew-calendar-m10', 'hebrew-calendar-m11', 'hebrew-calendar-m12',
105 'hebrew-calendar-m6a', 'hebrew-calendar-m6b'
106 );
107
108 static public $mHebrewCalendarMonthGenMsgs = array(
109 'hebrew-calendar-m1-gen', 'hebrew-calendar-m2-gen', 'hebrew-calendar-m3-gen',
110 'hebrew-calendar-m4-gen', 'hebrew-calendar-m5-gen', 'hebrew-calendar-m6-gen',
111 'hebrew-calendar-m7-gen', 'hebrew-calendar-m8-gen', 'hebrew-calendar-m9-gen',
112 'hebrew-calendar-m10-gen', 'hebrew-calendar-m11-gen', 'hebrew-calendar-m12-gen',
113 'hebrew-calendar-m6a-gen', 'hebrew-calendar-m6b-gen'
114 );
115
116 /**
117 * Create a language object for a given language code
118 */
119 static function factory( $code ) {
120 global $IP;
121 static $recursionLevel = 0;
122
123 if ( $code == 'en' ) {
124 $class = 'Language';
125 } else {
126 $class = 'Language' . str_replace( '-', '_', ucfirst( $code ) );
127 // Preload base classes to work around APC/PHP5 bug
128 if ( file_exists( "$IP/languages/classes/$class.deps.php" ) ) {
129 include_once("$IP/languages/classes/$class.deps.php");
130 }
131 if ( file_exists( "$IP/languages/classes/$class.php" ) ) {
132 include_once("$IP/languages/classes/$class.php");
133 }
134 }
135
136 if ( $recursionLevel > 5 ) {
137 throw new MWException( "Language fallback loop detected when creating class $class\n" );
138 }
139
140 if( ! class_exists( $class ) ) {
141 $fallback = Language::getFallbackFor( $code );
142 ++$recursionLevel;
143 $lang = Language::factory( $fallback );
144 --$recursionLevel;
145 $lang->setCode( $code );
146 } else {
147 $lang = new $class;
148 }
149
150 return $lang;
151 }
152
153 function __construct() {
154 $this->mConverter = new FakeConverter($this);
155 // Set the code to the name of the descendant
156 if ( get_class( $this ) == 'Language' ) {
157 $this->mCode = 'en';
158 } else {
159 $this->mCode = str_replace( '_', '-', strtolower( substr( get_class( $this ), 8 ) ) );
160 }
161 }
162
163 /**
164 * Hook which will be called if this is the content language.
165 * Descendants can use this to register hook functions or modify globals
166 */
167 function initContLang() {}
168
169 /**
170 * @deprecated
171 * @return array
172 */
173 function getDefaultUserOptions() {
174 return User::getDefaultOptions();
175 }
176
177 function getFallbackLanguageCode() {
178 $this->load();
179 return $this->fallback;
180 }
181
182 /**
183 * Exports $wgBookstoreListEn
184 * @return array
185 */
186 function getBookstoreList() {
187 $this->load();
188 return $this->bookstoreList;
189 }
190
191 /**
192 * @return array
193 */
194 function getNamespaces() {
195 $this->load();
196 return $this->namespaceNames;
197 }
198
199 /**
200 * A convenience function that returns the same thing as
201 * getNamespaces() except with the array values changed to ' '
202 * where it found '_', useful for producing output to be displayed
203 * e.g. in <select> forms.
204 *
205 * @return array
206 */
207 function getFormattedNamespaces() {
208 $ns = $this->getNamespaces();
209 foreach($ns as $k => $v) {
210 $ns[$k] = strtr($v, '_', ' ');
211 }
212 return $ns;
213 }
214
215 /**
216 * Get a namespace value by key
217 * <code>
218 * $mw_ns = $wgContLang->getNsText( NS_MEDIAWIKI );
219 * echo $mw_ns; // prints 'MediaWiki'
220 * </code>
221 *
222 * @param int $index the array key of the namespace to return
223 * @return mixed, string if the namespace value exists, otherwise false
224 */
225 function getNsText( $index ) {
226 $ns = $this->getNamespaces();
227 return isset( $ns[$index] ) ? $ns[$index] : false;
228 }
229
230 /**
231 * A convenience function that returns the same thing as
232 * getNsText() except with '_' changed to ' ', useful for
233 * producing output.
234 *
235 * @return array
236 */
237 function getFormattedNsText( $index ) {
238 $ns = $this->getNsText( $index );
239 return strtr($ns, '_', ' ');
240 }
241
242 /**
243 * Get a namespace key by value, case insensitive.
244 * Only matches namespace names for the current language, not the
245 * canonical ones defined in Namespace.php.
246 *
247 * @param string $text
248 * @return mixed An integer if $text is a valid value otherwise false
249 */
250 function getLocalNsIndex( $text ) {
251 $this->load();
252 $lctext = $this->lc($text);
253 return isset( $this->mNamespaceIds[$lctext] ) ? $this->mNamespaceIds[$lctext] : false;
254 }
255
256 /**
257 * Get a namespace key by value, case insensitive. Canonical namespace
258 * names override custom ones defined for the current language.
259 *
260 * @param string $text
261 * @return mixed An integer if $text is a valid value otherwise false
262 */
263 function getNsIndex( $text ) {
264 $this->load();
265 $lctext = $this->lc($text);
266 if( ( $ns = Namespace::getCanonicalIndex( $lctext ) ) !== null ) return $ns;
267 return isset( $this->mNamespaceIds[$lctext] ) ? $this->mNamespaceIds[$lctext] : false;
268 }
269
270 /**
271 * short names for language variants used for language conversion links.
272 *
273 * @param string $code
274 * @return string
275 */
276 function getVariantname( $code ) {
277 return $this->getMessageFromDB( "variantname-$code" );
278 }
279
280 function specialPage( $name ) {
281 $aliases = $this->getSpecialPageAliases();
282 if ( isset( $aliases[$name][0] ) ) {
283 $name = $aliases[$name][0];
284 }
285 return $this->getNsText(NS_SPECIAL) . ':' . $name;
286 }
287
288 function getQuickbarSettings() {
289 return array(
290 $this->getMessage( 'qbsettings-none' ),
291 $this->getMessage( 'qbsettings-fixedleft' ),
292 $this->getMessage( 'qbsettings-fixedright' ),
293 $this->getMessage( 'qbsettings-floatingleft' ),
294 $this->getMessage( 'qbsettings-floatingright' )
295 );
296 }
297
298 function getSkinNames() {
299 $this->load();
300 return $this->skinNames;
301 }
302
303 function getMathNames() {
304 $this->load();
305 return $this->mathNames;
306 }
307
308 function getDatePreferences() {
309 $this->load();
310 return $this->datePreferences;
311 }
312
313 function getDateFormats() {
314 $this->load();
315 return $this->dateFormats;
316 }
317
318 function getDefaultDateFormat() {
319 $this->load();
320 return $this->defaultDateFormat;
321 }
322
323 function getDatePreferenceMigrationMap() {
324 $this->load();
325 return $this->datePreferenceMigrationMap;
326 }
327
328 function getDefaultUserOptionOverrides() {
329 $this->load();
330 # XXX - apparently some languageas get empty arrays, didn't get to it yet -- midom
331 if (is_array($this->defaultUserOptionOverrides)) {
332 return $this->defaultUserOptionOverrides;
333 } else {
334 return array();
335 }
336 }
337
338 function getExtraUserToggles() {
339 $this->load();
340 return $this->extraUserToggles;
341 }
342
343 function getUserToggle( $tog ) {
344 return $this->getMessageFromDB( "tog-$tog" );
345 }
346
347 /**
348 * Get language names, indexed by code.
349 * If $customisedOnly is true, only returns codes with a messages file
350 */
351 public static function getLanguageNames( $customisedOnly = false ) {
352 global $wgLanguageNames, $wgExtraLanguageNames;
353 $allNames = $wgExtraLanguageNames + $wgLanguageNames;
354 if ( !$customisedOnly ) {
355 return $allNames;
356 }
357
358 global $IP;
359 $names = array();
360 $dir = opendir( "$IP/languages/messages" );
361 while( false !== ( $file = readdir( $dir ) ) ) {
362 $m = array();
363 if( preg_match( '/Messages([A-Z][a-z_]+)\.php$/', $file, $m ) ) {
364 $code = str_replace( '_', '-', strtolower( $m[1] ) );
365 if ( isset( $allNames[$code] ) ) {
366 $names[$code] = $allNames[$code];
367 }
368 }
369 }
370 closedir( $dir );
371 return $names;
372 }
373
374 /**
375 * Ugly hack to get a message maybe from the MediaWiki namespace, if this
376 * language object is the content or user language.
377 */
378 function getMessageFromDB( $msg ) {
379 global $wgContLang, $wgLang;
380 if ( $wgContLang->getCode() == $this->getCode() ) {
381 # Content language
382 return wfMsgForContent( $msg );
383 } elseif ( $wgLang->getCode() == $this->getCode() ) {
384 # User language
385 return wfMsg( $msg );
386 } else {
387 # Neither, get from localisation
388 return $this->getMessage( $msg );
389 }
390 }
391
392 function getLanguageName( $code ) {
393 $names = self::getLanguageNames();
394 if ( !array_key_exists( $code, $names ) ) {
395 return '';
396 }
397 return $names[$code];
398 }
399
400 function getMonthName( $key ) {
401 return $this->getMessageFromDB( self::$mMonthMsgs[$key-1] );
402 }
403
404 function getMonthNameGen( $key ) {
405 return $this->getMessageFromDB( self::$mMonthGenMsgs[$key-1] );
406 }
407
408 function getMonthAbbreviation( $key ) {
409 return $this->getMessageFromDB( self::$mMonthAbbrevMsgs[$key-1] );
410 }
411
412 function getWeekdayName( $key ) {
413 return $this->getMessageFromDB( self::$mWeekdayMsgs[$key-1] );
414 }
415
416 function getWeekdayAbbreviation( $key ) {
417 return $this->getMessageFromDB( self::$mWeekdayAbbrevMsgs[$key-1] );
418 }
419
420 function getIranianCalendarMonthName( $key ) {
421 return $this->getMessageFromDB( self::$mIranianCalendarMonthMsgs[$key-1] );
422 }
423
424 function getHebrewCalendarMonthName( $key ) {
425 return $this->getMessageFromDB( self::$mHebrewCalendarMonthMsgs[$key-1] );
426 }
427
428 function getHebrewCalendarMonthNameGen( $key ) {
429 return $this->getMessageFromDB( self::$mHebrewCalendarMonthGenMsgs[$key-1] );
430 }
431
432
433 /**
434 * Used by date() and time() to adjust the time output.
435 * @public
436 * @param int $ts the time in date('YmdHis') format
437 * @param mixed $tz adjust the time by this amount (default false,
438 * mean we get user timecorrection setting)
439 * @return int
440 */
441 function userAdjust( $ts, $tz = false ) {
442 global $wgUser, $wgLocalTZoffset;
443
444 if (!$tz) {
445 $tz = $wgUser->getOption( 'timecorrection' );
446 }
447
448 # minutes and hours differences:
449 $minDiff = 0;
450 $hrDiff = 0;
451
452 if ( $tz === '' ) {
453 # Global offset in minutes.
454 if( isset($wgLocalTZoffset) ) {
455 if( $wgLocalTZoffset >= 0 ) {
456 $hrDiff = floor($wgLocalTZoffset / 60);
457 } else {
458 $hrDiff = ceil($wgLocalTZoffset / 60);
459 }
460 $minDiff = $wgLocalTZoffset % 60;
461 }
462 } elseif ( strpos( $tz, ':' ) !== false ) {
463 $tzArray = explode( ':', $tz );
464 $hrDiff = intval($tzArray[0]);
465 $minDiff = intval($hrDiff < 0 ? -$tzArray[1] : $tzArray[1]);
466 } else {
467 $hrDiff = intval( $tz );
468 }
469
470 # No difference ? Return time unchanged
471 if ( 0 == $hrDiff && 0 == $minDiff ) { return $ts; }
472
473 wfSuppressWarnings(); // E_STRICT system time bitching
474 # Generate an adjusted date
475 $t = mktime( (
476 (int)substr( $ts, 8, 2) ) + $hrDiff, # Hours
477 (int)substr( $ts, 10, 2 ) + $minDiff, # Minutes
478 (int)substr( $ts, 12, 2 ), # Seconds
479 (int)substr( $ts, 4, 2 ), # Month
480 (int)substr( $ts, 6, 2 ), # Day
481 (int)substr( $ts, 0, 4 ) ); #Year
482
483 $date = date( 'YmdHis', $t );
484 wfRestoreWarnings();
485
486 return $date;
487 }
488
489 /**
490 * This is a workalike of PHP's date() function, but with better
491 * internationalisation, a reduced set of format characters, and a better
492 * escaping format.
493 *
494 * Supported format characters are dDjlNwzWFmMntLYyaAgGhHiscrU. See the
495 * PHP manual for definitions. There are a number of extensions, which
496 * start with "x":
497 *
498 * xn Do not translate digits of the next numeric format character
499 * xN Toggle raw digit (xn) flag, stays set until explicitly unset
500 * xr Use roman numerals for the next numeric format character
501 * xh Use hebrew numerals for the next numeric format character
502 * xx Literal x
503 * xg Genitive month name
504 *
505 * xij j (day number) in Iranian calendar
506 * xiF F (month name) in Iranian calendar
507 * xin n (month number) in Iranian calendar
508 * xiY Y (full year) in Iranian calendar
509 *
510 * xjj j (day number) in Hebrew calendar
511 * xjF F (month name) in Hebrew calendar
512 * xjx xg (genitive month name) in Hebrew calendar
513 * xjn n (month number) in Hebrew calendar
514 * xjY Y (full year) in Hebrew calendar
515 *
516 * xkY Y (full year) in Thai solar calendar. Months and days are
517 * identical to the Gregorian calendar
518 *
519 * Characters enclosed in double quotes will be considered literal (with
520 * the quotes themselves removed). Unmatched quotes will be considered
521 * literal quotes. Example:
522 *
523 * "The month is" F => The month is January
524 * i's" => 20'11"
525 *
526 * Backslash escaping is also supported.
527 *
528 * Input timestamp is assumed to be pre-normalized to the desired local
529 * time zone, if any.
530 *
531 * @param string $format
532 * @param string $ts 14-character timestamp
533 * YYYYMMDDHHMMSS
534 * 01234567890123
535 */
536 function sprintfDate( $format, $ts ) {
537 $s = '';
538 $raw = false;
539 $roman = false;
540 $hebrewNum = false;
541 $unix = false;
542 $rawToggle = false;
543 $iranian = false;
544 $hebrew = false;
545 $thai = false;
546 for ( $p = 0; $p < strlen( $format ); $p++ ) {
547 $num = false;
548 $code = $format[$p];
549 if ( $code == 'x' && $p < strlen( $format ) - 1 ) {
550 $code .= $format[++$p];
551 }
552
553 if ( ( $code === 'xi' || $code == 'xj' || $code == 'xk' ) && $p < strlen( $format ) - 1 ) {
554 $code .= $format[++$p];
555 }
556
557 switch ( $code ) {
558 case 'xx':
559 $s .= 'x';
560 break;
561 case 'xn':
562 $raw = true;
563 break;
564 case 'xN':
565 $rawToggle = !$rawToggle;
566 break;
567 case 'xr':
568 $roman = true;
569 break;
570 case 'xh':
571 $hebrewNum = true;
572 break;
573 case 'xg':
574 $s .= $this->getMonthNameGen( substr( $ts, 4, 2 ) );
575 break;
576 case 'xjx':
577 if ( !$hebrew ) $hebrew = self::tsToHebrew( $ts );
578 $s .= $this->getHebrewCalendarMonthNameGen( $hebrew[1] );
579 break;
580 case 'd':
581 $num = substr( $ts, 6, 2 );
582 break;
583 case 'D':
584 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
585 $s .= $this->getWeekdayAbbreviation( gmdate( 'w', $unix ) + 1 );
586 break;
587 case 'j':
588 $num = intval( substr( $ts, 6, 2 ) );
589 break;
590 case 'xij':
591 if ( !$iranian ) $iranian = self::tsToIranian( $ts );
592 $num = $iranian[2];
593 break;
594 case 'xjj':
595 if ( !$hebrew ) $hebrew = self::tsToHebrew( $ts );
596 $num = $hebrew[2];
597 break;
598 case 'l':
599 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
600 $s .= $this->getWeekdayName( gmdate( 'w', $unix ) + 1 );
601 break;
602 case 'N':
603 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
604 $w = gmdate( 'w', $unix );
605 $num = $w ? $w : 7;
606 break;
607 case 'w':
608 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
609 $num = gmdate( 'w', $unix );
610 break;
611 case 'z':
612 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
613 $num = gmdate( 'z', $unix );
614 break;
615 case 'W':
616 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
617 $num = gmdate( 'W', $unix );
618 break;
619 case 'F':
620 $s .= $this->getMonthName( substr( $ts, 4, 2 ) );
621 break;
622 case 'xiF':
623 if ( !$iranian ) $iranian = self::tsToIranian( $ts );
624 $s .= $this->getIranianCalendarMonthName( $iranian[1] );
625 break;
626 case 'xjF':
627 if ( !$hebrew ) $hebrew = self::tsToHebrew( $ts );
628 $s .= $this->getHebrewCalendarMonthName( $hebrew[1] );
629 break;
630 case 'm':
631 $num = substr( $ts, 4, 2 );
632 break;
633 case 'M':
634 $s .= $this->getMonthAbbreviation( substr( $ts, 4, 2 ) );
635 break;
636 case 'n':
637 $num = intval( substr( $ts, 4, 2 ) );
638 break;
639 case 'xin':
640 if ( !$iranian ) $iranian = self::tsToIranian( $ts );
641 $num = $iranian[1];
642 break;
643 case 'xjn':
644 if ( !$hebrew ) $hebrew = self::tsToHebrew( $ts );
645 $num = $hebrew[1];
646 break;
647 case 't':
648 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
649 $num = gmdate( 't', $unix );
650 break;
651 case 'L':
652 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
653 $num = gmdate( 'L', $unix );
654 break;
655 case 'Y':
656 $num = substr( $ts, 0, 4 );
657 break;
658 case 'xiY':
659 if ( !$iranian ) $iranian = self::tsToIranian( $ts );
660 $num = $iranian[0];
661 break;
662 case 'xjY':
663 if ( !$hebrew ) $hebrew = self::tsToHebrew( $ts );
664 $num = $hebrew[0];
665 break;
666 case 'xkY':
667 if ( !$thai ) $thai = self::tsToThai( $ts );
668 $num = $thai[0];
669 break;
670 case 'y':
671 $num = substr( $ts, 2, 2 );
672 break;
673 case 'a':
674 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ? 'am' : 'pm';
675 break;
676 case 'A':
677 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ? 'AM' : 'PM';
678 break;
679 case 'g':
680 $h = substr( $ts, 8, 2 );
681 $num = $h % 12 ? $h % 12 : 12;
682 break;
683 case 'G':
684 $num = intval( substr( $ts, 8, 2 ) );
685 break;
686 case 'h':
687 $h = substr( $ts, 8, 2 );
688 $num = sprintf( '%02d', $h % 12 ? $h % 12 : 12 );
689 break;
690 case 'H':
691 $num = substr( $ts, 8, 2 );
692 break;
693 case 'i':
694 $num = substr( $ts, 10, 2 );
695 break;
696 case 's':
697 $num = substr( $ts, 12, 2 );
698 break;
699 case 'c':
700 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
701 $s .= gmdate( 'c', $unix );
702 break;
703 case 'r':
704 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
705 $s .= gmdate( 'r', $unix );
706 break;
707 case 'U':
708 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
709 $num = $unix;
710 break;
711 case '\\':
712 # Backslash escaping
713 if ( $p < strlen( $format ) - 1 ) {
714 $s .= $format[++$p];
715 } else {
716 $s .= '\\';
717 }
718 break;
719 case '"':
720 # Quoted literal
721 if ( $p < strlen( $format ) - 1 ) {
722 $endQuote = strpos( $format, '"', $p + 1 );
723 if ( $endQuote === false ) {
724 # No terminating quote, assume literal "
725 $s .= '"';
726 } else {
727 $s .= substr( $format, $p + 1, $endQuote - $p - 1 );
728 $p = $endQuote;
729 }
730 } else {
731 # Quote at end of string, assume literal "
732 $s .= '"';
733 }
734 break;
735 default:
736 $s .= $format[$p];
737 }
738 if ( $num !== false ) {
739 if ( $rawToggle || $raw ) {
740 $s .= $num;
741 $raw = false;
742 } elseif ( $roman ) {
743 $s .= self::romanNumeral( $num );
744 $roman = false;
745 } elseif( $hebrewNum ) {
746 $s .= self::hebrewNumeral( $num );
747 $hebrewNum = false;
748 } else {
749 $s .= $this->formatNum( $num, true );
750 }
751 $num = false;
752 }
753 }
754 return $s;
755 }
756
757 private static $GREG_DAYS = array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
758 private static $IRANIAN_DAYS = array( 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29 );
759 /**
760 * Algorithm by Roozbeh Pournader and Mohammad Toossi to convert
761 * Gregorian dates to Iranian dates. Originally written in C, it
762 * is released under the terms of GNU Lesser General Public
763 * License. Conversion to PHP was performed by Niklas Laxström.
764 *
765 * Link: http://www.farsiweb.info/jalali/jalali.c
766 */
767 private static function tsToIranian( $ts ) {
768 $gy = substr( $ts, 0, 4 ) -1600;
769 $gm = substr( $ts, 4, 2 ) -1;
770 $gd = substr( $ts, 6, 2 ) -1;
771
772 # Days passed from the beginning (including leap years)
773 $gDayNo = 365*$gy
774 + floor(($gy+3) / 4)
775 - floor(($gy+99) / 100)
776 + floor(($gy+399) / 400);
777
778
779 // Add days of the past months of this year
780 for( $i = 0; $i < $gm; $i++ ) {
781 $gDayNo += self::$GREG_DAYS[$i];
782 }
783
784 // Leap years
785 if ( $gm > 1 && (($gy%4===0 && $gy%100!==0 || ($gy%400==0)))) {
786 $gDayNo++;
787 }
788
789 // Days passed in current month
790 $gDayNo += $gd;
791
792 $jDayNo = $gDayNo - 79;
793
794 $jNp = floor($jDayNo / 12053);
795 $jDayNo %= 12053;
796
797 $jy = 979 + 33*$jNp + 4*floor($jDayNo/1461);
798 $jDayNo %= 1461;
799
800 if ( $jDayNo >= 366 ) {
801 $jy += floor(($jDayNo-1)/365);
802 $jDayNo = floor(($jDayNo-1)%365);
803 }
804
805 for ( $i = 0; $i < 11 && $jDayNo >= self::$IRANIAN_DAYS[$i]; $i++ ) {
806 $jDayNo -= self::$IRANIAN_DAYS[$i];
807 }
808
809 $jm= $i+1;
810 $jd= $jDayNo+1;
811
812 return array($jy, $jm, $jd);
813 }
814
815 /**
816 * Converting Gregorian dates to Hebrew dates.
817 *
818 * Based on a JavaScript code by Abu Mami and Yisrael Hersch
819 * (abu-mami@kaluach.net, http://www.kaluach.net), who permitted
820 * to translate the relevant functions into PHP and release them under
821 * GNU GPL.
822 */
823 private static function tsToHebrew( $ts ) {
824 # Parse date
825 $year = substr( $ts, 0, 4 );
826 $month = substr( $ts, 4, 2 );
827 $day = substr( $ts, 6, 2 );
828
829 # Month number when March = 1, February = 12
830 $month -= 2;
831 if( $month <= 0 ) {
832 # January of February
833 $month += 12;
834 $year--;
835 }
836
837 # Days since 1 March - calculating 30 days a month,
838 # and then adding the missing number of days
839 $day += intval( 7 * $month / 12 + 30 * ( $month - 1 ) );
840 # Calculate Hebrew year for days after 1 Nisan
841 $hebrewYear = $year + 3760;
842 # Passover date for this year (as days since 1 March)
843 $passover = self::passoverDate( $hebrewYear );
844 if( $day <= $passover - 15 ) {
845 # Day is before 1 Nisan (passover is 15 Nisan) - it is the previous year
846 # Next year's passover (as days since 1 March)
847 $anchor = $passover;
848 # Add days since previous year's 1 March
849 $day += 365;
850 if( ( $year % 400 == 0 ) || ( $year % 100 != 0 && $year % 4 == 0 ) ) {
851 # Leap year
852 $day++;
853 }
854 # Previous year
855 $year--;
856 $hebrewYear--;
857 # Passover date for the new year (as days since 1 March)
858 $passover = self::passoverDate( $hebrewYear );
859 } else {
860 # Next year's passover (as days since 1 March)
861 $anchor = self::passoverDate( $hebrewYear + 1 );
862 }
863
864 # Days since 1 Nisan
865 $day -= $passover - 15;
866 # Difference between this year's passover date by gregorian calendar,
867 # and the next year's one + 12 days. This should be 1 days for a regular year,
868 # but 0 for incomplete one, 2 for complete, and those + 30 days of Adar I
869 # for a leap year.
870 $anchor -= $passover - 12;
871 $nextYear = $year + 1;
872 if( ( $nextYear % 400 == 0 ) || ( $nextYear % 100 != 0 && $nextYear % 4 == 0 ) ) {
873 # Next year is a leap year - difference is growing
874 $anchor++;
875 }
876
877 # Calculate day in the month from number of days sine 1 Nisan
878 # Don't check Adar - if the day is not in adar, we will stop before;
879 # if it is in adar, we will use it to check if it is Adar I or Adar II
880 for( $month = 0; $month < 11; $month++ ) {
881 # Calculate days in this month
882 if( $month == 7 && $anchor % 30 == 2 ) {
883 # Cheshvan in a complete year (otherwise as the rule below)
884 $days = 30;
885 } else if( $month == 8 && $anchor % 30 == 0 ) {
886 # Kislev in an incomplete year (otherwise as the rule below)
887 $days = 29;
888 } else {
889 # Even months have 30 days, odd have 29
890 $days = 30 - $month % 2;
891 }
892 if( $day <= $days ) {
893 # In this month
894 break;
895 }
896 # Try in next months
897 $day -= $days;
898 }
899
900 # Now we move to a year from Tishrei
901 if( $month >= 6 ) {
902 # After Tishrei, use next year
903 $hebrewYear++;
904 }
905 # Recalculate month number so that we start from Tishrei
906 $month = ( $month + 6 ) % 12 + 1;
907
908 # Fix Adar
909 if( $month == 6 && $anchor >= 30 ) {
910 # This *is* adar, and this year is leap
911 if( $day > 30 ) {
912 # Adar II
913 $month = 14;
914 $day -= 30;
915 } else {
916 # Adar I
917 $month = 13;
918 }
919 }
920
921 return array( $hebrewYear, $month, $day );
922 }
923
924 /**
925 * Algorithm to convert Gregorian dates to Thai solar dates.
926 *
927 * Link: http://en.wikipedia.org/wiki/Thai_solar_calendar
928 *
929 * @param string $ts 14-character timestamp
930 * @return array converted year, month, day
931 */
932 private static function tsToThai( $ts ) {
933 $gy = substr( $ts, 0, 4 );
934 $gm = substr( $ts, 4, 2 );
935 $gd = substr( $ts, 6, 2 );
936
937 # Add 543 years to the Gregorian calendar
938 # Months and days are identical
939 $gy_thai = $gy + 543;
940
941 return array( $gy_thai, $gm, $gd );
942 }
943
944
945 /**
946 * Based on Carl Friedrich Gauss algorithm for finding Easter date.
947 * Used for Hebrew date.
948 */
949 private static function passoverDate( $year ) {
950 $a = intval( ( 12 * $year + 17 ) % 19 );
951 $b = intval( $year % 4 );
952 $m = 32.044093161144 + 1.5542417966212 * $a + $b / 4.0 - 0.0031777940220923 * $year;
953 if( $m < 0 ) {
954 $m--;
955 }
956 $Mar = intval( $m );
957 if( $m < 0 ) {
958 $m++;
959 }
960 $m -= $Mar;
961
962 $c = intval( ( $Mar + 3 * $year + 5 * $b + 5 ) % 7);
963 if( $c == 0 && $a > 11 && $m >= 0.89772376543210 ) {
964 $Mar++;
965 } else if( $c == 1 && $a > 6 && $m >= 0.63287037037037 ) {
966 $Mar += 2;
967 } else if( $c == 2 || $c == 4 || $c == 6 ) {
968 $Mar++;
969 }
970
971 $Mar += intval( ( $year - 3760 ) / 100 ) - intval( ( $year - 3760 ) / 400 ) - 2;
972 return $Mar;
973 }
974
975 /**
976 * Roman number formatting up to 3000
977 */
978 static function romanNumeral( $num ) {
979 static $table = array(
980 array( '', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X' ),
981 array( '', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC', 'C' ),
982 array( '', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM', 'M' ),
983 array( '', 'M', 'MM', 'MMM' )
984 );
985
986 $num = intval( $num );
987 if ( $num > 3000 || $num <= 0 ) {
988 return $num;
989 }
990
991 $s = '';
992 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
993 if ( $num >= $pow10 ) {
994 $s .= $table[$i][floor($num / $pow10)];
995 }
996 $num = $num % $pow10;
997 }
998 return $s;
999 }
1000
1001 /**
1002 * Hebrew Gematria number formatting up to 9999
1003 */
1004 static function hebrewNumeral( $num ) {
1005 static $table = array(
1006 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' ),
1007 array( '', 'י', 'כ', 'ל', 'מ', 'נ', 'ס', 'ע', 'פ', 'צ', 'ק' ),
1008 array( '', 'ק', 'ר', 'ש', 'ת', 'תק', 'תר', 'תש', 'תת', 'תתק', 'תתר' ),
1009 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' )
1010 );
1011
1012 $num = intval( $num );
1013 if ( $num > 9999 || $num <= 0 ) {
1014 return $num;
1015 }
1016
1017 $s = '';
1018 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1019 if ( $num >= $pow10 ) {
1020 if ( $num == 15 || $num == 16 ) {
1021 $s .= $table[0][9] . $table[0][$num - 9];
1022 $num = 0;
1023 } else {
1024 $s .= $table[$i][intval( ( $num / $pow10 ) )];
1025 if( $pow10 == 1000 ) {
1026 $s .= "'";
1027 }
1028 }
1029 }
1030 $num = $num % $pow10;
1031 }
1032 if( strlen( $s ) == 2 ) {
1033 $str = $s . "'";
1034 } else {
1035 $str = substr( $s, 0, strlen( $s ) - 2 ) . '"';
1036 $str .= substr( $s, strlen( $s ) - 2, 2 );
1037 }
1038 $start = substr( $str, 0, strlen( $str ) - 2 );
1039 $end = substr( $str, strlen( $str ) - 2 );
1040 switch( $end ) {
1041 case 'כ':
1042 $str = $start . 'ך';
1043 break;
1044 case 'מ':
1045 $str = $start . 'ם';
1046 break;
1047 case 'נ':
1048 $str = $start . 'ן';
1049 break;
1050 case 'פ':
1051 $str = $start . 'ף';
1052 break;
1053 case 'צ':
1054 $str = $start . 'ץ';
1055 break;
1056 }
1057 return $str;
1058 }
1059
1060 /**
1061 * This is meant to be used by time(), date(), and timeanddate() to get
1062 * the date preference they're supposed to use, it should be used in
1063 * all children.
1064 *
1065 *<code>
1066 * function timeanddate([...], $format = true) {
1067 * $datePreference = $this->dateFormat($format);
1068 * [...]
1069 * }
1070 *</code>
1071 *
1072 * @param mixed $usePrefs: if true, the user's preference is used
1073 * if false, the site/language default is used
1074 * if int/string, assumed to be a format.
1075 * @return string
1076 */
1077 function dateFormat( $usePrefs = true ) {
1078 global $wgUser;
1079
1080 if( is_bool( $usePrefs ) ) {
1081 if( $usePrefs ) {
1082 $datePreference = $wgUser->getDatePreference();
1083 } else {
1084 $options = User::getDefaultOptions();
1085 $datePreference = (string)$options['date'];
1086 }
1087 } else {
1088 $datePreference = (string)$usePrefs;
1089 }
1090
1091 // return int
1092 if( $datePreference == '' ) {
1093 return 'default';
1094 }
1095
1096 return $datePreference;
1097 }
1098
1099 /**
1100 * @public
1101 * @param mixed $ts the time format which needs to be turned into a
1102 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1103 * @param bool $adj whether to adjust the time output according to the
1104 * user configured offset ($timecorrection)
1105 * @param mixed $format true to use user's date format preference
1106 * @param string $timecorrection the time offset as returned by
1107 * validateTimeZone() in Special:Preferences
1108 * @return string
1109 */
1110 function date( $ts, $adj = false, $format = true, $timecorrection = false ) {
1111 $this->load();
1112 if ( $adj ) {
1113 $ts = $this->userAdjust( $ts, $timecorrection );
1114 }
1115
1116 $pref = $this->dateFormat( $format );
1117 if( $pref == 'default' || !isset( $this->dateFormats["$pref date"] ) ) {
1118 $pref = $this->defaultDateFormat;
1119 }
1120 return $this->sprintfDate( $this->dateFormats["$pref date"], $ts );
1121 }
1122
1123 /**
1124 * @public
1125 * @param mixed $ts the time format which needs to be turned into a
1126 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1127 * @param bool $adj whether to adjust the time output according to the
1128 * user configured offset ($timecorrection)
1129 * @param mixed $format true to use user's date format preference
1130 * @param string $timecorrection the time offset as returned by
1131 * validateTimeZone() in Special:Preferences
1132 * @return string
1133 */
1134 function time( $ts, $adj = false, $format = true, $timecorrection = false ) {
1135 $this->load();
1136 if ( $adj ) {
1137 $ts = $this->userAdjust( $ts, $timecorrection );
1138 }
1139
1140 $pref = $this->dateFormat( $format );
1141 if( $pref == 'default' || !isset( $this->dateFormats["$pref time"] ) ) {
1142 $pref = $this->defaultDateFormat;
1143 }
1144 return $this->sprintfDate( $this->dateFormats["$pref time"], $ts );
1145 }
1146
1147 /**
1148 * @public
1149 * @param mixed $ts the time format which needs to be turned into a
1150 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1151 * @param bool $adj whether to adjust the time output according to the
1152 * user configured offset ($timecorrection)
1153
1154 * @param mixed $format what format to return, if it's false output the
1155 * default one (default true)
1156 * @param string $timecorrection the time offset as returned by
1157 * validateTimeZone() in Special:Preferences
1158 * @return string
1159 */
1160 function timeanddate( $ts, $adj = false, $format = true, $timecorrection = false) {
1161 $this->load();
1162
1163 $ts = wfTimestamp( TS_MW, $ts );
1164
1165 if ( $adj ) {
1166 $ts = $this->userAdjust( $ts, $timecorrection );
1167 }
1168
1169 $pref = $this->dateFormat( $format );
1170 if( $pref == 'default' || !isset( $this->dateFormats["$pref both"] ) ) {
1171 $pref = $this->defaultDateFormat;
1172 }
1173
1174 return $this->sprintfDate( $this->dateFormats["$pref both"], $ts );
1175 }
1176
1177 function getMessage( $key ) {
1178 $this->load();
1179 return isset( $this->messages[$key] ) ? $this->messages[$key] : null;
1180 }
1181
1182 function getAllMessages() {
1183 $this->load();
1184 return $this->messages;
1185 }
1186
1187 function iconv( $in, $out, $string ) {
1188 # For most languages, this is a wrapper for iconv
1189 return iconv( $in, $out . '//IGNORE', $string );
1190 }
1191
1192 // callback functions for uc(), lc(), ucwords(), ucwordbreaks()
1193 function ucwordbreaksCallbackAscii($matches){
1194 return $this->ucfirst($matches[1]);
1195 }
1196
1197 function ucwordbreaksCallbackMB($matches){
1198 return mb_strtoupper($matches[0]);
1199 }
1200
1201 function ucCallback($matches){
1202 list( $wikiUpperChars ) = self::getCaseMaps();
1203 return strtr( $matches[1], $wikiUpperChars );
1204 }
1205
1206 function lcCallback($matches){
1207 list( , $wikiLowerChars ) = self::getCaseMaps();
1208 return strtr( $matches[1], $wikiLowerChars );
1209 }
1210
1211 function ucwordsCallbackMB($matches){
1212 return mb_strtoupper($matches[0]);
1213 }
1214
1215 function ucwordsCallbackWiki($matches){
1216 list( $wikiUpperChars ) = self::getCaseMaps();
1217 return strtr( $matches[0], $wikiUpperChars );
1218 }
1219
1220 function ucfirst( $str ) {
1221 if ( empty($str) ) return $str;
1222 if ( ord($str[0]) < 128 ) return ucfirst($str);
1223 else return self::uc($str,true); // fall back to more complex logic in case of multibyte strings
1224 }
1225
1226 function uc( $str, $first = false ) {
1227 if ( function_exists( 'mb_strtoupper' ) ) {
1228 if ( $first ) {
1229 if ( self::isMultibyte( $str ) ) {
1230 return mb_strtoupper( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
1231 } else {
1232 return ucfirst( $str );
1233 }
1234 } else {
1235 return self::isMultibyte( $str ) ? mb_strtoupper( $str ) : strtoupper( $str );
1236 }
1237 } else {
1238 if ( self::isMultibyte( $str ) ) {
1239 list( $wikiUpperChars ) = $this->getCaseMaps();
1240 $x = $first ? '^' : '';
1241 return preg_replace_callback(
1242 "/$x([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
1243 array($this,"ucCallback"),
1244 $str
1245 );
1246 } else {
1247 return $first ? ucfirst( $str ) : strtoupper( $str );
1248 }
1249 }
1250 }
1251
1252 function lcfirst( $str ) {
1253 if ( empty($str) ) return $str;
1254 if ( is_string( $str ) && ord($str[0]) < 128 ) {
1255 // editing string in place = cool
1256 $str[0]=strtolower($str[0]);
1257 return $str;
1258 }
1259 else return self::lc( $str, true );
1260 }
1261
1262 function lc( $str, $first = false ) {
1263 if ( function_exists( 'mb_strtolower' ) )
1264 if ( $first )
1265 if ( self::isMultibyte( $str ) )
1266 return mb_strtolower( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
1267 else
1268 return strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 );
1269 else
1270 return self::isMultibyte( $str ) ? mb_strtolower( $str ) : strtolower( $str );
1271 else
1272 if ( self::isMultibyte( $str ) ) {
1273 list( , $wikiLowerChars ) = self::getCaseMaps();
1274 $x = $first ? '^' : '';
1275 return preg_replace_callback(
1276 "/$x([A-Z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
1277 array($this,"lcCallback"),
1278 $str
1279 );
1280 } else
1281 return $first ? strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 ) : strtolower( $str );
1282 }
1283
1284 function isMultibyte( $str ) {
1285 return (bool)preg_match( '/[\x80-\xff]/', $str );
1286 }
1287
1288 function ucwords($str) {
1289 if ( self::isMultibyte( $str ) ) {
1290 $str = self::lc($str);
1291
1292 // regexp to find first letter in each word (i.e. after each space)
1293 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)| ([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
1294
1295 // function to use to capitalize a single char
1296 if ( function_exists( 'mb_strtoupper' ) )
1297 return preg_replace_callback(
1298 $replaceRegexp,
1299 array($this,"ucwordsCallbackMB"),
1300 $str
1301 );
1302 else
1303 return preg_replace_callback(
1304 $replaceRegexp,
1305 array($this,"ucwordsCallbackWiki"),
1306 $str
1307 );
1308 }
1309 else
1310 return ucwords( strtolower( $str ) );
1311 }
1312
1313 # capitalize words at word breaks
1314 function ucwordbreaks($str){
1315 if (self::isMultibyte( $str ) ) {
1316 $str = self::lc($str);
1317
1318 // since \b doesn't work for UTF-8, we explicitely define word break chars
1319 $breaks= "[ \-\(\)\}\{\.,\?!]";
1320
1321 // find first letter after word break
1322 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)|$breaks([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
1323
1324 if ( function_exists( 'mb_strtoupper' ) )
1325 return preg_replace_callback(
1326 $replaceRegexp,
1327 array($this,"ucwordbreaksCallbackMB"),
1328 $str
1329 );
1330 else
1331 return preg_replace_callback(
1332 $replaceRegexp,
1333 array($this,"ucwordsCallbackWiki"),
1334 $str
1335 );
1336 }
1337 else
1338 return preg_replace_callback(
1339 '/\b([\w\x80-\xff]+)\b/',
1340 array($this,"ucwordbreaksCallbackAscii"),
1341 $str );
1342 }
1343
1344 /**
1345 * Return a case-folded representation of $s
1346 *
1347 * This is a representation such that caseFold($s1)==caseFold($s2) if $s1
1348 * and $s2 are the same except for the case of their characters. It is not
1349 * necessary for the value returned to make sense when displayed.
1350 *
1351 * Do *not* perform any other normalisation in this function. If a caller
1352 * uses this function when it should be using a more general normalisation
1353 * function, then fix the caller.
1354 */
1355 function caseFold( $s ) {
1356 return $this->uc( $s );
1357 }
1358
1359 function checkTitleEncoding( $s ) {
1360 if( is_array( $s ) ) {
1361 wfDebugDieBacktrace( 'Given array to checkTitleEncoding.' );
1362 }
1363 # Check for non-UTF-8 URLs
1364 $ishigh = preg_match( '/[\x80-\xff]/', $s);
1365 if(!$ishigh) return $s;
1366
1367 $isutf8 = preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
1368 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s );
1369 if( $isutf8 ) return $s;
1370
1371 return $this->iconv( $this->fallback8bitEncoding(), "utf-8", $s );
1372 }
1373
1374 function fallback8bitEncoding() {
1375 $this->load();
1376 return $this->fallback8bitEncoding;
1377 }
1378
1379 /**
1380 * Some languages have special punctuation to strip out
1381 * or characters which need to be converted for MySQL's
1382 * indexing to grok it correctly. Make such changes here.
1383 *
1384 * @param string $in
1385 * @return string
1386 */
1387 function stripForSearch( $string ) {
1388 global $wgDBtype;
1389 if ( $wgDBtype != 'mysql' ) {
1390 return $string;
1391 }
1392
1393 # MySQL fulltext index doesn't grok utf-8, so we
1394 # need to fold cases and convert to hex
1395
1396 wfProfileIn( __METHOD__ );
1397 if( function_exists( 'mb_strtolower' ) ) {
1398 $out = preg_replace(
1399 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
1400 "'U8' . bin2hex( \"$1\" )",
1401 mb_strtolower( $string ) );
1402 } else {
1403 list( , $wikiLowerChars ) = self::getCaseMaps();
1404 $out = preg_replace(
1405 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
1406 "'U8' . bin2hex( strtr( \"\$1\", \$wikiLowerChars ) )",
1407 $string );
1408 }
1409 wfProfileOut( __METHOD__ );
1410 return $out;
1411 }
1412
1413 function convertForSearchResult( $termsArray ) {
1414 # some languages, e.g. Chinese, need to do a conversion
1415 # in order for search results to be displayed correctly
1416 return $termsArray;
1417 }
1418
1419 /**
1420 * Get the first character of a string.
1421 *
1422 * @param string $s
1423 * @return string
1424 */
1425 function firstChar( $s ) {
1426 $matches = array();
1427 preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
1428 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})/', $s, $matches);
1429
1430 return isset( $matches[1] ) ? $matches[1] : "";
1431 }
1432
1433 function initEncoding() {
1434 # Some languages may have an alternate char encoding option
1435 # (Esperanto X-coding, Japanese furigana conversion, etc)
1436 # If this language is used as the primary content language,
1437 # an override to the defaults can be set here on startup.
1438 }
1439
1440 function recodeForEdit( $s ) {
1441 # For some languages we'll want to explicitly specify
1442 # which characters make it into the edit box raw
1443 # or are converted in some way or another.
1444 # Note that if wgOutputEncoding is different from
1445 # wgInputEncoding, this text will be further converted
1446 # to wgOutputEncoding.
1447 global $wgEditEncoding;
1448 if( $wgEditEncoding == '' or
1449 $wgEditEncoding == 'UTF-8' ) {
1450 return $s;
1451 } else {
1452 return $this->iconv( 'UTF-8', $wgEditEncoding, $s );
1453 }
1454 }
1455
1456 function recodeInput( $s ) {
1457 # Take the previous into account.
1458 global $wgEditEncoding;
1459 if($wgEditEncoding != "") {
1460 $enc = $wgEditEncoding;
1461 } else {
1462 $enc = 'UTF-8';
1463 }
1464 if( $enc == 'UTF-8' ) {
1465 return $s;
1466 } else {
1467 return $this->iconv( $enc, 'UTF-8', $s );
1468 }
1469 }
1470
1471 /**
1472 * For right-to-left language support
1473 *
1474 * @return bool
1475 */
1476 function isRTL() {
1477 $this->load();
1478 return $this->rtl;
1479 }
1480
1481 /**
1482 * A hidden direction mark (LRM or RLM), depending on the language direction
1483 *
1484 * @return string
1485 */
1486 function getDirMark() {
1487 return $this->isRTL() ? "\xE2\x80\x8F" : "\xE2\x80\x8E";
1488 }
1489
1490 /**
1491 * An arrow, depending on the language direction
1492 *
1493 * @return string
1494 */
1495 function getArrow() {
1496 return $this->isRTL() ? '←' : '→';
1497 }
1498
1499 /**
1500 * To allow "foo[[bar]]" to extend the link over the whole word "foobar"
1501 *
1502 * @return bool
1503 */
1504 function linkPrefixExtension() {
1505 $this->load();
1506 return $this->linkPrefixExtension;
1507 }
1508
1509 function &getMagicWords() {
1510 $this->load();
1511 return $this->magicWords;
1512 }
1513
1514 # Fill a MagicWord object with data from here
1515 function getMagic( &$mw ) {
1516 if ( !$this->mMagicHookDone ) {
1517 $this->mMagicHookDone = true;
1518 wfRunHooks( 'LanguageGetMagic', array( &$this->mMagicExtensions, $this->getCode() ) );
1519 }
1520 if ( isset( $this->mMagicExtensions[$mw->mId] ) ) {
1521 $rawEntry = $this->mMagicExtensions[$mw->mId];
1522 } else {
1523 $magicWords =& $this->getMagicWords();
1524 if ( isset( $magicWords[$mw->mId] ) ) {
1525 $rawEntry = $magicWords[$mw->mId];
1526 } else {
1527 # Fall back to English if local list is incomplete
1528 $magicWords =& Language::getMagicWords();
1529 $rawEntry = $magicWords[$mw->mId];
1530 }
1531 }
1532
1533 if( !is_array( $rawEntry ) ) {
1534 error_log( "\"$rawEntry\" is not a valid magic thingie for \"$mw->mId\"" );
1535 } else {
1536 $mw->mCaseSensitive = $rawEntry[0];
1537 $mw->mSynonyms = array_slice( $rawEntry, 1 );
1538 }
1539 }
1540
1541 /**
1542 * Add magic words to the extension array
1543 */
1544 function addMagicWordsByLang( $newWords ) {
1545 $code = $this->getCode();
1546 $fallbackChain = array();
1547 while ( $code && !in_array( $code, $fallbackChain ) ) {
1548 $fallbackChain[] = $code;
1549 $code = self::getFallbackFor( $code );
1550 }
1551 if ( !in_array( 'en', $fallbackChain ) ) {
1552 $fallbackChain[] = 'en';
1553 }
1554 $fallbackChain = array_reverse( $fallbackChain );
1555 foreach ( $fallbackChain as $code ) {
1556 if ( isset( $newWords[$code] ) ) {
1557 $this->mMagicExtensions = $newWords[$code] + $this->mMagicExtensions;
1558 }
1559 }
1560 }
1561
1562 /**
1563 * Get special page names, as an associative array
1564 * case folded alias => real name
1565 */
1566 function getSpecialPageAliases() {
1567 $this->load();
1568 if ( !isset( $this->mExtendedSpecialPageAliases ) ) {
1569 $this->mExtendedSpecialPageAliases = $this->specialPageAliases;
1570 wfRunHooks( 'LanguageGetSpecialPageAliases',
1571 array( &$this->mExtendedSpecialPageAliases, $this->getCode() ) );
1572 }
1573 return $this->mExtendedSpecialPageAliases;
1574 }
1575
1576 /**
1577 * Italic is unsuitable for some languages
1578 *
1579 * @public
1580 *
1581 * @param string $text The text to be emphasized.
1582 * @return string
1583 */
1584 function emphasize( $text ) {
1585 return "<em>$text</em>";
1586 }
1587
1588 /**
1589 * Normally we output all numbers in plain en_US style, that is
1590 * 293,291.235 for twohundredninetythreethousand-twohundredninetyone
1591 * point twohundredthirtyfive. However this is not sutable for all
1592 * languages, some such as Pakaran want ੨੯੩,੨੯੫.੨੩੫ and others such as
1593 * Icelandic just want to use commas instead of dots, and dots instead
1594 * of commas like "293.291,235".
1595 *
1596 * An example of this function being called:
1597 * <code>
1598 * wfMsg( 'message', $wgLang->formatNum( $num ) )
1599 * </code>
1600 *
1601 * See LanguageGu.php for the Gujarati implementation and
1602 * LanguageIs.php for the , => . and . => , implementation.
1603 *
1604 * @todo check if it's viable to use localeconv() for the decimal
1605 * seperator thing.
1606 * @public
1607 * @param mixed $number the string to be formatted, should be an integer or
1608 * a floating point number.
1609 * @param bool $nocommafy Set to true for special numbers like dates
1610 * @return string
1611 */
1612 function formatNum( $number, $nocommafy = false ) {
1613 global $wgTranslateNumerals;
1614 if (!$nocommafy) {
1615 $number = $this->commafy($number);
1616 $s = $this->separatorTransformTable();
1617 if (!is_null($s)) { $number = strtr($number, $s); }
1618 }
1619
1620 if ($wgTranslateNumerals) {
1621 $s = $this->digitTransformTable();
1622 if (!is_null($s)) { $number = strtr($number, $s); }
1623 }
1624
1625 return $number;
1626 }
1627
1628 function parseFormattedNumber( $number ) {
1629 $s = $this->digitTransformTable();
1630 if (!is_null($s)) { $number = strtr($number, array_flip($s)); }
1631
1632 $s = $this->separatorTransformTable();
1633 if (!is_null($s)) { $number = strtr($number, array_flip($s)); }
1634
1635 $number = strtr( $number, array (',' => '') );
1636 return $number;
1637 }
1638
1639 /**
1640 * Adds commas to a given number
1641 *
1642 * @param mixed $_
1643 * @return string
1644 */
1645 function commafy($_) {
1646 return strrev((string)preg_replace('/(\d{3})(?=\d)(?!\d*\.)/','$1,',strrev($_)));
1647 }
1648
1649 function digitTransformTable() {
1650 $this->load();
1651 return $this->digitTransformTable;
1652 }
1653
1654 function separatorTransformTable() {
1655 $this->load();
1656 return $this->separatorTransformTable;
1657 }
1658
1659
1660 /**
1661 * For the credit list in includes/Credits.php (action=credits)
1662 *
1663 * @param array $l
1664 * @return string
1665 */
1666 function listToText( $l ) {
1667 $s = '';
1668 $m = count($l) - 1;
1669 for ($i = $m; $i >= 0; $i--) {
1670 if ($i == $m) {
1671 $s = $l[$i];
1672 } else if ($i == $m - 1) {
1673 $s = $l[$i] . ' ' . $this->getMessageFromDB( 'and' ) . ' ' . $s;
1674 } else {
1675 $s = $l[$i] . ', ' . $s;
1676 }
1677 }
1678 return $s;
1679 }
1680
1681 /**
1682 * Truncate a string to a specified length in bytes, appending an optional
1683 * string (e.g. for ellipses)
1684 *
1685 * The database offers limited byte lengths for some columns in the database;
1686 * multi-byte character sets mean we need to ensure that only whole characters
1687 * are included, otherwise broken characters can be passed to the user
1688 *
1689 * If $length is negative, the string will be truncated from the beginning
1690 *
1691 * @param string $string String to truncate
1692 * @param int $length Maximum length (excluding ellipses)
1693 * @param string $ellipses String to append to the truncated text
1694 * @return string
1695 */
1696 function truncate( $string, $length, $ellipsis = "" ) {
1697 if( $length == 0 ) {
1698 return $ellipsis;
1699 }
1700 if ( strlen( $string ) <= abs( $length ) ) {
1701 return $string;
1702 }
1703 if( $length > 0 ) {
1704 $string = substr( $string, 0, $length );
1705 $char = ord( $string[strlen( $string ) - 1] );
1706 $m = array();
1707 if ($char >= 0xc0) {
1708 # We got the first byte only of a multibyte char; remove it.
1709 $string = substr( $string, 0, -1 );
1710 } elseif( $char >= 0x80 &&
1711 preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
1712 '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m ) ) {
1713 # We chopped in the middle of a character; remove it
1714 $string = $m[1];
1715 }
1716 return $string . $ellipsis;
1717 } else {
1718 $string = substr( $string, $length );
1719 $char = ord( $string[0] );
1720 if( $char >= 0x80 && $char < 0xc0 ) {
1721 # We chopped in the middle of a character; remove the whole thing
1722 $string = preg_replace( '/^[\x80-\xbf]+/', '', $string );
1723 }
1724 return $ellipsis . $string;
1725 }
1726 }
1727
1728 /**
1729 * Grammatical transformations, needed for inflected languages
1730 * Invoked by putting {{grammar:case|word}} in a message
1731 *
1732 * @param string $word
1733 * @param string $case
1734 * @return string
1735 */
1736 function convertGrammar( $word, $case ) {
1737 global $wgGrammarForms;
1738 if ( isset($wgGrammarForms['en'][$case][$word]) ) {
1739 return $wgGrammarForms['en'][$case][$word];
1740 }
1741 return $word;
1742 }
1743
1744 /**
1745 * Plural form transformations, needed for some languages.
1746 * For example, there are 3 form of plural in Russian and Polish,
1747 * depending on "count mod 10". See [[w:Plural]]
1748 * For English it is pretty simple.
1749 *
1750 * Invoked by putting {{plural:count|wordform1|wordform2}}
1751 * or {{plural:count|wordform1|wordform2|wordform3}}
1752 *
1753 * Example: {{plural:{{NUMBEROFARTICLES}}|article|articles}}
1754 *
1755 * @param integer $count Non-localized number
1756 * @param array $forms Different plural forms
1757 * @return string Correct form of plural for $count in this language
1758 */
1759 function convertPlural( $count, $forms ) {
1760 if ( !count($forms) ) { return ''; }
1761 $forms = $this->preConvertPlural( $forms, 2 );
1762
1763 return ( abs($count) == 1 ) ? $forms[0] : $forms[1];
1764 }
1765
1766 /**
1767 * Checks that convertPlural was given an array and pads it to requested
1768 * amound of forms by copying the last one.
1769 *
1770 * @param integer $count How many forms should there be at least
1771 * @param array $forms Array of forms given to convertPlural
1772 * @return array Padded array of forms or an exception if not an array
1773 */
1774 protected function preConvertPlural( Array $forms, $count ) {
1775 while ( count($forms) < $count ) {
1776 $forms[] = $forms[count($forms)-1];
1777 }
1778 return $forms;
1779 }
1780
1781 /**
1782 * For translaing of expiry times
1783 * @param string The validated block time in English
1784 * @return Somehow translated block time
1785 * @see LanguageFi.php for example implementation
1786 */
1787 function translateBlockExpiry( $str ) {
1788
1789 $scBlockExpiryOptions = $this->getMessageFromDB( 'ipboptions' );
1790
1791 if ( $scBlockExpiryOptions == '-') {
1792 return $str;
1793 }
1794
1795 foreach (explode(',', $scBlockExpiryOptions) as $option) {
1796 if ( strpos($option, ":") === false )
1797 continue;
1798 list($show, $value) = explode(":", $option);
1799 if ( strcmp ( $str, $value) == 0 ) {
1800 return htmlspecialchars( trim( $show ) );
1801 }
1802 }
1803
1804 return $str;
1805 }
1806
1807 /**
1808 * languages like Chinese need to be segmented in order for the diff
1809 * to be of any use
1810 *
1811 * @param string $text
1812 * @return string
1813 */
1814 function segmentForDiff( $text ) {
1815 return $text;
1816 }
1817
1818 /**
1819 * and unsegment to show the result
1820 *
1821 * @param string $text
1822 * @return string
1823 */
1824 function unsegmentForDiff( $text ) {
1825 return $text;
1826 }
1827
1828 # convert text to different variants of a language.
1829 function convert( $text, $isTitle = false) {
1830 return $this->mConverter->convert($text, $isTitle);
1831 }
1832
1833 # Convert text from within Parser
1834 function parserConvert( $text, &$parser ) {
1835 return $this->mConverter->parserConvert( $text, $parser );
1836 }
1837
1838 # Check if this is a language with variants
1839 function hasVariants(){
1840 return sizeof($this->getVariants())>1;
1841 }
1842
1843 # Put custom tags (e.g. -{ }-) around math to prevent conversion
1844 function armourMath($text){
1845 return $this->mConverter->armourMath($text);
1846 }
1847
1848
1849 /**
1850 * Perform output conversion on a string, and encode for safe HTML output.
1851 * @param string $text
1852 * @param bool $isTitle -- wtf?
1853 * @return string
1854 * @todo this should get integrated somewhere sane
1855 */
1856 function convertHtml( $text, $isTitle = false ) {
1857 return htmlspecialchars( $this->convert( $text, $isTitle ) );
1858 }
1859
1860 function convertCategoryKey( $key ) {
1861 return $this->mConverter->convertCategoryKey( $key );
1862 }
1863
1864 /**
1865 * get the list of variants supported by this langauge
1866 * see sample implementation in LanguageZh.php
1867 *
1868 * @return array an array of language codes
1869 */
1870 function getVariants() {
1871 return $this->mConverter->getVariants();
1872 }
1873
1874
1875 function getPreferredVariant( $fromUser = true ) {
1876 return $this->mConverter->getPreferredVariant( $fromUser );
1877 }
1878
1879 /**
1880 * if a language supports multiple variants, it is
1881 * possible that non-existing link in one variant
1882 * actually exists in another variant. this function
1883 * tries to find it. See e.g. LanguageZh.php
1884 *
1885 * @param string $link the name of the link
1886 * @param mixed $nt the title object of the link
1887 * @return null the input parameters may be modified upon return
1888 */
1889 function findVariantLink( &$link, &$nt ) {
1890 $this->mConverter->findVariantLink($link, $nt);
1891 }
1892
1893 /**
1894 * If a language supports multiple variants, converts text
1895 * into an array of all possible variants of the text:
1896 * 'variant' => text in that variant
1897 */
1898
1899 function convertLinkToAllVariants($text){
1900 return $this->mConverter->convertLinkToAllVariants($text);
1901 }
1902
1903
1904 /**
1905 * returns language specific options used by User::getPageRenderHash()
1906 * for example, the preferred language variant
1907 *
1908 * @return string
1909 * @public
1910 */
1911 function getExtraHashOptions() {
1912 return $this->mConverter->getExtraHashOptions();
1913 }
1914
1915 /**
1916 * for languages that support multiple variants, the title of an
1917 * article may be displayed differently in different variants. this
1918 * function returns the apporiate title defined in the body of the article.
1919 *
1920 * @return string
1921 */
1922 function getParsedTitle() {
1923 return $this->mConverter->getParsedTitle();
1924 }
1925
1926 /**
1927 * Enclose a string with the "no conversion" tag. This is used by
1928 * various functions in the Parser
1929 *
1930 * @param string $text text to be tagged for no conversion
1931 * @return string the tagged text
1932 */
1933 function markNoConversion( $text, $noParse=false ) {
1934 return $this->mConverter->markNoConversion( $text, $noParse );
1935 }
1936
1937 /**
1938 * A regular expression to match legal word-trailing characters
1939 * which should be merged onto a link of the form [[foo]]bar.
1940 *
1941 * @return string
1942 * @public
1943 */
1944 function linkTrail() {
1945 $this->load();
1946 return $this->linkTrail;
1947 }
1948
1949 function getLangObj() {
1950 return $this;
1951 }
1952
1953 /**
1954 * Get the RFC 3066 code for this language object
1955 */
1956 function getCode() {
1957 return $this->mCode;
1958 }
1959
1960 function setCode( $code ) {
1961 $this->mCode = $code;
1962 }
1963
1964 static function getFileName( $prefix = 'Language', $code, $suffix = '.php' ) {
1965 return $prefix . str_replace( '-', '_', ucfirst( $code ) ) . $suffix;
1966 }
1967
1968 static function getMessagesFileName( $code ) {
1969 global $IP;
1970 return self::getFileName( "$IP/languages/messages/Messages", $code, '.php' );
1971 }
1972
1973 static function getClassFileName( $code ) {
1974 global $IP;
1975 return self::getFileName( "$IP/languages/classes/Language", $code, '.php' );
1976 }
1977
1978 static function getLocalisationArray( $code, $disableCache = false ) {
1979 self::loadLocalisation( $code, $disableCache );
1980 return self::$mLocalisationCache[$code];
1981 }
1982
1983 /**
1984 * Load localisation data for a given code into the static cache
1985 *
1986 * @return array Dependencies, map of filenames to mtimes
1987 */
1988 static function loadLocalisation( $code, $disableCache = false ) {
1989 static $recursionGuard = array();
1990 global $wgMemc, $wgCheckSerialized;
1991
1992 if ( !$code ) {
1993 throw new MWException( "Invalid language code requested" );
1994 }
1995
1996 if ( !$disableCache ) {
1997 # Try the per-process cache
1998 if ( isset( self::$mLocalisationCache[$code] ) ) {
1999 return self::$mLocalisationCache[$code]['deps'];
2000 }
2001
2002 wfProfileIn( __METHOD__ );
2003
2004 # Try the serialized directory
2005 $cache = wfGetPrecompiledData( self::getFileName( "Messages", $code, '.ser' ) );
2006 if ( $cache ) {
2007 if ( $wgCheckSerialized && self::isLocalisationOutOfDate( $cache ) ) {
2008 $cache = false;
2009 wfDebug( "Language::loadLocalisation(): precompiled data file for $code is out of date\n" );
2010 } else {
2011 self::$mLocalisationCache[$code] = $cache;
2012 wfDebug( "Language::loadLocalisation(): got localisation for $code from precompiled data file\n" );
2013 wfProfileOut( __METHOD__ );
2014 return self::$mLocalisationCache[$code]['deps'];
2015 }
2016 }
2017
2018 # Try the global cache
2019 $memcKey = wfMemcKey('localisation', $code );
2020 $cache = $wgMemc->get( $memcKey );
2021 if ( $cache ) {
2022 if ( self::isLocalisationOutOfDate( $cache ) ) {
2023 $wgMemc->delete( $memcKey );
2024 $cache = false;
2025 wfDebug( "Language::loadLocalisation(): localisation cache for $code had expired\n" );
2026 } else {
2027 self::$mLocalisationCache[$code] = $cache;
2028 wfDebug( "Language::loadLocalisation(): got localisation for $code from cache\n" );
2029 wfProfileOut( __METHOD__ );
2030 return $cache['deps'];
2031 }
2032 }
2033 } else {
2034 wfProfileIn( __METHOD__ );
2035 }
2036
2037 # Default fallback, may be overridden when the messages file is included
2038 if ( $code != 'en' ) {
2039 $fallback = 'en';
2040 } else {
2041 $fallback = false;
2042 }
2043
2044 # Load the primary localisation from the source file
2045 $filename = self::getMessagesFileName( $code );
2046 if ( !file_exists( $filename ) ) {
2047 wfDebug( "Language::loadLocalisation(): no localisation file for $code, using implicit fallback to en\n" );
2048 $cache = array();
2049 $deps = array();
2050 } else {
2051 $deps = array( $filename => filemtime( $filename ) );
2052 require( $filename );
2053 $cache = compact( self::$mLocalisationKeys );
2054 wfDebug( "Language::loadLocalisation(): got localisation for $code from source\n" );
2055 }
2056
2057 if ( !empty( $fallback ) ) {
2058 # Load the fallback localisation, with a circular reference guard
2059 if ( isset( $recursionGuard[$code] ) ) {
2060 throw new MWException( "Error: Circular fallback reference in language code $code" );
2061 }
2062 $recursionGuard[$code] = true;
2063 $newDeps = self::loadLocalisation( $fallback, $disableCache );
2064 unset( $recursionGuard[$code] );
2065
2066 $secondary = self::$mLocalisationCache[$fallback];
2067 $deps = array_merge( $deps, $newDeps );
2068
2069 # Merge the fallback localisation with the current localisation
2070 foreach ( self::$mLocalisationKeys as $key ) {
2071 if ( isset( $cache[$key] ) ) {
2072 if ( isset( $secondary[$key] ) ) {
2073 if ( in_array( $key, self::$mMergeableMapKeys ) ) {
2074 $cache[$key] = $cache[$key] + $secondary[$key];
2075 } elseif ( in_array( $key, self::$mMergeableListKeys ) ) {
2076 $cache[$key] = array_merge( $secondary[$key], $cache[$key] );
2077 } elseif ( in_array( $key, self::$mMergeableAliasListKeys ) ) {
2078 $cache[$key] = array_merge_recursive( $cache[$key], $secondary[$key] );
2079 }
2080 }
2081 } else {
2082 $cache[$key] = $secondary[$key];
2083 }
2084 }
2085
2086 # Merge bookstore lists if requested
2087 if ( !empty( $cache['bookstoreList']['inherit'] ) ) {
2088 $cache['bookstoreList'] = array_merge( $cache['bookstoreList'], $secondary['bookstoreList'] );
2089 }
2090 if ( isset( $cache['bookstoreList']['inherit'] ) ) {
2091 unset( $cache['bookstoreList']['inherit'] );
2092 }
2093 }
2094
2095 # Add dependencies to the cache entry
2096 $cache['deps'] = $deps;
2097
2098 # Replace spaces with underscores in namespace names
2099 $cache['namespaceNames'] = str_replace( ' ', '_', $cache['namespaceNames'] );
2100
2101 # Save to both caches
2102 self::$mLocalisationCache[$code] = $cache;
2103 if ( !$disableCache ) {
2104 $wgMemc->set( $memcKey, $cache );
2105 }
2106
2107 wfProfileOut( __METHOD__ );
2108 return $deps;
2109 }
2110
2111 /**
2112 * Test if a given localisation cache is out of date with respect to the
2113 * source Messages files. This is done automatically for the global cache
2114 * in $wgMemc, but is only done on certain occasions for the serialized
2115 * data file.
2116 *
2117 * @param $cache mixed Either a language code or a cache array
2118 */
2119 static function isLocalisationOutOfDate( $cache ) {
2120 if ( !is_array( $cache ) ) {
2121 self::loadLocalisation( $cache );
2122 $cache = self::$mLocalisationCache[$cache];
2123 }
2124 $expired = false;
2125 foreach ( $cache['deps'] as $file => $mtime ) {
2126 if ( !file_exists( $file ) || filemtime( $file ) > $mtime ) {
2127 $expired = true;
2128 break;
2129 }
2130 }
2131 return $expired;
2132 }
2133
2134 /**
2135 * Get the fallback for a given language
2136 */
2137 static function getFallbackFor( $code ) {
2138 self::loadLocalisation( $code );
2139 return self::$mLocalisationCache[$code]['fallback'];
2140 }
2141
2142 /**
2143 * Get all messages for a given language
2144 */
2145 static function getMessagesFor( $code ) {
2146 self::loadLocalisation( $code );
2147 return self::$mLocalisationCache[$code]['messages'];
2148 }
2149
2150 /**
2151 * Get a message for a given language
2152 */
2153 static function getMessageFor( $key, $code ) {
2154 self::loadLocalisation( $code );
2155 return isset( self::$mLocalisationCache[$code]['messages'][$key] ) ? self::$mLocalisationCache[$code]['messages'][$key] : null;
2156 }
2157
2158 /**
2159 * Load localisation data for this object
2160 */
2161 function load() {
2162 if ( !$this->mLoaded ) {
2163 self::loadLocalisation( $this->getCode() );
2164 $cache =& self::$mLocalisationCache[$this->getCode()];
2165 foreach ( self::$mLocalisationKeys as $key ) {
2166 $this->$key = $cache[$key];
2167 }
2168 $this->mLoaded = true;
2169
2170 $this->fixUpSettings();
2171 }
2172 }
2173
2174 /**
2175 * Do any necessary post-cache-load settings adjustment
2176 */
2177 function fixUpSettings() {
2178 global $wgExtraNamespaces, $wgMetaNamespace, $wgMetaNamespaceTalk,
2179 $wgNamespaceAliases, $wgAmericanDates;
2180 wfProfileIn( __METHOD__ );
2181 if ( $wgExtraNamespaces ) {
2182 $this->namespaceNames = $wgExtraNamespaces + $this->namespaceNames;
2183 }
2184
2185 $this->namespaceNames[NS_PROJECT] = $wgMetaNamespace;
2186 if ( $wgMetaNamespaceTalk ) {
2187 $this->namespaceNames[NS_PROJECT_TALK] = $wgMetaNamespaceTalk;
2188 } else {
2189 $talk = $this->namespaceNames[NS_PROJECT_TALK];
2190 $talk = str_replace( '$1', $wgMetaNamespace, $talk );
2191
2192 # Allow grammar transformations
2193 # Allowing full message-style parsing would make simple requests
2194 # such as action=raw much more expensive than they need to be.
2195 # This will hopefully cover most cases.
2196 $talk = preg_replace_callback( '/{{grammar:(.*?)\|(.*?)}}/i',
2197 array( &$this, 'replaceGrammarInNamespace' ), $talk );
2198 $talk = str_replace( ' ', '_', $talk );
2199 $this->namespaceNames[NS_PROJECT_TALK] = $talk;
2200 }
2201
2202 # The above mixing may leave namespaces out of canonical order.
2203 # Re-order by namespace ID number...
2204 ksort( $this->namespaceNames );
2205
2206 # Put namespace names and aliases into a hashtable.
2207 # If this is too slow, then we should arrange it so that it is done
2208 # before caching. The catch is that at pre-cache time, the above
2209 # class-specific fixup hasn't been done.
2210 $this->mNamespaceIds = array();
2211 foreach ( $this->namespaceNames as $index => $name ) {
2212 $this->mNamespaceIds[$this->lc($name)] = $index;
2213 }
2214 if ( $this->namespaceAliases ) {
2215 foreach ( $this->namespaceAliases as $name => $index ) {
2216 $this->mNamespaceIds[$this->lc($name)] = $index;
2217 }
2218 }
2219 if ( $wgNamespaceAliases ) {
2220 foreach ( $wgNamespaceAliases as $name => $index ) {
2221 $this->mNamespaceIds[$this->lc($name)] = $index;
2222 }
2223 }
2224
2225 if ( $this->defaultDateFormat == 'dmy or mdy' ) {
2226 $this->defaultDateFormat = $wgAmericanDates ? 'mdy' : 'dmy';
2227 }
2228 wfProfileOut( __METHOD__ );
2229 }
2230
2231 function replaceGrammarInNamespace( $m ) {
2232 return $this->convertGrammar( trim( $m[2] ), trim( $m[1] ) );
2233 }
2234
2235 static function getCaseMaps() {
2236 static $wikiUpperChars, $wikiLowerChars;
2237 if ( isset( $wikiUpperChars ) ) {
2238 return array( $wikiUpperChars, $wikiLowerChars );
2239 }
2240
2241 wfProfileIn( __METHOD__ );
2242 $arr = wfGetPrecompiledData( 'Utf8Case.ser' );
2243 if ( $arr === false ) {
2244 throw new MWException(
2245 "Utf8Case.ser is missing, please run \"make\" in the serialized directory\n" );
2246 }
2247 extract( $arr );
2248 wfProfileOut( __METHOD__ );
2249 return array( $wikiUpperChars, $wikiLowerChars );
2250 }
2251
2252 function formatTimePeriod( $seconds ) {
2253 if ( $seconds < 10 ) {
2254 return $this->formatNum( sprintf( "%.1f", $seconds ) ) . wfMsg( 'seconds-abbrev' );
2255 } elseif ( $seconds < 60 ) {
2256 return $this->formatNum( round( $seconds ) ) . wfMsg( 'seconds-abbrev' );
2257 } elseif ( $seconds < 3600 ) {
2258 return $this->formatNum( floor( $seconds / 60 ) ) . wfMsg( 'minutes-abbrev' ) .
2259 $this->formatNum( round( fmod( $seconds, 60 ) ) ) . wfMsg( 'seconds-abbrev' );
2260 } else {
2261 $hours = floor( $seconds / 3600 );
2262 $minutes = floor( ( $seconds - $hours * 3600 ) / 60 );
2263 $secondsPart = round( $seconds - $hours * 3600 - $minutes * 60 );
2264 return $this->formatNum( $hours ) . wfMsg( 'hours-abbrev' ) .
2265 $this->formatNum( $minutes ) . wfMsg( 'minutes-abbrev' ) .
2266 $this->formatNum( $secondsPart ) . wfMsg( 'seconds-abbrev' );
2267 }
2268 }
2269
2270 function formatBitrate( $bps ) {
2271 $units = array( 'bps', 'kbps', 'Mbps', 'Gbps' );
2272 if ( $bps <= 0 ) {
2273 return $this->formatNum( $bps ) . $units[0];
2274 }
2275 $unitIndex = floor( log10( $bps ) / 3 );
2276 $mantissa = $bps / pow( 1000, $unitIndex );
2277 if ( $mantissa < 10 ) {
2278 $mantissa = round( $mantissa, 1 );
2279 } else {
2280 $mantissa = round( $mantissa );
2281 }
2282 return $this->formatNum( $mantissa ) . $units[$unitIndex];
2283 }
2284
2285 /**
2286 * Format a size in bytes for output, using an appropriate
2287 * unit (B, KB, MB or GB) according to the magnitude in question
2288 *
2289 * @param $size Size to format
2290 * @return string Plain text (not HTML)
2291 */
2292 function formatSize( $size ) {
2293 // For small sizes no decimal places necessary
2294 $round = 0;
2295 if( $size > 1024 ) {
2296 $size = $size / 1024;
2297 if( $size > 1024 ) {
2298 $size = $size / 1024;
2299 // For MB and bigger two decimal places are smarter
2300 $round = 2;
2301 if( $size > 1024 ) {
2302 $size = $size / 1024;
2303 $msg = 'size-gigabytes';
2304 } else {
2305 $msg = 'size-megabytes';
2306 }
2307 } else {
2308 $msg = 'size-kilobytes';
2309 }
2310 } else {
2311 $msg = 'size-bytes';
2312 }
2313 $size = round( $size, $round );
2314 $text = $this->getMessageFromDB( $msg );
2315 return str_replace( '$1', $this->formatNum( $size ), $text );
2316 }
2317 }