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