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