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