PageRenderTime 66ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/mediawiki-1.16.5/languages/Language.php

#
PHP | 2653 lines | 1655 code | 256 blank | 742 comment | 390 complexity | 3651c59d9b381223e3b30435bcd19c85 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0, Apache-2.0

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

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

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