Fix autonumbering that was broke following bug 787 fixing
[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 $this->mFP = fsockopen($this->mHost, $this->mPort, $errno, $errstr, 30);
42 wfRestoreWarnings();
43 if(!$this->mFP) {
44 return false;
45 }
46 return true;
47 }
48
49 /**
50 * Query the daemon and return the result
51 *
52 * @access private
53 */
54 function query($request) {
55 if(!$this->mConnected)
56 return false;
57
58 fwrite($this->mFP, $request);
59
60 $result=fgets($this->mFP, 1024);
61
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;
67 }
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;
74 }
75 //data should be of length $len. otherwise something is wrong
76 if(strlen($data) != $len)
77 return false;
78 return $data;
79 }
80
81 /**
82 * Convert the input to a different language variant
83 *
84 * @param string $text input text
85 * @param string $tolang language variant
86 * @return string the converted text
87 * @access public
88 */
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;
96 }
97
98 /**
99 * Convert the input to all possible variants
100 *
101 * @param string $text input text
102 * @return array langcode => converted_string
103 * @access public
104 */
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;
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 ?>