coding style tweaks, doc updates, marked some public functions as such
[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 function isconnected() {
24 return $this->mConnected;
25 }
26
27 /**
28 * Establish conncetion
29 *
30 * @access private
31 */
32 function connect() {
33 wfSuppressWarnings();
34 $errno = $errstr = '';
35 $this->mFP = fsockopen( $this->mHost, $this->mPort, $errno, $errstr, 30 );
36 wfRestoreWarnings();
37 if ( !$this->mFP ) {
38 return false;
39 }
40 return true;
41 }
42
43 /**
44 * Query the daemon and return the result
45 *
46 * @access private
47 */
48 function query( $request ) {
49 if ( !$this->mConnected ) {
50 return false;
51 }
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 }
74 return $data;
75 }
76
77 /**
78 * Convert the input to a different language variant
79 *
80 * @param $text String: input text
81 * @param $tolang String: language variant
82 * @return string the converted text
83 */
84 function convert( $text, $tolang ) {
85 $len = strlen( $text );
86 $q = "CONV $tolang $len\n$text";
87 $result = $this->query( $q );
88 if ( !$result ) {
89 $result = $text;
90 }
91 return $result;
92 }
93
94 /**
95 * Convert the input to all possible variants
96 *
97 * @param $text String: input text
98 * @return array langcode => converted_string
99 */
100 function convertToAllVariants( $text ) {
101 $len = strlen( $text );
102 $q = "CONV ALL $len\n$text";
103 $result = $this->query( $q );
104 if ( !$result ) {
105 return false;
106 }
107 list( $infoline, $data ) = explode( '|', $result, 2 );
108 $info = explode( ';', $infoline );
109 $ret = array();
110 $i = 0;
111 foreach( $info as $variant ) {
112 list( $code, $len ) = explode( ' ', $variant );
113 $ret[strtolower( $code )] = substr( $data, $i, $len );
114 $i += $len;
115 }
116 return $ret;
117 }
118 /**
119 * Perform word segmentation
120 *
121 * @param $text String: input text
122 * @return string segmented text
123 */
124 function segment( $text ) {
125 $len = strlen( $text );
126 $q = "SEG $len\n$text";
127 $result = $this->query( $q );
128 if ( !$result ) { // fallback to character based segmentation
129 $result = $this->segment( $text );
130 }
131 return $result;
132 }
133
134 /**
135 * Close the connection
136 */
137 function close() {
138 fclose( $this->mFP );
139 }
140 }