Followup r78924: keep track of exception/warning comments separately, to prevent...
[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 __construct($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 function isconnected() {
25 return $this->mConnected;
26 }
27
28 /**
29 * Establish conncetion
30 *
31 * @access private
32 */
33 function connect() {
34 wfSuppressWarnings();
35 $errno = $errstr = '';
36 $this->mFP = fsockopen($this->mHost, $this->mPort, $errno, $errstr, 30);
37 wfRestoreWarnings();
38 if ( !$this->mFP ) {
39 return false;
40 }
41 return true;
42 }
43
44 /**
45 * Query the daemon and return the result
46 *
47 * @access private
48 */
49 function query($request) {
50 if ( !$this->mConnected ) {
51 return false;
52 }
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 //data should be of length $len. otherwise something is wrong
72 if ( strlen($data) != $len ) {
73 return false;
74 }
75 return $data;
76 }
77
78 /**
79 * Convert the input to a different language variant
80 *
81 * @param $text string: input text
82 * @param $tolang string: language variant
83 * @return string the converted text
84 */
85 function convert($text, $tolang) {
86 $len = strlen($text);
87 $q = "CONV $tolang $len\n$text";
88 $result = $this->query($q);
89 if ( !$result ) {
90 $result = $text;
91 }
92 return $result;
93 }
94
95 /**
96 * Convert the input to all possible variants
97 *
98 * @param $text string: input text
99 * @return array langcode => converted_string
100 */
101 function convertToAllVariants($text) {
102 $len = strlen($text);
103 $q = "CONV ALL $len\n$text";
104 $result = $this->query($q);
105 if ( !$result ) {
106 return false;
107 }
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 $text string: input text
123 * @return string segmented text
124 */
125 function segment($text) {
126 $len = strlen($text);
127 $q = "SEG $len\n$text";
128 $result = $this->query($q);
129 if ( !$result ) {// fallback to character based segmentation
130 $result = $this->segment($text);
131 }
132 return $result;
133 }
134
135 /**
136 * Close the connection
137 */
138 function close() {
139 fclose($this->mFP);
140 }
141 }