Add support for Number grouping(commafy) based on CLDR number grouping patterns like...
[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 * @param $host
13 * @param $port
14 *
15 * @return ZhClient
16 */
17 function __construct( $host, $port ) {
18 $this->mHost = $host;
19 $this->mPort = $port;
20 $this->mConnected = $this->connect();
21 }
22
23 /**
24 * Check if connection to zhdaemon is successful
25 *
26 * @return bool
27 */
28 function isconnected() {
29 return $this->mConnected;
30 }
31
32 /**
33 * Establish conncetion
34 *
35 * @access private
36 *
37 * @return bool
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 * @return string
56 */
57 function query( $request ) {
58 if ( !$this->mConnected ) {
59 return false;
60 }
61
62 fwrite( $this->mFP, $request );
63
64 $result = fgets( $this->mFP, 1024 );
65
66 list( $status, $len ) = explode( ' ', $result );
67 if( $status == 'ERROR' ) {
68 // $len is actually the error code...
69 print "zhdaemon error $len<br />\n";
70 return false;
71 }
72 $bytesread = 0;
73 $data = '';
74 while( !feof( $this->mFP ) && $bytesread < $len ) {
75 $str = fread( $this->mFP, $len - $bytesread );
76 $bytesread += strlen( $str );
77 $data .= $str;
78 }
79 // data should be of length $len. otherwise something is wrong
80 if ( strlen( $data ) != $len ) {
81 return false;
82 }
83 return $data;
84 }
85
86 /**
87 * Convert the input to a different language variant
88 *
89 * @param $text String: input text
90 * @param $tolang String: language variant
91 * @return string the converted text
92 */
93 function convert( $text, $tolang ) {
94 $len = strlen( $text );
95 $q = "CONV $tolang $len\n$text";
96 $result = $this->query( $q );
97 if ( !$result ) {
98 $result = $text;
99 }
100 return $result;
101 }
102
103 /**
104 * Convert the input to all possible variants
105 *
106 * @param $text String: input text
107 * @return array langcode => converted_string
108 */
109 function convertToAllVariants( $text ) {
110 $len = strlen( $text );
111 $q = "CONV ALL $len\n$text";
112 $result = $this->query( $q );
113 if ( !$result ) {
114 return false;
115 }
116 list( $infoline, $data ) = explode( '|', $result, 2 );
117 $info = explode( ';', $infoline );
118 $ret = array();
119 $i = 0;
120 foreach( $info as $variant ) {
121 list( $code, $len ) = explode( ' ', $variant );
122 $ret[strtolower( $code )] = substr( $data, $i, $len );
123 $i += $len;
124 }
125 return $ret;
126 }
127 /**
128 * Perform word segmentation
129 *
130 * @param $text String: input text
131 * @return string segmented text
132 */
133 function segment( $text ) {
134 $len = strlen( $text );
135 $q = "SEG $len\n$text";
136 $result = $this->query( $q );
137 if ( !$result ) { // fallback to character based segmentation
138 $result = $this->segment( $text );
139 }
140 return $result;
141 }
142
143 /**
144 * Close the connection
145 */
146 function close() {
147 fclose( $this->mFP );
148 }
149 }