Mark userLink, userTalkLink, blockLink() and emailLink() as public static per CR...
[mediawiki.git] / includes / ZhClient.php
blob8bb36b231a897161d7efc83526ef1b9a5e2a3566
1 <?php
3 /**
4 * Client for querying zhdaemon
5 */
6 class ZhClient {
7 var $mHost, $mPort, $mFP, $mConnected;
9 /**
10 * Constructor
12 * @param $host
13 * @param $port
15 * @return ZhClient
17 function __construct( $host, $port ) {
18 $this->mHost = $host;
19 $this->mPort = $port;
20 $this->mConnected = $this->connect();
23 /**
24 * Check if connection to zhdaemon is successful
26 * @return bool
28 function isconnected() {
29 return $this->mConnected;
32 /**
33 * Establish conncetion
35 * @access private
37 * @return bool
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;
47 return true;
50 /**
51 * Query the daemon and return the result
53 * @access private
55 * @return string
57 function query( $request ) {
58 if ( !$this->mConnected ) {
59 return false;
62 fwrite( $this->mFP, $request );
64 $result = fgets( $this->mFP, 1024 );
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;
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;
79 // data should be of length $len. otherwise something is wrong
80 if ( strlen( $data ) != $len ) {
81 return false;
83 return $data;
86 /**
87 * Convert the input to a different language variant
89 * @param $text String: input text
90 * @param $tolang String: language variant
91 * @return string the converted text
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;
100 return $result;
104 * Convert the input to all possible variants
106 * @param $text String: input text
107 * @return array langcode => converted_string
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;
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;
125 return $ret;
128 * Perform word segmentation
130 * @param $text String: input text
131 * @return string segmented text
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 );
140 return $result;
144 * Close the connection
146 function close() {
147 fclose( $this->mFP );