Add a way for packagers to override some installation details
[lhc/web/wiklou.git] / includes / ZhClient.php
1 <?php
2 /**
3 * Client for querying zhdaemon.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * Client for querying zhdaemon
25 */
26 class ZhClient {
27 var $mHost, $mPort, $mFP, $mConnected;
28
29 /**
30 * Constructor
31 *
32 * @param $host
33 * @param $port
34 *
35 * @return ZhClient
36 */
37 function __construct( $host, $port ) {
38 $this->mHost = $host;
39 $this->mPort = $port;
40 $this->mConnected = $this->connect();
41 }
42
43 /**
44 * Check if connection to zhdaemon is successful
45 *
46 * @return bool
47 */
48 function isconnected() {
49 return $this->mConnected;
50 }
51
52 /**
53 * Establish conncetion
54 *
55 * @access private
56 *
57 * @return bool
58 */
59 function connect() {
60 wfSuppressWarnings();
61 $errno = $errstr = '';
62 $this->mFP = fsockopen( $this->mHost, $this->mPort, $errno, $errstr, 30 );
63 wfRestoreWarnings();
64 return !$this->mFP;
65 }
66
67 /**
68 * Query the daemon and return the result
69 *
70 * @access private
71 *
72 * @return string
73 */
74 function query( $request ) {
75 if ( !$this->mConnected ) {
76 return false;
77 }
78
79 fwrite( $this->mFP, $request );
80
81 $result = fgets( $this->mFP, 1024 );
82
83 list( $status, $len ) = explode( ' ', $result );
84 if( $status == 'ERROR' ) {
85 // $len is actually the error code...
86 print "zhdaemon error $len<br />\n";
87 return false;
88 }
89 $bytesread = 0;
90 $data = '';
91 while( !feof( $this->mFP ) && $bytesread < $len ) {
92 $str = fread( $this->mFP, $len - $bytesread );
93 $bytesread += strlen( $str );
94 $data .= $str;
95 }
96 // data should be of length $len. otherwise something is wrong
97 return strlen( $data ) == $len;
98 }
99
100 /**
101 * Convert the input to a different language variant
102 *
103 * @param $text String: input text
104 * @param $tolang String: language variant
105 * @return string the converted text
106 */
107 function convert( $text, $tolang ) {
108 $len = strlen( $text );
109 $q = "CONV $tolang $len\n$text";
110 $result = $this->query( $q );
111 if ( !$result ) {
112 $result = $text;
113 }
114 return $result;
115 }
116
117 /**
118 * Convert the input to all possible variants
119 *
120 * @param $text String: input text
121 * @return array langcode => converted_string
122 */
123 function convertToAllVariants( $text ) {
124 $len = strlen( $text );
125 $q = "CONV ALL $len\n$text";
126 $result = $this->query( $q );
127 if ( !$result ) {
128 return false;
129 }
130 list( $infoline, $data ) = explode( '|', $result, 2 );
131 $info = explode( ';', $infoline );
132 $ret = array();
133 $i = 0;
134 foreach( $info as $variant ) {
135 list( $code, $len ) = explode( ' ', $variant );
136 $ret[strtolower( $code )] = substr( $data, $i, $len );
137 $i += $len;
138 }
139 return $ret;
140 }
141
142 /**
143 * Perform word segmentation
144 *
145 * @param $text String: input text
146 * @return string segmented text
147 */
148 function segment( $text ) {
149 $len = strlen( $text );
150 $q = "SEG $len\n$text";
151 $result = $this->query( $q );
152 if ( !$result ) { // fallback to character based segmentation
153 $result = $this->segment( $text );
154 }
155 return $result;
156 }
157
158 /**
159 * Close the connection
160 */
161 function close() {
162 fclose( $this->mFP );
163 }
164 }