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