LanguageSr.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. require_once( dirname(__FILE__).'/../LanguageConverter.php' );
  3. require_once( dirname(__FILE__).'/LanguageSr_ec.php' );
  4. require_once( dirname(__FILE__).'/LanguageSr_el.php' );
  5. /**
  6. * There are two levels of conversion for Serbian: the script level
  7. * (Cyrillics <-> Latin), and the variant level (ekavian
  8. * <->iyekavian). The two are orthogonal. So we really only need two
  9. * dictionaries: one for Cyrillics and Latin, and one for ekavian and
  10. * iyekavian.
  11. *
  12. * @ingroup Language
  13. */
  14. class SrConverter extends LanguageConverter {
  15. var $mToLatin = array(
  16. 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd',
  17. 'ђ' => 'đ', 'е' => 'e', 'ж' => 'ž', 'з' => 'z', 'и' => 'i',
  18. 'ј' => 'j', 'к' => 'k', 'л' => 'l', 'љ' => 'lj', 'м' => 'm',
  19. 'н' => 'n', 'њ' => 'nj', 'о' => 'o', 'п' => 'p', 'р' => 'r',
  20. 'с' => 's', 'т' => 't', 'ћ' => 'ć', 'у' => 'u', 'ф' => 'f',
  21. 'х' => 'h', 'ц' => 'c', 'ч' => 'č', 'џ' => 'dž', 'ш' => 'š',
  22. 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D',
  23. 'Ђ' => 'Đ', 'Е' => 'E', 'Ж' => 'Ž', 'З' => 'Z', 'И' => 'I',
  24. 'Ј' => 'J', 'К' => 'K', 'Л' => 'L', 'Љ' => 'Lj', 'М' => 'M',
  25. 'Н' => 'N', 'Њ' => 'Nj', 'О' => 'O', 'П' => 'P', 'Р' => 'R',
  26. 'С' => 'S', 'Т' => 'T', 'Ћ' => 'Ć', 'У' => 'U', 'Ф' => 'F',
  27. 'Х' => 'H', 'Ц' => 'C', 'Ч' => 'Č', 'Џ' => 'Dž', 'Ш' => 'Š',
  28. );
  29. var $mToCyrillics = array(
  30. 'a' => 'а', 'b' => 'б', 'c' => 'ц', 'č' => 'ч', 'ć' => 'ћ',
  31. 'd' => 'д', 'dž' => 'џ', 'đ' => 'ђ', 'e' => 'е', 'f' => 'ф',
  32. 'g' => 'г', 'h' => 'х', 'i' => 'и', 'j' => 'ј', 'k' => 'к',
  33. 'l' => 'л', 'lj' => 'љ', 'm' => 'м', 'n' => 'н', 'nj' => 'њ',
  34. 'o' => 'о', 'p' => 'п', 'r' => 'р', 's' => 'с', 'š' => 'ш',
  35. 't' => 'т', 'u' => 'у', 'v' => 'в', 'z' => 'з', 'ž' => 'ж',
  36. 'A' => 'А', 'B' => 'Б', 'C' => 'Ц', 'Č' => 'Ч', 'Ć' => 'Ћ',
  37. 'D' => 'Д', 'Dž' => 'Џ', 'Đ' => 'Ђ', 'E' => 'Е', 'F' => 'Ф',
  38. 'G' => 'Г', 'H' => 'Х', 'I' => 'И', 'J' => 'Ј', 'K' => 'К',
  39. 'L' => 'Л', 'LJ' => 'Љ', 'M' => 'М', 'N' => 'Н', 'NJ' => 'Њ',
  40. 'O' => 'О', 'P' => 'П', 'R' => 'Р', 'S' => 'С', 'Š' => 'Ш',
  41. 'T' => 'Т', 'U' => 'У', 'V' => 'В', 'Z' => 'З', 'Ž' => 'Ж',
  42. 'DŽ' => 'Џ', 'd!ž' => 'дж', 'D!ž'=> 'Дж', 'D!Ž'=> 'ДЖ',
  43. 'Lj' => 'Љ', 'l!j' => 'лј', 'L!j'=> 'Лј', 'L!J'=> 'ЛЈ',
  44. 'Nj' => 'Њ', 'n!j' => 'нј', 'N!j'=> 'Нј', 'N!J'=> 'НЈ'
  45. );
  46. function loadDefaultTables() {
  47. $this->mTables = array(
  48. 'sr-ec' => new ReplacementArray( $this->mToCyrillics ),
  49. 'sr-el' => new ReplacementArray( $this->mToLatin),
  50. 'sr' => new ReplacementArray()
  51. );
  52. }
  53. /* rules should be defined as -{ekavian | iyekavian-} -or-
  54. -{code:text | code:text | ...}-
  55. update: delete all rule parsing because it's not used
  56. currently, and just produces a couple of bugs
  57. */
  58. function parseManualRule($rule, $flags=array()) {
  59. if(in_array('T',$flags)){
  60. return parent::parseManualRule($rule, $flags);
  61. }
  62. // otherwise ignore all formatting
  63. foreach($this->mVariants as $v) {
  64. $carray[$v] = $rule;
  65. }
  66. return $carray;
  67. }
  68. // Do not convert content on talk pages
  69. function parserConvert( $text, &$parser ){
  70. if(is_object($parser->getTitle() ) && $parser->getTitle()->isTalkPage())
  71. $this->mDoContentConvert=false;
  72. else
  73. $this->mDoContentConvert=true;
  74. return parent::parserConvert($text, $parser );
  75. }
  76. /*
  77. * A function wrapper:
  78. * - if there is no selected variant, leave the link
  79. * names as they were
  80. * - do not try to find variants for usernames
  81. */
  82. function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
  83. // check for user namespace
  84. if(is_object($nt)){
  85. $ns = $nt->getNamespace();
  86. if($ns==NS_USER || $ns==NS_USER_TALK)
  87. return;
  88. }
  89. $oldlink=$link;
  90. parent::findVariantLink( $link, $nt, $ignoreOtherCond );
  91. if($this->getPreferredVariant()==$this->mMainLanguageCode)
  92. $link=$oldlink;
  93. }
  94. /*
  95. * We want our external link captions to be converted in variants,
  96. * so we return the original text instead -{$text}-, except for URLs
  97. */
  98. function markNoConversion($text, $noParse=false) {
  99. if($noParse || preg_match("/^https?:\/\/|ftp:\/\/|irc:\/\//",$text))
  100. return parent::markNoConversion($text);
  101. return $text;
  102. }
  103. /*
  104. * An ugly function wrapper for parsing Image titles
  105. * (to prevent image name conversion)
  106. */
  107. function autoConvert($text, $toVariant=false) {
  108. global $wgTitle;
  109. if(is_object($wgTitle) && $wgTitle->getNameSpace()==NS_FILE){
  110. $imagename = $wgTitle->getNsText();
  111. if(preg_match("/^$imagename:/",$text)) return $text;
  112. }
  113. return parent::autoConvert($text,$toVariant);
  114. }
  115. /**
  116. * It translates text into variant, specials:
  117. * - ommiting roman numbers
  118. */
  119. function translate($text, $toVariant){
  120. $breaks = '[^\w\x80-\xff]';
  121. // regexp for roman numbers
  122. $roman = 'M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})';
  123. $reg = '/^'.$roman.'$|^'.$roman.$breaks.'|'.$breaks.$roman.'$|'.$breaks.$roman.$breaks.'/';
  124. $matches = preg_split($reg, $text, -1, PREG_SPLIT_OFFSET_CAPTURE);
  125. $m = array_shift($matches);
  126. if( !isset( $this->mTables[$toVariant] ) ) {
  127. throw new MWException( "Broken variant table: " . implode( ',', array_keys( $this->mTables ) ) );
  128. }
  129. $ret = $this->mTables[$toVariant]->replace( $m[0] );
  130. $mstart = $m[1]+strlen($m[0]);
  131. foreach($matches as $m) {
  132. $ret .= substr($text, $mstart, $m[1]-$mstart);
  133. $ret .= parent::translate($m[0], $toVariant);
  134. $mstart = $m[1] + strlen($m[0]);
  135. }
  136. return $ret;
  137. }
  138. }
  139. /**
  140. * @ingroup Language
  141. */
  142. class LanguageSr extends LanguageSr_ec {
  143. function __construct() {
  144. global $wgHooks;
  145. parent::__construct();
  146. $variants = array('sr', 'sr-ec', 'sr-el');
  147. $variantfallbacks = array(
  148. 'sr' => 'sr-ec',
  149. 'sr-ec' => 'sr',
  150. 'sr-el' => 'sr',
  151. );
  152. $marker = array();//don't mess with these, leave them as they are
  153. $flags = array(
  154. 'S' => 'S', 'писмо' => 'S', 'pismo' => 'S',
  155. 'W' => 'W', 'реч' => 'W', 'reč' => 'W', 'ријеч' => 'W', 'riječ' => 'W'
  156. );
  157. $this->mConverter = new SrConverter($this, 'sr', $variants, $variantfallbacks, $marker, $flags);
  158. $wgHooks['ArticleSaveComplete'][] = $this->mConverter;
  159. }
  160. }