phpcs: More require/include is not a function
[lhc/web/wiklou.git] / languages / classes / LanguageShi.php
1 <?php
2 /**
3 * Shilha specific code.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Language
22 */
23
24 require_once __DIR__ . '/../LanguageConverter.php';
25
26 /**
27 * Conversion script between Latin and Tifinagh for Tachelhit.
28 * - Tifinagh -> lowercase Latin
29 * - lowercase/uppercase Latin -> Tifinagh
30 *
31 *
32 * Based on:
33 * - http://en.wikipedia.org/wiki/Shilha_language
34 * - LanguageSr.php
35 *
36 * @ingroup Language
37 */
38 class ShiConverter extends LanguageConverter {
39
40 protected $mDoContentConvert;
41
42 public $mToLatin = array(
43 'ⴰ' => 'a', 'ⴱ' => 'b', 'ⴳ' => 'g', 'ⴷ' => 'd', 'ⴹ' => 'ḍ', 'ⴻ' => 'e',
44 'ⴼ' => 'f', 'ⴽ' => 'k', 'ⵀ' => 'h', 'ⵃ' => 'ḥ', 'ⵄ' => 'ε', 'ⵅ' => 'x',
45 'ⵇ' => 'q', 'ⵉ' => 'i', 'ⵊ' => 'j', 'ⵍ' => 'l', 'ⵎ' => 'm', 'ⵏ' => 'n',
46 'ⵓ' => 'u', 'ⵔ' => 'r', 'ⵕ' => 'ṛ', 'ⵖ' => 'γ', 'ⵙ' => 's', 'ⵚ' => 'ṣ',
47 'ⵛ' => 'š', 'ⵜ' => 't', 'ⵟ' => 'ṭ', 'ⵡ' => 'w', 'ⵢ' => 'y', 'ⵣ' => 'z',
48 'ⵥ' => 'ẓ', 'ⵯ' => 'ʷ', 'ⵖ' => 'ɣ', 'ⵠ' => 'v', 'ⵒ' => 'p',
49 );
50
51 public $mUpperToLowerCaseLatin = array(
52 'A' => 'a', 'B' => 'b', 'C' => 'c', 'D' => 'd', 'E' => 'e',
53 'F' => 'f', 'G' => 'g', 'H' => 'h', 'I' => 'i', 'J' => 'j',
54 'K' => 'k', 'L' => 'l', 'M' => 'm', 'N' => 'n', 'O' => 'o',
55 'P' => 'p', 'Q' => 'q', 'R' => 'r', 'S' => 's', 'T' => 't',
56 'U' => 'u', 'V' => 'v', 'W' => 'w', 'X' => 'x', 'Y' => 'y',
57 'Z' => 'z', 'Ɣ' => 'ɣ',
58 );
59
60 public $mToTifinagh = array(
61 'a' => 'ⴰ', 'b' => 'ⴱ', 'g' => 'ⴳ', 'd' => 'ⴷ', 'ḍ' => 'ⴹ', 'e' => 'ⴻ',
62 'f' => 'ⴼ', 'k' => 'ⴽ', 'h' => 'ⵀ', 'ḥ' => 'ⵃ', 'ε' => 'ⵄ', 'x' => 'ⵅ',
63 'q' => 'ⵇ', 'i' => 'ⵉ', 'j' => 'ⵊ', 'l' => 'ⵍ', 'm' => 'ⵎ', 'n' => 'ⵏ',
64 'u' => 'ⵓ', 'r' => 'ⵔ', 'ṛ' => 'ⵕ', 'γ' => 'ⵖ', 's' => 'ⵙ', 'ṣ' => 'ⵚ',
65 'š' => 'ⵛ', 't' => 'ⵜ', 'ṭ' => 'ⵟ', 'w' => 'ⵡ', 'y' => 'ⵢ', 'z' => 'ⵣ',
66 'ẓ' => 'ⵥ', 'ʷ' => 'ⵯ', 'ɣ' => 'ⵖ', 'v' => 'ⵠ', 'p' => 'ⵒ',
67 );
68
69 function loadDefaultTables() {
70 $this->mTables = array(
71 'lowercase' => new ReplacementArray( $this->mUpperToLowerCaseLatin ),
72 'shi-tfng' => new ReplacementArray( $this->mToTifinagh ),
73 'shi-latn' => new ReplacementArray( $this->mToLatin ),
74 'shi' => new ReplacementArray()
75 );
76 }
77
78 /**
79 * rules should be defined as -{Tifinagh | Latin-} -or-
80 * -{code:text | code:text | ...}-
81 * update: delete all rule parsing because it's not used
82 * currently, and just produces a couple of bugs
83 *
84 * @param $rule string
85 * @param $flags array
86 * @return array
87 */
88 function parseManualRule( $rule, $flags = array() ) {
89 if ( in_array( 'T', $flags ) ) {
90 return parent::parseManualRule( $rule, $flags );
91 }
92
93 $carray = array();
94 // otherwise ignore all formatting
95 foreach ( $this->mVariants as $v ) {
96 $carray[$v] = $rule;
97 }
98
99 return $carray;
100 }
101
102 /**
103 * Do not convert content on talk pages
104 *
105 * @param $text string
106 * @param $parser Parser
107 * @return string
108 */
109 function parserConvert( $text, &$parser ) {
110 $this->mDoContentConvert = !( is_object( $parser->getTitle() ) && $parser->getTitle()->isTalkPage() );
111
112 return parent::parserConvert( $text, $parser );
113 }
114
115 /**
116 * A function wrapper:
117 * - if there is no selected variant, leave the link
118 * names as they were
119 * - do not try to find variants for usernames
120 *
121 * @param $link string
122 * @param $nt Title
123 * @param $ignoreOtherCond bool
124 */
125 function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
126 // check for user namespace
127 if ( is_object( $nt ) ) {
128 $ns = $nt->getNamespace();
129 if ( $ns == NS_USER || $ns == NS_USER_TALK ) {
130 return;
131 }
132 }
133
134 $oldlink = $link;
135 parent::findVariantLink( $link, $nt, $ignoreOtherCond );
136 if ( $this->getPreferredVariant() == $this->mMainLanguageCode ) {
137 $link = $oldlink;
138 }
139 }
140
141 /**
142 * An ugly function wrapper for parsing Image titles
143 * (to prevent image name conversion)
144 *
145 * @param $text string
146 * @param $toVariant bool
147 *
148 * @return string
149 */
150 function autoConvert( $text, $toVariant = false ) {
151 global $wgTitle;
152 if ( is_object( $wgTitle ) && $wgTitle->getNamespace() == NS_FILE ) {
153 $imagename = $wgTitle->getNsText();
154 if ( preg_match( "/^$imagename:/", $text ) ) {
155 return $text;
156 }
157 }
158 return parent::autoConvert( $text, $toVariant );
159 }
160
161 /**
162 * It translates text into variant
163 *
164 * @param $text string
165 * @param $toVariant string
166 *
167 * @return string
168 */
169 function translate( $text, $toVariant ) {
170 // If $text is empty or only includes spaces, do nothing
171 // Otherwise translate it
172 if ( trim( $text ) ) {
173 $this->loadTables();
174 // To Tifinagh, first translate uppercase to lowercase Latin
175 if ( $toVariant == 'shi-tfng' ) {
176 $text = $this->mTables['lowercase']->replace( $text );
177 }
178 $text = $this->mTables[$toVariant]->replace( $text );
179 }
180 return $text;
181 }
182 }
183
184 /**
185 * Tachelhit
186 *
187 * @ingroup Language
188 */
189 class LanguageShi extends Language {
190 function __construct() {
191 global $wgHooks;
192
193 parent::__construct();
194
195 $variants = array( 'shi', 'shi-tfng', 'shi-latn' );
196 $variantfallbacks = array(
197 'shi' => 'shi-tfng',
198 'shi-tfng' => 'shi',
199 'shi-latn' => 'shi',
200 );
201
202 $flags = array();
203 $this->mConverter = new ShiConverter( $this, 'shi', $variants, $variantfallbacks, $flags );
204 $wgHooks['PageContentSaveComplete'][] = $this->mConverter;
205 }
206 }