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