PageRenderTime 121ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/mediawiki-integration/source/php/mediawiki/languages/Language.php

https://code.google.com/
PHP | 1779 lines | 1105 code | 196 blank | 478 comment | 185 complexity | 12320c6297f10eb5b9bcefcbe149731b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * @package MediaWiki
  4. * @subpackage Language
  5. */
  6. if( !defined( 'MEDIAWIKI' ) ) {
  7. echo "This file is part of MediaWiki, it is not a valid entry point.\n";
  8. exit( 1 );
  9. }
  10. #
  11. # In general you should not make customizations in these language files
  12. # directly, but should use the MediaWiki: special namespace to customize
  13. # user interface messages through the wiki.
  14. # See http://meta.wikipedia.org/wiki/MediaWiki_namespace
  15. #
  16. # NOTE TO TRANSLATORS: Do not copy this whole file when making translations!
  17. # A lot of common constants and a base class with inheritable methods are
  18. # defined here, which should not be redefined. See the other LanguageXx.php
  19. # files for examples.
  20. #
  21. # Read language names
  22. global $wgLanguageNames;
  23. require_once( 'Names.php' );
  24. global $wgInputEncoding, $wgOutputEncoding;
  25. /**
  26. * These are always UTF-8, they exist only for backwards compatibility
  27. */
  28. $wgInputEncoding = "UTF-8";
  29. $wgOutputEncoding = "UTF-8";
  30. if( function_exists( 'mb_strtoupper' ) ) {
  31. mb_internal_encoding('UTF-8');
  32. }
  33. /* a fake language converter */
  34. class FakeConverter {
  35. var $mLang;
  36. function FakeConverter($langobj) {$this->mLang = $langobj;}
  37. function convert($t, $i) {return $t;}
  38. function parserConvert($t, $p) {return $t;}
  39. function getVariants() { return array( $this->mLang->getCode() ); }
  40. function getPreferredVariant() {return $this->mLang->getCode(); }
  41. function findVariantLink(&$l, &$n) {}
  42. function getExtraHashOptions() {return '';}
  43. function getParsedTitle() {return '';}
  44. function markNoConversion($text, $noParse=false) {return $text;}
  45. function convertCategoryKey( $key ) {return $key; }
  46. function convertLinkToAllVariants($text){ return array( $this->mLang->getCode() => $text); }
  47. function armourMath($text){ return $text; }
  48. }
  49. #--------------------------------------------------------------------------
  50. # Internationalisation code
  51. #--------------------------------------------------------------------------
  52. class Language {
  53. var $mConverter, $mVariants, $mCode, $mLoaded = false;
  54. static public $mLocalisationKeys = array( 'fallback', 'namespaceNames',
  55. 'quickbarSettings', 'skinNames', 'mathNames',
  56. 'bookstoreList', 'magicWords', 'messages', 'rtl', 'digitTransformTable',
  57. 'separatorTransformTable', 'fallback8bitEncoding', 'linkPrefixExtension',
  58. 'defaultUserOptionOverrides', 'linkTrail', 'namespaceAliases',
  59. 'dateFormats', 'datePreferences', 'datePreferenceMigrationMap',
  60. 'defaultDateFormat', 'extraUserToggles', 'specialPageAliases' );
  61. static public $mMergeableMapKeys = array( 'messages', 'namespaceNames', 'mathNames',
  62. 'dateFormats', 'defaultUserOptionOverrides', 'magicWords' );
  63. static public $mMergeableListKeys = array( 'extraUserToggles' );
  64. static public $mMergeableAliasListKeys = array( 'specialPageAliases' );
  65. static public $mLocalisationCache = array();
  66. static public $mWeekdayMsgs = array(
  67. 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday',
  68. 'friday', 'saturday'
  69. );
  70. static public $mWeekdayAbbrevMsgs = array(
  71. 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'
  72. );
  73. static public $mMonthMsgs = array(
  74. 'january', 'february', 'march', 'april', 'may_long', 'june',
  75. 'july', 'august', 'september', 'october', 'november',
  76. 'december'
  77. );
  78. static public $mMonthGenMsgs = array(
  79. 'january-gen', 'february-gen', 'march-gen', 'april-gen', 'may-gen', 'june-gen',
  80. 'july-gen', 'august-gen', 'september-gen', 'october-gen', 'november-gen',
  81. 'december-gen'
  82. );
  83. static public $mMonthAbbrevMsgs = array(
  84. 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
  85. 'sep', 'oct', 'nov', 'dec'
  86. );
  87. /**
  88. * Create a language object for a given language code
  89. */
  90. static function factory( $code ) {
  91. global $IP;
  92. static $recursionLevel = 0;
  93. if ( $code == 'en' ) {
  94. $class = 'Language';
  95. } else {
  96. $class = 'Language' . str_replace( '-', '_', ucfirst( $code ) );
  97. // Preload base classes to work around APC/PHP5 bug
  98. if ( file_exists( "$IP/languages/classes/$class.deps.php" ) ) {
  99. include_once("$IP/languages/classes/$class.deps.php");
  100. }
  101. if ( file_exists( "$IP/languages/classes/$class.php" ) ) {
  102. include_once("$IP/languages/classes/$class.php");
  103. }
  104. }
  105. if ( $recursionLevel > 5 ) {
  106. throw new MWException( "Language fallback loop detected when creating class $class\n" );
  107. }
  108. if( ! class_exists( $class ) ) {
  109. $fallback = Language::getFallbackFor( $code );
  110. ++$recursionLevel;
  111. $lang = Language::factory( $fallback );
  112. --$recursionLevel;
  113. $lang->setCode( $code );
  114. } else {
  115. $lang = new $class;
  116. }
  117. return $lang;
  118. }
  119. function __construct() {
  120. $this->mConverter = new FakeConverter($this);
  121. // Set the code to the name of the descendant
  122. if ( get_class( $this ) == 'Language' ) {
  123. $this->mCode = 'en';
  124. } else {
  125. $this->mCode = str_replace( '_', '-', strtolower( substr( get_class( $this ), 8 ) ) );
  126. }
  127. }
  128. /**
  129. * Hook which will be called if this is the content language.
  130. * Descendants can use this to register hook functions or modify globals
  131. */
  132. function initContLang() {}
  133. /**
  134. * @deprecated
  135. * @return array
  136. */
  137. function getDefaultUserOptions() {
  138. return User::getDefaultOptions();
  139. }
  140. /**
  141. * Exports $wgBookstoreListEn
  142. * @return array
  143. */
  144. function getBookstoreList() {
  145. $this->load();
  146. return $this->bookstoreList;
  147. }
  148. /**
  149. * @return array
  150. */
  151. function getNamespaces() {
  152. $this->load();
  153. return $this->namespaceNames;
  154. }
  155. /**
  156. * A convenience function that returns the same thing as
  157. * getNamespaces() except with the array values changed to ' '
  158. * where it found '_', useful for producing output to be displayed
  159. * e.g. in <select> forms.
  160. *
  161. * @return array
  162. */
  163. function getFormattedNamespaces() {
  164. $ns = $this->getNamespaces();
  165. foreach($ns as $k => $v) {
  166. $ns[$k] = strtr($v, '_', ' ');
  167. }
  168. return $ns;
  169. }
  170. /**
  171. * Get a namespace value by key
  172. * <code>
  173. * $mw_ns = $wgContLang->getNsText( NS_MEDIAWIKI );
  174. * echo $mw_ns; // prints 'MediaWiki'
  175. * </code>
  176. *
  177. * @param int $index the array key of the namespace to return
  178. * @return mixed, string if the namespace value exists, otherwise false
  179. */
  180. function getNsText( $index ) {
  181. $ns = $this->getNamespaces();
  182. return isset( $ns[$index] ) ? $ns[$index] : false;
  183. }
  184. /**
  185. * A convenience function that returns the same thing as
  186. * getNsText() except with '_' changed to ' ', useful for
  187. * producing output.
  188. *
  189. * @return array
  190. */
  191. function getFormattedNsText( $index ) {
  192. $ns = $this->getNsText( $index );
  193. return strtr($ns, '_', ' ');
  194. }
  195. /**
  196. * Get a namespace key by value, case insensetive.
  197. *
  198. * @param string $text
  199. * @return mixed An integer if $text is a valid value otherwise false
  200. */
  201. function getNsIndex( $text ) {
  202. $this->load();
  203. $lctext = $this->lc($text);
  204. return isset( $this->mNamespaceIds[$lctext] ) ? $this->mNamespaceIds[$lctext] : false;
  205. }
  206. /**
  207. * short names for language variants used for language conversion links.
  208. *
  209. * @param string $code
  210. * @return string
  211. */
  212. function getVariantname( $code ) {
  213. return $this->getMessageFromDB( "variantname-$code" );
  214. }
  215. function specialPage( $name ) {
  216. $aliases = $this->getSpecialPageAliases();
  217. if ( isset( $aliases[$name][0] ) ) {
  218. $name = $aliases[$name][0];
  219. }
  220. return $this->getNsText(NS_SPECIAL) . ':' . $name;
  221. }
  222. function getQuickbarSettings() {
  223. $this->load();
  224. return $this->quickbarSettings;
  225. }
  226. function getSkinNames() {
  227. $this->load();
  228. return $this->skinNames;
  229. }
  230. function getMathNames() {
  231. $this->load();
  232. return $this->mathNames;
  233. }
  234. function getDatePreferences() {
  235. $this->load();
  236. return $this->datePreferences;
  237. }
  238. function getDateFormats() {
  239. $this->load();
  240. return $this->dateFormats;
  241. }
  242. function getDefaultDateFormat() {
  243. $this->load();
  244. return $this->defaultDateFormat;
  245. }
  246. function getDatePreferenceMigrationMap() {
  247. $this->load();
  248. return $this->datePreferenceMigrationMap;
  249. }
  250. function getDefaultUserOptionOverrides() {
  251. $this->load();
  252. return $this->defaultUserOptionOverrides;
  253. }
  254. function getExtraUserToggles() {
  255. $this->load();
  256. return $this->extraUserToggles;
  257. }
  258. function getUserToggle( $tog ) {
  259. return $this->getMessageFromDB( "tog-$tog" );
  260. }
  261. /**
  262. * Get language names, indexed by code.
  263. * If $customisedOnly is true, only returns codes with a messages file
  264. */
  265. public static function getLanguageNames( $customisedOnly = false ) {
  266. global $wgLanguageNames;
  267. if ( !$customisedOnly ) {
  268. return $wgLanguageNames;
  269. }
  270. global $IP;
  271. $messageFiles = glob( "$IP/languages/messages/Messages*.php" );
  272. $names = array();
  273. foreach ( $messageFiles as $file ) {
  274. if( preg_match( '/Messages([A-Z][a-z_]+)\.php$/', $file, $m ) ) {
  275. $code = str_replace( '_', '-', strtolower( $m[1] ) );
  276. if ( isset( $wgLanguageNames[$code] ) ) {
  277. $names[$code] = $wgLanguageNames[$code];
  278. }
  279. }
  280. }
  281. return $names;
  282. }
  283. /**
  284. * Ugly hack to get a message maybe from the MediaWiki namespace, if this
  285. * language object is the content or user language.
  286. */
  287. function getMessageFromDB( $msg ) {
  288. global $wgContLang, $wgLang;
  289. if ( $wgContLang->getCode() == $this->getCode() ) {
  290. # Content language
  291. return wfMsgForContent( $msg );
  292. } elseif ( $wgLang->getCode() == $this->getCode() ) {
  293. # User language
  294. return wfMsg( $msg );
  295. } else {
  296. # Neither, get from localisation
  297. return $this->getMessage( $msg );
  298. }
  299. }
  300. function getLanguageName( $code ) {
  301. global $wgLanguageNames;
  302. if ( ! array_key_exists( $code, $wgLanguageNames ) ) {
  303. return '';
  304. }
  305. return $wgLanguageNames[$code];
  306. }
  307. function getMonthName( $key ) {
  308. return $this->getMessageFromDB( self::$mMonthMsgs[$key-1] );
  309. }
  310. function getMonthNameGen( $key ) {
  311. return $this->getMessageFromDB( self::$mMonthGenMsgs[$key-1] );
  312. }
  313. function getMonthAbbreviation( $key ) {
  314. return $this->getMessageFromDB( self::$mMonthAbbrevMsgs[$key-1] );
  315. }
  316. function getWeekdayName( $key ) {
  317. return $this->getMessageFromDB( self::$mWeekdayMsgs[$key-1] );
  318. }
  319. function getWeekdayAbbreviation( $key ) {
  320. return $this->getMessageFromDB( self::$mWeekdayAbbrevMsgs[$key-1] );
  321. }
  322. /**
  323. * Used by date() and time() to adjust the time output.
  324. * @public
  325. * @param int $ts the time in date('YmdHis') format
  326. * @param mixed $tz adjust the time by this amount (default false,
  327. * mean we get user timecorrection setting)
  328. * @return int
  329. */
  330. function userAdjust( $ts, $tz = false ) {
  331. global $wgUser, $wgLocalTZoffset;
  332. if (!$tz) {
  333. $tz = $wgUser->getOption( 'timecorrection' );
  334. }
  335. # minutes and hours differences:
  336. $minDiff = 0;
  337. $hrDiff = 0;
  338. if ( $tz === '' ) {
  339. # Global offset in minutes.
  340. if( isset($wgLocalTZoffset) ) {
  341. $hrDiff = $wgLocalTZoffset % 60;
  342. $minDiff = $wgLocalTZoffset - ($hrDiff * 60);
  343. }
  344. } elseif ( strpos( $tz, ':' ) !== false ) {
  345. $tzArray = explode( ':', $tz );
  346. $hrDiff = intval($tzArray[0]);
  347. $minDiff = intval($hrDiff < 0 ? -$tzArray[1] : $tzArray[1]);
  348. } else {
  349. $hrDiff = intval( $tz );
  350. }
  351. # No difference ? Return time unchanged
  352. if ( 0 == $hrDiff && 0 == $minDiff ) { return $ts; }
  353. # Generate an adjusted date
  354. $t = mktime( (
  355. (int)substr( $ts, 8, 2) ) + $hrDiff, # Hours
  356. (int)substr( $ts, 10, 2 ) + $minDiff, # Minutes
  357. (int)substr( $ts, 12, 2 ), # Seconds
  358. (int)substr( $ts, 4, 2 ), # Month
  359. (int)substr( $ts, 6, 2 ), # Day
  360. (int)substr( $ts, 0, 4 ) ); #Year
  361. return date( 'YmdHis', $t );
  362. }
  363. /**
  364. * This is a workalike of PHP's date() function, but with better
  365. * internationalisation, a reduced set of format characters, and a better
  366. * escaping format.
  367. *
  368. * Supported format characters are dDjlNwzWFmMntLYyaAgGhHiscrU. See the
  369. * PHP manual for definitions. There are a number of extensions, which
  370. * start with "x":
  371. *
  372. * xn Do not translate digits of the next numeric format character
  373. * xN Toggle raw digit (xn) flag, stays set until explicitly unset
  374. * xr Use roman numerals for the next numeric format character
  375. * xx Literal x
  376. * xg Genitive month name
  377. *
  378. * Characters enclosed in double quotes will be considered literal (with
  379. * the quotes themselves removed). Unmatched quotes will be considered
  380. * literal quotes. Example:
  381. *
  382. * "The month is" F => The month is January
  383. * i's" => 20'11"
  384. *
  385. * Backslash escaping is also supported.
  386. *
  387. * @param string $format
  388. * @param string $ts 14-character timestamp
  389. * YYYYMMDDHHMMSS
  390. * 01234567890123
  391. */
  392. function sprintfDate( $format, $ts ) {
  393. $s = '';
  394. $raw = false;
  395. $roman = false;
  396. $unix = false;
  397. $rawToggle = false;
  398. for ( $p = 0; $p < strlen( $format ); $p++ ) {
  399. $num = false;
  400. $code = $format[$p];
  401. if ( $code == 'x' && $p < strlen( $format ) - 1 ) {
  402. $code .= $format[++$p];
  403. }
  404. switch ( $code ) {
  405. case 'xx':
  406. $s .= 'x';
  407. break;
  408. case 'xn':
  409. $raw = true;
  410. break;
  411. case 'xN':
  412. $rawToggle = !$rawToggle;
  413. break;
  414. case 'xr':
  415. $roman = true;
  416. break;
  417. case 'xg':
  418. $s .= $this->getMonthNameGen( substr( $ts, 4, 2 ) );
  419. break;
  420. case 'd':
  421. $num = substr( $ts, 6, 2 );
  422. break;
  423. case 'D':
  424. if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
  425. $s .= $this->getWeekdayAbbreviation( date( 'w', $unix ) + 1 );
  426. break;
  427. case 'j':
  428. $num = intval( substr( $ts, 6, 2 ) );
  429. break;
  430. case 'l':
  431. if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
  432. $s .= $this->getWeekdayName( date( 'w', $unix ) + 1 );
  433. break;
  434. case 'N':
  435. if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
  436. $w = date( 'w', $unix );
  437. $num = $w ? $w : 7;
  438. break;
  439. case 'w':
  440. if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
  441. $num = date( 'w', $unix );
  442. break;
  443. case 'z':
  444. if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
  445. $num = date( 'z', $unix );
  446. break;
  447. case 'W':
  448. if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
  449. $num = date( 'W', $unix );
  450. break;
  451. case 'F':
  452. $s .= $this->getMonthName( substr( $ts, 4, 2 ) );
  453. break;
  454. case 'm':
  455. $num = substr( $ts, 4, 2 );
  456. break;
  457. case 'M':
  458. $s .= $this->getMonthAbbreviation( substr( $ts, 4, 2 ) );
  459. break;
  460. case 'n':
  461. $num = intval( substr( $ts, 4, 2 ) );
  462. break;
  463. case 't':
  464. if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
  465. $num = date( 't', $unix );
  466. break;
  467. case 'L':
  468. if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
  469. $num = date( 'L', $unix );
  470. break;
  471. case 'Y':
  472. $num = substr( $ts, 0, 4 );
  473. break;
  474. case 'y':
  475. $num = substr( $ts, 2, 2 );
  476. break;
  477. case 'a':
  478. $s .= intval( substr( $ts, 8, 2 ) ) < 12 ? 'am' : 'pm';
  479. break;
  480. case 'A':
  481. $s .= intval( substr( $ts, 8, 2 ) ) < 12 ? 'AM' : 'PM';
  482. break;
  483. case 'g':
  484. $h = substr( $ts, 8, 2 );
  485. $num = $h % 12 ? $h % 12 : 12;
  486. break;
  487. case 'G':
  488. $num = intval( substr( $ts, 8, 2 ) );
  489. break;
  490. case 'h':
  491. $h = substr( $ts, 8, 2 );
  492. $num = sprintf( '%02d', $h % 12 ? $h % 12 : 12 );
  493. break;
  494. case 'H':
  495. $num = substr( $ts, 8, 2 );
  496. break;
  497. case 'i':
  498. $num = substr( $ts, 10, 2 );
  499. break;
  500. case 's':
  501. $num = substr( $ts, 12, 2 );
  502. break;
  503. case 'c':
  504. if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
  505. $s .= date( 'c', $unix );
  506. break;
  507. case 'r':
  508. if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
  509. $s .= date( 'r', $unix );
  510. break;
  511. case 'U':
  512. if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
  513. $num = $unix;
  514. break;
  515. case '\\':
  516. # Backslash escaping
  517. if ( $p < strlen( $format ) - 1 ) {
  518. $s .= $format[++$p];
  519. } else {
  520. $s .= '\\';
  521. }
  522. break;
  523. case '"':
  524. # Quoted literal
  525. if ( $p < strlen( $format ) - 1 ) {
  526. $endQuote = strpos( $format, '"', $p + 1 );
  527. if ( $endQuote === false ) {
  528. # No terminating quote, assume literal "
  529. $s .= '"';
  530. } else {
  531. $s .= substr( $format, $p + 1, $endQuote - $p - 1 );
  532. $p = $endQuote;
  533. }
  534. } else {
  535. # Quote at end of string, assume literal "
  536. $s .= '"';
  537. }
  538. break;
  539. default:
  540. $s .= $format[$p];
  541. }
  542. if ( $num !== false ) {
  543. if ( $rawToggle || $raw ) {
  544. $s .= $num;
  545. $raw = false;
  546. } elseif ( $roman ) {
  547. $s .= self::romanNumeral( $num );
  548. $roman = false;
  549. } else {
  550. $s .= $this->formatNum( $num, true );
  551. }
  552. $num = false;
  553. }
  554. }
  555. return $s;
  556. }
  557. /**
  558. * Roman number formatting up to 3000
  559. */
  560. static function romanNumeral( $num ) {
  561. static $table = array(
  562. array( '', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X' ),
  563. array( '', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC', 'C' ),
  564. array( '', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM', 'M' ),
  565. array( '', 'M', 'MM', 'MMM' )
  566. );
  567. $num = intval( $num );
  568. if ( $num > 3000 || $num <= 0 ) {
  569. return $num;
  570. }
  571. $s = '';
  572. for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
  573. if ( $num >= $pow10 ) {
  574. $s .= $table[$i][floor($num / $pow10)];
  575. }
  576. $num = $num % $pow10;
  577. }
  578. return $s;
  579. }
  580. /**
  581. * This is meant to be used by time(), date(), and timeanddate() to get
  582. * the date preference they're supposed to use, it should be used in
  583. * all children.
  584. *
  585. *<code>
  586. * function timeanddate([...], $format = true) {
  587. * $datePreference = $this->dateFormat($format);
  588. * [...]
  589. * }
  590. *</code>
  591. *
  592. * @param mixed $usePrefs: if true, the user's preference is used
  593. * if false, the site/language default is used
  594. * if int/string, assumed to be a format.
  595. * @return string
  596. */
  597. function dateFormat( $usePrefs = true ) {
  598. global $wgUser;
  599. if( is_bool( $usePrefs ) ) {
  600. if( $usePrefs ) {
  601. $datePreference = $wgUser->getDatePreference();
  602. } else {
  603. $options = User::getDefaultOptions();
  604. $datePreference = (string)$options['date'];
  605. }
  606. } else {
  607. $datePreference = (string)$usePrefs;
  608. }
  609. // return int
  610. if( $datePreference == '' ) {
  611. return 'default';
  612. }
  613. return $datePreference;
  614. }
  615. /**
  616. * @public
  617. * @param mixed $ts the time format which needs to be turned into a
  618. * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
  619. * @param bool $adj whether to adjust the time output according to the
  620. * user configured offset ($timecorrection)
  621. * @param mixed $format true to use user's date format preference
  622. * @param string $timecorrection the time offset as returned by
  623. * validateTimeZone() in Special:Preferences
  624. * @return string
  625. */
  626. function date( $ts, $adj = false, $format = true, $timecorrection = false ) {
  627. $this->load();
  628. if ( $adj ) {
  629. $ts = $this->userAdjust( $ts, $timecorrection );
  630. }
  631. $pref = $this->dateFormat( $format );
  632. if( $pref == 'default' || !isset( $this->dateFormats["$pref date"] ) ) {
  633. $pref = $this->defaultDateFormat;
  634. }
  635. return $this->sprintfDate( $this->dateFormats["$pref date"], $ts );
  636. }
  637. /**
  638. * @public
  639. * @param mixed $ts the time format which needs to be turned into a
  640. * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
  641. * @param bool $adj whether to adjust the time output according to the
  642. * user configured offset ($timecorrection)
  643. * @param mixed $format true to use user's date format preference
  644. * @param string $timecorrection the time offset as returned by
  645. * validateTimeZone() in Special:Preferences
  646. * @return string
  647. */
  648. function time( $ts, $adj = false, $format = true, $timecorrection = false ) {
  649. $this->load();
  650. if ( $adj ) {
  651. $ts = $this->userAdjust( $ts, $timecorrection );
  652. }
  653. $pref = $this->dateFormat( $format );
  654. if( $pref == 'default' || !isset( $this->dateFormats["$pref time"] ) ) {
  655. $pref = $this->defaultDateFormat;
  656. }
  657. return $this->sprintfDate( $this->dateFormats["$pref time"], $ts );
  658. }
  659. /**
  660. * @public
  661. * @param mixed $ts the time format which needs to be turned into a
  662. * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
  663. * @param bool $adj whether to adjust the time output according to the
  664. * user configured offset ($timecorrection)
  665. * @param mixed $format what format to return, if it's false output the
  666. * default one (default true)
  667. * @param string $timecorrection the time offset as returned by
  668. * validateTimeZone() in Special:Preferences
  669. * @return string
  670. */
  671. function timeanddate( $ts, $adj = false, $format = true, $timecorrection = false) {
  672. $this->load();
  673. if ( $adj ) {
  674. $ts = $this->userAdjust( $ts, $timecorrection );
  675. }
  676. $pref = $this->dateFormat( $format );
  677. if( $pref == 'default' || !isset( $this->dateFormats["$pref both"] ) ) {
  678. $pref = $this->defaultDateFormat;
  679. }
  680. return $this->sprintfDate( $this->dateFormats["$pref both"], $ts );
  681. }
  682. function getMessage( $key ) {
  683. $this->load();
  684. return isset( $this->messages[$key] ) ? $this->messages[$key] : null;
  685. }
  686. function getAllMessages() {
  687. $this->load();
  688. return $this->messages;
  689. }
  690. function iconv( $in, $out, $string ) {
  691. # For most languages, this is a wrapper for iconv
  692. return iconv( $in, $out, $string );
  693. }
  694. // callback functions for uc(), lc(), ucwords(), ucwordbreaks()
  695. function ucwordbreaksCallbackAscii($matches){
  696. return $this->ucfirst($matches[1]);
  697. }
  698. function ucwordbreaksCallbackMB($matches){
  699. return mb_strtoupper($matches[0]);
  700. }
  701. function ucCallback($matches){
  702. list( $wikiUpperChars ) = self::getCaseMaps();
  703. return strtr( $matches[1], $wikiUpperChars );
  704. }
  705. function lcCallback($matches){
  706. list( , $wikiLowerChars ) = self::getCaseMaps();
  707. return strtr( $matches[1], $wikiLowerChars );
  708. }
  709. function ucwordsCallbackMB($matches){
  710. return mb_strtoupper($matches[0]);
  711. }
  712. function ucwordsCallbackWiki($matches){
  713. list( $wikiUpperChars ) = self::getCaseMaps();
  714. return strtr( $matches[0], $wikiUpperChars );
  715. }
  716. function ucfirst( $str ) {
  717. return self::uc( $str, true );
  718. }
  719. function uc( $str, $first = false ) {
  720. if ( function_exists( 'mb_strtoupper' ) )
  721. if ( $first )
  722. if ( self::isMultibyte( $str ) )
  723. return mb_strtoupper( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
  724. else
  725. return ucfirst( $str );
  726. else
  727. return self::isMultibyte( $str ) ? mb_strtoupper( $str ) : strtoupper( $str );
  728. else
  729. if ( self::isMultibyte( $str ) ) {
  730. list( $wikiUpperChars ) = $this->getCaseMaps();
  731. $x = $first ? '^' : '';
  732. return preg_replace_callback(
  733. "/$x([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
  734. array($this,"ucCallback"),
  735. $str
  736. );
  737. } else
  738. return $first ? ucfirst( $str ) : strtoupper( $str );
  739. }
  740. function lcfirst( $str ) {
  741. return self::lc( $str, true );
  742. }
  743. function lc( $str, $first = false ) {
  744. if ( function_exists( 'mb_strtolower' ) )
  745. if ( $first )
  746. if ( self::isMultibyte( $str ) )
  747. return mb_strtolower( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
  748. else
  749. return strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 );
  750. else
  751. return self::isMultibyte( $str ) ? mb_strtolower( $str ) : strtolower( $str );
  752. else
  753. if ( self::isMultibyte( $str ) ) {
  754. list( , $wikiLowerChars ) = self::getCaseMaps();
  755. $x = $first ? '^' : '';
  756. return preg_replace_callback(
  757. "/$x([A-Z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
  758. array($this,"lcCallback"),
  759. $str
  760. );
  761. } else
  762. return $first ? strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 ) : strtolower( $str );
  763. }
  764. function isMultibyte( $str ) {
  765. return (bool)preg_match( '/[\x80-\xff]/', $str );
  766. }
  767. function ucwords($str) {
  768. if ( self::isMultibyte( $str ) ) {
  769. $str = self::lc($str);
  770. // regexp to find first letter in each word (i.e. after each space)
  771. $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)| ([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
  772. // function to use to capitalize a single char
  773. if ( function_exists( 'mb_strtoupper' ) )
  774. return preg_replace_callback(
  775. $replaceRegexp,
  776. array($this,"ucwordsCallbackMB"),
  777. $str
  778. );
  779. else
  780. return preg_replace_callback(
  781. $replaceRegexp,
  782. array($this,"ucwordsCallbackWiki"),
  783. $str
  784. );
  785. }
  786. else
  787. return ucwords( strtolower( $str ) );
  788. }
  789. # capitalize words at word breaks
  790. function ucwordbreaks($str){
  791. if (self::isMultibyte( $str ) ) {
  792. $str = self::lc($str);
  793. // since \b doesn't work for UTF-8, we explicitely define word break chars
  794. $breaks= "[ \-\(\)\}\{\.,\?!]";
  795. // find first letter after word break
  796. $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)|$breaks([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
  797. if ( function_exists( 'mb_strtoupper' ) )
  798. return preg_replace_callback(
  799. $replaceRegexp,
  800. array($this,"ucwordbreaksCallbackMB"),
  801. $str
  802. );
  803. else
  804. return preg_replace_callback(
  805. $replaceRegexp,
  806. array($this,"ucwordsCallbackWiki"),
  807. $str
  808. );
  809. }
  810. else
  811. return preg_replace_callback(
  812. '/\b([\w\x80-\xff]+)\b/',
  813. array($this,"ucwordbreaksCallbackAscii"),
  814. $str );
  815. }
  816. /**
  817. * Return a case-folded representation of $s
  818. *
  819. * This is a representation such that caseFold($s1)==caseFold($s2) if $s1
  820. * and $s2 are the same except for the case of their characters. It is not
  821. * necessary for the value returned to make sense when displayed.
  822. *
  823. * Do *not* perform any other normalisation in this function. If a caller
  824. * uses this function when it should be using a more general normalisation
  825. * function, then fix the caller.
  826. */
  827. function caseFold( $s ) {
  828. return $this->uc( $s );
  829. }
  830. function checkTitleEncoding( $s ) {
  831. if( is_array( $s ) ) {
  832. wfDebugDieBacktrace( 'Given array to checkTitleEncoding.' );
  833. }
  834. # Check for non-UTF-8 URLs
  835. $ishigh = preg_match( '/[\x80-\xff]/', $s);
  836. if(!$ishigh) return $s;
  837. $isutf8 = preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
  838. '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s );
  839. if( $isutf8 ) return $s;
  840. return $this->iconv( $this->fallback8bitEncoding(), "utf-8", $s );
  841. }
  842. function fallback8bitEncoding() {
  843. $this->load();
  844. return $this->fallback8bitEncoding;
  845. }
  846. /**
  847. * Some languages have special punctuation to strip out
  848. * or characters which need to be converted for MySQL's
  849. * indexing to grok it correctly. Make such changes here.
  850. *
  851. * @param string $in
  852. * @return string
  853. */
  854. function stripForSearch( $string ) {
  855. global $wgDBtype;
  856. if ( $wgDBtype != 'mysql' ) {
  857. return $string;
  858. }
  859. # MySQL fulltext index doesn't grok utf-8, so we
  860. # need to fold cases and convert to hex
  861. wfProfileIn( __METHOD__ );
  862. if( function_exists( 'mb_strtolower' ) ) {
  863. $out = preg_replace(
  864. "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
  865. "'U8' . bin2hex( \"$1\" )",
  866. mb_strtolower( $string ) );
  867. } else {
  868. list( , $wikiLowerChars ) = self::getCaseMaps();
  869. $out = preg_replace(
  870. "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
  871. "'U8' . bin2hex( strtr( \"\$1\", \$wikiLowerChars ) )",
  872. $string );
  873. }
  874. wfProfileOut( __METHOD__ );
  875. return $out;
  876. }
  877. function convertForSearchResult( $termsArray ) {
  878. # some languages, e.g. Chinese, need to do a conversion
  879. # in order for search results to be displayed correctly
  880. return $termsArray;
  881. }
  882. /**
  883. * Get the first character of a string.
  884. *
  885. * @param string $s
  886. * @return string
  887. */
  888. function firstChar( $s ) {
  889. preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
  890. '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})/', $s, $matches);
  891. return isset( $matches[1] ) ? $matches[1] : "";
  892. }
  893. function initEncoding() {
  894. # Some languages may have an alternate char encoding option
  895. # (Esperanto X-coding, Japanese furigana conversion, etc)
  896. # If this language is used as the primary content language,
  897. # an override to the defaults can be set here on startup.
  898. }
  899. function recodeForEdit( $s ) {
  900. # For some languages we'll want to explicitly specify
  901. # which characters make it into the edit box raw
  902. # or are converted in some way or another.
  903. # Note that if wgOutputEncoding is different from
  904. # wgInputEncoding, this text will be further converted
  905. # to wgOutputEncoding.
  906. global $wgEditEncoding;
  907. if( $wgEditEncoding == '' or
  908. $wgEditEncoding == 'UTF-8' ) {
  909. return $s;
  910. } else {
  911. return $this->iconv( 'UTF-8', $wgEditEncoding, $s );
  912. }
  913. }
  914. function recodeInput( $s ) {
  915. # Take the previous into account.
  916. global $wgEditEncoding;
  917. if($wgEditEncoding != "") {
  918. $enc = $wgEditEncoding;
  919. } else {
  920. $enc = 'UTF-8';
  921. }
  922. if( $enc == 'UTF-8' ) {
  923. return $s;
  924. } else {
  925. return $this->iconv( $enc, 'UTF-8', $s );
  926. }
  927. }
  928. /**
  929. * For right-to-left language support
  930. *
  931. * @return bool
  932. */
  933. function isRTL() {
  934. $this->load();
  935. return $this->rtl;
  936. }
  937. /**
  938. * A hidden direction mark (LRM or RLM), depending on the language direction
  939. *
  940. * @return string
  941. */
  942. function getDirMark() {
  943. return $this->isRTL() ? "\xE2\x80\x8F" : "\xE2\x80\x8E";
  944. }
  945. /**
  946. * An arrow, depending on the language direction
  947. *
  948. * @return string
  949. */
  950. function getArrow() {
  951. return $this->isRTL() ? '?' : '?';
  952. }
  953. /**
  954. * To allow "foo[[bar]]" to extend the link over the whole word "foobar"
  955. *
  956. * @return bool
  957. */
  958. function linkPrefixExtension() {
  959. $this->load();
  960. return $this->linkPrefixExtension;
  961. }
  962. function &getMagicWords() {
  963. $this->load();
  964. return $this->magicWords;
  965. }
  966. # Fill a MagicWord object with data from here
  967. function getMagic( &$mw ) {
  968. if ( !isset( $this->mMagicExtensions ) ) {
  969. $this->mMagicExtensions = array();
  970. wfRunHooks( 'LanguageGetMagic', array( &$this->mMagicExtensions, $this->getCode() ) );
  971. }
  972. if ( isset( $this->mMagicExtensions[$mw->mId] ) ) {
  973. $rawEntry = $this->mMagicExtensions[$mw->mId];
  974. } else {
  975. $magicWords =& $this->getMagicWords();
  976. if ( isset( $magicWords[$mw->mId] ) ) {
  977. $rawEntry = $magicWords[$mw->mId];
  978. } else {
  979. # Fall back to English if local list is incomplete
  980. $magicWords =& Language::getMagicWords();
  981. $rawEntry = $magicWords[$mw->mId];
  982. }
  983. }
  984. if( !is_array( $rawEntry ) ) {
  985. error_log( "\"$rawEntry\" is not a valid magic thingie for \"$mw->mId\"" );
  986. }
  987. $mw->mCaseSensitive = $rawEntry[0];
  988. $mw->mSynonyms = array_slice( $rawEntry, 1 );
  989. }
  990. /**
  991. * Get special page names, as an associative array
  992. * case folded alias => real name
  993. */
  994. function getSpecialPageAliases() {
  995. $this->load();
  996. if ( !isset( $this->mExtendedSpecialPageAliases ) ) {
  997. $this->mExtendedSpecialPageAliases = $this->specialPageAliases;
  998. wfRunHooks( 'LangugeGetSpecialPageAliases',
  999. array( &$this->mExtendedSpecialPageAliases, $this->getCode() ) );
  1000. }
  1001. return $this->mExtendedSpecialPageAliases;
  1002. }
  1003. /**
  1004. * Italic is unsuitable for some languages
  1005. *
  1006. * @public
  1007. *
  1008. * @param string $text The text to be emphasized.
  1009. * @return string
  1010. */
  1011. function emphasize( $text ) {
  1012. return "<em>$text</em>";
  1013. }
  1014. /**
  1015. * Normally we output all numbers in plain en_US style, that is
  1016. * 293,291.235 for twohundredninetythreethousand-twohundredninetyone
  1017. * point twohundredthirtyfive. However this is not sutable for all
  1018. * languages, some such as Pakaran want ???,???.??? and others such as
  1019. * Icelandic just want to use commas instead of dots, and dots instead
  1020. * of commas like "293.291,235".
  1021. *
  1022. * An example of this function being called:
  1023. * <code>
  1024. * wfMsg( 'message', $wgLang->formatNum( $num ) )
  1025. * </code>
  1026. *
  1027. * See LanguageGu.php for the Gujarati implementation and
  1028. * LanguageIs.php for the , => . and . => , implementation.
  1029. *
  1030. * @todo check if it's viable to use localeconv() for the decimal
  1031. * seperator thing.
  1032. * @public
  1033. * @param mixed $number the string to be formatted, should be an integer or
  1034. * a floating point number.
  1035. * @param bool $nocommafy Set to true for special numbers like dates
  1036. * @return string
  1037. */
  1038. function formatNum( $number, $nocommafy = false ) {
  1039. global $wgTranslateNumerals;
  1040. if (!$nocommafy) {
  1041. $number = $this->commafy($number);
  1042. $s = $this->separatorTransformTable();
  1043. if (!is_null($s)) { $number = strtr($number, $s); }
  1044. }
  1045. if ($wgTranslateNumerals) {
  1046. $s = $this->digitTransformTable();
  1047. if (!is_null($s)) { $number = strtr($number, $s); }
  1048. }
  1049. return $number;
  1050. }
  1051. function parseFormattedNumber( $number ) {
  1052. $s = $this->digitTransformTable();
  1053. if (!is_null($s)) { $number = strtr($number, array_flip($s)); }
  1054. $s = $this->separatorTransformTable();
  1055. if (!is_null($s)) { $number = strtr($number, array_flip($s)); }
  1056. $number = strtr( $number, array (',' => '') );
  1057. return $number;
  1058. }
  1059. /**
  1060. * Adds commas to a given number
  1061. *
  1062. * @param mixed $_
  1063. * @return string
  1064. */
  1065. function commafy($_) {
  1066. return strrev((string)preg_replace('/(\d{3})(?=\d)(?!\d*\.)/','$1,',strrev($_)));
  1067. }
  1068. function digitTransformTable() {
  1069. $this->load();
  1070. return $this->digitTransformTable;
  1071. }
  1072. function separatorTransformTable() {
  1073. $this->load();
  1074. return $this->separatorTransformTable;
  1075. }
  1076. /**
  1077. * For the credit list in includes/Credits.php (action=credits)
  1078. *
  1079. * @param array $l
  1080. * @return string
  1081. */
  1082. function listToText( $l ) {
  1083. $s = '';
  1084. $m = count($l) - 1;
  1085. for ($i = $m; $i >= 0; $i--) {
  1086. if ($i == $m) {
  1087. $s = $l[$i];
  1088. } else if ($i == $m - 1) {
  1089. $s = $l[$i] . ' ' . $this->getMessageFromDB( 'and' ) . ' ' . $s;
  1090. } else {
  1091. $s = $l[$i] . ', ' . $s;
  1092. }
  1093. }
  1094. return $s;
  1095. }
  1096. # Crop a string from the beginning or end to a certain number of bytes.
  1097. # (Bytes are used because our storage has limited byte lengths for some
  1098. # columns in the database.) Multibyte charsets will need to make sure that
  1099. # only whole characters are included!
  1100. #
  1101. # $length does not include the optional ellipsis.
  1102. # If $length is negative, snip from the beginning
  1103. function truncate( $string, $length, $ellipsis = "" ) {
  1104. if( $length == 0 ) {
  1105. return $ellipsis;
  1106. }
  1107. if ( strlen( $string ) <= abs( $length ) ) {
  1108. return $string;
  1109. }
  1110. if( $length > 0 ) {
  1111. $string = substr( $string, 0, $length );
  1112. $char = ord( $string[strlen( $string ) - 1] );
  1113. if ($char >= 0xc0) {
  1114. # We got the first byte only of a multibyte char; remove it.
  1115. $string = substr( $string, 0, -1 );
  1116. } elseif( $char >= 0x80 &&
  1117. preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
  1118. '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m ) ) {
  1119. # We chopped in the middle of a character; remove it
  1120. $string = $m[1];
  1121. }
  1122. return $string . $ellipsis;
  1123. } else {
  1124. $string = substr( $string, $length );
  1125. $char = ord( $string[0] );
  1126. if( $char >= 0x80 && $char < 0xc0 ) {
  1127. # We chopped in the middle of a character; remove the whole thing
  1128. $string = preg_replace( '/^[\x80-\xbf]+/', '', $string );
  1129. }
  1130. return $ellipsis . $string;
  1131. }
  1132. }
  1133. /**
  1134. * Grammatical transformations, needed for inflected languages
  1135. * Invoked by putting {{grammar:case|word}} in a message
  1136. *
  1137. * @param string $word
  1138. * @param string $case
  1139. * @return string
  1140. */
  1141. function convertGrammar( $word, $case ) {
  1142. global $wgGrammarForms;
  1143. if ( isset($wgGrammarForms['en'][$case][$word]) ) {
  1144. return $wgGrammarForms['en'][$case][$word];
  1145. }
  1146. return $word;
  1147. }
  1148. /**
  1149. * Plural form transformations, needed for some languages.
  1150. * For example, where are 3 form of plural in Russian and Polish,
  1151. * depending on "count mod 10". See [[w:Plural]]
  1152. * For English it is pretty simple.
  1153. *
  1154. * Invoked by putting {{plural:count|wordform1|wordform2}}
  1155. * or {{plural:count|wordform1|wordform2|wordform3}}
  1156. *
  1157. * Example: {{plural:{{NUMBEROFARTICLES}}|article|articles}}
  1158. *
  1159. * @param integer $count
  1160. * @param string $wordform1
  1161. * @param string $wordform2
  1162. * @param string $wordform3 (optional)
  1163. * @param string $wordform4 (optional)
  1164. * @param string $wordform5 (optional)
  1165. * @return string
  1166. */
  1167. function convertPlural( $count, $w1, $w2, $w3, $w4, $w5) {
  1168. return $count == '1' ? $w1 : $w2;
  1169. }
  1170. /**
  1171. * For translaing of expiry times
  1172. * @param string The validated block time in English
  1173. * @return Somehow translated block time
  1174. * @see LanguageFi.php for example implementation
  1175. */
  1176. function translateBlockExpiry( $str ) {
  1177. $scBlockExpiryOptions = $this->getMessageFromDB( 'ipboptions' );
  1178. if ( $scBlockExpiryOptions == '-') {
  1179. return $str;
  1180. }
  1181. foreach (explode(',', $scBlockExpiryOptions) as $option) {
  1182. if ( strpos($option, ":") === false )
  1183. continue;
  1184. list($show, $value) = explode(":", $option);
  1185. if ( strcmp ( $str, $value) == 0 )
  1186. return '<span title="' . htmlspecialchars($str). '">' .
  1187. htmlspecialchars( trim( $show ) ) . '</span>';
  1188. }
  1189. return $str;
  1190. }
  1191. /**
  1192. * languages like Chinese need to be segmented in order for the diff
  1193. * to be of any use
  1194. *
  1195. * @param string $text
  1196. * @return string
  1197. */
  1198. function segmentForDiff( $text ) {
  1199. return $text;
  1200. }
  1201. /**
  1202. * and unsegment to show the result
  1203. *
  1204. * @param string $text
  1205. * @return string
  1206. */
  1207. function unsegmentForDiff( $text ) {
  1208. return $text;
  1209. }
  1210. # convert text to different variants of a language.
  1211. function convert( $text, $isTitle = false) {
  1212. return $this->mConverter->convert($text, $isTitle);
  1213. }
  1214. # Convert text from within Parser
  1215. function parserConvert( $text, &$parser ) {
  1216. return $this->mConverter->parserConvert( $text, $parser );
  1217. }
  1218. # Check if this is a language with variants
  1219. function hasVariants(){
  1220. return sizeof($this->getVariants())>1;
  1221. }
  1222. # Put custom tags (e.g. -{ }-) around math to prevent conversion
  1223. function armourMath($text){
  1224. return $this->mConverter->armourMath($text);
  1225. }
  1226. /**
  1227. * Perform output conversion on a string, and encode for safe HTML output.
  1228. * @param string $text
  1229. * @param bool $isTitle -- wtf?
  1230. * @return string
  1231. * @todo this should get integrated somewhere sane
  1232. */
  1233. function convertHtml( $text, $isTitle = false ) {
  1234. return htmlspecialchars( $this->convert( $text, $isTitle ) );
  1235. }
  1236. function convertCategoryKey( $key ) {
  1237. return $this->mConverter->convertCategoryKey( $key );
  1238. }
  1239. /**
  1240. * get the list of variants supported by this langauge
  1241. * see sample implementation in LanguageZh.php
  1242. *
  1243. * @return array an array of language codes
  1244. */
  1245. function getVariants() {
  1246. return $this->mConverter->getVariants();
  1247. }
  1248. function getPreferredVariant( $fromUser = true ) {
  1249. return $this->mConverter->getPreferredVariant( $fromUser );
  1250. }
  1251. /**
  1252. * if a language supports multiple variants, it is
  1253. * possible that non-existing link in one variant
  1254. * actually exists in another variant. this function
  1255. * tries to find it. See e.g. LanguageZh.php
  1256. *
  1257. * @param string $link the name of the link
  1258. * @param mixed $nt the title object of the link
  1259. * @return null the input parameters may be modified upon return
  1260. */
  1261. function findVariantLink( &$link, &$nt ) {
  1262. $this->mConverter->findVariantLink($link, $nt);
  1263. }
  1264. /**
  1265. * If a language supports multiple variants, converts text
  1266. * into an array of all possible variants of the text:
  1267. * 'variant' => text in that variant
  1268. */
  1269. function convertLinkToAllVariants($text){
  1270. return $this->mConverter->convertLinkToAllVariants($text);
  1271. }
  1272. /**
  1273. * returns language specific options used by User::getPageRenderHash()
  1274. * for example, the preferred language variant
  1275. *
  1276. * @return string
  1277. * @public
  1278. */
  1279. function getExtraHashOptions() {
  1280. return $this->mConverter->getExtraHashOptions();
  1281. }
  1282. /**
  1283. * for languages that support multiple variants, the title of an
  1284. * article may be displayed differently in different variants. this
  1285. * function returns the apporiate title defined in the body of the article.
  1286. *
  1287. * @return string
  1288. */
  1289. function getParsedTitle() {
  1290. return $this->mConverter->getParsedTitle();
  1291. }
  1292. /**
  1293. * Enclose a string with the "no conversion" tag. This is used by
  1294. * various functions in the Parser
  1295. *
  1296. * @param string $text text to be tagged for no conversion
  1297. * @return string the tagged text
  1298. */
  1299. function markNoConversion( $text, $noParse=false ) {
  1300. return $this->mConverter->markNoConversion( $text, $noParse );
  1301. }
  1302. /**
  1303. * A regular expression to match legal word-trailing characters
  1304. * which should be merged onto a link of the form [[foo]]bar.
  1305. *
  1306. * @return string
  1307. * @public
  1308. */
  1309. function linkTrail() {
  1310. $this->load();
  1311. return $this->linkTrail;
  1312. }
  1313. function getLangObj() {
  1314. return $this;
  1315. }
  1316. /**
  1317. * Get the RFC 3066 code for this language object
  1318. */
  1319. function getCode() {
  1320. return $this->mCode;
  1321. }
  1322. function setCode( $code ) {
  1323. $this->mCode = $code;
  1324. }
  1325. static function getFileName( $prefix = 'Language', $code, $suffix = '.php' ) {
  1326. return $prefix . str_replace( '-', '_', ucfirst( $code ) ) . $suffix;
  1327. }
  1328. static function getMessagesFileName( $code ) {
  1329. global $IP;
  1330. return self::getFileName( "$IP/languages/messages/Messages", $code, '.php' );
  1331. }
  1332. static function getClassFileName( $code ) {
  1333. global $IP;
  1334. return self::getFileName( "$IP/languages/classes/Language", $code, '.php' );
  1335. }
  1336. static function getLocalisationArray( $code, $disableCache = false ) {
  1337. self::loadLocalisation( $code, $disableCache );
  1338. return self::$mLocalisationCache[$code];
  1339. }
  1340. /**
  1341. * Load localisation data for a given code into the static cache
  1342. *
  1343. * @return array Dependencies, map of filenames to mtimes
  1344. */
  1345. static function loadLocalisation( $code, $disableCache = false ) {
  1346. static $recursionGuard = array();
  1347. global $wgMemc;
  1348. if ( !$code ) {
  1349. throw new MWException( "Invalid language code requested" );
  1350. }
  1351. if ( !$disableCache ) {
  1352. # Try the per-process cache
  1353. if ( isset( self::$mLocalisationCache[$code] ) ) {
  1354. return self::$mLocalisationCache[$code]['deps'];
  1355. }
  1356. wfProfileIn( __METHOD__ );
  1357. # Try the serialized directory
  1358. $cache = wfGetPrecompiledData( self::getFileName( "Messages", $code, '.ser' ) );
  1359. if ( $cache ) {
  1360. self::$mLocalisationCache[$code] = $cache;
  1361. wfDebug( "Language::loadLocalisation(): got localisation for $code from precompiled data file\n" );
  1362. wfProfileOut( __METHOD__ );
  1363. return self::$mLocalisationCache[$code]['deps'];
  1364. }
  1365. # Try the global cache
  1366. $memcKey = wfMemcKey('localisation', $code );
  1367. $cache = $wgMemc->get( $memcKey );
  1368. if ( $cache ) {
  1369. $expired = false;
  1370. # Check file modification times
  1371. foreach ( $cache['deps'] as $file => $mtime ) {
  1372. if ( !file_exists( $file ) || filemtime( $file ) > $mtime ) {
  1373. $expired = true;
  1374. break;
  1375. }
  1376. }
  1377. if ( self::isLocalisationOutOfDate( $cache ) ) {
  1378. $wgMemc->delete( $memcKey );
  1379. $cache = false;
  1380. wfDebug( "Language::loadLocalisation(): localisation cache for $code had expired due to update of $file\n" );
  1381. } else {
  1382. self::$mLocalisationCache[$code] = $cache;
  1383. wfDebug( "Language::loadLocalisation(): got localisation for $code from cache\n" );
  1384. wfProfileOut( __METHOD__ );
  1385. return $cache['deps'];
  1386. }
  1387. }
  1388. } else {
  1389. wfProfileIn( __METHOD__ );
  1390. }
  1391. # Default fallback, may be overridden when the messages file is included
  1392. if ( $code != 'en' ) {
  1393. $fallback = 'en';
  1394. } else {
  1395. $fallback = false;
  1396. }
  1397. # Load the primary localisation from the source file
  1398. $filename = self::getMessagesFileName( $code );
  1399. if ( !file_exists( $filename ) ) {
  1400. wfDebug( "Language::loadLocalisation(): no localisation file for $code, using implicit fallback to en\n" );
  1401. $cache = array();
  1402. $deps = array();
  1403. } else {
  1404. $deps = array( $filename => filemtime( $filename ) );
  1405. require( $filename );
  1406. $cache = compact( self::$mLocalisationKeys );
  1407. wfDebug( "Language::loadLocalisation(): got localisation for $code from source\n" );
  1408. }
  1409. if ( !empty( $fallback ) ) {
  1410. # Load the fallback localisation, with a circular reference guard
  1411. if ( isset( $recursionGuard[$code] ) ) {
  1412. throw new MWException( "Error: Circular fallback reference in language code $code" );
  1413. }
  1414. $recursionGuard[$code] = true;
  1415. $newDeps = self::loadLocalisation( $fallback, $disableCache );
  1416. unset( $recursionGuard[$code] );
  1417. $secondary = self::$mLocalisationCache[$fallback];
  1418. $deps = array_merge( $deps, $newDeps );
  1419. # Merge the fallback localisation with the current localisation
  1420. foreach ( self::$mLocalisationKeys as $key ) {
  1421. if ( isset( $cache[$key] ) ) {
  1422. if ( isset( $secondary[$key] ) ) {
  1423. if ( in_array( $key, self::$mMergeableMapKeys ) ) {
  1424. $cache[$key] = $cache[$key] + $secondary[$key];
  1425. } elseif ( in_array( $key, self::$mMergeableListKeys ) ) {
  1426. $cache[$key] = array_merge( $secondary[$key], $cache[$key] );
  1427. } elseif ( in_array( $key, self::$mMergeableAliasListKeys ) ) {
  1428. $cache[$key] = array_merge_recursive( $cache[$key], $secondary[$key] );
  1429. }
  1430. }
  1431. } else {
  1432. $cache[$key] = $secondary[$key];
  1433. }
  1434. }
  1435. # Merge bookstore lists if requested
  1436. if ( !empty( $cache['bookstoreList']['inherit'] ) ) {
  1437. $cache['bookstoreList'] = array_merge( $cache['bookstoreList'], $secondary['bookstoreList'] );
  1438. }
  1439. if ( isset( $cache['bookstoreList']['inherit'] ) ) {
  1440. unset( $cache['bookstoreList']['inherit'] );
  1441. }
  1442. }
  1443. # Add dependencies to the cache entry
  1444. $cache['deps'] = $deps;
  1445. # Replace spaces with underscores in namespace names
  1446. $cache['namespaceNames'] = str_replace( ' ', '_', $cache['namespaceNames'] );
  1447. # Save to both caches
  1448. self::$mLocalisationCache[$code] = $cache;
  1449. if ( !$disableCache ) {
  1450. $wgMemc->set( $memcKey, $cache );
  1451. }
  1452. wfProfileOut( __METHOD__ );
  1453. return $deps;
  1454. }
  1455. /**
  1456. * Test if a given localisation cache is out of date with respect to the
  1457. * source Messages files. This is done automatically for the global cache
  1458. * in $wgMemc, but is only done on certain occasions for the serialized
  1459. * data file.
  1460. *
  1461. * @param $cache mixed Either a language code or a cache array
  1462. */
  1463. static function isLocalisationOutOfDate( $cache ) {
  1464. if ( !is_array( $cache ) ) {
  1465. self::loadLocalisation( $cache );
  1466. $cache = self::$mLocalisationCache[$cache];
  1467. }
  1468. $expired = false;
  1469. foreach ( $cache['deps'] as $file => $mtime ) {
  1470. if ( !file_exists( $file ) || filemtime( $file ) > $mtime ) {
  1471. $expired = true;
  1472. break;
  1473. }
  1474. }
  1475. return $expired;
  1476. }
  1477. /**
  1478. * Get the fallback for a given language
  1479. */
  1480. static function getFallbackFor( $code ) {
  1481. self::loadLocalisation( $code );
  1482. return self::$mLocalisationCache[$code]['fallback'];
  1483. }
  1484. /**
  1485. * Get all messages for a given language
  1486. */
  1487. static function getMessagesFor( $code ) {
  1488. self::loadLocalisation( $code );
  1489. return self::$mLocalisationCache[$code]['messages'];
  1490. }
  1491. /**
  1492. * Get a message for a given language
  1493. */
  1494. static function getMessageFor( $key, $code ) {
  1495. self::loadLocalisation( $code );
  1496. return isset( self::$mLocalisationCache[$code]['messages'][$key] ) ? self::$mLocalisationCache[$code]['messages'][$key] : null;
  1497. }
  1498. /**
  1499. * Load localisation data for this object
  1500. */
  1501. function load() {
  1502. if ( !$this->mLoaded ) {
  1503. self::loadLocalisation( $this->getCode() );
  1504. $cache =& self::$mLocalisationCache[$this->getCode()];
  1505. foreach ( self::$mLocalisationKeys as $key ) {
  1506. $this->$key = $cache[$key];
  1507. }
  1508. $this->mLoaded = true;
  1509. $this->fixUpSettings();
  1510. }
  1511. }
  1512. /**
  1513. * Do any necessary post-cache-load settings adjustment
  1514. */
  1515. function fixUpSettings() {
  1516. global $wgExtraNamespaces, $wgMetaNamespace, $wgMetaNamespaceTalk, $wgMessageCache,
  1517. $wgNamespaceAliases, $wgAmericanDates;
  1518. wfProfileIn( __METHOD__ );
  1519. if ( $wgExtraNamespaces ) {
  1520. $this->namespaceNames = $wgExtraNamespaces + $this->namespaceNames;
  1521. }
  1522. $this->namespaceNames[NS_PROJECT] = $wgMetaNamespace;
  1523. if ( $wgMetaNamespaceTalk ) {
  1524. $this->namespaceNames[NS_PROJECT_TALK] = $wgMetaNamespaceTalk;
  1525. } else {
  1526. $talk = $this->namespaceNames[NS_PROJECT_TALK];
  1527. $talk = str_replace( '$1', $wgMetaNamespace, $talk );
  1528. # Allow grammar transformations
  1529. # Allowing full message-style parsing would make simple requests
  1530. # such as action=raw much more expensive than they need to be.
  1531. # This will hopefully cover most cases.
  1532. $talk = preg_replace_callback( '/{{grammar:(.*?)\|(.*?)}}/i',
  1533. array( &$this, 'replaceGrammarInNamespace' ), $talk );
  1534. $talk = str_replace( ' ', '_', $talk );
  1535. $this->namespaceNames[NS_PROJECT_TALK] = $talk;
  1536. }
  1537. # The above mixing may leave namespaces out of canonical order.
  1538. # Re-order by namespace ID number...
  1539. ksort( $this->namespaceNames );
  1540. # Put namespace names and aliases into a hashtable.
  1541. # If this is too slow, then we should arrange it so that it is done
  1542. # before caching. The catch is that at pre-cache time, the above
  1543. # class-specific fixup hasn't been done.
  1544. $this->mNamespaceIds = array();
  1545. foreach ( $this->namespaceNames as $index => $name ) {
  1546. $this->mNamespaceIds[$this->lc($name)] = $index;
  1547. }
  1548. if ( $this->namespaceAliases ) {
  1549. foreach ( $this->namespaceAliases as $name => $index ) {
  1550. $this->mNamespaceIds[$this->lc($name)] = $index;
  1551. }
  1552. }
  1553. if ( $wgNamespaceAliases ) {
  1554. foreach ( $wgNamespaceAliases as $name => $index ) {
  1555. $this->mNamespaceIds[$this->lc($name)] = $index;
  1556. }
  1557. }
  1558. if ( $this->defaultDateFormat == 'dmy or mdy' ) {
  1559. $this->defaultDateFormat = $wgAmericanDates ? 'mdy' : 'dmy';
  1560. }
  1561. wfProfileOut( __METHOD__ );
  1562. }
  1563. function replaceGrammarInNamespace( $m ) {
  1564. return $this->convertGrammar( trim( $m[2] ), trim( $m[1] ) );
  1565. }
  1566. static function getCaseMaps() {
  1567. static $wikiUpperChars, $wikiLowerChars;
  1568. if ( isset( $wikiUpperChars ) ) {
  1569. return array( $wikiUpperChars, $wikiLowerChars );
  1570. }
  1571. wfProfileIn( __METHOD__ );
  1572. $arr = wfGetPrecompiledData( 'Utf8Case.ser' );
  1573. if ( $arr === false ) {
  1574. throw new MWException(
  1575. "Utf8Case.ser is missing, please run \"make\" in the serialized directory\n" );
  1576. }
  1577. extract( $arr );
  1578. wfProfileOut( __METHOD__ );
  1579. return array( $wikiUpperChars, $wikiLowerChars );
  1580. }
  1581. }
  1582. ?>