PageRenderTime 153ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/languages/Language.php

https://github.com/spenser-roark/OOUG-Wiki
PHP | 3990 lines | 2382 code | 275 blank | 1333 comment | 473 complexity | c1c5181dce028b2742c271e2734bb8d3 MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, LGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Internationalisation code
  4. *
  5. * @file
  6. * @ingroup Language
  7. */
  8. /**
  9. * @defgroup Language Language
  10. */
  11. if ( !defined( 'MEDIAWIKI' ) ) {
  12. echo "This file is part of MediaWiki, it is not a valid entry point.\n";
  13. exit( 1 );
  14. }
  15. # Read language names
  16. global $wgLanguageNames;
  17. require_once( dirname( __FILE__ ) . '/Names.php' );
  18. if ( function_exists( 'mb_strtoupper' ) ) {
  19. mb_internal_encoding( 'UTF-8' );
  20. }
  21. /**
  22. * a fake language converter
  23. *
  24. * @ingroup Language
  25. */
  26. class FakeConverter {
  27. var $mLang;
  28. function __construct( $langobj ) { $this->mLang = $langobj; }
  29. function autoConvertToAllVariants( $text ) { return array( $this->mLang->getCode() => $text ); }
  30. function convert( $t ) { return $t; }
  31. function convertTo( $text, $variant ) { return $text; }
  32. function convertTitle( $t ) { return $t->getPrefixedText(); }
  33. function getVariants() { return array( $this->mLang->getCode() ); }
  34. function getPreferredVariant() { return $this->mLang->getCode(); }
  35. function getDefaultVariant() { return $this->mLang->getCode(); }
  36. function getURLVariant() { return ''; }
  37. function getConvRuleTitle() { return false; }
  38. function findVariantLink( &$l, &$n, $ignoreOtherCond = false ) { }
  39. function getExtraHashOptions() { return ''; }
  40. function getParsedTitle() { return ''; }
  41. function markNoConversion( $text, $noParse = false ) { return $text; }
  42. function convertCategoryKey( $key ) { return $key; }
  43. function convertLinkToAllVariants( $text ) { return $this->autoConvertToAllVariants( $text ); }
  44. function armourMath( $text ) { return $text; }
  45. }
  46. /**
  47. * Internationalisation code
  48. * @ingroup Language
  49. */
  50. class Language {
  51. /**
  52. * @var LanguageConverter
  53. */
  54. var $mConverter;
  55. var $mVariants, $mCode, $mLoaded = false;
  56. var $mMagicExtensions = array(), $mMagicHookDone = false;
  57. private $mHtmlCode = null;
  58. var $dateFormatStrings = array();
  59. var $mExtendedSpecialPageAliases;
  60. protected $namespaceNames, $mNamespaceIds, $namespaceAliases;
  61. /**
  62. * ReplacementArray object caches
  63. */
  64. var $transformData = array();
  65. /**
  66. * @var LocalisationCache
  67. */
  68. static public $dataCache;
  69. static public $mLangObjCache = array();
  70. static public $mWeekdayMsgs = array(
  71. 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday',
  72. 'friday', 'saturday'
  73. );
  74. static public $mWeekdayAbbrevMsgs = array(
  75. 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'
  76. );
  77. static public $mMonthMsgs = array(
  78. 'january', 'february', 'march', 'april', 'may_long', 'june',
  79. 'july', 'august', 'september', 'october', 'november',
  80. 'december'
  81. );
  82. static public $mMonthGenMsgs = array(
  83. 'january-gen', 'february-gen', 'march-gen', 'april-gen', 'may-gen', 'june-gen',
  84. 'july-gen', 'august-gen', 'september-gen', 'october-gen', 'november-gen',
  85. 'december-gen'
  86. );
  87. static public $mMonthAbbrevMsgs = array(
  88. 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
  89. 'sep', 'oct', 'nov', 'dec'
  90. );
  91. static public $mIranianCalendarMonthMsgs = array(
  92. 'iranian-calendar-m1', 'iranian-calendar-m2', 'iranian-calendar-m3',
  93. 'iranian-calendar-m4', 'iranian-calendar-m5', 'iranian-calendar-m6',
  94. 'iranian-calendar-m7', 'iranian-calendar-m8', 'iranian-calendar-m9',
  95. 'iranian-calendar-m10', 'iranian-calendar-m11', 'iranian-calendar-m12'
  96. );
  97. static public $mHebrewCalendarMonthMsgs = array(
  98. 'hebrew-calendar-m1', 'hebrew-calendar-m2', 'hebrew-calendar-m3',
  99. 'hebrew-calendar-m4', 'hebrew-calendar-m5', 'hebrew-calendar-m6',
  100. 'hebrew-calendar-m7', 'hebrew-calendar-m8', 'hebrew-calendar-m9',
  101. 'hebrew-calendar-m10', 'hebrew-calendar-m11', 'hebrew-calendar-m12',
  102. 'hebrew-calendar-m6a', 'hebrew-calendar-m6b'
  103. );
  104. static public $mHebrewCalendarMonthGenMsgs = array(
  105. 'hebrew-calendar-m1-gen', 'hebrew-calendar-m2-gen', 'hebrew-calendar-m3-gen',
  106. 'hebrew-calendar-m4-gen', 'hebrew-calendar-m5-gen', 'hebrew-calendar-m6-gen',
  107. 'hebrew-calendar-m7-gen', 'hebrew-calendar-m8-gen', 'hebrew-calendar-m9-gen',
  108. 'hebrew-calendar-m10-gen', 'hebrew-calendar-m11-gen', 'hebrew-calendar-m12-gen',
  109. 'hebrew-calendar-m6a-gen', 'hebrew-calendar-m6b-gen'
  110. );
  111. static public $mHijriCalendarMonthMsgs = array(
  112. 'hijri-calendar-m1', 'hijri-calendar-m2', 'hijri-calendar-m3',
  113. 'hijri-calendar-m4', 'hijri-calendar-m5', 'hijri-calendar-m6',
  114. 'hijri-calendar-m7', 'hijri-calendar-m8', 'hijri-calendar-m9',
  115. 'hijri-calendar-m10', 'hijri-calendar-m11', 'hijri-calendar-m12'
  116. );
  117. /**
  118. * Get a cached language object for a given language code
  119. * @param $code String
  120. * @return Language
  121. */
  122. static function factory( $code ) {
  123. if ( !isset( self::$mLangObjCache[$code] ) ) {
  124. if ( count( self::$mLangObjCache ) > 10 ) {
  125. // Don't keep a billion objects around, that's stupid.
  126. self::$mLangObjCache = array();
  127. }
  128. self::$mLangObjCache[$code] = self::newFromCode( $code );
  129. }
  130. return self::$mLangObjCache[$code];
  131. }
  132. /**
  133. * Create a language object for a given language code
  134. * @param $code String
  135. * @return Language
  136. */
  137. protected static function newFromCode( $code ) {
  138. // Protect against path traversal below
  139. if ( !Language::isValidCode( $code )
  140. || strcspn( $code, ":/\\\000" ) !== strlen( $code ) )
  141. {
  142. throw new MWException( "Invalid language code \"$code\"" );
  143. }
  144. if ( !Language::isValidBuiltInCode( $code ) ) {
  145. // It's not possible to customise this code with class files, so
  146. // just return a Language object. This is to support uselang= hacks.
  147. $lang = new Language;
  148. $lang->setCode( $code );
  149. return $lang;
  150. }
  151. // Check if there is a language class for the code
  152. $class = self::classFromCode( $code );
  153. self::preloadLanguageClass( $class );
  154. if ( MWInit::classExists( $class ) ) {
  155. $lang = new $class;
  156. return $lang;
  157. }
  158. // Keep trying the fallback list until we find an existing class
  159. $fallbacks = Language::getFallbacksFor( $code );
  160. foreach ( $fallbacks as $fallbackCode ) {
  161. if ( !Language::isValidBuiltInCode( $fallbackCode ) ) {
  162. throw new MWException( "Invalid fallback '$fallbackCode' in fallback sequence for '$code'" );
  163. }
  164. $class = self::classFromCode( $fallbackCode );
  165. self::preloadLanguageClass( $class );
  166. if ( MWInit::classExists( $class ) ) {
  167. $lang = Language::newFromCode( $fallbackCode );
  168. $lang->setCode( $code );
  169. return $lang;
  170. }
  171. }
  172. throw new MWException( "Invalid fallback sequence for language '$code'" );
  173. }
  174. /**
  175. * Returns true if a language code string is of a valid form, whether or
  176. * not it exists. This includes codes which are used solely for
  177. * customisation via the MediaWiki namespace.
  178. *
  179. * @param $code string
  180. *
  181. * @return bool
  182. */
  183. public static function isValidCode( $code ) {
  184. return
  185. // People think language codes are html safe, so enforce it.
  186. // Ideally we should only allow a-zA-Z0-9-
  187. // but, .+ and other chars are often used for {{int:}} hacks
  188. // see bugs 37564, 37587, 36938
  189. strcspn( $code, ":/\\\000&<>'\"" ) === strlen( $code )
  190. && !preg_match( Title::getTitleInvalidRegex(), $code );
  191. }
  192. /**
  193. * Returns true if a language code is of a valid form for the purposes of
  194. * internal customisation of MediaWiki, via Messages*.php.
  195. *
  196. * @param $code string
  197. *
  198. * @since 1.18
  199. * @return bool
  200. */
  201. public static function isValidBuiltInCode( $code ) {
  202. return preg_match( '/^[a-z0-9-]+$/i', $code );
  203. }
  204. /**
  205. * @param $code
  206. * @return String Name of the language class
  207. */
  208. public static function classFromCode( $code ) {
  209. if ( $code == 'en' ) {
  210. return 'Language';
  211. } else {
  212. return 'Language' . str_replace( '-', '_', ucfirst( $code ) );
  213. }
  214. }
  215. /**
  216. * Includes language class files
  217. *
  218. * @param $class string Name of the language class
  219. */
  220. public static function preloadLanguageClass( $class ) {
  221. global $IP;
  222. if ( $class === 'Language' ) {
  223. return;
  224. }
  225. if ( !defined( 'MW_COMPILED' ) ) {
  226. // Preload base classes to work around APC/PHP5 bug
  227. if ( file_exists( "$IP/languages/classes/$class.deps.php" ) ) {
  228. include_once( "$IP/languages/classes/$class.deps.php" );
  229. }
  230. if ( file_exists( "$IP/languages/classes/$class.php" ) ) {
  231. include_once( "$IP/languages/classes/$class.php" );
  232. }
  233. }
  234. }
  235. /**
  236. * Get the LocalisationCache instance
  237. *
  238. * @return LocalisationCache
  239. */
  240. public static function getLocalisationCache() {
  241. if ( is_null( self::$dataCache ) ) {
  242. global $wgLocalisationCacheConf;
  243. $class = $wgLocalisationCacheConf['class'];
  244. self::$dataCache = new $class( $wgLocalisationCacheConf );
  245. }
  246. return self::$dataCache;
  247. }
  248. function __construct() {
  249. $this->mConverter = new FakeConverter( $this );
  250. // Set the code to the name of the descendant
  251. if ( get_class( $this ) == 'Language' ) {
  252. $this->mCode = 'en';
  253. } else {
  254. $this->mCode = str_replace( '_', '-', strtolower( substr( get_class( $this ), 8 ) ) );
  255. }
  256. self::getLocalisationCache();
  257. }
  258. /**
  259. * Reduce memory usage
  260. */
  261. function __destruct() {
  262. foreach ( $this as $name => $value ) {
  263. unset( $this->$name );
  264. }
  265. }
  266. /**
  267. * Hook which will be called if this is the content language.
  268. * Descendants can use this to register hook functions or modify globals
  269. */
  270. function initContLang() { }
  271. /**
  272. * Same as getFallbacksFor for current language.
  273. * @return array|bool
  274. * @deprecated in 1.19
  275. */
  276. function getFallbackLanguageCode() {
  277. wfDeprecated( __METHOD__ );
  278. return self::getFallbackFor( $this->mCode );
  279. }
  280. /**
  281. * @return array
  282. * @since 1.19
  283. */
  284. function getFallbackLanguages() {
  285. return self::getFallbacksFor( $this->mCode );
  286. }
  287. /**
  288. * Exports $wgBookstoreListEn
  289. * @return array
  290. */
  291. function getBookstoreList() {
  292. return self::$dataCache->getItem( $this->mCode, 'bookstoreList' );
  293. }
  294. /**
  295. * @return array
  296. */
  297. public function getNamespaces() {
  298. if ( is_null( $this->namespaceNames ) ) {
  299. global $wgMetaNamespace, $wgMetaNamespaceTalk, $wgExtraNamespaces;
  300. $this->namespaceNames = self::$dataCache->getItem( $this->mCode, 'namespaceNames' );
  301. $validNamespaces = MWNamespace::getCanonicalNamespaces();
  302. $this->namespaceNames = $wgExtraNamespaces + $this->namespaceNames + $validNamespaces;
  303. $this->namespaceNames[NS_PROJECT] = $wgMetaNamespace;
  304. if ( $wgMetaNamespaceTalk ) {
  305. $this->namespaceNames[NS_PROJECT_TALK] = $wgMetaNamespaceTalk;
  306. } else {
  307. $talk = $this->namespaceNames[NS_PROJECT_TALK];
  308. $this->namespaceNames[NS_PROJECT_TALK] =
  309. $this->fixVariableInNamespace( $talk );
  310. }
  311. # Sometimes a language will be localised but not actually exist on this wiki.
  312. foreach ( $this->namespaceNames as $key => $text ) {
  313. if ( !isset( $validNamespaces[$key] ) ) {
  314. unset( $this->namespaceNames[$key] );
  315. }
  316. }
  317. # The above mixing may leave namespaces out of canonical order.
  318. # Re-order by namespace ID number...
  319. ksort( $this->namespaceNames );
  320. wfRunHooks( 'LanguageGetNamespaces', array( &$this->namespaceNames ) );
  321. }
  322. return $this->namespaceNames;
  323. }
  324. /**
  325. * Arbitrarily set all of the namespace names at once. Mainly used for testing
  326. * @param $namespaces Array of namespaces (id => name)
  327. */
  328. public function setNamespaces( array $namespaces ) {
  329. $this->namespaceNames = $namespaces;
  330. }
  331. /**
  332. * A convenience function that returns the same thing as
  333. * getNamespaces() except with the array values changed to ' '
  334. * where it found '_', useful for producing output to be displayed
  335. * e.g. in <select> forms.
  336. *
  337. * @return array
  338. */
  339. function getFormattedNamespaces() {
  340. $ns = $this->getNamespaces();
  341. foreach ( $ns as $k => $v ) {
  342. $ns[$k] = strtr( $v, '_', ' ' );
  343. }
  344. return $ns;
  345. }
  346. /**
  347. * Get a namespace value by key
  348. * <code>
  349. * $mw_ns = $wgContLang->getNsText( NS_MEDIAWIKI );
  350. * echo $mw_ns; // prints 'MediaWiki'
  351. * </code>
  352. *
  353. * @param $index Int: the array key of the namespace to return
  354. * @return mixed, string if the namespace value exists, otherwise false
  355. */
  356. function getNsText( $index ) {
  357. $ns = $this->getNamespaces();
  358. return isset( $ns[$index] ) ? $ns[$index] : false;
  359. }
  360. /**
  361. * A convenience function that returns the same thing as
  362. * getNsText() except with '_' changed to ' ', useful for
  363. * producing output.
  364. *
  365. * @param $index string
  366. *
  367. * @return array
  368. */
  369. function getFormattedNsText( $index ) {
  370. $ns = $this->getNsText( $index );
  371. return strtr( $ns, '_', ' ' );
  372. }
  373. /**
  374. * Returns gender-dependent namespace alias if available.
  375. * @param $index Int: namespace index
  376. * @param $gender String: gender key (male, female... )
  377. * @return String
  378. * @since 1.18
  379. */
  380. function getGenderNsText( $index, $gender ) {
  381. global $wgExtraGenderNamespaces;
  382. $ns = $wgExtraGenderNamespaces + self::$dataCache->getItem( $this->mCode, 'namespaceGenderAliases' );
  383. return isset( $ns[$index][$gender] ) ? $ns[$index][$gender] : $this->getNsText( $index );
  384. }
  385. /**
  386. * Whether this language makes distinguishes genders for example in
  387. * namespaces.
  388. * @return bool
  389. * @since 1.18
  390. */
  391. function needsGenderDistinction() {
  392. global $wgExtraGenderNamespaces, $wgExtraNamespaces;
  393. if ( count( $wgExtraGenderNamespaces ) > 0 ) {
  394. // $wgExtraGenderNamespaces overrides everything
  395. return true;
  396. } elseif ( isset( $wgExtraNamespaces[NS_USER] ) && isset( $wgExtraNamespaces[NS_USER_TALK] ) ) {
  397. /// @todo There may be other gender namespace than NS_USER & NS_USER_TALK in the future
  398. // $wgExtraNamespaces overrides any gender aliases specified in i18n files
  399. return false;
  400. } else {
  401. // Check what is in i18n files
  402. $aliases = self::$dataCache->getItem( $this->mCode, 'namespaceGenderAliases' );
  403. return count( $aliases ) > 0;
  404. }
  405. }
  406. /**
  407. * Get a namespace key by value, case insensitive.
  408. * Only matches namespace names for the current language, not the
  409. * canonical ones defined in Namespace.php.
  410. *
  411. * @param $text String
  412. * @return mixed An integer if $text is a valid value otherwise false
  413. */
  414. function getLocalNsIndex( $text ) {
  415. $lctext = $this->lc( $text );
  416. $ids = $this->getNamespaceIds();
  417. return isset( $ids[$lctext] ) ? $ids[$lctext] : false;
  418. }
  419. /**
  420. * @return array
  421. */
  422. function getNamespaceAliases() {
  423. if ( is_null( $this->namespaceAliases ) ) {
  424. $aliases = self::$dataCache->getItem( $this->mCode, 'namespaceAliases' );
  425. if ( !$aliases ) {
  426. $aliases = array();
  427. } else {
  428. foreach ( $aliases as $name => $index ) {
  429. if ( $index === NS_PROJECT_TALK ) {
  430. unset( $aliases[$name] );
  431. $name = $this->fixVariableInNamespace( $name );
  432. $aliases[$name] = $index;
  433. }
  434. }
  435. }
  436. global $wgExtraGenderNamespaces;
  437. $genders = $wgExtraGenderNamespaces + (array)self::$dataCache->getItem( $this->mCode, 'namespaceGenderAliases' );
  438. foreach ( $genders as $index => $forms ) {
  439. foreach ( $forms as $alias ) {
  440. $aliases[$alias] = $index;
  441. }
  442. }
  443. $this->namespaceAliases = $aliases;
  444. }
  445. return $this->namespaceAliases;
  446. }
  447. /**
  448. * @return array
  449. */
  450. function getNamespaceIds() {
  451. if ( is_null( $this->mNamespaceIds ) ) {
  452. global $wgNamespaceAliases;
  453. # Put namespace names and aliases into a hashtable.
  454. # If this is too slow, then we should arrange it so that it is done
  455. # before caching. The catch is that at pre-cache time, the above
  456. # class-specific fixup hasn't been done.
  457. $this->mNamespaceIds = array();
  458. foreach ( $this->getNamespaces() as $index => $name ) {
  459. $this->mNamespaceIds[$this->lc( $name )] = $index;
  460. }
  461. foreach ( $this->getNamespaceAliases() as $name => $index ) {
  462. $this->mNamespaceIds[$this->lc( $name )] = $index;
  463. }
  464. if ( $wgNamespaceAliases ) {
  465. foreach ( $wgNamespaceAliases as $name => $index ) {
  466. $this->mNamespaceIds[$this->lc( $name )] = $index;
  467. }
  468. }
  469. }
  470. return $this->mNamespaceIds;
  471. }
  472. /**
  473. * Get a namespace key by value, case insensitive. Canonical namespace
  474. * names override custom ones defined for the current language.
  475. *
  476. * @param $text String
  477. * @return mixed An integer if $text is a valid value otherwise false
  478. */
  479. function getNsIndex( $text ) {
  480. $lctext = $this->lc( $text );
  481. $ns = MWNamespace::getCanonicalIndex( $lctext );
  482. if ( $ns !== null ) {
  483. return $ns;
  484. }
  485. $ids = $this->getNamespaceIds();
  486. return isset( $ids[$lctext] ) ? $ids[$lctext] : false;
  487. }
  488. /**
  489. * short names for language variants used for language conversion links.
  490. *
  491. * @param $code String
  492. * @param $usemsg bool Use the "variantname-xyz" message if it exists
  493. * @return string
  494. */
  495. function getVariantname( $code, $usemsg = true ) {
  496. $msg = "variantname-$code";
  497. list( $rootCode ) = explode( '-', $code );
  498. if ( $usemsg && wfMessage( $msg )->exists() ) {
  499. return $this->getMessageFromDB( $msg );
  500. }
  501. $name = self::getLanguageName( $code );
  502. if ( $name ) {
  503. return $name; # if it's defined as a language name, show that
  504. } else {
  505. # otherwise, output the language code
  506. return $code;
  507. }
  508. }
  509. /**
  510. * @param $name string
  511. * @return string
  512. */
  513. function specialPage( $name ) {
  514. $aliases = $this->getSpecialPageAliases();
  515. if ( isset( $aliases[$name][0] ) ) {
  516. $name = $aliases[$name][0];
  517. }
  518. return $this->getNsText( NS_SPECIAL ) . ':' . $name;
  519. }
  520. /**
  521. * @return array
  522. */
  523. function getQuickbarSettings() {
  524. return array(
  525. $this->getMessage( 'qbsettings-none' ),
  526. $this->getMessage( 'qbsettings-fixedleft' ),
  527. $this->getMessage( 'qbsettings-fixedright' ),
  528. $this->getMessage( 'qbsettings-floatingleft' ),
  529. $this->getMessage( 'qbsettings-floatingright' ),
  530. $this->getMessage( 'qbsettings-directionality' )
  531. );
  532. }
  533. /**
  534. * @return array
  535. */
  536. function getDatePreferences() {
  537. return self::$dataCache->getItem( $this->mCode, 'datePreferences' );
  538. }
  539. /**
  540. * @return array
  541. */
  542. function getDateFormats() {
  543. return self::$dataCache->getItem( $this->mCode, 'dateFormats' );
  544. }
  545. /**
  546. * @return array|string
  547. */
  548. function getDefaultDateFormat() {
  549. $df = self::$dataCache->getItem( $this->mCode, 'defaultDateFormat' );
  550. if ( $df === 'dmy or mdy' ) {
  551. global $wgAmericanDates;
  552. return $wgAmericanDates ? 'mdy' : 'dmy';
  553. } else {
  554. return $df;
  555. }
  556. }
  557. /**
  558. * @return array
  559. */
  560. function getDatePreferenceMigrationMap() {
  561. return self::$dataCache->getItem( $this->mCode, 'datePreferenceMigrationMap' );
  562. }
  563. /**
  564. * @param $image
  565. * @return array|null
  566. */
  567. function getImageFile( $image ) {
  568. return self::$dataCache->getSubitem( $this->mCode, 'imageFiles', $image );
  569. }
  570. /**
  571. * @return array
  572. */
  573. function getExtraUserToggles() {
  574. return (array)self::$dataCache->getItem( $this->mCode, 'extraUserToggles' );
  575. }
  576. /**
  577. * @param $tog
  578. * @return string
  579. */
  580. function getUserToggle( $tog ) {
  581. return $this->getMessageFromDB( "tog-$tog" );
  582. }
  583. /**
  584. * Get native language names, indexed by code.
  585. * Only those defined in MediaWiki, no other data like CLDR.
  586. * If $customisedOnly is true, only returns codes with a messages file
  587. *
  588. * @param $customisedOnly bool
  589. *
  590. * @return array
  591. */
  592. public static function getLanguageNames( $customisedOnly = false ) {
  593. global $wgExtraLanguageNames;
  594. static $coreLanguageNames;
  595. if ( $coreLanguageNames === null ) {
  596. include( MWInit::compiledPath( 'languages/Names.php' ) );
  597. }
  598. $allNames = $wgExtraLanguageNames + $coreLanguageNames;
  599. if ( !$customisedOnly ) {
  600. return $allNames;
  601. }
  602. $names = array();
  603. // We do this using a foreach over the codes instead of a directory
  604. // loop so that messages files in extensions will work correctly.
  605. foreach ( $allNames as $code => $value ) {
  606. if ( is_readable( self::getMessagesFileName( $code ) ) ) {
  607. $names[$code] = $allNames[$code];
  608. }
  609. }
  610. return $names;
  611. }
  612. /**
  613. * Get translated language names. This is done on best effort and
  614. * by default this is exactly the same as Language::getLanguageNames.
  615. * The CLDR extension provides translated names.
  616. * @param $code String Language code.
  617. * @return Array language code => language name
  618. * @since 1.18.0
  619. */
  620. public static function getTranslatedLanguageNames( $code ) {
  621. $names = array();
  622. wfRunHooks( 'LanguageGetTranslatedLanguageNames', array( &$names, $code ) );
  623. foreach ( self::getLanguageNames() as $code => $name ) {
  624. if ( !isset( $names[$code] ) ) $names[$code] = $name;
  625. }
  626. return $names;
  627. }
  628. /**
  629. * Get a message from the MediaWiki namespace.
  630. *
  631. * @param $msg String: message name
  632. * @return string
  633. */
  634. function getMessageFromDB( $msg ) {
  635. return wfMsgExt( $msg, array( 'parsemag', 'language' => $this ) );
  636. }
  637. /**
  638. * Get the native language name of $code.
  639. * Only if defined in MediaWiki, no other data like CLDR.
  640. * @param $code string
  641. * @return string
  642. */
  643. function getLanguageName( $code ) {
  644. $names = self::getLanguageNames();
  645. if ( !array_key_exists( $code, $names ) ) {
  646. return '';
  647. }
  648. return $names[$code];
  649. }
  650. /**
  651. * @param $key string
  652. * @return string
  653. */
  654. function getMonthName( $key ) {
  655. return $this->getMessageFromDB( self::$mMonthMsgs[$key - 1] );
  656. }
  657. /**
  658. * @return array
  659. */
  660. function getMonthNamesArray() {
  661. $monthNames = array( '' );
  662. for ( $i = 1; $i < 13; $i++ ) {
  663. $monthNames[] = $this->getMonthName( $i );
  664. }
  665. return $monthNames;
  666. }
  667. /**
  668. * @param $key string
  669. * @return string
  670. */
  671. function getMonthNameGen( $key ) {
  672. return $this->getMessageFromDB( self::$mMonthGenMsgs[$key - 1] );
  673. }
  674. /**
  675. * @param $key string
  676. * @return string
  677. */
  678. function getMonthAbbreviation( $key ) {
  679. return $this->getMessageFromDB( self::$mMonthAbbrevMsgs[$key - 1] );
  680. }
  681. /**
  682. * @return array
  683. */
  684. function getMonthAbbreviationsArray() {
  685. $monthNames = array( '' );
  686. for ( $i = 1; $i < 13; $i++ ) {
  687. $monthNames[] = $this->getMonthAbbreviation( $i );
  688. }
  689. return $monthNames;
  690. }
  691. /**
  692. * @param $key string
  693. * @return string
  694. */
  695. function getWeekdayName( $key ) {
  696. return $this->getMessageFromDB( self::$mWeekdayMsgs[$key - 1] );
  697. }
  698. /**
  699. * @param $key string
  700. * @return string
  701. */
  702. function getWeekdayAbbreviation( $key ) {
  703. return $this->getMessageFromDB( self::$mWeekdayAbbrevMsgs[$key - 1] );
  704. }
  705. /**
  706. * @param $key string
  707. * @return string
  708. */
  709. function getIranianCalendarMonthName( $key ) {
  710. return $this->getMessageFromDB( self::$mIranianCalendarMonthMsgs[$key - 1] );
  711. }
  712. /**
  713. * @param $key string
  714. * @return string
  715. */
  716. function getHebrewCalendarMonthName( $key ) {
  717. return $this->getMessageFromDB( self::$mHebrewCalendarMonthMsgs[$key - 1] );
  718. }
  719. /**
  720. * @param $key string
  721. * @return string
  722. */
  723. function getHebrewCalendarMonthNameGen( $key ) {
  724. return $this->getMessageFromDB( self::$mHebrewCalendarMonthGenMsgs[$key - 1] );
  725. }
  726. /**
  727. * @param $key string
  728. * @return string
  729. */
  730. function getHijriCalendarMonthName( $key ) {
  731. return $this->getMessageFromDB( self::$mHijriCalendarMonthMsgs[$key - 1] );
  732. }
  733. /**
  734. * This is a workalike of PHP's date() function, but with better
  735. * internationalisation, a reduced set of format characters, and a better
  736. * escaping format.
  737. *
  738. * Supported format characters are dDjlNwzWFmMntLoYyaAgGhHiscrU. See the
  739. * PHP manual for definitions. There are a number of extensions, which
  740. * start with "x":
  741. *
  742. * xn Do not translate digits of the next numeric format character
  743. * xN Toggle raw digit (xn) flag, stays set until explicitly unset
  744. * xr Use roman numerals for the next numeric format character
  745. * xh Use hebrew numerals for the next numeric format character
  746. * xx Literal x
  747. * xg Genitive month name
  748. *
  749. * xij j (day number) in Iranian calendar
  750. * xiF F (month name) in Iranian calendar
  751. * xin n (month number) in Iranian calendar
  752. * xiy y (two digit year) in Iranian calendar
  753. * xiY Y (full year) in Iranian calendar
  754. *
  755. * xjj j (day number) in Hebrew calendar
  756. * xjF F (month name) in Hebrew calendar
  757. * xjt t (days in month) in Hebrew calendar
  758. * xjx xg (genitive month name) in Hebrew calendar
  759. * xjn n (month number) in Hebrew calendar
  760. * xjY Y (full year) in Hebrew calendar
  761. *
  762. * xmj j (day number) in Hijri calendar
  763. * xmF F (month name) in Hijri calendar
  764. * xmn n (month number) in Hijri calendar
  765. * xmY Y (full year) in Hijri calendar
  766. *
  767. * xkY Y (full year) in Thai solar calendar. Months and days are
  768. * identical to the Gregorian calendar
  769. * xoY Y (full year) in Minguo calendar or Juche year.
  770. * Months and days are identical to the
  771. * Gregorian calendar
  772. * xtY Y (full year) in Japanese nengo. Months and days are
  773. * identical to the Gregorian calendar
  774. *
  775. * Characters enclosed in double quotes will be considered literal (with
  776. * the quotes themselves removed). Unmatched quotes will be considered
  777. * literal quotes. Example:
  778. *
  779. * "The month is" F => The month is January
  780. * i's" => 20'11"
  781. *
  782. * Backslash escaping is also supported.
  783. *
  784. * Input timestamp is assumed to be pre-normalized to the desired local
  785. * time zone, if any.
  786. *
  787. * @param $format String
  788. * @param $ts String: 14-character timestamp
  789. * YYYYMMDDHHMMSS
  790. * 01234567890123
  791. * @todo handling of "o" format character for Iranian, Hebrew, Hijri & Thai?
  792. *
  793. * @return string
  794. */
  795. function sprintfDate( $format, $ts ) {
  796. $s = '';
  797. $raw = false;
  798. $roman = false;
  799. $hebrewNum = false;
  800. $unix = false;
  801. $rawToggle = false;
  802. $iranian = false;
  803. $hebrew = false;
  804. $hijri = false;
  805. $thai = false;
  806. $minguo = false;
  807. $tenno = false;
  808. for ( $p = 0; $p < strlen( $format ); $p++ ) {
  809. $num = false;
  810. $code = $format[$p];
  811. if ( $code == 'x' && $p < strlen( $format ) - 1 ) {
  812. $code .= $format[++$p];
  813. }
  814. if ( ( $code === 'xi' || $code == 'xj' || $code == 'xk' || $code == 'xm' || $code == 'xo' || $code == 'xt' ) && $p < strlen( $format ) - 1 ) {
  815. $code .= $format[++$p];
  816. }
  817. switch ( $code ) {
  818. case 'xx':
  819. $s .= 'x';
  820. break;
  821. case 'xn':
  822. $raw = true;
  823. break;
  824. case 'xN':
  825. $rawToggle = !$rawToggle;
  826. break;
  827. case 'xr':
  828. $roman = true;
  829. break;
  830. case 'xh':
  831. $hebrewNum = true;
  832. break;
  833. case 'xg':
  834. $s .= $this->getMonthNameGen( substr( $ts, 4, 2 ) );
  835. break;
  836. case 'xjx':
  837. if ( !$hebrew ) $hebrew = self::tsToHebrew( $ts );
  838. $s .= $this->getHebrewCalendarMonthNameGen( $hebrew[1] );
  839. break;
  840. case 'd':
  841. $num = substr( $ts, 6, 2 );
  842. break;
  843. case 'D':
  844. if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
  845. $s .= $this->getWeekdayAbbreviation( gmdate( 'w', $unix ) + 1 );
  846. break;
  847. case 'j':
  848. $num = intval( substr( $ts, 6, 2 ) );
  849. break;
  850. case 'xij':
  851. if ( !$iranian ) {
  852. $iranian = self::tsToIranian( $ts );
  853. }
  854. $num = $iranian[2];
  855. break;
  856. case 'xmj':
  857. if ( !$hijri ) {
  858. $hijri = self::tsToHijri( $ts );
  859. }
  860. $num = $hijri[2];
  861. break;
  862. case 'xjj':
  863. if ( !$hebrew ) {
  864. $hebrew = self::tsToHebrew( $ts );
  865. }
  866. $num = $hebrew[2];
  867. break;
  868. case 'l':
  869. if ( !$unix ) {
  870. $unix = wfTimestamp( TS_UNIX, $ts );
  871. }
  872. $s .= $this->getWeekdayName( gmdate( 'w', $unix ) + 1 );
  873. break;
  874. case 'N':
  875. if ( !$unix ) {
  876. $unix = wfTimestamp( TS_UNIX, $ts );
  877. }
  878. $w = gmdate( 'w', $unix );
  879. $num = $w ? $w : 7;
  880. break;
  881. case 'w':
  882. if ( !$unix ) {
  883. $unix = wfTimestamp( TS_UNIX, $ts );
  884. }
  885. $num = gmdate( 'w', $unix );
  886. break;
  887. case 'z':
  888. if ( !$unix ) {
  889. $unix = wfTimestamp( TS_UNIX, $ts );
  890. }
  891. $num = gmdate( 'z', $unix );
  892. break;
  893. case 'W':
  894. if ( !$unix ) {
  895. $unix = wfTimestamp( TS_UNIX, $ts );
  896. }
  897. $num = gmdate( 'W', $unix );
  898. break;
  899. case 'F':
  900. $s .= $this->getMonthName( substr( $ts, 4, 2 ) );
  901. break;
  902. case 'xiF':
  903. if ( !$iranian ) {
  904. $iranian = self::tsToIranian( $ts );
  905. }
  906. $s .= $this->getIranianCalendarMonthName( $iranian[1] );
  907. break;
  908. case 'xmF':
  909. if ( !$hijri ) {
  910. $hijri = self::tsToHijri( $ts );
  911. }
  912. $s .= $this->getHijriCalendarMonthName( $hijri[1] );
  913. break;
  914. case 'xjF':
  915. if ( !$hebrew ) {
  916. $hebrew = self::tsToHebrew( $ts );
  917. }
  918. $s .= $this->getHebrewCalendarMonthName( $hebrew[1] );
  919. break;
  920. case 'm':
  921. $num = substr( $ts, 4, 2 );
  922. break;
  923. case 'M':
  924. $s .= $this->getMonthAbbreviation( substr( $ts, 4, 2 ) );
  925. break;
  926. case 'n':
  927. $num = intval( substr( $ts, 4, 2 ) );
  928. break;
  929. case 'xin':
  930. if ( !$iranian ) {
  931. $iranian = self::tsToIranian( $ts );
  932. }
  933. $num = $iranian[1];
  934. break;
  935. case 'xmn':
  936. if ( !$hijri ) {
  937. $hijri = self::tsToHijri ( $ts );
  938. }
  939. $num = $hijri[1];
  940. break;
  941. case 'xjn':
  942. if ( !$hebrew ) {
  943. $hebrew = self::tsToHebrew( $ts );
  944. }
  945. $num = $hebrew[1];
  946. break;
  947. case 't':
  948. if ( !$unix ) {
  949. $unix = wfTimestamp( TS_UNIX, $ts );
  950. }
  951. $num = gmdate( 't', $unix );
  952. break;
  953. case 'xjt':
  954. if ( !$hebrew ) {
  955. $hebrew = self::tsToHebrew( $ts );
  956. }
  957. $num = $hebrew[3];
  958. break;
  959. case 'L':
  960. if ( !$unix ) {
  961. $unix = wfTimestamp( TS_UNIX, $ts );
  962. }
  963. $num = gmdate( 'L', $unix );
  964. break;
  965. case 'o':
  966. if ( !$unix ) {
  967. $unix = wfTimestamp( TS_UNIX, $ts );
  968. }
  969. $num = gmdate( 'o', $unix );
  970. break;
  971. case 'Y':
  972. $num = substr( $ts, 0, 4 );
  973. break;
  974. case 'xiY':
  975. if ( !$iranian ) {
  976. $iranian = self::tsToIranian( $ts );
  977. }
  978. $num = $iranian[0];
  979. break;
  980. case 'xmY':
  981. if ( !$hijri ) {
  982. $hijri = self::tsToHijri( $ts );
  983. }
  984. $num = $hijri[0];
  985. break;
  986. case 'xjY':
  987. if ( !$hebrew ) {
  988. $hebrew = self::tsToHebrew( $ts );
  989. }
  990. $num = $hebrew[0];
  991. break;
  992. case 'xkY':
  993. if ( !$thai ) {
  994. $thai = self::tsToYear( $ts, 'thai' );
  995. }
  996. $num = $thai[0];
  997. break;
  998. case 'xoY':
  999. if ( !$minguo ) {
  1000. $minguo = self::tsToYear( $ts, 'minguo' );
  1001. }
  1002. $num = $minguo[0];
  1003. break;
  1004. case 'xtY':
  1005. if ( !$tenno ) {
  1006. $tenno = self::tsToYear( $ts, 'tenno' );
  1007. }
  1008. $num = $tenno[0];
  1009. break;
  1010. case 'y':
  1011. $num = substr( $ts, 2, 2 );
  1012. break;
  1013. case 'xiy':
  1014. if ( !$iranian ) {
  1015. $iranian = self::tsToIranian( $ts );
  1016. }
  1017. $num = substr( $iranian[0], -2 );
  1018. break;
  1019. case 'a':
  1020. $s .= intval( substr( $ts, 8, 2 ) ) < 12 ? 'am' : 'pm';
  1021. break;
  1022. case 'A':
  1023. $s .= intval( substr( $ts, 8, 2 ) ) < 12 ? 'AM' : 'PM';
  1024. break;
  1025. case 'g':
  1026. $h = substr( $ts, 8, 2 );
  1027. $num = $h % 12 ? $h % 12 : 12;
  1028. break;
  1029. case 'G':
  1030. $num = intval( substr( $ts, 8, 2 ) );
  1031. break;
  1032. case 'h':
  1033. $h = substr( $ts, 8, 2 );
  1034. $num = sprintf( '%02d', $h % 12 ? $h % 12 : 12 );
  1035. break;
  1036. case 'H':
  1037. $num = substr( $ts, 8, 2 );
  1038. break;
  1039. case 'i':
  1040. $num = substr( $ts, 10, 2 );
  1041. break;
  1042. case 's':
  1043. $num = substr( $ts, 12, 2 );
  1044. break;
  1045. case 'c':
  1046. if ( !$unix ) {
  1047. $unix = wfTimestamp( TS_UNIX, $ts );
  1048. }
  1049. $s .= gmdate( 'c', $unix );
  1050. break;
  1051. case 'r':
  1052. if ( !$unix ) {
  1053. $unix = wfTimestamp( TS_UNIX, $ts );
  1054. }
  1055. $s .= gmdate( 'r', $unix );
  1056. break;
  1057. case 'U':
  1058. if ( !$unix ) {
  1059. $unix = wfTimestamp( TS_UNIX, $ts );
  1060. }
  1061. $num = $unix;
  1062. break;
  1063. case '\\':
  1064. # Backslash escaping
  1065. if ( $p < strlen( $format ) - 1 ) {
  1066. $s .= $format[++$p];
  1067. } else {
  1068. $s .= '\\';
  1069. }
  1070. break;
  1071. case '"':
  1072. # Quoted literal
  1073. if ( $p < strlen( $format ) - 1 ) {
  1074. $endQuote = strpos( $format, '"', $p + 1 );
  1075. if ( $endQuote === false ) {
  1076. # No terminating quote, assume literal "
  1077. $s .= '"';
  1078. } else {
  1079. $s .= substr( $format, $p + 1, $endQuote - $p - 1 );
  1080. $p = $endQuote;
  1081. }
  1082. } else {
  1083. # Quote at end of string, assume literal "
  1084. $s .= '"';
  1085. }
  1086. break;
  1087. default:
  1088. $s .= $format[$p];
  1089. }
  1090. if ( $num !== false ) {
  1091. if ( $rawToggle || $raw ) {
  1092. $s .= $num;
  1093. $raw = false;
  1094. } elseif ( $roman ) {
  1095. $s .= self::romanNumeral( $num );
  1096. $roman = false;
  1097. } elseif ( $hebrewNum ) {
  1098. $s .= self::hebrewNumeral( $num );
  1099. $hebrewNum = false;
  1100. } else {
  1101. $s .= $this->formatNum( $num, true );
  1102. }
  1103. }
  1104. }
  1105. return $s;
  1106. }
  1107. private static $GREG_DAYS = array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
  1108. private static $IRANIAN_DAYS = array( 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29 );
  1109. /**
  1110. * Algorithm by Roozbeh Pournader and Mohammad Toossi to convert
  1111. * Gregorian dates to Iranian dates. Originally written in C, it
  1112. * is released under the terms of GNU Lesser General Public
  1113. * License. Conversion to PHP was performed by Niklas Laxström.
  1114. *
  1115. * Link: http://www.farsiweb.info/jalali/jalali.c
  1116. *
  1117. * @param $ts string
  1118. *
  1119. * @return string
  1120. */
  1121. private static function tsToIranian( $ts ) {
  1122. $gy = substr( $ts, 0, 4 ) -1600;
  1123. $gm = substr( $ts, 4, 2 ) -1;
  1124. $gd = substr( $ts, 6, 2 ) -1;
  1125. # Days passed from the beginning (including leap years)
  1126. $gDayNo = 365 * $gy
  1127. + floor( ( $gy + 3 ) / 4 )
  1128. - floor( ( $gy + 99 ) / 100 )
  1129. + floor( ( $gy + 399 ) / 400 );
  1130. // Add days of the past months of this year
  1131. for ( $i = 0; $i < $gm; $i++ ) {
  1132. $gDayNo += self::$GREG_DAYS[$i];
  1133. }
  1134. // Leap years
  1135. if ( $gm > 1 && ( ( $gy % 4 === 0 && $gy % 100 !== 0 || ( $gy % 400 == 0 ) ) ) ) {
  1136. $gDayNo++;
  1137. }
  1138. // Days passed in current month
  1139. $gDayNo += (int)$gd;
  1140. $jDayNo = $gDayNo - 79;
  1141. $jNp = floor( $jDayNo / 12053 );
  1142. $jDayNo %= 12053;
  1143. $jy = 979 + 33 * $jNp + 4 * floor( $jDayNo / 1461 );
  1144. $jDayNo %= 1461;
  1145. if ( $jDayNo >= 366 ) {
  1146. $jy += floor( ( $jDayNo - 1 ) / 365 );
  1147. $jDayNo = floor( ( $jDayNo - 1 ) % 365 );
  1148. }
  1149. for ( $i = 0; $i < 11 && $jDayNo >= self::$IRANIAN_DAYS[$i]; $i++ ) {
  1150. $jDayNo -= self::$IRANIAN_DAYS[$i];
  1151. }
  1152. $jm = $i + 1;
  1153. $jd = $jDayNo + 1;
  1154. return array( $jy, $jm, $jd );
  1155. }
  1156. /**
  1157. * Converting Gregorian dates to Hijri dates.
  1158. *
  1159. * Based on a PHP-Nuke block by Sharjeel which is released under GNU/GPL license
  1160. *
  1161. * @see http://phpnuke.org/modules.php?name=News&file=article&sid=8234&mode=thread&order=0&thold=0
  1162. *
  1163. * @param $ts string
  1164. *
  1165. * @return string
  1166. */
  1167. private static function tsToHijri( $ts ) {
  1168. $year = substr( $ts, 0, 4 );
  1169. $month = substr( $ts, 4, 2 );
  1170. $day = substr( $ts, 6, 2 );
  1171. $zyr = $year;
  1172. $zd = $day;
  1173. $zm = $month;
  1174. $zy = $zyr;
  1175. if (
  1176. ( $zy > 1582 ) || ( ( $zy == 1582 ) && ( $zm > 10 ) ) ||
  1177. ( ( $zy == 1582 ) && ( $zm == 10 ) && ( $zd > 14 ) )
  1178. )
  1179. {
  1180. $zjd = (int)( ( 1461 * ( $zy + 4800 + (int)( ( $zm - 14 ) / 12 ) ) ) / 4 ) +
  1181. (int)( ( 367 * ( $zm - 2 - 12 * ( (int)( ( $zm - 14 ) / 12 ) ) ) ) / 12 ) -
  1182. (int)( ( 3 * (int)( ( ( $zy + 4900 + (int)( ( $zm - 14 ) / 12 ) ) / 100 ) ) ) / 4 ) +
  1183. $zd - 32075;
  1184. } else {
  1185. $zjd = 367 * $zy - (int)( ( 7 * ( $zy + 5001 + (int)( ( $zm - 9 ) / 7 ) ) ) / 4 ) +
  1186. (int)( ( 275 * $zm ) / 9 ) + $zd + 1729777;
  1187. }
  1188. $zl = $zjd -1948440 + 10632;
  1189. $zn = (int)( ( $zl - 1 ) / 10631 );
  1190. $zl = $zl - 10631 * $zn + 354;
  1191. $zj = ( (int)( ( 10985 - $zl ) / 5316 ) ) * ( (int)( ( 50 * $zl ) / 17719 ) ) + ( (int)( $zl / 5670 ) ) * ( (int)( ( 43 * $zl ) / 15238 ) );
  1192. $zl = $zl - ( (int)( ( 30 - $zj ) / 15 ) ) * ( (int)( ( 17719 * $zj ) / 50 ) ) - ( (int)( $zj / 16 ) ) * ( (int)( ( 15238 * $zj ) / 43 ) ) + 29;
  1193. $zm = (int)( ( 24 * $zl ) / 709 );
  1194. $zd = $zl - (int)( ( 709 * $zm ) / 24 );
  1195. $zy = 30 * $zn + $zj - 30;
  1196. return array( $zy, $zm, $zd );
  1197. }
  1198. /**
  1199. * Converting Gregorian dates to Hebrew dates.
  1200. *
  1201. * Based on a JavaScript code by Abu Mami and Yisrael Hersch
  1202. * (abu-mami@kaluach.net, http://www.kaluach.net), who permitted
  1203. * to translate the relevant functions into PHP and release them under
  1204. * GNU GPL.
  1205. *
  1206. * The months are counted from Tishrei = 1. In a leap year, Adar I is 13
  1207. * and Adar II is 14. In a non-leap year, Adar is 6.
  1208. *
  1209. * @param $ts string
  1210. *
  1211. * @return string
  1212. */
  1213. private static function tsToHebrew( $ts ) {
  1214. # Parse date
  1215. $year = substr( $ts, 0, 4 );
  1216. $month = substr( $ts, 4, 2 );
  1217. $day = substr( $ts, 6, 2 );
  1218. # Calculate Hebrew year
  1219. $hebrewYear = $year + 3760;
  1220. # Month number when September = 1, August = 12
  1221. $month += 4;
  1222. if ( $month > 12 ) {
  1223. # Next year
  1224. $month -= 12;
  1225. $year++;
  1226. $hebrewYear++;
  1227. }
  1228. # Calculate day of year from 1 September
  1229. $dayOfYear = $day;
  1230. for ( $i = 1; $i < $month; $i++ ) {
  1231. if ( $i == 6 ) {
  1232. # February
  1233. $dayOfYear += 28;
  1234. # Check if the year is leap
  1235. if ( $year % 400 == 0 || ( $year % 4 == 0 && $year % 100 > 0 ) ) {
  1236. $dayOfYear++;
  1237. }
  1238. } elseif ( $i == 8 || $i == 10 || $i == 1 || $i == 3 ) {
  1239. $dayOfYear += 30;
  1240. } else {
  1241. $dayOfYear += 31;
  1242. }
  1243. }
  1244. # Calculate the start of the Hebrew year
  1245. $start = self::hebrewYearStart( $hebrewYear );
  1246. # Calculate next year's start
  1247. if ( $dayOfYear <= $start ) {
  1248. # Day is before the start of the year - it is the previous year
  1249. # Next year's start
  1250. $nextStart = $start;
  1251. # Previous year
  1252. $year--;
  1253. $hebrewYear--;
  1254. # Add days since previous year's 1 September
  1255. $dayOfYear += 365;
  1256. if ( ( $year % 400 == 0 ) || ( $year % 100 != 0 && $year % 4 == 0 ) ) {
  1257. # Leap year
  1258. $dayOfYear++;
  1259. }
  1260. # Start of the new (previous) year
  1261. $start = self::hebrewYearStart( $hebrewYear );
  1262. } else {
  1263. # Next year's start
  1264. $nextStart = self::hebrewYearStart( $hebrewYear + 1 );
  1265. }
  1266. # Calculate Hebrew day of year
  1267. $hebrewDayOfYear = $dayOfYear - $start;
  1268. # Difference between year's days
  1269. $diff = $nextStart - $start;
  1270. # Add 12 (or 13 for leap years) days to ignore the difference between
  1271. # Hebrew and Gregorian year (353 at least vs. 365/6) - now the
  1272. # difference is only about the year type
  1273. if ( ( $year % 400 == 0 ) || ( $year % 100 != 0 && $year % 4 == 0 ) ) {
  1274. $diff += 13;
  1275. } else {
  1276. $diff += 12;
  1277. }
  1278. # Check the year pattern, and is leap year
  1279. # 0 means an incomplete year, 1 means a regular year, 2 means a complete year
  1280. # This is mod 30, to work on both leap years (which add 30 days of Adar I)
  1281. # and non-leap years
  1282. $yearPattern = $diff % 30;
  1283. # Check if leap year
  1284. $isLeap = $diff >= 30;
  1285. # Calculate day in the month from number of day in the Hebrew year
  1286. # Don't check Adar - if the day is not in Adar, we will stop before;
  1287. # if it is in Adar, we will use it to check if it is Adar I or Adar II
  1288. $hebrewDay = $hebrewDayOfYear;
  1289. $hebrewMonth = 1;
  1290. $days = 0;
  1291. while ( $hebrewMonth <= 12 ) {
  1292. # Calculate days in this month
  1293. if ( $isLeap && $hebrewMonth == 6 ) {
  1294. # Adar in a leap year
  1295. if ( $isLeap ) {
  1296. # Leap year - has Adar I, with 30 days, and Adar II, with 29 days
  1297. $days = 30;
  1298. if ( $hebrewDay <= $days ) {
  1299. # Day in Adar I
  1300. $hebrewMonth = 13;
  1301. } else {
  1302. # Subtract the days of Adar I
  1303. $hebrewDay -= $days;
  1304. # Try Adar II
  1305. $days = 29;
  1306. if ( $hebrewDay <= $days ) {
  1307. # Day in Adar II
  1308. $hebrewMonth = 14;
  1309. }
  1310. }
  1311. }
  1312. } elseif ( $hebrewMonth == 2 && $yearPattern == 2 ) {
  1313. # Cheshvan in a complete year (otherwise as the rule below)
  1314. $days = 30;
  1315. } elseif ( $hebrewMonth == 3 && $yearPattern == 0 ) {
  1316. # Kislev in an incomplete year (otherwise as the rule below)
  1317. $days = 29;
  1318. } else {
  1319. # Odd months have 30 days, even have 29
  1320. $days = 30 - ( $hebrewMonth - 1 ) % 2;
  1321. }
  1322. if ( $hebrewDay <= $days ) {
  1323. # In the current month
  1324. break;
  1325. } else {
  1326. # Subtract the days of the current month
  1327. $hebrewDay -= $days;
  1328. # Try in the next month
  1329. $hebrewMonth++;
  1330. }
  1331. }
  1332. return array( $hebrewYear, $hebrewMonth, $hebrewDay, $days );
  1333. }
  1334. /**
  1335. * This calculates the Hebrew year start, as days since 1 September.
  1336. * Based on Carl Friedrich Gauss algorithm for finding Easter date.
  1337. * Used for Hebrew date.
  1338. *
  1339. * @param $year int
  1340. *
  1341. * @return string
  1342. */
  1343. private static function hebrewYearStart( $year ) {
  1344. $a = intval( ( 12 * ( $year - 1 ) + 17 ) % 19 );
  1345. $b = intval( ( $year - 1 ) % 4 );
  1346. $m = 32.044093161144 + 1.5542417966212 * $a + $b / 4.0 - 0.0031777940220923 * ( $year - 1 );
  1347. if ( $m < 0 ) {
  1348. $m--;
  1349. }
  1350. $Mar = intval( $m );
  1351. if ( $m < 0 ) {
  1352. $m++;
  1353. }
  1354. $m -= $Mar;
  1355. $c = intval( ( $Mar + 3 * ( $year - 1 ) + 5 * $b + 5 ) % 7 );
  1356. if ( $c == 0 && $a > 11 && $m >= 0.89772376543210 ) {
  1357. $Mar++;
  1358. } elseif ( $c == 1 && $a > 6 && $m >= 0.63287037037037 ) {
  1359. $Mar += 2;
  1360. } elseif ( $c == 2 || $c == 4 || $c == 6 ) {
  1361. $Mar++;
  1362. }
  1363. $Mar += intval( ( $year - 3761 ) / 100 ) - intval( ( $year - 3761 ) / 400 ) - 24;
  1364. return $Mar;
  1365. }
  1366. /**
  1367. * Algorithm to convert Gregorian dates to Thai solar dates,
  1368. * Minguo dates or Minguo dates.
  1369. *
  1370. * Link: http://en.wikipedia.org/wiki/Thai_solar_calendar
  1371. * http://en.wikipedia.org/wiki/Minguo_calendar
  1372. * http://en.wikipedia.org/wiki/Japanese_era_name
  1373. *
  1374. * @param $ts String: 14-character timestamp
  1375. * @param $cName String: calender name
  1376. * @return Array: converted year, month, day
  1377. */
  1378. private static function tsToYear( $ts, $cName ) {
  1379. $gy = substr( $ts, 0, 4 );
  1380. $gm = substr( $ts, 4, 2 );
  1381. $gd = substr( $ts, 6, 2 );
  1382. if ( !strcmp( $cName, 'thai' ) ) {
  1383. # Thai solar dates
  1384. # Add 543 years to the Gregorian calendar
  1385. # Months and days are identical
  1386. $gy_offset = $gy + 543;
  1387. } elseif ( ( !strcmp( $cName, 'minguo' ) ) || !strcmp( $cName, 'juche' ) ) {
  1388. # Minguo dates
  1389. # Deduct 1911 years from the Gregorian calendar
  1390. # Months and days are identical
  1391. $gy_offset = $gy - 1911;
  1392. } elseif ( !strcmp( $cName, 'tenno' ) ) {
  1393. # Nengō dates up to Meiji period
  1394. # Deduct years from the Gregorian calendar
  1395. # depending on the nengo periods
  1396. # Months and days are identical
  1397. if ( ( $gy < 1912 ) || ( ( $gy == 1912 ) && ( $gm < 7 ) ) || ( ( $gy == 1912 ) && ( $gm == 7 ) && ( $gd < 31 ) ) ) {
  1398. # Meiji period
  1399. $gy_gannen = $gy - 1868 + 1;
  1400. $gy_offset = $gy_gannen;
  1401. if ( $gy_gannen == 1 ) {
  1402. $gy_offset = '元';
  1403. }
  1404. $gy_offset = '明治' . $gy_offset;
  1405. } elseif (
  1406. ( ( $gy == 1912 ) && ( $gm == 7 ) && ( $gd == 31 ) ) ||
  1407. ( ( $gy == 1912 ) && ( $gm >= 8 ) ) ||
  1408. ( ( $gy > 1912 ) && ( $gy < 1926 ) ) ||
  1409. ( ( $gy == 1926 ) && ( $gm < 12 ) ) ||
  1410. ( ( $gy == 1926 ) && ( $gm == 12 ) && ( $gd < 26 ) )
  1411. )
  1412. {
  1413. # Taishō period
  1414. $gy_gannen = $gy - 1912 + 1;
  1415. $gy_offset = $gy_gannen;
  1416. if ( $gy_gannen == 1 ) {
  1417. $gy_offset = '元';
  1418. }
  1419. $gy_offset = '大正' . $gy_offset;
  1420. } elseif (
  1421. ( ( $gy == 1926 ) && ( $gm == 12 ) && ( $gd >= 26 ) ) ||
  1422. ( ( $gy > 1926 ) && ( $gy < 1989 ) ) ||
  1423. ( ( $gy == 1989 ) && ( $gm == 1 ) && ( $gd < 8 ) )
  1424. )
  1425. {
  1426. # Shōwa period
  1427. $gy_gannen = $gy - 1926 + 1;
  1428. $gy_offset = $gy_gannen;
  1429. if ( $gy_gannen == 1 ) {
  1430. $gy_offset = '元';
  1431. }
  1432. $gy_offset = '昭和' . $gy_offset;
  1433. } else {
  1434. # Heisei period
  1435. $gy_gannen = $gy - 1989 + 1;
  1436. $gy_offset = $gy_gannen;
  1437. if ( $gy_gannen == 1 ) {
  1438. $gy_offset = '元';
  1439. }
  1440. $gy_offset = '平成' . $gy_offset;
  1441. }
  1442. } else {
  1443. $gy_offset = $gy;
  1444. }
  1445. return array( $gy_offset, $gm, $gd );
  1446. }
  1447. /**
  1448. * Roman number formatting up to 3000
  1449. *
  1450. * @param $num int
  1451. *
  1452. * @return string
  1453. */
  1454. static function romanNumeral( $num ) {
  1455. static $table = array(
  1456. array( '', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X' ),
  1457. array( '', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC', 'C' ),
  1458. array( '', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM', 'M' ),
  1459. array( '', 'M', 'MM', 'MMM' )
  1460. );
  1461. $num = intval( $num );
  1462. if ( $num > 3000 || $num <= 0 ) {
  1463. return $num;
  1464. }
  1465. $s = '';
  1466. for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
  1467. if ( $num >= $pow10 ) {
  1468. $s .= $table[$i][(int)floor( $num / $pow10 )];
  1469. }
  1470. $num = $num % $pow10;
  1471. }
  1472. return $s;
  1473. }
  1474. /**
  1475. * Hebrew Gematria number formatting up to 9999
  1476. *
  1477. * @param $num int
  1478. *
  1479. * @return string
  1480. */
  1481. static function hebrewNumeral( $num ) {
  1482. static $table = array(
  1483. array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' ),
  1484. array( '', 'י', 'כ', 'ל', 'מ', 'נ', 'ס', 'ע', 'פ', 'צ', 'ק' ),
  1485. array( '', 'ק', 'ר', 'ש', 'ת', 'תק', 'תר', 'תש', 'תת', 'תתק', 'תתר' ),
  1486. array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' )
  1487. );
  1488. $num = intval( $num );
  1489. if ( $num > 9999 || $num <= 0 ) {
  1490. return $num;
  1491. }
  1492. $s = '';
  1493. for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
  1494. if ( $num >= $pow10 ) {
  1495. if ( $num == 15 || $num == 16 ) {
  1496. $s .= $table[0][9] . $table[0][$num - 9];
  1497. $num = 0;
  1498. } else {
  1499. $s .= $table[$i][intval( ( $num / $pow10 ) )];
  1500. if ( $pow10 == 1000 ) {
  1501. $s .= "'";
  1502. }
  1503. }
  1504. }
  1505. $num = $num % $pow10;
  1506. }
  1507. if ( strlen( $s ) == 2 ) {
  1508. $str = $s . "'";
  1509. } else {
  1510. $str = substr( $s, 0, strlen( $s ) - 2 ) . '"';
  1511. $str .= substr( $s, strlen( $s ) - 2, 2 );
  1512. }
  1513. $start = substr( $str, 0, strlen( $str ) - 2 );
  1514. $end = substr( $str, strlen( $str ) - 2 );
  1515. switch( $end ) {
  1516. case 'כ':
  1517. $str = $start . 'ך';
  1518. break;
  1519. case 'מ':
  1520. $str = $start . 'ם';
  1521. break;
  1522. case 'נ':
  1523. $str = $start . 'ן';
  1524. break;
  1525. case 'פ':
  1526. $str = $start . 'ף';
  1527. break;
  1528. case 'צ':
  1529. $str = $start . 'ץ';
  1530. break;
  1531. }
  1532. return $str;
  1533. }
  1534. /**
  1535. * Used by date() and time() to adjust the time output.
  1536. *
  1537. * @param $ts Int the time in date('YmdHis') format
  1538. * @param $tz Mixed: adjust the time by this amount (default false, mean we
  1539. * get user timecorrection setting)
  1540. * @return int
  1541. */
  1542. function userAdjust( $ts, $tz = false ) {
  1543. global $wgUser, $wgLocalTZoffset;
  1544. if ( $tz === false ) {
  1545. $tz = $wgUser->getOption( 'timecorrection' );
  1546. }
  1547. $data = explode( '|', $tz, 3 );
  1548. if ( $data[0] == 'ZoneInfo' ) {
  1549. wfSuppressWarnings();
  1550. $userTZ = timezone_open( $data[2] );
  1551. wfRestoreWarnings();
  1552. if ( $userTZ !== false ) {
  1553. $date = date_create( $ts, timezone_open( 'UTC' ) );
  1554. date_timezone_set( $date, $userTZ );
  1555. $date = date_format( $date, 'YmdHis' );
  1556. return $date;
  1557. }
  1558. # Unrecognized timezone, default to 'Offset' with the stored offset.
  1559. $data[0] = 'Offset';
  1560. }
  1561. $minDiff = 0;
  1562. if ( $data[0] == 'System' || $tz == '' ) {
  1563. #  Global offset in minutes.
  1564. if ( isset( $wgLocalTZoffset ) ) {
  1565. $minDiff = $wgLocalTZoffset;
  1566. }
  1567. } elseif ( $data[0] == 'Offset' ) {
  1568. $minDiff = intval( $data[1] );
  1569. } else {
  1570. $data = explode( ':', $tz );
  1571. if ( count( $data ) == 2 ) {
  1572. $data[0] = intval( $data[0] );
  1573. $data[1] = intval( $data[1] );
  1574. $minDiff = abs( $data[0] ) * 60 + $data[1];
  1575. if ( $data[0] < 0 ) {
  1576. $minDiff = -$minDiff;
  1577. }
  1578. } else {
  1579. $minDiff = intval( $data[0] ) * 60;
  1580. }
  1581. }
  1582. # No difference ? Return time unchanged
  1583. if ( 0 == $minDiff ) {
  1584. return $ts;
  1585. }
  1586. wfSuppressWarnings(); // E_STRICT system time bitching
  1587. # Generate an adjusted date; take advantage of the fact that mktime
  1588. # will normalize out-of-range values so we don't have to split $minDiff
  1589. # into hours and minutes.
  1590. $t = mktime( (
  1591. (int)substr( $ts, 8, 2 ) ), # Hours
  1592. (int)substr( $ts, 10, 2 ) + $minDiff, # Minutes
  1593. (int)substr( $ts, 12, 2 ), # Seconds
  1594. (int)substr( $ts, 4, 2 ), # Month
  1595. (int)substr( $ts, 6, 2 ), # Day
  1596. (int)substr( $ts, 0, 4 ) ); # Year
  1597. $date = date( 'YmdHis', $t );
  1598. wfRestoreWarnings();
  1599. return $date;
  1600. }
  1601. /**
  1602. * This is meant to be used by time(), date(), and timeanddate() to get
  1603. * the date preference they're supposed to use, it should be used in
  1604. * all children.
  1605. *
  1606. *<code>
  1607. * function timeanddate([...], $format = true) {
  1608. * $datePreference = $this->dateFormat($format);
  1609. * [...]
  1610. * }
  1611. *</code>
  1612. *
  1613. * @param $usePrefs Mixed: if true, the user's preference is used
  1614. * if false, the site/language default is used
  1615. * if int/string, assumed to be a format.
  1616. * @return string
  1617. */
  1618. function dateFormat( $usePrefs = true ) {
  1619. global $wgUser;
  1620. if ( is_bool( $usePrefs ) ) {
  1621. if ( $usePrefs ) {
  1622. $datePreference = $wgUser->getDatePreference();
  1623. } else {
  1624. $datePreference = (string)User::getDefaultOption( 'date' );
  1625. }
  1626. } else {
  1627. $datePreference = (string)$usePrefs;
  1628. }
  1629. // return int
  1630. if ( $datePreference == '' ) {
  1631. return 'default';
  1632. }
  1633. return $datePreference;
  1634. }
  1635. /**
  1636. * Get a format string for a given type and preference
  1637. * @param $type string May be date, time or both
  1638. * @param $pref string The format name as it appears in Messages*.php
  1639. *
  1640. * @return string
  1641. */
  1642. function getDateFormatString( $type, $pref ) {
  1643. if ( !isset( $this->dateFormatStrings[$type][$pref] ) ) {
  1644. if ( $pref == 'default' ) {
  1645. $pref = $this->getDefaultDateFormat();
  1646. $df = self::$dataCache->getSubitem( $this->mCode, 'dateFormats', "$pref $type" );
  1647. } else {
  1648. $df = self::$dataCache->getSubitem( $this->mCode, 'dateFormats', "$pref $type" );
  1649. if ( is_null( $df ) ) {
  1650. $pref = $this->getDefaultDateFormat();
  1651. $df = self::$dataCache->getSubitem( $this->mCode, 'dateFormats', "$pref $type" );
  1652. }
  1653. }
  1654. $this->dateFormatStrings[$type][$pref] = $df;
  1655. }
  1656. return $this->dateFormatStrings[$type][$pref];
  1657. }
  1658. /**
  1659. * @param $ts Mixed: the time format which needs to be turned into a
  1660. * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
  1661. * @param $adj Bool: whether to adjust the time output according to the
  1662. * user configured offset ($timecorrection)
  1663. * @param $format Mixed: true to use user's date format preference
  1664. * @param $timecorrection String|bool the time offset as returned by
  1665. * validateTimeZone() in Special:Preferences
  1666. * @return string
  1667. */
  1668. function date( $ts, $adj = false, $format = true, $timecorrection = false ) {
  1669. $ts = wfTimestamp( TS_MW, $ts );
  1670. if ( $adj ) {
  1671. $ts = $this->userAdjust( $ts, $timecorrection );
  1672. }
  1673. $df = $this->getDateFormatString( 'date', $this->dateFormat( $format ) );
  1674. return $this->sprintfDate( $df, $ts );
  1675. }
  1676. /**
  1677. * @param $ts Mixed: the time format wh…

Large files files are truncated, but you can click here to view the full file