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