Profiling points. Avoid reparsing of titles which came out of the database. Snip...
[mediawiki.git] / includes / ZhClient.php
blob3982ca6220526eeb553736f34389c8637ca90162
1 <?php
2 /**
3 * Client for querying zhdaemon
5 * @package MediaWiki
6 * @version $Id$
7 */
9 class ZhClient {
10 var $mHost, $mPort, $mFP, $mConnected;
12 /**
13 * Constructor
15 * @access private
17 function ZhClient($host, $port) {
18 $this->mHost = $host;
19 $this->mPort = $port;
20 $this->mConnected = $this->connect();
23 /**
24 * Check if connection to zhdaemon is successful
26 * @access public
28 function isconnected() {
29 return $this->mConnected;
32 /**
33 * Establish conncetion
35 * @access private
37 function connect() {
38 wfSuppressWarnings();
39 $this->mFP = fsockopen($this->mHost, $this->mPort, $errno, $errstr, 30);
40 wfRestoreWarnings();
41 if(!$this->mFP) {
42 return false;
44 return true;
47 /**
48 * Query the daemon and return the result
50 * @access private
52 function query($request) {
53 if(!$this->mConnected)
54 return false;
56 fwrite($this->mFP, $request);
58 $result=fgets($this->mFP, 1024);
60 list($status, $len) = explode(" ", $result);
61 if($status == 'ERROR') {
62 //$len is actually the error code...
63 print "zhdaemon error $len<br />\n";
64 return false;
66 $bytesread=0;
67 $data='';
68 while(!feof($this->mFP) && $bytesread<$len) {
69 $str= fread($this->mFP, $len-$bytesread);
70 $bytesread += strlen($str);
71 $data .= $str;
73 //data should be of length $len. otherwise something is wrong
74 if(strlen($data) != $len)
75 return false;
76 return $data;
79 /**
80 * Convert the input to a different language variant
82 * @param string $text input text
83 * @param string $tolang language variant
84 * @return string the converted text
85 * @access public
87 function convert($text, $tolang) {
88 $len = strlen($text);
89 $q = "CONV $tolang $len\n$text";
90 $result = $this->query($q);
91 if(!$result)
92 $result = $text;
93 return $result;
96 /**
97 * Convert the input to all possible variants
99 * @param string $text input text
100 * @return array langcode => converted_string
101 * @access public
103 function convertToAllVariants($text) {
104 $len = strlen($text);
105 $q = "CONV ALL $len\n$text";
106 $result = $this->query($q);
107 if(!$result)
108 return false;
109 list($infoline, $data) = explode('|', $result);
110 $info = explode(";", $infoline);
111 $ret = array();
112 $i=0;
113 foreach($info as $code => $len) {
114 $ret[strtolower($code)] = substr($data, $i, $len);
115 $i+=$len+1;
117 return $ret;
120 * Perform word segmentation
122 * @param string $text input text
123 * @return string segmented text
124 * @access public
126 function segment($text) {
127 $len = strlen($text);
128 $q = "SEG $len\n$text";
129 $result = $this->query($q);
130 if(!$result) {// fallback to character based segmentation
131 $result = ZhClientFake::segment($text);
133 return $result;
137 * Close the connection
139 * @access public
141 function close() {
142 fclose($this->mFP);
147 class ZhClientFake {
148 function ZhClientFake() {
149 global $wgMemc, $wgDBname;
150 $this->zh2TW = $wgMemc->get($key1 = "$wgDBname:zhConvert:tw");
151 $this->zh2CN = $wgMemc->get($key2 = "$wgDBname:zhConvert:cn");
152 $this->zh2SG = $wgMemc->get($key3 = "$wgDBname:zhConvert:sg");
153 $this->zh2HK = $wgMemc->get($key4 = "$wgDBname:zhConvert:hk");
154 if(empty($this->zh2TW) || empty($this->zh2CN) || empty($this->zh2SG) || empty($this->zh2HK)) {
155 require("includes/ZhConversion.php");
156 $this->zh2TW = $zh2TW;
157 $this->zh2CN = $zh2CN;
158 $this->zh2HK = $zh2HK;
159 $this->zh2SG = $zh2SG;
160 $wgMemc->set($key1, $this->zh2TW);
161 $wgMemc->set($key2, $this->zh2CN);
162 $wgMemc->set($key3, $this->zh2SG);
163 $wgMemc->set($key4, $this->zh2HK);
167 function isconnected() {
168 return true;
172 * Convert to zh-tw
174 * @access private
176 function zh2tw($text) {
177 return strtr($text, $this->zh2TW);
181 * Convert to zh-cn
183 * @access private
185 function zh2cn($text) {
186 return strtr($text, $this->zh2CN);
190 * Convert to zh-sg
192 * @access private
194 function zh2sg($text) {
195 return strtr(strtr($text, $this->zh2CN), $this->zh2SG);
199 * Convert to zh-hk
201 * @access private
203 function zh2hk($text) {
204 return strtr(strtr($text, $this->zh2TW), $this->zh2HK);
208 * Convert the input to a different language variant
210 * @param string $text input text
211 * @param string $tolang language variant
212 * @return string the converted text
213 * @access public
215 function convert($text, $tolang) {
216 $t = '';
217 switch($tolang) {
218 case 'zh-cn':
219 $t = $this->zh2cn($text);
220 break;
221 case 'zh-tw':
222 $t = $this->zh2tw($text);
223 break;
224 case 'zh-sg':
225 $t = $this->zh2sg($text);
226 break;
227 case 'zh-hk':
228 $t = $this->zh2hk($text);
229 break;
230 default:
231 $t = $text;
233 return $t;
236 function convertToAllVariants($text) {
237 $ret = array();
238 $ret['zh-cn'] = $this->zh2cn($text);
239 $ret['zh-tw'] = $this->zh2tw($text);
240 $ret['zh-sg'] = $this->zh2sg($text);
241 $ret['zh-hk'] = $this->zh2hk($text);
242 return $ret;
246 * Perform "fake" word segmentation, i.e. treating each character as a word
248 * @param string $text input text
249 * @return string segmented text
250 * @access public
252 function segment($text) {
253 /* adapted from LanguageZh_cn.stripForSearch()
254 here we will first separate the single characters,
255 and let the caller conver it to hex
257 if( function_exists( 'mb_strtolower' ) ) {
258 return preg_replace(
259 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
260 "' ' .\"$1\"",
261 mb_strtolower( $text ) );
262 } else {
263 global $wikiLowerChars;
264 return preg_replace(
265 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
266 "' ' . strtr( \"\$1\", \$wikiLowerChars )",
267 $text );
272 * Close the fake connection
274 * @access public
276 function close() { }