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