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