API: Requesting a token you aren't allowed to request no longer dies with an error...
[lhc/web/wiklou.git] / includes / ZhClient.php
1 <?php
2
3 /**
4 * Client for querying zhdaemon
5 *
6 */
7 class ZhClient {
8 var $mHost, $mPort, $mFP, $mConnected;
9
10 /**
11 * Constructor
12 *
13 * @access private
14 */
15 function ZhClient($host, $port) {
16 $this->mHost = $host;
17 $this->mPort = $port;
18 $this->mConnected = $this->connect();
19 }
20
21 /**
22 * Check if connection to zhdaemon is successful
23 *
24 * @access public
25 */
26 function isconnected() {
27 return $this->mConnected;
28 }
29
30 /**
31 * Establish conncetion
32 *
33 * @access private
34 */
35 function connect() {
36 wfSuppressWarnings();
37 $errno = $errstr = '';
38 $this->mFP = fsockopen($this->mHost, $this->mPort, $errno, $errstr, 30);
39 wfRestoreWarnings();
40 if(!$this->mFP) {
41 return false;
42 }
43 return true;
44 }
45
46 /**
47 * Query the daemon and return the result
48 *
49 * @access private
50 */
51 function query($request) {
52 if(!$this->mConnected)
53 return false;
54
55 fwrite($this->mFP, $request);
56
57 $result=fgets($this->mFP, 1024);
58
59 list($status, $len) = explode(" ", $result);
60 if($status == 'ERROR') {
61 //$len is actually the error code...
62 print "zhdaemon error $len<br />\n";
63 return false;
64 }
65 $bytesread=0;
66 $data='';
67 while(!feof($this->mFP) && $bytesread<$len) {
68 $str= fread($this->mFP, $len-$bytesread);
69 $bytesread += strlen($str);
70 $data .= $str;
71 }
72 //data should be of length $len. otherwise something is wrong
73 if(strlen($data) != $len)
74 return false;
75 return $data;
76 }
77
78 /**
79 * Convert the input to a different language variant
80 *
81 * @param string $text input text
82 * @param string $tolang language variant
83 * @return string the converted text
84 * @access public
85 */
86 function convert($text, $tolang) {
87 $len = strlen($text);
88 $q = "CONV $tolang $len\n$text";
89 $result = $this->query($q);
90 if(!$result)
91 $result = $text;
92 return $result;
93 }
94
95 /**
96 * Convert the input to all possible variants
97 *
98 * @param string $text input text
99 * @return array langcode => converted_string
100 * @access public
101 */
102 function convertToAllVariants($text) {
103 $len = strlen($text);
104 $q = "CONV ALL $len\n$text";
105 $result = $this->query($q);
106 if(!$result)
107 return false;
108 list($infoline, $data) = explode('|', $result, 2);
109 $info = explode(";", $infoline);
110 $ret = array();
111 $i=0;
112 foreach($info as $variant) {
113 list($code, $len) = explode(' ', $variant);
114 $ret[strtolower($code)] = substr($data, $i, $len);
115 $i+=$len;
116 }
117 return $ret;
118 }
119 /**
120 * Perform word segmentation
121 *
122 * @param string $text input text
123 * @return string segmented text
124 * @access public
125 */
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);
132 }
133 return $result;
134 }
135
136 /**
137 * Close the connection
138 *
139 * @access public
140 */
141 function close() {
142 fclose($this->mFP);
143 }
144 }