* (bug 6029) Improvement to German localisation (de)
[mediawiki.git] / includes / ZhClient.php
blob0451ce8143ebb09b85c1f9938d9477cbc822faf8
1 <?php
2 /**
3 * @package MediaWiki
4 */
6 /**
7 * Client for querying zhdaemon
9 * @package MediaWiki
11 class ZhClient {
12 var $mHost, $mPort, $mFP, $mConnected;
14 /**
15 * Constructor
17 * @access private
19 function ZhClient($host, $port) {
20 $this->mHost = $host;
21 $this->mPort = $port;
22 $this->mConnected = $this->connect();
25 /**
26 * Check if connection to zhdaemon is successful
28 * @access public
30 function isconnected() {
31 return $this->mConnected;
34 /**
35 * Establish conncetion
37 * @access private
39 function connect() {
40 wfSuppressWarnings();
41 $this->mFP = fsockopen($this->mHost, $this->mPort, $errno, $errstr, 30);
42 wfRestoreWarnings();
43 if(!$this->mFP) {
44 return false;
46 return true;
49 /**
50 * Query the daemon and return the result
52 * @access private
54 function query($request) {
55 if(!$this->mConnected)
56 return false;
58 fwrite($this->mFP, $request);
60 $result=fgets($this->mFP, 1024);
62 list($status, $len) = explode(" ", $result);
63 if($status == 'ERROR') {
64 //$len is actually the error code...
65 print "zhdaemon error $len<br />\n";
66 return false;
68 $bytesread=0;
69 $data='';
70 while(!feof($this->mFP) && $bytesread<$len) {
71 $str= fread($this->mFP, $len-$bytesread);
72 $bytesread += strlen($str);
73 $data .= $str;
75 //data should be of length $len. otherwise something is wrong
76 if(strlen($data) != $len)
77 return false;
78 return $data;
81 /**
82 * Convert the input to a different language variant
84 * @param string $text input text
85 * @param string $tolang language variant
86 * @return string the converted text
87 * @access public
89 function convert($text, $tolang) {
90 $len = strlen($text);
91 $q = "CONV $tolang $len\n$text";
92 $result = $this->query($q);
93 if(!$result)
94 $result = $text;
95 return $result;
98 /**
99 * Convert the input to all possible variants
101 * @param string $text input text
102 * @return array langcode => converted_string
103 * @access public
105 function convertToAllVariants($text) {
106 $len = strlen($text);
107 $q = "CONV ALL $len\n$text";
108 $result = $this->query($q);
109 if(!$result)
110 return false;
111 list($infoline, $data) = explode('|', $result, 2);
112 $info = explode(";", $infoline);
113 $ret = array();
114 $i=0;
115 foreach($info as $variant) {
116 list($code, $len) = explode(' ', $variant);
117 $ret[strtolower($code)] = substr($data, $i, $len);
118 $r = $ret[strtolower($code)];
119 $i+=$len;
121 return $ret;
124 * Perform word segmentation
126 * @param string $text input text
127 * @return string segmented text
128 * @access public
130 function segment($text) {
131 $len = strlen($text);
132 $q = "SEG $len\n$text";
133 $result = $this->query($q);
134 if(!$result) {// fallback to character based segmentation
135 $result = ZhClientFake::segment($text);
137 return $result;
141 * Close the connection
143 * @access public
145 function close() {
146 fclose($this->mFP);