PageRenderTime 75ms 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
  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 such changes here.
  1559. *
  1560. * @param $string String
  1561. * @return String
  1562. */
  1563. function normalizeForSearch( $string ) {
  1564. return $string;
  1565. }
  1566. /**
  1567. * convert double-width roman characters to single-width.
  1568. * range: ff00-ff5f ~= 0020-007f
  1569. */
  1570. protected static function convertDoubleWidth( $string ) {
  1571. $string = preg_replace( '/\xef\xbc([\x80-\xbf])/e', 'chr((ord("$1") & 0x3f) + 0x20)', $string );
  1572. $string = preg_replace( '/\xef\xbd([\x80-\x99])/e', 'chr((ord("$1") & 0x3f) + 0x60)', $string );
  1573. return $string;
  1574. }
  1575. protected static function insertSpace( $string, $pattern ) {
  1576. $string = preg_replace( $pattern, " $1 ", $string );
  1577. $string = preg_replace( '/ +/', ' ', $string );
  1578. return $string;
  1579. }
  1580. function convertForSearchResult( $termsArray ) {
  1581. # some languages, e.g. Chinese, need to do a conversion
  1582. # in order for search results to be displayed correctly
  1583. return $termsArray;
  1584. }
  1585. /**
  1586. * Get the first character of a string.
  1587. *
  1588. * @param $s string
  1589. * @return string
  1590. */
  1591. function firstChar( $s ) {
  1592. $matches = array();
  1593. preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
  1594. '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})/', $s, $matches);
  1595. if ( isset( $matches[1] ) ) {
  1596. if ( strlen( $matches[1] ) != 3 ) {
  1597. return $matches[1];
  1598. }
  1599. // Break down Hangul syllables to grab the first jamo
  1600. $code = utf8ToCodepoint( $matches[1] );
  1601. if ( $code < 0xac00 || 0xd7a4 <= $code) {
  1602. return $matches[1];
  1603. } elseif ( $code < 0xb098 ) {
  1604. return "\xe3\x84\xb1";
  1605. } elseif ( $code < 0xb2e4 ) {
  1606. return "\xe3\x84\xb4";
  1607. } elseif ( $code < 0xb77c ) {
  1608. return "\xe3\x84\xb7";
  1609. } elseif ( $code < 0xb9c8 ) {
  1610. return "\xe3\x84\xb9";
  1611. } elseif ( $code < 0xbc14 ) {
  1612. return "\xe3\x85\x81";
  1613. } elseif ( $code < 0xc0ac ) {
  1614. return "\xe3\x85\x82";
  1615. } elseif ( $code < 0xc544 ) {
  1616. return "\xe3\x85\x85";
  1617. } elseif ( $code < 0xc790 ) {
  1618. return "\xe3\x85\x87";
  1619. } elseif ( $code < 0xcc28 ) {
  1620. return "\xe3\x85\x88";
  1621. } elseif ( $code < 0xce74 ) {
  1622. return "\xe3\x85\x8a";
  1623. } elseif ( $code < 0xd0c0 ) {
  1624. return "\xe3\x85\x8b";
  1625. } elseif ( $code < 0xd30c ) {
  1626. return "\xe3\x85\x8c";
  1627. } elseif ( $code < 0xd558 ) {
  1628. return "\xe3\x85\x8d";
  1629. } else {
  1630. return "\xe3\x85\x8e";
  1631. }
  1632. } else {
  1633. return "";
  1634. }
  1635. }
  1636. function initEncoding() {
  1637. # Some languages may have an alternate char encoding option
  1638. # (Esperanto X-coding, Japanese furigana conversion, etc)
  1639. # If this language is used as the primary content language,
  1640. # an override to the defaults can be set here on startup.
  1641. }
  1642. function recodeForEdit( $s ) {
  1643. # For some languages we'll want to explicitly specify
  1644. # which characters make it into the edit box raw
  1645. # or are converted in some way or another.
  1646. # Note that if wgOutputEncoding is different from
  1647. # wgInputEncoding, this text will be further converted
  1648. # to wgOutputEncoding.
  1649. global $wgEditEncoding;
  1650. if( $wgEditEncoding == '' or
  1651. $wgEditEncoding == 'UTF-8' ) {
  1652. return $s;
  1653. } else {
  1654. return $this->iconv( 'UTF-8', $wgEditEncoding, $s );
  1655. }
  1656. }
  1657. function recodeInput( $s ) {
  1658. # Take the previous into account.
  1659. global $wgEditEncoding;
  1660. if($wgEditEncoding != "") {
  1661. $enc = $wgEditEncoding;
  1662. } else {
  1663. $enc = 'UTF-8';
  1664. }
  1665. if( $enc == 'UTF-8' ) {
  1666. return $s;
  1667. } else {
  1668. return $this->iconv( $enc, 'UTF-8', $s );
  1669. }
  1670. }
  1671. /**
  1672. * Convert a UTF-8 string to normal form C. In Malayalam and Arabic, this
  1673. * also cleans up certain backwards-compatible sequences, converting them
  1674. * to the modern Unicode equivalent.
  1675. *
  1676. * This is language-specific for performance reasons only.
  1677. */
  1678. function normalize( $s ) {
  1679. return UtfNormal::cleanUp( $s );
  1680. }
  1681. /**
  1682. * Transform a string using serialized data stored in the given file (which
  1683. * must be in the serialized subdirectory of $IP). The file contains pairs
  1684. * mapping source characters to destination characters.
  1685. *
  1686. * The data is cached in process memory. This will go faster if you have the
  1687. * FastStringSearch extension.
  1688. */
  1689. function transformUsingPairFile( $file, $string ) {
  1690. if ( !isset( $this->transformData[$file] ) ) {
  1691. $data = wfGetPrecompiledData( $file );
  1692. if ( $data === false ) {
  1693. throw new MWException( __METHOD__.": The transformation file $file is missing" );
  1694. }
  1695. $this->transformData[$file] = new ReplacementArray( $data );
  1696. }
  1697. return $this->transformData[$file]->replace( $string );
  1698. }
  1699. /**
  1700. * For right-to-left language support
  1701. *
  1702. * @return bool
  1703. */
  1704. function isRTL() {
  1705. return self::$dataCache->getItem( $this->mCode, 'rtl' );
  1706. }
  1707. /**
  1708. * Return the correct HTML 'dir' attribute value for this language.
  1709. * @return String
  1710. */
  1711. function getDir() {
  1712. return $this->isRTL() ? 'rtl' : 'ltr';
  1713. }
  1714. /**
  1715. * Return 'left' or 'right' as appropriate alignment for line-start
  1716. * for this language's text direction.
  1717. *
  1718. * Should be equivalent to CSS3 'start' text-align value....
  1719. *
  1720. * @return String
  1721. */
  1722. function alignStart() {
  1723. return $this->isRTL() ? 'right' : 'left';
  1724. }
  1725. /**
  1726. * Return 'right' or 'left' as appropriate alignment for line-end
  1727. * for this language's text direction.
  1728. *
  1729. * Should be equivalent to CSS3 'end' text-align value....
  1730. *
  1731. * @return String
  1732. */
  1733. function alignEnd() {
  1734. return $this->isRTL() ? 'left' : 'right';
  1735. }
  1736. /**
  1737. * A hidden direction mark (LRM or RLM), depending on the language direction
  1738. *
  1739. * @return string
  1740. */
  1741. function getDirMark() {
  1742. return $this->isRTL() ? "\xE2\x80\x8F" : "\xE2\x80\x8E";
  1743. }
  1744. function capitalizeAllNouns() {
  1745. return self::$dataCache->getItem( $this->mCode, 'capitalizeAllNouns' );
  1746. }
  1747. /**
  1748. * An arrow, depending on the language direction
  1749. *
  1750. * @return string
  1751. */
  1752. function getArrow() {
  1753. return $this->isRTL() ? '?' : '?';
  1754. }
  1755. /**
  1756. * To allow "foo[[bar]]" to extend the link over the whole word "foobar"
  1757. *
  1758. * @return bool
  1759. */
  1760. function linkPrefixExtension() {
  1761. return self::$dataCache->getItem( $this->mCode, 'linkPrefixExtension' );
  1762. }
  1763. function getMagicWords() {
  1764. return self::$dataCache->getItem( $this->mCode, 'magicWords' );
  1765. }
  1766. # Fill a MagicWord object with data from here
  1767. function getMagic( $mw ) {
  1768. if ( !$this->mMagicHookDone ) {
  1769. $this->mMagicHookDone = true;
  1770. wfProfileIn( 'LanguageGetMagic' );
  1771. wfRunHooks( 'LanguageGetMagic', array( &$this->mMagicExtensions, $this->getCode() ) );
  1772. wfProfileOut( 'LanguageGetMagic' );
  1773. }
  1774. if ( isset( $this->mMagicExtensions[$mw->mId] ) ) {
  1775. $rawEntry = $this->mMagicExtensions[$mw->mId];
  1776. } else {
  1777. $magicWords = $this->getMagicWords();
  1778. if ( isset( $magicWords[$mw->mId] ) ) {
  1779. $rawEntry = $magicWords[$mw->mId];
  1780. } else {
  1781. $rawEntry = false;
  1782. }
  1783. }
  1784. if( !is_array( $rawEntry ) ) {
  1785. error_log( "\"$rawEntry\" is not a valid magic thingie for \"$mw->mId\"" );
  1786. } else {
  1787. $mw->mCaseSensitive = $rawEntry[0];
  1788. $mw->mSynonyms = array_slice( $rawEntry, 1 );
  1789. }
  1790. }
  1791. /**
  1792. * Add magic words to the extension array
  1793. */
  1794. function addMagicWordsByLang( $newWords ) {
  1795. $code = $this->getCode();
  1796. $fallbackChain = array();
  1797. while ( $code && !in_array( $code, $fallbackChain ) ) {
  1798. $fallbackChain[] = $code;
  1799. $code = self::getFallbackFor( $code );
  1800. }
  1801. if ( !in_array( 'en', $fallbackChain ) ) {
  1802. $fallbackChain[] = 'en';
  1803. }
  1804. $fallbackChain = array_reverse( $fallbackChain );
  1805. foreach ( $fallbackChain as $code ) {
  1806. if ( isset( $newWords[$code] ) ) {
  1807. $this->mMagicExtensions = $newWords[$code] + $this->mMagicExtensions;
  1808. }
  1809. }
  1810. }
  1811. /**
  1812. * Get special page names, as an associative array
  1813. * case folded alias => real name
  1814. */
  1815. function getSpecialPageAliases() {
  1816. // Cache aliases because it may be slow to load them
  1817. if ( is_null( $this->mExtendedSpecialPageAliases ) ) {
  1818. // Initialise array
  1819. $this->mExtendedSpecialPageAliases =
  1820. self::$dataCache->getItem( $this->mCode, 'specialPageAliases' );
  1821. wfRunHooks( 'LanguageGetSpecialPageAliases',
  1822. array( &$this->mExtendedSpecialPageAliases, $this->getCode() ) );
  1823. }
  1824. return $this->mExtendedSpecialPageAliases;
  1825. }
  1826. /**
  1827. * Italic is unsuitable for some languages
  1828. *
  1829. * @param $text String: the text to be emphasized.
  1830. * @return string
  1831. */
  1832. function emphasize( $text ) {
  1833. return "<em>$text</em>";
  1834. }
  1835. /**
  1836. * Normally we output all numbers in plain en_US style, that is
  1837. * 293,291.235 for twohundredninetythreethousand-twohundredninetyone
  1838. * point twohundredthirtyfive. However this is not sutable for all
  1839. * languages, some such as Pakaran want ???,???.??? and others such as
  1840. * Icelandic just want to use commas instead of dots, and dots instead
  1841. * of commas like "293.291,235".
  1842. *
  1843. * An example of this function being called:
  1844. * <code>
  1845. * wfMsg( 'message', $wgLang->formatNum( $num ) )
  1846. * </code>
  1847. *
  1848. * See LanguageGu.php for the Gujarati implementation and
  1849. * $separatorTransformTable on MessageIs.php for
  1850. * the , => . and . => , implementation.
  1851. *
  1852. * @todo check if it's viable to use localeconv() for the decimal
  1853. * separator thing.
  1854. * @param $number Mixed: the string to be formatted, should be an integer
  1855. * or a floating point number.
  1856. * @param $nocommafy Bool: set to true for special numbers like dates
  1857. * @return string
  1858. */
  1859. function formatNum( $number, $nocommafy = false ) {
  1860. global $wgTranslateNumerals;
  1861. if (!$nocommafy) {
  1862. $number = $this->commafy($number);
  1863. $s = $this->separatorTransformTable();
  1864. if ($s) { $number = strtr($number, $s); }
  1865. }
  1866. if ($wgTranslateNumerals) {
  1867. $s = $this->digitTransformTable();
  1868. if ($s) { $number = strtr($number, $s); }
  1869. }
  1870. return $number;
  1871. }
  1872. function parseFormattedNumber( $number ) {
  1873. $s = $this->digitTransformTable();
  1874. if ($s) { $number = strtr($number, array_flip($s)); }
  1875. $s = $this->separatorTransformTable();
  1876. if ($s) { $number = strtr($number, array_flip($s)); }
  1877. $number = strtr( $number, array (',' => '') );
  1878. return $number;
  1879. }
  1880. /**
  1881. * Adds commas to a given number
  1882. *
  1883. * @param $_ mixed
  1884. * @return string
  1885. */
  1886. function commafy($_) {
  1887. return strrev((string)preg_replace('/(\d{3})(?=\d)(?!\d*\.)/','$1,',strrev($_)));
  1888. }
  1889. function digitTransformTable() {
  1890. return self::$dataCache->getItem( $this->mCode, 'digitTransformTable' );
  1891. }
  1892. function separatorTransformTable() {
  1893. return self::$dataCache->getItem( $this->mCode, 'separatorTransformTable' );
  1894. }
  1895. /**
  1896. * Take a list of strings and build a locale-friendly comma-separated
  1897. * list, using the local comma-separator message.
  1898. * The last two strings are chained with an "and".
  1899. *
  1900. * @param $l Array
  1901. * @return string
  1902. */
  1903. function listToText( $l ) {
  1904. $s = '';
  1905. $m = count( $l ) - 1;
  1906. if( $m == 1 ) {
  1907. return $l[0] . $this->getMessageFromDB( 'and' ) . $this->getMessageFromDB( 'word-separator' ) . $l[1];
  1908. }
  1909. else {
  1910. for ( $i = $m; $i >= 0; $i-- ) {
  1911. if ( $i == $m ) {
  1912. $s = $l[$i];
  1913. } else if( $i == $m - 1 ) {
  1914. $s = $l[$i] . $this->getMessageFromDB( 'and' ) . $this->getMessageFromDB( 'word-separator' ) . $s;
  1915. } else {
  1916. $s = $l[$i] . $this->getMessageFromDB( 'comma-separator' ) . $s;
  1917. }
  1918. }
  1919. return $s;
  1920. }
  1921. }
  1922. /**
  1923. * Take a list of strings and build a locale-friendly comma-separated
  1924. * list, using the local comma-separator message.
  1925. * @param $list array of strings to put in a comma list
  1926. * @return string
  1927. */
  1928. function commaList( $list ) {
  1929. return implode(
  1930. $list,
  1931. wfMsgExt( 'comma-separator', array( 'parsemag', 'escapenoentities', 'language' => $this ) ) );
  1932. }
  1933. /**
  1934. * Take a list of strings and build a locale-friendly semicolon-separated
  1935. * list, using the local semicolon-separator message.
  1936. * @param $list array of strings to put in a semicolon list
  1937. * @return string
  1938. */
  1939. function semicolonList( $list ) {
  1940. return implode(
  1941. $list,
  1942. wfMsgExt( 'semicolon-separator', array( 'parsemag', 'escapenoentities', 'language' => $this ) ) );
  1943. }
  1944. /**
  1945. * Same as commaList, but separate it with the pipe instead.
  1946. * @param $list array of strings to put in a pipe list
  1947. * @return string
  1948. */
  1949. function pipeList( $list ) {
  1950. return implode(
  1951. $list,
  1952. wfMsgExt( 'pipe-separator', array( 'escapenoentities', 'language' => $this ) ) );
  1953. }
  1954. /**
  1955. * Truncate a string to a specified length in bytes, appending an optional
  1956. * string (e.g. for ellipses)
  1957. *
  1958. * The database offers limited byte lengths for some columns in the database;
  1959. * multi-byte character sets mean we need to ensure that only whole characters
  1960. * are included, otherwise broken characters can be passed to the user
  1961. *
  1962. * If $length is negative, the string will be truncated from the beginning
  1963. *
  1964. * @param $string String to truncate
  1965. * @param $length Int: maximum length (excluding ellipses)
  1966. * @param $ellipsis String to append to the truncated text
  1967. * @return string
  1968. */
  1969. function truncate( $string, $length, $ellipsis = '...' ) {
  1970. # Use the localized ellipsis character
  1971. if( $ellipsis == '...' ) {
  1972. $ellipsis = wfMsgExt( 'ellipsis', array( 'escapenoentities', 'language' => $this ) );
  1973. }
  1974. if( $length == 0 ) {
  1975. return $ellipsis;
  1976. }
  1977. if ( strlen( $string ) <= abs( $length ) ) {
  1978. return $string;
  1979. }
  1980. $stringOriginal = $string;
  1981. if( $length > 0 ) {
  1982. $string = substr( $string, 0, $length );
  1983. $char = ord( $string[strlen( $string ) - 1] );
  1984. $m = array();
  1985. if ($char >= 0xc0) {
  1986. # We got the first byte only of a multibyte char; remove it.
  1987. $string = substr( $string, 0, -1 );
  1988. } elseif( $char >= 0x80 &&
  1989. preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
  1990. '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m ) ) {
  1991. # We chopped in the middle of a character; remove it
  1992. $string = $m[1];
  1993. }
  1994. $string = $string . $ellipsis;
  1995. } else {
  1996. $string = substr( $string, $length );
  1997. $char = ord( $string[0] );
  1998. if( $char >= 0x80 && $char < 0xc0 ) {
  1999. # We chopped in the middle of a character; remove the whole thing
  2000. $string = preg_replace( '/^[\x80-\xbf]+/', '', $string );
  2001. }
  2002. $string = $ellipsis . $string;
  2003. }
  2004. # Do not truncate if the ellipsis actually make the string longer. Bug 22181
  2005. if ( strlen( $string ) < strlen( $stringOriginal ) ) {
  2006. return $string;
  2007. } else {
  2008. return $stringOriginal;
  2009. }
  2010. }
  2011. /**
  2012. * Grammatical transformations, needed for inflected languages
  2013. * Invoked by putting {{grammar:case|word}} in a message
  2014. *
  2015. * @param $word string
  2016. * @param $case string
  2017. * @return string
  2018. */
  2019. function convertGrammar( $word, $case ) {
  2020. global $wgGrammarForms;
  2021. if ( isset($wgGrammarForms[$this->getCode()][$case][$word]) ) {
  2022. return $wgGrammarForms[$this->getCode()][$case][$word];
  2023. }
  2024. return $word;
  2025. }
  2026. /**
  2027. * Provides an alternative text depending on specified gender.
  2028. * Usage {{gender:username|masculine|feminine|neutral}}.
  2029. * username is optional, in which case the gender of current user is used,
  2030. * but only in (some) interface messages; otherwise default gender is used.
  2031. * If second or third parameter are not specified, masculine is used.
  2032. * These details may be overriden per language.
  2033. */
  2034. function gender( $gender, $forms ) {
  2035. if ( !count($forms) ) { return ''; }
  2036. $forms = $this->preConvertPlural( $forms, 2 );
  2037. if ( $gender === 'male' ) return $forms[0];
  2038. if ( $gender === 'female' ) return $forms[1];
  2039. return isset($forms[2]) ? $forms[2] : $forms[0];
  2040. }
  2041. /**
  2042. * Plural form transformations, needed for some languages.
  2043. * For example, there are 3 form of plural in Russian and Polish,
  2044. * depending on "count mod 10". See [[w:Plural]]
  2045. * For English it is pretty simple.
  2046. *
  2047. * Invoked by putting {{plural:count|wordform1|wordform2}}
  2048. * or {{plural:count|wordform1|wordform2|wordform3}}
  2049. *
  2050. * Example: {{plural:{{NUMBEROFARTICLES}}|article|articles}}
  2051. *
  2052. * @param $count Integer: non-localized number
  2053. * @param $forms Array: different plural forms
  2054. * @return string Correct form of plural for $count in this language
  2055. */
  2056. function convertPlural( $count, $forms ) {
  2057. if ( !count($forms) ) { return ''; }
  2058. $forms = $this->preConvertPlural( $forms, 2 );
  2059. return ( $count == 1 ) ? $forms[0] : $forms[1];
  2060. }
  2061. /**
  2062. * Checks that convertPlural was given an array and pads it to requested
  2063. * amound of forms by copying the last one.
  2064. *
  2065. * @param $count Integer: How many forms should there be at least
  2066. * @param $forms Array of forms given to convertPlural
  2067. * @return array Padded array of forms or an exception if not an array
  2068. */
  2069. protected function preConvertPlural( /* Array */ $forms, $count ) {
  2070. while ( count($forms) < $count ) {
  2071. $forms[] = $forms[count($forms)-1];
  2072. }
  2073. return $forms;
  2074. }
  2075. /**
  2076. * For translaing of expiry times
  2077. * @param $str String: the validated block time in English
  2078. * @return Somehow translated block time
  2079. * @see LanguageFi.php for example implementation
  2080. */
  2081. function translateBlockExpiry( $str ) {
  2082. $scBlockExpiryOptions = $this->getMessageFromDB( 'ipboptions' );
  2083. if ( $scBlockExpiryOptions == '-') {
  2084. return $str;
  2085. }
  2086. foreach (explode(',', $scBlockExpiryOptions) as $option) {
  2087. if ( strpos($option, ":") === false )
  2088. continue;
  2089. list($show, $value) = explode(":", $option);
  2090. if ( strcmp ( $str, $value) == 0 ) {
  2091. return htmlspecialchars( trim( $show ) );
  2092. }
  2093. }
  2094. return $str;
  2095. }
  2096. /**
  2097. * languages like Chinese need to be segmented in order for the diff
  2098. * to be of any use
  2099. *
  2100. * @param $text String
  2101. * @return String
  2102. */
  2103. function segmentForDiff( $text ) {
  2104. return $text;
  2105. }
  2106. /**
  2107. * and unsegment to show the result
  2108. *
  2109. * @param $text String
  2110. * @return String
  2111. */
  2112. function unsegmentForDiff( $text ) {
  2113. return $text;
  2114. }
  2115. # convert text to all supported variants
  2116. function autoConvertToAllVariants($text) {
  2117. return $this->mConverter->autoConvertToAllVariants($text);
  2118. }
  2119. # convert text to different variants of a language.
  2120. function convert( $text ) {
  2121. return $this->mConverter->convert( $text );
  2122. }
  2123. # Convert a Title object to a string in the preferred variant
  2124. function convertTitle( $title ) {
  2125. return $this->mConverter->convertTitle( $title );
  2126. }
  2127. # Check if this is a language with variants
  2128. function hasVariants(){
  2129. return sizeof($this->getVariants())>1;
  2130. }
  2131. # Put custom tags (e.g. -{ }-) around math to prevent conversion
  2132. function armourMath($text){
  2133. return $this->mConverter->armourMath($text);
  2134. }
  2135. /**
  2136. * Perform output conversion on a string, and encode for safe HTML output.
  2137. * @param $text String text to be converted
  2138. * @param $isTitle Bool whether this conversion is for the article title
  2139. * @return string
  2140. * @todo this should get integrated somewhere sane
  2141. */
  2142. function convertHtml( $text, $isTitle = false ) {
  2143. return htmlspecialchars( $this->convert( $text, $isTitle ) );
  2144. }
  2145. function convertCategoryKey( $key ) {
  2146. return $this->mConverter->convertCategoryKey( $key );
  2147. }
  2148. /**
  2149. * get the list of variants supported by this langauge
  2150. * see sample implementation in LanguageZh.php
  2151. *
  2152. * @return array an array of language codes
  2153. */
  2154. function getVariants() {
  2155. return $this->mConverter->getVariants();
  2156. }
  2157. function getPreferredVariant( $fromUser = true, $fromHeader = false ) {
  2158. return $this->mConverter->getPreferredVariant( $fromUser, $fromHeader );
  2159. }
  2160. /**
  2161. * if a language supports multiple variants, it is
  2162. * possible that non-existing link in one variant
  2163. * actually exists in another variant. this function
  2164. * tries to find it. See e.g. LanguageZh.php
  2165. *
  2166. * @param $link String: the name of the link
  2167. * @param $nt Mixed: the title object of the link
  2168. * @param boolean $ignoreOtherCond: to disable other conditions when
  2169. * we need to transclude a template or update a category's link
  2170. * @return null the input parameters may be modified upon return
  2171. */
  2172. function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
  2173. $this->mConverter->findVariantLink( $link, $nt, $ignoreOtherCond );
  2174. }
  2175. /**
  2176. * If a language supports multiple variants, converts text
  2177. * into an array of all possible variants of the text:
  2178. * 'variant' => text in that variant
  2179. */
  2180. function convertLinkToAllVariants($text){
  2181. return $this->mConverter->convertLinkToAllVariants($text);
  2182. }
  2183. /**
  2184. * returns language specific options used by User::getPageRenderHash()
  2185. * for example, the preferred language variant
  2186. *
  2187. * @return string
  2188. */
  2189. function getExtraHashOptions() {
  2190. return $this->mConverter->getExtraHashOptions();
  2191. }
  2192. /**
  2193. * for languages that support multiple variants, the title of an
  2194. * article may be displayed differently in different variants. this
  2195. * function returns the apporiate title defined in the body of the article.
  2196. *
  2197. * @return string
  2198. */
  2199. function getParsedTitle() {
  2200. return $this->mConverter->getParsedTitle();
  2201. }
  2202. /**
  2203. * Enclose a string with the "no conversion" tag. This is used by
  2204. * various functions in the Parser
  2205. *
  2206. * @param $text String: text to be tagged for no conversion
  2207. * @param $noParse
  2208. * @return string the tagged text
  2209. */
  2210. function markNoConversion( $text, $noParse=false ) {
  2211. return $this->mConverter->markNoConversion( $text, $noParse );
  2212. }
  2213. /**
  2214. * A regular expression to match legal word-trailing characters
  2215. * which should be merged onto a link of the form [[foo]]bar.
  2216. *
  2217. * @return string
  2218. */
  2219. function linkTrail() {
  2220. return self::$dataCache->getItem( $this->mCode, 'linkTrail' );
  2221. }
  2222. function getLangObj() {
  2223. return $this;
  2224. }
  2225. /**
  2226. * Get the RFC 3066 code for this language object
  2227. */
  2228. function getCode() {
  2229. return $this->mCode;
  2230. }
  2231. function setCode( $code ) {
  2232. $this->mCode = $code;
  2233. }
  2234. /**
  2235. * Get the name of a file for a certain language code
  2236. * @param $prefix string Prepend this to the filename
  2237. * @param $code string Language code
  2238. * @param $suffix string Append this to the filename
  2239. * @return string $prefix . $mangledCode . $suffix
  2240. */
  2241. static function getFileName( $prefix = 'Language', $code, $suffix = '.php' ) {
  2242. // Protect against path traversal
  2243. if ( !Language::isValidCode( $code )
  2244. || strcspn( $code, "/\\\000" ) !== strlen( $code ) )
  2245. {
  2246. throw new MWException( "Invalid language code \"$code\"" );
  2247. }
  2248. return $prefix . str_replace( '-', '_', ucfirst( $code ) ) . $suffix;
  2249. }
  2250. /**
  2251. * Get the language code from a file name. Inverse of getFileName()
  2252. * @param $filename string $prefix . $languageCode . $suffix
  2253. * @param $prefix string Prefix before the language code
  2254. * @param $suffix string Suffix after the language code
  2255. * @return Language code, or false if $prefix or $suffix isn't found
  2256. */
  2257. static function getCodeFromFileName( $filename, $prefix = 'Language', $suffix = '.php' ) {
  2258. $m = null;
  2259. preg_match( '/' . preg_quote( $prefix, '/' ) . '([A-Z][a-z_]+)' .
  2260. preg_quote( $suffix, '/' ) . '/', $filename, $m );
  2261. if ( !count( $m ) ) {
  2262. return false;
  2263. }
  2264. return str_replace( '_', '-', strtolower( $m[1] ) );
  2265. }
  2266. static function getMessagesFileName( $code ) {
  2267. global $IP;
  2268. return self::getFileName( "$IP/languages/messages/Messages", $code, '.php' );
  2269. }
  2270. static function getClassFileName( $code ) {
  2271. global $IP;
  2272. return self::getFileName( "$IP/languages/classes/Language", $code, '.php' );
  2273. }
  2274. /**
  2275. * Get the fallback for a given language
  2276. */
  2277. static function getFallbackFor( $code ) {
  2278. if ( $code === 'en' ) {
  2279. // Shortcut
  2280. return false;
  2281. } else {
  2282. return self::getLocalisationCache()->getItem( $code, 'fallback' );
  2283. }
  2284. }
  2285. /**
  2286. * Get all messages for a given language
  2287. * WARNING: this may take a long time
  2288. */
  2289. static function getMessagesFor( $code ) {
  2290. return self::getLocalisationCache()->getItem( $code, 'messages' );
  2291. }
  2292. /**
  2293. * Get a message for a given language
  2294. */
  2295. static function getMessageFor( $key, $code ) {
  2296. return self::getLocalisationCache()->getSubitem( $code, 'messages', $key );
  2297. }
  2298. function fixVariableInNamespace( $talk ) {
  2299. if ( strpos( $talk, '$1' ) === false ) return $talk;
  2300. global $wgMetaNamespace;
  2301. $talk = str_replace( '$1', $wgMetaNamespace, $talk );
  2302. # Allow grammar transformations
  2303. # Allowing full message-style parsing would make simple requests
  2304. # such as action=raw much more expensive than they need to be.
  2305. # This will hopefully cover most cases.
  2306. $talk = preg_replace_callback( '/{{grammar:(.*?)\|(.*?)}}/i',
  2307. array( &$this, 'replaceGrammarInNamespace' ), $talk );
  2308. return str_replace( ' ', '_', $talk );
  2309. }
  2310. function replaceGrammarInNamespace( $m ) {
  2311. return $this->convertGrammar( trim( $m[2] ), trim( $m[1] ) );
  2312. }
  2313. static function getCaseMaps() {
  2314. static $wikiUpperChars, $wikiLowerChars;
  2315. if ( isset( $wikiUpperChars ) ) {
  2316. return array( $wikiUpperChars, $wikiLowerChars );
  2317. }
  2318. wfProfileIn( __METHOD__ );
  2319. $arr = wfGetPrecompiledData( 'Utf8Case.ser' );
  2320. if ( $arr === false ) {
  2321. throw new MWException(
  2322. "Utf8Case.ser is missing, please run \"make\" in the serialized directory\n" );
  2323. }
  2324. extract( $arr );
  2325. wfProfileOut( __METHOD__ );
  2326. return array( $wikiUpperChars, $wikiLowerChars );
  2327. }
  2328. function formatTimePeriod( $seconds ) {
  2329. if ( $seconds < 10 ) {
  2330. return $this->formatNum( sprintf( "%.1f", $seconds ) ) . wfMsg( 'seconds-abbrev' );
  2331. } elseif ( $seconds < 60 ) {
  2332. return $this->formatNum( round( $seconds ) ) . wfMsg( 'seconds-abbrev' );
  2333. } elseif ( $seconds < 3600 ) {
  2334. return $this->formatNum( floor( $seconds / 60 ) ) . wfMsg( 'minutes-abbrev' ) .
  2335. $this->formatNum( round( fmod( $seconds, 60 ) ) ) . wfMsg( 'seconds-abbrev' );
  2336. } else {
  2337. $hours = floor( $seconds / 3600 );
  2338. $minutes = floor( ( $seconds - $hours * 3600 ) / 60 );
  2339. $secondsPart = round( $seconds - $hours * 3600 - $minutes * 60 );
  2340. return $this->formatNum( $hours ) . wfMsg( 'hours-abbrev' ) .
  2341. $this->formatNum( $minutes ) . wfMsg( 'minutes-abbrev' ) .
  2342. $this->formatNum( $secondsPart ) . wfMsg( 'seconds-abbrev' );
  2343. }
  2344. }
  2345. function formatBitrate( $bps ) {
  2346. $units = array( 'bps', 'kbps', 'Mbps', 'Gbps' );
  2347. if ( $bps <= 0 ) {
  2348. return $this->formatNum( $bps ) . $units[0];
  2349. }
  2350. $unitIndex = floor( log10( $bps ) / 3 );
  2351. $mantissa = $bps / pow( 1000, $unitIndex );
  2352. if ( $mantissa < 10 ) {
  2353. $mantissa = round( $mantissa, 1 );
  2354. } else {
  2355. $mantissa = round( $mantissa );
  2356. }
  2357. return $this->formatNum( $mantissa ) . $units[$unitIndex];
  2358. }
  2359. /**
  2360. * Format a size in bytes for output, using an appropriate
  2361. * unit (B, KB, MB or GB) according to the magnitude in question
  2362. *
  2363. * @param $size Size to format
  2364. * @return string Plain text (not HTML)
  2365. */
  2366. function formatSize( $size ) {
  2367. // For small sizes no decimal places necessary
  2368. $round = 0;
  2369. if( $size > 1024 ) {
  2370. $size = $size / 1024;
  2371. if( $size > 1024 ) {
  2372. $size = $size / 1024;
  2373. // For MB and bigger two decimal places are smarter
  2374. $round = 2;
  2375. if( $size > 1024 ) {
  2376. $size = $size / 1024;
  2377. $msg = 'size-gigabytes';
  2378. } else {
  2379. $msg = 'size-megabytes';
  2380. }
  2381. } else {
  2382. $msg = 'size-kilobytes';
  2383. }
  2384. } else {
  2385. $msg = 'size-bytes';
  2386. }
  2387. $size = round( $size, $round );
  2388. $text = $this->getMessageFromDB( $msg );
  2389. return str_replace( '$1', $this->formatNum( $size ), $text );
  2390. }
  2391. /**
  2392. * Get the conversion rule title, if any.
  2393. */
  2394. function getConvRuleTitle() {
  2395. return $this->mConverter->getConvRuleTitle();
  2396. }
  2397. }