elaborate fix for bug 1156: Block log: missing expiry time. Changed schema for loggin...
[mediawiki.git] / includes / ZhClient.php
blob4f1524f621257639649b713a29a6f8f99b77d77c
1 <?php
2 /**
3 * Client for querying zhdaemon
5 * @package MediaWiki
6 */
8 class ZhClient {
9 var $mHost, $mPort, $mFP, $mConnected;
11 /**
12 * Constructor
14 * @access private
16 function ZhClient($host, $port) {
17 $this->mHost = $host;
18 $this->mPort = $port;
19 $this->mConnected = $this->connect();
22 /**
23 * Check if connection to zhdaemon is successful
25 * @access public
27 function isconnected() {
28 return $this->mConnected;
31 /**
32 * Establish conncetion
34 * @access private
36 function connect() {
37 wfSuppressWarnings();
38 $this->mFP = fsockopen($this->mHost, $this->mPort, $errno, $errstr, 30);
39 wfRestoreWarnings();
40 if(!$this->mFP) {
41 return false;
43 return true;
46 /**
47 * Query the daemon and return the result
49 * @access private
51 function query($request) {
52 if(!$this->mConnected)
53 return false;
55 fwrite($this->mFP, $request);
57 $result=fgets($this->mFP, 1024);
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;
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;
72 //data should be of length $len. otherwise something is wrong
73 if(strlen($data) != $len)
74 return false;
75 return $data;
78 /**
79 * Convert the input to a different language variant
81 * @param string $text input text
82 * @param string $tolang language variant
83 * @return string the converted text
84 * @access public
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;
95 /**
96 * Convert the input to all possible variants
98 * @param string $text input text
99 * @return array langcode => converted_string
100 * @access public
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 $r = $ret[strtolower($code)];
116 $i+=$len;
118 return $ret;
121 * Perform word segmentation
123 * @param string $text input text
124 * @return string segmented text
125 * @access public
127 function segment($text) {
128 $len = strlen($text);
129 $q = "SEG $len\n$text";
130 $result = $this->query($q);
131 if(!$result) {// fallback to character based segmentation
132 $result = ZhClientFake::segment($text);
134 return $result;
138 * Close the connection
140 * @access public
142 function close() {
143 fclose($this->mFP);