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