* Added FileRepo::SKIP_LOCKING constant and made storeBatch() check it.
[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 return !$this->mFP;
45 }
46
47 /**
48 * Query the daemon and return the result
49 *
50 * @access private
51 *
52 * @return string
53 */
54 function query( $request ) {
55 if ( !$this->mConnected ) {
56 return false;
57 }
58
59 fwrite( $this->mFP, $request );
60
61 $result = fgets( $this->mFP, 1024 );
62
63 list( $status, $len ) = explode( ' ', $result );
64 if( $status == 'ERROR' ) {
65 // $len is actually the error code...
66 print "zhdaemon error $len<br />\n";
67 return false;
68 }
69 $bytesread = 0;
70 $data = '';
71 while( !feof( $this->mFP ) && $bytesread < $len ) {
72 $str = fread( $this->mFP, $len - $bytesread );
73 $bytesread += strlen( $str );
74 $data .= $str;
75 }
76 // data should be of length $len. otherwise something is wrong
77 return strlen( $data ) == $len;
78 }
79
80 /**
81 * Convert the input to a different language variant
82 *
83 * @param $text String: input text
84 * @param $tolang String: language variant
85 * @return string the converted text
86 */
87 function convert( $text, $tolang ) {
88 $len = strlen( $text );
89 $q = "CONV $tolang $len\n$text";
90 $result = $this->query( $q );
91 if ( !$result ) {
92 $result = $text;
93 }
94 return $result;
95 }
96
97 /**
98 * Convert the input to all possible variants
99 *
100 * @param $text String: input text
101 * @return array langcode => converted_string
102 */
103 function convertToAllVariants( $text ) {
104 $len = strlen( $text );
105 $q = "CONV ALL $len\n$text";
106 $result = $this->query( $q );
107 if ( !$result ) {
108 return false;
109 }
110 list( $infoline, $data ) = explode( '|', $result, 2 );
111 $info = explode( ';', $infoline );
112 $ret = array();
113 $i = 0;
114 foreach( $info as $variant ) {
115 list( $code, $len ) = explode( ' ', $variant );
116 $ret[strtolower( $code )] = substr( $data, $i, $len );
117 $i += $len;
118 }
119 return $ret;
120 }
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 }