Capitalization fix in memcached setting
[lhc/web/wiklou.git] / includes / ZhClient.php
1 <?php
2 /**
3 * Client for querying zhdaemon
4 *
5 * @package MediaWiki
6 * @version $Id$
7 */
8
9 class ZhClient {
10 var $mHost, $mPort, $mFP, $mConnected;
11
12 /**
13 * Constructor
14 *
15 * @access private
16 */
17 function ZhClient($host, $port) {
18 $this->mHost = $host;
19 $this->mPort = $port;
20 $this->mConnected = $this->connect();
21 }
22
23 /**
24 * Check if connection to zhdaemon is successful
25 *
26 * @access public
27 */
28 function isconnected() {
29 return $this->mConnected;
30 }
31
32 /**
33 * Establish conncetion
34 *
35 * @access private
36 */
37 function connect() {
38 $this->mFP = fsockopen($this->mHost, $this->mPort, $errno, $errstr, 30);
39 if(!$this->mFP) {
40 return false;
41 }
42 return true;
43 }
44
45 /**
46 * Query the daemon and return the result
47 *
48 * @access private
49 */
50 function query($request) {
51 if(!$this->mConnected)
52 return false;
53
54 fwrite($this->mFP, $request);
55
56 $result=fgets($this->mFP, 1024);
57
58 list($status, $len) = explode(" ", $result);
59 if($status == 'ERROR') {
60 //$len is actually the error code...
61 print "zhdaemon error $len<br />\n";
62 return false;
63 }
64 $bytesread=0;
65 $data='';
66 while(!feof($this->mFP) && $bytesread<$len) {
67 $str= fread($this->mFP, $len-$bytesread);
68 $bytesread += strlen($str);
69 $data .= $str;
70 }
71 return $data;
72 }
73
74 /**
75 * Convert the input to a different language variant
76 *
77 * @param string $text input text
78 * @param string $tolang language variant
79 * @return string the converted text
80 * @access public
81 */
82 function convert($text, $tolang) {
83 $len = strlen($text);
84 $q = "CONV $tolang $len\n$text";
85 $result = $this->query($q);
86 if(!$result)
87 $result = $text;
88 return $result;
89 }
90
91 /**
92 * Perform word segmentation
93 *
94 * @param string $text input text
95 * @return string segmented text
96 * @access public
97 */
98 function segment($text) {
99 $len = strlen($text);
100 $q = "SEG $len\n$text";
101 $result = $this->query($q);
102 if(!$result)
103 $result = $text;
104 return $result;
105 }
106
107 /**
108 * Close the connection
109 *
110 * @access public
111 */
112 function close() {
113 fclose($this->mFP);
114 }
115 }
116
117
118 class ZhClientFake {
119
120 function ZhClientFake() {
121 global $wgMemc, $wgDBname;
122 $this->zh2TW = $wgMemc->get($key1 = "$wgDBname:zhConvert:tw");
123 $this->zh2CN = $wgMemc->get($key2 = "$wgDBname:zhConvert:cn");
124 $this->zh2SG = $wgMemc->get($key3 = "$wgDBname:zhConvert:sg");
125 $this->zh2HK = $wgMemc->get($key4 = "$wgDBname:zhConvert:hk");
126 if(empty($this->zh2TW) || empty($this->zh2CN) || empty($this->zh2SG) || empty($this->zh2HK)) {
127 require_once("includes/ZhConversion.php");
128 $this->zh2TW = $zh2TW;
129 $this->zh2CN = $zh2CN;
130 $this->zh2HK = $zh2HK;
131 $this->zh2SG = $zh2SG;
132 $wgMemc->set($key1, $this->zh2TW);
133 $wgMemc->set($key2, $this->zh2CN);
134 $wgMemc->set($key3, $this->zh2SG);
135 $wgMemc->set($key4, $this->zh2HK);
136 }
137 }
138
139 function isconnected() {
140 return true;
141 }
142
143 /**
144 * Convert to zh-tw
145 *
146 * @access private
147 */
148 function zh2tw($text) {
149 return strtr($text, $this->zh2TW);
150 }
151
152 /**
153 * Convert to zh-cn
154 *
155 * @access private
156 */
157 function zh2cn($text) {
158 return strtr($text, $this->zh2CN);
159 }
160
161 /**
162 * Convert to zh-sg
163 *
164 * @access private
165 */
166 function zh2sg($text) {
167 return strtr(strtr($text, $this->zh2CN), $this->zh2SG);
168 }
169
170 /**
171 * Convert to zh-hk
172 *
173 * @access private
174 */
175 function zh2hk($text) {
176 return strtr(strtr($text, $this->zh2TW), $this->zh2HK);
177 }
178
179 /**
180 * Convert the input to a different language variant
181 *
182 * @param string $text input text
183 * @param string $tolang language variant
184 * @return string the converted text
185 * @access public
186 */
187 function convert($text, $tolang) {
188 $t = '';
189 switch($tolang) {
190 case 'zh-cn':
191 $t = $this->zh2cn($text);
192 break;
193 case 'zh-tw':
194 $t = $this->zh2tw($text);
195 break;
196 case 'zh-sg':
197 $t = $this->zh2sg($text);
198 break;
199 case 'zh-hk':
200 $t = $this->zh2hk($text);
201 break;
202 default:
203 $t = $text;
204 }
205 return $t;
206 }
207
208 /**
209 * Perform "fake" word segmentation, i.e. treating each character as a word
210 *
211 * @param string $text input text
212 * @return string segmented text
213 * @access public
214 */
215 function segment($text) {
216 /* copied from LanguageZh_cn.stripForSearch() */
217 if( function_exists( 'mb_strtolower' ) ) {
218 return preg_replace(
219 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
220 "' U8' . bin2hex( \"$1\" )",
221 mb_strtolower( $string ) );
222 } else {
223 global $wikiLowerChars;
224 return preg_replace(
225 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
226 "' U8' . bin2hex( strtr( \"\$1\", \$wikiLowerChars ) )",
227 $string );
228 }
229 }
230
231 /**
232 * Close the fake connection
233 *
234 * @access public
235 */
236 function close() { }
237 }
238
239 ?>