(bug 37179) Allow undelete revision rows l10n
[lhc/web/wiklou.git] / languages / Language.php
1 <?php
2 /**
3 * Internationalisation code
4 *
5 * @file
6 * @ingroup Language
7 */
8
9 /**
10 * @defgroup Language Language
11 */
12
13 if ( !defined( 'MEDIAWIKI' ) ) {
14 echo "This file is part of MediaWiki, it is not a valid entry point.\n";
15 exit( 1 );
16 }
17
18 # Read language names
19 global $wgLanguageNames;
20 require_once( dirname( __FILE__ ) . '/Names.php' );
21
22 if ( function_exists( 'mb_strtoupper' ) ) {
23 mb_internal_encoding( 'UTF-8' );
24 }
25
26 /**
27 * a fake language converter
28 *
29 * @ingroup Language
30 */
31 class FakeConverter {
32
33 /**
34 * @var Language
35 */
36 var $mLang;
37 function __construct( $langobj ) { $this->mLang = $langobj; }
38 function autoConvertToAllVariants( $text ) { return array( $this->mLang->getCode() => $text ); }
39 function convert( $t ) { return $t; }
40 function convertTo( $text, $variant ) { return $text; }
41 function convertTitle( $t ) { return $t->getPrefixedText(); }
42 function getVariants() { return array( $this->mLang->getCode() ); }
43 function getPreferredVariant() { return $this->mLang->getCode(); }
44 function getDefaultVariant() { return $this->mLang->getCode(); }
45 function getURLVariant() { return ''; }
46 function getConvRuleTitle() { return false; }
47 function findVariantLink( &$l, &$n, $ignoreOtherCond = false ) { }
48 function getExtraHashOptions() { return ''; }
49 function getParsedTitle() { return ''; }
50 function markNoConversion( $text, $noParse = false ) { return $text; }
51 function convertCategoryKey( $key ) { return $key; }
52 function convertLinkToAllVariants( $text ) { return $this->autoConvertToAllVariants( $text ); }
53 function armourMath( $text ) { return $text; }
54 }
55
56 /**
57 * Internationalisation code
58 * @ingroup Language
59 */
60 class Language {
61
62 /**
63 * @var LanguageConverter
64 */
65 var $mConverter;
66
67 var $mVariants, $mCode, $mLoaded = false;
68 var $mMagicExtensions = array(), $mMagicHookDone = false;
69 private $mHtmlCode = null;
70
71 var $dateFormatStrings = array();
72 var $mExtendedSpecialPageAliases;
73
74 protected $namespaceNames, $mNamespaceIds, $namespaceAliases;
75
76 /**
77 * ReplacementArray object caches
78 */
79 var $transformData = array();
80
81 /**
82 * @var LocalisationCache
83 */
84 static public $dataCache;
85
86 static public $mLangObjCache = array();
87
88 static public $mWeekdayMsgs = array(
89 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday',
90 'friday', 'saturday'
91 );
92
93 static public $mWeekdayAbbrevMsgs = array(
94 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'
95 );
96
97 static public $mMonthMsgs = array(
98 'january', 'february', 'march', 'april', 'may_long', 'june',
99 'july', 'august', 'september', 'october', 'november',
100 'december'
101 );
102 static public $mMonthGenMsgs = array(
103 'january-gen', 'february-gen', 'march-gen', 'april-gen', 'may-gen', 'june-gen',
104 'july-gen', 'august-gen', 'september-gen', 'october-gen', 'november-gen',
105 'december-gen'
106 );
107 static public $mMonthAbbrevMsgs = array(
108 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
109 'sep', 'oct', 'nov', 'dec'
110 );
111
112 static public $mIranianCalendarMonthMsgs = array(
113 'iranian-calendar-m1', 'iranian-calendar-m2', 'iranian-calendar-m3',
114 'iranian-calendar-m4', 'iranian-calendar-m5', 'iranian-calendar-m6',
115 'iranian-calendar-m7', 'iranian-calendar-m8', 'iranian-calendar-m9',
116 'iranian-calendar-m10', 'iranian-calendar-m11', 'iranian-calendar-m12'
117 );
118
119 static public $mHebrewCalendarMonthMsgs = array(
120 'hebrew-calendar-m1', 'hebrew-calendar-m2', 'hebrew-calendar-m3',
121 'hebrew-calendar-m4', 'hebrew-calendar-m5', 'hebrew-calendar-m6',
122 'hebrew-calendar-m7', 'hebrew-calendar-m8', 'hebrew-calendar-m9',
123 'hebrew-calendar-m10', 'hebrew-calendar-m11', 'hebrew-calendar-m12',
124 'hebrew-calendar-m6a', 'hebrew-calendar-m6b'
125 );
126
127 static public $mHebrewCalendarMonthGenMsgs = array(
128 'hebrew-calendar-m1-gen', 'hebrew-calendar-m2-gen', 'hebrew-calendar-m3-gen',
129 'hebrew-calendar-m4-gen', 'hebrew-calendar-m5-gen', 'hebrew-calendar-m6-gen',
130 'hebrew-calendar-m7-gen', 'hebrew-calendar-m8-gen', 'hebrew-calendar-m9-gen',
131 'hebrew-calendar-m10-gen', 'hebrew-calendar-m11-gen', 'hebrew-calendar-m12-gen',
132 'hebrew-calendar-m6a-gen', 'hebrew-calendar-m6b-gen'
133 );
134
135 static public $mHijriCalendarMonthMsgs = array(
136 'hijri-calendar-m1', 'hijri-calendar-m2', 'hijri-calendar-m3',
137 'hijri-calendar-m4', 'hijri-calendar-m5', 'hijri-calendar-m6',
138 'hijri-calendar-m7', 'hijri-calendar-m8', 'hijri-calendar-m9',
139 'hijri-calendar-m10', 'hijri-calendar-m11', 'hijri-calendar-m12'
140 );
141
142 /**
143 * @since 1.20
144 * @var array
145 */
146 static public $durationIntervals = array(
147 'millennia' => 31557600000,
148 'centuries' => 3155760000,
149 'decades' => 315576000,
150 'years' => 31557600, // 86400 * 365.25
151 'weeks' => 604800,
152 'days' => 86400,
153 'hours' => 3600,
154 'minutes' => 60,
155 'seconds' => 1,
156 );
157
158 /**
159 * Get a cached language object for a given language code
160 * @param $code String
161 * @return Language
162 */
163 static function factory( $code ) {
164 if ( !isset( self::$mLangObjCache[$code] ) ) {
165 if ( count( self::$mLangObjCache ) > 10 ) {
166 // Don't keep a billion objects around, that's stupid.
167 self::$mLangObjCache = array();
168 }
169 self::$mLangObjCache[$code] = self::newFromCode( $code );
170 }
171 return self::$mLangObjCache[$code];
172 }
173
174 /**
175 * Create a language object for a given language code
176 * @param $code String
177 * @throws MWException
178 * @return Language
179 */
180 protected static function newFromCode( $code ) {
181 // Protect against path traversal below
182 if ( !Language::isValidCode( $code )
183 || strcspn( $code, ":/\\\000" ) !== strlen( $code ) )
184 {
185 throw new MWException( "Invalid language code \"$code\"" );
186 }
187
188 if ( !Language::isValidBuiltInCode( $code ) ) {
189 // It's not possible to customise this code with class files, so
190 // just return a Language object. This is to support uselang= hacks.
191 $lang = new Language;
192 $lang->setCode( $code );
193 return $lang;
194 }
195
196 // Check if there is a language class for the code
197 $class = self::classFromCode( $code );
198 self::preloadLanguageClass( $class );
199 if ( MWInit::classExists( $class ) ) {
200 $lang = new $class;
201 return $lang;
202 }
203
204 // Keep trying the fallback list until we find an existing class
205 $fallbacks = Language::getFallbacksFor( $code );
206 foreach ( $fallbacks as $fallbackCode ) {
207 if ( !Language::isValidBuiltInCode( $fallbackCode ) ) {
208 throw new MWException( "Invalid fallback '$fallbackCode' in fallback sequence for '$code'" );
209 }
210
211 $class = self::classFromCode( $fallbackCode );
212 self::preloadLanguageClass( $class );
213 if ( MWInit::classExists( $class ) ) {
214 $lang = Language::newFromCode( $fallbackCode );
215 $lang->setCode( $code );
216 return $lang;
217 }
218 }
219
220 throw new MWException( "Invalid fallback sequence for language '$code'" );
221 }
222
223 /**
224 * Returns true if a language code string is of a valid form, whether or
225 * not it exists. This includes codes which are used solely for
226 * customisation via the MediaWiki namespace.
227 *
228 * @param $code string
229 *
230 * @return bool
231 */
232 public static function isValidCode( $code ) {
233 return
234 strcspn( $code, ":/\\\000" ) === strlen( $code )
235 && !preg_match( Title::getTitleInvalidRegex(), $code );
236 }
237
238 /**
239 * Returns true if a language code is of a valid form for the purposes of
240 * internal customisation of MediaWiki, via Messages*.php.
241 *
242 * @param $code string
243 *
244 * @since 1.18
245 * @return bool
246 */
247 public static function isValidBuiltInCode( $code ) {
248
249 if( !is_string($code) ) {
250 $type = gettype( $code );
251 if( $type === 'object' ) {
252 $addmsg = " of class " . get_class( $code );
253 } else {
254 $addmsg = '';
255 }
256 throw new MWException( __METHOD__ . " must be passed a string, $type given$addmsg" );
257 }
258
259 return preg_match( '/^[a-z0-9-]+$/i', $code );
260 }
261
262 /**
263 * @param $code
264 * @return String Name of the language class
265 */
266 public static function classFromCode( $code ) {
267 if ( $code == 'en' ) {
268 return 'Language';
269 } else {
270 return 'Language' . str_replace( '-', '_', ucfirst( $code ) );
271 }
272 }
273
274 /**
275 * Includes language class files
276 *
277 * @param $class string Name of the language class
278 */
279 public static function preloadLanguageClass( $class ) {
280 global $IP;
281
282 if ( $class === 'Language' ) {
283 return;
284 }
285
286 if ( !defined( 'MW_COMPILED' ) ) {
287 if ( file_exists( "$IP/languages/classes/$class.php" ) ) {
288 include_once( "$IP/languages/classes/$class.php" );
289 }
290 }
291 }
292
293 /**
294 * Get the LocalisationCache instance
295 *
296 * @return LocalisationCache
297 */
298 public static function getLocalisationCache() {
299 if ( is_null( self::$dataCache ) ) {
300 global $wgLocalisationCacheConf;
301 $class = $wgLocalisationCacheConf['class'];
302 self::$dataCache = new $class( $wgLocalisationCacheConf );
303 }
304 return self::$dataCache;
305 }
306
307 function __construct() {
308 $this->mConverter = new FakeConverter( $this );
309 // Set the code to the name of the descendant
310 if ( get_class( $this ) == 'Language' ) {
311 $this->mCode = 'en';
312 } else {
313 $this->mCode = str_replace( '_', '-', strtolower( substr( get_class( $this ), 8 ) ) );
314 }
315 self::getLocalisationCache();
316 }
317
318 /**
319 * Reduce memory usage
320 */
321 function __destruct() {
322 foreach ( $this as $name => $value ) {
323 unset( $this->$name );
324 }
325 }
326
327 /**
328 * Hook which will be called if this is the content language.
329 * Descendants can use this to register hook functions or modify globals
330 */
331 function initContLang() { }
332
333 /**
334 * Same as getFallbacksFor for current language.
335 * @return array|bool
336 * @deprecated in 1.19
337 */
338 function getFallbackLanguageCode() {
339 wfDeprecated( __METHOD__ );
340 return self::getFallbackFor( $this->mCode );
341 }
342
343 /**
344 * @return array
345 * @since 1.19
346 */
347 function getFallbackLanguages() {
348 return self::getFallbacksFor( $this->mCode );
349 }
350
351 /**
352 * Exports $wgBookstoreListEn
353 * @return array
354 */
355 function getBookstoreList() {
356 return self::$dataCache->getItem( $this->mCode, 'bookstoreList' );
357 }
358
359 /**
360 * @return array
361 */
362 public function getNamespaces() {
363 if ( is_null( $this->namespaceNames ) ) {
364 global $wgMetaNamespace, $wgMetaNamespaceTalk, $wgExtraNamespaces;
365
366 $this->namespaceNames = self::$dataCache->getItem( $this->mCode, 'namespaceNames' );
367 $validNamespaces = MWNamespace::getCanonicalNamespaces();
368
369 $this->namespaceNames = $wgExtraNamespaces + $this->namespaceNames + $validNamespaces;
370
371 $this->namespaceNames[NS_PROJECT] = $wgMetaNamespace;
372 if ( $wgMetaNamespaceTalk ) {
373 $this->namespaceNames[NS_PROJECT_TALK] = $wgMetaNamespaceTalk;
374 } else {
375 $talk = $this->namespaceNames[NS_PROJECT_TALK];
376 $this->namespaceNames[NS_PROJECT_TALK] =
377 $this->fixVariableInNamespace( $talk );
378 }
379
380 # Sometimes a language will be localised but not actually exist on this wiki.
381 foreach ( $this->namespaceNames as $key => $text ) {
382 if ( !isset( $validNamespaces[$key] ) ) {
383 unset( $this->namespaceNames[$key] );
384 }
385 }
386
387 # The above mixing may leave namespaces out of canonical order.
388 # Re-order by namespace ID number...
389 ksort( $this->namespaceNames );
390
391 wfRunHooks( 'LanguageGetNamespaces', array( &$this->namespaceNames ) );
392 }
393 return $this->namespaceNames;
394 }
395
396 /**
397 * Arbitrarily set all of the namespace names at once. Mainly used for testing
398 * @param $namespaces Array of namespaces (id => name)
399 */
400 public function setNamespaces( array $namespaces ) {
401 $this->namespaceNames = $namespaces;
402 }
403
404 /**
405 * A convenience function that returns the same thing as
406 * getNamespaces() except with the array values changed to ' '
407 * where it found '_', useful for producing output to be displayed
408 * e.g. in <select> forms.
409 *
410 * @return array
411 */
412 function getFormattedNamespaces() {
413 $ns = $this->getNamespaces();
414 foreach ( $ns as $k => $v ) {
415 $ns[$k] = strtr( $v, '_', ' ' );
416 }
417 return $ns;
418 }
419
420 /**
421 * Get a namespace value by key
422 * <code>
423 * $mw_ns = $wgContLang->getNsText( NS_MEDIAWIKI );
424 * echo $mw_ns; // prints 'MediaWiki'
425 * </code>
426 *
427 * @param $index Int: the array key of the namespace to return
428 * @return mixed, string if the namespace value exists, otherwise false
429 */
430 function getNsText( $index ) {
431 $ns = $this->getNamespaces();
432 return isset( $ns[$index] ) ? $ns[$index] : false;
433 }
434
435 /**
436 * A convenience function that returns the same thing as
437 * getNsText() except with '_' changed to ' ', useful for
438 * producing output.
439 *
440 * @param $index string
441 *
442 * @return array
443 */
444 function getFormattedNsText( $index ) {
445 $ns = $this->getNsText( $index );
446 return strtr( $ns, '_', ' ' );
447 }
448
449 /**
450 * Returns gender-dependent namespace alias if available.
451 * @param $index Int: namespace index
452 * @param $gender String: gender key (male, female... )
453 * @return String
454 * @since 1.18
455 */
456 function getGenderNsText( $index, $gender ) {
457 global $wgExtraGenderNamespaces;
458
459 $ns = $wgExtraGenderNamespaces + self::$dataCache->getItem( $this->mCode, 'namespaceGenderAliases' );
460 return isset( $ns[$index][$gender] ) ? $ns[$index][$gender] : $this->getNsText( $index );
461 }
462
463 /**
464 * Whether this language makes distinguishes genders for example in
465 * namespaces.
466 * @return bool
467 * @since 1.18
468 */
469 function needsGenderDistinction() {
470 global $wgExtraGenderNamespaces, $wgExtraNamespaces;
471 if ( count( $wgExtraGenderNamespaces ) > 0 ) {
472 // $wgExtraGenderNamespaces overrides everything
473 return true;
474 } elseif ( isset( $wgExtraNamespaces[NS_USER] ) && isset( $wgExtraNamespaces[NS_USER_TALK] ) ) {
475 /// @todo There may be other gender namespace than NS_USER & NS_USER_TALK in the future
476 // $wgExtraNamespaces overrides any gender aliases specified in i18n files
477 return false;
478 } else {
479 // Check what is in i18n files
480 $aliases = self::$dataCache->getItem( $this->mCode, 'namespaceGenderAliases' );
481 return count( $aliases ) > 0;
482 }
483 }
484
485 /**
486 * Get a namespace key by value, case insensitive.
487 * Only matches namespace names for the current language, not the
488 * canonical ones defined in Namespace.php.
489 *
490 * @param $text String
491 * @return mixed An integer if $text is a valid value otherwise false
492 */
493 function getLocalNsIndex( $text ) {
494 $lctext = $this->lc( $text );
495 $ids = $this->getNamespaceIds();
496 return isset( $ids[$lctext] ) ? $ids[$lctext] : false;
497 }
498
499 /**
500 * @return array
501 */
502 function getNamespaceAliases() {
503 if ( is_null( $this->namespaceAliases ) ) {
504 $aliases = self::$dataCache->getItem( $this->mCode, 'namespaceAliases' );
505 if ( !$aliases ) {
506 $aliases = array();
507 } else {
508 foreach ( $aliases as $name => $index ) {
509 if ( $index === NS_PROJECT_TALK ) {
510 unset( $aliases[$name] );
511 $name = $this->fixVariableInNamespace( $name );
512 $aliases[$name] = $index;
513 }
514 }
515 }
516
517 global $wgExtraGenderNamespaces;
518 $genders = $wgExtraGenderNamespaces + (array)self::$dataCache->getItem( $this->mCode, 'namespaceGenderAliases' );
519 foreach ( $genders as $index => $forms ) {
520 foreach ( $forms as $alias ) {
521 $aliases[$alias] = $index;
522 }
523 }
524
525 $this->namespaceAliases = $aliases;
526 }
527 return $this->namespaceAliases;
528 }
529
530 /**
531 * @return array
532 */
533 function getNamespaceIds() {
534 if ( is_null( $this->mNamespaceIds ) ) {
535 global $wgNamespaceAliases;
536 # Put namespace names and aliases into a hashtable.
537 # If this is too slow, then we should arrange it so that it is done
538 # before caching. The catch is that at pre-cache time, the above
539 # class-specific fixup hasn't been done.
540 $this->mNamespaceIds = array();
541 foreach ( $this->getNamespaces() as $index => $name ) {
542 $this->mNamespaceIds[$this->lc( $name )] = $index;
543 }
544 foreach ( $this->getNamespaceAliases() as $name => $index ) {
545 $this->mNamespaceIds[$this->lc( $name )] = $index;
546 }
547 if ( $wgNamespaceAliases ) {
548 foreach ( $wgNamespaceAliases as $name => $index ) {
549 $this->mNamespaceIds[$this->lc( $name )] = $index;
550 }
551 }
552 }
553 return $this->mNamespaceIds;
554 }
555
556 /**
557 * Get a namespace key by value, case insensitive. Canonical namespace
558 * names override custom ones defined for the current language.
559 *
560 * @param $text String
561 * @return mixed An integer if $text is a valid value otherwise false
562 */
563 function getNsIndex( $text ) {
564 $lctext = $this->lc( $text );
565 $ns = MWNamespace::getCanonicalIndex( $lctext );
566 if ( $ns !== null ) {
567 return $ns;
568 }
569 $ids = $this->getNamespaceIds();
570 return isset( $ids[$lctext] ) ? $ids[$lctext] : false;
571 }
572
573 /**
574 * short names for language variants used for language conversion links.
575 *
576 * @param $code String
577 * @param $usemsg bool Use the "variantname-xyz" message if it exists
578 * @return string
579 */
580 function getVariantname( $code, $usemsg = true ) {
581 $msg = "variantname-$code";
582 if ( $usemsg && wfMessage( $msg )->exists() ) {
583 return $this->getMessageFromDB( $msg );
584 }
585 $name = self::fetchLanguageName( $code );
586 if ( $name ) {
587 return $name; # if it's defined as a language name, show that
588 } else {
589 # otherwise, output the language code
590 return $code;
591 }
592 }
593
594 /**
595 * @param $name string
596 * @return string
597 */
598 function specialPage( $name ) {
599 $aliases = $this->getSpecialPageAliases();
600 if ( isset( $aliases[$name][0] ) ) {
601 $name = $aliases[$name][0];
602 }
603 return $this->getNsText( NS_SPECIAL ) . ':' . $name;
604 }
605
606 /**
607 * @return array
608 */
609 function getQuickbarSettings() {
610 return array(
611 $this->getMessage( 'qbsettings-none' ),
612 $this->getMessage( 'qbsettings-fixedleft' ),
613 $this->getMessage( 'qbsettings-fixedright' ),
614 $this->getMessage( 'qbsettings-floatingleft' ),
615 $this->getMessage( 'qbsettings-floatingright' ),
616 $this->getMessage( 'qbsettings-directionality' )
617 );
618 }
619
620 /**
621 * @return array
622 */
623 function getDatePreferences() {
624 return self::$dataCache->getItem( $this->mCode, 'datePreferences' );
625 }
626
627 /**
628 * @return array
629 */
630 function getDateFormats() {
631 return self::$dataCache->getItem( $this->mCode, 'dateFormats' );
632 }
633
634 /**
635 * @return array|string
636 */
637 function getDefaultDateFormat() {
638 $df = self::$dataCache->getItem( $this->mCode, 'defaultDateFormat' );
639 if ( $df === 'dmy or mdy' ) {
640 global $wgAmericanDates;
641 return $wgAmericanDates ? 'mdy' : 'dmy';
642 } else {
643 return $df;
644 }
645 }
646
647 /**
648 * @return array
649 */
650 function getDatePreferenceMigrationMap() {
651 return self::$dataCache->getItem( $this->mCode, 'datePreferenceMigrationMap' );
652 }
653
654 /**
655 * @param $image
656 * @return array|null
657 */
658 function getImageFile( $image ) {
659 return self::$dataCache->getSubitem( $this->mCode, 'imageFiles', $image );
660 }
661
662 /**
663 * @return array
664 */
665 function getExtraUserToggles() {
666 return (array)self::$dataCache->getItem( $this->mCode, 'extraUserToggles' );
667 }
668
669 /**
670 * @param $tog
671 * @return string
672 */
673 function getUserToggle( $tog ) {
674 return $this->getMessageFromDB( "tog-$tog" );
675 }
676
677 /**
678 * Get native language names, indexed by code.
679 * Only those defined in MediaWiki, no other data like CLDR.
680 * If $customisedOnly is true, only returns codes with a messages file
681 *
682 * @param $customisedOnly bool
683 *
684 * @return array
685 * @deprecated in 1.20, use fetchLanguageNames()
686 */
687 public static function getLanguageNames( $customisedOnly = false ) {
688 return self::fetchLanguageNames( null, $customisedOnly ? 'mwfile' : 'mw' );
689 }
690
691 /**
692 * Get translated language names. This is done on best effort and
693 * by default this is exactly the same as Language::getLanguageNames.
694 * The CLDR extension provides translated names.
695 * @param $code String Language code.
696 * @return Array language code => language name
697 * @since 1.18.0
698 * @deprecated in 1.20, use fetchLanguageNames()
699 */
700 public static function getTranslatedLanguageNames( $code ) {
701 return self::fetchLanguageNames( $code, 'all' );
702 }
703
704 /**
705 * Get an array of language names, indexed by code.
706 * @param $inLanguage null|string: Code of language in which to return the names
707 * Use null for autonyms (native names)
708 * @param $include string:
709 * 'all' all available languages
710 * 'mw' only if the language is defined in MediaWiki or wgExtraLanguageNames (default)
711 * 'mwfile' only if the language is in 'mw' *and* has a message file
712 * @return array: language code => language name
713 * @since 1.20
714 */
715 public static function fetchLanguageNames( $inLanguage = null, $include = 'mw' ) {
716 global $wgExtraLanguageNames;
717 static $coreLanguageNames;
718
719 if ( $coreLanguageNames === null ) {
720 include( MWInit::compiledPath( 'languages/Names.php' ) );
721 }
722
723 $names = array();
724
725 if( $inLanguage ) {
726 # TODO: also include when $inLanguage is null, when this code is more efficient
727 wfRunHooks( 'LanguageGetTranslatedLanguageNames', array( &$names, $inLanguage ) );
728 }
729
730 $mwNames = $wgExtraLanguageNames + $coreLanguageNames;
731 foreach ( $mwNames as $mwCode => $mwName ) {
732 # - Prefer own MediaWiki native name when not using the hook
733 # - For other names just add if not added through the hook
734 if ( $mwCode === $inLanguage || !isset( $names[$mwCode] ) ) {
735 $names[$mwCode] = $mwName;
736 }
737 }
738
739 if ( $include === 'all' ) {
740 return $names;
741 }
742
743 $returnMw = array();
744 $coreCodes = array_keys( $mwNames );
745 foreach( $coreCodes as $coreCode ) {
746 $returnMw[$coreCode] = $names[$coreCode];
747 }
748
749 if( $include === 'mwfile' ) {
750 $namesMwFile = array();
751 # We do this using a foreach over the codes instead of a directory
752 # loop so that messages files in extensions will work correctly.
753 foreach ( $returnMw as $code => $value ) {
754 if ( is_readable( self::getMessagesFileName( $code ) ) ) {
755 $namesMwFile[$code] = $names[$code];
756 }
757 }
758 return $namesMwFile;
759 }
760 # 'mw' option; default if it's not one of the other two options (all/mwfile)
761 return $returnMw;
762 }
763
764 /**
765 * @param $code string: The code of the language for which to get the name
766 * @param $inLanguage null|string: Code of language in which to return the name (null for autonyms)
767 * @param $include string: 'all', 'mw' or 'mwfile'; see fetchLanguageNames()
768 * @return string: Language name or empty
769 * @since 1.20
770 */
771 public static function fetchLanguageName( $code, $inLanguage = null, $include = 'all' ) {
772 $array = self::fetchLanguageNames( $inLanguage, $include );
773 return !array_key_exists( $code, $array ) ? '' : $array[$code];
774 }
775
776 /**
777 * Get a message from the MediaWiki namespace.
778 *
779 * @param $msg String: message name
780 * @return string
781 */
782 function getMessageFromDB( $msg ) {
783 return wfMsgExt( $msg, array( 'parsemag', 'language' => $this ) );
784 }
785
786 /**
787 * Get the native language name of $code.
788 * Only if defined in MediaWiki, no other data like CLDR.
789 * @param $code string
790 * @return string
791 * @deprecated in 1.20, use fetchLanguageName()
792 */
793 function getLanguageName( $code ) {
794 return self::fetchLanguageName( $code );
795 }
796
797 /**
798 * @param $key string
799 * @return string
800 */
801 function getMonthName( $key ) {
802 return $this->getMessageFromDB( self::$mMonthMsgs[$key - 1] );
803 }
804
805 /**
806 * @return array
807 */
808 function getMonthNamesArray() {
809 $monthNames = array( '' );
810 for ( $i = 1; $i < 13; $i++ ) {
811 $monthNames[] = $this->getMonthName( $i );
812 }
813 return $monthNames;
814 }
815
816 /**
817 * @param $key string
818 * @return string
819 */
820 function getMonthNameGen( $key ) {
821 return $this->getMessageFromDB( self::$mMonthGenMsgs[$key - 1] );
822 }
823
824 /**
825 * @param $key string
826 * @return string
827 */
828 function getMonthAbbreviation( $key ) {
829 return $this->getMessageFromDB( self::$mMonthAbbrevMsgs[$key - 1] );
830 }
831
832 /**
833 * @return array
834 */
835 function getMonthAbbreviationsArray() {
836 $monthNames = array( '' );
837 for ( $i = 1; $i < 13; $i++ ) {
838 $monthNames[] = $this->getMonthAbbreviation( $i );
839 }
840 return $monthNames;
841 }
842
843 /**
844 * @param $key string
845 * @return string
846 */
847 function getWeekdayName( $key ) {
848 return $this->getMessageFromDB( self::$mWeekdayMsgs[$key - 1] );
849 }
850
851 /**
852 * @param $key string
853 * @return string
854 */
855 function getWeekdayAbbreviation( $key ) {
856 return $this->getMessageFromDB( self::$mWeekdayAbbrevMsgs[$key - 1] );
857 }
858
859 /**
860 * @param $key string
861 * @return string
862 */
863 function getIranianCalendarMonthName( $key ) {
864 return $this->getMessageFromDB( self::$mIranianCalendarMonthMsgs[$key - 1] );
865 }
866
867 /**
868 * @param $key string
869 * @return string
870 */
871 function getHebrewCalendarMonthName( $key ) {
872 return $this->getMessageFromDB( self::$mHebrewCalendarMonthMsgs[$key - 1] );
873 }
874
875 /**
876 * @param $key string
877 * @return string
878 */
879 function getHebrewCalendarMonthNameGen( $key ) {
880 return $this->getMessageFromDB( self::$mHebrewCalendarMonthGenMsgs[$key - 1] );
881 }
882
883 /**
884 * @param $key string
885 * @return string
886 */
887 function getHijriCalendarMonthName( $key ) {
888 return $this->getMessageFromDB( self::$mHijriCalendarMonthMsgs[$key - 1] );
889 }
890
891 /**
892 * This is a workalike of PHP's date() function, but with better
893 * internationalisation, a reduced set of format characters, and a better
894 * escaping format.
895 *
896 * Supported format characters are dDjlNwzWFmMntLoYyaAgGhHiscrU. See the
897 * PHP manual for definitions. There are a number of extensions, which
898 * start with "x":
899 *
900 * xn Do not translate digits of the next numeric format character
901 * xN Toggle raw digit (xn) flag, stays set until explicitly unset
902 * xr Use roman numerals for the next numeric format character
903 * xh Use hebrew numerals for the next numeric format character
904 * xx Literal x
905 * xg Genitive month name
906 *
907 * xij j (day number) in Iranian calendar
908 * xiF F (month name) in Iranian calendar
909 * xin n (month number) in Iranian calendar
910 * xiy y (two digit year) in Iranian calendar
911 * xiY Y (full year) in Iranian calendar
912 *
913 * xjj j (day number) in Hebrew calendar
914 * xjF F (month name) in Hebrew calendar
915 * xjt t (days in month) in Hebrew calendar
916 * xjx xg (genitive month name) in Hebrew calendar
917 * xjn n (month number) in Hebrew calendar
918 * xjY Y (full year) in Hebrew calendar
919 *
920 * xmj j (day number) in Hijri calendar
921 * xmF F (month name) in Hijri calendar
922 * xmn n (month number) in Hijri calendar
923 * xmY Y (full year) in Hijri calendar
924 *
925 * xkY Y (full year) in Thai solar calendar. Months and days are
926 * identical to the Gregorian calendar
927 * xoY Y (full year) in Minguo calendar or Juche year.
928 * Months and days are identical to the
929 * Gregorian calendar
930 * xtY Y (full year) in Japanese nengo. Months and days are
931 * identical to the Gregorian calendar
932 *
933 * Characters enclosed in double quotes will be considered literal (with
934 * the quotes themselves removed). Unmatched quotes will be considered
935 * literal quotes. Example:
936 *
937 * "The month is" F => The month is January
938 * i's" => 20'11"
939 *
940 * Backslash escaping is also supported.
941 *
942 * Input timestamp is assumed to be pre-normalized to the desired local
943 * time zone, if any.
944 *
945 * @param $format String
946 * @param $ts String: 14-character timestamp
947 * YYYYMMDDHHMMSS
948 * 01234567890123
949 * @todo handling of "o" format character for Iranian, Hebrew, Hijri & Thai?
950 *
951 * @return string
952 */
953 function sprintfDate( $format, $ts ) {
954 $s = '';
955 $raw = false;
956 $roman = false;
957 $hebrewNum = false;
958 $unix = false;
959 $rawToggle = false;
960 $iranian = false;
961 $hebrew = false;
962 $hijri = false;
963 $thai = false;
964 $minguo = false;
965 $tenno = false;
966 for ( $p = 0; $p < strlen( $format ); $p++ ) {
967 $num = false;
968 $code = $format[$p];
969 if ( $code == 'x' && $p < strlen( $format ) - 1 ) {
970 $code .= $format[++$p];
971 }
972
973 if ( ( $code === 'xi' || $code == 'xj' || $code == 'xk' || $code == 'xm' || $code == 'xo' || $code == 'xt' ) && $p < strlen( $format ) - 1 ) {
974 $code .= $format[++$p];
975 }
976
977 switch ( $code ) {
978 case 'xx':
979 $s .= 'x';
980 break;
981 case 'xn':
982 $raw = true;
983 break;
984 case 'xN':
985 $rawToggle = !$rawToggle;
986 break;
987 case 'xr':
988 $roman = true;
989 break;
990 case 'xh':
991 $hebrewNum = true;
992 break;
993 case 'xg':
994 $s .= $this->getMonthNameGen( substr( $ts, 4, 2 ) );
995 break;
996 case 'xjx':
997 if ( !$hebrew ) $hebrew = self::tsToHebrew( $ts );
998 $s .= $this->getHebrewCalendarMonthNameGen( $hebrew[1] );
999 break;
1000 case 'd':
1001 $num = substr( $ts, 6, 2 );
1002 break;
1003 case 'D':
1004 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
1005 $s .= $this->getWeekdayAbbreviation( gmdate( 'w', $unix ) + 1 );
1006 break;
1007 case 'j':
1008 $num = intval( substr( $ts, 6, 2 ) );
1009 break;
1010 case 'xij':
1011 if ( !$iranian ) {
1012 $iranian = self::tsToIranian( $ts );
1013 }
1014 $num = $iranian[2];
1015 break;
1016 case 'xmj':
1017 if ( !$hijri ) {
1018 $hijri = self::tsToHijri( $ts );
1019 }
1020 $num = $hijri[2];
1021 break;
1022 case 'xjj':
1023 if ( !$hebrew ) {
1024 $hebrew = self::tsToHebrew( $ts );
1025 }
1026 $num = $hebrew[2];
1027 break;
1028 case 'l':
1029 if ( !$unix ) {
1030 $unix = wfTimestamp( TS_UNIX, $ts );
1031 }
1032 $s .= $this->getWeekdayName( gmdate( 'w', $unix ) + 1 );
1033 break;
1034 case 'N':
1035 if ( !$unix ) {
1036 $unix = wfTimestamp( TS_UNIX, $ts );
1037 }
1038 $w = gmdate( 'w', $unix );
1039 $num = $w ? $w : 7;
1040 break;
1041 case 'w':
1042 if ( !$unix ) {
1043 $unix = wfTimestamp( TS_UNIX, $ts );
1044 }
1045 $num = gmdate( 'w', $unix );
1046 break;
1047 case 'z':
1048 if ( !$unix ) {
1049 $unix = wfTimestamp( TS_UNIX, $ts );
1050 }
1051 $num = gmdate( 'z', $unix );
1052 break;
1053 case 'W':
1054 if ( !$unix ) {
1055 $unix = wfTimestamp( TS_UNIX, $ts );
1056 }
1057 $num = gmdate( 'W', $unix );
1058 break;
1059 case 'F':
1060 $s .= $this->getMonthName( substr( $ts, 4, 2 ) );
1061 break;
1062 case 'xiF':
1063 if ( !$iranian ) {
1064 $iranian = self::tsToIranian( $ts );
1065 }
1066 $s .= $this->getIranianCalendarMonthName( $iranian[1] );
1067 break;
1068 case 'xmF':
1069 if ( !$hijri ) {
1070 $hijri = self::tsToHijri( $ts );
1071 }
1072 $s .= $this->getHijriCalendarMonthName( $hijri[1] );
1073 break;
1074 case 'xjF':
1075 if ( !$hebrew ) {
1076 $hebrew = self::tsToHebrew( $ts );
1077 }
1078 $s .= $this->getHebrewCalendarMonthName( $hebrew[1] );
1079 break;
1080 case 'm':
1081 $num = substr( $ts, 4, 2 );
1082 break;
1083 case 'M':
1084 $s .= $this->getMonthAbbreviation( substr( $ts, 4, 2 ) );
1085 break;
1086 case 'n':
1087 $num = intval( substr( $ts, 4, 2 ) );
1088 break;
1089 case 'xin':
1090 if ( !$iranian ) {
1091 $iranian = self::tsToIranian( $ts );
1092 }
1093 $num = $iranian[1];
1094 break;
1095 case 'xmn':
1096 if ( !$hijri ) {
1097 $hijri = self::tsToHijri ( $ts );
1098 }
1099 $num = $hijri[1];
1100 break;
1101 case 'xjn':
1102 if ( !$hebrew ) {
1103 $hebrew = self::tsToHebrew( $ts );
1104 }
1105 $num = $hebrew[1];
1106 break;
1107 case 't':
1108 if ( !$unix ) {
1109 $unix = wfTimestamp( TS_UNIX, $ts );
1110 }
1111 $num = gmdate( 't', $unix );
1112 break;
1113 case 'xjt':
1114 if ( !$hebrew ) {
1115 $hebrew = self::tsToHebrew( $ts );
1116 }
1117 $num = $hebrew[3];
1118 break;
1119 case 'L':
1120 if ( !$unix ) {
1121 $unix = wfTimestamp( TS_UNIX, $ts );
1122 }
1123 $num = gmdate( 'L', $unix );
1124 break;
1125 case 'o':
1126 if ( !$unix ) {
1127 $unix = wfTimestamp( TS_UNIX, $ts );
1128 }
1129 $num = gmdate( 'o', $unix );
1130 break;
1131 case 'Y':
1132 $num = substr( $ts, 0, 4 );
1133 break;
1134 case 'xiY':
1135 if ( !$iranian ) {
1136 $iranian = self::tsToIranian( $ts );
1137 }
1138 $num = $iranian[0];
1139 break;
1140 case 'xmY':
1141 if ( !$hijri ) {
1142 $hijri = self::tsToHijri( $ts );
1143 }
1144 $num = $hijri[0];
1145 break;
1146 case 'xjY':
1147 if ( !$hebrew ) {
1148 $hebrew = self::tsToHebrew( $ts );
1149 }
1150 $num = $hebrew[0];
1151 break;
1152 case 'xkY':
1153 if ( !$thai ) {
1154 $thai = self::tsToYear( $ts, 'thai' );
1155 }
1156 $num = $thai[0];
1157 break;
1158 case 'xoY':
1159 if ( !$minguo ) {
1160 $minguo = self::tsToYear( $ts, 'minguo' );
1161 }
1162 $num = $minguo[0];
1163 break;
1164 case 'xtY':
1165 if ( !$tenno ) {
1166 $tenno = self::tsToYear( $ts, 'tenno' );
1167 }
1168 $num = $tenno[0];
1169 break;
1170 case 'y':
1171 $num = substr( $ts, 2, 2 );
1172 break;
1173 case 'xiy':
1174 if ( !$iranian ) {
1175 $iranian = self::tsToIranian( $ts );
1176 }
1177 $num = substr( $iranian[0], -2 );
1178 break;
1179 case 'a':
1180 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ? 'am' : 'pm';
1181 break;
1182 case 'A':
1183 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ? 'AM' : 'PM';
1184 break;
1185 case 'g':
1186 $h = substr( $ts, 8, 2 );
1187 $num = $h % 12 ? $h % 12 : 12;
1188 break;
1189 case 'G':
1190 $num = intval( substr( $ts, 8, 2 ) );
1191 break;
1192 case 'h':
1193 $h = substr( $ts, 8, 2 );
1194 $num = sprintf( '%02d', $h % 12 ? $h % 12 : 12 );
1195 break;
1196 case 'H':
1197 $num = substr( $ts, 8, 2 );
1198 break;
1199 case 'i':
1200 $num = substr( $ts, 10, 2 );
1201 break;
1202 case 's':
1203 $num = substr( $ts, 12, 2 );
1204 break;
1205 case 'c':
1206 if ( !$unix ) {
1207 $unix = wfTimestamp( TS_UNIX, $ts );
1208 }
1209 $s .= gmdate( 'c', $unix );
1210 break;
1211 case 'r':
1212 if ( !$unix ) {
1213 $unix = wfTimestamp( TS_UNIX, $ts );
1214 }
1215 $s .= gmdate( 'r', $unix );
1216 break;
1217 case 'U':
1218 if ( !$unix ) {
1219 $unix = wfTimestamp( TS_UNIX, $ts );
1220 }
1221 $num = $unix;
1222 break;
1223 case '\\':
1224 # Backslash escaping
1225 if ( $p < strlen( $format ) - 1 ) {
1226 $s .= $format[++$p];
1227 } else {
1228 $s .= '\\';
1229 }
1230 break;
1231 case '"':
1232 # Quoted literal
1233 if ( $p < strlen( $format ) - 1 ) {
1234 $endQuote = strpos( $format, '"', $p + 1 );
1235 if ( $endQuote === false ) {
1236 # No terminating quote, assume literal "
1237 $s .= '"';
1238 } else {
1239 $s .= substr( $format, $p + 1, $endQuote - $p - 1 );
1240 $p = $endQuote;
1241 }
1242 } else {
1243 # Quote at end of string, assume literal "
1244 $s .= '"';
1245 }
1246 break;
1247 default:
1248 $s .= $format[$p];
1249 }
1250 if ( $num !== false ) {
1251 if ( $rawToggle || $raw ) {
1252 $s .= $num;
1253 $raw = false;
1254 } elseif ( $roman ) {
1255 $s .= self::romanNumeral( $num );
1256 $roman = false;
1257 } elseif ( $hebrewNum ) {
1258 $s .= self::hebrewNumeral( $num );
1259 $hebrewNum = false;
1260 } else {
1261 $s .= $this->formatNum( $num, true );
1262 }
1263 }
1264 }
1265 return $s;
1266 }
1267
1268 private static $GREG_DAYS = array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
1269 private static $IRANIAN_DAYS = array( 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29 );
1270
1271 /**
1272 * Algorithm by Roozbeh Pournader and Mohammad Toossi to convert
1273 * Gregorian dates to Iranian dates. Originally written in C, it
1274 * is released under the terms of GNU Lesser General Public
1275 * License. Conversion to PHP was performed by Niklas Laxström.
1276 *
1277 * Link: http://www.farsiweb.info/jalali/jalali.c
1278 *
1279 * @param $ts string
1280 *
1281 * @return string
1282 */
1283 private static function tsToIranian( $ts ) {
1284 $gy = substr( $ts, 0, 4 ) -1600;
1285 $gm = substr( $ts, 4, 2 ) -1;
1286 $gd = substr( $ts, 6, 2 ) -1;
1287
1288 # Days passed from the beginning (including leap years)
1289 $gDayNo = 365 * $gy
1290 + floor( ( $gy + 3 ) / 4 )
1291 - floor( ( $gy + 99 ) / 100 )
1292 + floor( ( $gy + 399 ) / 400 );
1293
1294 // Add days of the past months of this year
1295 for ( $i = 0; $i < $gm; $i++ ) {
1296 $gDayNo += self::$GREG_DAYS[$i];
1297 }
1298
1299 // Leap years
1300 if ( $gm > 1 && ( ( $gy % 4 === 0 && $gy % 100 !== 0 || ( $gy % 400 == 0 ) ) ) ) {
1301 $gDayNo++;
1302 }
1303
1304 // Days passed in current month
1305 $gDayNo += (int)$gd;
1306
1307 $jDayNo = $gDayNo - 79;
1308
1309 $jNp = floor( $jDayNo / 12053 );
1310 $jDayNo %= 12053;
1311
1312 $jy = 979 + 33 * $jNp + 4 * floor( $jDayNo / 1461 );
1313 $jDayNo %= 1461;
1314
1315 if ( $jDayNo >= 366 ) {
1316 $jy += floor( ( $jDayNo - 1 ) / 365 );
1317 $jDayNo = floor( ( $jDayNo - 1 ) % 365 );
1318 }
1319
1320 for ( $i = 0; $i < 11 && $jDayNo >= self::$IRANIAN_DAYS[$i]; $i++ ) {
1321 $jDayNo -= self::$IRANIAN_DAYS[$i];
1322 }
1323
1324 $jm = $i + 1;
1325 $jd = $jDayNo + 1;
1326
1327 return array( $jy, $jm, $jd );
1328 }
1329
1330 /**
1331 * Converting Gregorian dates to Hijri dates.
1332 *
1333 * Based on a PHP-Nuke block by Sharjeel which is released under GNU/GPL license
1334 *
1335 * @see http://phpnuke.org/modules.php?name=News&file=article&sid=8234&mode=thread&order=0&thold=0
1336 *
1337 * @param $ts string
1338 *
1339 * @return string
1340 */
1341 private static function tsToHijri( $ts ) {
1342 $year = substr( $ts, 0, 4 );
1343 $month = substr( $ts, 4, 2 );
1344 $day = substr( $ts, 6, 2 );
1345
1346 $zyr = $year;
1347 $zd = $day;
1348 $zm = $month;
1349 $zy = $zyr;
1350
1351 if (
1352 ( $zy > 1582 ) || ( ( $zy == 1582 ) && ( $zm > 10 ) ) ||
1353 ( ( $zy == 1582 ) && ( $zm == 10 ) && ( $zd > 14 ) )
1354 )
1355 {
1356 $zjd = (int)( ( 1461 * ( $zy + 4800 + (int)( ( $zm - 14 ) / 12 ) ) ) / 4 ) +
1357 (int)( ( 367 * ( $zm - 2 - 12 * ( (int)( ( $zm - 14 ) / 12 ) ) ) ) / 12 ) -
1358 (int)( ( 3 * (int)( ( ( $zy + 4900 + (int)( ( $zm - 14 ) / 12 ) ) / 100 ) ) ) / 4 ) +
1359 $zd - 32075;
1360 } else {
1361 $zjd = 367 * $zy - (int)( ( 7 * ( $zy + 5001 + (int)( ( $zm - 9 ) / 7 ) ) ) / 4 ) +
1362 (int)( ( 275 * $zm ) / 9 ) + $zd + 1729777;
1363 }
1364
1365 $zl = $zjd -1948440 + 10632;
1366 $zn = (int)( ( $zl - 1 ) / 10631 );
1367 $zl = $zl - 10631 * $zn + 354;
1368 $zj = ( (int)( ( 10985 - $zl ) / 5316 ) ) * ( (int)( ( 50 * $zl ) / 17719 ) ) + ( (int)( $zl / 5670 ) ) * ( (int)( ( 43 * $zl ) / 15238 ) );
1369 $zl = $zl - ( (int)( ( 30 - $zj ) / 15 ) ) * ( (int)( ( 17719 * $zj ) / 50 ) ) - ( (int)( $zj / 16 ) ) * ( (int)( ( 15238 * $zj ) / 43 ) ) + 29;
1370 $zm = (int)( ( 24 * $zl ) / 709 );
1371 $zd = $zl - (int)( ( 709 * $zm ) / 24 );
1372 $zy = 30 * $zn + $zj - 30;
1373
1374 return array( $zy, $zm, $zd );
1375 }
1376
1377 /**
1378 * Converting Gregorian dates to Hebrew dates.
1379 *
1380 * Based on a JavaScript code by Abu Mami and Yisrael Hersch
1381 * (abu-mami@kaluach.net, http://www.kaluach.net), who permitted
1382 * to translate the relevant functions into PHP and release them under
1383 * GNU GPL.
1384 *
1385 * The months are counted from Tishrei = 1. In a leap year, Adar I is 13
1386 * and Adar II is 14. In a non-leap year, Adar is 6.
1387 *
1388 * @param $ts string
1389 *
1390 * @return string
1391 */
1392 private static function tsToHebrew( $ts ) {
1393 # Parse date
1394 $year = substr( $ts, 0, 4 );
1395 $month = substr( $ts, 4, 2 );
1396 $day = substr( $ts, 6, 2 );
1397
1398 # Calculate Hebrew year
1399 $hebrewYear = $year + 3760;
1400
1401 # Month number when September = 1, August = 12
1402 $month += 4;
1403 if ( $month > 12 ) {
1404 # Next year
1405 $month -= 12;
1406 $year++;
1407 $hebrewYear++;
1408 }
1409
1410 # Calculate day of year from 1 September
1411 $dayOfYear = $day;
1412 for ( $i = 1; $i < $month; $i++ ) {
1413 if ( $i == 6 ) {
1414 # February
1415 $dayOfYear += 28;
1416 # Check if the year is leap
1417 if ( $year % 400 == 0 || ( $year % 4 == 0 && $year % 100 > 0 ) ) {
1418 $dayOfYear++;
1419 }
1420 } elseif ( $i == 8 || $i == 10 || $i == 1 || $i == 3 ) {
1421 $dayOfYear += 30;
1422 } else {
1423 $dayOfYear += 31;
1424 }
1425 }
1426
1427 # Calculate the start of the Hebrew year
1428 $start = self::hebrewYearStart( $hebrewYear );
1429
1430 # Calculate next year's start
1431 if ( $dayOfYear <= $start ) {
1432 # Day is before the start of the year - it is the previous year
1433 # Next year's start
1434 $nextStart = $start;
1435 # Previous year
1436 $year--;
1437 $hebrewYear--;
1438 # Add days since previous year's 1 September
1439 $dayOfYear += 365;
1440 if ( ( $year % 400 == 0 ) || ( $year % 100 != 0 && $year % 4 == 0 ) ) {
1441 # Leap year
1442 $dayOfYear++;
1443 }
1444 # Start of the new (previous) year
1445 $start = self::hebrewYearStart( $hebrewYear );
1446 } else {
1447 # Next year's start
1448 $nextStart = self::hebrewYearStart( $hebrewYear + 1 );
1449 }
1450
1451 # Calculate Hebrew day of year
1452 $hebrewDayOfYear = $dayOfYear - $start;
1453
1454 # Difference between year's days
1455 $diff = $nextStart - $start;
1456 # Add 12 (or 13 for leap years) days to ignore the difference between
1457 # Hebrew and Gregorian year (353 at least vs. 365/6) - now the
1458 # difference is only about the year type
1459 if ( ( $year % 400 == 0 ) || ( $year % 100 != 0 && $year % 4 == 0 ) ) {
1460 $diff += 13;
1461 } else {
1462 $diff += 12;
1463 }
1464
1465 # Check the year pattern, and is leap year
1466 # 0 means an incomplete year, 1 means a regular year, 2 means a complete year
1467 # This is mod 30, to work on both leap years (which add 30 days of Adar I)
1468 # and non-leap years
1469 $yearPattern = $diff % 30;
1470 # Check if leap year
1471 $isLeap = $diff >= 30;
1472
1473 # Calculate day in the month from number of day in the Hebrew year
1474 # Don't check Adar - if the day is not in Adar, we will stop before;
1475 # if it is in Adar, we will use it to check if it is Adar I or Adar II
1476 $hebrewDay = $hebrewDayOfYear;
1477 $hebrewMonth = 1;
1478 $days = 0;
1479 while ( $hebrewMonth <= 12 ) {
1480 # Calculate days in this month
1481 if ( $isLeap && $hebrewMonth == 6 ) {
1482 # Adar in a leap year
1483 if ( $isLeap ) {
1484 # Leap year - has Adar I, with 30 days, and Adar II, with 29 days
1485 $days = 30;
1486 if ( $hebrewDay <= $days ) {
1487 # Day in Adar I
1488 $hebrewMonth = 13;
1489 } else {
1490 # Subtract the days of Adar I
1491 $hebrewDay -= $days;
1492 # Try Adar II
1493 $days = 29;
1494 if ( $hebrewDay <= $days ) {
1495 # Day in Adar II
1496 $hebrewMonth = 14;
1497 }
1498 }
1499 }
1500 } elseif ( $hebrewMonth == 2 && $yearPattern == 2 ) {
1501 # Cheshvan in a complete year (otherwise as the rule below)
1502 $days = 30;
1503 } elseif ( $hebrewMonth == 3 && $yearPattern == 0 ) {
1504 # Kislev in an incomplete year (otherwise as the rule below)
1505 $days = 29;
1506 } else {
1507 # Odd months have 30 days, even have 29
1508 $days = 30 - ( $hebrewMonth - 1 ) % 2;
1509 }
1510 if ( $hebrewDay <= $days ) {
1511 # In the current month
1512 break;
1513 } else {
1514 # Subtract the days of the current month
1515 $hebrewDay -= $days;
1516 # Try in the next month
1517 $hebrewMonth++;
1518 }
1519 }
1520
1521 return array( $hebrewYear, $hebrewMonth, $hebrewDay, $days );
1522 }
1523
1524 /**
1525 * This calculates the Hebrew year start, as days since 1 September.
1526 * Based on Carl Friedrich Gauss algorithm for finding Easter date.
1527 * Used for Hebrew date.
1528 *
1529 * @param $year int
1530 *
1531 * @return string
1532 */
1533 private static function hebrewYearStart( $year ) {
1534 $a = intval( ( 12 * ( $year - 1 ) + 17 ) % 19 );
1535 $b = intval( ( $year - 1 ) % 4 );
1536 $m = 32.044093161144 + 1.5542417966212 * $a + $b / 4.0 - 0.0031777940220923 * ( $year - 1 );
1537 if ( $m < 0 ) {
1538 $m--;
1539 }
1540 $Mar = intval( $m );
1541 if ( $m < 0 ) {
1542 $m++;
1543 }
1544 $m -= $Mar;
1545
1546 $c = intval( ( $Mar + 3 * ( $year - 1 ) + 5 * $b + 5 ) % 7 );
1547 if ( $c == 0 && $a > 11 && $m >= 0.89772376543210 ) {
1548 $Mar++;
1549 } elseif ( $c == 1 && $a > 6 && $m >= 0.63287037037037 ) {
1550 $Mar += 2;
1551 } elseif ( $c == 2 || $c == 4 || $c == 6 ) {
1552 $Mar++;
1553 }
1554
1555 $Mar += intval( ( $year - 3761 ) / 100 ) - intval( ( $year - 3761 ) / 400 ) - 24;
1556 return $Mar;
1557 }
1558
1559 /**
1560 * Algorithm to convert Gregorian dates to Thai solar dates,
1561 * Minguo dates or Minguo dates.
1562 *
1563 * Link: http://en.wikipedia.org/wiki/Thai_solar_calendar
1564 * http://en.wikipedia.org/wiki/Minguo_calendar
1565 * http://en.wikipedia.org/wiki/Japanese_era_name
1566 *
1567 * @param $ts String: 14-character timestamp
1568 * @param $cName String: calender name
1569 * @return Array: converted year, month, day
1570 */
1571 private static function tsToYear( $ts, $cName ) {
1572 $gy = substr( $ts, 0, 4 );
1573 $gm = substr( $ts, 4, 2 );
1574 $gd = substr( $ts, 6, 2 );
1575
1576 if ( !strcmp( $cName, 'thai' ) ) {
1577 # Thai solar dates
1578 # Add 543 years to the Gregorian calendar
1579 # Months and days are identical
1580 $gy_offset = $gy + 543;
1581 } elseif ( ( !strcmp( $cName, 'minguo' ) ) || !strcmp( $cName, 'juche' ) ) {
1582 # Minguo dates
1583 # Deduct 1911 years from the Gregorian calendar
1584 # Months and days are identical
1585 $gy_offset = $gy - 1911;
1586 } elseif ( !strcmp( $cName, 'tenno' ) ) {
1587 # Nengō dates up to Meiji period
1588 # Deduct years from the Gregorian calendar
1589 # depending on the nengo periods
1590 # Months and days are identical
1591 if ( ( $gy < 1912 ) || ( ( $gy == 1912 ) && ( $gm < 7 ) ) || ( ( $gy == 1912 ) && ( $gm == 7 ) && ( $gd < 31 ) ) ) {
1592 # Meiji period
1593 $gy_gannen = $gy - 1868 + 1;
1594 $gy_offset = $gy_gannen;
1595 if ( $gy_gannen == 1 ) {
1596 $gy_offset = '元';
1597 }
1598 $gy_offset = '明治' . $gy_offset;
1599 } elseif (
1600 ( ( $gy == 1912 ) && ( $gm == 7 ) && ( $gd == 31 ) ) ||
1601 ( ( $gy == 1912 ) && ( $gm >= 8 ) ) ||
1602 ( ( $gy > 1912 ) && ( $gy < 1926 ) ) ||
1603 ( ( $gy == 1926 ) && ( $gm < 12 ) ) ||
1604 ( ( $gy == 1926 ) && ( $gm == 12 ) && ( $gd < 26 ) )
1605 )
1606 {
1607 # Taishō period
1608 $gy_gannen = $gy - 1912 + 1;
1609 $gy_offset = $gy_gannen;
1610 if ( $gy_gannen == 1 ) {
1611 $gy_offset = '元';
1612 }
1613 $gy_offset = '大正' . $gy_offset;
1614 } elseif (
1615 ( ( $gy == 1926 ) && ( $gm == 12 ) && ( $gd >= 26 ) ) ||
1616 ( ( $gy > 1926 ) && ( $gy < 1989 ) ) ||
1617 ( ( $gy == 1989 ) && ( $gm == 1 ) && ( $gd < 8 ) )
1618 )
1619 {
1620 # Shōwa period
1621 $gy_gannen = $gy - 1926 + 1;
1622 $gy_offset = $gy_gannen;
1623 if ( $gy_gannen == 1 ) {
1624 $gy_offset = '元';
1625 }
1626 $gy_offset = '昭和' . $gy_offset;
1627 } else {
1628 # Heisei period
1629 $gy_gannen = $gy - 1989 + 1;
1630 $gy_offset = $gy_gannen;
1631 if ( $gy_gannen == 1 ) {
1632 $gy_offset = '元';
1633 }
1634 $gy_offset = '平成' . $gy_offset;
1635 }
1636 } else {
1637 $gy_offset = $gy;
1638 }
1639
1640 return array( $gy_offset, $gm, $gd );
1641 }
1642
1643 /**
1644 * Roman number formatting up to 3000
1645 *
1646 * @param $num int
1647 *
1648 * @return string
1649 */
1650 static function romanNumeral( $num ) {
1651 static $table = array(
1652 array( '', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X' ),
1653 array( '', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC', 'C' ),
1654 array( '', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM', 'M' ),
1655 array( '', 'M', 'MM', 'MMM' )
1656 );
1657
1658 $num = intval( $num );
1659 if ( $num > 3000 || $num <= 0 ) {
1660 return $num;
1661 }
1662
1663 $s = '';
1664 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1665 if ( $num >= $pow10 ) {
1666 $s .= $table[$i][(int)floor( $num / $pow10 )];
1667 }
1668 $num = $num % $pow10;
1669 }
1670 return $s;
1671 }
1672
1673 /**
1674 * Hebrew Gematria number formatting up to 9999
1675 *
1676 * @param $num int
1677 *
1678 * @return string
1679 */
1680 static function hebrewNumeral( $num ) {
1681 static $table = array(
1682 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' ),
1683 array( '', 'י', 'כ', 'ל', 'מ', 'נ', 'ס', 'ע', 'פ', 'צ', 'ק' ),
1684 array( '', 'ק', 'ר', 'ש', 'ת', 'תק', 'תר', 'תש', 'תת', 'תתק', 'תתר' ),
1685 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' )
1686 );
1687
1688 $num = intval( $num );
1689 if ( $num > 9999 || $num <= 0 ) {
1690 return $num;
1691 }
1692
1693 $s = '';
1694 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1695 if ( $num >= $pow10 ) {
1696 if ( $num == 15 || $num == 16 ) {
1697 $s .= $table[0][9] . $table[0][$num - 9];
1698 $num = 0;
1699 } else {
1700 $s .= $table[$i][intval( ( $num / $pow10 ) )];
1701 if ( $pow10 == 1000 ) {
1702 $s .= "'";
1703 }
1704 }
1705 }
1706 $num = $num % $pow10;
1707 }
1708 if ( strlen( $s ) == 2 ) {
1709 $str = $s . "'";
1710 } else {
1711 $str = substr( $s, 0, strlen( $s ) - 2 ) . '"';
1712 $str .= substr( $s, strlen( $s ) - 2, 2 );
1713 }
1714 $start = substr( $str, 0, strlen( $str ) - 2 );
1715 $end = substr( $str, strlen( $str ) - 2 );
1716 switch( $end ) {
1717 case 'כ':
1718 $str = $start . 'ך';
1719 break;
1720 case 'מ':
1721 $str = $start . 'ם';
1722 break;
1723 case 'נ':
1724 $str = $start . 'ן';
1725 break;
1726 case 'פ':
1727 $str = $start . 'ף';
1728 break;
1729 case 'צ':
1730 $str = $start . 'ץ';
1731 break;
1732 }
1733 return $str;
1734 }
1735
1736 /**
1737 * Used by date() and time() to adjust the time output.
1738 *
1739 * @param $ts Int the time in date('YmdHis') format
1740 * @param $tz Mixed: adjust the time by this amount (default false, mean we
1741 * get user timecorrection setting)
1742 * @return int
1743 */
1744 function userAdjust( $ts, $tz = false ) {
1745 global $wgUser, $wgLocalTZoffset;
1746
1747 if ( $tz === false ) {
1748 $tz = $wgUser->getOption( 'timecorrection' );
1749 }
1750
1751 $data = explode( '|', $tz, 3 );
1752
1753 if ( $data[0] == 'ZoneInfo' ) {
1754 wfSuppressWarnings();
1755 $userTZ = timezone_open( $data[2] );
1756 wfRestoreWarnings();
1757 if ( $userTZ !== false ) {
1758 $date = date_create( $ts, timezone_open( 'UTC' ) );
1759 date_timezone_set( $date, $userTZ );
1760 $date = date_format( $date, 'YmdHis' );
1761 return $date;
1762 }
1763 # Unrecognized timezone, default to 'Offset' with the stored offset.
1764 $data[0] = 'Offset';
1765 }
1766
1767 $minDiff = 0;
1768 if ( $data[0] == 'System' || $tz == '' ) {
1769 #  Global offset in minutes.
1770 if ( isset( $wgLocalTZoffset ) ) {
1771 $minDiff = $wgLocalTZoffset;
1772 }
1773 } elseif ( $data[0] == 'Offset' ) {
1774 $minDiff = intval( $data[1] );
1775 } else {
1776 $data = explode( ':', $tz );
1777 if ( count( $data ) == 2 ) {
1778 $data[0] = intval( $data[0] );
1779 $data[1] = intval( $data[1] );
1780 $minDiff = abs( $data[0] ) * 60 + $data[1];
1781 if ( $data[0] < 0 ) {
1782 $minDiff = -$minDiff;
1783 }
1784 } else {
1785 $minDiff = intval( $data[0] ) * 60;
1786 }
1787 }
1788
1789 # No difference ? Return time unchanged
1790 if ( 0 == $minDiff ) {
1791 return $ts;
1792 }
1793
1794 wfSuppressWarnings(); // E_STRICT system time bitching
1795 # Generate an adjusted date; take advantage of the fact that mktime
1796 # will normalize out-of-range values so we don't have to split $minDiff
1797 # into hours and minutes.
1798 $t = mktime( (
1799 (int)substr( $ts, 8, 2 ) ), # Hours
1800 (int)substr( $ts, 10, 2 ) + $minDiff, # Minutes
1801 (int)substr( $ts, 12, 2 ), # Seconds
1802 (int)substr( $ts, 4, 2 ), # Month
1803 (int)substr( $ts, 6, 2 ), # Day
1804 (int)substr( $ts, 0, 4 ) ); # Year
1805
1806 $date = date( 'YmdHis', $t );
1807 wfRestoreWarnings();
1808
1809 return $date;
1810 }
1811
1812 /**
1813 * This is meant to be used by time(), date(), and timeanddate() to get
1814 * the date preference they're supposed to use, it should be used in
1815 * all children.
1816 *
1817 *<code>
1818 * function timeanddate([...], $format = true) {
1819 * $datePreference = $this->dateFormat($format);
1820 * [...]
1821 * }
1822 *</code>
1823 *
1824 * @param $usePrefs Mixed: if true, the user's preference is used
1825 * if false, the site/language default is used
1826 * if int/string, assumed to be a format.
1827 * @return string
1828 */
1829 function dateFormat( $usePrefs = true ) {
1830 global $wgUser;
1831
1832 if ( is_bool( $usePrefs ) ) {
1833 if ( $usePrefs ) {
1834 $datePreference = $wgUser->getDatePreference();
1835 } else {
1836 $datePreference = (string)User::getDefaultOption( 'date' );
1837 }
1838 } else {
1839 $datePreference = (string)$usePrefs;
1840 }
1841
1842 // return int
1843 if ( $datePreference == '' ) {
1844 return 'default';
1845 }
1846
1847 return $datePreference;
1848 }
1849
1850 /**
1851 * Get a format string for a given type and preference
1852 * @param $type string May be date, time or both
1853 * @param $pref string The format name as it appears in Messages*.php
1854 *
1855 * @return string
1856 */
1857 function getDateFormatString( $type, $pref ) {
1858 if ( !isset( $this->dateFormatStrings[$type][$pref] ) ) {
1859 if ( $pref == 'default' ) {
1860 $pref = $this->getDefaultDateFormat();
1861 $df = self::$dataCache->getSubitem( $this->mCode, 'dateFormats', "$pref $type" );
1862 } else {
1863 $df = self::$dataCache->getSubitem( $this->mCode, 'dateFormats', "$pref $type" );
1864 if ( is_null( $df ) ) {
1865 $pref = $this->getDefaultDateFormat();
1866 $df = self::$dataCache->getSubitem( $this->mCode, 'dateFormats', "$pref $type" );
1867 }
1868 }
1869 $this->dateFormatStrings[$type][$pref] = $df;
1870 }
1871 return $this->dateFormatStrings[$type][$pref];
1872 }
1873
1874 /**
1875 * @param $ts Mixed: the time format which needs to be turned into a
1876 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1877 * @param $adj Bool: whether to adjust the time output according to the
1878 * user configured offset ($timecorrection)
1879 * @param $format Mixed: true to use user's date format preference
1880 * @param $timecorrection String|bool the time offset as returned by
1881 * validateTimeZone() in Special:Preferences
1882 * @return string
1883 */
1884 function date( $ts, $adj = false, $format = true, $timecorrection = false ) {
1885 $ts = wfTimestamp( TS_MW, $ts );
1886 if ( $adj ) {
1887 $ts = $this->userAdjust( $ts, $timecorrection );
1888 }
1889 $df = $this->getDateFormatString( 'date', $this->dateFormat( $format ) );
1890 return $this->sprintfDate( $df, $ts );
1891 }
1892
1893 /**
1894 * @param $ts Mixed: the time format which needs to be turned into a
1895 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1896 * @param $adj Bool: whether to adjust the time output according to the
1897 * user configured offset ($timecorrection)
1898 * @param $format Mixed: true to use user's date format preference
1899 * @param $timecorrection String|bool the time offset as returned by
1900 * validateTimeZone() in Special:Preferences
1901 * @return string
1902 */
1903 function time( $ts, $adj = false, $format = true, $timecorrection = false ) {
1904 $ts = wfTimestamp( TS_MW, $ts );
1905 if ( $adj ) {
1906 $ts = $this->userAdjust( $ts, $timecorrection );
1907 }
1908 $df = $this->getDateFormatString( 'time', $this->dateFormat( $format ) );
1909 return $this->sprintfDate( $df, $ts );
1910 }
1911
1912 /**
1913 * @param $ts Mixed: the time format which needs to be turned into a
1914 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1915 * @param $adj Bool: whether to adjust the time output according to the
1916 * user configured offset ($timecorrection)
1917 * @param $format Mixed: what format to return, if it's false output the
1918 * default one (default true)
1919 * @param $timecorrection String|bool the time offset as returned by
1920 * validateTimeZone() in Special:Preferences
1921 * @return string
1922 */
1923 function timeanddate( $ts, $adj = false, $format = true, $timecorrection = false ) {
1924 $ts = wfTimestamp( TS_MW, $ts );
1925 if ( $adj ) {
1926 $ts = $this->userAdjust( $ts, $timecorrection );
1927 }
1928 $df = $this->getDateFormatString( 'both', $this->dateFormat( $format ) );
1929 return $this->sprintfDate( $df, $ts );
1930 }
1931
1932 /**
1933 * Takes a number of seconds and turns it into a text using values such as hours and minutes.
1934 *
1935 * @since 1.20
1936 *
1937 * @param integer $seconds The amount of seconds.
1938 * @param array $chosenIntervals The intervals to enable.
1939 *
1940 * @return string
1941 */
1942 public function formatDuration( $seconds, array $chosenIntervals = array() ) {
1943 $intervals = $this->getDurationIntervals( $seconds, $chosenIntervals );
1944
1945 $segments = array();
1946
1947 foreach ( $intervals as $intervalName => $intervalValue ) {
1948 $message = new Message( 'duration-' . $intervalName, array( $intervalValue ) );
1949 $segments[] = $message->inLanguage( $this )->escaped();
1950 }
1951
1952 return $this->listToText( $segments );
1953 }
1954
1955 /**
1956 * Takes a number of seconds and returns an array with a set of corresponding intervals.
1957 * For example 65 will be turned into array( minutes => 1, seconds => 5 ).
1958 *
1959 * @since 1.20
1960 *
1961 * @param integer $seconds The amount of seconds.
1962 * @param array $chosenIntervals The intervals to enable.
1963 *
1964 * @return array
1965 */
1966 public function getDurationIntervals( $seconds, array $chosenIntervals = array() ) {
1967 if ( empty( $chosenIntervals ) ) {
1968 $chosenIntervals = array( 'millennia', 'centuries', 'decades', 'years', 'days', 'hours', 'minutes', 'seconds' );
1969 }
1970
1971 $intervals = array_intersect_key( self::$durationIntervals, array_flip( $chosenIntervals ) );
1972 $sortedNames = array_keys( $intervals );
1973 $smallestInterval = array_pop( $sortedNames );
1974
1975 $segments = array();
1976
1977 foreach ( $intervals as $name => $length ) {
1978 $value = floor( $seconds / $length );
1979
1980 if ( $value > 0 || ( $name == $smallestInterval && empty( $segments ) ) ) {
1981 $seconds -= $value * $length;
1982 $segments[$name] = $value;
1983 }
1984 }
1985
1986 return $segments;
1987 }
1988
1989 /**
1990 * Internal helper function for userDate(), userTime() and userTimeAndDate()
1991 *
1992 * @param $type String: can be 'date', 'time' or 'both'
1993 * @param $ts Mixed: the time format which needs to be turned into a
1994 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1995 * @param $user User object used to get preferences for timezone and format
1996 * @param $options Array, can contain the following keys:
1997 * - 'timecorrection': time correction, can have the following values:
1998 * - true: use user's preference
1999 * - false: don't use time correction
2000 * - integer: value of time correction in minutes
2001 * - 'format': format to use, can have the following values:
2002 * - true: use user's preference
2003 * - false: use default preference
2004 * - string: format to use
2005 * @since 1.19
2006 * @return String
2007 */
2008 private function internalUserTimeAndDate( $type, $ts, User $user, array $options ) {
2009 $ts = wfTimestamp( TS_MW, $ts );
2010 $options += array( 'timecorrection' => true, 'format' => true );
2011 if ( $options['timecorrection'] !== false ) {
2012 if ( $options['timecorrection'] === true ) {
2013 $offset = $user->getOption( 'timecorrection' );
2014 } else {
2015 $offset = $options['timecorrection'];
2016 }
2017 $ts = $this->userAdjust( $ts, $offset );
2018 }
2019 if ( $options['format'] === true ) {
2020 $format = $user->getDatePreference();
2021 } else {
2022 $format = $options['format'];
2023 }
2024 $df = $this->getDateFormatString( $type, $this->dateFormat( $format ) );
2025 return $this->sprintfDate( $df, $ts );
2026 }
2027
2028 /**
2029 * Get the formatted date for the given timestamp and formatted for
2030 * the given user.
2031 *
2032 * @param $ts Mixed: the time format which needs to be turned into a
2033 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2034 * @param $user User object used to get preferences for timezone and format
2035 * @param $options Array, can contain the following keys:
2036 * - 'timecorrection': time correction, can have the following values:
2037 * - true: use user's preference
2038 * - false: don't use time correction
2039 * - integer: value of time correction in minutes
2040 * - 'format': format to use, can have the following values:
2041 * - true: use user's preference
2042 * - false: use default preference
2043 * - string: format to use
2044 * @since 1.19
2045 * @return String
2046 */
2047 public function userDate( $ts, User $user, array $options = array() ) {
2048 return $this->internalUserTimeAndDate( 'date', $ts, $user, $options );
2049 }
2050
2051 /**
2052 * Get the formatted time for the given timestamp and formatted for
2053 * the given user.
2054 *
2055 * @param $ts Mixed: the time format which needs to be turned into a
2056 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2057 * @param $user User object used to get preferences for timezone and format
2058 * @param $options Array, can contain the following keys:
2059 * - 'timecorrection': time correction, can have the following values:
2060 * - true: use user's preference
2061 * - false: don't use time correction
2062 * - integer: value of time correction in minutes
2063 * - 'format': format to use, can have the following values:
2064 * - true: use user's preference
2065 * - false: use default preference
2066 * - string: format to use
2067 * @since 1.19
2068 * @return String
2069 */
2070 public function userTime( $ts, User $user, array $options = array() ) {
2071 return $this->internalUserTimeAndDate( 'time', $ts, $user, $options );
2072 }
2073
2074 /**
2075 * Get the formatted date and time for the given timestamp and formatted for
2076 * the given user.
2077 *
2078 * @param $ts Mixed: the time format which needs to be turned into a
2079 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2080 * @param $user User object used to get preferences for timezone and format
2081 * @param $options Array, can contain the following keys:
2082 * - 'timecorrection': time correction, can have the following values:
2083 * - true: use user's preference
2084 * - false: don't use time correction
2085 * - integer: value of time correction in minutes
2086 * - 'format': format to use, can have the following values:
2087 * - true: use user's preference
2088 * - false: use default preference
2089 * - string: format to use
2090 * @since 1.19
2091 * @return String
2092 */
2093 public function userTimeAndDate( $ts, User $user, array $options = array() ) {
2094 return $this->internalUserTimeAndDate( 'both', $ts, $user, $options );
2095 }
2096
2097 /**
2098 * @param $key string
2099 * @return array|null
2100 */
2101 function getMessage( $key ) {
2102 return self::$dataCache->getSubitem( $this->mCode, 'messages', $key );
2103 }
2104
2105 /**
2106 * @return array
2107 */
2108 function getAllMessages() {
2109 return self::$dataCache->getItem( $this->mCode, 'messages' );
2110 }
2111
2112 /**
2113 * @param $in
2114 * @param $out
2115 * @param $string
2116 * @return string
2117 */
2118 function iconv( $in, $out, $string ) {
2119 # This is a wrapper for iconv in all languages except esperanto,
2120 # which does some nasty x-conversions beforehand
2121
2122 # Even with //IGNORE iconv can whine about illegal characters in
2123 # *input* string. We just ignore those too.
2124 # REF: http://bugs.php.net/bug.php?id=37166
2125 # REF: https://bugzilla.wikimedia.org/show_bug.cgi?id=16885
2126 wfSuppressWarnings();
2127 $text = iconv( $in, $out . '//IGNORE', $string );
2128 wfRestoreWarnings();
2129 return $text;
2130 }
2131
2132 // callback functions for uc(), lc(), ucwords(), ucwordbreaks()
2133
2134 /**
2135 * @param $matches array
2136 * @return mixed|string
2137 */
2138 function ucwordbreaksCallbackAscii( $matches ) {
2139 return $this->ucfirst( $matches[1] );
2140 }
2141
2142 /**
2143 * @param $matches array
2144 * @return string
2145 */
2146 function ucwordbreaksCallbackMB( $matches ) {
2147 return mb_strtoupper( $matches[0] );
2148 }
2149
2150 /**
2151 * @param $matches array
2152 * @return string
2153 */
2154 function ucCallback( $matches ) {
2155 list( $wikiUpperChars ) = self::getCaseMaps();
2156 return strtr( $matches[1], $wikiUpperChars );
2157 }
2158
2159 /**
2160 * @param $matches array
2161 * @return string
2162 */
2163 function lcCallback( $matches ) {
2164 list( , $wikiLowerChars ) = self::getCaseMaps();
2165 return strtr( $matches[1], $wikiLowerChars );
2166 }
2167
2168 /**
2169 * @param $matches array
2170 * @return string
2171 */
2172 function ucwordsCallbackMB( $matches ) {
2173 return mb_strtoupper( $matches[0] );
2174 }
2175
2176 /**
2177 * @param $matches array
2178 * @return string
2179 */
2180 function ucwordsCallbackWiki( $matches ) {
2181 list( $wikiUpperChars ) = self::getCaseMaps();
2182 return strtr( $matches[0], $wikiUpperChars );
2183 }
2184
2185 /**
2186 * Make a string's first character uppercase
2187 *
2188 * @param $str string
2189 *
2190 * @return string
2191 */
2192 function ucfirst( $str ) {
2193 $o = ord( $str );
2194 if ( $o < 96 ) { // if already uppercase...
2195 return $str;
2196 } elseif ( $o < 128 ) {
2197 return ucfirst( $str ); // use PHP's ucfirst()
2198 } else {
2199 // fall back to more complex logic in case of multibyte strings
2200 return $this->uc( $str, true );
2201 }
2202 }
2203
2204 /**
2205 * Convert a string to uppercase
2206 *
2207 * @param $str string
2208 * @param $first bool
2209 *
2210 * @return string
2211 */
2212 function uc( $str, $first = false ) {
2213 if ( function_exists( 'mb_strtoupper' ) ) {
2214 if ( $first ) {
2215 if ( $this->isMultibyte( $str ) ) {
2216 return mb_strtoupper( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
2217 } else {
2218 return ucfirst( $str );
2219 }
2220 } else {
2221 return $this->isMultibyte( $str ) ? mb_strtoupper( $str ) : strtoupper( $str );
2222 }
2223 } else {
2224 if ( $this->isMultibyte( $str ) ) {
2225 $x = $first ? '^' : '';
2226 return preg_replace_callback(
2227 "/$x([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
2228 array( $this, 'ucCallback' ),
2229 $str
2230 );
2231 } else {
2232 return $first ? ucfirst( $str ) : strtoupper( $str );
2233 }
2234 }
2235 }
2236
2237 /**
2238 * @param $str string
2239 * @return mixed|string
2240 */
2241 function lcfirst( $str ) {
2242 $o = ord( $str );
2243 if ( !$o ) {
2244 return strval( $str );
2245 } elseif ( $o >= 128 ) {
2246 return $this->lc( $str, true );
2247 } elseif ( $o > 96 ) {
2248 return $str;
2249 } else {
2250 $str[0] = strtolower( $str[0] );
2251 return $str;
2252 }
2253 }
2254
2255 /**
2256 * @param $str string
2257 * @param $first bool
2258 * @return mixed|string
2259 */
2260 function lc( $str, $first = false ) {
2261 if ( function_exists( 'mb_strtolower' ) ) {
2262 if ( $first ) {
2263 if ( $this->isMultibyte( $str ) ) {
2264 return mb_strtolower( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
2265 } else {
2266 return strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 );
2267 }
2268 } else {
2269 return $this->isMultibyte( $str ) ? mb_strtolower( $str ) : strtolower( $str );
2270 }
2271 } else {
2272 if ( $this->isMultibyte( $str ) ) {
2273 $x = $first ? '^' : '';
2274 return preg_replace_callback(
2275 "/$x([A-Z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
2276 array( $this, 'lcCallback' ),
2277 $str
2278 );
2279 } else {
2280 return $first ? strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 ) : strtolower( $str );
2281 }
2282 }
2283 }
2284
2285 /**
2286 * @param $str string
2287 * @return bool
2288 */
2289 function isMultibyte( $str ) {
2290 return (bool)preg_match( '/[\x80-\xff]/', $str );
2291 }
2292
2293 /**
2294 * @param $str string
2295 * @return mixed|string
2296 */
2297 function ucwords( $str ) {
2298 if ( $this->isMultibyte( $str ) ) {
2299 $str = $this->lc( $str );
2300
2301 // regexp to find first letter in each word (i.e. after each space)
2302 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)| ([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
2303
2304 // function to use to capitalize a single char
2305 if ( function_exists( 'mb_strtoupper' ) ) {
2306 return preg_replace_callback(
2307 $replaceRegexp,
2308 array( $this, 'ucwordsCallbackMB' ),
2309 $str
2310 );
2311 } else {
2312 return preg_replace_callback(
2313 $replaceRegexp,
2314 array( $this, 'ucwordsCallbackWiki' ),
2315 $str
2316 );
2317 }
2318 } else {
2319 return ucwords( strtolower( $str ) );
2320 }
2321 }
2322
2323 /**
2324 * capitalize words at word breaks
2325 *
2326 * @param $str string
2327 * @return mixed
2328 */
2329 function ucwordbreaks( $str ) {
2330 if ( $this->isMultibyte( $str ) ) {
2331 $str = $this->lc( $str );
2332
2333 // since \b doesn't work for UTF-8, we explicitely define word break chars
2334 $breaks = "[ \-\(\)\}\{\.,\?!]";
2335
2336 // find first letter after word break
2337 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)|$breaks([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
2338
2339 if ( function_exists( 'mb_strtoupper' ) ) {
2340 return preg_replace_callback(
2341 $replaceRegexp,
2342 array( $this, 'ucwordbreaksCallbackMB' ),
2343 $str
2344 );
2345 } else {
2346 return preg_replace_callback(
2347 $replaceRegexp,
2348 array( $this, 'ucwordsCallbackWiki' ),
2349 $str
2350 );
2351 }
2352 } else {
2353 return preg_replace_callback(
2354 '/\b([\w\x80-\xff]+)\b/',
2355 array( $this, 'ucwordbreaksCallbackAscii' ),
2356 $str
2357 );
2358 }
2359 }
2360
2361 /**
2362 * Return a case-folded representation of $s
2363 *
2364 * This is a representation such that caseFold($s1)==caseFold($s2) if $s1
2365 * and $s2 are the same except for the case of their characters. It is not
2366 * necessary for the value returned to make sense when displayed.
2367 *
2368 * Do *not* perform any other normalisation in this function. If a caller
2369 * uses this function when it should be using a more general normalisation
2370 * function, then fix the caller.
2371 *
2372 * @param $s string
2373 *
2374 * @return string
2375 */
2376 function caseFold( $s ) {
2377 return $this->uc( $s );
2378 }
2379
2380 /**
2381 * @param $s string
2382 * @return string
2383 */
2384 function checkTitleEncoding( $s ) {
2385 if ( is_array( $s ) ) {
2386 wfDebugDieBacktrace( 'Given array to checkTitleEncoding.' );
2387 }
2388 # Check for non-UTF-8 URLs
2389 $ishigh = preg_match( '/[\x80-\xff]/', $s );
2390 if ( !$ishigh ) {
2391 return $s;
2392 }
2393
2394 if ( function_exists( 'mb_check_encoding' ) ) {
2395 $isutf8 = mb_check_encoding( $s, 'UTF-8' );
2396 } else {
2397 $isutf8 = preg_match( '/^(?>[\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
2398 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s );
2399 }
2400 if ( $isutf8 ) {
2401 return $s;
2402 }
2403
2404 return $this->iconv( $this->fallback8bitEncoding(), 'utf-8', $s );
2405 }
2406
2407 /**
2408 * @return array
2409 */
2410 function fallback8bitEncoding() {
2411 return self::$dataCache->getItem( $this->mCode, 'fallback8bitEncoding' );
2412 }
2413
2414 /**
2415 * Most writing systems use whitespace to break up words.
2416 * Some languages such as Chinese don't conventionally do this,
2417 * which requires special handling when breaking up words for
2418 * searching etc.
2419 *
2420 * @return bool
2421 */
2422 function hasWordBreaks() {
2423 return true;
2424 }
2425
2426 /**
2427 * Some languages such as Chinese require word segmentation,
2428 * Specify such segmentation when overridden in derived class.
2429 *
2430 * @param $string String
2431 * @return String
2432 */
2433 function segmentByWord( $string ) {
2434 return $string;
2435 }
2436
2437 /**
2438 * Some languages have special punctuation need to be normalized.
2439 * Make such changes here.
2440 *
2441 * @param $string String
2442 * @return String
2443 */
2444 function normalizeForSearch( $string ) {
2445 return self::convertDoubleWidth( $string );
2446 }
2447
2448 /**
2449 * convert double-width roman characters to single-width.
2450 * range: ff00-ff5f ~= 0020-007f
2451 *
2452 * @param $string string
2453 *
2454 * @return string
2455 */
2456 protected static function convertDoubleWidth( $string ) {
2457 static $full = null;
2458 static $half = null;
2459
2460 if ( $full === null ) {
2461 $fullWidth = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
2462 $halfWidth = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
2463 $full = str_split( $fullWidth, 3 );
2464 $half = str_split( $halfWidth );
2465 }
2466
2467 $string = str_replace( $full, $half, $string );
2468 return $string;
2469 }
2470
2471 /**
2472 * @param $string string
2473 * @param $pattern string
2474 * @return string
2475 */
2476 protected static function insertSpace( $string, $pattern ) {
2477 $string = preg_replace( $pattern, " $1 ", $string );
2478 $string = preg_replace( '/ +/', ' ', $string );
2479 return $string;
2480 }
2481
2482 /**
2483 * @param $termsArray array
2484 * @return array
2485 */
2486 function convertForSearchResult( $termsArray ) {
2487 # some languages, e.g. Chinese, need to do a conversion
2488 # in order for search results to be displayed correctly
2489 return $termsArray;
2490 }
2491
2492 /**
2493 * Get the first character of a string.
2494 *
2495 * @param $s string
2496 * @return string
2497 */
2498 function firstChar( $s ) {
2499 $matches = array();
2500 preg_match(
2501 '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
2502 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})/',
2503 $s,
2504 $matches
2505 );
2506
2507 if ( isset( $matches[1] ) ) {
2508 if ( strlen( $matches[1] ) != 3 ) {
2509 return $matches[1];
2510 }
2511
2512 // Break down Hangul syllables to grab the first jamo
2513 $code = utf8ToCodepoint( $matches[1] );
2514 if ( $code < 0xac00 || 0xd7a4 <= $code ) {
2515 return $matches[1];
2516 } elseif ( $code < 0xb098 ) {
2517 return "\xe3\x84\xb1";
2518 } elseif ( $code < 0xb2e4 ) {
2519 return "\xe3\x84\xb4";
2520 } elseif ( $code < 0xb77c ) {
2521 return "\xe3\x84\xb7";
2522 } elseif ( $code < 0xb9c8 ) {
2523 return "\xe3\x84\xb9";
2524 } elseif ( $code < 0xbc14 ) {
2525 return "\xe3\x85\x81";
2526 } elseif ( $code < 0xc0ac ) {
2527 return "\xe3\x85\x82";
2528 } elseif ( $code < 0xc544 ) {
2529 return "\xe3\x85\x85";
2530 } elseif ( $code < 0xc790 ) {
2531 return "\xe3\x85\x87";
2532 } elseif ( $code < 0xcc28 ) {
2533 return "\xe3\x85\x88";
2534 } elseif ( $code < 0xce74 ) {
2535 return "\xe3\x85\x8a";
2536 } elseif ( $code < 0xd0c0 ) {
2537 return "\xe3\x85\x8b";
2538 } elseif ( $code < 0xd30c ) {
2539 return "\xe3\x85\x8c";
2540 } elseif ( $code < 0xd558 ) {
2541 return "\xe3\x85\x8d";
2542 } else {
2543 return "\xe3\x85\x8e";
2544 }
2545 } else {
2546 return '';
2547 }
2548 }
2549
2550 function initEncoding() {
2551 # Some languages may have an alternate char encoding option
2552 # (Esperanto X-coding, Japanese furigana conversion, etc)
2553 # If this language is used as the primary content language,
2554 # an override to the defaults can be set here on startup.
2555 }
2556
2557 /**
2558 * @param $s string
2559 * @return string
2560 */
2561 function recodeForEdit( $s ) {
2562 # For some languages we'll want to explicitly specify
2563 # which characters make it into the edit box raw
2564 # or are converted in some way or another.
2565 global $wgEditEncoding;
2566 if ( $wgEditEncoding == '' || $wgEditEncoding == 'UTF-8' ) {
2567 return $s;
2568 } else {
2569 return $this->iconv( 'UTF-8', $wgEditEncoding, $s );
2570 }
2571 }
2572
2573 /**
2574 * @param $s string
2575 * @return string
2576 */
2577 function recodeInput( $s ) {
2578 # Take the previous into account.
2579 global $wgEditEncoding;
2580 if ( $wgEditEncoding != '' ) {
2581 $enc = $wgEditEncoding;
2582 } else {
2583 $enc = 'UTF-8';
2584 }
2585 if ( $enc == 'UTF-8' ) {
2586 return $s;
2587 } else {
2588 return $this->iconv( $enc, 'UTF-8', $s );
2589 }
2590 }
2591
2592 /**
2593 * Convert a UTF-8 string to normal form C. In Malayalam and Arabic, this
2594 * also cleans up certain backwards-compatible sequences, converting them
2595 * to the modern Unicode equivalent.
2596 *
2597 * This is language-specific for performance reasons only.
2598 *
2599 * @param $s string
2600 *
2601 * @return string
2602 */
2603 function normalize( $s ) {
2604 global $wgAllUnicodeFixes;
2605 $s = UtfNormal::cleanUp( $s );
2606 if ( $wgAllUnicodeFixes ) {
2607 $s = $this->transformUsingPairFile( 'normalize-ar.ser', $s );
2608 $s = $this->transformUsingPairFile( 'normalize-ml.ser', $s );
2609 }
2610
2611 return $s;
2612 }
2613
2614 /**
2615 * Transform a string using serialized data stored in the given file (which
2616 * must be in the serialized subdirectory of $IP). The file contains pairs
2617 * mapping source characters to destination characters.
2618 *
2619 * The data is cached in process memory. This will go faster if you have the
2620 * FastStringSearch extension.
2621 *
2622 * @param $file string
2623 * @param $string string
2624 *
2625 * @throws MWException
2626 * @return string
2627 */
2628 function transformUsingPairFile( $file, $string ) {
2629 if ( !isset( $this->transformData[$file] ) ) {
2630 $data = wfGetPrecompiledData( $file );
2631 if ( $data === false ) {
2632 throw new MWException( __METHOD__ . ": The transformation file $file is missing" );
2633 }
2634 $this->transformData[$file] = new ReplacementArray( $data );
2635 }
2636 return $this->transformData[$file]->replace( $string );
2637 }
2638
2639 /**
2640 * For right-to-left language support
2641 *
2642 * @return bool
2643 */
2644 function isRTL() {
2645 return self::$dataCache->getItem( $this->mCode, 'rtl' );
2646 }
2647
2648 /**
2649 * Return the correct HTML 'dir' attribute value for this language.
2650 * @return String
2651 */
2652 function getDir() {
2653 return $this->isRTL() ? 'rtl' : 'ltr';
2654 }
2655
2656 /**
2657 * Return 'left' or 'right' as appropriate alignment for line-start
2658 * for this language's text direction.
2659 *
2660 * Should be equivalent to CSS3 'start' text-align value....
2661 *
2662 * @return String
2663 */
2664 function alignStart() {
2665 return $this->isRTL() ? 'right' : 'left';
2666 }
2667
2668 /**
2669 * Return 'right' or 'left' as appropriate alignment for line-end
2670 * for this language's text direction.
2671 *
2672 * Should be equivalent to CSS3 'end' text-align value....
2673 *
2674 * @return String
2675 */
2676 function alignEnd() {
2677 return $this->isRTL() ? 'left' : 'right';
2678 }
2679
2680 /**
2681 * A hidden direction mark (LRM or RLM), depending on the language direction.
2682 * Unlike getDirMark(), this function returns the character as an HTML entity.
2683 * This function should be used when the output is guaranteed to be HTML,
2684 * because it makes the output HTML source code more readable. When
2685 * the output is plain text or can be escaped, getDirMark() should be used.
2686 *
2687 * @param $opposite Boolean Get the direction mark opposite to your language
2688 * @return string
2689 */
2690 function getDirMarkEntity( $opposite = false ) {
2691 if ( $opposite ) { return $this->isRTL() ? '&lrm;' : '&rlm;'; }
2692 return $this->isRTL() ? '&rlm;' : '&lrm;';
2693 }
2694
2695 /**
2696 * A hidden direction mark (LRM or RLM), depending on the language direction.
2697 * This function produces them as invisible Unicode characters and
2698 * the output may be hard to read and debug, so it should only be used
2699 * when the output is plain text or can be escaped. When the output is
2700 * HTML, use getDirMarkEntity() instead.
2701 *
2702 * @param $opposite Boolean Get the direction mark opposite to your language
2703 * @return string
2704 */
2705 function getDirMark( $opposite = false ) {
2706 $lrm = "\xE2\x80\x8E"; # LEFT-TO-RIGHT MARK, commonly abbreviated LRM
2707 $rlm = "\xE2\x80\x8F"; # RIGHT-TO-LEFT MARK, commonly abbreviated RLM
2708 if ( $opposite ) { return $this->isRTL() ? $lrm : $rlm; }
2709 return $this->isRTL() ? $rlm : $lrm;
2710 }
2711
2712 /**
2713 * @return array
2714 */
2715 function capitalizeAllNouns() {
2716 return self::$dataCache->getItem( $this->mCode, 'capitalizeAllNouns' );
2717 }
2718
2719 /**
2720 * An arrow, depending on the language direction.
2721 *
2722 * @param $direction String: the direction of the arrow: forwards (default), backwards, left, right, up, down.
2723 * @return string
2724 */
2725 function getArrow( $direction = 'forwards' ) {
2726 switch ( $direction ) {
2727 case 'forwards':
2728 return $this->isRTL() ? '←' : '→';
2729 case 'backwards':
2730 return $this->isRTL() ? '→' : '←';
2731 case 'left':
2732 return '←';
2733 case 'right':
2734 return '→';
2735 case 'up':
2736 return '↑';
2737 case 'down':
2738 return '↓';
2739 }
2740 }
2741
2742 /**
2743 * To allow "foo[[bar]]" to extend the link over the whole word "foobar"
2744 *
2745 * @return bool
2746 */
2747 function linkPrefixExtension() {
2748 return self::$dataCache->getItem( $this->mCode, 'linkPrefixExtension' );
2749 }
2750
2751 /**
2752 * @return array
2753 */
2754 function getMagicWords() {
2755 return self::$dataCache->getItem( $this->mCode, 'magicWords' );
2756 }
2757
2758 protected function doMagicHook() {
2759 if ( $this->mMagicHookDone ) {
2760 return;
2761 }
2762 $this->mMagicHookDone = true;
2763 wfProfileIn( 'LanguageGetMagic' );
2764 wfRunHooks( 'LanguageGetMagic', array( &$this->mMagicExtensions, $this->getCode() ) );
2765 wfProfileOut( 'LanguageGetMagic' );
2766 }
2767
2768 /**
2769 * Fill a MagicWord object with data from here
2770 *
2771 * @param $mw
2772 */
2773 function getMagic( $mw ) {
2774 $this->doMagicHook();
2775
2776 if ( isset( $this->mMagicExtensions[$mw->mId] ) ) {
2777 $rawEntry = $this->mMagicExtensions[$mw->mId];
2778 } else {
2779 $magicWords = $this->getMagicWords();
2780 if ( isset( $magicWords[$mw->mId] ) ) {
2781 $rawEntry = $magicWords[$mw->mId];
2782 } else {
2783 $rawEntry = false;
2784 }
2785 }
2786
2787 if ( !is_array( $rawEntry ) ) {
2788 error_log( "\"$rawEntry\" is not a valid magic word for \"$mw->mId\"" );
2789 } else {
2790 $mw->mCaseSensitive = $rawEntry[0];
2791 $mw->mSynonyms = array_slice( $rawEntry, 1 );
2792 }
2793 }
2794
2795 /**
2796 * Add magic words to the extension array
2797 *
2798 * @param $newWords array
2799 */
2800 function addMagicWordsByLang( $newWords ) {
2801 $fallbackChain = $this->getFallbackLanguages();
2802 $fallbackChain = array_reverse( $fallbackChain );
2803 foreach ( $fallbackChain as $code ) {
2804 if ( isset( $newWords[$code] ) ) {
2805 $this->mMagicExtensions = $newWords[$code] + $this->mMagicExtensions;
2806 }
2807 }
2808 }
2809
2810 /**
2811 * Get special page names, as an associative array
2812 * case folded alias => real name
2813 */
2814 function getSpecialPageAliases() {
2815 // Cache aliases because it may be slow to load them
2816 if ( is_null( $this->mExtendedSpecialPageAliases ) ) {
2817 // Initialise array
2818 $this->mExtendedSpecialPageAliases =
2819 self::$dataCache->getItem( $this->mCode, 'specialPageAliases' );
2820 wfRunHooks( 'LanguageGetSpecialPageAliases',
2821 array( &$this->mExtendedSpecialPageAliases, $this->getCode() ) );
2822 }
2823
2824 return $this->mExtendedSpecialPageAliases;
2825 }
2826
2827 /**
2828 * Italic is unsuitable for some languages
2829 *
2830 * @param $text String: the text to be emphasized.
2831 * @return string
2832 */
2833 function emphasize( $text ) {
2834 return "<em>$text</em>";
2835 }
2836
2837 /**
2838 * Normally we output all numbers in plain en_US style, that is
2839 * 293,291.235 for twohundredninetythreethousand-twohundredninetyone
2840 * point twohundredthirtyfive. However this is not suitable for all
2841 * languages, some such as Pakaran want ੨੯੩,੨੯੫.੨੩੫ and others such as
2842 * Icelandic just want to use commas instead of dots, and dots instead
2843 * of commas like "293.291,235".
2844 *
2845 * An example of this function being called:
2846 * <code>
2847 * wfMsg( 'message', $wgLang->formatNum( $num ) )
2848 * </code>
2849 *
2850 * See LanguageGu.php for the Gujarati implementation and
2851 * $separatorTransformTable on MessageIs.php for
2852 * the , => . and . => , implementation.
2853 *
2854 * @todo check if it's viable to use localeconv() for the decimal
2855 * separator thing.
2856 * @param $number Mixed: the string to be formatted, should be an integer
2857 * or a floating point number.
2858 * @param $nocommafy Bool: set to true for special numbers like dates
2859 * @return string
2860 */
2861 public function formatNum( $number, $nocommafy = false ) {
2862 global $wgTranslateNumerals;
2863 if ( !$nocommafy ) {
2864 $number = $this->commafy( $number );
2865 $s = $this->separatorTransformTable();
2866 if ( $s ) {
2867 $number = strtr( $number, $s );
2868 }
2869 }
2870
2871 if ( $wgTranslateNumerals ) {
2872 $s = $this->digitTransformTable();
2873 if ( $s ) {
2874 $number = strtr( $number, $s );
2875 }
2876 }
2877
2878 return $number;
2879 }
2880
2881 /**
2882 * @param $number string
2883 * @return string
2884 */
2885 function parseFormattedNumber( $number ) {
2886 $s = $this->digitTransformTable();
2887 if ( $s ) {
2888 $number = strtr( $number, array_flip( $s ) );
2889 }
2890
2891 $s = $this->separatorTransformTable();
2892 if ( $s ) {
2893 $number = strtr( $number, array_flip( $s ) );
2894 }
2895
2896 $number = strtr( $number, array( ',' => '' ) );
2897 return $number;
2898 }
2899
2900 /**
2901 * Adds commas to a given number
2902 * @since 1.19
2903 * @param $_ mixed
2904 * @return string
2905 */
2906 function commafy( $_ ) {
2907 $digitGroupingPattern = $this->digitGroupingPattern();
2908 if ( $_ === null ) {
2909 return '';
2910 }
2911
2912 if ( !$digitGroupingPattern || $digitGroupingPattern === "###,###,###" ) {
2913 // default grouping is at thousands, use the same for ###,###,### pattern too.
2914 return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,', strrev( $_ ) ) );
2915 } else {
2916 // Ref: http://cldr.unicode.org/translation/number-patterns
2917 $sign = "";
2918 if ( intval( $_ ) < 0 ) {
2919 // For negative numbers apply the algorithm like positive number and add sign.
2920 $sign = "-";
2921 $_ = substr( $_, 1 );
2922 }
2923 $numberpart = array();
2924 $decimalpart = array();
2925 $numMatches = preg_match_all( "/(#+)/", $digitGroupingPattern, $matches );
2926 preg_match( "/\d+/", $_, $numberpart );
2927 preg_match( "/\.\d*/", $_, $decimalpart );
2928 $groupedNumber = ( count( $decimalpart ) > 0 ) ? $decimalpart[0]:"";
2929 if ( $groupedNumber === $_ ) {
2930 // the string does not have any number part. Eg: .12345
2931 return $sign . $groupedNumber;
2932 }
2933 $start = $end = strlen( $numberpart[0] );
2934 while ( $start > 0 ) {
2935 $match = $matches[0][$numMatches -1] ;
2936 $matchLen = strlen( $match );
2937 $start = $end - $matchLen;
2938 if ( $start < 0 ) {
2939 $start = 0;
2940 }
2941 $groupedNumber = substr( $_ , $start, $end -$start ) . $groupedNumber ;
2942 $end = $start;
2943 if ( $numMatches > 1 ) {
2944 // use the last pattern for the rest of the number
2945 $numMatches--;
2946 }
2947 if ( $start > 0 ) {
2948 $groupedNumber = "," . $groupedNumber;
2949 }
2950 }
2951 return $sign . $groupedNumber;
2952 }
2953 }
2954 /**
2955 * @return String
2956 */
2957 function digitGroupingPattern() {
2958 return self::$dataCache->getItem( $this->mCode, 'digitGroupingPattern' );
2959 }
2960
2961 /**
2962 * @return array
2963 */
2964 function digitTransformTable() {
2965 return self::$dataCache->getItem( $this->mCode, 'digitTransformTable' );
2966 }
2967
2968 /**
2969 * @return array
2970 */
2971 function separatorTransformTable() {
2972 return self::$dataCache->getItem( $this->mCode, 'separatorTransformTable' );
2973 }
2974
2975 /**
2976 * Take a list of strings and build a locale-friendly comma-separated
2977 * list, using the local comma-separator message.
2978 * The last two strings are chained with an "and".
2979 *
2980 * @param $l Array
2981 * @return string
2982 */
2983 function listToText( array $l ) {
2984 $s = '';
2985 $m = count( $l ) - 1;
2986 if ( $m == 1 ) {
2987 return $l[0] . $this->getMessageFromDB( 'and' ) . $this->getMessageFromDB( 'word-separator' ) . $l[1];
2988 } else {
2989 for ( $i = $m; $i >= 0; $i-- ) {
2990 if ( $i == $m ) {
2991 $s = $l[$i];
2992 } elseif ( $i == $m - 1 ) {
2993 $s = $l[$i] . $this->getMessageFromDB( 'and' ) . $this->getMessageFromDB( 'word-separator' ) . $s;
2994 } else {
2995 $s = $l[$i] . $this->getMessageFromDB( 'comma-separator' ) . $s;
2996 }
2997 }
2998 return $s;
2999 }
3000 }
3001
3002 /**
3003 * Take a list of strings and build a locale-friendly comma-separated
3004 * list, using the local comma-separator message.
3005 * @param $list array of strings to put in a comma list
3006 * @return string
3007 */
3008 function commaList( array $list ) {
3009 return implode(
3010 wfMsgExt(
3011 'comma-separator',
3012 array( 'parsemag', 'escapenoentities', 'language' => $this )
3013 ),
3014 $list
3015 );
3016 }
3017
3018 /**
3019 * Take a list of strings and build a locale-friendly semicolon-separated
3020 * list, using the local semicolon-separator message.
3021 * @param $list array of strings to put in a semicolon list
3022 * @return string
3023 */
3024 function semicolonList( array $list ) {
3025 return implode(
3026 wfMsgExt(
3027 'semicolon-separator',
3028 array( 'parsemag', 'escapenoentities', 'language' => $this )
3029 ),
3030 $list
3031 );
3032 }
3033
3034 /**
3035 * Same as commaList, but separate it with the pipe instead.
3036 * @param $list array of strings to put in a pipe list
3037 * @return string
3038 */
3039 function pipeList( array $list ) {
3040 return implode(
3041 wfMsgExt(
3042 'pipe-separator',
3043 array( 'escapenoentities', 'language' => $this )
3044 ),
3045 $list
3046 );
3047 }
3048
3049 /**
3050 * Truncate a string to a specified length in bytes, appending an optional
3051 * string (e.g. for ellipses)
3052 *
3053 * The database offers limited byte lengths for some columns in the database;
3054 * multi-byte character sets mean we need to ensure that only whole characters
3055 * are included, otherwise broken characters can be passed to the user
3056 *
3057 * If $length is negative, the string will be truncated from the beginning
3058 *
3059 * @param $string String to truncate
3060 * @param $length Int: maximum length (including ellipses)
3061 * @param $ellipsis String to append to the truncated text
3062 * @param $adjustLength Boolean: Subtract length of ellipsis from $length.
3063 * $adjustLength was introduced in 1.18, before that behaved as if false.
3064 * @return string
3065 */
3066 function truncate( $string, $length, $ellipsis = '...', $adjustLength = true ) {
3067 # Use the localized ellipsis character
3068 if ( $ellipsis == '...' ) {
3069 $ellipsis = wfMsgExt( 'ellipsis', array( 'escapenoentities', 'language' => $this ) );
3070 }
3071 # Check if there is no need to truncate
3072 if ( $length == 0 ) {
3073 return $ellipsis; // convention
3074 } elseif ( strlen( $string ) <= abs( $length ) ) {
3075 return $string; // no need to truncate
3076 }
3077 $stringOriginal = $string;
3078 # If ellipsis length is >= $length then we can't apply $adjustLength
3079 if ( $adjustLength && strlen( $ellipsis ) >= abs( $length ) ) {
3080 $string = $ellipsis; // this can be slightly unexpected
3081 # Otherwise, truncate and add ellipsis...
3082 } else {
3083 $eLength = $adjustLength ? strlen( $ellipsis ) : 0;
3084 if ( $length > 0 ) {
3085 $length -= $eLength;
3086 $string = substr( $string, 0, $length ); // xyz...
3087 $string = $this->removeBadCharLast( $string );
3088 $string = $string . $ellipsis;
3089 } else {
3090 $length += $eLength;
3091 $string = substr( $string, $length ); // ...xyz
3092 $string = $this->removeBadCharFirst( $string );
3093 $string = $ellipsis . $string;
3094 }
3095 }
3096 # Do not truncate if the ellipsis makes the string longer/equal (bug 22181).
3097 # This check is *not* redundant if $adjustLength, due to the single case where
3098 # LEN($ellipsis) > ABS($limit arg); $stringOriginal could be shorter than $string.
3099 if ( strlen( $string ) < strlen( $stringOriginal ) ) {
3100 return $string;
3101 } else {
3102 return $stringOriginal;
3103 }
3104 }
3105
3106 /**
3107 * Remove bytes that represent an incomplete Unicode character
3108 * at the end of string (e.g. bytes of the char are missing)
3109 *
3110 * @param $string String
3111 * @return string
3112 */
3113 protected function removeBadCharLast( $string ) {
3114 if ( $string != '' ) {
3115 $char = ord( $string[strlen( $string ) - 1] );
3116 $m = array();
3117 if ( $char >= 0xc0 ) {
3118 # We got the first byte only of a multibyte char; remove it.
3119 $string = substr( $string, 0, -1 );
3120 } elseif ( $char >= 0x80 &&
3121 preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
3122 '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m ) )
3123 {
3124 # We chopped in the middle of a character; remove it
3125 $string = $m[1];
3126 }
3127 }
3128 return $string;
3129 }
3130
3131 /**
3132 * Remove bytes that represent an incomplete Unicode character
3133 * at the start of string (e.g. bytes of the char are missing)
3134 *
3135 * @param $string String
3136 * @return string
3137 */
3138 protected function removeBadCharFirst( $string ) {
3139 if ( $string != '' ) {
3140 $char = ord( $string[0] );
3141 if ( $char >= 0x80 && $char < 0xc0 ) {
3142 # We chopped in the middle of a character; remove the whole thing
3143 $string = preg_replace( '/^[\x80-\xbf]+/', '', $string );
3144 }
3145 }
3146 return $string;
3147 }
3148
3149 /**
3150 * Truncate a string of valid HTML to a specified length in bytes,
3151 * appending an optional string (e.g. for ellipses), and return valid HTML
3152 *
3153 * This is only intended for styled/linked text, such as HTML with
3154 * tags like <span> and <a>, were the tags are self-contained (valid HTML).
3155 * Also, this will not detect things like "display:none" CSS.
3156 *
3157 * Note: since 1.18 you do not need to leave extra room in $length for ellipses.
3158 *
3159 * @param string $text HTML string to truncate
3160 * @param int $length (zero/positive) Maximum length (including ellipses)
3161 * @param string $ellipsis String to append to the truncated text
3162 * @return string
3163 */
3164 function truncateHtml( $text, $length, $ellipsis = '...' ) {
3165 # Use the localized ellipsis character
3166 if ( $ellipsis == '...' ) {
3167 $ellipsis = wfMsgExt( 'ellipsis', array( 'escapenoentities', 'language' => $this ) );
3168 }
3169 # Check if there is clearly no need to truncate
3170 if ( $length <= 0 ) {
3171 return $ellipsis; // no text shown, nothing to format (convention)
3172 } elseif ( strlen( $text ) <= $length ) {
3173 return $text; // string short enough even *with* HTML (short-circuit)
3174 }
3175
3176 $dispLen = 0; // innerHTML legth so far
3177 $testingEllipsis = false; // checking if ellipses will make string longer/equal?
3178 $tagType = 0; // 0-open, 1-close
3179 $bracketState = 0; // 1-tag start, 2-tag name, 0-neither
3180 $entityState = 0; // 0-not entity, 1-entity
3181 $tag = $ret = ''; // accumulated tag name, accumulated result string
3182 $openTags = array(); // open tag stack
3183 $maybeState = null; // possible truncation state
3184
3185 $textLen = strlen( $text );
3186 $neLength = max( 0, $length - strlen( $ellipsis ) ); // non-ellipsis len if truncated
3187 for ( $pos = 0; true; ++$pos ) {
3188 # Consider truncation once the display length has reached the maximim.
3189 # We check if $dispLen > 0 to grab tags for the $neLength = 0 case.
3190 # Check that we're not in the middle of a bracket/entity...
3191 if ( $dispLen && $dispLen >= $neLength && $bracketState == 0 && !$entityState ) {
3192 if ( !$testingEllipsis ) {
3193 $testingEllipsis = true;
3194 # Save where we are; we will truncate here unless there turn out to
3195 # be so few remaining characters that truncation is not necessary.
3196 if ( !$maybeState ) { // already saved? ($neLength = 0 case)
3197 $maybeState = array( $ret, $openTags ); // save state
3198 }
3199 } elseif ( $dispLen > $length && $dispLen > strlen( $ellipsis ) ) {
3200 # String in fact does need truncation, the truncation point was OK.
3201 list( $ret, $openTags ) = $maybeState; // reload state
3202 $ret = $this->removeBadCharLast( $ret ); // multi-byte char fix
3203 $ret .= $ellipsis; // add ellipsis
3204 break;
3205 }
3206 }
3207 if ( $pos >= $textLen ) break; // extra iteration just for above checks
3208
3209 # Read the next char...
3210 $ch = $text[$pos];
3211 $lastCh = $pos ? $text[$pos - 1] : '';
3212 $ret .= $ch; // add to result string
3213 if ( $ch == '<' ) {
3214 $this->truncate_endBracket( $tag, $tagType, $lastCh, $openTags ); // for bad HTML
3215 $entityState = 0; // for bad HTML
3216 $bracketState = 1; // tag started (checking for backslash)
3217 } elseif ( $ch == '>' ) {
3218 $this->truncate_endBracket( $tag, $tagType, $lastCh, $openTags );
3219 $entityState = 0; // for bad HTML
3220 $bracketState = 0; // out of brackets
3221 } elseif ( $bracketState == 1 ) {
3222 if ( $ch == '/' ) {
3223 $tagType = 1; // close tag (e.g. "</span>")
3224 } else {
3225 $tagType = 0; // open tag (e.g. "<span>")
3226 $tag .= $ch;
3227 }
3228 $bracketState = 2; // building tag name
3229 } elseif ( $bracketState == 2 ) {
3230 if ( $ch != ' ' ) {
3231 $tag .= $ch;
3232 } else {
3233 // Name found (e.g. "<a href=..."), add on tag attributes...
3234 $pos += $this->truncate_skip( $ret, $text, "<>", $pos + 1 );
3235 }
3236 } elseif ( $bracketState == 0 ) {
3237 if ( $entityState ) {
3238 if ( $ch == ';' ) {
3239 $entityState = 0;
3240 $dispLen++; // entity is one displayed char
3241 }
3242 } else {
3243 if ( $neLength == 0 && !$maybeState ) {
3244 // Save state without $ch. We want to *hit* the first
3245 // display char (to get tags) but not *use* it if truncating.
3246 $maybeState = array( substr( $ret, 0, -1 ), $openTags );
3247 }
3248 if ( $ch == '&' ) {
3249 $entityState = 1; // entity found, (e.g. "&#160;")
3250 } else {
3251 $dispLen++; // this char is displayed
3252 // Add the next $max display text chars after this in one swoop...
3253 $max = ( $testingEllipsis ? $length : $neLength ) - $dispLen;
3254 $skipped = $this->truncate_skip( $ret, $text, "<>&", $pos + 1, $max );
3255 $dispLen += $skipped;
3256 $pos += $skipped;
3257 }
3258 }
3259 }
3260 }
3261 // Close the last tag if left unclosed by bad HTML
3262 $this->truncate_endBracket( $tag, $text[$textLen - 1], $tagType, $openTags );
3263 while ( count( $openTags ) > 0 ) {
3264 $ret .= '</' . array_pop( $openTags ) . '>'; // close open tags
3265 }
3266 return $ret;
3267 }
3268
3269 /**
3270 * truncateHtml() helper function
3271 * like strcspn() but adds the skipped chars to $ret
3272 *
3273 * @param $ret
3274 * @param $text
3275 * @param $search
3276 * @param $start
3277 * @param $len
3278 * @return int
3279 */
3280 private function truncate_skip( &$ret, $text, $search, $start, $len = null ) {
3281 if ( $len === null ) {
3282 $len = -1; // -1 means "no limit" for strcspn
3283 } elseif ( $len < 0 ) {
3284 $len = 0; // sanity
3285 }
3286 $skipCount = 0;
3287 if ( $start < strlen( $text ) ) {
3288 $skipCount = strcspn( $text, $search, $start, $len );
3289 $ret .= substr( $text, $start, $skipCount );
3290 }
3291 return $skipCount;
3292 }
3293
3294 /**
3295 * truncateHtml() helper function
3296 * (a) push or pop $tag from $openTags as needed
3297 * (b) clear $tag value
3298 * @param &$tag string Current HTML tag name we are looking at
3299 * @param $tagType int (0-open tag, 1-close tag)
3300 * @param $lastCh string Character before the '>' that ended this tag
3301 * @param &$openTags array Open tag stack (not accounting for $tag)
3302 */
3303 private function truncate_endBracket( &$tag, $tagType, $lastCh, &$openTags ) {
3304 $tag = ltrim( $tag );
3305 if ( $tag != '' ) {
3306 if ( $tagType == 0 && $lastCh != '/' ) {
3307 $openTags[] = $tag; // tag opened (didn't close itself)
3308 } elseif ( $tagType == 1 ) {
3309 if ( $openTags && $tag == $openTags[count( $openTags ) - 1] ) {
3310 array_pop( $openTags ); // tag closed
3311 }
3312 }
3313 $tag = '';
3314 }
3315 }
3316
3317 /**
3318 * Grammatical transformations, needed for inflected languages
3319 * Invoked by putting {{grammar:case|word}} in a message
3320 *
3321 * @param $word string
3322 * @param $case string
3323 * @return string
3324 */
3325 function convertGrammar( $word, $case ) {
3326 global $wgGrammarForms;
3327 if ( isset( $wgGrammarForms[$this->getCode()][$case][$word] ) ) {
3328 return $wgGrammarForms[$this->getCode()][$case][$word];
3329 }
3330 return $word;
3331 }
3332 /**
3333 * Get the grammar forms for the content language
3334 * @return array of grammar forms
3335 * @since 1.20
3336 */
3337 function getGrammarForms() {
3338 global $wgGrammarForms;
3339 if ( isset( $wgGrammarForms[$this->getCode()] ) && is_array( $wgGrammarForms[$this->getCode()] ) ) {
3340 return $wgGrammarForms[$this->getCode()];
3341 }
3342 return array();
3343 }
3344 /**
3345 * Provides an alternative text depending on specified gender.
3346 * Usage {{gender:username|masculine|feminine|neutral}}.
3347 * username is optional, in which case the gender of current user is used,
3348 * but only in (some) interface messages; otherwise default gender is used.
3349 *
3350 * If no forms are given, an empty string is returned. If only one form is
3351 * given, it will be returned unconditionally. These details are implied by
3352 * the caller and cannot be overridden in subclasses.
3353 *
3354 * If more than one form is given, the default is to use the neutral one
3355 * if it is specified, and to use the masculine one otherwise. These
3356 * details can be overridden in subclasses.
3357 *
3358 * @param $gender string
3359 * @param $forms array
3360 *
3361 * @return string
3362 */
3363 function gender( $gender, $forms ) {
3364 if ( !count( $forms ) ) {
3365 return '';
3366 }
3367 $forms = $this->preConvertPlural( $forms, 2 );
3368 if ( $gender === 'male' ) {
3369 return $forms[0];
3370 }
3371 if ( $gender === 'female' ) {
3372 return $forms[1];
3373 }
3374 return isset( $forms[2] ) ? $forms[2] : $forms[0];
3375 }
3376
3377 /**
3378 * Plural form transformations, needed for some languages.
3379 * For example, there are 3 form of plural in Russian and Polish,
3380 * depending on "count mod 10". See [[w:Plural]]
3381 * For English it is pretty simple.
3382 *
3383 * Invoked by putting {{plural:count|wordform1|wordform2}}
3384 * or {{plural:count|wordform1|wordform2|wordform3}}
3385 *
3386 * Example: {{plural:{{NUMBEROFARTICLES}}|article|articles}}
3387 *
3388 * @param $count Integer: non-localized number
3389 * @param $forms Array: different plural forms
3390 * @return string Correct form of plural for $count in this language
3391 */
3392 function convertPlural( $count, $forms ) {
3393 if ( !count( $forms ) ) {
3394 return '';
3395 }
3396 $forms = $this->preConvertPlural( $forms, 2 );
3397
3398 return ( $count == 1 ) ? $forms[0] : $forms[1];
3399 }
3400
3401 /**
3402 * Checks that convertPlural was given an array and pads it to requested
3403 * amount of forms by copying the last one.
3404 *
3405 * @param $count Integer: How many forms should there be at least
3406 * @param $forms Array of forms given to convertPlural
3407 * @return array Padded array of forms or an exception if not an array
3408 */
3409 protected function preConvertPlural( /* Array */ $forms, $count ) {
3410 while ( count( $forms ) < $count ) {
3411 $forms[] = $forms[count( $forms ) - 1];
3412 }
3413 return $forms;
3414 }
3415
3416 /**
3417 * @todo Maybe translate block durations. Note that this function is somewhat misnamed: it
3418 * deals with translating the *duration* ("1 week", "4 days", etc), not the expiry time
3419 * (which is an absolute timestamp). Please note: do NOT add this blindly, as it is used
3420 * on old expiry lengths recorded in log entries. You'd need to provide the start date to
3421 * match up with it.
3422 *
3423 * @param $str String: the validated block duration in English
3424 * @return string Somehow translated block duration
3425 * @see LanguageFi.php for example implementation
3426 */
3427 function translateBlockExpiry( $str ) {
3428 $duration = SpecialBlock::getSuggestedDurations( $this );
3429 foreach ( $duration as $show => $value ) {
3430 if ( strcmp( $str, $value ) == 0 ) {
3431 return htmlspecialchars( trim( $show ) );
3432 }
3433 }
3434
3435 // Since usually only infinite or indefinite is only on list, so try
3436 // equivalents if still here.
3437 $indefs = array( 'infinite', 'infinity', 'indefinite' );
3438 if ( in_array( $str, $indefs ) ) {
3439 foreach ( $indefs as $val ) {
3440 $show = array_search( $val, $duration, true );
3441 if ( $show !== false ) {
3442 return htmlspecialchars( trim( $show ) );
3443 }
3444 }
3445 }
3446 // If all else fails, return the original string.
3447 return $str;
3448 }
3449
3450 /**
3451 * languages like Chinese need to be segmented in order for the diff
3452 * to be of any use
3453 *
3454 * @param $text String
3455 * @return String
3456 */
3457 public function segmentForDiff( $text ) {
3458 return $text;
3459 }
3460
3461 /**
3462 * and unsegment to show the result
3463 *
3464 * @param $text String
3465 * @return String
3466 */
3467 public function unsegmentForDiff( $text ) {
3468 return $text;
3469 }
3470
3471 /**
3472 * Return the LanguageConverter used in the Language
3473 *
3474 * @since 1.19
3475 * @return LanguageConverter
3476 */
3477 public function getConverter() {
3478 return $this->mConverter;
3479 }
3480
3481 /**
3482 * convert text to all supported variants
3483 *
3484 * @param $text string
3485 * @return array
3486 */
3487 public function autoConvertToAllVariants( $text ) {
3488 return $this->mConverter->autoConvertToAllVariants( $text );
3489 }
3490
3491 /**
3492 * convert text to different variants of a language.
3493 *
3494 * @param $text string
3495 * @return string
3496 */
3497 public function convert( $text ) {
3498 return $this->mConverter->convert( $text );
3499 }
3500
3501 /**
3502 * Convert a Title object to a string in the preferred variant
3503 *
3504 * @param $title Title
3505 * @return string
3506 */
3507 public function convertTitle( $title ) {
3508 return $this->mConverter->convertTitle( $title );
3509 }
3510
3511 /**
3512 * Check if this is a language with variants
3513 *
3514 * @return bool
3515 */
3516 public function hasVariants() {
3517 return sizeof( $this->getVariants() ) > 1;
3518 }
3519
3520 /**
3521 * Check if the language has the specific variant
3522 *
3523 * @since 1.19
3524 * @param $variant string
3525 * @return bool
3526 */
3527 public function hasVariant( $variant ) {
3528 return (bool)$this->mConverter->validateVariant( $variant );
3529 }
3530
3531 /**
3532 * Put custom tags (e.g. -{ }-) around math to prevent conversion
3533 *
3534 * @param $text string
3535 * @return string
3536 */
3537 public function armourMath( $text ) {
3538 return $this->mConverter->armourMath( $text );
3539 }
3540
3541 /**
3542 * Perform output conversion on a string, and encode for safe HTML output.
3543 * @param $text String text to be converted
3544 * @param $isTitle Bool whether this conversion is for the article title
3545 * @return string
3546 * @todo this should get integrated somewhere sane
3547 */
3548 public function convertHtml( $text, $isTitle = false ) {
3549 return htmlspecialchars( $this->convert( $text, $isTitle ) );
3550 }
3551
3552 /**
3553 * @param $key string
3554 * @return string
3555 */
3556 public function convertCategoryKey( $key ) {
3557 return $this->mConverter->convertCategoryKey( $key );
3558 }
3559
3560 /**
3561 * Get the list of variants supported by this language
3562 * see sample implementation in LanguageZh.php
3563 *
3564 * @return array an array of language codes
3565 */
3566 public function getVariants() {
3567 return $this->mConverter->getVariants();
3568 }
3569
3570 /**
3571 * @return string
3572 */
3573 public function getPreferredVariant() {
3574 return $this->mConverter->getPreferredVariant();
3575 }
3576
3577 /**
3578 * @return string
3579 */
3580 public function getDefaultVariant() {
3581 return $this->mConverter->getDefaultVariant();
3582 }
3583
3584 /**
3585 * @return string
3586 */
3587 public function getURLVariant() {
3588 return $this->mConverter->getURLVariant();
3589 }
3590
3591 /**
3592 * If a language supports multiple variants, it is
3593 * possible that non-existing link in one variant
3594 * actually exists in another variant. this function
3595 * tries to find it. See e.g. LanguageZh.php
3596 *
3597 * @param $link String: the name of the link
3598 * @param $nt Mixed: the title object of the link
3599 * @param $ignoreOtherCond Boolean: to disable other conditions when
3600 * we need to transclude a template or update a category's link
3601 * @return null the input parameters may be modified upon return
3602 */
3603 public function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
3604 $this->mConverter->findVariantLink( $link, $nt, $ignoreOtherCond );
3605 }
3606
3607 /**
3608 * If a language supports multiple variants, converts text
3609 * into an array of all possible variants of the text:
3610 * 'variant' => text in that variant
3611 *
3612 * @deprecated since 1.17 Use autoConvertToAllVariants()
3613 *
3614 * @param $text string
3615 *
3616 * @return string
3617 */
3618 public function convertLinkToAllVariants( $text ) {
3619 return $this->mConverter->convertLinkToAllVariants( $text );
3620 }
3621
3622 /**
3623 * returns language specific options used by User::getPageRenderHash()
3624 * for example, the preferred language variant
3625 *
3626 * @return string
3627 */
3628 function getExtraHashOptions() {
3629 return $this->mConverter->getExtraHashOptions();
3630 }
3631
3632 /**
3633 * For languages that support multiple variants, the title of an
3634 * article may be displayed differently in different variants. this
3635 * function returns the apporiate title defined in the body of the article.
3636 *
3637 * @return string
3638 */
3639 public function getParsedTitle() {
3640 return $this->mConverter->getParsedTitle();
3641 }
3642
3643 /**
3644 * Enclose a string with the "no conversion" tag. This is used by
3645 * various functions in the Parser
3646 *
3647 * @param $text String: text to be tagged for no conversion
3648 * @param $noParse bool
3649 * @return string the tagged text
3650 */
3651 public function markNoConversion( $text, $noParse = false ) {
3652 return $this->mConverter->markNoConversion( $text, $noParse );
3653 }
3654
3655 /**
3656 * A regular expression to match legal word-trailing characters
3657 * which should be merged onto a link of the form [[foo]]bar.
3658 *
3659 * @return string
3660 */
3661 public function linkTrail() {
3662 return self::$dataCache->getItem( $this->mCode, 'linkTrail' );
3663 }
3664
3665 /**
3666 * @return Language
3667 */
3668 function getLangObj() {
3669 return $this;
3670 }
3671
3672 /**
3673 * Get the RFC 3066 code for this language object
3674 *
3675 * @return string
3676 */
3677 public function getCode() {
3678 return $this->mCode;
3679 }
3680
3681 /**
3682 * Get the code in Bcp47 format which we can use
3683 * inside of html lang="" tags.
3684 * @since 1.19
3685 * @return string
3686 */
3687 public function getHtmlCode() {
3688 if ( is_null( $this->mHtmlCode ) ) {
3689 $this->mHtmlCode = wfBCP47( $this->getCode() );
3690 }
3691 return $this->mHtmlCode;
3692 }
3693
3694 /**
3695 * @param $code string
3696 */
3697 public function setCode( $code ) {
3698 $this->mCode = $code;
3699 // Ensure we don't leave an incorrect html code lying around
3700 $this->mHtmlCode = null;
3701 }
3702
3703 /**
3704 * Get the name of a file for a certain language code
3705 * @param $prefix string Prepend this to the filename
3706 * @param $code string Language code
3707 * @param $suffix string Append this to the filename
3708 * @throws MWException
3709 * @return string $prefix . $mangledCode . $suffix
3710 */
3711 public static function getFileName( $prefix = 'Language', $code, $suffix = '.php' ) {
3712 // Protect against path traversal
3713 if ( !Language::isValidCode( $code )
3714 || strcspn( $code, ":/\\\000" ) !== strlen( $code ) )
3715 {
3716 throw new MWException( "Invalid language code \"$code\"" );
3717 }
3718
3719 return $prefix . str_replace( '-', '_', ucfirst( $code ) ) . $suffix;
3720 }
3721
3722 /**
3723 * Get the language code from a file name. Inverse of getFileName()
3724 * @param $filename string $prefix . $languageCode . $suffix
3725 * @param $prefix string Prefix before the language code
3726 * @param $suffix string Suffix after the language code
3727 * @return string Language code, or false if $prefix or $suffix isn't found
3728 */
3729 public static function getCodeFromFileName( $filename, $prefix = 'Language', $suffix = '.php' ) {
3730 $m = null;
3731 preg_match( '/' . preg_quote( $prefix, '/' ) . '([A-Z][a-z_]+)' .
3732 preg_quote( $suffix, '/' ) . '/', $filename, $m );
3733 if ( !count( $m ) ) {
3734 return false;
3735 }
3736 return str_replace( '_', '-', strtolower( $m[1] ) );
3737 }
3738
3739 /**
3740 * @param $code string
3741 * @return string
3742 */
3743 public static function getMessagesFileName( $code ) {
3744 global $IP;
3745 $file = self::getFileName( "$IP/languages/messages/Messages", $code, '.php' );
3746 wfRunHooks( 'Language::getMessagesFileName', array( $code, &$file ) );
3747 return $file;
3748 }
3749
3750 /**
3751 * @param $code string
3752 * @return string
3753 */
3754 public static function getClassFileName( $code ) {
3755 global $IP;
3756 return self::getFileName( "$IP/languages/classes/Language", $code, '.php' );
3757 }
3758
3759 /**
3760 * Get the first fallback for a given language.
3761 *
3762 * @param $code string
3763 *
3764 * @return bool|string
3765 */
3766 public static function getFallbackFor( $code ) {
3767 if ( $code === 'en' || !Language::isValidBuiltInCode( $code ) ) {
3768 return false;
3769 } else {
3770 $fallbacks = self::getFallbacksFor( $code );
3771 $first = array_shift( $fallbacks );
3772 return $first;
3773 }
3774 }
3775
3776 /**
3777 * Get the ordered list of fallback languages.
3778 *
3779 * @since 1.19
3780 * @param $code string Language code
3781 * @return array
3782 */
3783 public static function getFallbacksFor( $code ) {
3784 if ( $code === 'en' || !Language::isValidBuiltInCode( $code ) ) {
3785 return array();
3786 } else {
3787 $v = self::getLocalisationCache()->getItem( $code, 'fallback' );
3788 $v = array_map( 'trim', explode( ',', $v ) );
3789 if ( $v[count( $v ) - 1] !== 'en' ) {
3790 $v[] = 'en';
3791 }
3792 return $v;
3793 }
3794 }
3795
3796 /**
3797 * Get all messages for a given language
3798 * WARNING: this may take a long time. If you just need all message *keys*
3799 * but need the *contents* of only a few messages, consider using getMessageKeysFor().
3800 *
3801 * @param $code string
3802 *
3803 * @return array
3804 */
3805 public static function getMessagesFor( $code ) {
3806 return self::getLocalisationCache()->getItem( $code, 'messages' );
3807 }
3808
3809 /**
3810 * Get a message for a given language
3811 *
3812 * @param $key string
3813 * @param $code string
3814 *
3815 * @return string
3816 */
3817 public static function getMessageFor( $key, $code ) {
3818 return self::getLocalisationCache()->getSubitem( $code, 'messages', $key );
3819 }
3820
3821 /**
3822 * Get all message keys for a given language. This is a faster alternative to
3823 * array_keys( Language::getMessagesFor( $code ) )
3824 *
3825 * @since 1.19
3826 * @param $code string Language code
3827 * @return array of message keys (strings)
3828 */
3829 public static function getMessageKeysFor( $code ) {
3830 return self::getLocalisationCache()->getSubItemList( $code, 'messages' );
3831 }
3832
3833 /**
3834 * @param $talk
3835 * @return mixed
3836 */
3837 function fixVariableInNamespace( $talk ) {
3838 if ( strpos( $talk, '$1' ) === false ) {
3839 return $talk;
3840 }
3841
3842 global $wgMetaNamespace;
3843 $talk = str_replace( '$1', $wgMetaNamespace, $talk );
3844
3845 # Allow grammar transformations
3846 # Allowing full message-style parsing would make simple requests
3847 # such as action=raw much more expensive than they need to be.
3848 # This will hopefully cover most cases.
3849 $talk = preg_replace_callback( '/{{grammar:(.*?)\|(.*?)}}/i',
3850 array( &$this, 'replaceGrammarInNamespace' ), $talk );
3851 return str_replace( ' ', '_', $talk );
3852 }
3853
3854 /**
3855 * @param $m string
3856 * @return string
3857 */
3858 function replaceGrammarInNamespace( $m ) {
3859 return $this->convertGrammar( trim( $m[2] ), trim( $m[1] ) );
3860 }
3861
3862 /**
3863 * @throws MWException
3864 * @return array
3865 */
3866 static function getCaseMaps() {
3867 static $wikiUpperChars, $wikiLowerChars;
3868 if ( isset( $wikiUpperChars ) ) {
3869 return array( $wikiUpperChars, $wikiLowerChars );
3870 }
3871
3872 wfProfileIn( __METHOD__ );
3873 $arr = wfGetPrecompiledData( 'Utf8Case.ser' );
3874 if ( $arr === false ) {
3875 throw new MWException(
3876 "Utf8Case.ser is missing, please run \"make\" in the serialized directory\n" );
3877 }
3878 $wikiUpperChars = $arr['wikiUpperChars'];
3879 $wikiLowerChars = $arr['wikiLowerChars'];
3880 wfProfileOut( __METHOD__ );
3881 return array( $wikiUpperChars, $wikiLowerChars );
3882 }
3883
3884 /**
3885 * Decode an expiry (block, protection, etc) which has come from the DB
3886 *
3887 * @FIXME: why are we returnings DBMS-dependent strings???
3888 *
3889 * @param $expiry String: Database expiry String
3890 * @param $format Bool|Int true to process using language functions, or TS_ constant
3891 * to return the expiry in a given timestamp
3892 * @return String
3893 */
3894 public function formatExpiry( $expiry, $format = true ) {
3895 static $infinity, $infinityMsg;
3896 if ( $infinity === null ) {
3897 $infinityMsg = wfMessage( 'infiniteblock' );
3898 $infinity = wfGetDB( DB_SLAVE )->getInfinity();
3899 }
3900
3901 if ( $expiry == '' || $expiry == $infinity ) {
3902 return $format === true
3903 ? $infinityMsg
3904 : $infinity;
3905 } else {
3906 return $format === true
3907 ? $this->timeanddate( $expiry, /* User preference timezone */ true )
3908 : wfTimestamp( $format, $expiry );
3909 }
3910 }
3911
3912 /**
3913 * @todo Document
3914 * @param $seconds int|float
3915 * @param $format Array Optional
3916 * If $format['avoid'] == 'avoidseconds' - don't mention seconds if $seconds >= 1 hour
3917 * If $format['avoid'] == 'avoidminutes' - don't mention seconds/minutes if $seconds > 48 hours
3918 * If $format['noabbrevs'] is true - use 'seconds' and friends instead of 'seconds-abbrev' and friends
3919 * For backwards compatibility, $format may also be one of the strings 'avoidseconds' or 'avoidminutes'
3920 * @return string
3921 */
3922 function formatTimePeriod( $seconds, $format = array() ) {
3923 if ( !is_array( $format ) ) {
3924 $format = array( 'avoid' => $format ); // For backwards compatibility
3925 }
3926 if ( !isset( $format['avoid'] ) ) {
3927 $format['avoid'] = false;
3928 }
3929 if ( !isset( $format['noabbrevs' ] ) ) {
3930 $format['noabbrevs'] = false;
3931 }
3932 $secondsMsg = wfMessage(
3933 $format['noabbrevs'] ? 'seconds' : 'seconds-abbrev' )->inLanguage( $this );
3934 $minutesMsg = wfMessage(
3935 $format['noabbrevs'] ? 'minutes' : 'minutes-abbrev' )->inLanguage( $this );
3936 $hoursMsg = wfMessage(
3937 $format['noabbrevs'] ? 'hours' : 'hours-abbrev' )->inLanguage( $this );
3938 $daysMsg = wfMessage(
3939 $format['noabbrevs'] ? 'days' : 'days-abbrev' )->inLanguage( $this );
3940
3941 if ( round( $seconds * 10 ) < 100 ) {
3942 $s = $this->formatNum( sprintf( "%.1f", round( $seconds * 10 ) / 10 ) );
3943 $s = $secondsMsg->params( $s )->text();
3944 } elseif ( round( $seconds ) < 60 ) {
3945 $s = $this->formatNum( round( $seconds ) );
3946 $s = $secondsMsg->params( $s )->text();
3947 } elseif ( round( $seconds ) < 3600 ) {
3948 $minutes = floor( $seconds / 60 );
3949 $secondsPart = round( fmod( $seconds, 60 ) );
3950 if ( $secondsPart == 60 ) {
3951 $secondsPart = 0;
3952 $minutes++;
3953 }
3954 $s = $minutesMsg->params( $this->formatNum( $minutes ) )->text();
3955 $s .= ' ';
3956 $s .= $secondsMsg->params( $this->formatNum( $secondsPart ) )->text();
3957 } elseif ( round( $seconds ) <= 2 * 86400 ) {
3958 $hours = floor( $seconds / 3600 );
3959 $minutes = floor( ( $seconds - $hours * 3600 ) / 60 );
3960 $secondsPart = round( $seconds - $hours * 3600 - $minutes * 60 );
3961 if ( $secondsPart == 60 ) {
3962 $secondsPart = 0;
3963 $minutes++;
3964 }
3965 if ( $minutes == 60 ) {
3966 $minutes = 0;
3967 $hours++;
3968 }
3969 $s = $hoursMsg->params( $this->formatNum( $hours ) )->text();
3970 $s .= ' ';
3971 $s .= $minutesMsg->params( $this->formatNum( $minutes ) )->text();
3972 if ( !in_array( $format['avoid'], array( 'avoidseconds', 'avoidminutes' ) ) ) {
3973 $s .= ' ' . $secondsMsg->params( $this->formatNum( $secondsPart ) )->text();
3974 }
3975 } else {
3976 $days = floor( $seconds / 86400 );
3977 if ( $format['avoid'] === 'avoidminutes' ) {
3978 $hours = round( ( $seconds - $days * 86400 ) / 3600 );
3979 if ( $hours == 24 ) {
3980 $hours = 0;
3981 $days++;
3982 }
3983 $s = $daysMsg->params( $this->formatNum( $days ) )->text();
3984 $s .= ' ';
3985 $s .= $hoursMsg->params( $this->formatNum( $hours ) )->text();
3986 } elseif ( $format['avoid'] === 'avoidseconds' ) {
3987 $hours = floor( ( $seconds - $days * 86400 ) / 3600 );
3988 $minutes = round( ( $seconds - $days * 86400 - $hours * 3600 ) / 60 );
3989 if ( $minutes == 60 ) {
3990 $minutes = 0;
3991 $hours++;
3992 }
3993 if ( $hours == 24 ) {
3994 $hours = 0;
3995 $days++;
3996 }
3997 $s = $daysMsg->params( $this->formatNum( $days ) )->text();
3998 $s .= ' ';
3999 $s .= $hoursMsg->params( $this->formatNum( $hours ) )->text();
4000 $s .= ' ';
4001 $s .= $minutesMsg->params( $this->formatNum( $minutes ) )->text();
4002 } else {
4003 $s = $daysMsg->params( $this->formatNum( $days ) )->text();
4004 $s .= ' ';
4005 $s .= $this->formatTimePeriod( $seconds - $days * 86400, $format );
4006 }
4007 }
4008 return $s;
4009 }
4010
4011 /**
4012 * Format a bitrate for output, using an appropriate
4013 * unit (bps, kbps, Mbps, Gbps, Tbps, Pbps, Ebps, Zbps or Ybps) according to the magnitude in question
4014 *
4015 * This use base 1000. For base 1024 use formatSize(), for another base
4016 * see formatComputingNumbers()
4017 *
4018 * @param $bps int
4019 * @return string
4020 */
4021 function formatBitrate( $bps ) {
4022 return $this->formatComputingNumbers( $bps, 1000, "bitrate-$1bits" );
4023 }
4024
4025 /**
4026 * @param $size int Size of the unit
4027 * @param $boundary int Size boundary (1000, or 1024 in most cases)
4028 * @param $messageKey string Message key to be uesd
4029 * @return string
4030 */
4031 function formatComputingNumbers( $size, $boundary, $messageKey ) {
4032 if ( $size <= 0 ) {
4033 return str_replace( '$1', $this->formatNum( $size ),
4034 $this->getMessageFromDB( str_replace( '$1', '', $messageKey ) )
4035 );
4036 }
4037 $sizes = array( '', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zeta', 'yotta' );
4038 $index = 0;
4039
4040 $maxIndex = count( $sizes ) - 1;
4041 while ( $size >= $boundary && $index < $maxIndex ) {
4042 $index++;
4043 $size /= $boundary;
4044 }
4045
4046 // For small sizes no decimal places necessary
4047 $round = 0;
4048 if ( $index > 1 ) {
4049 // For MB and bigger two decimal places are smarter
4050 $round = 2;
4051 }
4052 $msg = str_replace( '$1', $sizes[$index], $messageKey );
4053
4054 $size = round( $size, $round );
4055 $text = $this->getMessageFromDB( $msg );
4056 return str_replace( '$1', $this->formatNum( $size ), $text );
4057 }
4058
4059 /**
4060 * Format a size in bytes for output, using an appropriate
4061 * unit (B, KB, MB, GB, TB, PB, EB, ZB or YB) according to the magnitude in question
4062 *
4063 * This method use base 1024. For base 1000 use formatBitrate(), for
4064 * another base see formatComputingNumbers()
4065 *
4066 * @param $size int Size to format
4067 * @return string Plain text (not HTML)
4068 */
4069 function formatSize( $size ) {
4070 return $this->formatComputingNumbers( $size, 1024, "size-$1bytes" );
4071 }
4072
4073 /**
4074 * Make a list item, used by various special pages
4075 *
4076 * @param $page String Page link
4077 * @param $details String Text between brackets
4078 * @param $oppositedm Boolean Add the direction mark opposite to your
4079 * language, to display text properly
4080 * @return String
4081 */
4082 function specialList( $page, $details, $oppositedm = true ) {
4083 $dirmark = ( $oppositedm ? $this->getDirMark( true ) : '' ) .
4084 $this->getDirMark();
4085 $details = $details ? $dirmark . $this->getMessageFromDB( 'word-separator' ) .
4086 wfMsgExt( 'parentheses', array( 'escape', 'replaceafter', 'language' => $this ), $details ) : '';
4087 return $page . $details;
4088 }
4089
4090 /**
4091 * Generate (prev x| next x) (20|50|100...) type links for paging
4092 *
4093 * @param $title Title object to link
4094 * @param $offset Integer offset parameter
4095 * @param $limit Integer limit parameter
4096 * @param $query String optional URL query parameter string
4097 * @param $atend Bool optional param for specified if this is the last page
4098 * @return String
4099 */
4100 public function viewPrevNext( Title $title, $offset, $limit, array $query = array(), $atend = false ) {
4101 // @todo FIXME: Why on earth this needs one message for the text and another one for tooltip?
4102
4103 # Make 'previous' link
4104 $prev = wfMessage( 'prevn' )->inLanguage( $this )->title( $title )->numParams( $limit )->text();
4105 if ( $offset > 0 ) {
4106 $plink = $this->numLink( $title, max( $offset - $limit, 0 ), $limit,
4107 $query, $prev, 'prevn-title', 'mw-prevlink' );
4108 } else {
4109 $plink = htmlspecialchars( $prev );
4110 }
4111
4112 # Make 'next' link
4113 $next = wfMessage( 'nextn' )->inLanguage( $this )->title( $title )->numParams( $limit )->text();
4114 if ( $atend ) {
4115 $nlink = htmlspecialchars( $next );
4116 } else {
4117 $nlink = $this->numLink( $title, $offset + $limit, $limit,
4118 $query, $next, 'prevn-title', 'mw-nextlink' );
4119 }
4120
4121 # Make links to set number of items per page
4122 $numLinks = array();
4123 foreach ( array( 20, 50, 100, 250, 500 ) as $num ) {
4124 $numLinks[] = $this->numLink( $title, $offset, $num,
4125 $query, $this->formatNum( $num ), 'shown-title', 'mw-numlink' );
4126 }
4127
4128 return wfMessage( 'viewprevnext' )->inLanguage( $this )->title( $title
4129 )->rawParams( $plink, $nlink, $this->pipeList( $numLinks ) )->escaped();
4130 }
4131
4132 /**
4133 * Helper function for viewPrevNext() that generates links
4134 *
4135 * @param $title Title object to link
4136 * @param $offset Integer offset parameter
4137 * @param $limit Integer limit parameter
4138 * @param $query Array extra query parameters
4139 * @param $link String text to use for the link; will be escaped
4140 * @param $tooltipMsg String name of the message to use as tooltip
4141 * @param $class String value of the "class" attribute of the link
4142 * @return String HTML fragment
4143 */
4144 private function numLink( Title $title, $offset, $limit, array $query, $link, $tooltipMsg, $class ) {
4145 $query = array( 'limit' => $limit, 'offset' => $offset ) + $query;
4146 $tooltip = wfMessage( $tooltipMsg )->inLanguage( $this )->title( $title )->numParams( $limit )->text();
4147 return Html::element( 'a', array( 'href' => $title->getLocalURL( $query ),
4148 'title' => $tooltip, 'class' => $class ), $link );
4149 }
4150
4151 /**
4152 * Get the conversion rule title, if any.
4153 *
4154 * @return string
4155 */
4156 public function getConvRuleTitle() {
4157 return $this->mConverter->getConvRuleTitle();
4158 }
4159 }