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