]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/ZhClient.php
MediaWiki 1.14.0
[autoinstalls/mediawiki.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         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                 fwrite($this->mFP, $request);
54
55                 $result=fgets($this->mFP, 1024);
56
57                 list($status, $len) = explode(" ", $result);
58                 if($status == 'ERROR') {
59                         //$len is actually the error code...
60                         print "zhdaemon error $len<br />\n";
61                         return false;
62                 }
63                 $bytesread=0;
64                 $data='';
65                 while(!feof($this->mFP) && $bytesread<$len) {
66                         $str= fread($this->mFP, $len-$bytesread);
67                         $bytesread += strlen($str);
68                         $data .= $str;
69                 }
70                 //data should be of length $len. otherwise something is wrong
71                 if(strlen($data) != $len)
72                         return false;
73                 return $data;
74         }
75
76         /**
77          * Convert the input to a different language variant
78          *
79          * @param $text string: input text
80          * @param $tolang string: language variant
81          * @return string the converted text
82          */
83         function convert($text, $tolang) {
84                 $len = strlen($text);
85                 $q = "CONV $tolang $len\n$text";
86                 $result = $this->query($q);
87                 if(!$result)
88                         $result = $text;
89                 return $result;
90         }
91
92         /**
93          * Convert the input to all possible variants
94          *
95          * @param $text string: input text
96          * @return array langcode => converted_string
97          */
98         function convertToAllVariants($text) {
99                 $len = strlen($text);
100                 $q = "CONV ALL $len\n$text";
101                 $result = $this->query($q);
102                 if(!$result)
103                         return false;
104                 list($infoline, $data) = explode('|', $result, 2);
105                 $info = explode(";", $infoline);
106                 $ret = array();
107                 $i=0;
108                 foreach($info as $variant) {
109                         list($code, $len) = explode(' ', $variant);
110                         $ret[strtolower($code)] = substr($data, $i, $len);
111                         $i+=$len;
112                 }
113                 return $ret;
114         }
115         /**
116          * Perform word segmentation
117          *
118          * @param $text string: input text
119          * @return string segmented text
120          */
121         function segment($text) {
122                 $len = strlen($text);
123                 $q = "SEG $len\n$text";
124                 $result = $this->query($q);
125                 if(!$result) {// fallback to character based segmentation
126                         $result = ZhClientFake::segment($text);
127                 }
128                 return $result;
129         }
130
131         /**
132          * Close the connection
133          */
134         function close() {
135                 fclose($this->mFP);
136         }
137 }