(bug 33321. Sort of) Adding a line to MediaWiki:Sidebar that contains a pipe, but...
[mediawiki.git] / includes / ZhClient.php
blobd3d79165397bfe56afe8a9a50c94ed095a6ce4b1
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 return !$this->mFP;
47 /**
48 * Query the daemon and return the result
50 * @access private
52 * @return string
54 function query( $request ) {
55 if ( !$this->mConnected ) {
56 return false;
59 fwrite( $this->mFP, $request );
61 $result = fgets( $this->mFP, 1024 );
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;
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;
76 // data should be of length $len. otherwise something is wrong
77 return strlen( $data ) == $len;
80 /**
81 * Convert the input to a different language variant
83 * @param $text String: input text
84 * @param $tolang String: language variant
85 * @return string the converted text
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;
94 return $result;
97 /**
98 * Convert the input to all possible variants
100 * @param $text String: input text
101 * @return array langcode => converted_string
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;
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;
119 return $ret;
123 * Perform word segmentation
125 * @param $text String: input text
126 * @return string segmented text
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 );
135 return $result;
139 * Close the connection
141 function close() {
142 fclose( $this->mFP );